Example #1
0
 def exit_if_path_exists(self):
     """
     Exit early if the path cannot be found.
     """
     if os.path.exists(self.output_path):
         ui.error(c.MESSAGES["path_exists"], self.output_path)
         sys.exit(1)
Example #2
0
 def exit_if_path_exists(self):
     """
     Exit early if the path cannot be found.
     """
     if os.path.exists(self.output_path):
         ui.error(c.MESSAGES["path_exists"], self.output_path)
         sys.exit(1)
 def on_error(self, status_code):
     if status_code == 420:
         ui.error("Calm down! Error code:", status_code)
         return False
     else:
         ui.error(status_code)
         return True
def fetch_tweets():
    """This will start collection and will store them in the database.

    //DONE: Ask the user to change filter keyword value.
    //TODO bug: counter resets on disconnection so it starts gathering x tweets
    on top of whatever it gathered before.
     """
    try:
        trump = change_filter()  # A variable named trump
        myStreamListener = MyStreamListener()
        myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener)
        myStream.filter(track=[trump])
        myStream.disconnect()
    except sqlite3.InterfaceError as e:
        ui.info_3(e)
        ui.error("Connection timeout... Reconnecting...")
        myStreamListener = MyStreamListener()
        myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener)
        myStream.filter(track=[trump])
        myStream.disconnect()
    except urlib3.exceptions.ProtocolError as e:
        ui.info_3(e)
        ui.error("Connection broken... Reconnecting...")
        myStreamListener = MyStreamListener()
        myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener)
        myStream.filter(track=[trump])
        myStream.disconnect()
Example #5
0
    def make_request(self,
                     url,
                     cf=False,
                     meth="GET",
                     timeout=30,
                     redirs=True,
                     data=None,
                     params=None):
        if cf is False:
            try:
                response = requests.request(url=url,
                                            headers=self.headers,
                                            method=meth,
                                            timeout=timeout,
                                            allow_redirects=redirs,
                                            data=data,
                                            params=params)
                ui.debug(ui.purple, "REQUEST", response.url, ui.bold,
                         response.status_code)

            except Exception as ex:
                ui.error("Request could not be made for", self.email, ex)
        else:  # cf is True
            try:
                import cfscrape
                scraper = cfscrape.create_scraper()
                response = scraper.get(url)
            except Exception as ex:
                ui.error("Cloudflare bypass request could not be made for",
                         self.email, ex)

        if response.status_code == 429:
            ui.warning("Reached RATE LIMIT, sleeping", ui.purple, self.email)
            sleep(2.5)
        return response
Example #6
0
def status(publish_path):
    u''' 检查发布库的编译状态 '''
    publish_path, root_path = StaticPackage.get_roots(publish_path)
    if not publish_path:
        ui.error(u'不是发布库')
        return 1

    package = StaticPackage(root_path, publish_path)

    files = package.get_publish_files()
    for filename in files:
        filetype = os.path.splitext(filename)[1]

        source, mode = package.parse(filename)
        try:
            rfiles = package.get_relation_files(source, all = True)
        except PackageNotFoundException, e:
            ui.error(u'%s package not found' % e.url)
        else:
            modified, not_exists = package.listener.check(filename, rfiles)
            if len(modified) or len(not_exists):
                for modified_file in modified:
                    ui.msg('M ' + modified_file)

                for not_exists_file in not_exists:
                    ui.msg('! ' + not_exists_file)
Example #7
0
def main():
    try:
        meta, settings, profile, name, port = setup()

        while True:
            data = ui.main(meta, settings)

            if data is None:
                break

            if data['local']:
                # Local Server
                server_obj = server_interface.LocalInterface(name, data['save'], port)
            else:
                # Remote Server
                server_obj = server_interface.RemoteInterface(name, data['ip'], data['port'])

            if not server_obj.error:
                if profile:
                    cProfile.runctx('game(server_obj, settings)', globals(), locals(), filename='game.profile')
                else:
                    game(server_obj, settings)

            if server_obj.error:
                ui.error(server_obj.error)

    finally:
        setdown()
Example #8
0
def exit_if_path_not_found(path):
    """
    Exit if the path is not found.
    """
    if not os.path.exists(path):
        ui.error(c.MESSAGES["path_missing"], path)
        sys.exit(1)
Example #9
0
def main():
    settings = None
    try:
        meta, settings, profile, debug, benchmarks, name, port = setup()

        while True:
            data = ui.main(meta, settings)

            if data is None:
                break

            if data['local']:
                # Local Server
                server_obj = server_interface.LocalInterface(name, data['save'], port, settings)
            else:
                # Remote Server
                server_obj = server_interface.RemoteInterface(name, data['ip'], data['port'])

            if not server_obj.error:
                render_interface.setup_render_module(settings)

                if profile:
                    cProfile.runctx('game(server_obj, settings, benchmarks)', globals(), locals(), filename='game.profile')
                elif debug:
                    pdb.run('game(server_obj, settings, benchmarks)', globals(), locals())
                else:
                    game(server_obj, settings, benchmarks)

            if server_obj.error:
                ui.error(server_obj.error)

    finally:
        setdown()
Example #10
0
def link(path, link_path, force = False):
    u''' 将发布库与源库进行映射

如果库设置了url,则同时使用.package文件进行连接,需要工作区支持,如果没有url,则只进行本地连接。'''

    publish_path, root_path = StaticPackage.get_roots(path)
    if not publish_path and not root_path and link_path:
        publish_path, root_path = StaticPackage.get_roots(link_path)
        path, link_path = link_path, path

    if not publish_path:
        publish_path = os.path.realpath(link_path)
    else:
        root_path = os.path.realpath(link_path)

    if not root_path:
        ui.error('package not found')

    package = StaticPackage(root_path, publish_path = publish_path)

    if not os.path.exists(publish_path):
        if force:
            os.makedirs(publish_path)
        else:
            ui.msg(u'%s path not exists, run opm link path -f to create it.' % publish_path)
            return 1

    package.link()

    ui.msg(u'linked publish %s to %s' % (publish_path, root_path))
Example #11
0
def serve(workspace_path, fastcgi = False, port = 8080, debug = False, noload = False, hg = False, hg_port = 8000):
    u''' 启动一个可实时编译的静态服务器

请指定工作区路径'''

    if Workspace.is_root(workspace_path):
        workspace = Workspace(os.path.realpath(workspace_path))
        if not noload:
            load(workspace = workspace)
    else:
        ui.error(u'工作区无效');
        workspace = None

    def print_request(environ, start_response):
        ''' 输出fastcgi本次请求的相关信息 '''

        import cgi
        start_response('200 OK', [('Content-Type', 'text/html')])
        yield '<html><head><title>Hello World!</title></head>\n' \
              '<body>\n' \
              '<p>Hello World!</p>\n' \
              '<table border="1">'
        names = environ.keys()
        names.sort()
        for name in names:
            yield '<tr><td>%s</td><td>%s</td></tr>\n' % (
                name, cgi.escape(`environ[name]`))

        form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ,
                                keep_blank_values=1)
        if form.list:
            yield '<tr><th colspan="2">Form data</th></tr>'

        for field in form.list:
            yield '<tr><td>%s</td><td>%s</td></tr>\n' % (
                field.name, field.value)

        yield '</table>\n' \
              '</body></html>\n'

    def listen(environ, start_response):
        ''' 监听请求 '''
        if environ['DOCUMENT_URI'].endswith('/net.test'):
            return print_request(environ, start_response)

        DEBUG = debug

        filename = os.path.realpath(environ['REQUEST_FILENAME'])
        url = environ['DOCUMENT_URI']

        force = False # 是否强制重新编译
        # 没有 referer 时强制重新编译
        if not 'HTTP_REFERER' in environ.keys():
            force = True

        try:
            publish_path, root_path = StaticPackage.get_roots(filename, workspace = workspace)
        except PackageNotFoundException, e:
            ui.error(u'%s package not found' % e.url)
        else:
Example #12
0
def exit_if_path_not_found(path):
    """
    Exit if the path is not found.
    """
    if not os.path.exists(path):
        ui.error(c.MESSAGES["path_missing"], path)
        sys.exit(1)
Example #13
0
    def exit_if_missing_graphviz(self):
        """
        Detect the presence of the dot utility to make a png graph.
        """
        (out, err) = utils.capture_shell("which dot")

        if "dot" not in out:
            ui.error(c.MESSAGES["dot_missing"])
Example #14
0
 def login(self):
     content = open('config.json').read()
     config = json.loads(content)
     result = rh.login(config['email'], config["password"])
     Client.current_user_id = config['email']
     if result is not None and result[
             "detail"] != "logged in using authentication in robinhood.pickle":
         ui.error(result)
Example #15
0
    def exit_if_missing_graphviz(self):
        """
        Detect the presence of the dot utility to make a png graph.
        """
        (out, err) = utils.capture_shell("which dot")

        if "dot" not in out:
            ui.error(c.MESSAGES["dot_missing"])
Example #16
0
 def handle_errors(self):
     ui.error(self.task.description(), "failed")
     for item, error in self.errors:
         item_desc = self.task.display_item(item)
         message = [ui.green, "*", " ", ui.reset, ui.bold, item_desc]
         if error.message:
             message.extend([ui.reset, ": ", error.message])
         ui.info(*message, sep="")
     raise ExecutorFailed()
Example #17
0
 def display_bad_branches(self):
     if not self.bad_branches:
         return
     ui.error("Some projects were not on the correct branch")
     headers = ("project", "actual", "expected")
     data = [((ui.bold, name), (ui.red, actual), (ui.green, expected))
             for (name, actual, expected) in self.bad_branches]
     ui.info_table(data, headers=headers)
     raise BadBranches()
Example #18
0
def file_to_string(path):
    """
    Return the contents of a file when given a path.
    """
    if not os.path.exists(path):
        ui.error(c.MESSAGES["path_missing"], path)
        sys.exit(1)

    with codecs.open(path, "r", "UTF-8") as contents:
        return contents.read()
Example #19
0
    def validate_format(self, allowed_formats):
        """
        Validate the allowed formats for a specific type.
        """
        if self.format in allowed_formats:
            return

        ui.error("Export type '{0}' does not accept '{1}' format, only: "
                 "{2}".format(self.type, self.format, allowed_formats))
        sys.exit(1)
Example #20
0
    def validate_format(self, allowed_formats):
        """
        Validate the allowed formats for a specific type.
        """
        if self.format in allowed_formats:
            return

        ui.error("Export type '{0}' does not accept '{1}' format, only: "
                 "{2}".format(self.type, self.format, allowed_formats))
        sys.exit(1)
Example #21
0
def file_to_string(path):
    """
    Return the contents of a file when given a path.
    """
    if not os.path.exists(path):
        ui.error(c.MESSAGES["path_missing"], path)
        sys.exit(1)

    with open(path, "r") as contents:
        return contents.read()
Example #22
0
def url_to_string(url):
    """
    Return the contents of a web site url as a string.
    """
    try:
        page = urllib2.urlopen(url)
    except (urllib2.HTTPError, urllib2.URLError) as err:
        ui.error(c.MESSAGES["url_unreachable"], err)
        sys.exit(1)

    return page
Example #23
0
def url_to_string(url):
    """
    Return the contents of a web site url as a string.
    """
    try:
        page = urllib2.urlopen(url)
    except (urllib2.HTTPError, urllib2.URLError) as err:
        ui.error(c.MESSAGES["url_unreachable"], err)
        sys.exit(1)

    return page
Example #24
0
def mkdir_p(path):
    """
    Emulate the behavior of mkdir -p.
    """
    try:
        os.makedirs(path)
    except OSError as err:
        if err.errno == errno.EEXIST and os.path.isdir(path):
            pass
        else:
            ui.error(c.MESSAGES["path_unmakable"], err)
            sys.exit(1)
Example #25
0
def file_to_list(path):
    """
    Return the contents of a file as a list when given a path.
    """
    if not os.path.exists(path):
        ui.error(c.MESSAGES["path_missing"], path)
        sys.exit(1)

    with open(path, "r") as contents:
        lines = contents.read().splitlines()

    return lines
Example #26
0
 def wrapped(args=None):
     colored_traceback.add_hook()
     try:
         main_func(args=args)
     except tsrc.Error as e:
         # "expected" failure, display it and exit
         if e.message:
             ui.error(e.message)
         sys.exit(1)
     except KeyboardInterrupt:
         ui.warning("Interrupted by user, quitting")
         sys.exit(1)
Example #27
0
def file_to_list(path):
    """
    Return the contents of a file as a list when given a path.
    """
    if not os.path.exists(path):
        ui.error(c.MESSAGES["path_missing"], path)
        sys.exit(1)

    with codecs.open(path, "r", "UTF-8") as contents:
        lines = contents.read().splitlines()

    return lines
Example #28
0
def mkdir_p(path):
    """
    Emulate the behavior of mkdir -p.
    """
    try:
        os.makedirs(path)
    except OSError as err:
        if err.errno == errno.EEXIST and os.path.isdir(path):
            pass
        else:
            ui.error(c.MESSAGES["path_unmakable"], err)
            sys.exit(1)
Example #29
0
def sell(quantity, symbol, limit):
    if limit is not None:
        ui.success("Selling {} of {} at {}".format(quantity, symbol, limit))
        result = rh.order_sell_limit(symbol, quantity, limit)
    else:
        # market order
        ui.success("Selling {} of {} at market price".format(quantity, symbol))
        result = rh.order_sell_market(symbol, quantity)
    if 'ref_id' in result:
        ui.success(result)
    else:
        ui.error(result)
Example #30
0
    def trade_on_fear_and_greed(self, current_fear_greed_index):

        investors_are_greedy = current_fear_greed_index >= fear_greed.sell_threshold
        investors_are_fearful = current_fear_greed_index <= fear_greed.buy_threshold
        owns_spy = fear_greed.owns_spy()
        current_date_time = datetime.now().strftime("%m/%d/%Y %H:%M:%S")

        if investors_are_greedy and owns_spy:
            # Sell entire SPY position
            price = fear_greed.get_sell_equity_amount()
            result = rh.order_sell_fractional_by_price(fear_greed.symbol,
                                                       price,
                                                       extendedHours=True,
                                                       timeInForce="gfd")

            if result is not None and 'account' in result.keys():
                TradeHistory.update_trade_history(fear_greed.algo,
                                                  current_fear_greed_index,
                                                  fear_greed.symbol, price,
                                                  "sell", current_date_time,
                                                  Client.current_user_id)
            else:
                ui.error(result)
        elif investors_are_fearful and not owns_spy:
            # Buy initial SPY investment or the last sold equity
            price = fear_greed.get_buy_equity_amount(
                TradeHistory.trade_history['trades'])
            result = rh.order_buy_fractional_by_price(fear_greed.symbol,
                                                      price,
                                                      extendedHours=True,
                                                      timeInForce="gfd")

            if result is not None and 'account' in result.keys():
                TradeHistory.update_trade_history(fear_greed.algo,
                                                  current_fear_greed_index,
                                                  fear_greed.symbol, price,
                                                  "buy", current_date_time,
                                                  Client.current_user_id)
            else:
                ui.error(result)
        else:
            # Skip SPY trade because there is not yet a significant fear or greed value
            skipped_dict = {
                "algo": fear_greed.algo,
                "index": current_fear_greed_index,
                "action": "skipped",
                "date": current_date_time,
                "price": "N/A",
                "symbol": "N/A",
                "user_id": Client.current_user_id
            }
            ui.success(skipped_dict)
            return
Example #31
0
def main():
    ui.info_1("Starting CI")
    checks = init_checks()
    for check in checks:
        check.run()
    failed_checks = [check for check in checks if not check.ok]
    if not failed_checks:
        ui.info(ui.green, "CI passed")
        return
    for check in failed_checks:
        ui.error(check.name, "failed")
    sys.exit(1)
def create_conection(db_file=TWEETS_DB):
    """Connects to the database.
    :param db_file: database file, default tweets_db
    :return conn: returns connection object

    This code was taken from SQLite Tutorial, http://www.sqlitetutorial.net/sqlite-python/creating-database/ (2018) accessed on 22-Nov-2018
    """

    try:
        conn = sqlite3.connect(db_file)
        return conn
    except:
        ui.error()
    return None
Example #33
0
def buy(quantity, symbol, limit=None):
    content = open('config.json').read()
    config = json.loads(content)
    rh.login(config['username'], config['password'])

    if limit is not None:
        ui.success("buying {} of {} at ${}".format(quantity, symbol, limit))
        result = rh.order_buy_limit(symbol, quantity, limit)
    else:
        ui.success("buying {} of {}".format(quantity, symbol))
        result = rh.order_buy_market(symbol, quantity)
    if 'detail' in result:
        ui.error(result)
    else:
        ui.success(result)
Example #34
0
def yaml_load(path, input="", err_quit=False):
    """
    Return a yaml dict from a file or string with error handling.
    """
    try:
        if len(input) > 0:
            return yaml.load(input)
        else:
            return yaml.load(file_to_string(path))
    except Exception as err:
        file = os.path.basename(path)
        ui.error("", c.MESSAGES["yaml_error"].replace("%file", file), err, "")

        if err_quit:
            sys.exit(1)

        return False
Example #35
0
def display_statuses(statuses, errors):
    if not statuses:
        return
    max_src = max((len(x.src) for x in statuses))
    for status in statuses:
        message = (ui.green, "*", ui.reset, ui.bold, status.src.ljust(max_src),
                   ui.reset, ui.green, status.branch)
        if status.dirty:
            message = message + (ui.reset, ui.brown, "(dirty)")
        ui.info(*message)

    if errors:
        ui.info()
        ui.error("Errors when getting branch")
        for src, error in errors:
            ui.info("*", ui.bold, src, ui.reset, error.output)
        ui.info()
Example #36
0
    def graph_png(self):
        """
        Export a graph of the data in png format using graphviz/dot.
        """
        if not self.out_file:
            ui.error(c.MESSAGES["png_missing_out"])
            sys.exit(1)

        cli_flags = "-Gsize='{0}' -Gdpi='{1}' {2} ".format(self.size, self.dpi,
                                                           self.flags)
        cli_flags += "-o {0}".format(self.out_file)

        (out, err) = utils.capture_shell(
            "ansigenome export -t graph -f dot | dot -Tpng {0}"
            .format(cli_flags))

        if err:
            ui.error(err)
Example #37
0
    def graph_png(self):
        """
        Export a graph of the data in png format using graphviz/dot.
        """
        if not self.out_file:
            ui.error(c.MESSAGES["png_missing_out"])
            sys.exit(1)

        cli_flags = "-Gsize='{0}' -Gdpi='{1}' {2} ".format(
            self.size, self.dpi, self.flags)
        cli_flags += "-o {0}".format(self.out_file)

        (out, err) = utils.capture_shell(
            "ansigenome export -t graph -f dot | dot -Tpng {0}".format(
                cli_flags))

        if err:
            ui.error(err)
Example #38
0
def yaml_load(path, input="", err_quit=False):
    """
    Return a yaml dict from a file or string with error handling.
    """
    try:
        if len(input) > 0:
            return yaml.load(input)
        elif len(path) > 0:
            return yaml.load(file_to_string(path))
    except Exception as err:
        file = os.path.basename(path)
        ui.error("",
                 c.MESSAGES["yaml_error"].replace("%file", file), err,
                 "")

        if err_quit:
            sys.exit(1)

        return False
Example #39
0
def packages(workspace_path, show_url = False):
    u''' 本工作区中所有源库 '''

    if os.path.isfile(workspace_path):
        workspace_path = os.path.dirname(workspace_path)

    # 有可能不是workspace跟路径,而是某个子路径
    workspace_path = Workspace.get_workspace(workspace_path)
    if not workspace_path:
        ui.error(u'没有工作区')
        return 1
    else:
        workspace = Workspace(workspace_path)
        if show_url:
            for url in workspace.url_packages.keys():
                ui.msg(url)
        else:
            for local_path in workspace.local_packages.keys():
                ui.msg(os.path.realpath(os.path.join(workspace.root, local_path)))
Example #40
0
def query_iex_history(symbol, current_date, three_months_ago):
    content = open('config.json').read()
    config = json.loads(content)
    if three_months_ago < current_date:
        df = pd.DataFrame(
            get_historical_data(symbol,
                                three_months_ago,
                                current_date,
                                output_format='pandas',
                                token=config["iex_api_key"]))
        update_df_with_macd(df)
    else:
        df = np.Dataframe()
        ui.error("Invalid start date {} and end date {}".format(
            current_date, three_months_ago))
    extra_columns = ["open", "high", "low", "volume"]
    df = df.drop(columns=extra_columns)
    pd.to_pickle(df, "./watchlist_history/{}_history.pkl".format(symbol))
    return df
Example #41
0
    def listen(environ, start_response):
        ''' 监听请求 '''
        if environ['DOCUMENT_URI'].endswith('/net.test'):
            return print_request(environ, start_response)

        DEBUG = debug

        filename = os.path.realpath(environ['REQUEST_FILENAME'])
        url = environ['DOCUMENT_URI']

        force = False # 是否强制重新编译
        # 没有 referer 时强制重新编译
        if not 'HTTP_REFERER' in environ.keys():
            force = True

        try:
            publish_path, root_path = StaticPackage.get_roots(filename, workspace = workspace)
        except PackageNotFoundException, e:
            ui.error(u'%s package not found' % e.url)
Example #42
0
def compile(filename, package = None, force = False, no_build_files = False):
    u'编译一个css/js文件'

    filename = os.path.realpath(filename)

    if not package:
        publish_path, root_path = StaticPackage.get_roots(filename)

        if not root_path:
            ui.error(u'没有找到源文件')
            return 1
        else:
            package = StaticPackage(root_path, publish_path)

    try:
        modified, not_exists = package.compile(filename, force = force)
    except IOError, e:
        ui.error('%s file not found' % e.filename)
        return 1
def tweets_table_create(
        conn=create_conection(), sql_file=TWEETS_DB, table_name=TABLE_NAME):
    """Create tweets table.
    :param:
    :return:
    This code was take from SQLite Tutorial,
    http://www.sqlitetutorial.net/sqlite-python/create-tables/ (2018), accessed on 22-Nov-2018
    It was modified to have {tn} instead of the table name, and added ui interface.
    """

    tweet_id = "tweets_id"
    tweet_text = "tweet_text"
    created_at = "created_at"
    location = "location"
    coordinates = "geocoordinates"
    user_followers = "user_followers"
    friends_no = "number_of_friends"
    senti = "sentiment_analysis"

    create_tweets_database = """CREATE TABLE IF NOT EXISTS {tn} (
    {ti} interger NOT NULL,
    {tt} text NOT NULL,
    {ct} text NOT NULL,
    {loc} text,
    {coor} text,
    {uf} interger,
    {fn} interger,
    {sn} text)""".format(tn=table_name,
                         ti=tweet_id,
                         tt=tweet_text,
                         ct=created_at,
                         loc=location,
                         coor=coordinates,
                         uf=user_followers,
                         fn=friends_no,
                         sn=senti)

    if conn is not None:
        create_table(conn, create_tweets_database)
        ui.info_1("New table created:", ui.blue, table_name)
    else:
        ui.error("Cannot create connection to DATABASE")
Example #44
0
def get(workspace, url):
    u''' 从公共代码库获取源库

 当前目录会被作为工作区,所有获取到的源库代码都会放到此工作区内
    '''

    if workspace.__class__ == str:
        try:
            workspace = Workspace(workspace)
        except ConfigError as e:
            ui.error(u'config error %s' % e.path)
            return 1

    load(workspace)

    try:
        packages = workspace.fetch_packages(url)
    except PackageNotFoundException, e:
        ui.error('%s package not found' % e.url)
        return 1
Example #45
0
def main() -> None:
    """
        Main loop of program. Terminates until either:
        1) 'q' input entered by user, or
        2) keyboard interrupt
    """
    while True:
        ui.mainloop()
        c = input()
        if valid_int(c):
            loc = db.get_location(c)
            if loc is not None:
                ui.print_location(loc)
            else:
                ui.error()
        else:
            if c.lower() == 'q':
                break
            else:
                continue
def create_table(conn, create_table_sql):
    """Creates a new table from create_table_sql
    :param conn: connection to database object
    :param create_table_sql: a CREATE TABLE statement
    :return:
    This code was take from SQLite Tutorial,
    http://www.sqlitetutorial.net/sqlite-python/create-tables/ (2018), accessed on 22-Nov-2018
    The except Error field was changed to OperationalError instead.
    """

    try:
        cursor = conn.cursor()
        cursor.execute(create_table_sql)
        conn.commit()
        conn.close()
    except sqlite3.OperationalError as e:
        ui.error(e)
        ui.error("Illegal name")
        ui.info(ui.bold, "Program Finished")
        exit()
Example #47
0
def incs(filename, all = False, reverse = False):
    u''' 某文件所有依赖的文件 '''

    filename = os.path.realpath(filename)
    root_path = StaticPackage.get_root(filename)
    package = StaticPackage(root_path)

    filetype = os.path.splitext(filename)[1]

    if reverse:
        if filetype == '.css':
            ui.error(u'Not support yet, sorry.')
            return 1
        else:
            files = package.get_included(filename, all = all)
    else:
        files = package.get_relation_files(filename, all = all)

    for file in files:
        ui.msg(file)
Example #48
0
def get_s_r(df):
    '''Loops through DF to determine and return clear support and resistance lines'''
    s = np.mean(df['high'] - df['low'])

    levels = []

    for i in range(2, df.shape[0] - 2):
        if is_support(df, i):
            l = df['low'][i]

            if np.sum([abs(l - x) < s for x in levels]) == 0:
                levels.append((i, l))
                ui.success(f'{l} | SUPPORT')

        elif is_resistance(df, i):
            l = df['high'][i]

            if np.sum([abs(l - x) < s for x in levels]) == 0:
                levels.append((i, l))

                ui.error(f'{l} | RESISTANCE')
Example #49
0
def main():
    settings = None
    try:
        meta, settings, profile, debug, benchmarks, name, port = setup()

        while True:
            data = ui.main(meta, settings)

            if data is None:
                break

            if data['local']:
                # Local Server
                server_obj = server_interface.LocalInterface(
                    name, data['save'], port, settings)
            else:
                # Remote Server
                server_obj = server_interface.RemoteInterface(
                    name, data['ip'], data['port'])

            if not server_obj.error:
                render_interface.setup_render_module(settings)

                if profile:
                    cProfile.runctx('game(server_obj, settings, benchmarks)',
                                    globals(),
                                    locals(),
                                    filename='game.profile')
                elif debug:
                    pdb.run('game(server_obj, settings, benchmarks)',
                            globals(), locals())
                else:
                    game(server_obj, settings, benchmarks)

            if server_obj.error:
                ui.error(server_obj.error)

    finally:
        setdown()
def user_ans_tweets(table_name=TABLE_NAME):
    """Asks user how many tweets to append to table, and does so.
    :param table_name: name of table
    """
    ui.info_1(ui.bold, "Add Tweets to", ui.blue, MyStreamListener.table_name)
    ans_int = input("   How many tweets? (interger) >>> ")
    MyStreamListener.tweet_stop = 0
    try:
        ans_int = int(ans_int)
        if ans_int > 0:
            MyStreamListener.tweet_stop = ans_int
            fetch_tweets()
        else:
            print("0 tweets gathered")
            return
    except ValueError:
        ui.info_1(ui.red, "Please write an interger")
        user_ans_tweets()
    except ConnectionError:
        ui.error(ui.cross, "Connection Error")
        ui.info(ui.bold, "Program Finished")
        exit()
Example #51
0
def load(workspace):
    u''' 加载本地工作区 '''

    if workspace.__class__ == str:
        workspace_path = Workspace.get_workspace(workspace)
        if not workspace_path:
            workspace_path = workspace

        try:
            workspace = Workspace(workspace_path)
        except ConfigError as e:
            ui.error(u'config error %s' % e.path)
            return 1

    old_count = len(workspace.local_packages)
    workspace.load()
    added_count = len(workspace.local_packages) - old_count

    if len(workspace.useless_packages):
        for package in workspace.useless_packages:
            ui.msg(u'删除无用package %s' % package)

    ui.msg(u'已加入 %s 个源库' % added_count)
Example #52
0
    def execute_command(self):
        """
        Execute the shell command.
        """
        stderr = ""
        role_count = 0
        for role in utils.roles_dict(self.roles_path):
            self.command = self.command.replace("%role_name", role)
            (_, err) = utils.capture_shell("cd {0} && {1}".
                                           format(os.path.join(
                                                  self.roles_path, role),
                                                  self.command))

            stderr = err
            role_count += 1

        utils.exit_if_no_roles(role_count, self.roles_path)

        if len(stderr) > 0:
            ui.error(c.MESSAGES["run_error"], stderr[:-1])
        else:
            if not self.config["options_quiet"]:
                ui.ok(c.MESSAGES["run_success"].replace(
                    "%role_count", str(role_count)), self.options.command)
Example #53
0
def init(root_path, publish_path = None, force = False):
    u''' 初始化一个新的库
    
初始化一个新的库,建立template-config.xml配置文件及常用的目录,如果指定了 -p 参数,还可以自动建立与发布目录的连接'''

    # 确保不要init了一个工作区
    if Workspace.get_workspace(root_path) == root_path:
        ui.error(u'工作区不能被初始化')
        return 1

    # 确保不要init了一个publish库
    if StaticPackage.get_publish(root_path):
        ui.error(u'发布目录不能被初始化')
        return 1

    ui.msg(u'初始化%s' % root_path)
    try:
        StaticPackage.init(root_path)
        ui.msg(u'创建配置文件')

    except PackageExistsException:
        ui.error(u'已经存在')
        return 1

    pathnames = ['test', 'doc', 'src', 'lib', 'res']
    for name in pathnames:
        path = os.path.join(root_path, name)
        if not os.path.exists(path):
            os.makedirs(path)
            ui.msg(u'生成默认目录 %s' % name)

    workspace_path = Workspace.get_workspace(root_path)
    if not workspace_path:
        ui.msg(u'没有工作区,请参照 opm help load')
    else:
        workspace = Workspace(workspace_path)

        if not workspace.has_package(root_path):
            workspace.add_package(root_path)
            ui.msg(u'加入本地工作区')
        else:
            ui.msg(u'本地工作区中已存在')

    ui.msg(u'成功!')

    if publish_path:
        link(root_path, publish_path, force = force)
Example #54
0
def libs(root_path, show_url = False, all = False, reverse = False):
    u''' 库的相关依赖库 '''
    root_path = StaticPackage.get_root(root_path)
    if not root_path:
        ui.error(u'不是源库')
        return 1

    package = StaticPackage(root_path)
    # 这个库没有设置url,不可能被别的库依赖
    if not package.url:
        ui.error(u'no url')
        return 1

    # 显示依赖自己的
    if reverse:
        try:
            libs = package.get_reverse_libs(all = all)
        except PackageNotFoundException, e:
            ui.error(u'%s package not found' % e.url)
            return 1
Example #55
0
def source(publish_path):
    u''' 映射的源库路径 '''
    if StaticPackage.get_publish(publish_path):
        StaticPackage.get_root(publish_path)
    else:
        ui.error(u'不是一个发布库')
Example #56
0
def main():
	print_banner()

	parser = argparse.ArgumentParser(description='Decode supplied shellcode automatically with the unicorn engine')
	parser.add_argument('-f', '--file', dest='file', help='file to shellcode binary file', required=False, type=file)
	parser.add_argument('-m', '--mode', dest='mode', help='mode of the emulator (--show-modes)', required=False, default="w86_32")
	parser.add_argument('-i', '--instructions', dest='max_instruction', help='max instructions to emulate', required=False)
	parser.add_argument('-d', '--debug', dest='debug', help='Enable extra hooks for debugging of shellcode', required=False, default=False, action='store_true')
	parser.add_argument('-o', '--output', dest='output', help='Where to write the decoded shellcode', required=False)
	parser.add_argument('-s', '--show-modes', dest='show', action='store_true', help='show available modes and exit', required=False)

	args = parser.parse_args()

	if args.show:
		for decoder in decoders:
			print(decoder)
		sys.exit(0)

	if not args.file or not args.mode or args.mode not in decoders:
		ui.error("bad commandline")
		sys.exit(0)

	bin_code = args.file.read()

	const = decoders[args.mode]
	cur_arch = const[0]
	cur_mode = const[1]
	stack_reg = const[2]
	stack_val = const[3]
	instr_ptr = const[4]
	cs_arch = const[5]
	cs_mode = const[6]

	PAGE_SIZE = 5 * 1024 * 1024
	START_RIP = 0x0

	disas_engine = SimpleEngine(cs_arch, cs_mode)

	# setup engine and write the memory there.
	emu = Uc(cur_arch, cur_mode)
	emu.disasm = disas_engine # python is silly but it works.
	emu.mem_map(0, PAGE_SIZE)
	# write machine code to be emulated to memory
	emu.mem_write(START_RIP, bin_code)

	# write a INT 0x3 near the end of the code blob to make sure emulation ends
	emu.mem_write(len(bin_code) + 0xff, "\xcc\xcc\xcc\xcc")

	emu.hook_add(UC_HOOK_MEM_INVALID, hook_mem_invalid)
	emu.hook_add(UC_HOOK_MEM_WRITE, hook_smc_check)
	emu.hook_add(UC_HOOK_INTR, hook_intr)
	
	if args.debug:
		emu.hook_add(UC_HOOK_MEM_READ, hook_mem_read)
		emu.hook_add(UC_HOOK_CODE, hook_code)

	# arbitrary address for ESP.
	emu.reg_write(stack_reg, stack_val)

	if args.max_instruction:
		end_addr = -1
	else:
		args.max_instruction = 0x1000
		end_addr = len(bin_code)

	try: 
		emu.emu_start(START_RIP, end_addr, 0, int(args.max_instruction))
	except UcError as e:
		ui.bug("%s" % e)

	if write_bounds[0] != None:
		ui.out("{{RED}}Shellcode address ranges:")
		ui.out("   low:  {{BLUE}}0x%X{{RED}}" % write_bounds[0])
		ui.out("   high: {{BLUE}}0x%X{{CLEAR}}\n\n" % write_bounds[1])
		ui.out("{{GREEN}}Decoded shellcode:{{CLEAR}}")
		mem = emu.mem_read(write_bounds[0], (write_bounds[1] - write_bounds[0]))
		emu.disasm.disas_all(str(mem), write_bounds[0])
		# Write the decoded shellcode
		if args.output:
			with open(args.output, "wb") as f:
				f.write(mem)
	else:
		ui.error("no SMC hits, no encoder detected")
Example #57
0
    if workspace.__class__ == str:
        try:
            workspace = Workspace(workspace)
        except ConfigError as e:
            ui.error(u'config error %s' % e.path)
            return 1

    load(workspace)

    try:
        packages = workspace.fetch_packages(url)
    except PackageNotFoundException, e:
        ui.error('%s package not found' % e.url)
        return 1
    except ConfigError as e:
        ui.error(u'config error %s' % e.path)
        return 1
    else:
        for package in packages:
            try:
                workspace.fetch(package)
            except ImportError, e:
                ui.error(u'mercurial module not found.')
                return 1
            except PackageExistsException, e:
                ui.error(u'%s package already exists' % e.root)

@cwdarg
@usage(u'opm workspace [源库路径]')
def workspace(root_path):
    u''' 源库所在工作区 '''
Example #58
0
def template(path, extend_path, out):
    """
    Return a jinja2 template instance with extends support.
    """
    files = []

    # add the "extender" template when it exists
    if len(extend_path) > 0:
        # determine the base readme path
        base_path = os.path.dirname(extend_path)
        new_base_path = os.path.join(base_path, "README.{0}.j2".format(out))

        if os.path.exists(new_base_path):
            path = new_base_path

        if os.path.exists(extend_path):
            files = [path, extend_path]
        else:
            ui.error(c.MESSAGES["template_extender_missing"])
            ui.error(extend_path)
            sys.exit(1)
    else:
        files = [path]

    try:
        # Use the subclassed relative environment class
        env = RelEnvironment(trim_blocks=True)

        # Add a unique dict filter, by key.
        # DEPRECATION WARNING: This is only used for backwards compatibility,
        #                      please use the unique filter instead.
        def unique_dict(items, key):
            return {v[key]: v for v in items}.values()

        env.filters["unique_dict"] = unique_dict

        def unique(a):

            # Don’t use the following commented out optimization which is used
            # in ansible/lib/ansible/plugins/filter/mathstuff.py in Ansigenome
            # as it resorts the role dependencies:
            # if isinstance(a,collections.Hashable):
            #     c = set(a)

            c = []
            for x in a:
                if x not in c:
                    c.append(x)
            return c

        env.filters["unique"] = unique

        # create a dictionary of templates
        templates = dict(
            (name, codecs.open(name, "rb", 'UTF-8').read())
            for name in files)
        env.loader = DictLoader(templates)

        # return the final result (the last template in the list)
        return env.get_template(files[len(files) - 1])
    except Exception as err:
        ui.error(c.MESSAGES["template_error"], err)
        sys.exit(1)