(self)
Guess PGDATA and configuration file locations.
Guess PGDATA and configuration file locations.
Guess PGDATA and configuration file locations .
def guess_locations(self):
        """
        Guess PGDATA and configuration file locations.
        """

        # find the PGDATA directory
        if self.options.pgdata:
            self.pgdata = self.options.pgdata
        elif 'PGDATA' in os.environ:
            self.pgdata = os.environ['PGDATA']
        else:
            self.pgdata = "~/%s/main" % DEFAULT_PG_VERSION

        self.pgdata = os.path.expanduser(self.pgdata)
        if not os.path.isdir(self.pgdata):
            die(1, 'Postgres data directory not found: %s' % self.pgdata)

        postmaster_opts = os.path.join(self.pgdata, 'postmaster.opts')
        self.postgres_bin = ""
        self.postgres_conf = ""

        if os.path.exists(postmaster_opts):
            # postmaster_opts exists, attempt to guess various paths

            # get unquoted args from opts file
            cmdline = [ k.strip('"') for k in open(postmaster_opts).read().split() ]

            if cmdline:
                self.postgres_bin = os.path.dirname(cmdline[0])
                cmdline = cmdline[1:]

            for item in cmdline:
                if item.startswith("config_file="):
                    self.postgres_conf = item.split("=")[1]

            if not self.postgres_conf:
                self.postgres_conf = os.path.join(self.pgdata, "postgresql.conf")

        else:
            # no postmaster opts, resort to guessing

            self.log.info('postmaster.opts not found, resorting to guesses')

            # use the directory of first postgres executable from path
            for path in os.environ['PATH'].split(os.pathsep):
                path = os.path.expanduser(path)
                exe = os.path.join(path, "postgres")
                if os.path.isfile(exe):
                    self.postgres_bin = path
                    break
            else:
                # not found, use Debian default
                self.postgres_bin = "/usr/lib/postgresql/%s/bin" % DEFAULT_PG_VERSION

            if os.path.exists(self.pgdata):
                self.postgres_conf = os.path.join(self.pgdata, "postgresql.conf")
            else:
                self.postgres_conf = "/etc/postgresql/%s/main/postgresql.conf" % DEFAULT_PG_VERSION

        if not os.path.isdir(self.postgres_bin):
            die(1, "Postgres bin directory not found.")

        if not os.path.isfile(self.postgres_conf):
            if not self.options.init_slave:
                # postgres_conf is required for master
                die(1, "Configuration file not found: %s" % self.postgres_conf)

        # Attempt to guess the init.d script name
        script_suffixes = [ "9.1", "9.0", "8.4", "8.3", "8.2", "8.1", "8.0" ]
        self.initd_script = "/etc/init.d/postgresql"
        if not os.path.exists(self.initd_script):
            for suffix in script_suffixes:
                try_file = "%s-%s" % (self.initd_script, suffix)
                if os.path.exists(try_file):
                    self.initd_script = try_file
                    break
            else:
                self.initd_script = "%s -m fast -D %s" % \
                    (os.path.join(self.postgres_bin, "pg_ctl"), os.path.abspath(self.pgdata))
def guess_locations ( self ) : # find the PGDATA directory if self . options . pgdata : self . pgdata = self . options . pgdata elif 'PGDATA' in os . environ : self . pgdata = os . environ [ 'PGDATA' ] else : self . pgdata = "~/%s/main" % DEFAULT_PG_VERSION self . pgdata = os . path . expanduser ( self . pgdata ) if not os . path . isdir ( self . pgdata ) : die ( 1 , 'Postgres data directory not found: %s' % self . pgdata ) postmaster_opts = os . path . join ( self . pgdata , 'postmaster.opts' ) self . postgres_bin = "" self . postgres_conf = "" if os . path . exists ( postmaster_opts ) : # postmaster_opts exists, attempt to guess various paths # get unquoted args from opts file cmdline = [ k . strip ( '"' ) for k in open ( postmaster_opts ) . read ( ) . split ( ) ] if cmdline : self . postgres_bin = os . path . dirname ( cmdline [ 0 ] ) cmdline = cmdline [ 1 : ] for item in cmdline : if item . startswith ( "config_file=" ) : self . postgres_conf = item . split ( "=" ) [ 1 ] if not self . postgres_conf : self . postgres_conf = os . path . join ( self . pgdata , "postgresql.conf" ) else : # no postmaster opts, resort to guessing self . log . info ( 'postmaster.opts not found, resorting to guesses' ) # use the directory of first postgres executable from path for path in os . environ [ 'PATH' ] . split ( os . pathsep ) : path = os . path . expanduser ( path ) exe = os . path . join ( path , "postgres" ) if os . path . isfile ( exe ) : self . postgres_bin = path break else : # not found, use Debian default self . postgres_bin = "/usr/lib/postgresql/%s/bin" % DEFAULT_PG_VERSION if os . path . exists ( self . pgdata ) : self . postgres_conf = os . path . join ( self . pgdata , "postgresql.conf" ) else : self . postgres_conf = "/etc/postgresql/%s/main/postgresql.conf" % DEFAULT_PG_VERSION if not os . path . isdir ( self . postgres_bin ) : die ( 1 , "Postgres bin directory not found." ) if not os . path . isfile ( self . postgres_conf ) : if not self . options . init_slave : # postgres_conf is required for master die ( 1 , "Configuration file not found: %s" % self . postgres_conf ) # Attempt to guess the init.d script name script_suffixes = [ "9.1" , "9.0" , "8.4" , "8.3" , "8.2" , "8.1" , "8.0" ] self . initd_script = "/etc/init.d/postgresql" if not os . path . exists ( self . initd_script ) : for suffix in script_suffixes : try_file = "%s-%s" % ( self . initd_script , suffix ) if os . path . exists ( try_file ) : self . initd_script = try_file break else : self . initd_script = "%s -m fast -D %s" % ( os . path . join ( self . postgres_bin , "pg_ctl" ) , os . path . abspath ( self . pgdata ) )