Rev 27 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 27 | magnus | 1 | Author: Magnus Holmgren <holmgren@debian.org> |
| 2 | Description: Mostly adapt server to run as a daemon |
||
| 2 | magnus | 3 | |
| 39 | magnus | 4 | --- a/pyscrabble/net/server.py |
| 5 | +++ b/pyscrabble/net/server.py |
||
| 6 | @@ -46,7 +46,7 @@ class ScrabbleServerFactory(protocol.Ser |
||
| 2 | magnus | 7 | self.db = db.DB() |
| 8 | self.maxUsersLoggedIn = 0 |
||
| 9 | self.startDate = util.Time(seconds=time.time(), dispDate=True) |
||
| 10 | - self.rankings = rank.Rankings( resources["config"][constants.RANK_CONFIG] ) |
||
| 11 | + self.rankings = rank.Rankings( resources["serverconfig"][constants.RANK_CONFIG] ) |
||
| 12 | |||
| 13 | dir = resources["resources"][constants.DICT_DIR].path |
||
| 14 | for lang in os.listdir( dir ): |
||
| 39 | magnus | 15 | --- a/pyscrabble/dist.py |
| 16 | +++ b/pyscrabble/dist.py |
||
| 17 | @@ -3,6 +3,8 @@ import os |
||
| 2 | magnus | 18 | import sys |
| 39 | magnus | 19 | from distutils.command.install_lib import install_lib |
| 20 | from distutils.command.install_scripts import install_scripts |
||
| 21 | +from distutils.command.install_data import install_data |
||
| 22 | +from distutils.command.install import install |
||
| 2 | magnus | 23 | |
| 24 | APP_NAME = 'pyscrabble' |
||
| 25 | |||
| 39 | magnus | 26 | @@ -31,8 +33,6 @@ except ImportError: |
| 2 | magnus | 27 | RESOURCE_PREFIX = 'resources' |
| 28 | |||
| 29 | CONFIG_DIR = get_app_data_dir() |
||
| 30 | -if not os.path.exists(CONFIG_DIR): |
||
| 31 | - CONFIG_DIR = os.path.join(RESOURCE_PREFIX, 'config') |
||
| 32 | |||
| 39 | magnus | 33 | def getLocaleDirs(dir, domain): |
| 34 | l = [] |
||
| 35 | @@ -76,6 +76,13 @@ def getDataFiles(): |
||
| 36 | getResourceDirs('resources/dict', True, 'resources') + \ |
||
| 37 | getResourceDirs('resources/letters', True, 'resources') |
||
| 38 | |||
| 2 | magnus | 39 | +try: |
| 40 | + from __installed__ import SERVER_LOG_DIR, SERVER_DB_DIR, SERVER_CONFIG_DIR |
||
| 41 | +except ImportError: |
||
| 42 | + SERVER_CONFIG_DIR = os.path.join(RESOURCE_PREFIX, 'config') |
||
| 43 | + SERVER_DB_DIR = CONFIG_DIR |
||
| 44 | + SERVER_LOG_DIR = CONFIG_DIR |
||
| 39 | magnus | 45 | + |
| 2 | magnus | 46 | def ensure_config_dir(dir): |
| 47 | ''' |
||
| 39 | magnus | 48 | Ensure config directory exists |
| 49 | @@ -112,11 +119,14 @@ class InstallLib(install_lib): |
||
| 50 | self.mkpath(os.path.dirname(filename)) |
||
| 51 | |||
| 52 | install = self.distribution.get_command_obj('install') |
||
| 53 | - datadir = os.path.join(install.prefix, 'share', APP_NAME) |
||
| 54 | + datadir = os.path.join(install.prefix, 'share', 'games', APP_NAME) |
||
| 55 | |||
| 56 | fp = open(filename, 'w') |
||
| 57 | fp.write('# Generated by setup.py do not modify\n') |
||
| 58 | - fp.write("RESOURCE_PREFIX = '%s'\n" % datadir) |
||
| 59 | + fp.write("RESOURCE_PREFIX = %r\n" % datadir) |
||
| 60 | + fp.write("SERVER_CONFIG_DIR = %r\n" % install.confdir) |
||
| 61 | + fp.write("SERVER_DB_DIR = %r\n" % install.dbdir) |
||
| 62 | + fp.write("SERVER_LOG_DIR = %r\n" % install.logdir) |
||
| 63 | fp.close() |
||
| 2 | magnus | 64 | |
| 39 | magnus | 65 | return filename |
| 66 | @@ -125,6 +135,35 @@ class InstallLib(install_lib): |
||
| 67 | template = self.generate_template() |
||
| 68 | return install_lib.install(self) + [template] |
||
| 2 | magnus | 69 | |
| 39 | magnus | 70 | +class InstallData(install_data): |
| 71 | + def fix_path(self, item): |
||
| 72 | + install = self.distribution.get_command_obj('install') |
||
| 73 | + if type(item) in (list, tuple): |
||
| 74 | + if 'config' in item[0]: |
||
| 75 | + return (item[0].replace('config', install.confdir), item[1]) |
||
| 76 | + else: |
||
| 77 | + return (item[0].replace('resources', 'share/games/'+APP_NAME), item[1]) |
||
| 78 | + else: |
||
| 79 | + return item |
||
| 80 | + |
||
| 81 | + def finalize_options(self): |
||
| 82 | + self.data_files = [ self.fix_path(f) for f in self.data_files ] |
||
| 83 | + install_data.finalize_options(self) |
||
| 84 | + |
||
| 85 | +class Install(install): |
||
| 86 | + user_options = install.user_options + [ ('logdir=', None, "log directory"), |
||
| 87 | + ('dbdir=', None, "database directory"), |
||
| 88 | + ('confdir=', None, "configuration directory") ] |
||
| 89 | + |
||
| 90 | + def initialize_options(self): |
||
| 91 | + self.logdir = None |
||
| 92 | + self.dbdir = None |
||
| 93 | + self.confdir = None |
||
| 94 | + install.initialize_options(self) |
||
| 95 | + |
||
| 96 | + def finalize_options(self): |
||
| 97 | + install.finalize_options(self) |
||
| 98 | + |
||
| 2 | magnus | 99 | class Resource(object): |
| 100 | ''' |
||
| 101 | Filesystem resource |
||
| 39 | magnus | 102 | @@ -180,4 +219,4 @@ class Resource(object): |
| 103 | @param value: |
||
| 104 | ''' |
||
| 105 | self.data[item] = value |
||
| 106 | - |
||
| 107 | \ No newline at end of file |
||
| 108 | + |
||
| 109 | --- a/pyscrabble/db.py |
||
| 110 | +++ b/pyscrabble/db.py |
||
| 2 | magnus | 111 | @@ -1,5 +1,4 @@ |
| 112 | from pyscrabble import constants |
||
| 113 | -from pyscrabble import manager |
||
| 114 | from ZODB import FileStorage, DB as _DB |
||
| 115 | import transaction |
||
| 116 | |||
| 39 | magnus | 117 | @@ -12,8 +11,10 @@ class DB(object): |
| 2 | magnus | 118 | ''' |
| 119 | Initialize the connection to the DB |
||
| 120 | ''' |
||
| 121 | - r = manager.ResourceManager() |
||
| 122 | - path = r["config"][constants.DB_LOCATION] |
||
| 123 | + import os |
||
| 124 | + from pyscrabble import dist |
||
| 125 | + |
||
| 126 | + path = os.path.join(dist.SERVER_DB_DIR, constants.DB_LOCATION) |
||
| 127 | |||
| 128 | storage = FileStorage.FileStorage(path) |
||
| 129 | db = _DB(storage) |
||
| 39 | magnus | 130 | --- a/pyscrabble/manager.py |
| 131 | +++ b/pyscrabble/manager.py |
||
| 132 | @@ -36,6 +36,7 @@ class ResourceManager(object): |
||
| 2 | magnus | 133 | ''' |
| 134 | self.loaded = True |
||
| 135 | |||
| 136 | + self["serverconfig"] = dist.Resource( dist.SERVER_CONFIG_DIR ) |
||
| 137 | self["config"] = dist.Resource( dist.CONFIG_DIR ) |
||
| 138 | self["resources"] = dist.Resource( dist.RESOURCE_PREFIX ) |
||
| 139 | |||
| 39 | magnus | 140 | --- a/server_console.py |
| 141 | +++ b/server_console.py |
||
| 142 | @@ -76,15 +76,15 @@ class ServerConsole(object): |
||
| 2 | magnus | 143 | ''' |
| 144 | Configure the server |
||
| 145 | ''' |
||
| 146 | - dist.ensure_config_dir(dist.CONFIG_DIR) |
||
| 147 | + dist.ensure_config_dir(dist.SERVER_CONFIG_DIR) |
||
| 148 | resources = manager.ResourceManager() |
||
| 149 | logging.basicConfig(level=logging.DEBUG, |
||
| 150 | format='%(asctime)s %(name)s %(levelname)s %(message)s', |
||
| 151 | - filename=resources["config"][constants.LOG_FILE], |
||
| 152 | - filemode='w') |
||
| 153 | + filename=os.path.join(dist.SERVER_LOG_DIR, constants.LOG_FILE), |
||
| 154 | + filemode='a') |
||
| 155 | |||
| 156 | |||
| 157 | - config = resources["config"][constants.SERVER_CONSOLE_CONFIG] |
||
| 158 | + config = resources["serverconfig"][constants.SERVER_CONSOLE_CONFIG] |
||
| 159 | |||
| 160 | if not os.path.exists(config): |
||
| 161 | raise IOError, "%s must exist in %s" % (constants.SERVER_CONSOLE_CONFIG, resources["config"].path) |
||
| 39 | magnus | 162 | --- a/setup.py |
| 163 | +++ b/setup.py |
||
| 164 | @@ -7,21 +7,12 @@ except ImportError: |
||
| 2 | magnus | 165 | HAS_PY2EXE = False |
| 166 | import glob |
||
| 167 | import os |
||
| 168 | -import pkg_resources |
||
| 169 | +#import pkg_resources |
||
| 170 | import sys |
||
| 171 | from pyscrabble.constants import VERSION |
||
| 172 | from pyscrabble import util |
||
| 173 | from pyscrabble import dist |
||
| 174 | |||
| 175 | -def fix_path(item): |
||
| 176 | - if type(item) in (list, tuple): |
||
| 177 | - if 'config' in item[0]: |
||
| 178 | - return (item[0].replace('config', dist.get_app_data_dir()), item[1]) |
||
| 179 | - else: |
||
| 180 | - return (item[0].replace('resources/', 'share/pyscrabble/'), item[1]) |
||
| 39 | magnus | 181 | - else: |
| 2 | magnus | 182 | - return item |
| 39 | magnus | 183 | - |
| 2 | magnus | 184 | kwargs = { |
| 185 | 'name': 'pyscrabble', |
||
| 39 | magnus | 186 | 'version': VERSION, |
| 187 | @@ -80,7 +71,7 @@ if HAS_PY2EXE and 'py2exe' in sys.argv: |
||
| 188 | # kwargs['data_files'] += dist.getResourceDirs(egg.location, ensureLower=False, basePath=None, outdir='extra') |
||
| 2 | magnus | 189 | else: |
| 190 | kwargs['scripts'] = ['pyscrabble-main.py', 'server_console.py', 'db_upgrade.py'] |
||
| 191 | - kwargs['data_files'] = [fix_path(x) for x in kwargs['data_files']] |
||
| 192 | - kwargs['cmdclass'] = {'install_lib': dist.InstallLib, 'install_scripts' : dist.InstallScripts} |
||
| 193 | + # kwargs['data_files'] = [fix_path(x) for x in kwargs['data_files']] |
||
| 39 | magnus | 194 | + kwargs['cmdclass'] = {'install': dist.Install, 'install_lib': dist.InstallLib, 'install_scripts' : dist.InstallScripts, 'install_data': dist.InstallData} |
| 2 | magnus | 195 | |
| 196 | -setup(**kwargs) |
||
| 197 | \ No newline at end of file |
||
| 198 | +setup(**kwargs) |