def __init__(self, gameid=None, strat1=None, strat2=None, owner=None):
   self.stdoutdata = None
   self.stderrdata = None
   if gameid == None:
     # TODO: There should be some limit for games opened in parallel
     assert strat1 != None
     assert strat2 != None
     assert owner != None
     self.round = 0
     self.owner = owner
     # Deploy files into game dir
     self.gamedir = tempfile.mkdtemp()
     self.gameid = os.path.basename(self.gamedir)
     shutil.copy(path.data_path('redbot'), self.gamedir)
     self.strat1 = strategies.Strategy(strat1)
     self.strat1.deploy(self.gamedir, self.strat1id)
     self.strat2 = strategies.Strategy(strat2)
     self.strat2.deploy(self.gamedir, self.strat2id)
     os.mkdir(os.path.join(self.gamedir, 'replay'))
     # Create INI file with details
     self.dump_ini()
     # Add a line into global CSV file
     games_csv = csv.writer(open(path.data_path('games.csv'),'a'))
     games_csv.writerow((self.gameid, self.owner))
   else:
     assert strat1 == None
     assert strat2 == None
     assert owner == None
     # Game already exists
     self.gameid = gameid
     # Load details from INI file
     self.load_ini()
def delete_stategy(label, username):
    strats = get_strategies()
    new = []
    for s in strats:
        if s["label"] != label:
            new.append(s)
        else:
            if s["author"] != username:
                raise Exception("You cant do this.")
            else:
                os.remove(path.data_path("strategies/" + s["label"]))
    writer = csv.writer(open(path.data_path("strategies.csv"), "w"))
    for s in new:
        writer.writerow([s["label"], s["name"], s["is_automated"], s["author"]])
def add_strategy(name, strat, author):
    strategies_csv = csv.reader(open(path.data_path("strategies.csv"), "r"))
    if not name or not strat or not author:
        raise Exception("Bad data.")
    exists = False
    for row in strategies_csv:
        if row[0] == name and row[3] != author:
            raise Exception("Strategy {0} is already used by {1}.".format(name, row[3]))
        if row[0] == name and row[3] == author:
            exists = True
    if not exists:
        strategies_csv = csv.writer(open(path.data_path("strategies.csv"), "a"))
        strategies_csv.writerow([name, name, "1", author])
    print >> open(path.data_path("strategies/" + name), "w"), strat,
    subprocess.call(["chmod", "+x", path.data_path("strategies/" + name)])
 def delete(self, target=None):
   """Delete given user. If user not provided (or set to None) delete
   yourself. Only admin can delete other users."""
   if not target:
     target=self.username
   strats=strategies.get_users_strategies(target)
   for s in strats:
     strategies.delete_stategy(s['label'], target)
   users_csv = csv.reader(open(path.data_path('users.csv'),'r'))
   new=[]
   for u in users_csv:
     if u[0]!=target:
       new.append(u)
   users_csv=csv.writer(open(path.data_path('users.csv'),'w'))
   users_csv.writerows(new)
 def add_user(self, password, is_admin=0):
   # First check user do not exist
   users_csv = csv.reader(open(path.data_path('users.csv'),'r'))
   user_present = False
   for row in users_csv:
     if row[0] == self.username:
       user_present = True
       break
   if user_present:
     raise Exception('User %s already exists' % self.username)
   # Calculate username:password hash
   self.base64str = base64.b64encode('%s:%s' % (self.username, password))
   # Remember is_admin
   self.is_admin = is_admin
   # Add the user
   users_csv = csv.writer(open(path.data_path('users.csv'),'a'))
   users_csv.writerow((self.username, self.base64str, self.is_admin))
Example #6
0
def _remove_old_user_settings():
    settings = sublime.load_settings(COLOR_HIGHLIGHTER_SETTINGS_NAME)  # pylint: disable=assignment-from-none
    if settings.get("channels", None) is None:
        return
    user_settings_path = os.path.join(path.packages_path(path.ABSOLUTE), "User", COLOR_HIGHLIGHTER_SETTINGS_NAME)
    os.remove(user_settings_path)
    shutil.rmtree(path.data_path(path.ABSOLUTE))
    if Settings(sublime.load_settings(COLOR_HIGHLIGHTER_SETTINGS_NAME)).debug:
        print("ColorHighlighter: action=remove_old_settings")
Example #7
0
def _remove_old_user_settings():
    settings = sublime.load_settings(COLOR_HIGHLIGHTER_SETTINGS_NAME)  # pylint: disable=assignment-from-none
    if settings.get("channels", None) is None:
        return
    user_settings_path = os.path.join(path.packages_path(path.ABSOLUTE), "User", COLOR_HIGHLIGHTER_SETTINGS_NAME)
    os.remove(user_settings_path)
    shutil.rmtree(path.data_path(path.ABSOLUTE))
    if Settings(sublime.load_settings(COLOR_HIGHLIGHTER_SETTINGS_NAME)).debug:
        print("ColorHighlighter: action=remove_old_settings")
 def __init__(self, strategie_name):
     self.label = strategie_name
     self.name = None
     self.is_automated = None
     self.description = None
     # TODO: support strategies which consist of many files
     assert os.path.isfile(path.data_path("strategies", self.label))
     self.file = path.data_path("strategies", self.label)
     strategies_csv = csv.reader(open(path.data_path("strategies.csv"), "r"))
     for row in strategies_csv:
         if row[0] == self.label:
             self.label = row[0]
             self.name = row[1]
             self.is_automated = int(row[2])
             self.author = row[3]
             break
     assert self.name != None
     assert self.is_automated != None
     assert self.author != None
 def load_details(self):
   user_present = False
   users_csv = csv.reader(open(path.data_path('users.csv'),'r'))
   for row in users_csv:
     if row[0] == self.username:
       self.base64str = row[1]
       self.is_admin = int(row[2])
       user_present = True
       break
   if not user_present:
     raise Exception('User %s do not exists' % username)
def _init_color_picker():
    _create_if_not_exists(path.data_path(path.ABSOLUTE))
    _create_if_not_exists(path.color_picker_path(path.ABSOLUTE))
    color_picker_file = path.color_picker_file(path.ABSOLUTE)
    color_picker_resource = path.color_picker_binary(path.RELATIVE)
    if (os.path.exists(color_picker_file) and
            os.path.getsize(color_picker_file) == get_binary_resource_size(color_picker_resource)):
        return

    copy_resource(color_picker_resource, color_picker_file)
    chmod_flags = stat.S_IXUSR | stat.S_IXGRP | stat.S_IRUSR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IWGRP
    os.chmod(color_picker_file, chmod_flags)
Example #11
0
def _init_color_picker():
    _create_if_not_exists(path.data_path(path.ABSOLUTE))
    _create_if_not_exists(path.color_picker_path(path.ABSOLUTE))
    color_picker_file = path.color_picker_file(path.ABSOLUTE)
    color_picker_resource = path.color_picker_binary(path.RELATIVE)
    if (os.path.exists(color_picker_file)
            and os.path.getsize(color_picker_file)
            == get_binary_resource_size(color_picker_resource)):
        return

    copy_resource(color_picker_resource, color_picker_file)
    chmod_flags = stat.S_IXUSR | stat.S_IXGRP | stat.S_IRUSR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IWGRP
    os.chmod(color_picker_file, chmod_flags)
    def _run_command(self, command):
        _create_if_not_exists(path.data_path(path.ABSOLUTE))
        _create_if_not_exists(path.icons_path(path.ABSOLUTE))
        process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
        try:
            output, error = process.communicate(timeout=self._execute_timeout_seconds)
        except subprocess.TimeoutExpired:
            process.kill()
            output, error = process.communicate()

        output = _decode_data(output)
        error = _decode_data(error)

        if error is not None and error != "":
            print("Traceback: error.\n\nOutput:\n%s\n\nError:\n%s" % (output, error))
            return False
        return True
    def _run_command(self, command):
        _create_if_not_exists(path.data_path(path.ABSOLUTE))
        _create_if_not_exists(path.icons_path(path.ABSOLUTE))
        process = subprocess.Popen(command,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE,
                                   shell=True)
        try:
            output, error = process.communicate(
                timeout=self._execute_timeout_seconds)
        except subprocess.TimeoutExpired:
            process.kill()
            output, error = process.communicate()

        output = _decode_data(output)
        error = _decode_data(error)

        if error is not None and error != "":
            print("Traceback: error.\n\nOutput:\n%s\n\nError:\n%s" %
                  (output, error))
            return False
        return True
def init_color_scheme_dir():
    """Initialise the directory for color schemes."""
    _create_if_not_exists(path.data_path(path.ABSOLUTE))
    _create_if_not_exists(path.themes_path(path.ABSOLUTE))
def init_color_scheme_dir():
    """Initialise the directory for color schemes."""
    _create_if_not_exists(path.data_path(path.ABSOLUTE))
    _create_if_not_exists(path.themes_path(path.ABSOLUTE))
def get_strategies():
    out = []
    strategies_csv = csv.reader(open(path.data_path("strategies.csv"), "r"))
    for row in strategies_csv:
        out.append({"label": row[0], "name": row[1], "is_automated": int(row[2]), "author": row[3]})
    return out